Pexpect interface to Maxima#

Maxima is a free GPL’d general purpose computer algebra system whose development started in 1968 at MIT. It contains symbolic manipulation algorithms, as well as implementations of special functions, including elliptic functions and generalized hypergeometric functions. Moreover, Maxima has implementations of many functions relating to the invariant theory of the symmetric group \(S_n\). (However, the commands for group invariants, and the corresponding Maxima documentation, are in French.) For many links to Maxima documentation see


  • William Stein (2005-12): Initial version

  • David Joyner: Improved documentation

  • William Stein (2006-01-08): Fixed bug in parsing

  • William Stein (2006-02-22): comparisons (following suggestion of David Joyner)

  • William Stein (2006-02-24): greatly improved robustness by adding sequence numbers to IO bracketing in _eval_line

  • Robert Bradshaw, Nils Bruin, Jean-Pierre Flori (2010,2011): Binary library interface

This is the interface used by the maxima object:

sage: type(maxima)
<class 'sage.interfaces.maxima.Maxima'>

If the string “error” (case insensitive) occurs in the output of anything from Maxima, a RuntimeError exception is raised.

EXAMPLES: We evaluate a very simple expression in Maxima.

sage: maxima('3 * 5')

We factor \(x^5 - y^5\) in Maxima in several different ways. The first way yields a Maxima object.

sage: x,y = SR.var('x,y')
sage: F = maxima.factor('x^5 - y^5')
sage: F # not tested - depends on maxima version
sage: actual = F.sage()
sage: expected = -(y-x)*(y^4+x*y^3+x^2*y^2+x^3*y+x^4)
sage: bool(actual == expected)
sage: type(F)
<class 'sage.interfaces.maxima.MaximaElement'>

Note that Maxima objects can also be displayed using “ASCII art”; to see a normal linear representation of any Maxima object x. Just use the print command: use str(x).

sage: print(F)
                           4      3    2  2    3      4
               - (y - x) (y  + x y  + x  y  + x  y + x )

You can always use repr(x) to obtain the linear representation of an object. This can be useful for moving maxima data to other systems.

sage: F = maxima('x * y')
sage: repr(F)
sage: F.str()

The maxima.eval command evaluates an expression in maxima and returns the result as a string not a maxima object.

sage: print(maxima.eval('factor(x^5 - 1)'))

We can create the polynomial \(f\) as a Maxima polynomial, then call the factor method on it. Notice that the notation f.factor() is consistent with how the rest of Sage works.

sage: f = maxima('x^5 + y^5')
sage: f^2
sage: f.factor()

Control-C interruption works well with the maxima interface, because of the excellent implementation of maxima. For example, try the following sum but with a much bigger range, and hit control-C.

sage: maxima('sum(1/x^2, x, 1, 10)')


We follow the tutorial at

sage: maxima('1/100 + 1/101')
sage: a = maxima('(1 + sqrt(2))^5'); a
sage: a.expand()
sage: a = maxima('(1 + sqrt(2))^5')
sage: float(a)
sage: a.numer()
sage: maxima.eval('fpprec : 100')
sage: a.bfloat()
sage: maxima('100!')
sage: f = maxima('(x + 3*y + x^2*y)^3')
sage: f.expand()
x^6*y^3+9*x^4*y^3+27*x^2*y^3+27*y^3+3*x^5*y^2+18*x^3*y^2+27*x*y^2 +3*x^4*y+9*x^2*y+x^3
sage: f.subst('x=5/z')
sage: g = f.subst('x=5/z')
sage: h = g.ratsimp(); h
(27*y^3*z^6+135*y^2*z^5+(675*y^3+225*y)*z^4+(2250*y^2+125)*z^3 +(5625*y^3+1875*y)*z^2+9375*y^2*z+15625*y^3) /z^6
sage: h.factor()
sage: eqn = maxima(['a+b*c=1', 'b-a*c=0', 'a+b=5'])
sage: s = eqn.solve('[a,b,c]'); s
[[a = -...(sqrt(79)*%i-11)/4...,b = (sqrt(79)*%i+9)/4, c = (sqrt(79)*%i+1)/10], [a = (sqrt(79)*%i+11)/4,b = -...(sqrt(79)*%i-9)/4..., c = -...(sqrt(79)*%i-1)/10...]]

Here is an example of solving an algebraic equation:

sage: maxima('x^2+y^2=1').solve('y')
[y = -sqrt(1-x^2),y = sqrt(1-x^2)]
sage: maxima('x^2 + y^2 = (x^2 - y^2)/sqrt(x^2 + y^2)').solve('y')
[y = -sqrt((...-y^2...-x^2)*sqrt(y^2+x^2)+x^2), y = sqrt((...-y^2...-x^2)*sqrt(y^2+x^2)+x^2)]

You can even nicely typeset the solution in latex:

sage: latex(s)
\left[ \left[ a=-...{{\sqrt{79}\,i-11}\over{4}}... , b={{...\sqrt{79}\,i+9...}\over{4}} , c={{\sqrt{79}\,i+1}\over{10}} \right]  , \left[ a={{...\sqrt{79}\,i+11}\over{4}} , b=-...{{\sqrt{79}\,i-9...}\over{4}}... , c=-...{{...\sqrt{79}\,i-1}\over{10}}... \right]  \right]

To have the above appear onscreen via xdvi, type view(s). (TODO: For OS X should create pdf output and use preview instead?)

sage: e = maxima('sin(u + v) * cos(u)^3'); e
sage: f = e.trigexpand(); f
sage: f.trigreduce()
sage: w = maxima('3 + k*%i')
sage: f = w^2 + maxima('%e')^w
sage: f.realpart()
sage: f = maxima('x^3 * %e^(k*x) * sin(w*x)'); f
sage: f.diff('x')
k*x^3*%e^(k*x)*sin(w*x)+3*x^2*%e^(k*x)*sin(w*x)+w*x^3*%e^(k*x) *cos(w*x)
sage: f.integrate('x')
(((k*w^6+3*k^3*w^4+3*k^5*w^2+k^7)*x^3 +(3*w^6+3*k^2*w^4-3*k^4*w^2-3*k^6)*x^2+(...-...18*k*w^4)-12*k^3*w^2+6*k^5)*x-6*w^4 +36*k^2*w^2-6*k^4) *%e^(k*x)*sin(w*x) +((...-w^7...-3*k^2*w^5-3*k^4*w^3-k^6*w)*x^3...+(6*k*w^5+12*k^3*w^3+6*k^5*w)*x^2...+(6*w^5-12*k^2*w^3-18*k^4*w)*x-24*k*w^3 +24*k^3*w) *%e^(k*x)*cos(w*x)) /(w^8+4*k^2*w^6+6*k^4*w^4+4*k^6*w^2+k^8)
sage: f = maxima('1/x^2')
sage: f.integrate('x', 1, 'inf')
sage: g = maxima('f/sinh(k*x)^4')
sage: g.taylor('x', 0, 3)
sage: maxima.taylor('asin(x)','x',0, 10)

Examples involving matrices#

We illustrate computing with the matrix whose \(i,j\) entry is \(i/j\), for \(i,j=1,\ldots,4\).

sage: f = maxima.eval('f[i,j] := i/j')
sage: A = maxima('genmatrix(f,4,4)'); A
sage: A.determinant()
sage: A.echelon()
sage: A.eigenvalues()
sage: A.eigenvectors()

We can also compute the echelon form in Sage:

sage: B = matrix(QQ, A)
sage: B.echelon_form()
[  1 1/2 1/3 1/4]
[  0   0   0   0]
[  0   0   0   0]
[  0   0   0   0]
sage: B.charpoly('x').factor()
(x - 4) * x^3

Laplace Transforms#

We illustrate Laplace transforms:

sage: _ = maxima.eval("f(t) := t*sin(t)")
sage: maxima("laplace(f(t),t,s)")
sage: maxima("laplace(delta(t-3),t,s)") #Dirac delta function
sage: _ = maxima.eval("f(t) := exp(t)*sin(t)")
sage: maxima("laplace(f(t),t,s)")
sage: _ = maxima.eval("f(t) := t^5*exp(t)*sin(t)")
sage: maxima("laplace(f(t),t,s)")
(360*(2*s-2))/(s^2-2*s+2)^4-(480*(2*s-2)^3)/(s^2-2*s+2)^5 +(120*(2*s-2)^5)/(s^2-2*s+2)^6
sage: print(maxima("laplace(f(t),t,s)"))
                                         3                 5
           360 (2 s - 2)    480 (2 s - 2)     120 (2 s - 2)
          --------------- - --------------- + ---------------
            2           4     2           5     2           6
          (s  - 2 s + 2)    (s  - 2 s + 2)    (s  - 2 s + 2)
sage: maxima("laplace(diff(x(t),t),t,s)")
sage: maxima("laplace(diff(x(t),t,2),t,s)")
...-...%at('diff(x(t),t,1),t = 0))+s^2*'laplace(x(t),t,s)-x(0)*s

It is difficult to read some of these without the 2d representation:

sage: print(maxima("laplace(diff(x(t),t,2),t,s)")) # not tested - depends on maxima version
                d        !          2
             (- -- (x(t))!     ) + s  laplace(x(t), t, s) - x(0) s
                dt       !
                         !t = 0

Even better, use view(maxima("laplace(diff(x(t),t,2),t,s)")) to see a typeset version.

Continued Fractions#

A continued fraction \(a + 1/(b + 1/(c + \cdots))\) is represented in maxima by the list \([a, b, c, \ldots]\).

sage: maxima("cf((1 + sqrt(5))/2)")
sage: maxima("cf ((1 + sqrt(341))/2)")

Special examples#

In this section we illustrate calculations that would be awkward to do (as far as I know) in non-symbolic computer algebra systems like MAGMA or GAP.

We compute the gcd of \(2x^{n+4} - x^{n+2}\) and \(4x^{n+1} + 3x^n\) for arbitrary \(n\).

sage: f = maxima('2*x^(n+4) - x^(n+2)')
sage: g = maxima('4*x^(n+1) + 3*x^n')
sage: f.gcd(g)

You can plot 3d graphs (via gnuplot):

sage: maxima('plot3d(x^2-y^2, [x,-2,2], [y,-2,2], [grid,12,12])')  # not tested
[displays a 3 dimensional graph]

You can formally evaluate sums (note the nusum command):

sage: S = maxima('nusum(exp(1+2*i/n),i,1,n)')
sage: print(S)
                        2/n + 3                   2/n + 1
                      %e                        %e
               ----------------------- - -----------------------
                  1/n         1/n           1/n         1/n
               (%e    - 1) (%e    + 1)   (%e    - 1) (%e    + 1)

We formally compute the limit as \(n\to\infty\) of \(2S/n\) as follows:

sage: T = S*maxima('2/n')
sage: T.tlimit('n','inf')


Obtaining digits of \(\pi\):

sage: maxima.eval('fpprec : 100')
sage: maxima(pi).bfloat()

Defining functions in maxima:

sage: maxima.eval('fun[a] := a^2')
sage: maxima('fun[10]')


Unfortunately maxima doesn’t seem to have a non-interactive mode, which is needed for the Sage interface. If any Sage call leads to maxima interactively answering questions, then the questions can’t be answered and the maxima session may hang. See the discussion at for some ideas about how to fix this problem. An example that illustrates this problem is maxima.eval('integrate (exp(a*x), x, 0, inf)').

Latex Output#

To TeX a maxima object do this:

sage: latex(maxima('sin(u) + sinh(v^2)'))
\sinh v^2+\sin u

Here’s another example:

sage: g = maxima('exp(3*%i*x)/(6*%i) + exp(%i*x)/(2*%i) + c')
sage: latex(g)

Long Input#

The MAXIMA interface reads in even very long input (using files) in a robust manner, as long as you are creating a new object.


Using maxima.eval for long input is much less robust, and is not recommended.

sage: t = '"%s"'%10^10000   # ten thousand character string.
sage: a = maxima(t)
class sage.interfaces.maxima.Maxima(script_subdirectory=None, logfile=None, server=None, init_code=None)#

Bases: MaximaAbstract, Expect

Interface to the Maxima interpreter.


sage: m = Maxima()
sage: m == maxima

Clear the variable named var.


sage: maxima.set('xxxxx', '2')
sage: maxima.get('xxxxx')
sage: maxima.clear('xxxxx')
sage: maxima.get('xxxxx')

Get the string value of the variable var.


sage: maxima.set('xxxxx', '2')
sage: maxima.get('xxxxx')

Send a lisp command to Maxima.


The output of this command is very raw - not pretty.


sage: maxima.lisp("(+ 2 17)")   # random formatted output
 :lisp (+ 2 17)
set(var, value)#

Set the variable var to the given value.


  • var – string

  • value – string


sage: maxima.set('xxxxx', '2')
sage: maxima.get('xxxxx')
set_seed(seed=None)# make_random_state (n) returns a new random state object created from an integer seed value equal to n modulo 2^32. n may be negative.


sage: m = Maxima()
sage: m.set_seed(1)
sage: [m.random(100) for i in range(5)]
[45, 39, 24, 68, 63]
class sage.interfaces.maxima.MaximaElement(parent, value, is_name=False, name=None)#

Bases: MaximaAbstractElement, ExpectElement

Element of Maxima through Pexpect interface.


Elements of this class should not be created directly. The targeted parent should be used instead:

sage: maxima(3)
sage: maxima(cos(x)+e^234)

Return the 2d string representation of this Maxima object.


sage: F = maxima('x^5 - y^5').factor()
sage: F.display2d()
                       4      3    2  2    3      4
           - (y - x) (y  + x y  + x  y  + x  y + x )
class sage.interfaces.maxima.MaximaElementFunction(parent, name, defn, args, latex)#

Bases: MaximaElement, MaximaAbstractElementFunction

Maxima user-defined functions.


Elements of this class should not be created directly. The method function of the targeted parent should be used instead:

sage: maxima.function('x,y','h(x)*y')

Return True if x is of type MaximaElement.


sage: from sage.interfaces.maxima import is_MaximaElement
sage: is_MaximaElement(1)
doctest:...: DeprecationWarning: the function is_MaximaElement is deprecated; use isinstance(x, instead
See for details.
sage: m = maxima(1)
sage: is_MaximaElement(m)

Unpickle a Maxima Pexpect interface.


sage: from sage.interfaces.maxima import reduce_load_Maxima
sage: reduce_load_Maxima()
sage.interfaces.maxima.reduce_load_Maxima_function(parent, defn, args, latex)#

Unpickle a Maxima function.


sage: from sage.interfaces.maxima import reduce_load_Maxima_function
sage: f = maxima.function('x,y','sin(x+y)')
sage: _,args = f.__reduce__()
sage: g = reduce_load_Maxima_function(*args)
sage: g == f