Arman Akbarian
UNIVERSITY OF BRITISH COLUMBIA
PHYSICS & ASTRONOMY DEPT.

> #Definition of a Procedure in Maple:

hello := proc()
 

> print("Hello World");
 

> end proc;
 

`:=`(hello, proc () print( (1)
 

> hello();
 

Hello World (2)
 

> f:= proc(x)
 

> local a; #a will be a local variable inside the procedure
 

> a := 1/3;
 

> evalf(x*a);
 

> end proc;
 

>
 

`:=`(f, proc (x) local a; `:=`(a, `/`(1, 3)); evalf(`*`(x, `*`(a))) end proc) (3)
 

> f(5);
 

1.666666667 (4)
 

> f();
 

Error, invalid input: f uses a 1st argument, x, which is missing
 

> #Types in Maple:

whattype(a);
 

symbol (5)
 

> whattype(2);
g:= x->x^2;

whattype(g);
 

integer (6)
 

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

symbol (6)
 

> whattype(2.1);
 

float (7)
 

> whattype(I);
 

complex(extended_numeric) (8)
 

> whattype(sin(x));
 

function (9)
 

> whattype([1..10]);
 

list (10)
 

> "This is a string";
 

This is a string (11)
 

> whattype(%);
 

string (12)
 

> s:="sin(2.1)+cos(5.1)";
 

`:=`(s, (13)
 

> s;
 

sin(2.1)+cos(5.1) (14)
 

> parse(s);
 

`+`(sin(2.1), cos(5.1)) (15)
 

> evalf(%);
 

>
 

1.241187109 (16)
 

> evalf(s);
 

sin(2.1)+cos(5.1) (17)
 

> whattype({1,3,5});
 

set (18)
 

> nops(x+y+1);
 

3 (19)
 

> whattype(x+y);
 

`+` (20)
 

> whattype(1..10);
myset:=[seq(1..5,0.5)];
 

`..` (21)
 

`:=`(myset, [1, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0]) (21)
 

> whattype(myset);
nops(myset); #nops returns the number of operands, in this case operands are members of the list
e:= a > b;
 

list (22)
 

9 (22)
 

`:=`(e, `<`(b, a)) (22)
 

> a := 1;
 

`:=`(a, 1) (23)
 

> b:= 2;
 

`:=`(b, 2) (24)
 

> whattype(e);
 

`<` (25)
 

> evalb(e);
 

false (26)
 

>
 

> f:=n!;
 

`:=`(f, factorial(n)) (27)
 

> type(f,function);
 

true (28)
 

> op(f); #finds the operant of a expression
 

n (29)
 

> op(0,f); #finds the operator of an expression
 

factorial (30)
 

> op(1,f);
 

n (31)
 

> z:=x+y;
 

`:=`(z, `+`(x, y)) (32)
 

> type(z,function);
 

false (33)
 

> op(z);
 

x, y (34)
 

> op(0,z);
 

>
 

`+` (35)
 

> op(1,z);
 

x (36)
 

> op(2,z);
 

y (37)
 

> assigned(x);
 

false (38)
 

> assigned(z);
 

true (39)
 

> z;
 

`+`(x, y) (40)
 

> unassign(z);
 

Error, (in unassign) cannot unassign `x+y' (argument must be assignable)
 

> unassign('z');
 

> z;
 

z (41)
 

> my_fact := proc(x::integer)
 

> local i; # 'i' will be a local variable
 

> local result;
if x > 0 then
 

>  result := 1;
 

>  for i from 1 to x do
 

>    result := result*i;
 

>  end do;
 result;
 

> else
 

>  error "Invalid argument";
 

> end if;
 

> end proc;
 

`:=`(my_fact, proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
`:=`(my_fact, proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
`:=`(my_fact, proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
`:=`(my_fact, proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
`:=`(my_fact, proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
`:=`(my_fact, proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
`:=`(my_fact, proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
`:=`(my_fact, proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
(42)
 

> my_fact(5);
 

120 (43)
 

> my_fact(2.1);
 

Error, invalid input: my_fact expects its 1st argument, x, to be of type integer, but received 2.1
 

> my_fact(-5);
 

Error, (in my_fact) Invalid argument
 

> fib := proc(x::integer)
 

> local a, b, c;
 

> a := 2;
 

> b := 3;
print(a);
print(b);
c := 5;
 

> while b <= x do
 

>   c := a + b;
  if ( c <= x) then
 

>      print(c);
  else   
      break;
  end if;
 

>   a := b;
 

>   b := c;
 

> end do;
 

> end proc;
 

`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
`:=`(fib, proc (`::`(x, integer)) local a, b, c; `:=`(a, 2); `:=`(b, 3); print(a); print(b); `:=`(c, 5); while `<=`(b, x) do `:=`(c, `+`(a, b)); if `<=`(c, x) then print(c) else break end if; `:=`(a, ...
(44)
 

> fib(88);
 

2 (45)
 

3 (45)
 

5 (45)
 

8 (45)
 

13 (45)
 

21 (45)
 

34 (45)
 

55 (45)
 

89 (45)
 

> #Note that reason we still see 89 is that maple returns the last calculation done in the proc as well
 

> my_MAX := proc()
 

> local i,m :: numerics;
description "finds the maximum of a list of numbers";
 

> if nargs=0 then              #nargs is the number of arguments
 

>  return -infinity;
 

> else
 

>  m:= args[1];               #arguments can be grabbed from args[i] list
 

>  for i from 2 to nargs do
 

>    if args[i] > m then
 

>       m := args[i];
 

>    end if;
 

>  end do;
 

>  return m;                  #explicit return, otherwise maple's procedure will return the value of last statement
end if;
 

> end proc;
 

`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
(46)
 

> my_MAX();
 

`+`(`-`(infinity)) (47)
 

> my_MAX(1,4,-3,12,6);
 

12 (48)
 

> my_MAX(1,infinity,2);
 

infinity (49)
 

> my_MAX(2,s,5);
 

Error, (in my_MAX) cannot determine if this expression is true or false: 2 < s
 

> save my_MAX, "My_Procs.mpl";  #Saves the procedure in a text file
 

> read "My_Procs.mpl";
 

`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
`:=`(my_MAX, proc () local i, `::`(m, numerics); description
(50)
 

>
 

> eval(my_fact);
 

proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
proc (`::`(x, integer)) local i, result; if `<`(0, x) then `:=`(result, 1); for i to x do `:=`(result, `*`(result, `*`(i))) end do; result else error
(51)
 

> #Procedure to integrate a given function numerically:
 

> my_int:=proc(f::operator, N:: integer, xmin::float, xmax::float)
 

> local i,intf;
 

> local x;
local dx;
dx := (xmax-xmin)/(N-1);
 

> x:=Array([seq(xmin+i*dx,i=0..N-1)]);
 

> intf:=Array(1..N);
intf[1]:=0;
 

> for i from 2 to N do
 

>  intf(i) := intf(i-1) + ( f(x[i])+f(x[i-1]) ) / 2 * dx;
 

> end do;
 

> return(intf);
 

> end proc;
 

>
 

`:=`(my_int, proc (`::`(f, operator), `::`(N, integer), `::`(xmin, float), `::`(xmax, float)) local i, intf, x, dx; `:=`(dx, `/`(`*`(`+`(xmax, `-`(xmin))), `*`(`+`(N, `-`(1))))); `:=`(x, Array([seq(`+...
`:=`(my_int, proc (`::`(f, operator), `::`(N, integer), `::`(xmin, float), `::`(xmax, float)) local i, intf, x, dx; `:=`(dx, `/`(`*`(`+`(xmax, `-`(xmin))), `*`(`+`(N, `-`(1))))); `:=`(x, Array([seq(`+...
`:=`(my_int, proc (`::`(f, operator), `::`(N, integer), `::`(xmin, float), `::`(xmax, float)) local i, intf, x, dx; `:=`(dx, `/`(`*`(`+`(xmax, `-`(xmin))), `*`(`+`(N, `-`(1))))); `:=`(x, Array([seq(`+...
`:=`(my_int, proc (`::`(f, operator), `::`(N, integer), `::`(xmin, float), `::`(xmax, float)) local i, intf, x, dx; `:=`(dx, `/`(`*`(`+`(xmax, `-`(xmin))), `*`(`+`(N, `-`(1))))); `:=`(x, Array([seq(`+...
`:=`(my_int, proc (`::`(f, operator), `::`(N, integer), `::`(xmin, float), `::`(xmax, float)) local i, intf, x, dx; `:=`(dx, `/`(`*`(`+`(xmax, `-`(xmin))), `*`(`+`(N, `-`(1))))); `:=`(x, Array([seq(`+...
`:=`(my_int, proc (`::`(f, operator), `::`(N, integer), `::`(xmin, float), `::`(xmax, float)) local i, intf, x, dx; `:=`(dx, `/`(`*`(`+`(xmax, `-`(xmin))), `*`(`+`(N, `-`(1))))); `:=`(x, Array([seq(`+...
`:=`(my_int, proc (`::`(f, operator), `::`(N, integer), `::`(xmin, float), `::`(xmax, float)) local i, intf, x, dx; `:=`(dx, `/`(`*`(`+`(xmax, `-`(xmin))), `*`(`+`(N, `-`(1))))); `:=`(x, Array([seq(`+...
`:=`(my_int, proc (`::`(f, operator), `::`(N, integer), `::`(xmin, float), `::`(xmax, float)) local i, intf, x, dx; `:=`(dx, `/`(`*`(`+`(xmax, `-`(xmin))), `*`(`+`(N, `-`(1))))); `:=`(x, Array([seq(`+...
`:=`(my_int, proc (`::`(f, operator), `::`(N, integer), `::`(xmin, float), `::`(xmax, float)) local i, intf, x, dx; `:=`(dx, `/`(`*`(`+`(xmax, `-`(xmin))), `*`(`+`(N, `-`(1))))); `:=`(x, Array([seq(`+...
(52)
 

> h:= x -> sin(x);
 

`:=`(h, proc (x) options operator, arrow; sin(x) end proc) (53)
 

> intsin:=my_int(h,10000,0.0,evalf(4*Pi,10));
 

`:=`(intsin, delayCrossProduct) (54)
 

> plot([seq(i*(4*evalf(Pi,10))/9999,i=0..9999)],intsin);
 

Plot_2d
 

> s:=readline("/home/arman/examples/maple/data.txt");
 

`:=`(s, (55)
 

> a:=readstat("Enter an integer number:");
 

Enter an integer number: 5:
 

`:=`(a, 5) (56)
 

> A:=readdata("data.txt",float);
#Reads the first column
 

`:=`(A, [1.0, 2.0, 3.0]) (57)
 

> B:=readdata("data.txt",[float,integer]);
#Reads the first two column
 

`:=`(B, [[1.0, 2], [2.0, 5], [3.0, 4]]) (58)
 

> C:=readdata("data.txt",[float,integer,float]);
 

`:=`(C, [[1.0, 2, 3.100], [2.0, 5, 4.200], [3.0, 4, 7.120]]) (59)
 

> writedata("data2.txt",C,float);
 

> ?writedata
 

>
 


last update: Wed May 11, 2016