Maple is a computer language designed to perform symbolic mathematical calculations or computer algebra. It uses a syntax and symbolic notation which is very close to the normal conventions used in mathematics.
Maple is available on CFS.
To start a Maple worksheet from folder My Documents:
My Documents(double click) worksheet(double click)
To start Maple from scratch with a blank worksheet:
ALl Programs --> CFS Software --> Maple 9.5 --> Maple 9.5 (click)
The fundamental element of the worksheet is the execution group. This ends up looking like a paragraph in the worksheet:
[>
In the following text the > prompt will be used to indicate text which is typed in by the user in response to this prompt.
A complete execution group usually consists of a Maple statement, some descriptive text and an echoed response from Maple.
As you type, characters will be entered into the worksheet at the position of the cursor. You can select the type of input for a line using control characters:
cntrl m start to insert a Maple input (this generates the > prompt) cntrl t start to insert text (a commentary to be read)
By default an execution group starts with the Maple input > prompt.
You can insert a new execution group ([ > ) into the worksheet using
cntrl k insert execution group before cursor cntrl j insert execution group after cursor
The delete key will delete the character immediately to the right of the cursor and the backspace key will delete the character immediately to the left of the cursor.
Use ctrl+F4 to close a worksheet and Alt+F4 to terminate the entire xmaple session. Either way you will get the chance to save your work.
Everything you type in response to the > prompt (usually at the start of an execution group) is treated as input by Maple. When you terminate the input with a semicolon ; (or colon :) and hit <return> Maple attempts to evaluate the expressions it finds. If you use a ; then Maple lists the result, if you use : then this listing is suppressed.
For example try typing the following input line:
> u:=diff(x^3-x,x);
Maple will response with the output
|
Then try
> v:=int(u,x);
Maple will response with the output
|
The diff() is the differentiation function and int() is the integration function. In both cases the first argument is the algebraic expression to be differentiated (or integrated) and the second argument is the independent variable. Notice that when int() calculates an indefinite integral the constant of integration is assumed to be zero.
The expressions manipulated by Maple consist of names, numbers, and operators. The names refer to functions or point to further expressions. The evaluation (and the inherent simplification) of any expression is the main task of Maple but it can be very confusing. All the names (words rather than punctuation and operators) can potentially point to further expressions in Maple. Maple will keep resolving (substituting) names and evaluating the expression until all the name references are complete and all the operators and simplification stages have been executed. This process can be very complicated and time consuming but very powerful. Maple will apply the rules of algebra and other mathematical rules much, much faster than you can.
If you want to suppress the evaluation process you must enclose expressions in forward quotes. For example try:
> v:='int(u,x)';
|
In this case Maple has rewritten the expression in neater notation (i.e. it still recognises the int() function) but no evaluation and/or simplification has occured.
A Maple Worksheet can be saved so that you can continue work at a later date and it can be printed so that you have a full record of your work. Both these options are provided within the ``File'' menu that is situated at the top left hand corner of the Maple window.
When you save a worksheet it should be given a file name with the extension .mws (maple work sheet). When you open a worksheet that was previously saved although the text is fully recovered Maple will be in a reset state. Nothing will be defined so you must work through the various execution groups to set up assignments and so forth before you can resume work where you left-off.
Once your file has entered the print queue you must use a printcard at the printstation situated between the 1st and 2nd year Laboratories to get your hardcopy.
The statement quit can be used to terminate a Maple worksheet
> quit
Note there is no ; (or:) because quit is a complete statement not an expression to be evaluated. When quit is used Maple will NOT give you the chance to save the current worksheet. If you use ctrl+F4 to exit the worksheet and there are definitions in existence then a dialogue box will be generated to give you the opportunity to save your current work.
The function restart can be used to start over again:
> restart;
In this case the ; is required. All assignments etc. will be forgotten. This should not be used within a procedure or a file input by a 'read' statement.
Maple has a comprehensive help facility. You can enter this by clicking with the mouse on the Help option at the top right of the screen. However you can also get help using the following commands which you can type directly from the keyboard. With a little practice you will find this is more convienient because it requires the minimum of hand movement and if you know the name of the function you want information about you can get directly to it without having to navigate down through the help heirarchy.
> ? enter online help > ? topic get help on a specified topic > info(function); enter online help for a function > usage(function); enter online help on how to use a function > examples(function); enter online help examples of use > related(function); enter online help on issues related to function ctrl+F4 close help window
A complete list of the initially-known mathematical functions available can be obtained using:
> ? inifcns
The example statements given above include an assignment using the operator : = . The expression on the right hand side is given the name which is on the left hand side. Whenever the name is encountered in subsequent expressions the assigned expression will be substituted. This is analagous to variable names in Fortran or C. However in Maple almost any complicated symbolic expression can be assigned a name not just numerical or character data.
Remember the expression on the right hand side will be evaluated before the assignment is carried out so if you want to suppress evaluation you must enclose the right hand side in forward (apostrophe) quotes.
You can also use forward quotes to unassign a name. The following expression assigns the value 4 to x:
> x:=4;
|
We just confirm the assignment by getting Maple to evaluate the expression x.
> x;
|
We now unassign x by setting it equal to ¢x¢ (enclosed in forward quotes).
> x:='x';
|
Because the x on the right of the assignment is enclosed in forward quotes it is not evaluated (to produce the value) and the assignment ends up setting x back to the character string 'x'.
We can confirm this by getting Maple to evaluate x again.
> x;
|
The unassign() function can be used to unassign a series of names:
> x:=22: a:=x^2: t:=r: ss:=3:
> unassign('x','a','t','ss');
Back quotes are used to enclose literal strings. For example we can refer to the string + rather than the addition operator by using back quotes:
> `+`;
|
If + is referenced without back quotes then an expression is expected.
> +; Syntax error, `;` unexpected
An expression is still expected if forward quotes are used:
> '+'; Syntax error, `;` unexpected
Or, for example, we can evaluate the sine of a floating point number:
> '+'; > x:=sin(4.0);
|
If we enclose this in back quotes then x is not a floating point number but a string. You can find the type of a Maple expression using the whattype() function.
> x:=`sin(4.0)`;
|
> whattype(x);
|
However if we enclose the expression with forward quotes x will be an expression which has not been evaluated but still has type float.
> x:='sin(4.0)';
|
> whattype(x);
|
The double-quotes or ditto symbol is used to refer to previous results. A single ditto refers to the most recent, 2 dittos refer to the second most recent and 3 dittos refer to the third most recent:
> x:='cos(4.0)';
|
> 5*";
|
> 6*"";
|
This can be useful if you are calculating something in sections but frankly I find this facility a bit of a waste of time. If you make a mistake it can be very difficult to work out what went wrong or more seriously you may not spot you made a mistake at all.
Maple is often used to solve polynomial equations. The main facility for doing this is the function solve(). For example to find the roots of a quadratic in x:
> solve(a*x^2+b*x+c,x);
Notice that it is assumed that the expression = 0 although this could be added (see section on equations below).
Each expression is represented internally as a tree of nodes with a fundamental data type at each node. An operand of an expression can be found using the function op() and the number of operands can be found using nops(). The type of an expression is returned by whattype() and you can test for specific type using type(). Consider the following:
> z:=x*(sin(y)+4);
|
> whattype(z);
|
> nops(z);
|
> op(1,z);
|
> o2:=op(2,z);
|
> whattype(x);
|
> whattype(o2);
|
> nops(o2);
|
> whattype(op(1,o2));
|
> type(op(1,o2),`function`);
|
...
You can see that the tree structure of an expression can gradually be elucidated by analysing each subexpression until nops(op(1,arg)) returns 1. Then whattype() can be used to find the final type of that branch of the tree. You can also use the has() and hastype() functions to look for a specific subexpression or specific type within an expression.
> has(z,sin);
|
> has(z,cos);
|
> hastype(z,function);
|
There are many different types defined in Maple. You will come across them as you gain familiarity and experience with the system. In the meantime some idea of how expressions are parsed and how Maple uses different types is useful in understanding error messages and debugging code.
An expression sequence has the form:
> sq:=a,b,c,d;
where a, b, c and d can be just names or may be more complicated expressions.
A set in Maple is an expression sequence enclosed in curly braces
> st:={a,b,c,d};
Because of the mathematical definition of a set any duplicates are removed and the order is not important.
A list is an expression sequence enclosed in square braces
> ls:= [a,b,c,d]
In this case the order is important and duplicates are allowed.
The selector operator, consisting of square braces enclosing an index or range, is used to pick out elements from structured data (a set, list or array).
> d:={x,y,z};
|
> st:={a,b,c,d};
|
> st[2];
|
A range is specified as follows
> st[2..4];
|
A range is itself an expression and can therefore be assigned to a name
> fromto:=2..4;
|
> st[fromto];
|
You can dig deeper into a structure by chaining selector operators one after the other
> st[4][2];
|
An array in Maple is a special form of a more general structure called a table. Arrays are tables which are indexed using integers (subscripts). Arrays are created using the array() function. For example a 2 by 2 matrix of floating point constants can be created as
> r:=array(1..2,1..2,[[1.1,2.2],[3.3,4.4]]);
|
Note that the entries are defined by a list of lists in the appropriate order.
Also note that the functions op() and nops() can be used on lists and sets:
> li:=[sqrt(5),sqrt(6)];
|
> nops(li);
|
> ll:=[op(li),sqrt(7)];
|
> nops(ll);
|
The nops() function returns the number of items. op() can be used with just a single argument to return the expression sequence from a list in order to augment the list.
The subop() function can be used to substitute an item in a list. If it is used with a NULL entry a list item can be remove completely. Continuing from the previous section:
> subop(1=NULL,ll);
|
More generally subop() can also be used to change an operand in any expression.
You can also change entries in a list by using an assignment and selection operator:
> ll[2]:=sqrt(10):ll;
|
The seq() function provides a neat way of generating expression sequences which have a generating function:
> seq(i^2,i=1..10);
|
Instead of using a range in the second argument a list can be used:
> li:=[3,7,11]: > seq(i^2,i=li);
|
So, for example, you can generate the first few terms of the exponential function using seq():
s:=seq((x^i)/i!,i=0..4);
|
The map() function can be used to apply a function to items in a list individually:
> map(sqrt,[s]);
|
Note that the second argument must be a list so s (which is an expression sequence) must be enclosed in square brackets [list].
If the function takes 2 arguments then the 2nd argument can be added after the list argument. For example the function irem(n,m) finds the remainder after division n/m:
map(irem,[18,25,38],6);
|
If the list is to be used for the second argument then the function map2() must be used:
map2(irem,31,[2,3,4]);
|
Now the divisor has been specified by the list.
The arithmetic operators available are similar to Fortran or C.
+ addition - subtraction and negation(unary minus) * multiplication / division ^ or ** exponentiation (raising to a power)
Two names can be concatenated using the dot operator
> this . that;
|
A sequence of names (an expression sequence) can be generated using the dot operator with a range
> num . (1..9);
|
The operand to the left of the dot operator must be a name and the operand on the right must be an expression. In this case the expression is a range which behaves like an implied Do Loop in Fortran.
The series() command can be used to produce the Taylor or related expansions of well known functions
> s:=series(sin(x),x);
| (1) |
This produces the Maple type ``series''. It is like a normal polynomial but includes an order term. A third argument can be included to specify the order term.
> s:=series(cos(x),x,8);
| (2) |
The series type cannot be manipulated in quite the same way as a normal polynomial (because of the handling of the order term). You can remove the order term and produce a polynomial using convert()
> s:=convert(series(sinh(x),x,10),polynom);
| (3) |
The same polynomial can be generated using the sum() function to produce a symbolic summation of a series of terms
s:=sum(x^(2*i+1)/(2*i+1)!,i=0..4);
| (4) |
Note the way in which the range i = 0..4 acts like an implied Fortran do loop.
Automatic evaluation of expressions doesn't always produce what you expect or what you intended. To force a particular form of result you may have to use an evaluator function. For example if the result of automatic evaluation is an array name the contents of the array are not normally listed (i.e. evaluated) but the eval() function will produce a listing:
> r:=array(1..2,1..2,[10,20],[30,40]): > r;
|
> eval(r);
|
The default arithmetic used during evaluation is exact (rational). You can evaluate expressions using floating-point arithmetic using the evalf() function:
> sin(1/2);
|
> evalf(sin(1/2));
|
The complete list of evaluators available is:
eval(expr) force evaluation of expression evala(expr) for algebraic functions evalb(expr) to Boolean expression evalc(expr) using complex numbers evalf(expr) using floating-point arithmetic evalm(expr) for matrix expressions evaln(expr) to a name
An equation is a Maple type consisting of two expressions separated by an equals sign. We have already seen them in use, for example:
> subop(1=NULL,ll); > seq(i^2,i=li);
Equations can appear in an assignment and they don't have to be true:
> s:=3=4;
|
> s;
|
> evalb(s);
|
The Boolean evaluator can be used to test whether an equation is true of false. The left hand side and right hand side of an equation are returned by the functions lhs() and rhs():
> lhs(s);
|
> rhs(s);
|
The function assign() is used to convert an equation into an assignment:
> z:=y=x^2;
|
> assign(z); > y;
|
Note that the function assign() returns a NULL value and therefore nothing is listed.
The function subs() can be used to substitute a particular value into an expression without altering the original expression:
y:=sin(x)^2;
|
> subs(x=4,y);
|
> y;
|
Technically subs() performs a syntactical substitution. If some algebra is required to perform the substitution then the function algsubs() must be used. The difference should be clear from the following:
> subs(x^2=4,x^3);
|
> algsubs(x^2=4,x^3);
|
An equation argument is also employed by the alias() function which is used to set up name aliases. For example we can define an alias for the GAMMA function:
> alias(G=GAMMA)
|
The function responds by returning all the current aliases; I is the default alias for sqrt(-1). To remove the alias use:
> alias(G=G)
|
Note you cannot alias() an alias().
In mathematics it is often the case that you have to assume something about the variables or whatever in order to get the required answer. For example there are two possible roots using the sqrt() function and so Maple will not simplify the following:
> sqrt(x^2);
|
If we want the negative root we can use:
> assume(x<0); > about(x); originally x, renamed x~: is assumed to be: RealRange(-infinity,Open(0)) > sqrt(x^2);
|
Maple appends the twiddles character to the name to indicate that it has attributes. The about() function can be used to list the attributes. In the above example x is uniquely defined as the answer to the sqrt() function, under the restriction that x is negative, and so simplification can be achieved.
Many functions have an inert version which starts with an uppercase (capital) letter. These are recognised during evaluation and produce the correct notation on output but don't cause any further action.
> diff(x^2,x);
|
> Diff(x^2,x);
|
In some cases or contexts an evaluator will take the Inert function as a request to calculate (or evaluate) in the same way as the usual function would in an expression. For example using Int() rather than int() forces evalf() to perform a numerical integration rather than an analytic integration.
> int(sin(x),x=1..2);
|
> Int(sin(x),x=1..2);
|
> evalf(Int(sin(x),x=1..2));
|
If Maple has to invent a new variable to produce the answer to something it will use names prefixed with underscore.
> solve(x^4+x-1);
|
Maple is unable to find an explicit formula for the roots of the 4th order polynomial above so it introduces a new name _Z and describes the answer using the RootOf function.
In some cases Maple will set it's own assignments when producing a result. Such names are usually an integer prefixed by %.
|
Try performing the following assignment before using the solve() function above:
> _EnvExplicit:=true: > solve(x^4+x-1);
The following names are also used in some circumstances:
_Z~ an arbitary integer _NN~ an arbitary non-negative integer _B~ a binary value 0 or 1
The functions in Maple are held in a library which has four parts or levels. The so-called kernel which performs the basic level manipulation is always present. On the next level are demand-loaded functions which are automatically accessed when required. The third level contains functions which must be manually loaded. This is done using the readlib() function:
> readlib(randpoly);
|
The argument of readlib is the function to be loaded, in this case randpoly(). Note it is sometimes a good idea to terminate the readlib() expression with a colon to prevent unnecessary/unwanted listings.
The fourth and largest level of the library is organised into packages which need to be loaded before they can be used. A specific function from a package can be used (without loading the entire package) using the following syntax:
> combinat[fibonacci](8);
|
The package name (in this case combinat) is in fact a table which is indexed using the function name (in this case fibonacci). The appropriate argument list for the function must follow enclosed in parentheses.
To load a package in its entirety use the with() command. For example to make the combinat package available use:
> with(combinat);
[Chi,bell,binomial,.........
........
subsets,vectoint]
> fibonacci(8);
21
The function with() returns the list of available functions (enclosed in square brackets as a maple list should be). In this example the function fibonacci() can then be used in the usual way.
It is very easy to plot functions over a specified range of the independent variable. For example, suppose we want to plot the Lorenzian line profile of a spectral line (due to natural broadening):
|
The width of the line is specified by w, the centre of the line is specified by x0 and a is a normalisation constant.
Firstly we define the function
> g:=a/((x-x0)^2+w^2);
|
and then we substitute values for the constants using the subs() function and draw the function using the plot() command.
> f:=subs(a=2,x0=5,w=0.5,g); > plot(f,x=1..10);
The result is shown in figure 1.
By default the plot is displayed ``inline'' within the execution group of the worksheet containing the plot command. However plots can be created in their own window using the ``Plot display'' in the Options menu.
In the example above the normalisation constant a can be defined such that the total integral under the curve is unity. We can find this value using the int() function.
> int(g,x);
|
Substituting limits of -¥ and +¥ gives
|
Actually we can confirm this by specifying a range for x in the int() function thereby calculating a definite integral
> int(g,x=-infinity..infinity);
|
The csgn() function simply returns a sign depending on the position of w in the complex plane. In the present example w is real and positive and so csgn() = +1.
Therefore if we let a = w/p the integral under the Lorenzian curve will be unity.
> g:=subs(a=w/Pi,g);
|
Now we can use the plot() command to draw a set of Lorenzian curves with different widths but all normalized. Firstly we generate a set of expressions (note the use of curly brackets to create a set) using the seq() function and then we plot the set.
> fs:={seq(subs(x0=5,w=0.1*i,g),i=1..10)}:
> plot(fs,x=1..10);
The resulting plot is shown in figure 2.
There are several ways to plot functions of two variables as three dimensional pictures. The simplest is the plot3d() command. For example we can plot the normalised Lorenzian as a function of x and w.
> plot3d(subs(x0=5,g),x=1..11,w=0.1..1.0,grid=[30,30],axes=FRAME);See figure 3 for the resulting plot.
cntrl+m start to insert a Maple input (this generates the > prompt) cntrl+t start to insert text (a commentary to be read) cntrl+p to move to previous line (up) cntrl+n to move to next line (down) cntrl+b to move back along a line (left) cntrl+f to move forward along a line (right) cntrl+k insert execution group before cursor cntrl+j insert execution group after cursor backspace delete character left of cursor delete delete character right of cursor cntrl+delete delete the paragraph containing cursor > quit statement to close the current worksheet (no dialogue box) > restart; function to start over, all assignments etc. forgotten ctrl+F4 close the worksheet in focus (with dialogue box) Alt+F4 exit from xmaple
> ? enter online help > ? topic get help on a specified topic > info(function); enter online help for a function > usage(function); enter online help on how to use a function > examples(function); enter online help for examples of use > related(function); enter online help on issues related to function > ? inifcns list of initially-known mathematical functions ctrl+F4 close Help window in focus
> a:='expression' forward quotes (apostophe) to prevent evaluation > b:=`string` backward quotes to enclose strings " single ditto to refer to latest result "" and """ double and triple dittos for 2nd and 3rd latest
> execution group prompt > statement; execute statement and output a result > statement: execute statement but don't output result > expression; evaluate expression (and output result) > name:=expression; assignment (and output result) > save 'filename': save Maple results in text file > save 'filename.m': save Maple results in internal Maple format file > read 'filename': read Maple results from text file > read 'filename.m': read Maple results from internal Maple format file > quit exit from a Maple worksheet > selections statements like if and elif (within procedures) > repetitions statements like do and for (within procedures) > 0; empty statement (zero followed by ; or :) > ; empty statement (spaces followed by ; or :)
> nops(expression); number of operands in expression > op(n,expression); nth operand of expression > whattype(expression); the type of an expression > type(expression,type); test if expression of given type (returns boolean) > hastype(expression,type); test if expression contains specific type > has(expression,subexp); test if expression contains subexpression > subsop(n=new,expression); substitute new for nth operand in expression > subs(x=a,expr); substitute subexpressions into an expression > algsubs(a=c,f); algebraic substitution into an expression > convert(expr,form,arg3,...) convert an expression to a different form > simplify(expr) apply simplification rules to an expression > normal(expr) simplify to numerator/denominator form > normal(expr,expanded) expand polynomials in normal rational form
> function(); parentheses are used to enclose function arguments
> e:=a,b,c,d,a; an expression sequence (can be assigned to name)
> s:={a,b,c}; {} enclose a set (no repeats, order unimportant)
> li:=[a,d,b,s,z,a]; [] enclose a list (repeats allowed, order important)
> li[3]; select an element from a list (or set)
> li[3..5]; select a range from a list (or set)
> li[4][2]; chain selectors to dig into structure
> rg:=1..4; assign name to range
> li[rg]; select using name of range
> r:=array(1..4,1..2); define an array using the array() function
> r:=array(1..2,1..2,[[1.0,2.0],[3.0,4.0]]);
initialize an array using lists
> name.(range) generate sequence of names with range (see operators)
> seq(i^2,i=1..5); generate an expression sequence using formula and range
> map(fun,list); apply function to items in list
> map(fun,list,arg2); apply function with another argument to items in list
> map2(fun,arg1,list); apply function with another argument to items in list
> subsop(n=NULL,list); delete nth item from a list
_name prefix underscore used by Maple for internal names %n names for assignments set by Maple _Z~ an arbitary integer _NN~ an arbitary non-negative integer _B~ a binary value 0 or 1 I square root of -1 infinity for use as limits etc. + addition - subtraction and negation(unary minus) * multiplication / division ^ or ** exponentiation (raising to a power) > name.expression dot operator, concatenate name with expression > name.(range) dot operator, generate sequence of names with range > assume(x>0); attribute properties to name (in this case x)
array(1..3,1..4) defining an array (see structured types above)
name.(range) with dot operator (see operators above)
seq(i^2,i=1..5) in seq() function (see structured types above)
> int(sin(x),x=1..2); limits of definite integrals
> add(k^2,k=1..10); limits of summations
> plot(tan(x),x=-2..2,-6..6);
plotting intervals
eval(expr) force evaluation of expression evala(expr) for algebraic functions evalb(expr) to Boolean expression evalc(expr) using complex numbers evalf(expr) using floating-point arithmetic evalm(expr) for matrix expressions evaln(expr) to a name
kernel and demand-loaded functions are available automatically > readlib(function); manually load a function > linalg[vector](args); use function vector() from package linalg > with(linalg); load complete package (to avoid using above syntax)
diff(f,x) differentiate f wrt x diff(f,x$2) differentiate f wrt x twice (2nd derivative) diff(f,x$n) differentiate f wrt x n times (nth derivative) int(f,x) integrate f wrt x int(f,x=a..b) definite integral of f over specified range sin(x),cos(x),tan(x) trigonometric functions (argument radians) sec(x),csc(x),cot(x) reciprocal trigonometric functions (argment radians) sinh(x),cosh(x),tanh(x) hyperbolic functions sech(x),csch(x),coth(x) reciprocal hyperbolic functions arcsin(x) etc. inverse trigonometrical functions (result in radians) arcsinh(x) etc. inverse hyperbolic functions Re(expr) real part of a complex expression Im(expr) imaginary part of a complex expression