introMaple.mws

>    restart;

  A Short Introduction to Maple

>   

 Arithmetic Operations

 Variable Assignement

>    a := 100;

a := 100

>    b := 200;

b := 200

>    a+b;

300

>    a mod 3;

1

>    12*23;

276

>    12^23;

6624737266949237011120128

>    30!;

265252859812191058636308480000000

>    100/20;

5

>    100/30;

10/3

>    trunc(100/30);

3

>    round(100/30);

3

>    trunc(-4.3);

-4

>    round(-4.3);

-4

>    floor(2.3); floor(-2.3);

2

-3

>    ceil(2.3); ceil(-2.3);

3

-2

>    abs(-4);

4

Quotes, Concatenation operator

>    a:=3;

a := 3

>    a:='a';

a := 'a'

>    a;

a

>    sqrt(s^2);

(s^2)^(1/2)

>    assume(s>0);   sqrt(s^2);

s

>    assume(s<0);   sqrt(s^2);

-s

>    s:='s';

s := 's'

>    a:=(x+y)^3;

a := (x+y)^3

  In Maple 5, the concatenation operator was the dot.

 In Maple 6, the concatenation operator is the two vertical bars.

>    a||1;

a1

>    a||1||2;

a12

  Definition of strings

>    c:="abcd";

c :=

  Interface, Code Generation

  Customize the user interface

>    interface(version);

`Maple Worksheet Interface, Maple 8.00, IBM INTEL LINUX, Apr 22 2002 Build ID 110847`

>    interface(screenwidth=100);

>    interface( quiet=true);

>    interface( quiet=false);

>    interface( verboseproc = 2 );

>    eval(isprime);

proc (n) local btor, nr, p, r; options remember, system, `Copyright (c) 1993 Gaston Gonnet, Wissenschaftliches Rechnen, ETH Zurich. All rights reserved.`; if not type(n,'integer') then if type(n,'numer...

  LaTeX interface

>    expr:=expand((x+y)^10);

expr := x^10+10*x^9*y+45*x^8*y^2+120*x^7*y^3+210*x^6*y^4+252*x^5*y^5+210*x^4*y^6+120*x^3*y^7+45*x^2*y^8+10*x*y^9+y^10

>    latex(expr);

{x}^{10}+10\,{x}^{9}y+45\,{x}^{8}{y}^{2}+120\,{x}^{7}{y}^{3}+210\,{x}^{6}{y}^{4}+252\,{x}^{

5}{y}^{5}+210\,{x}^{4}{y}^{6}+120\,{x}^{3}{y}^{7}+45\,{x}^{2}{y}^{8}+10\,x{y}^{9}+{y}^{10}

  C and Fortran code generation

>    with(codegen);

Warning, the protected name MathML has been redefined and unprotected

[C, GRAD, GRADIENT, HESSIAN, JACOBIAN, MathML, WebEQ, cost, declare, dontreturn, eqn, fortran, horner, intrep2maple, joinprocs, makeglobal, makeparam, makeproc, makevoid, maple2intrep, optimize, packar...
[C, GRAD, GRADIENT, HESSIAN, JACOBIAN, MathML, WebEQ, cost, declare, dontreturn, eqn, fortran, horner, intrep2maple, joinprocs, makeglobal, makeparam, makeproc, makevoid, maple2intrep, optimize, packar...

>    C(1-x/2+3*x^2-x^3+x^4);

      t0 = 1.0-x/2.0+3.0*x*x-x*x*x+x*x*x*x;

>    fortran(1-x/2+3*x^2-x^3+x^4);

      t0 = 1-x/2+3*x**2-x**3+x**4

>    C([a=10.3*x-y,b=z/t*log(z)]);

      a = 0.103E2*x-y;

      b = z/t*log(z);

>    fortran([a=10.3*x-y,b=z/t*log(z)]);

      a = 0.103D2*x-y

      b = z/t*log(z)

>    cost(expand((x+y)^5));

5*additions+28*multiplications

 Types  (whattype, type, hastype)

  Maple has a type system. Using it is not obligatory though.

>    a:=1; whattype(a);

a := 1

integer

>    b:=1.2; whattype(b);

b := 1.2

float

>    c:=2-3*I; whattype(c);

c := 2-3*I

complex

>    c:=cos(x); whattype(c);

c := cos(x)

function

>    whattype(1/2);

fraction

>    whattype(x);

symbol

   For lazy typists, there is an alias mechanism in Maple.

>    alias(w=whattype);

w

>    w(a+b);

float

>    w(x+y); w(x*y); w(x^3);

`+`

`*`

`^`

>    w(w);

symbol

Besides whattype, there are two more commands that handle types

>    type(a,integer); type(b,integer);

true

false

>    hastype(a,integer); hastype(b,integer);

true

false

The hastype(expr,t) returns true if expr has a subexpression of the type t

>    pol:=(x+x*y)^2;

pol := (x+x*y)^2

>    w(pol);

`^`

>    type(pol,`*`);

false

>    hastype(pol,`*`);

true

>    type(pol,`+`);

false

>    hastype(pol,`+`);

true

>    pol:=expand((x+x*y)^2);

pol := x^2+2*x^2*y+x^2*y^2

>    w(pol);

`+`

>    type(pol,`*`);

false

>    hastype(pol,`^`);

true

 Access parts of a Maple expression (op, nops, indets)

   Tree structure of an expression in Maple.

 Internal representation of mathematical expressions.

>    a:=x+y;

a := x+y

>    nops(a);

2

>    op(1,a);

x

>    op(2,a);

y

>    b:=sqrt(a);

b := (x+y)^(1/2)

>    nops(b);

2

>    op(1,b);

x+y

>    op(2,b);

1/2

>    w(b);

w((x+y)^(1/2))

>    nops(op(2,b));

2

>    op(1,op(2,b)); op(2,op(2,b));

1

2

 Because fractions are quite common,

 there are two special commands to get

 the numerator and the denominator.

>    numer(1/2); denom(1/2);

1

2

   There is a more compact way to  write this

>    op([2,1],b); op([2,2],b);

1

2

>    op(0,b);

`^`

>    op(1..3,[5,6,7,8]);

5, 6, 7

>    op(b);

x+y, 1/2

>    indets(x+y^2);

{x, y}

>    a:=x+z^3:  indets(a);

{x, z}

>    indets(sqrt(x)); nops(sqrt(x));

{x, x^(1/2)}

2

>    indets(sqrt(x)+y^(1/3));

{x, y, x^(1/2), y^(1/3)}

>    indets(exp(x+y));  indets(log(x+y));

{x, y, exp(x+y)}

{x, y, ln(x+y)}

 Sequences, Lists and Sets

  A sequence is defined as a succession of elements separated by commas

>    1,2,3;

1, 2, 3

>    whattype(%);

exprseq

We can create fairly complicated sequences

>    seq(i,i=1..10);

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

>    seq(i^2,i=1..10);

1, 4, 9, 16, 25, 36, 49, 64, 81, 100

>    seq(x^i-1,i=1..7);

x-1, x^2-1, x^3-1, x^4-1, x^5-1, x^6-1, x^7-1

  A list  is defined as a sequence enclosed in (square) brackets

>    l:=[1,2,3];

l := [1, 2, 3]

>    whattype(%);

list

  You can use seq to define new lists

>    l:=[seq(ifactor(i),i=1..6)];

l := [1, ``(2), ``(3), ``(2)^2, ``(5), ``(2)*``(3)]

   To access the i-th element of a list

>    l[4];

``(2)^2

  A set is defined as a sequence enclosed in (curly) braces

>    s:={1,2,3};

s := {1, 2, 3}

>    whattype(%);

set

  Sets cannot contain multiple elements as opposed to lists

>    l:=[1,1,2,3];

l := [1, 1, 2, 3]

>    s:={1,1,2,3};

s := {1, 2, 3}

 Apply a function to every element of a list (map)

>    l1:=[1,9,64];

l1 := [1, 9, 64]

>    map(sqrt,l1);

[1, 3, 8]

>    l2:=[-1,4,-6];

l2 := [-1, 4, -6]

>    map(abs,l2);

[1, 4, 6]

>    l3:=[10/3,2,3/7];

l3 := [10/3, 2, 3/7]

>    map(evalf,l3);

[3.333333333, 2., .4285714286]

>    map(evalf[15],l3);

[3.33333333333333, 2., .428571428571429]

>    f:=x->x mod 3;

f := proc (x) options operator, arrow; `mod`(x,3) end proc

>    map(f,[1,10,12]);

[1, 1, 0]

>    map(x->x^2+1,[1,10,12]);

[2, 101, 145]

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

[x^3-1, (y+1)^3-1, (z+2)^3-1]

>    map(factor,%);

[(x-1)*(x^2+x+1), y*(y^2+3*y+3), (z+1)*(z^2+5*z+7)]

When the function has arguments,

  these appear at the end.

>    l:=[1+x^2+x^2*y+x-x^2*y^3,x^3-x^3*y+x^2];

l := [1+x^2+x^2*y+x-x^2*y^3, x^3-x^3*y+x^2]

>    map(collect,l,x);

[1+(1+y-y^3)*x^2+x, (1-y)*x^3+x^2]

 Operations on sets (union, intersect, member)

>    s1 := {1,2,3};   s2 := {1,4,5};

s1 := {1, 2, 3}

s2 := {1, 4, 5}

>    s1 union s2;

{1, 2, 3, 4, 5}

>    s1 intersect s2;

{1}

>    member(2,s1); member(2,s2);

true

false

>    s1 minus s2;

{2, 3}

>    s2 minus s1;

{4, 5}

>    s1 minus {1};

{2, 3}

>    s2 union {6};

{1, 4, 5, 6}

 Operations on lists

 Add an element to a list

>    m1:=[1,2,3];

m1 := [1, 2, 3]

>    m1:=[op(m1),4];

m1 := [1, 2, 3, 4]

>    m1:=[5,op(m1),6];

m1 := [5, 1, 2, 3, 4, 6]

>    m1:=[op(1..3,m1),100,op(4..nops(m1))];

m1 := [5, 1, 2, 100, 4, 6]

  This technique is not efficient when

   we are dealing with big lists.

 Delete an element from a list

>    m2:= subsop(2=NULL,m1);

m2 := [5, 2, 100, 4, 6]

  Note that the initial list has been left unaltered.

 m2 is a simply copy of m1, with the second element removed.

>    m1;

[5, 1, 2, 100, 4, 6]

 Modify an element of a list

>    m1[2]:=20;

m1[2] := 20

>    m1;

[5, 20, 2, 100, 4, 6]

>    subsop(2=25,m1);

[5, 25, 2, 100, 4, 6]

>    m1;

[5, 20, 2, 100, 4, 6]

 Sums and Products (sum, prod)

>    sum(i,i=1..10^6);

500000500000

>    add(i,i=1..10^6);

500000500000

   sum looks for a closed form and then substitutes the

 numerical values of the bounds of summations

  add performs a loop from the lower bound to the upper bound

>    sum(i^2,i=1..1000000000000);

333333333333833333333333500000000000

>    sum(i,i=1..n);

1/2*(n+1)^2-1/2*n-1/2

>    factor(%);

1/2*n*(n+1)

>    factor(sum(i^3,i=1..n));

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

>    factor(sum(i^3,i=a..b));

-1/4*(a-1-b)*(a+b)*(a^2-a+b^2+b)

>    product(i,i=1..10);   10!;

3628800

3628800

   Infinite sums and products are handled

>    sum('1/i^2','i=1..infinity');

1/6*Pi^2

>    sum('1/k!', 'k'=0..infinity);

exp(1)

>    product('1-z^2/n^2','n=1..infinity');

product:   "Cannot show that 1-z^2/n^2 has no zeros on [1,infinity]"

1/z/Pi*sin(Pi*z)

  Sometimes the results are not immediately readable

>    product('1/i^2','i=1..n');

1/(GAMMA(n+1)^2)

>    i:='i':k:='k':
sum(combinat[binomial](i+k-1,k-1),i=0..n);

(n+1)/k*binomial(n+k,k-1)

>    sum('(2*z)/(z^2-n^2)','n=1..infinity');

Psi(1-z)-Psi(z+1)

  use single quotes to avoid premature evaluation

>    i:=2;

i := 2

>    sum(i,i=1..1000);

Error, (in sum) summation variable previously assigned, second argument evaluates to 2 = 1 .. 1000

>    sum('i','i'=1..1000);

500500

in the following sum we access the command binomial  from the

package combinat in the long form, avoiding the with(combinat)

>    n:='n';
sum(combinat[binomial](n,k),k=0..n);

n := 'n'

2^n

  the sum is actually equal to  (1+1)^n (binomial theorem)

   we can have nested sums and products

>    factor(sum(sum('i*j','i=1..n'),'j=1..n'));

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

>    i:='i': j:='j': n:='2':

>    sum(sum(n!/(i!*j!*(n-i-j)!),i=0..n),j=0..n-i);

-288/(5+7^(1/2)*I)/(-5+7^(1/2)*I)

>    evalc(%);

9

the sum is actually equal to  (1+1+1)^2 = 9, by the trinomial theorem

  now let us try to verify  that (1+1+1)^3 = 27:

>    i:='i': j:='j': n:='3':

>    sum(sum(n!/(i!*j!*(n-i-j)!),i=0..n),j=0..n-i);

(2*(i-2)/i*(i-3)*(-4+i)*((((-4+i)!+1/6*(-1+i)!)*(i-2)!+(-4+i)!*(-1+i)!)*(i-3)!+1/2*(i-2)!*(-4+i)!*(-1+i)!)*(i-5/2)*(-1+i)*(i^2+1/2*i+8)/(2+i)/(i+1)/(4-i)!/(i-2)!/(i-3)!/(-4+i)!/(-1+i)!*hypergeom([2, -1...
(2*(i-2)/i*(i-3)*(-4+i)*((((-4+i)!+1/6*(-1+i)!)*(i-2)!+(-4+i)!*(-1+i)!)*(i-3)!+1/2*(i-2)!*(-4+i)!*(-1+i)!)*(i-5/2)*(-1+i)*(i^2+1/2*i+8)/(2+i)/(i+1)/(4-i)!/(i-2)!/(i-3)!/(-4+i)!/(-1+i)!*hypergeom([2, -1...
(2*(i-2)/i*(i-3)*(-4+i)*((((-4+i)!+1/6*(-1+i)!)*(i-2)!+(-4+i)!*(-1+i)!)*(i-3)!+1/2*(i-2)!*(-4+i)!*(-1+i)!)*(i-5/2)*(-1+i)*(i^2+1/2*i+8)/(2+i)/(i+1)/(4-i)!/(i-2)!/(i-3)!/(-4+i)!/(-1+i)!*hypergeom([2, -1...
(2*(i-2)/i*(i-3)*(-4+i)*((((-4+i)!+1/6*(-1+i)!)*(i-2)!+(-4+i)!*(-1+i)!)*(i-3)!+1/2*(i-2)!*(-4+i)!*(-1+i)!)*(i-5/2)*(-1+i)*(i^2+1/2*i+8)/(2+i)/(i+1)/(4-i)!/(i-2)!/(i-3)!/(-4+i)!/(-1+i)!*hypergeom([2, -1...
(2*(i-2)/i*(i-3)*(-4+i)*((((-4+i)!+1/6*(-1+i)!)*(i-2)!+(-4+i)!*(-1+i)!)*(i-3)!+1/2*(i-2)!*(-4+i)!*(-1+i)!)*(i-5/2)*(-1+i)*(i^2+1/2*i+8)/(2+i)/(i+1)/(4-i)!/(i-2)!/(i-3)!/(-4+i)!/(-1+i)!*hypergeom([2, -1...
(2*(i-2)/i*(i-3)*(-4+i)*((((-4+i)!+1/6*(-1+i)!)*(i-2)!+(-4+i)!*(-1+i)!)*(i-3)!+1/2*(i-2)!*(-4+i)!*(-1+i)!)*(i-5/2)*(-1+i)*(i^2+1/2*i+8)/(2+i)/(i+1)/(4-i)!/(i-2)!/(i-3)!/(-4+i)!/(-1+i)!*hypergeom([2, -1...
(2*(i-2)/i*(i-3)*(-4+i)*((((-4+i)!+1/6*(-1+i)!)*(i-2)!+(-4+i)!*(-1+i)!)*(i-3)!+1/2*(i-2)!*(-4+i)!*(-1+i)!)*(i-5/2)*(-1+i)*(i^2+1/2*i+8)/(2+i)/(i+1)/(4-i)!/(i-2)!/(i-3)!/(-4+i)!/(-1+i)!*hypergeom([2, -1...
(2*(i-2)/i*(i-3)*(-4+i)*((((-4+i)!+1/6*(-1+i)!)*(i-2)!+(-4+i)!*(-1+i)!)*(i-3)!+1/2*(i-2)!*(-4+i)!*(-1+i)!)*(i-5/2)*(-1+i)*(i^2+1/2*i+8)/(2+i)/(i+1)/(4-i)!/(i-2)!/(i-3)!/(-4+i)!/(-1+i)!*hypergeom([2, -1...

 (more interesting things happen for n=4)

 to actually do this sum we break it in two pieces

>    i:='i': j:='j': n:='3':

>    sum(n!/(i!*j!*(n-i-j)!),j=0..n-i);

48/i!/(3-i)!/(2^i)

>    sum(%,i=0..n);

27

 now we know how to do the general sum (1+1+1)^n = 3^n

>    i:='i': j:='j': n:='n':

>    sum(n!/(i!*j!*(n-i-j)!),j=0..n-i);

n!/i!/(n-i)!/(2^(-n+i))

>    simplify(sum(%,i=0..n));

3^n

 Conversions (convert)

>    convert([1,2,3],set);

{1, 2, 3}

>    convert({1,2,3,4},list);

[1, 2, 3, 4]

>    convert([1,1,2,3],set);

{1, 2, 3}

>    l:=seq(i^3,i=1..5);

l := 1, 8, 27, 64, 125

>    convert([l],`+`);

225

>    convert([l],`*`);

1728000

>    series(tan(x),x,9);

series(1*x+1/3*x^3+2/15*x^5+17/315*x^7+O(x^9),x,9)

>    w(%);

w(series(1*x+1/3*x^3+2/15*x^5+17/315*x^7+O(x^9),x,9))

>    convert(%%,polynom);

x+1/3*x^3+2/15*x^5+17/315*x^7

>    w(%);

w(x+1/3*x^3+2/15*x^5+17/315*x^7)

>    convert(16341,hex);

`3FD5`

>    convert(16341,binary);

11111111010101

>    convert(16341,octal);

37725

  For more general bases, use the following syntax

>    convert(13,base,6);

[1, 2]

>    convert(15,base,15);

[0, 1]

  Partial fraction decomposition of a rational function

>    r:=(3*x+1)/(x^2-4*x+4);    w(r);

r := (3*x+1)/(x^2-4*x+4)

w((3*x+1)/(x^2-4*x+4))

>    convert(r,parfrac,x);

7/(x-2)^2+3/(x-2)