A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the...

20
A morsel of Maxima Alasdair McAndrew March 26, 2007 This report details the information presented to the RGMIA group at a seminar titled “Maxima: An Open-source Computer Algebra System”, given on March 26, 2007: see http://rgmia.vu.edu.au/seminars/index.html. 1 A bit of history Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project MAC in the late 1960’s. By the mid 1980’s, although early versions of Mathematica and Maple were then gaining market share, Macsyma was clearly the strongest such system, with robust symbolics, excellent interfaces, and a fine hypertext help system. Macsyma was written in the Maclisp dialect of lisp, a language which many considered perfect for mathematical computations. In 1982, William Schelter, of the University of Texas at Austin, had managed to acquire the source code to a version of Macsyma which had been licensed to the US Department of Energy; this version was known as DOE Macsyma. The source code, adapted By Schelter for common lisp, was released under the GPL by Schelter in 1998. Schelter maintained this version of Macsyma (renamed GNU Maxima) until his death from a heart attack in 2001, at the untimely age of 52. Since then Maxima has been maintained by an enthusiastic group of developers, including several of the original developers of Macsyma. The original Macsyma, although sold as a commercial package until the mid 1990’s, lost ground to Maple and Mathematica partly because of some poor business decisions, and partly because of some of its weaknesses—in particular its numerics. However, such was the power and strength of Macsyma that some commentators believe that Maple and Mathematica have taken decades to get to the stage where Macsyma was. As of 2007, Macsyma still exists, and has been acquired by a holding company Tenedos LLC; this company had previously bought Symbolics, the distributor of Macsyma. Tenedos LLC has not released Macsyma again, or resold it. Symbolics still officially sell Macsyma. 2 Maxima now To all intents and purposes, Macsyma is now unobtainable, and the best current version of this once-great CAS is Maxima. Maxima has several points in its favour: it is licensed under the GPL; it is now, and always will be, free. it is being actively developed. the Maxima mail group is extremely active. because Maxima is being developed by its users, it is very attuned to its user community. 1

Transcript of A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the...

Page 1: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

A morsel of Maxima

Alasdair McAndrew

March 26, 2007

This report details the information presented to the RGMIA group at a seminar titled “Maxima:An Open-source Computer Algebra System”, given on March 26, 2007: seehttp://rgmia.vu.edu.au/seminars/index.html.

1 A bit of history

Maxima is a descendant of the computer algebra system Macsyma, which was initially developedat MIT as part of Project MAC in the late 1960’s. By the mid 1980’s, although early versionsof Mathematica and Maple were then gaining market share, Macsyma was clearly the strongestsuch system, with robust symbolics, excellent interfaces, and a fine hypertext help system.Macsyma was written in the Maclisp dialect of lisp, a language which many considered perfectfor mathematical computations.

In 1982, William Schelter, of the University of Texas at Austin, had managed to acquirethe source code to a version of Macsyma which had been licensed to the US Department ofEnergy; this version was known as DOE Macsyma. The source code, adapted By Schelterfor common lisp, was released under the GPL by Schelter in 1998. Schelter maintained thisversion of Macsyma (renamed GNU Maxima) until his death from a heart attack in 2001, atthe untimely age of 52. Since then Maxima has been maintained by an enthusiastic group ofdevelopers, including several of the original developers of Macsyma.

The original Macsyma, although sold as a commercial package until the mid 1990’s, lostground to Maple and Mathematica partly because of some poor business decisions, and partlybecause of some of its weaknesses—in particular its numerics. However, such was the powerand strength of Macsyma that some commentators believe that Maple and Mathematica havetaken decades to get to the stage where Macsyma was.

As of 2007, Macsyma still exists, and has been acquired by a holding company Tenedos LLC;this company had previously bought Symbolics, the distributor of Macsyma. Tenedos LLC hasnot released Macsyma again, or resold it. Symbolics still officially sell Macsyma.

2 Maxima now

To all intents and purposes, Macsyma is now unobtainable, and the best current version of thisonce-great CAS is Maxima. Maxima has several points in its favour:

• it is licensed under the GPL; it is now, and always will be, free.

• it is being actively developed.

• the Maxima mail group is extremely active.

• because Maxima is being developed by its users, it is very attuned to its user community.

1

Page 2: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

• Maxima, in its current form, has excellent, calculus, linear algebra and general symboliccapabilities.

However, being based on an early version of Macsyma, many of the later refinements ofMacsyma—believed to be in the order of 50 man-years of work—have not made it as far asMaxima (yet). So Maxima is deficient in some areas:

• its interface is non-standard, a bit clunky and described as “something out of the 1970’s”.

• it is very weak in discrete mathematics.

• its numerics are still weak—there is still no robust solid method for finding numeric rootsof (systems of) nonlinear equations.

• its plotting is based on Gnuplot, which is good at plotting functions y = f(x) or z = f(x, y)and for plotting data sets, but not for much else. It can’t, for example, plot two surfacesat once and so show their intersections.

• Maxima does not have a Package system, such as Maple and MuPAD; individual filescan be loaded into a running session, and a single file may contain one or more Maximacommands.

• In its current form, Maxima is unsuitable as a teaching tool.

Maxima’s home on the Web is at http://maxima.sourceforge.net/index.shtml and thispage should provide a jumping off point for any investigations into it. There are screenshots,tutorials, other documentation, and links to related projects such as other CASs or Maximainterfaces.

3 Interfaces

Maxima has several interfaces, from plain console, to some reasonable full featured windowsapplications. The main difference between them is their ability to output typeset mathematics.

Figure 1 shows input and output taken from a window running Maxima in console mode;the result, typeset in ASCII, although readable, is not pleasant.

Figure 1: Maxima running in console mode

A slightly better interface, in that it allows cutting and pasting between commands, andincorporates a help browser, is Xmaxima, which comes standard with the software. Xmaximaalso allows included graphics, and provides a sort of very cut-down windowing interface.

2

Page 3: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

Figure 2: Maxima running in TeXmacs

A better result is obtained by running Maxima within the text processor TeXmacs; aninput-output pair is shown in figure 2.

Probably the best interface under MS Windows is wxMaxima, which is a third party projectdeveloped by Andrej Vodopivec; you can find it at http://wxmaxima.sourceforge.net/wiki/index.php/Main_Page. It is built with the wxWidgets toolkit, so is designed to work acrossdifferent platforms. It provides reasonably well typeset output, a good menu system, and accessto a help browser. An example of its typesetting is shown in figure 3.

Figure 3: Typesetting in wxMaxima

Further screenshots are available from the Maxima project page. There are also severalmodes which allow Maxima to run within the text editor Emacs; one of these modes (imaxima)provides TeX-formatted output.

4 Some examples of Maxima in use

Arithmetic

Maxima can, as all good CAS’s can, perform arithmetic to arbitrary precision:

(%i1) 2+3;

(%o1)

5

(%i2) 3^1000;

(%o2)

1322070819480806636890455259752144365965422032752148167664920368226828597346704899540778313850608061963909777696872582355950954582100618911865342725257953674027620225198320803878014774228964841274390400117588618041128947815623094438061566173054086674490506178125480344405547054397038895817465368254916136220830268563778582290228416398307887896918556404084898937609373242171846359938695516765018940588109060426089671438864102814350385648747165832010614366132173102768902855220001

(%i3) sum(1/n,n,1,100);

3

Page 4: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

(%o3)

14466636279520351160221518043104131447711

2788815009188499086581352357412492142272

(%i4) fpprec:80

(%i5) bfloat(%pi);

(%o5)

3.141592653589793238462643383279502884197169399375105820974944592307816406286209b0

Polynomials and rational functions

We can expect the usual things: expansion and factorization, division with remainder, partialfractions. Maxima in fact has a suite of commands to enable the efficient handling of suchexpressions.

(%i6) expand((x+y/2-3*z)^5);

(%o6)

−243z5 +405yz4

2+ 405xz4 −

135y2z3

2− 270xyz3 − 270x2z3 +

45y3z2

4

+135xy2z2

2+ 135x2yz2 + 90x3z2 −

15y4z

16−

15xy3z

2−

45x2y2z

2

− 30x3yz − 15x4z +y5

32+

5xy4

16+

5x2y3

4+

5x3y2

2+

5x4y

2+ x5

(%i7) factor(%)

(%o7)

−(6z − y − 2x)5

32

One nice application of factor is to factor polynomials over a field extended by an elementwhose minimal polynomial is given:

(%i8) factor(x^4+1);

(%o8)

x4 + 1

(%i9) factor(x^4+1,a^2+2);

(%o9)

(x2 − ax− 1)(x2 + ax− 1)

And of course partial fractions:(%i10) expand((x^3/(x^2+1))^4);

(%o10)

x12

x8 + 4x6 + 6x4 + 4x2 + 1

4

Page 5: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

(%i11) partfrac(%,x);

(%o11)

−20

x2 + 1+

15

(x2 + 1)2−

6

(x2 + 1)3+

1

(x2 + 1)4+ x4 − 4x2 + 10

Calculus

As you would expect, limits (directionless, or with a direction given), differentiation, integration,Taylor series:

(%i12) limit((sqrt(x^2+x+23)-5)/(x-1),x,1)

(%o12)

3

10

(%i13) k(x):=(3*x-15)/(x^2-10*x+25)^(1/2);

(%o13)

k(x) :=3x− 15

(x2 − 10x + 25)1

2

(%i14) limit(k(x),x,5,plus);

(%o14)

3

(%i15) limit(k(x),x,5,minus);

(%o15)

−3

(%i16) diff(bessel_i(0,x),x,4);

(%i17) radcan(%);

(%o17)

bessel i(0, x)x3 − 2 bessel i(1, x)x2 + 3 bessel i(0, x)x − 6 bessel i(1, x)

x3

(%i18) integrate(1/(x^5+1),x);

(%o18)

(√

5− 3) log(2x2 + (√

5− 1)x + 2)

10− 10√

5−

(√

5 + 3) log(2x2 + (−√

5− 1)x + 2)

10√

5 + 10

+(√

5 + 1) arctan

(

4x+√

5−1√(2√

5+10)

)

√5√

(2√

5 + 10)+

(√

5− 1) arctan

(

4x−√

5−1√(10−2

5)

)

(√

5√

(10− 2√

5))+

log(x + 1)

5

(%i19) t:taylor(sqrt(x^2+1),x,0,10);

(%o19)

1 +x2

2−

x4

8+

x6

16−

5x8

128+

7x10

256

(%i20) pade(t,6,6);

(%o20)[

−70x4 + 224x2 + 160

x6 − 18x4 − 144x2 − 160,x6 + 18x4 + 48x2 + 32

6x4 + 32x2 + 32

]

5

Page 6: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

Linear Algebra

We’ll create a few matrices and play with them:

(%i21) A:matrix([1,2,3],[4,5,6],[7,8,-9]);

(%o21)

1 2 34 5 67 8 −9

(%i22) B:matrix([1,-2,3],[-4,5,-6],[7,8,9]);

(%o22)

1 −2 3−4 5 −6

7 8 9

(%i23) A.B

(%o23)

14 32 1826 65 36−88 −46 −108

(%i24) invert(A);

(%o24)

−3118

79

118

139 −5

919

− 118

19 − 1

18

(%i25) charpoly(B,alpha);

(%i26) expand(%);

(%o26)

−α3 + 15α2 − 78α − 96

We can also compute eigenvalues:(%i27) eigenvalues(A);

(%o27)

(

−(√

3 i)

/2−1

2

)

(

3√

8682 i− 37) 1

3 +43

((√3 i

)

/2− 12

)

(

3√

8682 i− 37)

1

3

− 1,

(

(√3 i

)

/2−1

2

)

(

3√

8682 i− 37) 1

3 +43

(

−(√

3 i)

/2− 12

)

(

3√

8682 i− 37) 1

3

− 1,

(

3√

8682 i− 37) 1

3 + 43/

(

(

3√

8682 i− 37) 1

3

)

− 1

, [1, 1, 1]

6

Page 7: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

As you can see, the eigenvalues have been computed by attempting to solve the characteristicpolynomial of the matrix. A more useful result can be obtained by:

(%i28) allroots(charpoly(A));

(%o28)

[x = −0.42488196531247785, x = 10.05933172129618, x = −12.634449755983702]

Until the LAPACK libraries are fully integrated into Maxima (a project currently underway),Maxima’s ability to perform numerical linear algebra, such as calculating the eigenvalues of alarge matrix, is limited.

Variables and functions

As we have indicated above, a variable is assigned with the colon operator. Named functionsare defined with colon equals:

(%i29) f(x):=if x<0 then x else x^2;

(%o29)

f(x) := if x < 0 then x else x2

(%i30) f(-3);

(%o30)

−3

(%i31) f(3);

(%o31)

9

Unnamed functions are defined with the lambda operator. We can use this, for example, todefine a matrix whose elements are functions of the row and column indices:

(%i32) genmatrix(lambda([i,j],i^2-j^2+1),3,3);

(%o32)

1 −2 −74 1 −49 6 1

For recursive functions, whose call may result in a long tree of other recursive calls, it may bemore convenient to define an array function, where the function delimiters are square brackets,rather than parentheses. Here is Hofstadter’s chaotic function:

(%i33) q[n]:=if n<2 then 1 else q[n-q[n-1]]+q[n-q[n-2]];

(%o33)

q[n] := if x < 2 then 1 else q[n− q[n− 1]] + q[n− q[n− 2]]

(%i34) makelist(q[n],n,1,30);

(%o34)

[1, 2, 3, 3, 4, 5, 5, 6, 6, 6, 8, 8, 8, 10, 9, 10, 11, 11, 12, 12, 12, 12, 16, 14, 14, 16, 16, 16, 16, 20]

7

Page 8: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

Solving equations

Maxima can solve many algebraic equations, some numerically, some symbolically:

(%i35) p(x):=3x^3-18x^2+33x-19;

(%i36) s:solve(p(x)=0,x);

(%i37) map(lambda([x],trigsimp(rectform(x))),s);

(%o37)

[

x =3 sin

(

π

18

)

−√

3 cos(

π

18

)

+ 6

3, x = −

3 sin(

π

18

)

+√

3 cos(

π

18

)

− 6

3, x =

2√

3 cos(

π

18

)

+ 6

3

]

(%i38) eq1:a*x+b*y+c*z=A;

(%i39) eq2:d*x+e*y+f*z=B;

(%i40) eq3:g*x+h*y+i*z=C;

(%i41) linsolve([eq1,eq2,eq3],[x,y,z]);

(%o41)

[

x =b (iB − f C) + c (eC − hB) + (f h− e i) A

a (f h− e i) + b (d i− f g) + c (e g − dh),

y = −a (iB − f C) + c (dC − g B) + (f g − d i) A

a (f h− e i) + b (d i− f g) + c (e g − dh),

z =a (hB − eC) + b (dC − g B) + (e g − dh) A

a (f h− e i) + b (d i− f g) + c (e g − dh)

]

(%i42) allroots(p(x));

(%o42)

[x = 1.257772801031441, x = 1.605069156365301, x = 3.137158042603259]

Differential equations

Maxima can solve many differential equations with ease; let’s try a simple second order linearODE

d2y

dx2− 3

dy

dx+ 2y = x2, y(0) = 1, y(1) = 3.

This can be done in three steps: first defining the equation, then solving it, and finally enteringthe initial conditions:(%i43) de:’diff(y,x,2)-3*’diff(y,x)+2*y=x^2;

(%o43)

d2

dx2y − 3

(

d

dxy

)

+ 2y = x2

(%i44) s:ode2(de,y,x);

(%o44)

y = %k1 e2x + %k2 ex +2x2 + 6x + 7

4

(%i45) bc2(s,x=0,y=1,x=1,y=3);

(%o45)

y =3e2x−1

4−

(3e + 3)ex−1

4+

2x2 + 6x + 7

4

8

Page 9: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

There are various user-provided functions to solve particular types of differential equations. Forexample, here is one of the Riccati type:

dy

dx= x2 + y2, y(1) = 1.

To solve this, first we must load the ode1 riccati.mac file, which contains the necessaryMaxima programs to handle such ODE’s. As above, we first define the equation, then solve it,and lastly enter the initial condition.

(%i46) load(ode1_riccati);

(%i47) de:’diff(y,x)=x^2+y^2;

(%i48) s:ode1_riccati(de,y,x);

(%o48)

(

bessel j(

−54 , x2

2

)

%c + bessel j(

−34 , x2

2

))

x2 + bessel j(

−14 , x2

2

)

%c(

bessel j(

−14 , x2

2

)

%c + bessel j(

14 , x2

2

))

x

(%i49) ic1(s,x=1,y=1);

(%o49)

y = −

(

bessel j(

−34 , x

2

2

)

bessel j(

−14 , 1

2

)

+ bessel j(

−54 , 1

2

)

bessel j(

−34 , x

2

2

)

− bessel j(

−54 , x2

2

)

bessel j(

−34 , 1

2

))

x2 − bessel j(

−34 , 1

2

)

bessel j(

−14 , x2

2

)

((

bessel j(

−14 , 1

2

)

+ bessel j(

−54 , 1

2

))

bessel j(

14 , x2

2

)

− bessel j(

−34 , 1

2

)

bessel j(

−14 , x2

2

))

x

Maxima’s knowledge and handling of Bessel functions isn’t up to simplifying this magnificentexpression.

Although Maxima doesn’t have an inbuilt command for producing a series solution to anODE, such a solution can be generated very easily by repeatedly integrating the equation. Thisis Picard’s method and for the general equation

dy

dx= f(x, y), y(a) = b

the initial value is y = b and then the iteration

y ← b +

x

a

f(x, y) dx

is used to obtain better approximations to y. For the Riccati equation above, with the simplerinitial condition y(0) = 1, the integration starts with y = 1 and then uses the iteration

y ← 1 +x3

3+

y2 dx

until as many terms of the series as desired have been computed. One problem with applyingthis integration is that the computation quickly becomes swamped with large powers of x. Tokeep the computation manageable, Maxima provides the useful command tellrat, which inthe form tellrat(X) means substitute 0 for X in algebraic expressions. So, here goes:

9

Page 10: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

(%i50) tellrat(x^11);

(%i51) algebraic:true;

(%i52) for i : 1 thru 12 do y : rat(1+x^3/3+integrate(y^2,x));

(%i53) expand(y);

(%o53)

1961x10

1400+

428x9

315+

369x8

280+

404x7

315+

37x6

30+

6x5

5+

7x4

6+

4x3

3+ x2 + x + 1

Plotting

Maxima’s plotting is based on the Gnuplot software, which allows for plotting of two and threedimensional functions, and datasets. So a plot2d or plot3d command collects the functionsand parameters, and passes them all to Gnuplot for plotting. Gnuplot is bundled with Maxima,so there is no need to install it separately.

Here is a selection of different plot commands, starting with two simple plots: a function,and a parametric plot:

(%i54) plot2d (sin(x)+sin(3*x)/3, [x, -2*%pi, 2*%pi])$

(%i55) plot2d ([parametric, cos(t), sin(3*t), [t, 0, %pi*2],[nticks,180]]);

The outputs of these commands are shown in figure 4.

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

-8 -6 -4 -2 0 2 4 6 8

sin(3*x)/3+sin(x)

-1

-0.8

-0.6

-0.4

-0.2

0

0.2

0.4

0.6

0.8

1

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

cos(t), sin(3*t)

Figure 4: Simple plots

Next we show a point plot; in this case a plot of the first 1000 values of Hofstadter’s chaoticfunction:

(%i56) xs:makelist(n,n,1,1000)$

(%i57) qs:makelist(q[n],n,1,1000)$

(%i58) plot2d([discrete,xs,qs],[gnuplot_curve_styles,["with points"]]);

This graph is shown in figure 5.Finally, a clutch of three-dimensional plots, all with different parameters:

(%i59) plot3d(sin(sqrt(x^2+y^2))/sqrt(x^2+y^2),[x,-4*%pi,4*%pi],[y,-4*%pi,4*%pi])$

(%i60) plot3d(sin(sqrt(x^2+y^2))/sqrt(x^2+y^2),[x,-4*%pi,4*%pi],[y,-4*%pi,4*%pi],

[’grid,180,180])$

10

Page 11: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

0

100

200

300

400

500

600

700

0 100 200 300 400 500 600 700 800 900 1000

discrete1

Figure 5: A point plot

(%i61) plot3d(sin(sqrt(x^2+y^2))/sqrt(x^2+y^2),[x,-4*%pi,4*%pi],[y,-4*%pi,4*%pi],

[plot_format,openmath])$

(%i62) plot3d ([cos(x)*(3 + y*cos(x/2)), sin(x)*(3 + y*cos(x/2)),y*sin(x/2)],

[x, -%pi, %pi], [y, -1, 1], [’grid, 50, 15])$

These graphs are shown in figure 6.

User contributed functions

Many of Maxima’s commands are automatically loaded when Maxima is started. But there is avast collection of further material, stored within the share directory, which extends Maxima’sfunctionality. Much of this material has been provided by users; some, after enough testing,gets moved into the Maxima “core” with newer releases.

Some of these functions are:

• Symbolic evaluation (using the Gosper-Zeilberger algorithm) of finite sums.

• Solving difference equations.

• Solving particular types of differential equations.

• String processing.

• The simplex algorithm for linear programming.

• Some numerical routines for interpolation, FFT, quadrature.

• LU and Cholesky factorization of matrices.

Here are a few examples; first a symbolic sum

n∑

k =1

1

k(k + 1)(k + 2)

(%i63) load(zeilberger);

(%i64) GosperSum(1/(k*(k+1)*(k+2)),k,1,n);

11

Page 12: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

-15-10

-5 0

5 10

15-15-10

-5 0

5 10

15

-0.4-0.2

0 0.2 0.4 0.6 0.8

1

Function

-15-10

-5 0

5 10

15-15-10

-5 0

5 10

15

-0.4-0.2

0 0.2 0.4 0.6 0.8

1

Function

-4 -3 -2 -1 0 1 2 3 4-4-3

-2-1

0 1

2 3

4

-1

-0.5

0

0.5

1

Function

Figure 6: Three dimensional plots

12

Page 13: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

(%o64)

1

4−

1

2 (n + 1) (n + 2)

Now a difference equation;

an+2 − 5an+1 + 6an = n2, a0 = 2, a1 = 0.

(%i65) load(recur);

(%i66) char(a(n+2)-5*a(n+1)+6*a(n),n^2,a,n,2,[a(0)=2,a(1)=0]);

(%o66)

a (n) = −7 3n

2+ 32n +

n2

2+

3n

2+

5

2

Now an example of string processing: reducing a string of capital letters into a sequence givingtheir position from the start of the alphabet (so A = 0, B = 1, C = 2 and so on.):

(%i67) load(stringproc);

(%i68) str:"GIVEMEALLYOURMONEY";

(%i69) strl:charlist(str);

(%o69)

[G, I,V,E,M,E,A,L,L,Y,O,U,R,M,O,N, E,Y]

(%i70) stn:maplist(lambda([x],cint(x)),strl);

(%o70)

[71, 73, 86, 69, 77, 69, 65, 76, 76, 89, 79, 85, 82, 77, 79, 78, 69, 89]

(%i71) stn2:maplist(lambda([x],x-65),stn);

(%o71)

[6, 8, 21, 4, 12, 4, 0, 11, 11, 24, 14, 20, 17, 12, 14, 13, 4, 24]

Of course the last two commands could be written as one, using lambda([x],cint(x)-65).

5 Programming in Maxima

As we have indicated earlier, Maxima contains all the programming structures required tobuild programs of any complexity. Maxima’s base language is Lisp, but Maxima can either beprogrammed in Lisp, or in its own language.

We shall look at a very simple program: Heun’s method (or the Modified Euler Method)1

for solving a single ODE numerically.The ODEs to be solved are of the form

dy

dx= f(x, y), y(a) = b

1Some writers distinguish carefully between these two; others claim they are two descriptions of the same

method. I don’t care, really.

13

Page 14: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

Assuming that (xn, yn) is a point on the solution curve (for example, x = a, y = b), then anothersolution point (xn+1 = xn + h, yn+1) can be determined by integrating through the equation:

yn+1 = yn +

xn+1

xn

f(x, y) dx.

We can approximate the integral by the left-hand rule∫

b

a

f(x) dx ≈ (b− a)f(a)

which leads toyn+1 = yn + hf(xn, yn).

This is known as Euler’s method. Heun’s method is obtained by approximating the integralwith the trapezoidal rule:

b

a

f(x) dx ≈b− a

2(f(a) + f(b))

which leads to

yn+1 = yn +h

2(f(xn, yn), f(xn+1, yn+1)) .

The trouble with this equation is that the right hand side refers to yn+1, which we don’t actuallyknow. We overcome this by substituting the value obtained from Euler’s method above; thus:

yn+1 = yn +h

2(f(xn, yn), f(xn+1, yn + hf(xn, yn))) .

This is normally written as a sequence of three steps:

xn+1 = xn + h

pn+1 = yn + hf(xn, yn)

yn+1 = yn +h

2(f(xn, yn) + f(xn+1, pn+1)) .

In this form it is a simple example (in fact, the simplest example), of a predictor-corrector

method: the value of yn+1 is first predicted (by Euler’s method) and then corrected (by thetrapezoidal rule).

Now to programming. Rather than produce three sequences xn, yn and pn, we shall justhave the variables x, y, xnew ynew and p. The heart of our program, the bit which actuallydoes the calculation, will be:

xnew:x+h,

p:y+h*f(x,y),

ynew:y+(h/2)*(f(x,y)+f(xnew,p)

This will be placed inside a loop, which will be computed a number of times entered by theuser; this value will be denoted steps:

for i:1 thru steps do

block(

xnew:x+h,

p:y+h*f(x,y),

ynew:y+(h/2)*(f(x,y)+f(xnew,p),

x:xnew,

y:ynew

),

14

Page 15: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

where in Maxima the block construct simply groups together a list of commands and treatsthem as a single statement. Now we can turn the whole thing into a command heun, as follows:

heun(f,initial,xfinal,steps):=block(

[h:(xfinal-initial[1])/steps,

x:initial[1],

y:initial[2],

xnew,

ynew,

p],

for i:1 thru steps do block(

xnew:float(x+h),

p:float(y+h*f(x,y)),

ynew:float(y+(h/2)*(f(x,y)+f(xnew,p))),

x:xnew,

y:ynew

),

return([x,y])

);

The first block statement allows us to initialize some local variables; these are delineated bysquare brackets. The initial value is a list of two elements, being the initial values (x, y) =(a, b). The xfinal variable is the value of x for which we require the corresponding y value.And steps is the number of steps! We have also placed float calls around the calculationsfor xnew, ynew and p to ensure that all values are computed numerically. This program can besaved in a file called heun.mac and placed somewhere in Maxima’s search path.

Now for an example:dy

dx= y − x, y(0) = 2

for which the analytic solution can be determined to be y(x) = ex + x + 1. Try y(1):

(%i72) load(heun);

(%i73) f(x,y):=y-x;

(%i74) heun(f,[0,2],1,10),

(%o74)

[.9999999999999999, 4.714080846608224]

(%i75) heun(f,[0,2],1,20),

(%o75)

[1.0, 4.717191054354884]

(%i76) exp(1.0)+1+1;

(%o76)

4.718281828459045

The program could be easily modified to list intermediate values of x and y, or to write allvalues to an array for nice display. Also, the program assumes that the variables will be x andy; it doesn’t allow the use of different variables.

15

Page 16: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

6 Help and documentation

Plenty of help is provided. Its main fault is that it is not properly typeset: all the mathematicalmaterial is presented as ASCII. Here, for example, is a section for the help section on integration:

(%i1) assume(a > 0)$

(%i2) ’integrate (%e**sqrt(a*y), y, 0, 4);

4

/

[ sqrt(a) sqrt(y)

(%o2) I %e dy

]

/

0

The advantage of using ASCII though, means that Maxima’s help can be read on many systemsand interfaces, from plain console mode to a PDF file (in which the math still appears asASCII). So a loss, possibly, of typesetting elegance may be compensated by flexibility. Manyusers, however, will consider the slightly clunky appearance as a negative.

7 Some current Maxima projects

MaximaPHP

This is a very new project, available at http://www.my-tool.com/mathematics/maximaphp/.It aims to provide access to Maxima from any web browser, without the user having to downloadand install Maxima first. Although it is still in early stages of development, it provides an easyway of testing out Maxima’s strengths.

Figure 7 shows part of a screenshot of MaximaPHP in action. As you see, output is presentedas ASCII.

Z transforms

Although Maxima contains some Laplace transform functionality, the discrete counterpart, thez-transform, does not yet exist. However, a z-transform package is currently underway; it isan excellent example of Maxima’s rules and patterns usage. Here is a selection of some of therules:

defrule(r5,zt(a^(n-1),n,z),1/(z-a));

defrule(r6,zt(n^b,n,z),block([tmp:z/(z-1)],

for i:1 thru b do tmp:-z*diff(tmp,z),

factor(tmp)));

defrule(r7,zt(sin(a*n),n,z),z*’sin(a)/((z^2-2*z*’cos(a)+1)));

defrule(r8,zt(cos(a*n),n,z),z*(z-’cos(a))/(z^2-2*z*’cos(a)+1));

defrule(r9,zt(’delta(c,n),n,z),1/z^c);

Although the package is still in “pre-alpha” stage, it can be used to solve some simple differenceequations. Here it is on the difference equation we solved earlier:

16

Page 17: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

Figure 7: MaximaPHP

17

Page 18: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

(%i77) funmake(a,[n]);a[0]:2;a[1]:0;

(%i78) eq:a(n+2)-5*a(n+1)+6*a(n)-n^2;

(%o78)

a (n + 2)− 5 a (n + 1) + 6 a (n)− n2

(%i79) zte:ztrans(eq,n,z);

(%o79)

(zt (a (n) , n, z)− 2) z2 −z (z + 1)

(z − 1) 3− 5 (zt (a (n) , n, z)− 2) z + 6 zt (a (n) , n, z)

(%i80) s:solve(zte,zt(a(n),n,z));

(%o80)[

zt (a (n) , n, z) =2 z5 − 16 z4 + 36 z3 − 31 z2 + 11 z

z5 − 8 z4 + 24 z3 − 34 z2 + 23 z − 6

]

(%i81) izt:rhs(s[1]);

(%i82) invztrans(izt,z,n);

(%o55)

−7 3n

2+ 32n +

(n− 1) n

2+ 2n +

5

2

STACK

“STACK” is the acronym for “System for Teaching and Assessment using a Computer algebraKernel” and is the brainchild of Chris Sangwin, of the University of Birmingham. It can be seenat http://www.stack.bham.ac.uk/index.php. STACK aims to provide an online learning andassessment tool, using Maxima as its underlying mathematical “engine” in the same way thatMaple TA uses Maple. A screenshot is shown in figure 8.

STACK is a full-featured and mature product, and is capturing the interest of teachersworld-wide. As with Maxima itself, STACK is free for download and use.

8 How good is Maxima?

Basically, we can say that it is very good at what it does, but it doesn’t have the breadth ofits rivals. There is no standard benchmark for CASs, and the last major attempt to comparethem dates back to 1999. However, Maxima stands up pretty well for basic mathematics, as wehave seen. Here’s a simple table which sums up what we know so far (tick for “good”, cross for“bad”, empty box for “could do better”):

Algebra Polynomials 2�

Calculus Basic 2� Advanced 2

Linear Algebra Matrices and vectors 2� Numerical linear algebra 4

Solution of equations Polynomials 2� Other equations 2

Systems of non-linear equations 4

Graphics Simple functions 2� Data sets 2�

Advanced graphics 4 Geometric figures 4

Differential equations First, second order 2� Higher order 2

Help Content 2� Appearance 4

18

Page 19: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

Figure 8: STACK

19

Page 20: A morsel of Maxima - UMKmuch/MK/samouczki/maxima_rgmia.pdf · Maxima is a descendant of the computer algebra system Macsyma, which was initially developed at MIT as part of Project

9 Conclusions

Maxima is full-featured, robust and powerful software, continually being upgraded and en-hanced. It is also easy to extend. It does not have the power and breadth of Maple or Mathe-matica (yet!), but it is a very clean system, elegantly programmed, and of course available onmany platforms—even, as we have seen, online.

As to whether Maxima will suit your needs, the only way to find out is to test it: downloadit, play with it, investigate it.

Over to you!

20