The Maxima Workshop

This is a workshop about the crossplatform computer algebra system Maxima. Its focus is on linear algebra, with a side line in differentiation and integration.


Table of contents

  1. Introduction
    1. What is Maxima
    2. Installation and system requirements
  2. About this document
  3. Copyright
  4. First steps
    1. Starting Maxima
    2. How to type commands
    3. Getting help
    4. Quitting Maxima
  5. Basic examples
    1. Basic arithmetic operations
    2. Basic mathematical functions
    3. Stacking functions
    4. Number conversion
  6. Variables and constants
  7. Writing functions and basic differentiation and integration
  8. Simple equations
  9. Simplifying and solving polynomials
  10. File input and output
  11. Comparison between Maxima and other systems
  12. Conclusion
  13. Thanks and acknowledgements
  14. Appendix A: functions and constants
    1. Alphabetical list of Maxima constants and variables
    2. Alphabetical list of Maxima functions

[Back to top]


Introduction

What is Maxima

Maxima is a computer algebra system (CAS), which works under Linux, MacOS and windows. It is a Common Lisp version of the Macsyma package, originally developed at MIT. "It is an interactive expert system and programming environment for symbolic and numerical mathematical manipulation."

Practically speaking: Maxima offers a command-line interface and a powerful programming language to solve mathematical problems from simple one-liners to very advanced calculations. It is comparable to Octave and R (both also demonstrated at Summer University) or Matlab and Maple. Its strong points lie both in its mathematical capabilities and its programming - using an ALGOL-like syntax or lisp functions and pattern matching.

Maxima is free software under the GNU General Public License (GPL). You should have received a copy of this license with your version of Maxima. If not, please take a look at the GNU GPL website. This means, that you can download, use, modify and redistribute Maxima free of charge, as long as you include original copyright notices. Usage is not restricted to non-commercial contexts. Please read the GPL for further information on legal issues.

Maxima's history is a long one. In the late 1960s the MIT - funded by the united states Department of Energy - developed the mathematical system Macsyma. In 1982 William Schelter started to develop his own branch written in Common Lisp. That was the birth of Maxima. In 1998 Schelter got permission from the Department of Energy to release his version as open source software. So we have here a system with over 40 years of development and knowledge at our fingertips.

[Back to top]


Installation and system requirements

You can download packages for windows and MacOS from the official Sourceforge.net project download page. Every Linux distribution should offer Maxima packages via the package manager (RPM, DEB, EBuild). To follow this workshop a version >=5.21.1 is sufficient.

There is nothing to be found about system requirements in the official documentation. CPU and memory usage depend on the tasks performed. Requirements for startup are minimal (tested on an 800MHz single core CPU).

[Back to top]


About this document

This document is structured into comprehensive sections, each accessible from the table of contents at the top.

Each section has a link below it, to bring you back to the top of the document, i.e. the table of contents.

Notations in this document:

[Back to top]


Copyright

This document is copyright © 2012 Julien Claassen. You are welcome to redistribute it and use it in non-commercial contexts, under the condition, that this copyright is present and no fee is charged.

This document contains citations from the Maxima Unix manpage and information taken from the Maxima website and online manual. These citations are copyrighted by their respective copyright holders This document also mentions commercial as well as non-commercial applications, which too are the property of their respective copyright holders.

[Back to top]


First steps

Starting Maxima

In Linux - or wherever you have a command-line prompt - starting Maxima is easy. You just type:

$ maxima

On windows, if correctly installed, you should find Maxima following these instructions:

  1. Go to start menu
  2. Click all programs
  3. Look for Maxima

Unfortunately I don't have any information on how to find the program under MacOSX, yet I suspect, that following the usual way to start any Mac program should take you there. If someone can provide more specific information, please drop me an e-mail.

When Maxima is started, it will display a short message and a prompt looking like this:
(%i1)

Congratulations, you are now ready to type commands and explore Maxima.

[Back to top]


How to type commands

Maxima is based on Lisp, a functional programming language. This is no need to panic! Users experienced with functional programming, can skip this section.

A valid command in Maxima looks like this:
command();<ENTER>

Let's take this apart:

NOTE: Intermediate and advanced users may wish to know, that it is possible to type more than one command after a single prompt, separating commands by a semicolon. This is, however, not advisable for novices, since it makes error detection more difficult and generally looks more confusing.

The only exception to this form of commands are direct expressions. That means assignments and basic operations.

[Back to top]


Getting help

There are several ways to get help. There are external sources of help:

From within Maxima you can use its own powerful help system, using these commands:

You can try the following examples:

[Back to top]


Quitting Maxima

The other very important step to know is, how to leave Maxima. The command is called quit. So you type:

(%i3) quit();

and then press the ENTER key.

This is to my knowledge the only way to quit Maxima. Control-c does not work. Unix users may of course use the external command:

$ killall -15 maxima

[Back to top]


Basic examples

Let us begin the real work now, with some simple examples; typical pocket calculator tasks. Those of you, who are familiar with mathematical applications in general, can skip this section.

Before we start however, those using assistive technology want to change one setting. Maxima normally has a two dimensional display. So fractions are displayed semi-graphically. We change that by typing:

(%i0) display2d:false;
(%o0) false

[Back to top]


Basic arithmetic operations

(%i1) 763 + 854;
(%o1) 1617
(%i2) 696 / 12;
(%o2) 58
(%i3) 12 * 7 + 3;
(%o3) 87

Of course we can change operator precedence by using parenthesis '(' and ')':

(%i4) 12 * (7 + 3);
(%o4) 120

[Back to top]


Basic mathematical functions

The last feature missing for the pocket calculator are the basic trigonometric and exponential functions. Sine, cosine, tangent, the logarithm and the exp function. They have the usual names: sin, cos, tan, log and exp.

(%i5) sin(0);
(%o5) 0
(%i6) log(1);
(%o6) 0

Now here is an example of Maxima's strength:

(%i7) 16 * 3 / 18;
(%o7) 8/3

Maxima won't automatically display numerical approximations. It will rather show exact fractions and cancel them for you. The same goes for roots:

(%i8) sqrt(2 * 3);
(%o8) sqrt(6)

So you can calculate with exact results until the end.

[Back to top]


Stacking functions

It is possible to stack, or concatenate, functions. So you can evaluate the sine of the logarithm of 2. Or you might ask Maxima to simplify a result of a complex operation or print it in TeX syntax. Let us see one simple example:

(%i9) log(sqrt(2));
(%o9) log(2)/2

This is a very useful technique, which we will use throughout this workshop.

[Back to top]


Number conversion

You can convert any fraction or other helpfully exact symbol to a numerical approximation with either float or bfloat. The latter is more exact. Example:

(%i10) float(sqrt(6));
(%o10) 2.449489742783178

[Back to top]


Variables and constants

To use a constant you prefix it with a percent sign "%constant". So now we can ask Maxima to calculate the result of the natural logarithm of e:

(%i11) log(%e);
(%o11) 1

Now you may notice, that the Maxima prompt rather looks like a constant. Indeed it is one. The %inumber and %onumber are constants, which you can use. The %onumber constant will translate to the result of that operation and the %inumber constants will translate to the corresponding input. This can be of use, when you have long terms with variables, which you'd like to change and then evaluate again. Let us give it a try:

(%i12) %o11;
(%o12) 1

NOTE: %inumber will literally print the input, to evaluate it, you have to use the ev() function, see Appendix A.

Variables in Maxima are straight forward with only one hitch. The assignment operator is not the equal sign '=', but a colon ':'. So to assign the value of 6 to the variable stupid, you type:

(%i13) stupid : 6;
(%o13) 6

Of course this really is stupid and we would rather like to assign the result of some calculation to a variable, so let's try this:

(%i14) clever : sin(%pi/2) * log(2);
(%o14) log(2)

Now we have two options to get a numerical approximation of this result. We could use the constant provided by Maxima - %o14 in this case - or we can use our variable. Let's try the latter:

(%i15) float(clever);
(%o15) 0.69314718055995

See Appendix A for a short list of known functions and constants.

[Back to top]


Writing functions and basic differentiation and integration

A typical use for a mathematical application is to evaluate complex functions. Writing functions is straight forward.

(%i16) f(x) := x^2 + 6 * x + 9;
(%o16) f(x):=x^2+6*x+9

Remember always to use the * as the multiplication operator.

So now we can evaluate this function at specific points:

(%i17) f(6);
(%o17) 81
(%i18) f(−5);
(%o18) 4

We can also try to factorise the function:

(%i19) factor(f(x));
(%o19) (x+3)^2

Now we can find the roots of this function:

(%i20) allroots(f(x));
(%i20) [x = −3.0,x = −3.0]

Of course Maxima allows us to perform complete curve sketching. Let's just see an example. We have already found the roots of our function f. So next would be to derive f:

(%i21) diff(f(x),x);
(%o21) 2*x+6

We could store this result in a new function for later use:

(%i22) f_(x) := %o21;
(%o22) 2*x+6

And let's get the second derivative as well:

(%i23) f__(x) := diff(f_(x),x);
(%o23) f__(x):=diff(f_(x),x)

We can use the allroots function again to find the solution for f_(x) = 0. There is another way, which will be demonstrated later. Try the allroots way yourself.

The last thing missing for a full curve sketch is the integral function:

(%i24) F(x) := integrate(f(x),x);
(%o24) F(x):=integrate(f(x),x)

Maxima allows you to do much more with functions. You can declare a function to possess certain properties, I will only give one example here and leave the rest to you:

(%i25) declare (f, commutative);
(%o25) done

A list of all available function properties is stored in the variable opproperties.

[Back to top]


Simple equations

Maxima offers two ways of writing equations: as simple equations
x −7 = 5
or as matrices. We will begin with those. To create a matrix we have to use the matrix function. The matrix function takes lists of elements as its parameters. A list is always surrounded by square brackets '[' and ']'. elements are seperated by commas. We want to solve the system:
19 * x + 9 * y = 180
12 * x + 22 * y = 130
to make sure, that we can easily reuse the matrix, we will assign it to a variable:

(%i26) M : matrix([19,9],[12,22]);
(%o26) matrix([19,9],[12,22])

So now we have a two by two matrix with the rows: [19 9] and [12 22]. To complete and then solve our equations we need the resultant column vector:

(%i27) V : matrix([180],[130]);
(%o27) matrix([180],[130])

We know, that we can solve this system by M^−1 . V. For that we need to be sure, that the matrix M is invertible. This is the case, if the determinant of M is ≠ 0. So we need to find the determinant first. We use the determinant function:

(%i28) determinant(M);
(%o28) 310

Now we can solve the system using the dot operator and the special ^^ operator for matrices:

(%i29) M^^−1 . V;
(%o29) matrix([9],[1])

So we have our result a column vector or one by two matrix. x = 9 and y = 1.

You can naturally do much more with matrices. You can calculate eigenvalues, eigenvectors, characteristic polynomials or transform them, multiply them... See appendix A for a few more matrix related functions.

The second alternative is to write equation systems in the traditional form. Let us take the same example:

(%i30) eq_1 : 19 * x + 9 * y = 180;
(%o30) 9*y+19*x = 180
(%i31) eq_2 : 12 * x + 22 * y = 130;
(%o31) 22*y+12*x = 130

Now Maxima offers the function solve, which does exactly, what you expect. It can solve systems of linear equations, even some nonlinear systems and of course polynomials. As parameters it takes a list of equations and a list of variables, which should be considered. We stored our equations in eq_1 and eq_2. We want to solve the system with respect to the variables x and y, so the call is:

(%i32) solve([eq_1,eq_2],[x,y]);
(%o32) [[x = 9,y = 1]]

[Back to top]


Simplifying and solving polynomials

Maxima can not only solve equations, but also simplify terms. For these purposes it has the *simp family of functions. We will see an example of ratsimp for simplification of rational terms. This can be a valuable feature for teachers as well.

Our task: create a polynomial which has simple solutions only, i.e. integer numbers. So we write the polynomial in its factorial form:

(%i33) eq_3 : (x − 12) * (x −15) * (x −22) * (x −5);
(%o33) (x−22)*(x−15)*(x−12)*(x−5)

Now we'd like to see that multiplied and all terms combined, so we can use the ratsimp function here, we will store the result of that operation in the variable eq_4 for further evaluation:

(%i34) eq_4 : ratsimp(eq_3);
(%o34) x^4−54*x^3+1019*x^2−7830*x+19800

To demonstrate the power of Maxima's solver and to validate, that Maxima did everything correctly, we will try to solve this equation:

(%i35) solve(eq_4,x);
(%o35) [x = 15,x = 5,x = 22,x = 12]

Maxima can also solve a system of nonlinear equations. Consider this:

(%i36) eq_5 : x^2 −y + 3 * z = 673;
(%o36) 3*z−y+x^2 = 673
(%i37) eq_6 : 3 * x^2 − 41 * y − 39 * z = 441;
(%o37) −39*z−41*y+3*x^2 = 441
(%i38) eq_7 : − x^2 + 16 * y + 19 * z = 14;
(%o38) 19*z+16*y−x^2 = 14
(%i39) solve([eq_5,eq_6,eq_7],[x,y,z]);
(%o39) [[x = 25,y = 15,z = 21],[x = −25,y = 15,z = 21]]

Simplification of expressions is also available for trigonometric expressions (trigsimp), logarithmic expressions (logsimp) and matrices and linear algebra (vectorsimp).

[Back to top]


File input and output

There are two helpful ways to export sessions or parts of session in Maxima. The first version exports Maxima commands as you type them:

(%i40) stringout("session.mac",input);
(%o40) /home/jpc/session.mac

Call this at the end of your session or at the point, where you would like to save. You can reload this file using the batch function:

(%i41) batch("session.mac");
[copious output]
(%o81) session.mac

There is a second helpful form of the stringout command, it takes another argument: a list of two numbers, indicating the first and last input lines, that you'd like to export:

(%i82) stringout("session2.mac",[1,6]);
(%o82) /home/jpc/session2.mac

You can easily edit these files by hand using a word processor of your choice. Just make sure, that you save the file as simple text - often referred to as ASCII text.

You can also comment the file. Maxima uses C-Style-comments, i.e.:

/* this is a comment */

The second way of exporting a Maxima session is more powerful, but perhaps not the way for everyday student work. This form of writing session data to files stores everything in Lisp. Thus you can embed it in Lisp programs or pass it on to people, who don't properly know, how to handle Maxima, yet have experience with Lisp. You can use the save command, which will save everything up to the current position:

(%i83) save("session3.lisp",all);
(%o83) session3.lisp

These files can later be reloaded using the loadfile function. The save function can take additional options: so you can decide to only store functions or expressions or ... this form is useful for saving specific results, without saving the commands, that created them. In a word: a fine tool for real world projects.

[Back to top]


Comparison between Maxima and other systems

How does Maxima compare to other programs like Octave or Matlab?

Maxima's syntax is sometimes a little odd, due to its Lisp based language. It is easier and more intuitive to write a matrix in Octave/Matlab than in Maxima. The same goes - for example - for the assignment operator for variables, which is not the equal sign '=', but the colon ':'. There are more of these cases. Though not, what one might expect from other mathematical applications, it is still easy enough to get used to.

Maxima is a symbolic system, meaning, that it can give you the result of a solved equation as sqrt(3)+3/8, instead of a numerical approximation. This is a very powerful feature. Combined with its simplifier, which can simplify rational, trigonometric and other terms, this allows for a great deal of precision and might give greater meaning to your results. These are features lacking in Octave/Matlab and - as far as I know - also R.

In contrast to Octave/Matlab and R Maxima cannot import/export sessions from other de facto standard applications in itself. Octave has the advantage of being almost Matlab compatible by itself. If I am not mistaken, R has packages dealing with such format conversions.

Maxima, on the other hand can produce TeX output directly. This can be put to good advantage, when using Maxima in scientific research or thesis.

In terms of mathematics Octave/Matlab, R and Maxima all cover roughly the same areas. Maxima and Octave look to be stronger in the field of analysis and algebra. Yet I found Maxima to be much more obliging in the field of algebra. When it comes to statistics - an area, of which I know little -, I am however still convinced of R's superiority.

Maxima has documentation as extensive as that of the other programs, yet its online help system is a little annoying. I haven't found a way yet to page the output of the online docs. Octave directly opens the info documentation and R too allows to open its help in a web browser from inside the application. But Maxima too comes with info documentation, a manpage under Linux and there is the online help on the Maxima website. So this small lack in design doesn't count too strongly.

All of the mentioned applications are user extendible. Maxima's strong ties to Lisp give you a clear advantage here though. It easily integrates with Lisp code. You can enter Lisp code directly in the interactive interface, as well as use it in your own tinkering. I have not investigated this feature much though; so no help from me.

Maxima too offers a community, based on a mailing-list, available from the Maxima website.

[Back to top]


Conclusion

Maxima is a very powerful utility for students, teachers and researchers alike. Its open source nature and multi platform compatibility gives a lot of advantages: you can share projects with the blind and sighted alike, collaborate with people in very different technological environments and solve tasks from all areas of mathematics.

We have only focused on the basics of algebra and differentiation and integration. But Maxima offers functionality for many other areas. From number theory, to tensors, statistics and more.

Maxima is extendable by user supplied packages to suite your specific needs. These extensions can be very powerful and advanced due to to the Common Lisp ties again. Lisp is a well designed language and with huge collection of other packages.

The only disadvantage of Maxima is its incompatibility with any of the other well known mathematical systems like Octave/Matlab, Mathematica or R. A compensation for this lack is its ability to output TeX formatted results or direct Lisp code.

All in all, I found it to be a very powerful and versatile tool, which far outranks Octave and certainly triumphs over R, in the fields of algebra and analysis. A well developed application with a long history.

[Back to top]


Thanks and acknowledgements

I'd like to dish out a lot of thanks to the Maxima development team for a great piece of software and excellent documentation.

I'd also like to express my gratitude to the host of the 2012 Summer University, the Johannes Kepler Universität Linz for making this whole workshop possible. Thank you for the technical organisation.

In particular I would like to thank Bernhard Stöger from the University here in Linz for his valuable advise on the wonderful world of matrices.

[Back to top]


Appendix A: functions and constants

Alphabetical list of Maxima constants and variables

[Back to top]


Alphabetical list of Maxima functions

[Back to top]


Valid XHTML 1.0 Transitional

Maxima Workshop - copyright © 2012 Julien Claassen.

[End of document]