Maxima tutorial



Hi Mario,

thanks for linking this document. And thanks to Roland and Eike for posting
it. I attached remarks to some chapters.

I believe three points are worth to mention:

- Blocks in user functions terminated by a return statement. I have also
seen this Java/C++ habit in e.g. the Galois fields package
share/contrib/gf/gf.mac. Leaving out unnecessary returns and replacing
'block([ ],' expressions with no local variables by a simple '(' functions
can be written a little bit easier.

- Lambda expressions are widely used in this document (addressed to school
kids!) where in most cases they can be replaced by a call to an existing
function.

- For working with vectors in schools the vector_rebuild package might be
an alternative approach. It is documented in
share/vector/vector_rebuild.usg.

Greetings
Volker


2012/1/3 Mario Rodriguez <biomates at telefonica.net>

> El mar, 03-01-2012 a las 11:45 +0100, Roland Stewen escribi?:
> > Hi,
> >
> > I am writing a German tutorial for Maxima
> > together with Eike Sch?tze for German pupils at
> > secondary school (gymnasium).
> >
> > html-Version:
> >
> http://www.rvk-hagen.de/~stewen/maxima_in_beispielen/maxima_in_beispielen.html
> >
> > pdf-Version:
> > http://www.rvk-hagen.de/~stewen/maxima_in_beispielen.pdf
> >
> > The tutorial should be a "cookbook" with
> > short recipes for traditional mathproblems in school
> > for solving them with maxima.
> >
> > The tutorial will be changed from time to time
> > because of feedback from users and their needs.
> >
> > Comments are more than welcome.
> >
> > Roland Stewen
>
>
> This document is now linked from the Maxima web site.
>
> Danke!
>
> --
> Mario
>
>
>
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>
-------------- next part --------------

S. 18:

(%i1) f(x) := x^2;
                                           2
(%o1)                             f(x) := x
(%i2) df(x) := diff(f(x), x);
(%o2)                       df(x) := diff(f(x), x)
(%i3) df(x);
(%o3)                                 2 x
(%i4) df(2);
diff: second argument must be a variable; found 2
#0: df(x=2)
 -- an error. To debug this try: debugmode(true);
(%i5) df(x) := ''( diff(f(x), x) );
(%o5)                            df(x) := 2 x
(%i6) df(2);
(%o6)                                  4
(%i7) define(df(x), diff(f(x), x));
(%o7)                            df(x) := 2 x
(%i8) df(2);
(%o8)                                  4


S. 69:

(%i1) load (eigen)$
(%i2) a : covect([1,2,3])$
(%i3) b : covect([2,1,2])$
(%i4) basis : orthogonal_complement(a, b);
                                      [  1  ]
                                      [     ]
(%o4)                            span([  4  ])
                                      [     ]
                                      [ - 3 ]
(%i5) first(basis);
                                    [  1  ]
                                    [     ]
(%o5)                               [  4  ]
                                    [     ]
                                    [ - 3 ]

S. 70:

(%i1) load (eigen)$
(%i2) a : covect([1,2,3])$
(%i3) b : covect([1,2,3])$
(%i4) basis : orthogonal_complement(a, b);
                                 [ - 2 ]  [  0  ]
                                 [     ]  [     ]
(%o4)                       span([  1  ], [  3  ])
                                 [     ]  [     ]
                                 [  0  ]  [ - 2 ]
(%i5) n1 : first(basis);
                                    [ - 2 ]
                                    [     ]
(%o5)                               [  1  ]
                                    [     ]
                                    [  0  ]
(%i6) n2 : second(basis);
                                    [  0  ]
                                    [     ]
(%o6)                               [  3  ]
                                    [     ]
                                    [ - 2 ]


S. 72:

(%i1) load(vector_rebuild)$
(%i2) a : covect([1,2,3])$
(%i3) b : covect([2,1,2])$
(%i4) a~b;
                                    [  1  ]
                                    [     ]
(%o4)                               [  4  ]
                                    [     ]
                                    [ - 3 ]


S. 73:

(%i1) load(vector_rebuild)$
(%i2) v([args]):= covect(args)$
(%i3) x : v(1,2,3) + r*v(2,5,1) + s*v(3,-2,1);
                            [  3  ]     [ 2 ]   [ 1 ]
                            [     ]     [   ]   [   ]
(%o3)                     s [ - 2 ] + r [ 5 ] + [ 2 ]
                            [     ]     [   ]   [   ]
                            [  1  ]     [ 1 ]   [ 3 ]
(%i4) x, r = 0, s = 0, vector_simp;
                                     [ 1 ]
                                     [   ]
(%o4)                                [ 2 ]
                                     [   ]
                                     [ 3 ]
(%i5) x, r = 0, s = 2, vector_simp;
                                    [  7  ]
                                    [     ]
(%o5)                               [ - 2 ]
                                    [     ]
                                    [  5  ]


S. 75:

(%i1) load(vector_rebuild)$
(%i2) v([args]):= covect(args)$
(%i3) b : v(-25,2,0)$
(%i4) c : v(0, 2,-5)$
(%i5) n : b~c;
                                   [ - 10  ]
                                   [       ]
(%o5)                              [ - 125 ]
                                   [       ]
                                   [ - 50  ]
(%i6) n : vector_factor(n), vector_factor_minus:true;
                                      [ 2  ]
                                      [    ]
(%o6)                             - 5 [ 25 ]
                                      [    ]
                                      [ 10 ]
(%i7) en : 1/|n| * n;
                                        [ 2  ]
                                    1   [    ]
(%o7)                            (- --) [ 25 ]
                                    27  [    ]
                                        [ 10 ]
(%i8) |en|;
(%o8)                                  1



S. 76:

(%i1) load(vector_rebuild)$
(%i2) v([args]):= covect(args)$
(%i3) a : v(1,2,3)$
(%i4) n : v(2,25,10)$
(%i5) oc : orthogonal_complement(n)$
0 errors, 0 warnings
(%i6) [b, c] : [first(oc), second(oc)]$
(%i7) a + r * b + s * c;
                          [  0   ]     [ - 25 ]   [ 1 ]
                          [      ]     [      ]   [   ]
(%o7)                   s [  10  ] + r [  2   ] + [ 2 ]
                          [      ]     [      ]   [   ]
                          [ - 25 ]     [  0   ]   [ 3 ]
(%i8) nf : x.n = a.n;
                              [ 2  ]   [ 1 ]   [ 2  ]
                              [    ]   [   ]   [    ]
(%o8)                     x . [ 25 ] = [ 2 ] . [ 25 ]
                              [    ]   [   ]   [    ]
                              [ 10 ]   [ 3 ]   [ 10 ]
(%i9) nf, x=v(x1,x2,x3), vector_simp;
(%o9)                      10 x3 + 25 x2 + 2 x1 = 82
(%i10)  % / |n|;
                           10 x3 + 25 x2 + 2 x1   82
(%o10)                     -------------------- = --
                                    27            27



S. 100:

(%i1) len(vec) := block([sum:0],
for n in flatten(args(vec)) do sum : sum + n^2,
sqrt(sum) )$
(%i2) load (eigen)$
(%i3) a : covect ([3, 4]);
                                     [ 3 ]
(%o3)                                [   ]
                                     [ 4 ]
(%i4) len(a);
(%o4)                                  5
(%i5) len([3, 4]);
(%o5)                                  5

1. Wert eines Blocks ist stets der Wert des letzten Ausdrucks.
2. vec kann hier Spalten- oder Zeilenvektor sein.


S. 101:

(%i1) mysolve(a, b) := block([lsg, varListe, len],
lsg : solve (a, b),
varListe : [r, s, t],
len : length(%rnum_list),
for i thru len do 
   lsg : subst (varListe[i], %rnum_list[i], lsg),
lsg )$
(%i2) gl1 : 2*x = y;
(%o2)                               2 x = y
(%i3) gl2 : 2*gl1;
(%o3)                              4 x = 2 y
(%i4) gleichungen : [gl1, gl2];
(%o4)                        [2 x = y, 4 x = 2 y]
(%i5) variablen : [x, y];
(%o5)                               [x, y]
(%i6) mysolve (gleichungen, variablen);

solve: dependent equations eliminated: (2)
                                     r
(%o6)                          [[x = -, y = r]]
                                     2
(%i7) mysolve(a, b) := block([lsg, varListe, maperror:false],
lsg : solve (a, b),
varListe : [r, s, t],
lsg : subst (map("=", %rnum_list, varListe), lsg),
lsg )$
(%i8) mysolve (gleichungen, variablen)$

solve: dependent equations eliminated: (2)
map: truncating one or more arguments.
(%i9) %;
                                     r
(%o9)                          [[x = -, y = r]]
                                     2

1. Wert eines Blocks ist stets der Wert des letzten Ausdrucks.
2. i ist durch 'for i : 1 thru' lokal im Block der for-Schleife.
   Kein 'block[i], for i : 1 thru' noetig.
3. 'block([],' ist gleichwertig mit '('.
4. 'subst([a=b], expr)' entspricht 'subst(b, a, expr)'.
5. 'maperror:false' verhindert Abbruch(, erzeugt jedoch Meldung ohne Zeilenumbruch).


S. 148:

(%i1) a : 1$ 
(%i2) for i thru 10 do print(a : next_prime(a))$
2 
3 
5 
7 
11 
13 
17 
19 
23 
29 
(%i3) prim : primes(0, 50);
(%o3)      [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
(%i4) rest(prim, 10 - length(prim));
(%o4)                [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

1. 1 ist nicht prim.
2. 'block([],' ist gleichwertig mit '('.
3. 'for i : 1 while i < 10 do' --> 9 Durchlaeufe


S. 149:

(%i1) r : ifactors (12);
(%o1)                          [[2, 2], [3, 1]]
(%i2) r : ifactors (12), factors_only : true;
(%o2)                               [2, 3]

S. 150:

(%i1) genauigkeit : 40$
(%i2) fpprec : genauigkeit + 3$
(%i3) z : bfloat (sqrt(2));
(%o3)           1.414213562373095048801688724209698078569672b0
(%i4) l : map(parse_string, charlist(substring(string(z), 3, fpprec)));
(%o4) [4, 1, 4, 2, 1, 3, 5, 6, 2, 3, 7, 3, 0, 9, 5, 0, 4, 8, 8, 0, 1, 6, 8, 8, 
                                7, 2, 4, 2, 0, 9, 6, 9, 8, 0, 7, 8, 5, 6, 9, 6]



S. 155:

(%i1) l : [1, 2, 3, 4];
(%o1)                            [1, 2, 3, 4]

(%i3) (sum:0, for n in l do sum : sum + n, sum);
(%o3)                                 10

1. 'block([],' ist gleichwertig mit '('.
2. Wert eines Blocks ist stets der Wert des letzten Ausdrucks.


S. 156:

(%i1) l : [1, 2, 3, 4];
(%o1)                            [1, 2, 3, 4]

(%i3) (prod:1, for n in l do prod : prod * n, prod);
(%o3)                                 24


(%i1) l : [Max, Willi, Peter];
(%o1)                         [Max, Willi, Peter]

(%i3) jungen : endcons(Paul, l);
(%o3)                      [Max, Willi, Peter, Paul]
(%i4) jungen : cons(Paul, l);
(%o4)                      [Paul, Max, Willi, Peter]


S. 158:

(%i2) liste2 : [ [3, 5], [1,8], [4, 2] ];
(%o2)                      [[3, 5], [1, 8], [4, 2]]

(%i4) xWerte : map(first, liste2);
(%o4)                              [3, 1, 4]


(%i1) l : [1, 2, 3, 4];
(%o1)                            [1, 2, 3, 4]
(%i2) f(x) := 2*x+1;
(%o2)                           f(x) := 2 x + 1
(%i3) map(f, l);
(%o3)                            [3, 5, 7, 9]


S. 159:

(%i1) liste : [ [1,2], [3,4], [5,6] ]$
(%i2) M : apply(matrix, liste);
                                   [ 1  2 ]
                                   [      ]
(%o2)                              [ 3  4 ]
                                   [      ]
                                   [ 5  6 ]


S. 160:

(%i1) load (eigen)$
(%i2) a : covect ([1, 2, 3])$
(%i3) [x, y, z] : args(map(first, a));
(%o3)                              [1, 2, 3]


(%i1) a : [1, 2, 3];
(%o1)                              [1, 2, 3]
(%i2) b : 2*a;
(%o2)                              [2, 4, 6]
(%i3) a + b, listarith : false; 
(%o3)                        [2, 4, 6] + [1, 2, 3]
(%i4) map("+", a,b);
(%o4)                              [3, 6, 9]

Die Auswertung des Ausdrucks Liste + Liste ist abhaengig von listarith (default=true).


S. 161:

(%i1) a : [1, 2, 3];
(%o1)                              [1, 2, 3]
(%i2) b : 2*a;
(%o2)                              [2, 4, 6]
(%i3) map("[", a,b);
(%o3)                      [[1, 2], [2, 4], [3, 6]]