Funkce v Maplu

> y:=x^2;

[Maple Math]

> subs(x=2,y);

[Maple Math]

Funkce muze byt zadana pomoci operatoru -> nebo jako procedura.

> y:=x->x^2;

[Maple Math]

> whattype(eval(y));

[Maple Math]

> whattype(y);

[Maple Math]

Vsimnete si rozdilu mezi:

> print(y);

[Maple Math]

> print(y(x));

[Maple Math]

> y(0);y(1);y(2);y(c);

[Maple Math]

[Maple Math]

[Maple Math]

[Maple Math]

> y;

[Maple Math]

> eval(y);

[Maple Math]

Zde se vyhodnocuje pouze jmeno funkce.

> y(t);

[Maple Math]

Vsimnete si rozdilu mezi predchazejicim zadanim a nasledujicimi prikazy:

> restart;

> y(x):=x^2;

[Maple Math]

> y(x), y(0), y(1/c);

[Maple Math]

> print(y);

[Maple Math]

Definovali jsme funkci, ale bez funkcniho predpisu.

> y:=x->x^2;

[Maple Math]

> print(y);

[Maple Math]

> infolevel[solve]:=1;

[Maple Math]

> solve(y=9,x);

solve:   Warning: no solutions found

> solve(y(x)=9,x);

[Maple Math]

Definovani funkce vice promennych:

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

[Maple Math]

> f(3,2);

[Maple Math]

> plot3d(f,-1..1,-1..1,numpoints=2500, style=patchcontour, axes=framed);

Definovani po castech spojite funkce

Pomoci vetveni.

Syntaxe:

parametr -> if podminka 1 then hodnota 1

elif podminka 2 then hodnota 2

........

elif podminka n then hodnota n
else implicitni hodnota

fi

Definujme nyni funkci, ktera ma hodnotu -1 pro realna cisla mensi jak 1, 0 pro hodnotu x=1 a 1 jinak.

> step:=x-> if x<1 then -1 elif x=1 then 0 else 1 fi;

[Maple Math]

> step(3/2), step(1), step(1/2);

[Maple Math]

> plot(step, -1..Pi, discont=true, color=black, scaling=constrained);

> step(Pi);

Error, (in step) cannot evaluate boolean

Problem je v tom, ze Maple umi porovnavat pouze cisla typu integer, fraction a float.

> step(1.1);

[Maple Math]

> STEP:=x->piecewise(x<1, -1, x=1, 0, 1);

[Maple Math]

> STEP(3/2), STEP(1), STEP(1/2), STEP(Pi);

[Maple Math]

> STEP(u);

[Maple Math]

> f:=x->piecewise(x<=1, x^2, sin(x));

[Maple Math]

> D(f);

[Maple Math]

Definice funkce pomoci Maplovske procedury:

> sgn:=proc(n::integer) (-1)^n end:

> sgn(Pi);

Error, sgn expects its 1st argument, n, to be of type integer, but received Pi

> sgn(4);

[Maple Math]

Definice procedury ma obecne tuto syntaxi:

proc (posloupnost_parametru)

[ local posloupnost_jmen;]

[ global posloupnost_jmen;]

[ options posloupnost_jmen;]

[ description retezec;]

prikazy

end

U kazdeho parametru muze byt uvedeno, jakeho je typu.
Obecne procedura vraci posledni pocitanou hodnotu.

> y:=proc(x) x^2 end:

> y(x), y(0), y(2);

[Maple Math]

> whattype(%%);

[Maple Math]

> restart;

Rekurse:

Rekursivni definici funkce nebo procedury budeme ilustrovat na vypoctu

tzv. Lucasovych cisel Ln, ktere jsou definovany pomoci linerani rekurence:

L(1)=1, L(2)=3, L(n)=L(n-1)+L(n-2) pro n>2

> L:=proc(n::posint)

> if n=1 then 1

> elif n=2 then 3

> else L(n-1)+L(n-2)

> fi

> end:

> L(-6);

Error, L expects its 1st argument, n, to be of type posint, but received -6

> time(L(20));

[Maple Math]

> readlib(profile):

> profile(L):

> L(6);

[Maple Math]

> showprofile();

function           depth    calls     time    time%         bytes   bytes%

---------------------------------------------------------------------------

L                      5       15    0.000     0.00         14236   100.00

---------------------------------------------------------------------------

total:                 5       15    0.000     0.00         14236   100.00

> restart;

Pro zefektivneni procedury pouzijeme option remeber, ktera zpusobi zapamatovani

funkcnich hodnot tak, jak jsou pocitany.

> LL:=proc(n::nonnegint) Lucas(n) end:

pri zavolani L je kontrolovan typ argumentu,

pokud je v poradku, vola se rekursivni definice Lukas (timto zamezime opetovnemu testovani argumentu)

> Lucas:=proc(n)

> option remember;

kazda Maplovska procedura je spojena s pametovou tabulkou, ktera se aktivuje pomoci option remember.

Polozky tabulky jsou funkcni hodnoty, indexovane pomoci argumentu, odpovidajicimu volani funkce.

Pokud zavolame Lucas(n), Maple se podiva do tabulky, zda tam neni ulozena odpovidajici

funkcni hodnota. Pokud ne, vyvola se telo procedury a dvojice (n, Lucas(n)) se automaticky ulozi do pametove tabulky. Tim dosahneme, ze kazde cislo je pocitano pouze jednou.

> if n=1 then 1

> elif n=2 then 3

> else Lucas(n-1)+Lucas(n-2)

> fi

> end:

> LL(6);

[Maple Math]

Pametova tabulka je pristupna jako ctvrty operand procedury.

> op(4, eval(Lucas));

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

Odstraneni pametove tabulky:

> subsop(4=NULL, eval(Lucas)):

> op(4, eval(Lucas));

> readlib(profile):

> profile(Lucas):

> LL(6);

[Maple Math]

> showprofile();

function           depth    calls     time    time%         bytes   bytes%

---------------------------------------------------------------------------

Lucas                  5        9    0.000     0.00          8452   100.00

---------------------------------------------------------------------------

total:                 5        9    0.000     0.00          8452   100.00

> time(Lucas(300));

[Maple Math]

> restart;

> Lucas:=proc(n)

> Lucas(n):=Lucas(n-1)+Lucas(n-2)

> end:

> Lucas(1):=1: Lucas(2):=3:

> op(4, eval(Lucas));

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

> Lucas(5): op(4, eval(Lucas));

[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]
[Maple Math]

UNAPPLY

Tento zpusob definovani funkce je vyhodny zejmena tehdy, pokud

z nejakeho vyrazu ci formule chceme udelat funkci

> vzorec:=(b^2*x^2*sin(b*x)-2*sin(b*x)+2*b*x*cos(b*x)*a*t)/b^3;

[Maple Math]

> F:=unapply(vzorec, x, t);

[Maple Math]

> F(0,1),F(Pi/b,5);

[Maple Math]

Jine pokusy selhavaji.

> vzorec;

[Maple Math]

> F:=(x,t)->%;

[Maple Math]

> F(0,1);

> G:=(x,t)->vzorec;

[Maple Math]

> F(u,v),G(u,v);

[Maple Math]

Jinou moznosti je jedine:

> H:=subs(telo=vzorec, (x,t)->telo);

[Maple Math]

> H(u,v);

[Maple Math]

Operace s funkcemi.

> f:=x->ln(x)+1; g:=y->exp(y)-1;

[Maple Math]

[Maple Math]

> h:=f+g: h(z);

[Maple Math]

> h:=f*g: h(z);

[Maple Math]

> h:=f@g: h(z);

[Maple Math]

> h:=g@f: h(z);

[Maple Math]

> simplify(%);

[Maple Math]

> (f@@4)(z); #ekvivalent k f(f(f(f(z))))

[Maple Math]

> map(x->x^2, a+b+c);

[Maple Math]

> map(x->x+2, [1,2,3]);

[Maple Math]