Interface to Mathics3¶
Mathics3 is an open source interpreter for the Wolfram Language. From the introduction of its reference manual:
Note
Mathics3 — is a general-purpose computer algebra system (CAS). It is meant to be a free, light-weight alternative to Mathematica®. It is free both as in “free beer” and as in “freedom”. There are various online mirrors running Mathics3 but it is also possible to run Mathics3 locally. A list of mirrors can be found at the Mathics3 homepage, http://mathics.github.io.
The programming language of Mathics3 is meant to resemble Wolfram’s famous Mathematica® as much as possible. However, Mathics3 is in no way affiliated or supported by Wolfram. Mathics3 will probably never have the power to compete with Mathematica® in industrial applications; yet, it might be an interesting alternative for educational purposes.
The Mathics3 interface will only work if the optional Sage package Mathics3 is installed. The interface lets you send certain Sage objects to Mathics3, run Mathics3 functions, import certain Mathics3 expressions to Sage, or any combination of the above.
To send a Sage object sobj to Mathics3, call mathics3(sobj).
This exports the Sage object to Mathics3 and returns a new Sage object
wrapping the Mathics3 expression/variable, so that you can use the
Mathics3 variable from within Sage. You can then call Mathics3
functions on the new object; for example:
sage: from sage.interfaces.mathics3 import mathics3
sage: mobj = mathics3(x^2-1); mobj
-1 + x ^ 2
sage: mobj.Factor()
(-1 + x) (1 + x)
>>> from sage.all import *
>>> from sage.interfaces.mathics3 import mathics3
>>> mobj = mathics3(x**Integer(2)-Integer(1)); mobj
-1 + x ^ 2
>>> mobj.Factor()
(-1 + x) (1 + x)
from sage.interfaces.mathics3 import mathics3 mobj = mathics3(x^2-1); mobj mobj.Factor()
In the above example the factorization is done using Mathics3’s
Factor[] function.
To see Mathics3’s output you can simply print the Mathics3 wrapper
object. However if you want to import Mathics3’s output back to Sage,
call the Mathics3 wrapper object’s sage() method. This method returns
a native Sage object:
sage: mobj = mathics3(x^2-1)
sage: mobj2 = mobj.Factor(); mobj2
(-1 + x) (1 + x)
sage: mobj2.parent()
Mathics3
sage: sobj = mobj2.sage(); sobj
(x + 1)*(x - 1)
sage: sobj.parent()
Symbolic Ring
>>> from sage.all import *
>>> mobj = mathics3(x**Integer(2)-Integer(1))
>>> mobj2 = mobj.Factor(); mobj2
(-1 + x) (1 + x)
>>> mobj2.parent()
Mathics3
>>> sobj = mobj2.sage(); sobj
(x + 1)*(x - 1)
>>> sobj.parent()
Symbolic Ring
mobj = mathics3(x^2-1) mobj2 = mobj.Factor(); mobj2 mobj2.parent() sobj = mobj2.sage(); sobj sobj.parent()
If you want to run a Mathics3 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
mathics3(expr). This string will be evaluated as if you had typed it
into Mathics3:
sage: mathics3('Factor[x^2-1]')
(-1 + x) (1 + x)
sage: mathics3('Range[3]')
{1, 2, 3}
>>> from sage.all import *
>>> mathics3('Factor[x^2-1]')
(-1 + x) (1 + x)
>>> mathics3('Range[3]')
{1, 2, 3}
mathics3('Factor[x^2-1]')
mathics3('Range[3]')
If you want work with the internal Mathics3 expression, then you can call
mathics3.eval(expr), which returns an instance of
mathics3.core.expression.Expression. If you want the result to
be a string formatted like Mathics3’s InputForm, call repr(mobj) on
the wrapper object mobj. If you want a string formatted in Sage style,
call mobj._sage_repr():
sage: mathics3.eval('x^2 - 1')
'-1 + x ^ 2'
sage: repr(mathics3('Range[3]'))
'{1, 2, 3}'
sage: mathics3('Range[3]')._sage_repr()
'[1, 2, 3]'
>>> from sage.all import *
>>> mathics3.eval('x^2 - 1')
'-1 + x ^ 2'
>>> repr(mathics3('Range[3]'))
'{1, 2, 3}'
>>> mathics3('Range[3]')._sage_repr()
'[1, 2, 3]'
mathics3.eval('x^2 - 1')
repr(mathics3('Range[3]'))
mathics3('Range[3]')._sage_repr()
Finally, if you just want to use a Mathics3 command line from within
Sage, the function mathics3_console() dumps you into an interactive
command-line Mathics3 session.
Tutorial¶
We follow some of the tutorial from http://library.wolfram.com/conferences/devconf99/withoff/Basic1.html/.
Syntax¶
Now make 1 and add it to itself. The result is a Mathics3 object.
sage: m = mathics3
sage: a = m(1) + m(1); a
2
sage: a.parent()
Mathics3
sage: m('1+1')
2
sage: m(3)**m(50)
717897987691852588770249
>>> from sage.all import *
>>> m = mathics3
>>> a = m(Integer(1)) + m(Integer(1)); a
2
>>> a.parent()
Mathics3
>>> m('1+1')
2
>>> m(Integer(3))**m(Integer(50))
717897987691852588770249
m = mathics3
a = m(1) + m(1); a
a.parent()
m('1+1')
m(3)**m(50)
The following is equivalent to Plus[2, 3] in
Mathics3:
sage: m = mathics3
sage: m(2).Plus(m(3))
5
>>> from sage.all import *
>>> m = mathics3
>>> m(Integer(2)).Plus(m(Integer(3)))
5
m = mathics3 m(2).Plus(m(3))
We can also compute \(7(2+3)\).
sage: m(7).Times(m(2).Plus(m(3)))
35
sage: m('7(2+3)')
35
>>> from sage.all import *
>>> m(Integer(7)).Times(m(Integer(2)).Plus(m(Integer(3))))
35
>>> m('7(2+3)')
35
m(7).Times(m(2).Plus(m(3)))
m('7(2+3)')
Some typical input¶
We solve an equation and a system of two equations:
sage: eqn = mathics3('3x + 5 == 14')
sage: eqn
5 + 3 x == 14
sage: eqn.Solve('x')
{{x -> 3}}
sage: sys = mathics3('{x^2 - 3y == 3, 2x - y == 1}')
sage: print(sys)
{x ^ 2 - 3 y == 3, 2 x - y == 1}
sage: sys.Solve('{x, y}')
{{x -> 0, y -> -1}, {x -> 6, y -> 11}}
>>> from sage.all import *
>>> eqn = mathics3('3x + 5 == 14')
>>> eqn
5 + 3 x == 14
>>> eqn.Solve('x')
{{x -> 3}}
>>> sys = mathics3('{x^2 - 3y == 3, 2x - y == 1}')
>>> print(sys)
{x ^ 2 - 3 y == 3, 2 x - y == 1}
>>> sys.Solve('{x, y}')
{{x -> 0, y -> -1}, {x -> 6, y -> 11}}
eqn = mathics3('3x + 5 == 14')
eqn
eqn.Solve('x')
sys = mathics3('{x^2 - 3y == 3, 2x - y == 1}')
print(sys)
sys.Solve('{x, y}')
Assignments and definitions¶
If you assign the mathics3 \(5\) to a variable \(c\) in Sage, this does not affect the \(c\) in Mathics3.
sage: c = m(5)
sage: print(m('b + c x'))
b + c x
sage: print(m('b') + c*m('x'))
b + 5 x
>>> from sage.all import *
>>> c = m(Integer(5))
>>> print(m('b + c x'))
b + c x
>>> print(m('b') + c*m('x'))
b + 5 x
c = m(5)
print(m('b + c x'))
print(m('b') + c*m('x'))
The Sage interfaces changes Sage lists into Mathics3 lists:
sage: m = mathics3
sage: eq1 = m('x^2 - 3y == 3')
sage: eq2 = m('2x - y == 1')
sage: v = m([eq1, eq2]); v
{x ^ 2 - 3 y == 3, 2 x - y == 1}
sage: v.Solve(['x', 'y'])
{{x -> 0, y -> -1}, {x -> 6, y -> 11}}
>>> from sage.all import *
>>> m = mathics3
>>> eq1 = m('x^2 - 3y == 3')
>>> eq2 = m('2x - y == 1')
>>> v = m([eq1, eq2]); v
{x ^ 2 - 3 y == 3, 2 x - y == 1}
>>> v.Solve(['x', 'y'])
{{x -> 0, y -> -1}, {x -> 6, y -> 11}}
m = mathics3
eq1 = m('x^2 - 3y == 3')
eq2 = m('2x - y == 1')
v = m([eq1, eq2]); v
v.Solve(['x', 'y'])
Sage Numeric Constants¶
All of Sage’s numeric constants can be used in a mathics3() expression. For example:
sage: mathics3(pi/2)
Pi / 2
sage: mathics3(golden_ratio).N()
1.61803
>>> from sage.all import *
>>> mathics3(pi/Integer(2))
Pi / 2
>>> mathics3(golden_ratio).N()
1.61803
mathics3(pi/2) mathics3(golden_ratio).N()
Similarly, many Mathics3’s Numeric Constants translate into Sage’s numeric constants:
sage: [mathics3(c).sage() for c in ('Catalan', 'Glaisher', 'GoldenRatio', 'EulerGamma', 'Khinchin', 'Pi')]
[catalan, glaisher, golden_ratio, euler_gamma, khinchin, pi]
>>> from sage.all import *
>>> [mathics3(c).sage() for c in ('Catalan', 'Glaisher', 'GoldenRatio', 'EulerGamma', 'Khinchin', 'Pi')]
[catalan, glaisher, golden_ratio, euler_gamma, khinchin, pi]
[mathics3(c).sage() for c in ('Catalan', 'Glaisher', 'GoldenRatio', 'EulerGamma', 'Khinchin', 'Pi')]
Function definitions¶
Define mathics3 functions by simply sending the definition to the interpreter.
sage: m = mathics3
sage: _ = mathics3('f[p_] = p^2');
sage: m('f[9]')
81
>>> from sage.all import *
>>> m = mathics3
>>> _ = mathics3('f[p_] = p^2');
>>> m('f[9]')
81
m = mathics3
_ = mathics3('f[p_] = p^2');
m('f[9]')
Numerical Calculations¶
We find the \(x\) such that \(e^x - 3x = 0\).
sage: eqn = mathics3('Exp[x] - 3x == 0')
sage: eqn.FindRoot(['x', 2])
{x -> 1.51213}
>>> from sage.all import *
>>> eqn = mathics3('Exp[x] - 3x == 0')
>>> eqn.FindRoot(['x', Integer(2)])
{x -> 1.51213}
eqn = mathics3('Exp[x] - 3x == 0')
eqn.FindRoot(['x', 2])
Note that this agrees with what the PARI interpreter gp produces:
sage: gp('solve(x=1,2,exp(x)-3*x)')
1.5121345516578424738967396780720387046
>>> from sage.all import *
>>> gp('solve(x=1,2,exp(x)-3*x)')
1.5121345516578424738967396780720387046
gp('solve(x=1,2,exp(x)-3*x)')
Next we find the minimum of a polynomial using the two different ways of accessing Mathics3:
sage: mathics3('FindMinimum[x^3 - 6x^2 + 11x - 5, {x,3}]') # not tested (since not supported, so far)
{0.6151, {x -> 2.57735}}
sage: f = mathics3('x^3 - 6x^2 + 11x - 5')
sage: f.FindMinimum(['x', 3]) # not tested (since not supported, so far)
{0.6151, {x -> 2.57735}}
>>> from sage.all import *
>>> mathics3('FindMinimum[x^3 - 6x^2 + 11x - 5, {x,3}]') # not tested (since not supported, so far)
{0.6151, {x -> 2.57735}}
>>> f = mathics3('x^3 - 6x^2 + 11x - 5')
>>> f.FindMinimum(['x', Integer(3)]) # not tested (since not supported, so far)
{0.6151, {x -> 2.57735}}
mathics3('FindMinimum[x^3 - 6x^2 + 11x - 5, {x,3}]') # not tested (since not supported, so far)
f = mathics3('x^3 - 6x^2 + 11x - 5')
f.FindMinimum(['x', 3]) # not tested (since not supported, so far)
Polynomial and Integer Factorization¶
We factor a polynomial of degree 200 over the integers.
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 = mathics3(str(f))
sage: print(g)
100 + 315 x - 85 x ^ 2 + 25 x ^ 100 + 12 x ^ 101 + x ^ 200
sage: g
100 + 315 x - 85 x ^ 2 + 25 x ^ 100 + 12 x ^ 101 + x ^ 200
sage: print(g.Factor())
(5 + 17 x + x ^ 100) (20 - 5 x + x ^ 100)
>>> 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 = mathics3(str(f))
>>> print(g)
100 + 315 x - 85 x ^ 2 + 25 x ^ 100 + 12 x ^ 101 + x ^ 200
>>> g
100 + 315 x - 85 x ^ 2 + 25 x ^ 100 + 12 x ^ 101 + x ^ 200
>>> print(g.Factor())
(5 + 17 x + x ^ 100) (20 - 5 x + x ^ 100)
R.<x> = PolynomialRing(ZZ) f = (x**100+17*x+5)*(x**100-5*x+20) f g = mathics3(str(f)) print(g) g print(g.Factor())
We can also factor a multivariate polynomial:
sage: f = mathics3('x^6 + (-y - 2)*x^5 + (y^3 + 2*y)*x^4 - y^4*x^3')
sage: print(f.Factor())
x ^ 3 (x - y) (-2 x + x ^ 2 + y ^ 3)
>>> from sage.all import *
>>> f = mathics3('x^6 + (-y - 2)*x^5 + (y^3 + 2*y)*x^4 - y^4*x^3')
>>> print(f.Factor())
x ^ 3 (x - y) (-2 x + x ^ 2 + y ^ 3)
f = mathics3('x^6 + (-y - 2)*x^5 + (y^3 + 2*y)*x^4 - y^4*x^3')
print(f.Factor())
We factor an integer:
sage: n = mathics3(2434500)
sage: n.FactorInteger()
{{2, 2}, {3, 2}, {5, 3}, {541, 1}}
sage: n = mathics3(2434500)
sage: F = n.FactorInteger(); F
{{2, 2}, {3, 2}, {5, 3}, {541, 1}}
sage: F[1]
{2, 2}
sage: F[4]
{541, 1}
>>> from sage.all import *
>>> n = mathics3(Integer(2434500))
>>> n.FactorInteger()
{{2, 2}, {3, 2}, {5, 3}, {541, 1}}
>>> n = mathics3(Integer(2434500))
>>> F = n.FactorInteger(); F
{{2, 2}, {3, 2}, {5, 3}, {541, 1}}
>>> F[Integer(1)]
{2, 2}
>>> F[Integer(4)]
{541, 1}
n = mathics3(2434500) n.FactorInteger() n = mathics3(2434500) F = n.FactorInteger(); F F[1] F[4]
Long Input¶
The Mathics3 interface reads in even very long input (using files) in a robust manner.
sage: t = '"%s"'%10^10000 # ten thousand character string.
sage: a = mathics3(t)
sage: a = mathics3.eval(t)
>>> from sage.all import *
>>> t = '"%s"'%Integer(10)**Integer(10000) # ten thousand character string.
>>> a = mathics3(t)
>>> a = mathics3.eval(t)
t = '"%s"'%10^10000 # ten thousand character string. a = mathics3(t) a = mathics3.eval(t)
Loading and saving¶
Mathics3 has an excellent InputForm function,
which makes saving and loading Mathics3 objects possible. The
first examples test saving and loading to strings.
sage: x = mathics3('Pi/2') # Or 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
>>> from sage.all import *
>>> x = mathics3('Pi/2') # Or pi/2
>>> print(x)
Pi / 2
>>> loads(dumps(x)) == x
True
>>> n = x.N(Integer(50))
>>> print(n)
1.5707963267948966192313216916397514420985846996876
>>> loads(dumps(n)) == n
True
x = mathics3('Pi/2') # Or pi/2
print(x)
loads(dumps(x)) == x
n = x.N(50)
print(n)
loads(dumps(n)) == n
Complicated translations¶
The mobj.sage() method tries to convert a Mathics3 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 Mathics3’s, or
the Sage name for the function or constant is trivially related to Mathics3’s;
symbolic variables whose names don’t pathologically overlap with objects already defined in Sage.
This method will not work when Mathics3’s output includes:
strings;
functions unknown to Sage;
Mathics3 functions with different parameters/parameter order to the Sage equivalent.
If you want to convert more complicated Mathics3 expressions, you can
instead call mobj._sage_() and supply a translation dictionary:
sage: x = var('x')
sage: m = mathics3('NewFn[x]')
sage: m._sage_(locals={'NewFn': sin, 'x':x})
sin(x)
>>> from sage.all import *
>>> x = var('x')
>>> m = mathics3('NewFn[x]')
>>> m._sage_(locals={'NewFn': sin, 'x':x})
sin(x)
x = var('x')
m = mathics3('NewFn[x]')
m._sage_(locals={'NewFn': sin, 'x':x})
For more details, see the documentation for ._sage_().
OTHER Examples:
sage: def math_bessel_K(nu, x):
....: return mathics3(nu).BesselK(x).N(20)
sage: math_bessel_K(2,I)
-2.5928861754911969782 + 0.18048997206696202663 I
>>> from sage.all import *
>>> def math_bessel_K(nu, x):
... return mathics3(nu).BesselK(x).N(Integer(20))
>>> math_bessel_K(Integer(2),I)
-2.5928861754911969782 + 0.18048997206696202663 I
def math_bessel_K(nu, x):
return mathics3(nu).BesselK(x).N(20)
math_bessel_K(2,I)
sage: slist = [[1, 2], 3., 4 + I]
sage: mlist = mathics3(slist); mlist
{{1, 2}, 3., 4 + I}
sage: slist2 = list(mlist); slist2
[{1, 2}, 3., 4 + I]
sage: slist2[0]
{1, 2}
sage: slist2[0].parent()
Mathics3
sage: slist3 = mlist.sage(); slist3
[[1, 2], 3.00000000000000, 4.00000000000000 + 1.00000000000000*I]
>>> from sage.all import *
>>> slist = [[Integer(1), Integer(2)], RealNumber('3.'), Integer(4) + I]
>>> mlist = mathics3(slist); mlist
{{1, 2}, 3., 4 + I}
>>> slist2 = list(mlist); slist2
[{1, 2}, 3., 4 + I]
>>> slist2[Integer(0)]
{1, 2}
>>> slist2[Integer(0)].parent()
Mathics3
>>> slist3 = mlist.sage(); slist3
[[1, 2], 3.00000000000000, 4.00000000000000 + 1.00000000000000*I]
slist = [[1, 2], 3., 4 + I] mlist = mathics3(slist); mlist slist2 = list(mlist); slist2 slist2[0] slist2[0].parent() slist3 = mlist.sage(); slist3
sage: mathics3('10.^80')
1.×10^80
sage: mathics3('10.^80').sage()
1.00000000000000e80
>>> from sage.all import *
>>> mathics3('10.^80')
1.×10^80
>>> mathics3('10.^80').sage()
1.00000000000000e80
mathics3('10.^80')
mathics3('10.^80').sage()
AUTHORS:
Sebastian Oehms (2021): first version from a copy of the Mathematica interface (see upstream Issue #31778).
Rashad Alsharpini (2026): port to Mathics3 10.0.0 (see SAGE pull request 41885).
Thanks to Rocky Bernstein and Juan Mauricio Matera for their support. For further acknowledgments see this list.
- class sage.interfaces.mathics3.Mathics3(maxread=None, logfile=None, init_list_length=1024, seed=None)[source]¶
Bases:
InterfaceInterface to the Mathics3 interpreter.
Implemented according to the Mathematica interface but avoiding Pexpect functionality.
EXAMPLES:
sage: t = mathics3('Tan[I + 0.5]') sage: t.parent() Mathics3 sage: ts = t.sage() sage: ts.parent() Complex Field with 53 bits of precision sage: t == mathics3(ts) True sage: mtan = mathics3.Tan sage: mt = mtan(I+1/2) sage: mt == t True sage: u = mathics3(I+1/2) sage: u.Tan() == mt True
>>> from sage.all import * >>> t = mathics3('Tan[I + 0.5]') >>> t.parent() Mathics3 >>> ts = t.sage() >>> ts.parent() Complex Field with 53 bits of precision >>> t == mathics3(ts) True >>> mtan = mathics3.Tan >>> mt = mtan(I+Integer(1)/Integer(2)) >>> mt == t True >>> u = mathics3(I+Integer(1)/Integer(2)) >>> u.Tan() == mt True
t = mathics3('Tan[I + 0.5]') t.parent() ts = t.sage() ts.parent() t == mathics3(ts) mtan = mathics3.Tan mt = mtan(I+1/2) mt == t u = mathics3(I+1/2) u.Tan() == mtMore examples can be found in the module header.
- chdir(dir)[source]¶
Change Mathics3’s current working directory.
EXAMPLES:
sage: mathics3.chdir('/') sage: mathics3('Directory[]') /
>>> from sage.all import * >>> mathics3.chdir('/') >>> mathics3('Directory[]') /
mathics3.chdir('/') mathics3('Directory[]')
- console()[source]¶
Spawn a new Mathics3 command-line session.
EXAMPLES:
sage: mathics3.console() # not tested Mathics3 10.0.0 Running on linux CPython 3.12.3 (main, Mar 23 2026, 19:04:32) [GCC 13.3.0] using SymPy 1.14.0, mpmath 1.3.0, numpy 2.4.3, cython 3.2.4, scipy 1.17.1, skimage Not installed Copyright (C) 2011-2026 The Mathics3 Team. This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the documentation for the full license. Quit by evaluating Quit[] or by pressing CONTROL-D. In[1]:= Sin[0.5] Out[1]= 0.479426 Goodbye! sage:
>>> from sage.all import * >>> mathics3.console() # not tested Mathics3 10.0.0 Running on linux CPython 3.12.3 (main, Mar 23 2026, 19:04:32) [GCC 13.3.0] using SymPy 1.14.0, mpmath 1.3.0, numpy 2.4.3, cython 3.2.4, scipy 1.17.1, skimage Not installed Copyright (C) 2011-2026 The Mathics3 Team. This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the documentation for the full license. Quit by evaluating Quit[] or by pressing CONTROL-D. In[1]:= Sin[0.5] Out[1]= 0.479426 Goodbye! sage:
mathics3.console() # not tested
- eval(code, *args, **kwds)[source]¶
Evaluates a command inside the Mathics3 interpreter and returns the output in printable form.
EXAMPLES:
sage: mathics3.eval('1+1') '2'
>>> from sage.all import * >>> mathics3.eval('1+1') '2'
mathics3.eval('1+1')
- get(var)[source]¶
Get the value of the variable var.
EXAMPLES:
sage: mathics3.set('u', '2*x +E') sage: mathics3.get('u') '2 x + E'
>>> from sage.all import * >>> mathics3.set('u', '2*x +E') >>> mathics3.get('u') '2 x + E'
mathics3.set('u', '2*x +E') mathics3.get('u')
- help(cmd, long=False)[source]¶
Return the Mathics3 documentation of the given command.
EXAMPLES:
sage: mathics3.help('Sin') ... sage: print(_) Sin[z] returns the sine of z. Attributes[Sin] = {Listable, NumericFunction, Protected} sage: print(mathics3.help('Sin', long=True)) Sin[z] returns the sine of z. Attributes[Sin] = {Listable, NumericFunction, Protected} sage: print(mathics3.Factorial.__doc__) Factorial[n] n! computes the factorial of n. Attributes[Factorial] = {Listable, NumericFunction, Protected, ReadProtected} sage: u = mathics3('Pi') sage: print(u.Cos.__doc__) Cos[z] returns the cosine of z. Attributes[Cos] = {Listable, NumericFunction, Protected}
>>> from sage.all import * >>> mathics3.help('Sin') ... >>> print(_) <BLANKLINE> Sin[z] returns the sine of z. <BLANKLINE> <BLANKLINE> Attributes[Sin] = {Listable, NumericFunction, Protected} <BLANKLINE> >>> print(mathics3.help('Sin', long=True)) <BLANKLINE> Sin[z] returns the sine of z. <BLANKLINE> <BLANKLINE> Attributes[Sin] = {Listable, NumericFunction, Protected} <BLANKLINE> >>> print(mathics3.Factorial.__doc__) <BLANKLINE> Factorial[n] n! computes the factorial of n. <BLANKLINE> <BLANKLINE> Attributes[Factorial] = {Listable, NumericFunction, Protected, ReadProtected} >>> u = mathics3('Pi') >>> print(u.Cos.__doc__) <BLANKLINE> Cos[z] returns the cosine of z. <BLANKLINE> <BLANKLINE> Attributes[Cos] = {Listable, NumericFunction, Protected} <BLANKLINE>
mathics3.help('Sin') print(_) print(mathics3.help('Sin', long=True)) print(mathics3.Factorial.__doc__) u = mathics3('Pi') print(u.Cos.__doc__)
- set(var, value)[source]¶
Set the variable var to the given value.
EXAMPLES:
sage: mathics3.set('u', '2*x +E') sage: bool(mathics3('u').sage() == 2*x+e) True
>>> from sage.all import * >>> mathics3.set('u', '2*x +E') >>> bool(mathics3('u').sage() == Integer(2)*x+e) True
mathics3.set('u', '2*x +E') bool(mathics3('u').sage() == 2*x+e)
- class sage.interfaces.mathics3.Mathics3Element(parent, value, is_name=False, name=None)[source]¶
Bases:
ExtraTabCompletion,InterfaceElement,Mathics3ElementElement class of the Mathics3 interface.
Its instances are usually constructed via the instance call of its parent. It wraps the Mathics3 library for this object. In a session Mathics3 methods can be obtained using tab completion.
EXAMPLES:
sage: me=mathics3(e); me.sage() e sage: me=mathics3('E'); me.sage() e sage: type(me) <class 'sage.interfaces.mathics3.Mathics3Element'> sage: P = me.parent(); P Mathics3 sage: type(P) <class 'sage.interfaces.mathics3.Mathics3'>
>>> from sage.all import * >>> me=mathics3(e); me.sage() e >>> me=mathics3('E'); me.sage() e >>> type(me) <class 'sage.interfaces.mathics3.Mathics3Element'> >>> P = me.parent(); P Mathics3 >>> type(P) <class 'sage.interfaces.mathics3.Mathics3'>
me=mathics3(e); me.sage() me=mathics3('E'); me.sage() type(me) P = me.parent(); P type(P)Access to the Mathics3 expression objects:
sage: res = me._mathics3_result sage: type(res) <class 'mathics.core.evaluation.Result'> sage: expr = res.last_eval; expr <Symbol: System`E> sage: type(expr) <class 'mathics.core.symbols.Symbol'>
>>> from sage.all import * >>> res = me._mathics3_result >>> type(res) <class 'mathics.core.evaluation.Result'> >>> expr = res.last_eval; expr <Symbol: System`E> >>> type(expr) <class 'mathics.core.symbols.Symbol'>
res = me._mathics3_result type(res) expr = res.last_eval; expr type(expr)
Applying Mathics3 methods:
sage: me.to_sympy() E sage: me.get_name() 'System`E' sage: me.is_inexact() False
>>> from sage.all import * >>> me.to_sympy() E >>> me.get_name() 'System`E' >>> me.is_inexact() False
me.to_sympy() me.get_name() me.is_inexact()
Conversion to Sage:
sage: bool(me.sage() == e) True
>>> from sage.all import * >>> bool(me.sage() == e) True
bool(me.sage() == e)
- n(*args, **kwargs)[source]¶
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: mathics3('Pi').n(10) # optional -- mathics3 3.1 sage: mathics3('Pi').n() # optional -- mathics3 3.14159265358979 sage: mathics3('Pi').n(digits=10) # optional -- mathics3 3.141592654
>>> from sage.all import * >>> mathics3('Pi').n(Integer(10)) # optional -- mathics3 3.1 >>> mathics3('Pi').n() # optional -- mathics3 3.14159265358979 >>> mathics3('Pi').n(digits=Integer(10)) # optional -- mathics3 3.141592654
mathics3('Pi').n(10) # optional -- mathics3 mathics3('Pi').n() # optional -- mathics3 mathics3('Pi').n(digits=10) # optional -- mathics3
- save_image(filename, ImageSize=600)[source]¶
Save a mathics3 graphics.
INPUT:
filename– string; the filename to save as. The extension determines the image file formatImageSize– integer; the size of the resulting image
EXAMPLES:
sage: P = mathics3('Plot[Sin[x],{x,-2Pi,4Pi}]') sage: filename = tmp_filename(ext=".svg") sage: P.save_image(filename, ImageSize=800)
>>> from sage.all import * >>> P = mathics3('Plot[Sin[x],{x,-2Pi,4Pi}]') >>> filename = tmp_filename(ext=".svg") >>> P.save_image(filename, ImageSize=Integer(800))
P = mathics3('Plot[Sin[x],{x,-2Pi,4Pi}]') filename = tmp_filename(ext=".svg") P.save_image(filename, ImageSize=800)
- show(ImageSize=600)[source]¶
Show a mathics3 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: Q = mathics3('Sin[x Cos[y]]/Sqrt[1-x^2]') sage: show(Q) Sin[x Cos[y]] / Sqrt[1 - x ^ 2] sage: P = mathics3('Plot[Sin[x],{x,-2Pi,4Pi}]') sage: show(P) sage: P.show(ImageSize=800)
>>> from sage.all import * >>> Q = mathics3('Sin[x Cos[y]]/Sqrt[1-x^2]') >>> show(Q) Sin[x Cos[y]] / Sqrt[1 - x ^ 2] >>> P = mathics3('Plot[Sin[x],{x,-2Pi,4Pi}]') >>> show(P) >>> P.show(ImageSize=Integer(800))
Q = mathics3('Sin[x Cos[y]]/Sqrt[1-x^2]') show(Q) P = mathics3('Plot[Sin[x],{x,-2Pi,4Pi}]') show(P) P.show(ImageSize=800)
- sage.interfaces.mathics3.mathics3_console()[source]¶
Spawn a new Mathics3 command-line session.
EXAMPLES:
sage: mathics3_console() # not tested Mathics3 10.0.0 Running on linux CPython 3.14.3 (main, Mar 30 2026, 06:42:16) [GCC 13.3.0] using SymPy 1.13.3, mpmath 1.3.0, numpy 2.4.4, cython 3.2.4, scipy 1.17.1, skimage 0.26.0 Copyright (C) 2011-2026 The Mathics3 Team. This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the documentation for the full license. Quit by evaluating Quit[] or by pressing CONTROL-D. In[1]:= Sin[0.5] Out[1]= 0.479426 Goodbye!
>>> from sage.all import * >>> mathics3_console() # not tested Mathics3 10.0.0 Running on linux CPython 3.14.3 (main, Mar 30 2026, 06:42:16) [GCC 13.3.0] using SymPy 1.13.3, mpmath 1.3.0, numpy 2.4.4, cython 3.2.4, scipy 1.17.1, skimage 0.26.0 Copyright (C) 2011-2026 The Mathics3 Team. This program comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the documentation for the full license. Quit by evaluating Quit[] or by pressing CONTROL-D. In[1]:= Sin[0.5] Out[1]= 0.479426 Goodbye!
mathics3_console() # not tested
- sage.interfaces.mathics3.reduce_load(X)[source]¶
Used in unpickling a Mathics3 element.
This function is just the
__call__method of the interface instance.EXAMPLES:
sage: sage.interfaces.mathics3.reduce_load('Denominator[a / b]') # optional -- mathics3 b
>>> from sage.all import * >>> sage.interfaces.mathics3.reduce_load('Denominator[a / b]') # optional -- mathics3 b
sage.interfaces.mathics3.reduce_load('Denominator[a / b]') # optional -- mathics3