How are expressions interpreted? How can I represent expressions nicely?
Fractions are represented in simplified form:
4/2
If we enforce enumerators and denominators as python integers, the result is zero.
int(1)/int(3)
If we don't enforce python integers, sage integers will be used. Sage integers give rational results for division:
1/3
But how does it work? In python ‘1/3’ is the division of python integers. How is ‘1/3’ is different in sagemath? ‘preparse’ reveals the secret:
preparse("1/3")
Constants are replaced by sagemath. Integers are replaced with ‘Integer’ instances by sagemath. So, ‘int’ is the integer class of python…
int
… whereas ‘Integer’ is the integer class of sagemath. ‘Integer’ instances are - in general - more sophisticated than python's integers. ‘Integer’ instances are embebbed in the algebraic infrastructure of sagemath.
Integer
This is relevant if you use ‘range’.
‘range’ is a common python function to generate a sequence of numbers.
‘range’ returns ‘int’ instances…
type(range(10)[0])
… whereas sagemath's ‘srange’ returns sagemath integers:
type(srange(10)[0])
Instead of calling ‘int()’ to instantiate python integers, you can also append a ‘r’:
print(preparse('2r'))
print(type(2r))
Now we can also inspect more complex expressions from before. The syntax is invalid in python, but we can inspect how sagemath rewrites expressions for python:
preparse('R.<x> = PolynomialRing(QQ)')
‘x’ is a predefined, symbolic variable (besides ‘oo’, ‘e’, ‘i’ and many classes):
x
It represents a symbolic expression:
type(x)
We can also define our own symbolic variable. Unlike ‘x’, ‘y’ is not predefined:
var('y', domain='complex')
Here we see how symbolic variables can be combined with other expressions and those expressions can be simplified or expanded according to mathematical laws:
expr = (4 + y^2) * (4 + 3 + y^2)
print(expr.simplify())
print(expr.expand())
Given some expression, we can also call a symbolic expression with keyword arguments like a=3
to substitute symbolic variables with values:
var('x a b c')
expr = (a * x^2 + b * x + c)(a=3, b=5/99, c=pi)
expr
‘show()’ enforces a more beautiful rendering in the browser (MathJax library)
show(expr)
The default display mode is ‘plain’ and gives plain text representations:
%display plain
1/2
With display mode ‘typeset’ the MathJax rendering engine is used for all representations that follow:
%display typeset
1/2
We can ask for a list of display mode by asking the documentation:
%display?
‘ascii_art’ is probably an amusing one, but maybe helpful if you run sagemath in a terminal:
%display ascii_art
x^5 + 4/5*x + 25^x
Let's switch back to typeset display mode:
%display typeset
We consider a matrix representation example.
‘print()’ and ‘show()’ give different results, because
print(matrix.hilbert(3))
show(matrix.hilbert(3))
If you want to enable ‘%display typeset‘ globally (i.e. in a startup configuration file), don't use ‘%display typeset’ but the ‘pretty_print_default’ function.
pretty_print_default(True)
sagemath can also print arbitrary LaTeχ expressions:
LatexExpr("\int x \cos(x) dx")
You can also make entire cells to be LaTeχ input:
%%latex
Kleiner Gauß: $\frac{n(n-1)}2$
Given any expression, call ‘latex()’ to retrieve its LaTeχ representation:
print(latex(5*x^2 + 1/4*x))
This is possible, because every sagemath object implements the method ‘_latex_’:
pi._latex_()
‘view’ is another representation function. It calls LaTeχ itself to render an expression. LaTeχ is not installed on this particular docker image and hence an error is displayed.
view(3*x+5/6*x)