SageMath basics



1. Introduction
2. Install
3. Launch
4. Basics
5. Tips


1. Introduction

SageMath is an open-source mathematical software, similar to Wolfram Mathematica. SageMath has an interface to GAP by default.

2. Install

For Mac OSX, a simple way is to download a binary release (e.g. SageMath-10.2_arm64.dmg).

For Linux, it is complicated to install the latest version. Since the package managers can install only SageMath v9, one needs to build it from source code in GitHub. See the instructions at Reinstall Ubuntu.

3. Launch

Type sage in Terminal. If you want to launch jupyter notebook, type

sage -n jupyter

Create an ipynb file, and choose SageMath kernel.

4. Basics

Coding and Python

Much of Sage depends on Python. See official document.

Base ring and fields

Sage has four rings and fields by default; ZZ for integers, QQ for rational numbers, RR for real numbers and CC for complex numbers. Speaking precisely, they are

ZZ == IntegerRing()
QQ == RationalField()
RR == RealField()
CC == ComplexField()

which can be checked as

type(CC)
CC.parent()

These classes have various functions; see References. One basic usage is to convert data type, e.g. from character to Integers, or Python integer to Sage integer with infinite precision.

ZZ('1234')==1234

Note that ZZ does not round a real number to an integer, and it results in an error. Imaginary unit is I. The precisions can be specified as

RR == RealField(prec=53)
RDF == RealDoubleField()

where RDF is faster for numerical calculations.

Python class types and Sage parents

There is no one-to-one correspondence between Python class types (like int, float) and Sage parents (like rings, fields). We cannot compare elements in an abstract algebra by using 'is', rather we should use '==' which forces elements to be numerics. The following codes are all True,

int(15) is int(15)
int(-15) is not int(-15)
Integer(-15) is not Integer(-15)
-15 == -15
Integer(-15) == Integer(-15)

Object type

In Sage, we should create a variable with an appropriate object type. For example, this code works

p = Partitions(5).list()[:3]
> [5, [4,1], [3,2]]
p[2].size()
> 5
type(p[2])
> <class 'sage.combinat.partition.Partitions_n_with_category.element_class'>

but the following code doesn't work, because .

[4,1].size()
> AttributeError: 'list' object has no attribute 'size'

Sage permutation groups

Mostly overlap with GAP functions.

GAP library

In order to use the functions of library such as GAP, add the library name, like

S2S2 = libgap.DirectProduct(SymmetricGroup(2), SymmetricGroup(2))
orbits = libgap.OrbitsDomain(S2S2,SymmetricGroup(4))

It is said that libgap.(functions) are faster than gap.(functions), though they do the same computation.

Output and history

The contents of a variable can be displayed by print(), show().

show(orbits)

The function view() gives LaTeX output as a PDF.
You can reuse the last output by _, the second last by __, the thir last by ___, and the n-th last by _n. Note that the definition of a function produces no output.

5. Tips

When you run a SageMath code, the execution at Command Prompt is much faster than opening a notebook; see here. Similarly, GAP functions are much faster than using Python functions.