maple_as_programming_language.mws

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;

3.141592654

6.283185308

9.424777962

Unfortunately Maple's authors have chosen to give a loop index global scope.

> i;

4

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'):

3.141592654

6.283185308

9.424777962

One of the most useful data structures is the sequence: a comma-separated list of expressions:

> s:=1,seven,x^2;

s := 1, seven, x^2

Elements can be accessed as follows:

> s[2];

seven

My very favorite Maple command is seq:  for generating sequences.

> seq(evalf(i*Pi),i=1..3);

3.141592654, 6.283185308, 9.424777962

> seq(A[i],i=0..7);

A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7]

> seq(i^2,i=[2,10,-7]);

4, 100, 49

> plots[display](seq(plot(sin(n*x),x=0..Pi/2),n=1..3));

[Plot]

Associative arrays can be created simply by assigning an element:

> for i from 0 to 3 do
  A[i]:=i*x;

od;

A[0] := 0

A[1] := x

A[2] := 2*x

A[3] := 3*x

A sequence enclosed by square brackets is a Maple list:

> [%];

[3.141592654, 6.283185308, 9.424777962]

Lists can be used as vectors: simple arithmetic (usually) distributes over them as you might hope,

> u:=[2,x,y];
7*u;

u := [2, x, y]

[14, 7*x, 7*y]

> v:=[3,4,5];
u+v;

v := [3, 4, 5]

[5, 4+x, 5+y]

though sometimes you need to use expand to get Maple to do what you want:

> t*v;

t*[3, 4, 5]

> expand(t*v);

[3*t, 4*t, 5*t]

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;

w := [sin(t), 4*t, 5]

[2*sin(t), 8*t, 10]

[2*sin(t)+1, 8*t+2, 13]

and while diff distributes over lists, int does not:

> r:=[t,t^2,t^3];
diff(r,t);

int(r,t);

r := [t, t^2, t^3]

[1, 2*t, 3*t^2]

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;

a := 3

hello

> if(a>3) then
  print(hello);

else

  print(goodbye);

fi;

goodbye

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);

f := x^2-x*y^2

g := 2*x-y^2

-5

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);

f := proc (x, y) options operator, arrow; x^2-x*y^2 end proc

g := proc (x, y) options operator, arrow; 2*x-y^2 end proc

-5

Functions can be made from expressions using unapply:

> F:=x^2-x*y^2;
f:=unapply(F,x,y);

F := x^2-x*y^2

f := proc (x, y) options operator, arrow; x^2-x*y^2 end proc

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);

f := proc (x, y) foo; bar; x^2-x*y^2 end proc

g := proc (x, y) 2*x-y^2 end proc

-5

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);

cos(2)*sin(1), -sin(1), g(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.

>