Maple as a programming language (Last updated by JR, 8/19/2004)
Copyright 2000-2005, SUNY Buffalo.
The for loop:
| > | for i from 1 to 3 do
evalf(i*Pi); od; |
Unfortunately Maple's authors have chosen to give a loop index global scope.
| > | i; |
As the designers of C++ finally concluded, this is almost always undesirable,
so I recommend the practice of unassigning the loop index:
| > | for i from 1 to 3 do
evalf(i*Pi); od; unassign('i'): |
One of the most useful data structures is the sequence: a comma-separated list of expressions:
| > | s:=1,seven,x^2; |
Elements can be accessed as follows:
| > | s[2]; |
My very favorite Maple command is seq: for generating sequences.
| > | seq(evalf(i*Pi),i=1..3); |
| > | seq(A[i],i=0..7); |
| > | seq(i^2,i=[2,10,-7]); |
| > | plots[display](seq(plot(sin(n*x),x=0..Pi/2),n=1..3)); |
![[Plot]](images/maple_as_programming_language_13.gif)
Associative arrays can be created simply by assigning an element:
| > | for i from 0 to 3 do
A[i]:=i*x; od; |
A sequence enclosed by square brackets is a Maple list:
| > | [%]; |
Lists can be used as vectors: simple arithmetic (usually) distributes over them as you might hope,
| > | u:=[2,x,y];
7*u; |
| > | v:=[3,4,5];
u+v; |
though sometimes you need to use expand to get Maple to do what you want:
| > | t*v; |
| > | expand(t*v); |
and, there is a nasty bug in Maple 6, fixed in Maple 7, that gets the following just plain wrong:
| > | w:=[sin(t),4*t,5];
expand(2*w); [1,2,3]+2*w; |
and while diff distributes over lists, int does not:
| > | r:=[t,t^2,t^3];
diff(r,t); int(r,t); |
Error, (in int) wrong number (or type) of arguments
Conditional execution is achieved with the if construct:
| > | a:=3;
if(a=3) then print(hello); fi; |
| > | if(a>3) then
print(hello); else print(goodbye); fi; |
Sometimes, it's convenient to use expressions as functions in the following way:
| > | f:=x^2-x*y^2;
g:=diff(f,x); subs(x=2,y=3,g); |
Other times, you may find it more convenient to use true functions:
| > | f:=(x,y)->x^2-x*y^2;
g:=D[1](f); g(2,3); |
Functions can be made from expressions using unapply:
| > | F:=x^2-x*y^2;
f:=unapply(F,x,y); |
Procedures are similar to functions, and are suitable for functions that require multiple lines of
code for their evaluation. The last-evaluated expression is the return value of the procedure.
| > | f:=proc(x,y)
foo; bar; x^2-x*y^2; end; g:=D[1](f); g(2,3); |
Avoiding the annoying "cannot determine if this expression is true or false" error in functions that have conditionals:
| > | g1:=proc(x,xi)
if x < xi then cos(xi)*sin(x) else sin(xi)*cos(x) end if end proc: g1(1,2),g1(1,Pi); |
Error, (in g1) cannot determine if this expression is true or false: -Pi < -1
| > | g1(a,b); |
Error, (in g1) cannot determine if this expression is true or false: a-b < 0
| > | g2:=proc(x,xi) # (avoids "cannot determine if this expression is true or false" error)
if type(evalf(x),numeric) and type(evalf(xi),numeric) then if evalf(x)< evalf(xi) then cos(xi)*sin(x) else sin(xi)*cos(x) end if else 'g'(x,xi) # return function unevaluated fi: end proc: g2(1,2),g2(1,Pi),g2(a,b); |
One last tip
When typing nested commands, I find it saves a great deal of tedious debugging if I always close my parentheses before typing
the thing enclosed by them. E.g. type "seq()", then backspace and put in the contents.
| > |