Up | Next | Prev | PrevTail | Tail |
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.
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.
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 \(n^{th}\) coefficient of var in the polynomial exprn.
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(\(\langle \)expression\(\rangle \),\(\langle \)integer1\(\rangle \),\(\langle \)integer2\(\rangle \)) |
\(\longrightarrow \) part(part(\(\langle \)expression\(\rangle \),\(\langle \)integer1\(\rangle \)),\(\langle \)integer2\(\rangle \)) |
and so on, and
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
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.
Up | Next | Prev | PrevTail | Front |