## Maxima

Maxima [5 ] is an open-source work-(much-)alike to Mathematica [4 ] or Maple [3 ]. It descends from Macsyma [2 ]. One of its interfaces is constructed with Tk. There is a sf wiki [1 ]. Of course you can also run it from the command line.

It's also: 1) a car, 2) a princess, 3) the plural of "maximum" in several languages, beginning with Latin, and the name of an ancient place, e.g. Cloaca Maxima [6 ]

See the page Maxima through a pipe for a rudimentary programmatic interface to the command line Maxima.

See the page Calling Maxima from Tcl for a thorough description of how to hook into the Tk version of Maxima from Tcl.

TV 2005-02-03: I'll be doing a beginners tutorial on Tcl with maxima and bwise at Fosdem 2005 end of februari, see [8 ] and the schedule: [7 ] .

TV: I'm looking into maxima for reasons of lack of mathematica license, and because there are at least half a dozen applications I find interesting for mathematical symbolic manipulation, including a tcl formula manipulator, physics problems, maybe my string simulator, (electronic) network analysis, maybe drawing certain graphs, etc, and a bit of mathematical recreation of course.

I've downloaded the windows version, (see above) june 2004, which works fine, and shows for isntance these pictures by just a few clicks:

The welcome screen

A Riemann surface 3D plot from the examples

In the bottom window, double click on the blue links to make them work.

The 3D plot is not a perspective projection with fixed aspect ratio, it is not in that way correct, but it works interactive (clicking the mouse on it and translating makes it sort of rotate. Use a menu option to chose a separate plot window.

There might be a version with a nicer special fonts prettyprinter, maybe on linux, I didn't try (yet).

I'm making a page on Bwise combined with Maxima, which should be a very interesting combination. Sampled Signal Reconstruction in Tcl driven Maxima is a start to do decent sampling with a combined mathematical and tcl approach.

Also, I'm making a page on Calling Maxima from Tcl, which isn't trivial in general form, but quite possible it seems.

I've added a page Bwise blocks using exec maxima which contains a fairly easy way to call maxima from a bwise block, which requires decent stdio, so linux/unix work fine (and efficient enough). In the same context: a tcl script for function value listing based on a fortran expression can be used to convert maxima functions to C-code-available functions, for a bit of a hacker also automatically, like when wanting to create huge data sets from a graph. Because I think I find the subject likable, I'm starting a page on: constructing mathematical formulas with Bwise blocks.

Tcl/Tk and Maxima

The latest version I tried has its user interface based on Tk, and can open a normal tcl console window reporting

 info pa
8.4.1

and allows normal things, including inspecting the maxima window widget hierarchy (5 packed widgets with reasonable names) opening new toplevels and opening files/sockets, and it appears to be so that there are a lot of maxima related tcl functions.

I'm looking into how that is.

Lars H: Can you perhaps report some more about what the programmatic Tcl interfaces to Maxima look like? (What is below mostly reports on the user interfaces to Maxima.) Is it merely that there is some "Maxima evaluate" command (argument is a Maxima command, which is sent to the Maxima engine and evaluated there), or is there a larger integration between the two? It would be really cool if the result of a Maxima command could be returned to Tcl and then used as an argument of the next Maxima command.

TV 2004-07-21: As an at least 'in principle' answer, maxima has a text interface for normal user interaction, which can be used like this from the tcl console:

 .maxima.text insert insert "DISPLAY2D:false;"
CMeval .maxima.text

This will tell maxima to return the subsequent results in 1 dimensional (non-prettyprinted) form, which can at least be plucked from the text widget where it appears after calling CMeval.

Lars H: That is even slightly worse than I had feared. The CMeval (my "Maxima evaluate" above) doesn't even take the code to evaluate as an argument, but instead accesses a text widget directly. Pity. Tcl is in many ways very suitable for associative algebra, but one usually wants to have a commutative algebra package available to build upon. To my knowledge there is no such package for Tcl, but had there been a good interface to Maxima then that could have been it.

(Later.) I notice from what TV has written on Calling Maxima from Tcl that the communication between the Tcl interpreter and Maxima engine is really going over a socket, and that the format can be explicit Maxima commands/values. That is better, and probably about as good as one can hope for.

A bit of a tutorial

Lets say as excercise we want to find the roots to the equation

 [expr {$x*$x-1}]

I typed the stuff after the CX cursor (where X is the number of the line in the history), and pressed return, to define the function f of x:

 (C5) f(x):=x^2-1;
2
(D5)                          f(x) := x  - 1

The := defines the function, and it is printed in more or less normal mathematical form as a return value.

The semicolumn ';' at the end is obligatory, otherwise the parser will not recognize 'end of input', so one can enter more than one line as one command, ended by a ; and <Return>.

Now lets check for zeros in our function, by using the 'solve()' function, equating our function to zero, and solving that equation ofr 'x', type the stuff after the (C^), and Maxima returns the answer:

 (C6) solve(f(x)=0,x);
(D6)                         [x = - 1, x = 1]

Some special keys:

   ^G      break the maxima interpreter, resume with :q to get back the normal prompt
that also holds for resuming after an error
alt-p   scroll up to previous command.
%pi     short for PI in computations.

Maybe more interesting, we can solve the equations inverse symbolically:

 (C8) solve(f(x)=y,x);
(D8)               [x = - SQRT(y + 1), x = SQRT(y + 1)]

Symbolic integration of our function:

 (C13) integrate(f(x),x);
3
x
(D13)                                       -- - x
3

The second argument of the function is the integration variable. The same for differentiation:

 (C14) diff(f(x),x);
(D14)                                                2 x

lets see how invertable this is:

 (C15) integrate(diff(f(x),x),x);
2
(D15)                                                x
(C16) diff(integrate(f(x),x),x);
2
(D16)                                              x  - 1

When differentiating first, we lose the constant!

Fractions are handy:

 (C10) 1/6+1/4;
5
(D10)                                         --
12

as are 'infinite' or arbitrary precision numbers:

 (C19) block([FPPREC:30],bfloat(10/6));
(D19)                                 1.66666666666666666666666666667B0

30 is the number of computation digits, 10/6 is what we are computing, and the B0 inthe result appears to be the exponent, so pow(10,0) in this case.

One can get a good number of decimals voor approximation PI that way:

 (C2)  block([FPPREC:30],bfloat(%pi));
(D2)                         3.14159265358979323846264338328B0

For entering formulas, normal rules apply, mind the ordering of operators, or use braces:

 (C29)log(3*(%e^x))^2;
2     x
(D29)                                            LOG (3 %E )
(C30) log((3*(%e^x))^2);
2 x
(D30)                                           LOG(9 %E   )

Maxima automatically simplified the formula, which we can switch off (and back on, it's handy) by:

 (C39) SIMP:FALSE$(C40) log((3*(%e^x))^2); x 2 (D40) LOG((3 %E ) ) (C42) SIMP:TRUE$

Matrices can be entered row-wise, and symbolicaly, such as this 2 dimensional rotation matrix, with variable p as rotation angle:

 (C21) A:matrix([cos(p),-sin(p)],[sin(p),cos(p)]);
[ COS(p)  - SIN(p) ]
(D21)                                 [                  ]
[ SIN(p)   COS(p)  ]

Multiplying the result with the orthonomal 2D basis vectors X1 and X2:

 (C58) A . matrix([1],[0]);
[ COS(p) ]
(D58)                                [        ]
[ SIN(p) ]
(C59) A . matrix([0],[1]);
[ - SIN(p) ]
(D59)                               [          ]
[  COS(p)  ]

Automatic inverse (which could be simplified..):

 (C55) invert(A);
[       COS(p)              SIN(p)       ]
[  -----------------   ----------------- ]
[     2         2         2         2    ]
[  SIN (p) + COS (p)   SIN (p) + COS (p) ]
(D55)               [                                        ]
[        SIN(p)             COS(p)       ]
[ - -----------------  ----------------- ]
[      2         2        2         2    ]
[   SIN (p) + COS (p)  SIN (p) + COS (p) ]

A variable is set by:

 variable:value;

a function like:

 function(variable):=sin(x)^2+cos(x)^2;

unsetting is called kill, while stating the name of a stored entity prints its content:

 (C18) c:255;
(D18)                                       255
(C19) c;
(D19)                                       255
(C20) kill(c);
(D20)                                      DONE
(C21) c;
(D21)                                        C

The command line version of maxima stops at EOF, or by using

   quit();

in xmaxima, this only kills the maxima process, not the Tk shell.

Some more advanced / esoterical examples:

A Taylor series expansion of the sinc function until degree 6:

 (C53) TAYLOR(sin(x)/x,x,0,6);
2    4      6
x    x      x
(D53)/T/                            1 - -- + --- - ---- + . . .
6    120   5040

(TV (12 feb '04) It's of course touching or nice when people correct my writing, and I know I make more than a few typos. but in this case I meant to write sin_C_ and not sine, see the expression which acts as the argument for the taylor expansion maxima command. The taylor expansion for sine around 0 would have to be zero, for the sinc function it ascends to 1 around 0 as limit case, which is clear from the expansion.)

Maxima can produce (la)tex formatted output for equations, such as:

  tex(integrate(1/(1+x^3),x));

produces this tex:

 $$-{{\log \left(x^2-x+1\right)}\over{6}}+{{\arctan \left({{2\,x-1 }\over{\sqrt{3}}}\right)}\over{\sqrt{3}}}+{{\log \left(x+1\right) }\over{3}}$$

and this after cygwin provided pdflatex rendering of that result after prepending

 \documentclass{article}
\begin{document}

and postpending:

 \end{document}

to it, the adobe captured page looks like decent scientific text formulas:

It should be possible to automate this, maybe even with a bwise automation graph.

Alternatively, I've checked out texmacs, which is an tex enhanced emacs, which does direct mathematical formula display, and, at least I tried on Linux, can run maxima interactively, to get neatly formatted mathematica results straight away. I didn't make or see a tcl link, but there is always cut 'n paste I guess.

It sure looks neat to work that way, now I must try to force a unix console out of the tcl (if that's also being used here), to interact with the whole business. The approach is a lot slower than the Tk frame, but allows formula editing in high quality.

AMG: Is the above image truncated for anyone else? AK: Thanks, fixed. At that was the reason I put all of them into the wiki itself. The original server, or its network connection is very unreliable.

AMG: Holy Tab Damage, Batman! The ASCII-art typeset formulas were hit especially hard. I have restored them, hopefully without error. Speaking of typesetting, has any work gone into improving the appearance and behavior of the Tk version of Maxima? The images posted by TV are quite promising, though I haven't come across any effort to integrate that with the core distribution. Outside appearances suggest Tk is not getting any attention, that all the work is going into wxWindows/wxWidgets.