# Programming in Maxima

Recently, in one of my papers I needed to do somewhat involved polynomial algebra. Basically, adding and substracting polynomials in p and 1-p with degree n. The calculations were relatively straight forward, but I always make silly mistakes with such calculations. So, I decided to check my analysis using a computer algebra system. Matlab and Mathematica, perhaps the most popular computer algebra softwares (although Matlab is focussed more on numerical calculations), were out of the picture because they are commercial and I did not have site licenses for them. So, I googled a bit and settled on Maxima.

To get started, I used wxMaxima, which provides a nice GUI for Maxima. wxMaxima makes learning maxima easy. It works as I expect an CAS IDE to work. After playing around the GUI for a while, I wanted to write a script to check my calculations. It turns out that writing a script is not that easy.

You can create a wxMaxima document, which is somewhat like a Mathematica notebook. The difference is that wxMaxima document is a text file, in fact a valid maxima file. Therefore, all the formatting information is stored in the comments. Here is an example:

```/* [wxMaxima: comment start ]
In Proposition 1, we define a transformation \$A_i\$. Since we are only interested
in the symmetric arrival case, we only need to work with
[wxMaxima: comment end   ] */

/* [wxMaxima: input   start ] */
Ap(n) := 1 - (1-p)^(n+1) ;
/* [wxMaxima: input   end   ] */
```

The display output in wxMaxima looks really nice. But, for some unexplainable reason, I can only think logically when I am editing text using vim. All the moving up and down in the wxMaxima window makes me lose my train of thought. Writing an elaborate markup so that wxMaxima pretty prints the comments was too cumbersome, so I needed to figure out how to write a script in maxima. (On second thoughts, maybe using the wxMaxima syntax was not too bad. But then, all progress is made by the unsatisfied man!)

Now, Maxima is meant to be an interactive program. So, it echos everything that it reads and it is not easy to distinguish the desired output from the rest. After a bit of digging into the manual, I found that ending a line with `\$` instead of `;` does not show the output. However, if you read the file using `batch`, the input lines are still displayed, which is a bit annoying. After more digging around in the manual, I realized that I should be loading the file using `batchload`â€”which does not display input or output lines; only the output of `print` and `display` are shown. Unfortunately, maxima does not have a command line switch to `batchload` a file, but that can be overcome using `--batch-string` flag.

In short, if you are looking to use maxima as a normal programming language:

1. End your lines with `\$` rather than `;`
2. Run the program using `maxima --batch-string="batchload(\"filename\")\$"`

and be aware that maxima is meant for interactive use. For some functions, it uses `asksign` which requires explicit user intervention. Of course, there are workarounds.

Edit: Roman pointed out another neat trick; run your script as an initialization file using `--init-mac`. The only trouble is that `init-mac` is meant for initialization files, so after

``````
maxima -q --init-mac=filename
``````

you end up in interactive mode. To overcome that, either add an explicit `quit();` at the end of your script, or add the options `--batch-string="quit();"`.