LTK SageMath (2023)

Last updated on 14/04/2023


SageMath basics

Introductory material

Building blocks


Refer also to LW0200MATHEMATICS.

Invocation: Alternatively, there's the online version Useful:



In Python, everything is an object so there isn’t any difference between types and classes. One can get the class of the object el by type(el). Behavior is defined through methods.

Sage specifics about classes

Compared to Python, Sage has particular ways to handle objects: For more details, see the Sage Developer’s Guide.



When mixing different type of numbers, Sage does its best: if possible, it computes the result in the smaller set of numbers containing all the arguments.

Sage is a *computer algebra system*: unless specified otherwise, it does *symbolic computation* (as opposed to *numerical computation*).

Also when computing a division, Sage keeps the result exact and simplifies it only when possible. Check this: compute `10/2` and `7/2`.

Exact versus numerical approximation

Some mathematical expressions return ‘exact’ values, rather than numerical approximations.

To get a numerical approximation, use 'numerical_approx', in full, or use either of the abbreviations the function N or the method n (the function N is the same as n).

Length of a number

Dynamic typing


Symbolic variables and expressions

By default, the only symbolic variable predefined in Sage is x. To use more variables, use 'var', e.g. var('y,z').

You may need to specify assumptions. E.g. One can define *symbolic functions* using symbolic variables. Symbolic functions are useful to represent mathematical functions in Sage. For instance: Use of methods: More:

Solve function

The solve function is the most general tool in Sage to solve equations, systems of equations, or inequalities. It provides only exact i.e. symbolic solutions (as opposed to numerical solutions). Of course Sage is not always able to compute exact solutions. It has other functions dedicated to solving equations numerically. More details e.g. here.

SageMath Number Theory



From 'log?': The log function also works in finite fields as long as the argument lies in the multiplicative group generated by the base:


Group is a set and an operation that combines any two elements of the set to produce a third element of the set, in such a way that the operation is associative, an identity element exists and every element has an inverse.


Number rings

Ring is a set with 2 binary operations (+ and *) such that R is abelian with respect to +, * is associative, and + and * are distributive.

Rings of integers - referred to as ZZ, Z/nZ, ...

Defining a finite ring of integers: Working with elements:

Polynomial rings

You can simply use polynomials via symbolic expressions, without specifying the coefficient ring. However for more advanced algebra, this approach lacks some algebraic structure. To begin with, we would like to consider polynomials with coefficients in a predefined ring, for instance ℚ or a finite field.

Polynomial rings QQ

Working with QQ: any expression constructed from `X` and rational constants with operations `+` and `*` is an element of `R`. E.g. P = X^2+3/2*X+1.

Methods include

Integer polynomial rings, ZZ or Z/nZ or Zn or Zn

Other polynomial rings RR, GF

Changing the base ring

Changing the base ring may be useful, for instance when studying the irreducibility or factorization of a polynomial. For example: Polynomial M is irreducible over ℚ. Now let us look at it over ℝ.

Approach: SageMath: From documentation 'Polynomials Release 9.8' - illustration of 'change_ring' method.

Polynomials with multiple variables

Polynomial with several variables are constructed and manipulated in a similar way.

Other rings

Other rings include finite fields, p-adic integers, the ring of algebraic numbers, and matrix rings.


Factorisation - often using integers or polynomials

The 'Factorization' class provides a structure for holding quite general lists of objects with integer multiplicities. These may hold the results of an arithmetic or algebraic factorization, where the objects may be primes or irreducible polynomials and the multiplicities are the (non-zero) exponents in the factorization. Functions include: Factoring is a useful technique for solving polynomial equations. By breaking a polynomial down into smaller factors, we can often simplify the equation and find the solutions more easily. To factor polynomials: e.g. define the sets of polynomials with rational coefficients and real coefficients: You can then factor polynomials:


Relative primes and Euler phi (totient) and theorem


Mod(x,p): Mod(33,9) = 6

Euler's phi-function

phi(n) is the amount of numbers that are relatively prime to n.

Euler's Theorem

If gcd(x,n)=1 then xphi(n)=1 (mod n).

Relative primes

How to get list of relative primes for a given number? See

There are multiple ways to list integers coprime to a given integer in Sage.

One way is to build the ring of integers modulo m, then list the multiplicative group of that ring. Note that this returns a list of Python integers: To get Sage integers one would need Check:

Another way is to use the method coprime integers, which expects another argument to say how far you want to list integers that are coprime to m. To get them up to m - 1: To get them further: These are returned as Sage integers:

Elliptic curves

Oblivious Transfer