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
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Linux Systems Administrator
- New Products
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Favorite (and easily brute-forced) pw's
14 min 56 sec ago - Have you tried Boxen? It's a
6 hours 6 min ago - seo services in india
10 hours 38 min ago - For KDE install kio-mtp
10 hours 39 min ago - Evernote is much more...
12 hours 39 min ago - Reply to comment | Linux Journal
21 hours 24 min ago - Dynamic DNS
21 hours 58 min ago - Reply to comment | Linux Journal
22 hours 57 min ago - Reply to comment | Linux Journal
23 hours 47 min ago - Not free anymore
1 day 3 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?



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!