introMaple.mws

 Iterative constructs (for, while)

>    for i from 1 to 4 do i^2; od;

1

4

9

16

>    l:=[];

l := []

>    for i from 1 to 4 do l:=[op(l),i^2]; od;

l := [1]

l := [1, 4]

l := [1, 4, 9]

l := [1, 4, 9, 16]

>    i:=1;

i := 1

>    while i+2 < 6 do i:=i+1: od;

i := 2

i := 3

i := 4

 Functions

>    f:=x->x+1;

f := proc (x) options operator, arrow; x+1 end proc

>    f(2);

3

>    type(f,'function');
type(cos(x),'function');

false

true

>    whattype(f);

symbol

    The operators @  and @@  are used to compose functions

>    (f@f)(2);

4

>    (f@@5)(2);

7

>    g:=y->y+2;

g := proc (y) options operator, arrow; y+2 end proc

>    (f@g)(x);

x+3

>    ((f@g)@@3)(x);

x+9

>    h:=(x,y,z)->x^2+y^2-z^2;

h := proc (x, y, z) options operator, arrow; x^2+y^2-z^2 end proc

>    h(3,4,5);

0

  Recursive functions (factorial, Fibonacci)

>    fact:=n->if n = 0 then 1; else n*f(n-1); fi;

fact := proc (n) options operator, arrow; if n = 0 then 1 else n*f(n-1) end if end proc

>    fact(3); fact(7);

9

49

>    fibo:=n->if n = 0 then 0
    elif n = 1 then 1
    else fibo(n-1)+fibo(n-2);
fi;

fibo := proc (n) options operator, arrow; if n = 0 then 0 elif n = 1 then 1 else fibo(n-1)+fibo(n-2) end if end proc

>    seq(fibo(i),i=0..12);

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144

>    phi := (1+sqrt(5))/2;  
Phi := (1-sqrt(5))/2;

phi := 1/2+1/2*5^(1/2)

Phi := 1/2-1/2*5^(1/2)

>    fibo1:=n->(phi^n-Phi^n)/(sqrt(5));

fibo1 := proc (n) options operator, arrow; (phi^n-Phi^n)/sqrt(5) end proc

>    fibo1(100);

1/5*((1/2+1/2*5^(1/2))^100-(1/2-1/2*5^(1/2))^100)*5^(1/2)

>    expand(%);

354224848179261915075

>    seq(fibo(i)-expand(fibo1(i)),i=1..10);

0, 0, 0, 0, 0, 0, 0, 0, 0, 0

>    time(fibo(25));

4.270

>    time(expand(fibo1(500)));

4.910

 Procedures

>    f:=proc(x) x^2; end;

f := proc (x) x^2 end proc

>    f(3);

9

>    ff:=proc(n,g)
   local aux,i;
      aux:=[seq(i^2,i=1..n)];
      map(g,aux);
end proc;

ff := proc (n, g) local aux, i; aux := [seq(i^2,i = 1 .. n)]; map(g,aux) end proc

>    ff(3,f);

[1, 16, 81]

  The option remember updates the remember table

>    fib := proc(n) option remember;
    if n<2 then n
           else fib(n-1)+fib(n-2) end if;
end proc;

fib := proc (n) option remember; if n < 2 then n else fib(n-1)+fib(n-2) end if end proc

>    st:=time():
fib(100);
time()-st;

354224848179261915075

0.

  Accessing the remember table

>    op(4,eval(fib));

TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...
TABLE([60 = 1548008755920, 0 = 0, 61 = 2504730781961, 1 = 1, 62 = 4052739537881, 2 = 1, 63 = 6557470319842, 3 = 2, 4 = 3, 64 = 10610209857723, 5 = 5, 65 = 17167680177565, 6 = 8, 66 = 27777890035288, 7 ...

>    op(4,eval(cos));

TABLE([0 = 1, 1/6*Pi = 1/2*3^(1/2), -x = cos(x), 1/2*Pi = 0, Pi = -1, -infinity = undefined, 1/4*Pi = 1/2*2^(1/2), 1/3*Pi = 1/2, infinity = undefined, x = cos(x), I = cosh(1)])

>    cos(Pi/12):=1/4*sqrt(6)+1/4*sqrt(2);

cos(1/12*Pi) := 1/4*6^(1/2)+1/4*2^(1/2)

>    op(4,eval(cos));

TABLE([0 = 1, 1/6*Pi = 1/2*3^(1/2), -x = cos(x), 1/2*Pi = 0, Pi = -1, -infinity = undefined, 1/12*Pi = 1/4*6^(1/2)+1/4*2^(1/2), 1/4*Pi = 1/2*2^(1/2), 1/3*Pi = 1/2, infinity = undefined, x = cos(x), I =...

>    cos(Pi/12);

1/4*6^(1/2)+1/4*2^(1/2)