# Exploring Advanced Math with Maxima

When I took Calculus in college, my Professor would give us substantial partial credit for test problems that we got wrong for minor arithmetic errors, and rightfully so, too. Sometimes even simple-sounding problems resulted in a full page, or more, of calculations. Simply changing a -1 to a +1 early on in a problem could be completely devastating. Avoiding simple errors like that is what Computer Algebra Systems (CAS) are all about and I've found Maxima to be an extremely powerful CAS program.

Maxima is licensed under the GPL, so it has to be good! However, Maxima is a text based program, which means it's a bit clumsy by today's standards. Fortunately, there are a few front ends which make the program much easier to use and more polished. For this article, I'm using the wxMaxima front end.

Anyone who's taken a high school Algebra class remembers spending endless hours factoring polynomials, multiplying them together, and evaluating them for given values of x. Factoring polynomials always seemed to have an element of guess work built in, and I found it to be quite frustrating. Of course, the teacher spent weeks teaching various ways to factor these formulas, and then they finally introduced the Quadratic Equation, which made factoring second order polynomials almost trivial. For those of you who don't completely geek out over Math, a second order polynomial is one like this: ax^2 + bx + c, where the highest exponent is 2.

Manipulating polynomials with Maxima is very easy, as shown in Figure 1. Here you see 7 steps that I've asked Maxima to perform. My commands are indicated by the “%i” and a step number. The corresponding output is indicated by the “%o” and then the step number. So, (%i90) is my 90th input step and the result is on the line indicated by (%o90). So, at step 90, you see that I entered a simple polynomial equation. Then I asked Maxima to factor it. Here you see that I used a shortcut. The “%” character represents the return value of the previous step. So, when I entered “factor(%);” Maxima retrieved the previous step's value (that polynomial) and factored it. The expected result is on the next line. In step 92, I entered two polynomials multiplied by each other. As you can see, Maxima doesn't immediately do anything special with this formula. But when I asked it to expand the formula, it multiplied the two together, as you can see in step 93. At step 94, I asked Maxima to factor the resulting 4th order polynomial, and behold, we get the original equation back, as we expect. I don't think we even discussed factoring 4th order polynomials in high school Algebra! But we did a lot of evaluating polynomials at a given value of x, which I demonstrate in steps 95 and 96. In step 95, I assign a value to x. In step 96, I re-enter the polynomial, and Maxima calculates it's value at the given value of x.

Of course, Maxima can also solve equations, both symbolically, and numerically, as in Figure 2. Here we see that I've asked it to solve a quadratic equation, and it gives me the answer in symbolic form. Then I used the “numer” directive to ask Maxima to give it's answers in numerical form, which it does in step 13. Finally, I ask Maxima to use 25 digits of accuracy, instead of the default 16, and recalculate. Maxima will calculate to any degree of accuracy that you want and can handle arbitrarily large numbers. For example, Maxima can easily handle 2^200, and can even factor (2^200)-1.

Maxima will even handle calculations that you'd never even think about doing manually. For example, you can ask Maxima to solve a 4th order polynomial equation, and it will give you all 4 solutions, both real and complex. However, in symbolic form, these solutions are often so ugly that they scroll off your screen! But, you can then turn around and ask it to give you the same answers in numerical form, to any degree of accuracy that you need.

Maxima uses either GnuPlot or Xmaxima to plot expressions and can plot 2D, 3D and parametric equations. In Figure 3, I entered a fairly complex expression, and asked Maxima to plot it on a 2D plot. In Figure 4, I took an example from Maxima's documentation and plotted a 3D image. You can rotate this image and view it from any angle you like.

If all Maxima was able to do was manipulate polynomials, it would still be a powerful tool. But Maxima can do much more, as I'll try to demonstrate in Figure 5. Here, in step 16, I asked Maxima to expand a Trigonometric expression using the Double Angle Theorem. Even though I found Trig to be the easiest Math class I ever took, Maxima can manipulate Trigonometric Identities much better than I ever will. In step 17, I asked Maxima to differentiate sin(3x) with respect to x. This is a fairly simple derivative to calculate. There's nothing simple about the derivative I asked for in step 18. If we were performing this calculation manually, we would probably use a combination of the Product Rule and the Quotient Rule, or multiple instances of the Quotient Rule. Then we would have to simplify the result. Either way, “yuck” is a good way to describe the process. Maxima provides us with a nicely formatted answer. In step 19, I asked Maxima to calculate an indefinite integral with respect to x. We see Maxima's answer on the next line. Ya, that's what I came up with; how about you? In step 20, I asked Maxima to calculate the definite integral of the same function as x goes from 0 to 5. Of course, I could have asked for the answer in numerical format but the symbolic form is so much more geeky. In step 21, we see that Maxima can apply L'Hospital's rule to calculate the limit of an expression as x goes to infinity.

In step 22, I asked Maxima to calculate the first several terms of the Taylor Series of sin(x) centered around x=0. This is where we start to see a weakness in Maxima. There should have been a factorial in the denominator of each term of that Taylor Series. But Maxima simply gave us the answer in numerical form.

Maxima is also able to manipulate matrices and sets, though I'm not going to demonstrate any of that functionality here. Finally, Maxima has a built-in programming language, though it's not the most elegant language you'll ever see. I show you a short example in Figure 6. Here I assign an initial value to x. Then I calculate the value of an expression 10 times, using the previous result as the next value of x. There are only a few initial values of x for which this expression ever repeats itself. Can you find them?

As you can see, Maxima is a very sophisticated analysis tool and is adept at solving problems in Algebra, Trigonometry, Linear Algebra, Differential Calculus, and Integral Calculus. It can also calculate using arbitrarily large and precise numbers. With all that capability, you might be tempted to ask why we even need to teach Mathematics anymore and that's the real point of this article. For all it can do, Maxima has no understanding of any of the questions you ask it; it only knows how to calculate answers. The value of teaching and learning Mathematics is in understanding concepts and thought processes. The value in using a tool like Maxima is in removing some of the tedium from the process of Math.

Attachment | Size |
---|---|

Fig1.png | 37.23 KB |

Fig2.png | 58.2 KB |

Fig3.png | 23.65 KB |

Fig4.png | 33.23 KB |

Fig5.png | 48.85 KB |

Fig6.png | 76.02 KB |

cover.png | 48.04 KB |

Mike Diehl is a freelance Computer Nerd specializing in Linux administration, programing, and VoIP. Mike lives in Albuquerque, NM. with his wife and 3 sons. He can be reached at mdiehl@diehlnet.com

## Special Reports: DevOps

Have projects in development that need help? Have a great development operation in place that can ALWAYS be better? Regardless of where you are in your DevOps process, Linux Journal can help!

With deep focus on Collaborative Development, Continuous Testing and Release & Deployment, we offer here the DEFINITIVE DevOps for Dummies, a mobile Application Development Primer, advice & help from the experts, plus a host of other books, videos, podcasts and more. All free with a quick, one-time registration. Start browsing now...

## Geek Guides

Practical (and free!) books for the most technical people on the planet. Newly available books include:

**Plus many more.**

## Trending Topics

Science on Android | Oct 09, 2015 |

A First Look at IBM's New Linux Servers | Oct 08, 2015 |

The Ubuntu Conspiracy | Oct 07, 2015 |

Vigilante Malware | Oct 06, 2015 |

Non-Linux FOSS: Code Your Way To Victory! | Oct 05, 2015 |

Dealing with Boundary Issues | Oct 02, 2015 |

## Comments

## Re: Something wrong with accuracy?

No, it's just that Maxima provides a variable, fpprec, that controls the precision of its bigfloat computations and a variable, fpprintprec, that controls the number of digits printed. Here is what the correct output looks like:

(%i18) solve(x^2-5*x-3,x);

(%o18) [x = -(sqrt(37)-5)/2,x = (sqrt(37)+5)/2]

(%i19) %,numer;

(%o19) [x = -.5413812651491097,x = 5.54138126514911]

(%i20) (fpprintprec : fpprec : 25, bfloat(%o18));

(%o20) [x = -5.413812651491098444998421b-1,x = 5.541381265149109844499842b0]

I believe that %o20 agrees with what you expect.

## You might also want to look

You might also want to look into Sage, as it's a single python interface that combines a number of math packages including Maxima.

## Something wrong with accuracy?

From the logs it looks like SQRT(13) was replaced in step 13 with an approximate value that is accurate to 8 digits only, although the precision of numerical values was set to 16.

In step 15 you set fpprec to 25 digits, but again, the end results are accurate to 7 digits only. (The solutions should be: -.5413812651491098444998421 and 5.5413812651491098444998421).

Is there a way to avoid these problems?

## Fantastic Tool

I've always loved maths, especially all the algebra and calculus, but I'd love to see others who hate maths just because of this use this fantastic tool, it might just make them love maths. When I teach(maybe 6-7 years on) I'll recommend this to my students ;)

## It sure looks very nice and

It sure looks very nice and sophisticated, but how does it compare to the alternatives? And than I mean of course the commercial stuff they use in colleges and things like Wolfram Alpha, which is supposed to have some impressive calculating power as well? When I start my study of Applied Physics later this year, I can maybe find this all out by myself. But can you give a quick comparison maybe?

Btw, great article, very pleasant to read!