SageMath in Research

Numbers

sage: a = RR(sqrt(2)); a
1.41421356237310
sage: b = sqrt(RealField(100)(2)); b     
1.4142135623730950488016887242
sage: (a-b).parent()
Real Field with 53 bits of precision
sage: b.parent()
Real Field with 100 bits of precision
sage: c = RealField(100)(a); c
1.4142135623730951454746218587
sage: b-c
-9.6672933134529652200516048587e-17
There are different types of numbers available. There are three major groups for floating point arithmetic:
  • Python: float, complex, decimal
  • SageMath specific: RDF, CDF, RQDF, CC, RR, RIF, CIF
  • included Systems: pari, maxima
Most important are the SageMath specific types. They use specific libraries and have more functionality. Important are RR and CC, because their behavior is independent of the underlying system and architecture using the MPFR library. RDF and CDF use the GSL library and are very fast and compatible with the SageMath framework.
The example on the left shows how to construct different types of floating point numbers and interactions between them.

Algebra

sage: M = IntegerModRing(7)
sage: M(2) + M(8)
3
sage: M.list()
[0, 1, 2, 3, 4, 5, 6]

sage: A.<a,b,c> = AbelianGroup([2,2,3]); A
Multiplicative Abelian Group isomorphic to C2 x C2 x C3
sage: A.order()
12
sage: A.list()
[1, c, c^2, b, b*c, b*c^2, a, a*c, a*c^2,
                        a*b, a*b*c, a*b*c^2]
sage: c^5*b*a^4*c
b

SageMath is built on an object oriented programming language. It uses this feature to describe categories of mathematical objects. A good example are algebraic objects like groups, rings and fields.
On the left side you can see some examples on how to construct and use them. The first one picks two integers out of the ring of integers modulo 7. The list() method lists all elements of that ring. Similarly, the second example constructs an abelian group and assigns its generators to the letters a, b and c.

Combinatorics

sage: X = species.SingletonSpecies()
sage: Y = species.BinaryTreeSpecies()

sage: L = CombinatorialSpecies()
sage: L.define(X+X*Y*Y+Y*L)

sage: L.generating_series().coefficients(10)

[0, 1, 1, 3, 8, 23, 70, 222, 726, 2431]

sage: L.structures([1,2,3]).cardinality()
18
The demonstration on the left hand side shows how SageMath is able to work with combinatorial objects from the theory of Combinatorial Species.

Embedding SageMath into LaTeX

\section{SageTex Examples}

This is a small calculation:
The sum of $1+2+\sqrt{3} = \sage{1+2+sqrt(3)}$.

Here you can see a $sin()$-Function:
\sageplot{plot(sin(x), x, 0, 2*pi)}
     
It is possible to call SageMath commands from inside a LaTeX document. The SageTex package provides special LaTeX commands that translate SageMath code into a Python file. Then this file is evaluated by SageMath and the results of each calculation are written back into the LaTeX file. This even works for graphics, source-code and saved objects.
The example on the left shows how you can embed a formula like $$1+2+\sqrt{3}$$ and a plot in a LaTeX document.
This package is part of the SageMath distribution in the /examples/latex_embed sub-directory together with the documentation. You can also obtain it online: CTAN: SageTex Package
Tour Quickstart · Tour Graphics · Benchmarks