| ||||||||||||||||||
Expression Manipulation
Here are some useful tools for manipulating expressions and changing their appearance.
expand(expr)
This expands an expression by multiplying out products. For example, the expression (3+x)*(2+x) is
automatically 'simplified' to (2+x)*(3+x) but expand((3+x)*(2+x)) produces the
result 6 + 5*x + x^2.
subs(expression, old = new)
Given an expression, an old term and a new term, this function replaces the old term with the new term in the expression.
For example, subs(x*(1+x), x=2*y + 3) gives the result (3 + 2*y) * (4 + 2*y).
subs searches the expression given as its first argument for complete sub-expressions which
match the left hand side of the equation in the second argument. Therefore, subs(a + b*x*z, b*z=c) does
nothing, as b*z is not a complete sub-expression of a + b*x*z.
To achieve the required effect, use subs(a+b*x*z, b=c/z), which
produces a + c*x.
The one exception to this rule is that if the item to be replaced is a symbol which has the same name as a function in the main expression, and the replacement item is also a symbol, then the name of the function will be changed.
The subs function's second argument is either an equation
giving the substitution to be carried out, or it can be a list of such
equations. (A list is a series of expressions separated by commas, the whole
being enclosed inside square brackets, [].) When a list is supplied, the
substitutions are performed concurrently.
Example:
subs(a^2*b, [a = b+1, b = a+2]) gives (2 + a) * (1 + b)^2.
collect(expr)
This collects together like terms of an expression. See collect.
showpoly and showseries
These rearrange an expression in order to show it more clearly as a polynomial or power series. See showpoly and showseries.
rational
This function transforms an algebraic expression to an equivalent expression in rationalised form, but does not simplify the result. For more information, see rational expressions.
Example:
rational(a/b + b/c) produces (b^2 + a*c)/b/c.
type(expr)
This function returns a symbol whose name reflects the type of the expression, such as a sum or product.
Examples:
type(a^b) returns the symbol Power.
type(2/3) returns the symbol Fraction.
nops(expr)
This function returns the number of operands contained in the expression.
Examples:
type(a + b + c) returns 3.
type(3 * x * 4) returns 2, having first simplified the expression to 12 * x.
operand(expr, n)
This function returns one of the operands of an expression. It returns the symbol Undefined if the operand does not exist.
Examples:
operand(5 + a*b, 1) returns 5.
operand(5 + a*b, 2) returns a * b.
operand(5 + a*b, 3) returns Undefined.
operand(sin(2 * x, 1)) returns 2 * x.
