4.8.9 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: t = '"%s"'%10^10000   # ten thousand character string.
sage: a = maxima(t)

Module-level Functions

__doctest_cleanup( )

is_MaximaElement( x)

maxima_console( )

maxima_version( )

reduce_load_Maxima( )

Class: Maxima

class Maxima
Interface to the Maxima interpreter.
Maxima( self, [script_subdirectory=None], [logfile=None], [server=None])

Create an instance of the Maxima interpreter.

Functions: console,$  $ de_solve,$  $ de_solve_laplace,$  $ demo,$  $ describe,$  $ display2d,$  $ example,$  $ function,$  $ get,$  $ help,$  $ plot2d,$  $ plot2d_parametric,$  $ plot3d,$  $ plot3d_parametric,$  $ plot_list,$  $ plot_multilist,$  $ set,$  $ solve_linear,$  $ unit_quadratic_integer,$  $ version

de_solve( maxima, de, vars, [ics=None])

Solves a 1st or 2nd order ordinary differential equation (ODE) in two variables, possibly with initial conditions.

INPUT:
    de -- a string representing the ODE
    vars -- a list of strings representing the two variables.
    ics -- a triple of numbers [a,b1,b2] representing
           y(a)=b1, y'(a)=b2

sage: maxima.de_solve('diff(y,x,2) + 3*x = y', ['x','y'], [1,1,1])
y = 3*x - 2*%e^(x - 1)
sage: maxima.de_solve('diff(y,x,2) + 3*x = y', ['x','y'])
y = %k1*%e^x + %k2*%e^ - x + 3*x
sage: maxima.de_solve('diff(y,x) + 3*x = y', ['x','y'])
y = (%c - 3*( - x - 1)*%e^ - x)*%e^x
sage: maxima.de_solve('diff(y,x) + 3*x = y', ['x','y'],[1,1])
y =  - %e^ - 1*(5*%e^x - 3*%e*x - 3*%e)

de_solve_laplace( self, de, vars, [ics=None])

Solves an ordinary differential equation (ODE) using Laplace transforms.

INPUT:
    de -- a string representing the ODE
          (e.g., de = "diff(f(x),x,2)=diff(f(x),x)+sin(x)")
    vars -- a list of strings representing the variables
          (e.g., vars = ["x","f"])
    ics -- a list of numbers representing initial conditions,
           with symbols allowed which are represented by strings
           (eg, f(0)=1, f'(0)=2 is ics = [0,1,2])

sage: maxima.clear('x'); maxima.clear('f')
sage: maxima.de_solve_laplace("diff(f(x),x,2) = 2*diff(f(x),x)-f(x)", ["x","f"], [0,1,2])
f(x) = x*%e^x + %e^x

sage: maxima.clear('x'); maxima.clear('f')            
sage: f = maxima.de_solve_laplace("diff(f(x),x,2) = 2*diff(f(x),x)-f(x)", ["x","f"])
sage: f
f(x) = x*%e^x*(at('diff(f(x),x,1),x = 0)) - f(0)*x*%e^x + f(0)*%e^x
sage: f.display2d()
                                   !
                       x  d        !                  x          x
            f(x) = x %e  (-- (f(x))!     ) - f(0) x %e  + f(0) %e
                          dx       !
                                   !x = 0

oteThe second equation sets the values of $ f(0)$ and $ f'(0)$ in maxima, so subsequent ODEs involving these variables will have these initial conditions automatically imposed.

display2d( self, [flag=True])

Set the flag that determines whether Maxima objects are printed using their 2-d ASCII art representation. When the maxima interface starts the default is that objects are not represented in 2-d.

INPUT:
    flag -- bool (default: True)

EXAMPLES

sage: maxima('1/2')
1/2
sage: maxima.display2d(True)
sage: maxima('1/2')
                               1
                               -
                               2
sage: maxima.display2d(False)

function( self, args, defn)

Return the Maxima function with given arguments and definition.

INPUT:
    args -- a string with variable names separated by commas
    defn -- a string (or Maxima expression) that defines
            a function of the arguments in Maxima.

sage: f = maxima.function('x', 'sin(x)')
sage: f(3.2)
 -.0583741434275801
sage: f = maxima.function('x,y', 'sin(x)+cos(y)')
sage: f(2,3.5)
sin(2) - .9364566872907963
sage: f
sin(x)+cos(y)

sage: g = f.integrate('z'); g
(cos(y) + sin(x))*z
sage: g(1,2,3)
3*(cos(2) + sin(1))

The function definition can be a maxima object:

sage: an_expr = maxima('sin(x)*gamma(x)')
sage: t = maxima.function('x', an_expr)
sage: t
gamma(x)*sin(x)
sage: t(2)
sin(2)
sage: float(t(2))
0.90929742682568171
sage: loads(t.dumps())
gamma(x)*sin(x)

get( self, var)

Get the string value of the variable var.

plot2d( self)

Plot a 2d graph using Maxima / gnuplot.

maxima.plot2d(f, '[var, min, max]', options)

INPUT:
    f -- a string representing a function (such as f="sin(x)")
    [var, xmin, xmax] 
    options -- an optional string representing plot2d options in gnuplot
format

sage: maxima.plot2d('sin(x)','[x,-5,5]') 
sage: opts = '[gnuplot_term, ps], [gnuplot_out_file, "sin-plot.eps"]'
sage: maxima.plot2d('sin(x)','[x,-5,5]',opts)

The eps file is saved in the current directory.

plot2d_parametric( self, r, var, trange, [nticks=None], [options=50])

Plots r = [x(t), y(t)] for t = tmin...tmax using gnuplot with options

INPUT:
    r -- a string representing a function (such as r="[x(t),y(t)]")
    var -- a string representing the variable (such as var = "t")
    trange -- [tmin, tmax] are numbers with tmin<tmax
    nticks -- int (default: 50)
    options -- an optional string representing plot2d options in gnuplot
format

sage: maxima.plot2d_parametric(["sin(t)","cos(t)"], "t",[-3.1,3.1])

sage: opts = '[gnuplot_preamble, "set nokey"], [gnuplot_term, ps], [gnuplot_out_file, "circle-plot.eps"]'
sage: maxima.plot2d_parametric(["sin(t)","cos(t)"], "t", [-3.1,3.1], options=opts)

The eps file is saved to the current working directory.

Here is another fun plot:

sage: maxima.plot2d_parametric(["sin(5*t)","cos(11*t)"], "t", [0,2*pi()], nticks=400)

plot3d( self)

Plot a 3d graph using Maxima / gnuplot.

maxima.plot3d(f, '[x, xmin, xmax]', '[y, ymin, ymax]', '[grid, nx, ny]', options)

INPUT:
    f -- a string representing a function (such as f="sin(x)")
    [var, min, max]

sage: maxima.plot3d('1 + x^3 - y^2', '[x,-2,2]', '[y,-2,2]', '[grid,12,12]') 
sage: maxima.plot3d('sin(x)*cos(y)', '[x,-2,2]', '[y,-2,2]', '[grid,30,30]')
sage: opts = '[gnuplot_term, ps], [gnuplot_out_file, "sin-plot.eps"]' 
sage: maxima.plot3d('sin(x+y)', '[x,-5,5]', '[y,-1,1]', opts)

The eps file is saved in the current working directory.

plot3d_parametric( self, r, vars, urange, vrange, [options=None])

Plot a 3d parametric graph with r=(x,y,z), x = x(u,v), y = y(u,v), z = z(u,v), for u = umin...umax, v = vmin...vmax using gnuplot with options.

INPUT:
    x, y, z -- a string representing a function (such as x="u^2+v^2", ...)
    vars is a list or two strings representing variables (such as vars =
["u","v"])
    urange -- [umin, umax]
    vrange -- [vmin, vmax] are lists of numbers with
    umin < umax, vmin < vmax
    options -- optional string representing plot2d options in gnuplot
format

OUTPUT:
    displays a plot on screen or saves to a file

sage: maxima.plot3d_parametric(["v*sin(u)","v*cos(u)","v"], ["u","v"],[-3.2,3.2],[0,3])
sage: opts = '[gnuplot_term, ps], [gnuplot_out_file, "sin-cos-plot.eps"]'
sage: maxima.plot3d_parametric(["v*sin(u)","v*cos(u)","v"], ["u","v"],[-3.2,3.2],[0,3],opts)

The eps file is saved in the current working directory.

Here is a torus:

sage: _ = maxima.eval("expr_1: cos(y)*(10.0+6*cos(x)); expr_2: sin(y)*(10.0+6*cos(x)); expr_3: -6*sin(x);")  # optional
sage: maxima.plot3d_parametric(["expr_1","expr_2","expr_3"], ["x","y"],[0,6],[0,6])

Here is a Mobius strip:

sage: x = "cos(u)*(3 + v*cos(u/2))"
sage: y = "sin(u)*(3 + v*cos(u/2))"
sage: z = "v*sin(u/2)"
sage: maxima.plot3d_parametric([x,y,z],["u","v"],[-3.1,3.2],[-1/10,1/10])

plot_list( self, ptsx, ptsy, [options=None])

Plots a curve determined by a sequence of points.

INPUT:
    ptsx -- [x1,...,xn], where the xi and yi are real,
    ptsy -- [y1,...,yn]
    options -- a string representing maxima plot2d options.

The points are (x1,y1), (x2,y2), etc.

This function requires maxima 5.9.2 or newer.

Note: More that 150 points can sometimes lead to the program hanging. Why?

sage: zeta_ptsx = [ (pari(1/2 + i*I/10).zeta().real()).precision(1) for i in range (70,150)]  
sage: zeta_ptsy = [ (pari(1/2 + i*I/10).zeta().imag()).precision(1) for i in range (70,150)]  
sage: maxima.plot_list(zeta_ptsx, zeta_ptsy)                   
sage: opts='[gnuplot_preamble, "set nokey"], [gnuplot_term, ps], [gnuplot_out_file, "zeta.eps"]'
sage: maxima.plot_list(zeta_ptsx, zeta_ptsy, opts)

plot_multilist( self, pts_list, [options=None])

Plots a list of list of points pts_list=[pts1,pts2,...,ptsn], where each ptsi is of the form [[x1,y1],...,[xn,yn]] x's must be integers and y's reals options is a string representing maxima plot2d options.

Requires maxima 5.9.2 at least. Note: More that 150 points can sometimes lead to the program hanging.

sage: xx = [ i/10.0 for i in range (-10,10)]
sage: yy = [ i/10.0 for i in range (-10,10)]
sage: x0 = [ 0 for i in range (-10,10)]
sage: y0 = [ 0 for i in range (-10,10)]
sage: zeta_ptsx1 = [ (pari(1/2+i*I/10).zeta().real()).precision(1) for i in range (10)]
sage: zeta_ptsy1 = [ (pari(1/2+i*I/10).zeta().imag()).precision(1) for i in range (10)]
sage: maxima.plot_multilist([[zeta_ptsx1,zeta_ptsy1],[xx,y0],[x0,yy]])    
sage: zeta_ptsx1 = [ (pari(1/2+i*I/10).zeta().real()).precision(1) for i in range (10,150)]
sage: zeta_ptsy1 = [ (pari(1/2+i*I/10).zeta().imag()).precision(1) for i in range (10,150)]
sage: maxima.plot_multilist([[zeta_ptsx1,zeta_ptsy1],[xx,y0],[x0,yy]])    
sage: opts='[gnuplot_preamble, "set nokey"]'                 
sage: maxima.plot_multilist([[zeta_ptsx1,zeta_ptsy1],[xx,y0],[x0,yy]],opts)

set( self, var, value)

Set the variable var to the given value.

solve_linear( self, eqns, vars)

Wraps maxima's linsolve.

INPUT:
eqns is a list of m strings, each rperesenting a linear question
in m <= n variables
vars is a list of n strings, each representing a variable

sage: eqns = ["x + z = y","2*a*x - y = 2*a^2","y - 2*z = 2"]    
sage: vars = ["x","y","z"]                                      
sage: maxima.solve_linear(eqns, vars)                         
[x = a + 1,y = 2*a,z = a - 1]

unit_quadratic_integer( self, n)

Finds a unit of the ring of integers of the quadratic number field $ \mathbf{Q}(\sqrt{n})$ , $ n>1$ , using the qunit maxima command.

sage: u = maxima.unit_quadratic_integer(101)           
sage: u.parent()                                       
Number Field in a with defining polynomial x^2 - 101
sage: u                                                
a + 10
sage: u = maxima.unit_quadratic_integer(13)            
sage: u                                                
5*a + 18
sage: u.parent()                                       
Number Field in a with defining polynomial x^2 - 13

Special Functions: __call__,$  $ __reduce__,$  $ _eval_line,$  $ _eval_line_using_file,$  $ _false_symbol,$  $ _object_class,$  $ _quit_string,$  $ _start,$  $ _true_symbol

Class: MaximaElement

class MaximaElement

Functions: comma,$  $ derivative,$  $ diff,$  $ display2d,$  $ imag,$  $ integral,$  $ integrate,$  $ nintegral,$  $ numer,$  $ partial_fraction_decomposition,$  $ real,$  $ str,$  $ subst

derivative( self, [var=1], [n=x])

Return the n-th derivative of self.

INPUT:
    var -- variable (default: 'x')
    n -- integer (default: 1)

OUTPUT:
    n-th derivative of self with respect to the variable var

sage: f = maxima('x^2')                          
sage: f.diff()                                   
2*x
sage: f.diff('x')                                
2*x
sage: f.diff('x', 2)                             
2
sage: maxima('sin(x^2)').diff('x',4)             
16*x^4*sin(x^2) - 12*sin(x^2) - 48*x^2*cos(x^2)

sage: f = maxima('x^2 + 17*y^2')                 
sage: f.diff('x')
2*x
sage: f.diff('y')                                
34*y

diff( self, [var=1], [n=x])

Return the n-th derivative of self.

INPUT:
    var -- variable (default: 'x')
    n -- integer (default: 1)

OUTPUT:
    n-th derivative of self with respect to the variable var

sage: f = maxima('x^2')                          
sage: f.diff()                                   
2*x
sage: f.diff('x')                                
2*x
sage: f.diff('x', 2)                             
2
sage: maxima('sin(x^2)').diff('x',4)             
16*x^4*sin(x^2) - 12*sin(x^2) - 48*x^2*cos(x^2)

sage: f = maxima('x^2 + 17*y^2')                 
sage: f.diff('x')
2*x
sage: f.diff('y')                                
34*y

display2d( self, [onscreen=True])

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 )

integral( self, [var=None], [min=None], [max=x])

Return the integral of self with respect to the variable x.

INPUT:
    var -- variable
    min -- default: None
    max -- default: None

Returns the definite integral if xmin is not None, otherwise returns an indefinite integral.

sage: maxima('x^2+1').integral()                   
x^3/3 + x
sage: maxima('x^2+ 1 + y^2').integral('y')         
y^3/3 + x^2*y + y
sage: maxima('x / (x^2+1)').integral()             
log(x^2 + 1)/2
sage: maxima('1/(x^2+1)').integral()               
atan(x)
sage: maxima('1/(x^2+1)').integral('x', 0, infinity) 
%pi/2
sage: maxima('x/(x^2+1)').integral('x', -1, 1)     
0

sage: f = maxima('exp(x^2)').integral('x',0,1); f   
-sqrt(%pi)*%i*erf(%i)/2
sage: f.numer()         # I wonder how to get a real number (~1.463)?? 
-.8862269254527579*%i*erf(%i)

integrate( self, [var=None], [min=None], [max=x])

Return the integral of self with respect to the variable x.

INPUT:
    var -- variable
    min -- default: None
    max -- default: None

Returns the definite integral if xmin is not None, otherwise returns an indefinite integral.

sage: maxima('x^2+1').integral()                   
x^3/3 + x
sage: maxima('x^2+ 1 + y^2').integral('y')         
y^3/3 + x^2*y + y
sage: maxima('x / (x^2+1)').integral()             
log(x^2 + 1)/2
sage: maxima('1/(x^2+1)').integral()               
atan(x)
sage: maxima('1/(x^2+1)').integral('x', 0, infinity) 
%pi/2
sage: maxima('x/(x^2+1)').integral('x', -1, 1)     
0

sage: f = maxima('exp(x^2)').integral('x',0,1); f   
-sqrt(%pi)*%i*erf(%i)/2
sage: f.numer()         # I wonder how to get a real number (~1.463)?? 
-.8862269254527579*%i*erf(%i)

nintegral( self, [var=200], [a=1e-8], [b=1], [desired_relative_error=0], [maximum_num_subintervals=x])

Return a numerical approximation to the integral of self from a to b.

INPUT:
    var -- variable to integrate with respect to
    a -- lower endpoint of integration
    b -- upper endpoint of integration
    desired_relative_error -- (default: '1e-8') the desired
         relative error
    maximum_num_subintervals -- (default: 200) maxima number
         of subintervals

OUTPUT:
    -- approximation to the integral
    -- estimated absolute error of the approximation
    -- the number of integrand evaluations
    -- an error code:
          0 -- no problems were encountered
          1 -- too many subintervals were done
          2 -- excessive roundoff error
          3 -- extremely bad integrand behavior
          4 -- failed to converge
          5 -- integral is probably divergent or slowly convergent
          6 -- the input is invalid

sage: maxima('exp(-sqrt(x))').nintegral('x',0,1)
(.5284822353142306, 4.16331413788384E-11, 231, 0)

Note that GP also does numerical integration, and can do so to very high precision very quickly:

sage: gp('intnum(x=0,1,exp(-sqrt(x)))')            
0.5284822353142307136179049194             # 32-bit
0.52848223531423071361790491935415653021   # 64-bit
sage: _ = gp.set_precision(80)
sage: gp('intnum(x=0,1,exp(-sqrt(x)))')
0.5284822353142307136179049193541565302167554758729286619686527932101540170
2040079

partial_fraction_decomposition( self, [var=x])

Return the partial fraction decomposition of self with respect to the variable var.

sage: f = maxima('1/((1+x)*(x-1))')            
sage: f.partial_fraction_decomposition('x')    
1/(2*(x - 1)) - 1/(2*(x + 1))
sage: f.partial_fraction_decomposition('x').display2d() 
                     1           1
                 --------- - ---------
                 2 (x - 1)   2 (x + 1)

Special Functions: __call__,$  $ __float__,$  $ __getitem__,$  $ __len__,$  $ __repr__,$  $ _cmp_,$  $ _latex_,$  $ _matrix_

__getitem__( self, n)

Return the n-th element of this list.

Note: Lists are 0-based when accessed via the SAGE interface, not 1-based as they are in the Maxima interpreter.

sage: v = maxima('create_list(i*x^i,i,0,5)'); v    
[0,x,2*x^2,3*x^3,4*x^4,5*x^5]
sage: v[3]                                         
3*x^3
sage: v[0]                                           
0
sage: v[10]                                          
Traceback (most recent call last):
...
IndexError: n = (10) must be between 0 and 5

__len__( self)

Return the length of a list.

sage: v = maxima('create_list(x^i,i,0,5)')         
sage: len(v)                                       
6

_cmp_( self, other)

sage: a = maxima(1); b = maxima(2)
sage: a == b
False
sage: a < b
True
sage: a > b
False
sage: b < a
False
sage: b > a
True

We can also compare more complicated object such as functions:

sage: f = maxima('sin(x)'); g = maxima('cos(x)')
sage: -f == g.diff('x')
True

_matrix_( self, R)

If self is a Maxima matrix, return the corresponding SAGE matrix over the SAGE ring $ R$ .

This may or may not work depending in how complicated the entries of self are! It only works if the entries of self can be coerced as strings to produce meaningful elements of $ R$ .

sage: _ = 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._matrix_(QQ)                                
[  1 1/2 1/3 1/4]
[  2   1 2/3 1/2]
[  3 3/2   1 3/4]
[  4   2 4/3   1]

You can also use the matrix command (which is defined in sage.misc.functional):

sage: matrix(A, QQ)                                 
[  1 1/2 1/3 1/4]
[  2   1 2/3 1/2]
[  3 3/2   1 3/4]
[  4   2 4/3   1]

Class: MaximaFunction

class MaximaFunction
MaximaFunction( self, parent, name, defn, args)

Functions: integral,$  $ integrate

Special Functions: __call__,$  $ __repr__

See About this document... for information on suggesting changes.