# I/O — input and output¶

How are expressions interpreted? How can I represent expressions nicely?

Fractions are represented in simplified form:

In :
4/2

Out:
2

If we enforce enumerators and denominators as python integers, the result is zero.

In :
int(1)/int(3)

Out:
0

If we don't enforce python integers, sage integers will be used. Sage integers give rational results for division:

In :
1/3

Out:
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:

In :
preparse("1/3")

Out:
'Integer(1)/Integer(3)'

Constants are replaced by sagemath. Integers are replaced with ‘Integer’ instances by sagemath. So, ‘int’ is the integer class of python…

In :
int

Out:
<type '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.

In :
Integer

Out:
<type 'sage.rings.integer.Integer'>

This is relevant if you use ‘range’.
‘range’ is a common python function to generate a sequence of numbers.
‘range’ returns ‘int’ instances…

In :
type(range(10))

Out:
<type 'int'>

… whereas sagemath's ‘srange’ returns sagemath integers:

In :
type(srange(10))

Out:
<type 'sage.rings.integer.Integer'>

Instead of calling ‘int()’ to instantiate python integers, you can also append a ‘r’:

In :
print(preparse('2r'))
print(type(2r))

2
<type 'int'>


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:

In :
preparse('R.<x> = PolynomialRing(QQ)')

Out:
"R = PolynomialRing(QQ, names=('x',)); (x,) = R._first_ngens(1)"

‘x’ is a predefined, symbolic variable (besides ‘oo’, ‘e’, ‘i’ and many classes):

In :
x

Out:
x

It represents a symbolic expression:

In :
type(x)

Out:
<type 'sage.symbolic.expression.Expression'>

We can also define our own symbolic variable. Unlike ‘x’, ‘y’ is not predefined:

In :
var('y', domain='complex')

Out:
y

Here we see how symbolic variables can be combined with other expressions and those expressions can be simplified or expanded according to mathematical laws:

In :
expr = (4 + y^2) * (4 + 3 + y^2)
print(expr.simplify())
print(expr.expand())

(y^2 + 7)*(y^2 + 4)
y^4 + 11*y^2 + 28


Given some expression, we can also call a symbolic expression with keyword arguments like a=3 to substitute symbolic variables with values:

In :
var('x a b c')
expr = (a * x^2 + b * x + c)(a=3, b=5/99, c=pi)
expr

Out:
pi + 3*x^2 + 5/99*x

‘show()’ enforces a more beautiful rendering in the browser (MathJax library)

In :
show(expr)


The default display mode is ‘plain’ and gives plain text representations:

In :
%display plain
1/2

Out:
1/2

With display mode ‘typeset’ the MathJax rendering engine is used for all representations that follow:

In :
%display typeset
1/2

Out:

We can ask for a list of display mode by asking the documentation:

In :
%display?

Docstring:
A magic command to switch between simple display and ASCII art
display.

* "args" -- string.  See
"sage.misc.display_hook.DisplayHookBase.set_display()" for
allowed values. If the mode is "ascii_art", it can optionally be
followed by a width.

How to use: if you want activate the ASCII art mod:

sage: from sage.repl.interpreter import get_test_shell
sage: shell = get_test_shell()
sage: shell.run_cell('%display ascii_art')

That means you don't have to use "ascii_art()" to get an ASCII art
output:

sage: shell.run_cell("i = var('i')")
sage: shell.run_cell('sum(i^2*x^i, i, 0, 10)')
10       9       8       7       6       5       4      3      2
100*x   + 81*x  + 64*x  + 49*x  + 36*x  + 25*x  + 16*x  + 9*x  + 4*x  + x

Then when you want return in 'textual mode':

sage: shell.run_cell('%display text plain')
sage: shell.run_cell('%display plain')        # shortcut for "text plain"
sage: shell.run_cell('sum(i^2*x^i, i, 0, 10)')
100*x^10 + 81*x^9 + 64*x^8 + 49*x^7 + 36*x^6 + 25*x^5 + 16*x^4 + 9*x^3 + 4*x^2 + x

Sometime you could have to use a special output width and you could
specify it:

sage: shell.run_cell('%display ascii_art')
sage: shell.run_cell('StandardTableaux(4).list()')
[
[                                                                  1  4
[                 1  3  4    1  2  4    1  2  3    1  3    1  2    2
[   1  2  3  4,   2      ,   3      ,   4      ,   2  4,   3  4,   3   ,

1 ]
1  3    1  2    2 ]
2       3       3 ]
4   ,   4   ,   4 ]
sage: shell.run_cell('%display ascii_art 50')
sage: shell.run_cell('StandardTableaux(4).list()')
[
[
[                 1  3  4    1  2  4    1  2  3
[   1  2  3  4,   2      ,   3      ,   4      ,

1 ]
1  4    1  3    1  2    2 ]
1  3    1  2    2       2       3       3 ]
2  4,   3  4,   3   ,   4   ,   4   ,   4 ]

As yet another option, typeset mode. This is used in the emacs
interface:

sage: shell.run_cell('%display text latex')
sage: shell.run_cell('1/2')
newcommand{Bold}{mathbf{#1}}frac{1}{2}

Switch back:

sage: shell.run_cell('%display default')

Switch graphics to default to vector or raster graphics file
formats:

sage: shell.run_cell('%display graphics vector')
File:      ~/Notebooks/</home/sage/sage/local/lib/python2.7/site-packages/decorator.pyc:decorator-gen-126>


‘ascii_art’ is probably an amusing one, but maybe helpful if you run sagemath in a terminal:

In :
%display ascii_art
x^5 + 4/5*x + 25^x

Out:
  x    5   4*x
25  + x  + ---
5 

Let's switch back to typeset display mode:

In :
%display typeset


We consider a matrix representation example.

‘print()’ and ‘show()’ give different results, because

• ‘print()’ desires the python style plaintext representation
• ‘show()’ desires the MathJax rendered typeset representation
In :
print(matrix.hilbert(3))
show(matrix.hilbert(3))

[  1 1/2 1/3]
[1/2 1/3 1/4]
[1/3 1/4 1/5]


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.

In :
pretty_print_default(True)


sagemath can also print arbitrary LaTeχ expressions:

In :
LatexExpr("\int x \cos(x) dx")

Out:

You can also make entire cells to be LaTeχ input:

In :
%%latex
Kleiner Gauß: $\frac{n(n-1)}2$

Kleiner Gauß: $\frac{n(n-1)}2$

Given any expression, call ‘latex()’ to retrieve its LaTeχ representation:

In :
print(latex(5*x^2 + 1/4*x))

5 \, x^{2} + \frac{1}{4} \, x


This is possible, because every sagemath object implements the method ‘_latex_’:

In :
pi._latex_()

Out:

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

In :
view(3*x+5/6*x)

Error: PDFLaTeX does not seem to be installed.  Download it from
ctan.org and try again.
Latex error