> # FUNCTIONS can be specified in two ways: ---> or as a procedure > > # How to define a function > > #functions already defined --- initially known functions > ?inifcn; -------------------------------------------------------------------------------- > f:= 2^(t); t f := 2 -------------------------------------------------------------------------------- > subs(t=1/c, f); (1/c) 2 -------------------------------------------------------------------------------- > f:= t -> 2^(t); t f := t -> 2 -------------------------------------------------------------------------------- > f(1/c); (1/c) 2 -------------------------------------------------------------------------------- > simplify(solve(f(t) = 8, t)); 3 -------------------------------------------------------------------------------- > print(f); t t -> 2 -------------------------------------------------------------------------------- > # However you cannot say > f:= evaln(f); # f is a free variable f := f > f(t):=2^(t); t f(t) := 2 -------------------------------------------------------------------------------- > f(t), f(2); t 2 , f(2) -------------------------------------------------------------------------------- > print(f); proc() options remember; 'procname(args)' end -------------------------------------------------------------------------------- > # this is not a function description---- we stored in the remember table of f the value of the > # function at t > > #some plots > f:= t -> 2^t; t f := t -> 2 > plot(f, -5..5, numpoints=100); -------------------------------------------------------------------------------- > # constraint (same horiz and vertical scale) vs unconstraint > > > #function with two variables > g:= (x,y) -> x*cos(y) +3*x * y*sin(x); g := (x,y) -> x cos(y) + 3 x y sin(x) -------------------------------------------------------------------------------- > plot3d(g, -2..2, -1..1); -------------------------------------------------------------------------------- > > # Another way to define a function ---- using a procedure > > h:= proc(x, y) > x+y; > end; h := proc(x,y) x+y end -------------------------------------------------------------------------------- > h(3,4); 7 -------------------------------------------------------------------------------- > maximum := proc() > local result, i; > result:= args[1]; > for i from 2 to nargs do > if args[i] > result then result:= args[i]; fi; > od; > result; > end: -------------------------------------------------------------------------------- > maximum(2,3,4,5,220); 220 -------------------------------------------------------------------------------- > maximum(); Error, (in maximum) improper op or subscript selector -------------------------------------------------------------------------------- > # the value returned is the last value computed unless there is an explicit return > > #RECURSIVE procedure definitions > > fib:= n -> if n=0 then 0 elif n=1 then 1 else fib(n-1) + fib(n-2) fi; fib := proc(n) options operator,arrow; if n = 0 then 0 elif n = 1 then 1 else fib(n-1)+fib(n-2) fi end -------------------------------------------------------------------------------- > settime:= time(); settime := 71.683 -------------------------------------------------------------------------------- > fib(25); 75025 -------------------------------------------------------------------------------- > ctime:= (time() - settime) *seconds; ctime := 22.983 seconds -------------------------------------------------------------------------------- > fibr := proc(n) option remember; if n=0 then 0 elif n=1 then 1 else fibr(n-1) + fibr(n-2) fi end: -------------------------------------------------------------------------------- > fibr(10); 55 -------------------------------------------------------------------------------- > settime:= time(); settime := 94.716 -------------------------------------------------------------------------------- > fibr(1000); 4346655768693745643568852767504062580256466051737178040248172908953655541794905\ 1890403879840079255169295922593080322634775209689623239873322471161642996440906\ 533187938298969649928516003704476137795166849228875 -------------------------------------------------------------------------------- > ctime:= (time() - settime) *seconds; ctime := .200 seconds -------------------------------------------------------------------------------- > # from exponential time complexity to linear time complexity > > # Notice that each Maple procedure has an associated REMEMBER TABLE. > # the remember table is the 4th operand of the structure of the procedure > op(4, eval(fibr)): -------------------------------------------------------------------------------- > # to empty the remember table > forget(fibr); -------------------------------------------------------------------------------- > op(4, eval(fibr)); table([]) -------------------------------------------------------------------------------- > > > # UNAPPLY ---- another way to make new functions > > # the opposite of applying a function to symbolic parameters > > f4 := 2*x + y; f4 := 2 x + y -------------------------------------------------------------------------------- > f4(2,2); 2 x(2, 2) + y(2, 2) -------------------------------------------------------------------------------- > ff4:= unapply (f4, x, y); ff4 := (x,y) -> 2 x + y -------------------------------------------------------------------------------- > ff4(2,2); 6 -------------------------------------------------------------------------------- > > # OPERATIONS on functions > > gg4:= (x,y) -> x*y; gg4 := (x,y) -> x y -------------------------------------------------------------------------------- > fg4:= ff4 + gg4; fg4 := ff4 + gg4 -------------------------------------------------------------------------------- > fg4(2,2); 10 -------------------------------------------------------------------------------- > # composition > > cfg4:= ff4 @ gg4; cfg4 := ff4@gg4 -------------------------------------------------------------------------------- > cfg4(2,2); Error, (in ff4) ff4 uses a 2nd argument, y, which is missing -------------------------------------------------------------------------------- > f:= x -> x^2; 2 f := x -> x -------------------------------------------------------------------------------- > cfg := f @ gg4; cfg := f@gg4 -------------------------------------------------------------------------------- > cfg(2,2); 16 -------------------------------------------------------------------------------- >