Interface to Mathematica - Interpreter Interfaces

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

# Interface to Mathematica

The Mathematica interface will only work if Mathematica is installed on your computer with a command line interface that runs when you give the `math` command. The interface lets you send certain Sage objects to Mathematica, run Mathematica functions, import certain Mathematica expressions to Sage, or any combination of the above. The Sage command:

Sage

    sage: print(mathematica._install_hints())
    ...

Python

    >>> from sage.all import *
    >>> print(mathematica._install_hints())
    ...

prints more information on Mathematica installation.

To send a Sage object `sobj` to Mathematica, call `mathematica(sobj)`. This exports the Sage object to Mathematica and returns a new Sage object wrapping the Mathematica expression/variable, so that you can use the Mathematica variable from within Sage. You can then call Mathematica functions on the new object; for example:

Sage

    sage: mobj = mathematica(x^2-1)             # optional - mathematica
    sage: mobj.Factor()                         # optional - mathematica
    (-1 + x)*(1 + x)

[Python]

    >>> from sage.all import *
    >>> mobj = mathematica(x**Integer(2)-Integer(1))             # optional - mathematica
    >>> mobj.Factor()                         # optional - mathematica
    (-1 + x)*(1 + x)

In the above example the factorization is done using Mathematica’s `Factor[]` function.

To see Mathematica’s output you can simply print the Mathematica wrapper object. However if you want to import Mathematica’s output back to Sage, call the Mathematica wrapper object’s `sage()` method. This method returns a native Sage object:

Sage

    sage: # optional - mathematica
    sage: mobj = mathematica(x^2-1)
    sage: mobj2 = mobj.Factor(); mobj2
    (-1 + x)*(1 + x)
    sage: mobj2.parent()
    Mathematica
    sage: sobj = mobj2.sage(); sobj
    (x + 1)*(x - 1)
    sage: sobj.parent()
    Symbolic Ring

Python

    >>> from sage.all import *
    >>> # optional - mathematica
    >>> mobj = mathematica(x**Integer(2)-Integer(1))
    >>> mobj2 = mobj.Factor(); mobj2
    (-1 + x)*(1 + x)
    >>> mobj2.parent()
    Mathematica
    >>> sobj = mobj2.sage(); sobj
    (x + 1)*(x - 1)
    >>> sobj.parent()
    Symbolic Ring

If you want to run a Mathematica function and don’t already have the input in the form of a Sage object, then it might be simpler to input a string to `mathematica(expr)`. This string will be evaluated as if you had typed it into Mathematica:

Sage

    sage: mathematica('Factor[x^2-1]')          # optional - mathematica
    (-1 + x)*(1 + x)
    sage: mathematica('Range[3]')               # optional - mathematica
    {1, 2, 3}

[Python]

    >>> from sage.all import *
    >>> mathematica('Factor[x^2-1]')          # optional - mathematica
    (-1 + x)*(1 + x)
    >>> mathematica('Range[3]')               # optional - mathematica
    {1, 2, 3}

If you don’t want Sage to go to the trouble of creating a wrapper for the Mathematica expression, then you can call `mathematica.eval(expr)`, which returns the result as a Mathematica AsciiArtString formatted string. If you want the result to be a string formatted like Mathematica’s InputForm, call `repr(mobj)` on the wrapper object `mobj`. If you want a string formatted in Sage style, call `mobj._sage_repr()`:

Sage

    sage: mathematica.eval('x^2 - 1')           # optional - mathematica
                   2
             -1 + x
    sage: repr(mathematica('Range[3]'))         # optional - mathematica
    '{1, 2, 3}'
    sage: mathematica('Range[3]')._sage_repr()  # optional - mathematica
    '[1, 2, 3]'

Python

    >>> from sage.all import *
    >>> mathematica.eval('x^2 - 1')           # optional - mathematica
                   2
             -1 + x
    >>> repr(mathematica('Range[3]'))         # optional - mathematica
    '{1, 2, 3}'
    >>> mathematica('Range[3]')._sage_repr()  # optional - mathematica
    '[1, 2, 3]'

Finally, if you just want to use a Mathematica command line from within Sage, the function `mathematica_console()` dumps you into an interactive command-line Mathematica session. This is an enhanced version of the usual Mathematica command-line, in that it provides readline editing and history (the usual one doesn’t!)

## Tutorial

We follow some of the tutorial from http://library.wolfram.com/conferences/devconf99/withoff/Basic1.html/.

For any of this to work you must buy and install the Mathematica program, and it must be available as the command `math` in your PATH.

### Syntax

Now make 1 and add it to itself. The result is a Mathematica object.

Sage

    sage: m = mathematica
    sage: a = m(1) + m(1); a                # optional - mathematica
    2
    sage: a.parent()                        # optional - mathematica
    Mathematica
    sage: m('1+1')                          # optional - mathematica
    2
    sage: m(3)**m(50)                       # optional - mathematica
    717897987691852588770249

Python

    >>> from sage.all import *
    >>> m = mathematica
    >>> a = m(Integer(1)) + m(Integer(1)); a                # optional - mathematica
    2
    >>> a.parent()                        # optional - mathematica
    Mathematica
    >>> m('1+1')                          # optional - mathematica
    2
    >>> m(Integer(3))**m(Integer(50))                       # optional - mathematica
    717897987691852588770249

The following is equivalent to `Plus[2, 3]` in Mathematica:

Sage

    sage: m = mathematica
    sage: m(2).Plus(m(3))                   # optional - mathematica
    5

[Python]

    >>> from sage.all import *
    >>> m = mathematica
    >>> m(Integer(2)).Plus(m(Integer(3)))                   # optional - mathematica
    5

We can also compute \(7(2+3)\).

Sage

    sage: m(7).Times(m(2).Plus(m(3)))       # optional - mathematica
    35
    sage: m('7(2+3)')                       # optional - mathematica
    35

Python

    >>> from sage.all import *
    >>> m(Integer(7)).Times(m(Integer(2)).Plus(m(Integer(3))))       # optional - mathematica
    35
    >>> m('7(2+3)')                       # optional - mathematica
    35

### Some typical input

We solve an equation and a system of two equations:

Sage

    sage: # optional - mathematica
    sage: eqn = mathematica('3x + 5 == 14')
    sage: eqn
    5 + 3*x == 14
    sage: eqn.Solve('x')
    {{x -> 3}}
    sage: sys = mathematica('{x^2 - 3y == 3, 2x - y == 1}')
    sage: print(sys)
               2
             {x  - 3 y == 3, 2 x - y == 1}
    sage: sys.Solve('{x, y}')
    {{x -> 0, y -> -1}, {x -> 6, y -> 11}}

Python

    >>> from sage.all import *
    >>> # optional - mathematica
    >>> eqn = mathematica('3x + 5 == 14')
    >>> eqn
    5 + 3*x == 14
    >>> eqn.Solve('x')
    {{x -> 3}}
    >>> sys = mathematica('{x^2 - 3y == 3, 2x - y == 1}')
    >>> print(sys)
               2
             {x  - 3 y == 3, 2 x - y == 1}
    >>> sys.Solve('{x, y}')
    {{x -> 0, y -> -1}, {x -> 6, y -> 11}}

### Assignments and definitions

If you assign the mathematica \(5\) to a variable \(c\) in Sage, this does not affect the \(c\) in Mathematica.

Sage

    sage: c = m(5)                          # optional - mathematica
    sage: print(m('b + c x'))               # optional - mathematica
                 b + c x
    sage: print(m('b') + c*m('x'))          # optional - mathematica
             b + 5 x

Python

    >>> from sage.all import *
    >>> c = m(Integer(5))                          # optional - mathematica
    >>> print(m('b + c x'))               # optional - mathematica
                 b + c x
    >>> print(m('b') + c*m('x'))          # optional - mathematica
             b + 5 x

The Sage interfaces changes Sage lists into Mathematica lists:

Sage

    sage: m = mathematica
    sage: eq1 = m('x^2 - 3y == 3')          # optional - mathematica
    sage: eq2 = m('2x - y == 1')            # optional - mathematica
    sage: v = m([eq1, eq2]); v              # optional - mathematica
    {x^2 - 3*y == 3, 2*x - y == 1}
    sage: v.Solve(['x', 'y'])               # optional - mathematica
    {{x -> 0, y -> -1}, {x -> 6, y -> 11}}

[Python]

    >>> from sage.all import *
    >>> m = mathematica
    >>> eq1 = m('x^2 - 3y == 3')          # optional - mathematica
    >>> eq2 = m('2x - y == 1')            # optional - mathematica
    >>> v = m([eq1, eq2]); v              # optional - mathematica
    {x^2 - 3*y == 3, 2*x - y == 1}
    >>> v.Solve(['x', 'y'])               # optional - mathematica
    {{x -> 0, y -> -1}, {x -> 6, y -> 11}}

### Function definitions

Define mathematica functions by simply sending the definition to the interpreter.

Sage

    sage: m = mathematica
    sage: _ = mathematica('f[p_] = p^2');   # optional - mathematica
    sage: m('f[9]')                         # optional - mathematica
    81

Python

    >>> from sage.all import *
    >>> m = mathematica
    >>> _ = mathematica('f[p_] = p^2');   # optional - mathematica
    >>> m('f[9]')                         # optional - mathematica
    81

### Numerical Calculations

We find the \(x\) such that \(e^x - 3x = 0\).

Sage

    sage: eqn = mathematica('Exp[x] - 3x == 0') # optional - mathematica
    sage: eqn.FindRoot(['x', 2])                # optional - mathematica
    {x -> 1.512134551657842}

Python

    >>> from sage.all import *
    >>> eqn = mathematica('Exp[x] - 3x == 0') # optional - mathematica
    >>> eqn.FindRoot(['x', Integer(2)])                # optional - mathematica
    {x -> 1.512134551657842}

Note that this agrees with what the PARI interpreter gp produces:

Sage

    sage: gp('solve(x=1,2,exp(x)-3*x)')
    1.5121345516578424738967396780720387046

[Python]

    >>> from sage.all import *
    >>> gp('solve(x=1,2,exp(x)-3*x)')
    1.5121345516578424738967396780720387046

Next we find the minimum of a polynomial using the two different ways of accessing Mathematica:

Sage

    sage: mathematica('FindMinimum[x^3 - 6x^2 + 11x - 5, {x,3}]')  # optional - mathematica
    {0.6150998205402516, {x -> 2.5773502699629733}}
    sage: f = mathematica('x^3 - 6x^2 + 11x - 5')  # optional - mathematica
    sage: f.FindMinimum(['x', 3])                  # optional - mathematica
    {0.6150998205402516, {x -> 2.5773502699629733}}

Python

    >>> from sage.all import *
    >>> mathematica('FindMinimum[x^3 - 6x^2 + 11x - 5, {x,3}]')  # optional - mathematica
    {0.6150998205402516, {x -> 2.5773502699629733}}
    >>> f = mathematica('x^3 - 6x^2 + 11x - 5')  # optional - mathematica
    >>> f.FindMinimum(['x', Integer(3)])                  # optional - mathematica
    {0.6150998205402516, {x -> 2.5773502699629733}}

### Polynomial and Integer Factorization

We factor a polynomial of degree 200 over the integers.

Sage

    sage: R.<x> = PolynomialRing(ZZ)
    sage: f = (x**100+17*x+5)*(x**100-5*x+20)
    sage: f
    x^200 + 12*x^101 + 25*x^100 - 85*x^2 + 315*x + 100
    sage: g = mathematica(str(f))            # optional - mathematica
    sage: print(g)                           # optional - mathematica
                               2       100       101    200
             100 + 315 x - 85 x  + 25 x    + 12 x    + x
    sage: g                                  # optional - mathematica
    100 + 315*x - 85*x^2 + 25*x^100 + 12*x^101 + x^200
    sage: print(g.Factor())                  # optional - mathematica
                          100               100
             (20 - 5 x + x   ) (5 + 17 x + x   )

Python

    >>> from sage.all import *
    >>> R = PolynomialRing(ZZ, names=('x',)); (x,) = R._first_ngens(1)
    >>> f = (x**Integer(100)+Integer(17)*x+Integer(5))*(x**Integer(100)-Integer(5)*x+Integer(20))
    >>> f
    x^200 + 12*x^101 + 25*x^100 - 85*x^2 + 315*x + 100
    >>> g = mathematica(str(f))            # optional - mathematica
    >>> print(g)                           # optional - mathematica
                               2       100       101    200
             100 + 315 x - 85 x  + 25 x    + 12 x    + x
    >>> g                                  # optional - mathematica
    100 + 315*x - 85*x^2 + 25*x^100 + 12*x^101 + x^200
    >>> print(g.Factor())                  # optional - mathematica
                          100               100
             (20 - 5 x + x   ) (5 + 17 x + x   )

We can also factor a multivariate polynomial:

Sage

    sage: f = mathematica('x^6 + (-y - 2)*x^5 + (y^3 + 2*y)*x^4 - y^4*x^3')  # optional - mathematica
    sage: print(f.Factor())                  # optional - mathematica
              3                  2    3
             x  (x - y) (-2 x + x  + y )

[Python]

    >>> from sage.all import *
    >>> f = mathematica('x^6 + (-y - 2)*x^5 + (y^3 + 2*y)*x^4 - y^4*x^3')  # optional - mathematica
    >>> print(f.Factor())                  # optional - mathematica
              3                  2    3
             x  (x - y) (-2 x + x  + y )

We factor an integer:

Sage

    sage: # optional - mathematica
    sage: n = mathematica(2434500)
    sage: n.FactorInteger()
    {{2, 2}, {3, 2}, {5, 3}, {541, 1}}
    sage: n = mathematica(2434500)
    sage: F = n.FactorInteger(); F
    {{2, 2}, {3, 2}, {5, 3}, {541, 1}}
    sage: F[1]
    {2, 2}
    sage: F[4]
    {541, 1}

Python

    >>> from sage.all import *
    >>> # optional - mathematica
    >>> n = mathematica(Integer(2434500))
    >>> n.FactorInteger()
    {{2, 2}, {3, 2}, {5, 3}, {541, 1}}
    >>> n = mathematica(Integer(2434500))
    >>> F = n.FactorInteger(); F
    {{2, 2}, {3, 2}, {5, 3}, {541, 1}}
    >>> F[Integer(1)]
    {2, 2}
    >>> F[Integer(4)]
    {541, 1}

Mathematica’s ECM package is no longer available.

## Long Input

The Mathematica interface reads in even very long input (using files) in a robust manner.

[Sage]

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

[Python]

    >>> from sage.all import *
    >>> t = '"%s"'%Integer(10)**Integer(10000)   # ten thousand character string.
    >>> a = mathematica(t)        # optional - mathematica
    >>> a = mathematica.eval(t)   # optional - mathematica

## Loading and saving

Mathematica has an excellent `InputForm` function, which makes saving and loading Mathematica objects possible. The first examples test saving and loading to strings.

Sage

    sage: # optional - mathematica
    sage: x = mathematica(pi/2)
    sage: print(x)
             Pi
             --
             2
    sage: loads(dumps(x)) == x
    True
    sage: n = x.N(50)
    sage: print(n)
                  1.5707963267948966192313216916397514420985846996876
    sage: loads(dumps(n)) == n
    True

Python

    >>> from sage.all import *
    >>> # optional - mathematica
    >>> x = mathematica(pi/Integer(2))
    >>> print(x)
             Pi
             --
             2
    >>> loads(dumps(x)) == x
    True
    >>> n = x.N(Integer(50))
    >>> print(n)
                  1.5707963267948966192313216916397514420985846996876
    >>> loads(dumps(n)) == n
    True

## Complicated translations

The `mobj.sage()` method tries to convert a Mathematica object to a Sage object. In many cases, it will just work. In particular, it should be able to convert expressions entirely consisting of:

- numbers, i.e. integers, floats, complex numbers;

- functions and named constants also present in Sage, where:

  > - Sage knows how to translate the function or constant’s name from Mathematica’s, or

  > - the Sage name for the function or constant is trivially related to Mathematica’s;

- symbolic variables whose names don’t pathologically overlap with objects already defined in Sage.

This method will not work when Mathematica’s output includes:

- strings;

- functions unknown to Sage;

- Mathematica functions with different parameters/parameter order to the Sage equivalent.

If you want to convert more complicated Mathematica expressions, you can instead call `mobj._sage_()` and supply a translation dictionary:

Sage

    sage: m = mathematica('NewFn[x]')       # optional - mathematica
    sage: m._sage_(locals={('NewFn', 1): sin})   # optional - mathematica
    sin(x)

Python

    >>> from sage.all import *
    >>> m = mathematica('NewFn[x]')       # optional - mathematica
    >>> m._sage_(locals={('NewFn', Integer(1)): sin})   # optional - mathematica
    sin(x)

For more details, see the documentation for `._sage_()`.

OTHER Examples:

Sage

    sage: def math_bessel_K(nu, x):
    ....:     return mathematica(nu).BesselK(x).N(20)
    sage: math_bessel_K(2,I)                      # optional - mathematica
    -2.59288617549119697817 + 0.18048997206696202663*I

[Python]

    >>> from sage.all import *
    >>> def math_bessel_K(nu, x):
    ...     return mathematica(nu).BesselK(x).N(Integer(20))
    >>> math_bessel_K(Integer(2),I)                      # optional - mathematica
    -2.59288617549119697817 + 0.18048997206696202663*I

Sage

    sage: slist = [[1, 2], 3., 4 + I]
    sage: mlist = mathematica(slist); mlist     # optional - mathematica
    {{1, 2}, 3., 4 + I}
    sage: slist2 = list(mlist); slist2          # optional - mathematica
    [{1, 2}, 3., 4 + I]
    sage: slist2[0]                             # optional - mathematica
    {1, 2}
    sage: slist2[0].parent()                    # optional - mathematica
    Mathematica
    sage: slist3 = mlist.sage(); slist3         # optional - mathematica
    [[1, 2], 3.00000000000000, I + 4]

Python

    >>> from sage.all import *
    >>> slist = [[Integer(1), Integer(2)], RealNumber('3.'), Integer(4) + I]
    >>> mlist = mathematica(slist); mlist     # optional - mathematica
    {{1, 2}, 3., 4 + I}
    >>> slist2 = list(mlist); slist2          # optional - mathematica
    [{1, 2}, 3., 4 + I]
    >>> slist2[Integer(0)]                             # optional - mathematica
    {1, 2}
    >>> slist2[Integer(0)].parent()                    # optional - mathematica
    Mathematica
    >>> slist3 = mlist.sage(); slist3         # optional - mathematica
    [[1, 2], 3.00000000000000, I + 4]

Sage

    sage: mathematica('10.^80')     # optional - mathematica
    1.*^80
    sage: mathematica('10.^80').sage()  # optional - mathematica
    1.00000000000000e80

[Python]

    >>> from sage.all import *
    >>> mathematica('10.^80')     # optional - mathematica
    1.*^80
    >>> mathematica('10.^80').sage()  # optional - mathematica
    1.00000000000000e80

AUTHORS:

- William Stein (2005): first version

- Doug Cutrell (2006-03-01): Instructions for use under Cygwin/Windows.

- Felix Lawrence (2009-08-21): Added support for importing Mathematica lists and floats with exponents.

class sage.interfaces.mathematica.Mathematica(maxread=None, script_subdirectory=None, logfile=None, server=None, server_tmpdir=None, command=None, verbose_start=False)

  Bases: `ExtraTabCompletion`, `Expect`

  Interface to the Mathematica interpreter.

  chdir(dir)

    Change Mathematica’s current working directory.

    EXAMPLES:

    Sage

        sage: mathematica.chdir('/')          # optional - mathematica
        sage: mathematica('Directory[]')      # optional - mathematica
        "/"

    Python

        >>> from sage.all import *
        >>> mathematica.chdir('/')          # optional - mathematica
        >>> mathematica('Directory[]')      # optional - mathematica
        "/"

  console(readline=True)
  eval(code, strip=True, **kwds)
  get(var, ascii_art=False)

    Get the value of the variable var.

    AUTHORS:

    - William Stein

    - Kiran Kedlaya (2006-02-04): suggested using InputForm

  help(cmd)
  set(var, value)

    Set the variable var to the given value.

class sage.interfaces.mathematica.MathematicaElement(parent, value, is_name=False, name=None)

  Bases: `ExpectElement`

  n(*args, **kwargs)

    Numerical approximation by converting to Sage object first.

    Convert the object into a Sage object and return its numerical approximation. See documentation of the function `sage.misc.functional.n()` for details.

    EXAMPLES:

    Sage

        sage: mathematica('Pi').n(10)    # optional -- mathematica
        3.1
        sage: mathematica('Pi').n()      # optional -- mathematica
        3.14159265358979
        sage: mathematica('Pi').n(digits=10)   # optional -- mathematica
        3.141592654

    Python

        >>> from sage.all import *
        >>> mathematica('Pi').n(Integer(10))    # optional -- mathematica
        3.1
        >>> mathematica('Pi').n()      # optional -- mathematica
        3.14159265358979
        >>> mathematica('Pi').n(digits=Integer(10))   # optional -- mathematica
        3.141592654

  save_image(filename, ImageSize=600)

    Save a mathematica graphics.

    INPUT:

    - `filename` – string; the filename to save as. The extension determines the image file format

    - `ImageSize` – integer; the size of the resulting image

    EXAMPLES:

    Sage

        sage: P = mathematica('Plot[Sin[x],{x,-2Pi,4Pi}]')   # optional - mathematica
        sage: filename = tmp_filename()                      # optional - mathematica
        sage: P.save_image(filename, ImageSize=800)                # optional - mathematica

    Python

        >>> from sage.all import *
        >>> P = mathematica('Plot[Sin[x],{x,-2Pi,4Pi}]')   # optional - mathematica
        >>> filename = tmp_filename()                      # optional - mathematica
        >>> P.save_image(filename, ImageSize=Integer(800))                # optional - mathematica

  show(ImageSize=600)

    Show a mathematica expression immediately.

    This method attempts to display the graphics immediately, without waiting for the currently running code (if any) to return to the command line. Be careful, calling it from within a loop will potentially launch a large number of external viewer programs.

    INPUT:

    - `ImageSize` – integer; the size of the resulting image

    OUTPUT:

    This method does not return anything. Use `save()` if you want to save the figure as an image.

    EXAMPLES:

    Sage

        sage: Q = mathematica('Sin[x Cos[y]]/Sqrt[1-x^2]')   # optional - mathematica
        sage: show(Q)                                        # optional - mathematica
        Sin[x*Cos[y]]/Sqrt[1 - x^2]

    Python

        >>> from sage.all import *
        >>> Q = mathematica('Sin[x Cos[y]]/Sqrt[1-x^2]')   # optional - mathematica
        >>> show(Q)                                        # optional - mathematica
        Sin[x*Cos[y]]/Sqrt[1 - x^2]

    The following example starts a Mathematica frontend to do the rendering (Issue #28819):

    Sage

        sage: P = mathematica('Plot[Sin[x],{x,-2Pi,4Pi}]')   # optional - mathematica
        sage: show(P)                                        # optional - mathematica mathematicafrontend
        sage: P.show(ImageSize=800)                          # optional - mathematica mathematicafrontend

    [Python]

        >>> from sage.all import *
        >>> P = mathematica('Plot[Sin[x],{x,-2Pi,4Pi}]')   # optional - mathematica
        >>> show(P)                                        # optional - mathematica mathematicafrontend
        >>> P.show(ImageSize=Integer(800))                          # optional - mathematica mathematicafrontend

  str()

class sage.interfaces.mathematica.MathematicaFunction(parent, name)

  Bases: `ExpectFunction`

class sage.interfaces.mathematica.MathematicaFunctionElement(obj, name)

  Bases: `FunctionElement`

sage.interfaces.mathematica.clean_output(s)

sage.interfaces.mathematica.mathematica_console(readline=True)

sage.interfaces.mathematica.parse_moutput_from_json(page_data, verbose=False)

  Return the list of outputs found in the json (with key `'moutput'`).

  INPUT:

  - `page_data` – json obtained from Wolfram Alpha

  - `verbose` – boolean (default: `False`)

  OUTPUT: list of unicode strings

  EXAMPLES:

  Sage

      sage: from sage.interfaces.mathematica import request_wolfram_alpha
      sage: from sage.interfaces.mathematica import parse_moutput_from_json
      sage: page_data = request_wolfram_alpha('integrate Sin[x]') # optional internet
      sage: parse_moutput_from_json(page_data)                    # optional internet
      ['-Cos[x]']

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.mathematica import request_wolfram_alpha
      >>> from sage.interfaces.mathematica import parse_moutput_from_json
      >>> page_data = request_wolfram_alpha('integrate Sin[x]') # optional internet
      >>> parse_moutput_from_json(page_data)                    # optional internet
      ['-Cos[x]']

  Sage

      sage: page_data = request_wolfram_alpha('Sin[x]')           # optional internet
      sage: L = parse_moutput_from_json(page_data)                # optional internet
      sage: sorted(L)                                             # optional internet
      ['-Cos[x]', '{x == 0}', '{x == Pi C[1], Element[C[1], Integers]}']

  [Python]

      >>> from sage.all import *
      >>> page_data = request_wolfram_alpha('Sin[x]')           # optional internet
      >>> L = parse_moutput_from_json(page_data)                # optional internet
      >>> sorted(L)                                             # optional internet
      ['-Cos[x]', '{x == 0}', '{x == Pi C[1], Element[C[1], Integers]}']

sage.interfaces.mathematica.reduce_load(X)

sage.interfaces.mathematica.request_wolfram_alpha(input, verbose=False)

  Request Wolfram Alpha website.

  INPUT:

  - `input` – string

  - `verbose` – boolean (default: `False`)

  OUTPUT: json

  EXAMPLES:

  Sage

      sage: from sage.interfaces.mathematica import request_wolfram_alpha
      sage: page_data = request_wolfram_alpha('integrate Sin[x]')      # optional internet
      sage: [str(a) for a in sorted(page_data.keys())]                 # optional internet
      ['queryresult']
      sage: [str(a) for a in sorted(page_data['queryresult'].keys())]  # optional internet
      ['datatypes',
       'encryptedEvaluatedExpression',
       'encryptedParsedExpression',
       'error',
       'host',
       'id',
       'inputstring',
       'numpods',
       'parsetimedout',
       'parsetiming',
       'pods',
       'recalculate',
       'related',
       'server',
       'sponsorCategories',
       'success',
       'timedout',
       'timedoutpods',
       'timing',
       'version']

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.mathematica import request_wolfram_alpha
      >>> page_data = request_wolfram_alpha('integrate Sin[x]')      # optional internet
      >>> [str(a) for a in sorted(page_data.keys())]                 # optional internet
      ['queryresult']
      >>> [str(a) for a in sorted(page_data['queryresult'].keys())]  # optional internet
      ['datatypes',
       'encryptedEvaluatedExpression',
       'encryptedParsedExpression',
       'error',
       'host',
       'id',
       'inputstring',
       'numpods',
       'parsetimedout',
       'parsetiming',
       'pods',
       'recalculate',
       'related',
       'server',
       'sponsorCategories',
       'success',
       'timedout',
       'timedoutpods',
       'timing',
       'version']

sage.interfaces.mathematica.symbolic_expression_from_mathematica_string(mexpr)

  Translate a mathematica string into a symbolic expression.

  INPUT:

  - `mexpr` – string

  OUTPUT: symbolic expression

  EXAMPLES:

  Sage

      sage: from sage.interfaces.mathematica import symbolic_expression_from_mathematica_string
      sage: symbolic_expression_from_mathematica_string('-Cos[x]')
      -cos(x)

  Python

      >>> from sage.all import *
      >>> from sage.interfaces.mathematica import symbolic_expression_from_mathematica_string
      >>> symbolic_expression_from_mathematica_string('-Cos[x]')
      -cos(x)