In Maple, mathematical formulae, e.g. things
like , and
are called expressions.
They are made up of symbols, numbers, arithmetic
operators and functions. Symbols are things like sin, x, y, Pi etc.
Numbers include
etc.
The arithmetic operators are
+ (addition),
- (subtraction),
* (multiplication),
/ (division),
and ^ (exponentiation). And examples of functions include
sin(x), f(x,y), min(x1,x2,x3,x4).
For example, the formula
, which is a polynomial,
is input in Maple as
> p := x^2*y + 3*x^3*z + 2; 2 3 p := x y + 3 x z + 2
and the formula is input as
> sin(x+Pi/2)*exp(-x); cos(x) exp(- x)
Notice that Maple simplified to
for us.
Formulae in Maple are represented as expression trees
or DAGs (Directed Acyclic Graphs) in computer jargon.
When we program Maple functions to manipulate formulae, we are
basically manipulating expression trees. The three basic
routines for examining these expression trees are
type, op and nops .
The type function
type( ,
)
returns the value true if the expression is of type
.
The basic types are string, integer, fraction,
float, `+`, `*`, `^`, and function.
The whattype function is also useful for printing out the
type of an expression.
For example, our polynomial
is a sum of 3 terms. Thus
> type( p, integer ); false > whattype(p); + > type( p, `+` ); true
Note the use of the back quote character ` here. Back quotes are used for strings in Maple which contain funny characters like /,. etc. Back quotes are not the same as forward quotes (the apostrophe) ' or double quotes ".
Two other numerical types are rational, and numeric. The type rational refers to the rational numbers, i.e. integers and fractions. The type float refers to floating point numbers, i.e. numbers with a decimal point in them. The type numeric refers to any of these kinds of numbers, i.e. numbers of type rational or float. Maple users will have noticed that Maple distinguishes between exact rational numbers and approximate numbers. The presence of a decimal point is significant! Consider
> 2/3; 2/3 # That is a rational number, the following is a floating point number > 2/3.0; .6666666667
Our example is a sum of terms.
How many terms does it have? The nops function returns the number
of operands of an expression.
For a sum, this means the number of terms in the sum.
For a product, it means the number of terms in the product.
Hence
> nops(p); 3
The op function is used to extract one of the operands of an expression. It has the syntax
op( ,
)
meaning extract the 'th operand of the expression
where
must be in the range 1 to the nops of
. In our example,
this means extract the
'th term of the sum
.
> op(1,p); 2 x y > op(2,p); 3 3 x z > op(3,p); 2
The op function can also be used in the following way
op( ,
).
This returns a sequence of operands of from
to
. For example
> op(1..3,p); 2 3 x y, 3 x z, 2
A useful abbreviation is which is equivalent to
which means create a sequence
of all the operands of
. What about the second term of the sum
?
It is a product of 3 factors
> type(op(2,p),`*`); # It is a product true > nops(op(2,p)); # It has 3 factors 3 > op(1,op(2,p)); # Here is the first factor 3 > op(op(2,p)); # Here is a sequence of all 3 factors 3 3, x , z