REDUCE

8.5 Obtaining Parts of Algebraic Expressions

There are many occasions where it is desirable to obtain a specific part of an expression, or even change such a part to another expression. A number of operators are available in REDUCE for this purpose, and will be described in this section. In addition, operators for obtaining specific parts of polynomials and rational functions (such as a denominator) are described in another section.

8.5.1 COEFF Operator

Syntax:

        COEFF(EXPRN:polynomial,VAR:kernel)

COEFF is an operator that partitions EXPRN into its various coefficients with respect to VAR and returns them as a list, with the coefficient independent of VAR first.

Under normal circumstances, an error results if EXPRN is not a polynomial in VAR, although the coefficients themselves can be rational as long as they do not depend on VAR. However, if the switch RATARG is on, denominators are not checked for dependence on VAR, and are taken to be part of the coefficients.

Example:

        coeff((y^2+z)^3/z,y);

returns the result

          2  
        {Z ,0,3*Z,0,3,0,1/Z}.

whereas

        coeff((y^2+z)^3/y,y);

gives an error if RATARG is off, and the result

          3        2  
        {Z /Y,0,3*Z /Y,0,3*Z/Y,0,1/Y}

if RATARG is on.

The length of the result of COEFF is the highest power of VAR encountered plus 1. In the above examples it is 7. In addition, the variable HIGH_POW is set to the highest non-zero power found in EXPRN during the evaluation, and LOW_POW to the lowest non-zero power, or zero if there is a constant term. If EXPRN is a constant, then HIGH_POW and LOW_POW are both set to zero.

8.5.2 COEFFN Operator

The COEFFN operator is designed to give the user a particular coefficient of a variable in a polynomial, as opposed to COEFF that returns all coefficients. COEFFN is used with the syntax

        COEFFN(EXPRN:polynomial,VAR:kernel,N:integer)

It returns the nth coefficient of VAR in the polynomial EXPRN.

8.5.3 PART Operator

Syntax:

        PART(EXPRN:algebraic[,INTEXP:integer])

This operator works on the form of the expression as printed or as it would have been printed at that point in the calculation bearing in mind all the relevant switch settings at that point. The reader therefore needs some familiarity with the way that expressions are represented in prefix form in REDUCE to use these operators effectively. Furthermore, it is assumed that PRI is ON at that point in the calculation. The reason for this is that with PRI off, an expression is printed by walking the tree representing the expression internally. To save space, it is never actually transformed into the equivalent prefix expression as occurs when PRI is on. However, the operations on polynomials described elsewhere can be equally well used in this case to obtain the relevant parts.

The evaluation proceeds recursively down the integer expression list. In other words,

PART(expression,integer1,integer2)
PART(PART(expression,integer1),integer2)

and so on, and

PART(expression)expression.

INTEXP can be any expression that evaluates to an integer. If the integer is positive, then that term of the expression is found. If the integer is 0, the operator is returned. Finally, if the integer is negative, the counting is from the tail of the expression rather than the head.

For example, if the expression a+b is printed as A+B (i.e., the ordering of the variables is alphabetical), then

        part(a+b,2)  ->   B  
        part(a+b,-1) ->   B  
and  
        part(a+b,0)  ->  PLUS

An operator ARGLENGTH is available to determine the number of arguments of the top level operator in an expression. If the expression does not contain a top level operator, then -1 is returned. For example,

        arglength(a+b+c) ->  3  
        arglength(f())   ->  0  
        arglength(a)     ->  -1

8.5.4 Substituting for Parts of Expressions

PART may also be used to substitute for a given part of an expression. In this case, the PART construct appears on the left-hand side of an assignment statement, and the expression to replace the given part on the right-hand side.

For example, with the normal settings of the REDUCE switches:

        xx := a+b;  
        part(xx,2) := c;   ->  A+C  
        part(c+d,0) := -;   -> C-D

Note that xx in the above is not changed by this substitution. In addition, unlike expressions such as array and matrix elements that have an instant evaluation property, the values of part(xx,2) and part(c+d,0) are also not changed.