Pexpect interface to Maxima - Interpreter Interfaces

Skip to content
Back to top
View this page
Edit this page

# 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 http://maxima.sourceforge.net/documentation.html.

AUTHORS:

- 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

There is also an ECL-based version of the Maxima interface in `sage.interfaces.maxima_lib`. That ECL interface is used for all internal symbolic calculations in SageMath, providing a more direct and efficient connection to Maxima’s core functionality. The Pexpect interface defined here is isolated from the ECL-based interface: it communicates with Maxima via a subprocess and text-based interaction, and is primarily intended for interactive use.

This is the interface used by the maxima object:

Sage

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

Python

    >>> from sage.all import *
    >>> from sage.interfaces.maxima import maxima
    >>> 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

    sage: maxima('3 * 5')
    15

[Python]

    >>> from sage.all import *
    >>> maxima('3 * 5')
    15

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

Sage

    sage: from sage.interfaces.maxima import maxima
    sage: x,y = SR.var('x,y')
    sage: F = maxima.factor('x^5 - y^5')
    sage: F # not tested - depends on maxima version
    -((y-x)*(y^4+x*y^3+x^2*y^2+x^3*y+x^4))
    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)
    True
    sage: type(F)
    <class 'sage.interfaces.maxima.MaximaElement'>

Python

    >>> from sage.all import *
    >>> from sage.interfaces.maxima import maxima
    >>> x,y = SR.var('x,y')
    >>> F = maxima.factor('x^5 - y^5')
    >>> F # not tested - depends on maxima version
    -((y-x)*(y^4+x*y^3+x^2*y^2+x^3*y+x^4))
    >>> actual = F.sage()
    >>> expected = -(y-x)*(y**Integer(4)+x*y**Integer(3)+x**Integer(2)*y**Integer(2)+x**Integer(3)*y+x**Integer(4))
    >>> bool(actual == expected)
    True
    >>> 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

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

[Python]

    >>> from sage.all import *
    >>> 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

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

Python

    >>> from sage.all import *
    >>> from sage.interfaces.maxima import maxima
    >>> F = maxima('x * y')
    >>> repr(F)
    'x*y'
    >>> F.str()
    'x*y'

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

Sage

    sage: print(maxima.eval('factor(x^5 - 1)'))
    (x-1)*(x^4+x^3+x^2+x+1)

[Python]

    >>> from sage.all import *
    >>> print(maxima.eval('factor(x^5 - 1)'))
    (x-1)*(x^4+x^3+x^2+x+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

    sage: from sage.interfaces.maxima import maxima
    sage: f = maxima('x^5 + y^5')
    sage: f^2
    (y^5+x^5)^2
    sage: f.factor()
    (y+x)*(y^4-x*y^3+x^2*y^2-x^3*y+x^4)

Python

    >>> from sage.all import *
    >>> from sage.interfaces.maxima import maxima
    >>> f = maxima('x^5 + y^5')
    >>> f**Integer(2)
    (y^5+x^5)^2
    >>> f.factor()
    (y+x)*(y^4-x*y^3+x^2*y^2-x^3*y+x^4)

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

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

[Python]

    >>> from sage.all import *
    >>> maxima('sum(1/x^2, x, 1, 10)')
    1968329/1270080

## Tutorial

We follow the tutorial at http://maxima.sourceforge.net/docs/intromax/intromax.html.

Sage

    sage: from sage.interfaces.maxima import maxima
    sage: maxima('1/100 + 1/101')
    201/10100

Python

    >>> from sage.all import *
    >>> from sage.interfaces.maxima import maxima
    >>> maxima('1/100 + 1/101')
    201/10100

Sage

    sage: a = maxima('(1 + sqrt(2))^5'); a
    (sqrt(2)+1)^5
    sage: a.expand()
    29*sqrt(2)+41

[Python]

    >>> from sage.all import *
    >>> a = maxima('(1 + sqrt(2))^5'); a
    (sqrt(2)+1)^5
    >>> a.expand()
    29*sqrt(2)+41

Sage

    sage: a = maxima('(1 + sqrt(2))^5')
    sage: float(a)
    82.0121933088197...
    sage: a.numer()
    82.0121933088197...

Python

    >>> from sage.all import *
    >>> a = maxima('(1 + sqrt(2))^5')
    >>> float(a)
    82.0121933088197...
    >>> a.numer()
    82.0121933088197...

Sage

    sage: maxima.eval('fpprec : 100')
    '100'
    sage: a.bfloat()
    8.20121933088197564152489730020812442785204843859314941221237124017312418754011041266612384955016056b1

[Python]

    >>> from sage.all import *
    >>> maxima.eval('fpprec : 100')
    '100'
    >>> a.bfloat()
    8.20121933088197564152489730020812442785204843859314941221237124017312418754011041266612384955016056b1

Sage

    sage: maxima('100!')
    93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Python

    >>> from sage.all import *
    >>> maxima('100!')
    93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

Sage

    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')
    (5/z+(25*y)/z^2+3*y)^3
    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()
    (3*y*z^2+5*z+25*y)^3/z^6

[Python]

    >>> from sage.all import *
    >>> f = maxima('(x + 3*y + x^2*y)^3')
    >>> 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
    >>> f.subst('x=5/z')
    (5/z+(25*y)/z^2+3*y)^3
    >>> g = f.subst('x=5/z')
    >>> 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
    >>> h.factor()
    (3*y*z^2+5*z+25*y)^3/z^6

Sage

    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...]]

Python

    >>> from sage.all import *
    >>> eqn = maxima(['a+b*c=1', 'b-a*c=0', 'a+b=5'])
    >>> 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

    sage: from sage.interfaces.maxima import maxima
    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)]

[Python]

    >>> from sage.all import *
    >>> from sage.interfaces.maxima import maxima
    >>> maxima('x^2+y^2=1').solve('y')
    [y = -sqrt(1-x^2),y = sqrt(1-x^2)]
    >>> 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

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

Python

    >>> from sage.all import *
    >>> latex(s)
    \left[ \left[ a=-\left({{\sqrt{79}\,i-11}\over{4}}\right) , b={{\sqrt{79}\,i  +9}\over{4}} , c={{\sqrt{79}\,i+1}\over{10}} \right]  , \left[ a={{\sqrt{79}  \,i+11}\over{4}} , b=-\left({{\sqrt{79}\,i-9}\over{4}}\right) , c=-\left({{  \sqrt{79}\,i-1}\over{10}}\right) \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

    sage: e = maxima('sin(u + v) * cos(u)^3'); e
    cos(u)^3*sin(v+u)
    sage: f = e.trigexpand(); f
    cos(u)^3*(cos(u)*sin(v)+sin(u)*cos(v))
    sage: f.trigreduce()
    (sin(v+4*u)+sin(v-2*u))/8+(3*sin(v+2*u)+3*sin(v))/8
    sage: w = maxima('3 + k*%i')
    sage: f = w^2 + maxima('%e')^w
    sage: f.realpart()
    %e^3*cos(k)-k^2+9

[Python]

    >>> from sage.all import *
    >>> e = maxima('sin(u + v) * cos(u)^3'); e
    cos(u)^3*sin(v+u)
    >>> f = e.trigexpand(); f
    cos(u)^3*(cos(u)*sin(v)+sin(u)*cos(v))
    >>> f.trigreduce()
    (sin(v+4*u)+sin(v-2*u))/8+(3*sin(v+2*u)+3*sin(v))/8
    >>> w = maxima('3 + k*%i')
    >>> f = w**Integer(2) + maxima('%e')**w
    >>> f.realpart()
    %e^3*cos(k)-k^2+9

Sage

    sage: f = maxima('x^3 * %e^(k*x) * sin(w*x)'); f
    %e^(k*x)*x^3*sin(w*x)
    sage: f.diff('x')
    %e^(k*x)*k*x^3*sin(w*x)+3*%e^(k*x)*x^2*sin(w*x)+%e^(k*x)*w*x^3 *cos(w*x)
    sage: f.integrate('x')
    (((%e^(k*x)*k*w^6+3*%e^(k*x)*k^3*w^4+3*%e^(k*x)*k^5*w^2+%e^(k*x)*k^7)*x ^3 +(3*%e^(k*x)*w^6+3*%e^(k*x)*k^2*w^4-3*%e^(k*x)*k^4*w^2-3*%e^(k*x)*k^6)*x^2 +(-(18*%e^(k*x)*k*w^4)-12*%e^(k*x)*k^3*w^2+6*%e^(k*x)*k^5)*x-6*%e^(k*x)*w^4 +36*%e^(k*x)*k^2*w^2-6*%e^(k*x)*k^4) *sin(w*x) +((-(%e^(k*x)*w^7)-3*%e^(k*x)*k^2*w^5-3*%e^(k*x)*k^4*w^3-%e^(k*x)*k^6*w)*x^3 +(6*%e^(k*x)*k*w^5+12*%e^(k*x)*k^3*w^3+6*%e^(k*x)*k^5*w)*x^2 +(6*%e^(k*x)*w^5-12*%e^(k*x)*k^2*w^3-18*%e^(k*x)*k^4*w)*x-24*%e^(k*x)*k*w^3 +24*%e^(k*x)*k^3*w) *cos(w*x)) /(w^8+4*k^2*w^6+6*k^4*w^4+4*k^6*w^2+k^8)

Python

    >>> from sage.all import *
    >>> f = maxima('x^3 * %e^(k*x) * sin(w*x)'); f
    %e^(k*x)*x^3*sin(w*x)
    >>> f.diff('x')
    %e^(k*x)*k*x^3*sin(w*x)+3*%e^(k*x)*x^2*sin(w*x)+%e^(k*x)*w*x^3 *cos(w*x)
    >>> f.integrate('x')
    (((%e^(k*x)*k*w^6+3*%e^(k*x)*k^3*w^4+3*%e^(k*x)*k^5*w^2+%e^(k*x)*k^7)*x ^3 +(3*%e^(k*x)*w^6+3*%e^(k*x)*k^2*w^4-3*%e^(k*x)*k^4*w^2-3*%e^(k*x)*k^6)*x^2 +(-(18*%e^(k*x)*k*w^4)-12*%e^(k*x)*k^3*w^2+6*%e^(k*x)*k^5)*x-6*%e^(k*x)*w^4 +36*%e^(k*x)*k^2*w^2-6*%e^(k*x)*k^4) *sin(w*x) +((-(%e^(k*x)*w^7)-3*%e^(k*x)*k^2*w^5-3*%e^(k*x)*k^4*w^3-%e^(k*x)*k^6*w)*x^3 +(6*%e^(k*x)*k*w^5+12*%e^(k*x)*k^3*w^3+6*%e^(k*x)*k^5*w)*x^2 +(6*%e^(k*x)*w^5-12*%e^(k*x)*k^2*w^3-18*%e^(k*x)*k^4*w)*x-24*%e^(k*x)*k*w^3 +24*%e^(k*x)*k^3*w) *cos(w*x)) /(w^8+4*k^2*w^6+6*k^4*w^4+4*k^6*w^2+k^8)

Sage

    sage: f = maxima('1/x^2')
    sage: f.integrate('x', 1, 'inf')
    1
    sage: g = maxima('f/sinh(k*x)^4')
    sage: g.taylor('x', 0, 3)
    f/(k^4*x^4)-(2*f)/((3*k^2)*x^2)+(11*f)/45-((62*k^2*f)*x^2)/945

[Python]

    >>> from sage.all import *
    >>> f = maxima('1/x^2')
    >>> f.integrate('x', Integer(1), 'inf')
    1
    >>> g = maxima('f/sinh(k*x)^4')
    >>> g.taylor('x', Integer(0), Integer(3))
    f/(k^4*x^4)-(2*f)/((3*k^2)*x^2)+(11*f)/45-((62*k^2*f)*x^2)/945

Sage

    sage: maxima.taylor('asin(x)','x',0, 10)
    x+x^3/6+(3*x^5)/40+(5*x^7)/112+(35*x^9)/1152

Python

    >>> from sage.all import *
    >>> maxima.taylor('asin(x)','x',Integer(0), Integer(10))
    x+x^3/6+(3*x^5)/40+(5*x^7)/112+(35*x^9)/1152

## Examples involving matrices

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

Sage

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

Python

    >>> from sage.all import *
    >>> f = maxima.eval('f[i,j] := i/j')
    >>> A = maxima('genmatrix(f,4,4)'); A
    matrix([1,1/2,1/3,1/4],[2,1,2/3,1/2],[3,3/2,1,3/4],[4,2,4/3,1])
    >>> A.determinant()
    0
    >>> A.echelon()
    matrix([1,1/2,1/3,1/4],[0,0,0,0],[0,0,0,0],[0,0,0,0])
    >>> A.eigenvalues()
    [[0,4],[3,1]]
    >>> A.eigenvectors()
    [[[0,4],[3,1]],[[[1,0,0,-4],[0,1,0,-2],[0,0,1,-...4/3...]],[[1,2,3,4]]]]

We can also compute the echelon form in Sage:

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

[Python]

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

## Laplace Transforms

We illustrate Laplace transforms:

Sage

    sage: _ = maxima.eval("f(t) := t*sin(t)")
    sage: maxima("laplace(f(t),t,s)")
    (2*s)/(s^2+1)^2

Python

    >>> from sage.all import *
    >>> _ = maxima.eval("f(t) := t*sin(t)")
    >>> maxima("laplace(f(t),t,s)")
    (2*s)/(s^2+1)^2

Sage

    sage: maxima("laplace(delta(t-3),t,s)") #Dirac delta function
    %e^-(3*s)

[Python]

    >>> from sage.all import *
    >>> maxima("laplace(delta(t-3),t,s)") #Dirac delta function
    %e^-(3*s)

Sage

    sage: _ = maxima.eval("f(t) := exp(t)*sin(t)")
    sage: maxima("laplace(f(t),t,s)")
    1/(s^2-2*s+2)

Python

    >>> from sage.all import *
    >>> _ = maxima.eval("f(t) := exp(t)*sin(t)")
    >>> maxima("laplace(f(t),t,s)")
    1/(s^2-2*s+2)

Sage

    sage: _ = maxima.eval("f(t) := t^5*exp(t)*sin(t)")
    sage: maxima("laplace(f(t),t,s)")
    (720*s^5-3600*s^4+4800*s^3-2880*s+960) /(s^12-12*s^11+72*s^10-280*s^9+780*s^8-1632*s^7+2624*s^6-3264*s^5+3120*s^4 -2240*s^3+1152*s^2-384*s+64)
    sage: print(maxima("laplace(f(t),t,s)"))
                                                   5         4         3
                                             (720 s  - 3600 s  + 4800 s  - 2880 s + 960)
                                                12       11       10        9        8         7         6         5
                                             /(s   - 12 s   + 72 s   - 280 s  + 780 s  - 1632 s  + 2624 s  - 3264 s
                                                      4         3         2
                                              + 3120 s  - 2240 s  + 1152 s  - 384 s + 64)

[Python]

    >>> from sage.all import *
    >>> _ = maxima.eval("f(t) := t^5*exp(t)*sin(t)")
    >>> maxima("laplace(f(t),t,s)")
    (720*s^5-3600*s^4+4800*s^3-2880*s+960) /(s^12-12*s^11+72*s^10-280*s^9+780*s^8-1632*s^7+2624*s^6-3264*s^5+3120*s^4 -2240*s^3+1152*s^2-384*s+64)
    >>> print(maxima("laplace(f(t),t,s)"))
                                                   5         4         3
                                             (720 s  - 3600 s  + 4800 s  - 2880 s + 960)
                                                12       11       10        9        8         7         6         5
                                             /(s   - 12 s   + 72 s   - 280 s  + 780 s  - 1632 s  + 2624 s  - 3264 s
                                                      4         3         2
                                              + 3120 s  - 2240 s  + 1152 s  - 384 s + 64)

Sage

    sage: maxima("laplace(diff(x(t),t),t,s)")
    s*'laplace(x(t),t,s)-x(0)

Python

    >>> from sage.all import *
    >>> maxima("laplace(diff(x(t),t),t,s)")
    s*'laplace(x(t),t,s)-x(0)

Sage

    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

[Python]

    >>> from sage.all import *
    >>> 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

    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

Python

    >>> from sage.all import *
    >>> 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

    sage: maxima("cf((1 + sqrt(5))/2)")
    [1,1,1,1,2]
    sage: maxima("cf ((1 + sqrt(341))/2)")
    [9,1,2,1,2,1,17,1,2,1,2,1,17,1,2,1,2,1,17,2]

Python

    >>> from sage.all import *
    >>> maxima("cf((1 + sqrt(5))/2)")
    [1,1,1,1,2]
    >>> maxima("cf ((1 + sqrt(341))/2)")
    [9,1,2,1,2,1,17,1,2,1,2,1,17,1,2,1,2,1,17,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

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

Python

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

You can plot 3d graphs (via gnuplot):

Sage

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

[Python]

    >>> from sage.all import *
    >>> 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

    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)

Python

    >>> from sage.all import *
    >>> S = maxima('nusum(exp(1+2*i/n),i,1,n)')
    >>> 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

    sage: T = S*maxima('2/n')
    sage: T.tlimit('n','inf')
    %e^3-%e

[Python]

    >>> from sage.all import *
    >>> T = S*maxima('2/n')
    >>> T.tlimit('n','inf')
    %e^3-%e

## Miscellaneous

Obtaining digits of \(\pi\):

Sage

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

Python

    >>> from sage.all import *
    >>> maxima.eval('fpprec : 100')
    '100'
    >>> maxima(pi).bfloat()
    3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068b0

Defining functions in maxima:

Sage

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

[Python]

    >>> from sage.all import *
    >>> maxima.eval('fun[a] := a^2')
    'fun[a]:=a^2'
    >>> maxima('fun[10]')
    100

## Interactivity

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 http://www.ma.utexas.edu/pipermail/maxima/2005/011061.html 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

    sage: from sage.interfaces.maxima import maxima
    sage: latex(maxima('sin(u) + sinh(v^2)'))
    \sinh v^2+\sin u

Python

    >>> from sage.all import *
    >>> from sage.interfaces.maxima import maxima
    >>> latex(maxima('sin(u) + sinh(v^2)'))
    \sinh v^2+\sin u

Here’s another example:

Sage

    sage: from sage.interfaces.maxima import maxima
    sage: g = maxima('exp(3*%i*x)/(6*%i) + exp(%i*x)/(2*%i) + c')
    sage: latex(g)
    c-{{e^{3\,i\,x}\,i}\over{6}}-{{e^{i\,x}\,i}\over{2}}

[Python]

    >>> from sage.all import *
    >>> from sage.interfaces.maxima import maxima
    >>> g = maxima('exp(3*%i*x)/(6*%i) + exp(%i*x)/(2*%i) + c')
    >>> latex(g)
    c-{{e^{3\,i\,x}\,i}\over{6}}-{{e^{i\,x}\,i}\over{2}}

## 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.

Note

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

Sage

    sage: t = '"%s"'%10^10000   # ten thousand character string.
    sage: a = maxima(t)

Python

    >>> from sage.all import *
    >>> t = '"%s"'%Integer(10)**Integer(10000)   # ten thousand character string.
    >>> 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.

  EXAMPLES:

  Sage

      sage: from sage.interfaces.maxima import maxima, Maxima
      sage: m = Maxima()
      sage: m == maxima
      False

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.maxima import maxima, Maxima
      >>> m = Maxima()
      >>> m == maxima
      False

  clear(var)

    Clear the variable named var.

    EXAMPLES:

    Sage

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

    Python

        >>> from sage.all import *
        >>> maxima.set('xxxxx', '2')
        >>> maxima.get('xxxxx')
        '2'
        >>> maxima.clear('xxxxx')
        >>> maxima.get('xxxxx')
        'xxxxx'

  get(var)

    Get the string value of the variable var.

    EXAMPLES:

    Sage

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

    Python

        >>> from sage.all import *
        >>> maxima.set('xxxxx', '2')
        >>> maxima.get('xxxxx')
        '2'

  lisp(cmd)

    Send a lisp command to Maxima.

    Note

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

    EXAMPLES:

    Sage

        sage: maxima.lisp("(+ 2 17)")   # random formatted output
         :lisp (+ 2 17)
        19
        (

    Python

        >>> from sage.all import *
        >>> maxima.lisp("(+ 2 17)")   # random formatted output
         :lisp (+ 2 17)
        19
        (

  set(var, value)

    Set the variable var to the given value.

    INPUT:

    - `var` – string

    - `value` – string

    EXAMPLES:

    Sage

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

    Python

        >>> from sage.all import *
        >>> maxima.set('xxxxx', '2')
        >>> maxima.get('xxxxx')
        '2'

  set_seed(seed=None)

    http://maxima.sourceforge.net/docs/manual/maxima_10.html 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.

    EXAMPLES:

    Sage

        sage: from sage.interfaces.maxima import Maxima
        sage: m = Maxima()
        sage: m.set_seed(1)
        1
        sage: [m.random(100) for i in range(5)]
        [45, 39, 24, 68, 63]

    Python

        >>> from sage.all import *
        >>> from sage.interfaces.maxima import Maxima
        >>> m = Maxima()
        >>> m.set_seed(Integer(1))
        1
        >>> [m.random(Integer(100)) for i in range(Integer(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.

  EXAMPLES:

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

  Sage

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

  Python

      >>> from sage.all import *
      >>> maxima(Integer(3))
      3
      >>> maxima(cos(x)+e**Integer(234))
      cos(_SAGE_VAR_x)+%e^234

  display2d(onscreen=True)

    Return the 2d string representation of this Maxima object.

    EXAMPLES:

    Sage

        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 )

    Python

        >>> from sage.all import *
        >>> F = maxima('x^5 - y^5').factor()
        >>> 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.

  EXAMPLES:

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

  Sage

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

  Python

      >>> from sage.all import *
      >>> maxima.function('x,y','h(x)*y')
      h(x)*y

sage.interfaces.maxima.reduce_load_Maxima()

  Unpickle a Maxima Pexpect interface.

  EXAMPLES:

  Sage

      sage: from sage.interfaces.maxima import reduce_load_Maxima
      sage: reduce_load_Maxima()
      Maxima

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.maxima import reduce_load_Maxima
      >>> reduce_load_Maxima()
      Maxima

sage.interfaces.maxima.reduce_load_Maxima_function(parent, defn, args, latex)

  Unpickle a Maxima function.

  EXAMPLES:

  Sage

      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
      True

  Python

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