Tutorial on SageMath

Introduction

SageMath is mainly a computer algebra system based on Python, which is very popular in discrete mathematics. SageMath is a free open-source software. As a "consequence", not all of the documentations are written for generic computer users (see this meme). The main aim of this article is an attempt of making up the gaps.

Getting Start

You can have a quick try of SageMath online via SageCell. It also provides a way to embed Sage computations into any webpage like this.

You can also use CoCalc in the cloud (online). When using it, it is recommended to start with creating a Jupyter Notebook with kernel the lastest SageMath.

If you do hope to download and install SageMath, it will be complicated. Here are some useful links providing some hints: Windows, Binaries, Sources. After installation, it is strongly suggested to use the shortcut "SageMath n.m Notebook" which links to a Jupyter Notebook.

There are two official introductory documentations:

They are good so far. The readers are suggested to try SageMath as an advanced calculator by themselves.

Python syntax

Of course, we are only not using SageMath a calculator. Actually, Sage Math is based on Python, and so is its syntax (grammar). So the scientific way is to tell something about Python. See the official documentation. As Python becomes popular recent years, there are many good sources of learning it.

To proceed this article, we will assume the readers know the following

Here are two (mathematical) examples.

Example: Collatz conjecture

Consider the following operation on an arbitrary positive integer: \[n\longmapsto \begin{cases} n/2, & \text{if $n$ is even}, \\ 3n+1, & \text{if $n$ is odd}. \end{cases}\] The Collatz conjecture is: This process will eventually reach the cycle $1\mapsto 4\mapsto 2 \mapsto \cdots$, regardless of which positive integer is chosen initially.

Example: Fibonacci sequence

Fibonacci sequence is the sequence defined by \[F_0=0,F_1=1, \text{ and } F_n=F_{n-1}+F_{n-2} \text{ when $n\geq 2$}.\] We can compute it using the following code.

Mathematical structures

Here are some pages frequently used:

Here I list some basis example I used a lot.

Example: divided difference

Let us consider the divided difference operator \[\partial_{a,b}f=\frac{f-f|_{x_a\leftrightarrow x_b}}{x_a-x_b}.\] We take the convention that \(\partial_i=\partial_{i,i+1}\).

Example: Vandermonde determinant

Recall the Vandermonde identity \[\left| \begin{matrix} 1 & 1 & \cdots & 1\\ x_1 & x_2 & \cdots & x_n\\ \vdots & \vdots & \ddots & \vdots \\ x_1^{n-1} & x_2^{n-1} & \cdots & x_n^{n-1}\\ \end{matrix}\right| =\prod_{1\leq i<j\leq n}(x_i-x_j)\]

In general, most of the commands/methods you need can be found by searching on the internet (very similar to the situation of LaTeX). Most of them are from the the official documentations.

Appendix: Advanced Examples

In the last section, I give a number of advanced examples requiring more knowledge of Python.

Example: Schur operators

For a partition \(\lambda\), we define \(u_i\) the operator \[u_i\lambda = \begin{cases} \lambda\cup\text{a box in the $i$-th row},& \text{if it is a partition},\\ 0, & \text{otherwise}. \end{cases}\] This operator extends to the free vector space of partitions linearly. The Schur operators satisfy the relation of Plactic monoid.

Example: Dunkl elements

For fixed \(n\), the Chevalley formula gives \[x_k\mathfrak{S}_w = -\sum_{\begin{subarray}{c}i<k\\\ell(wt_{ik})=\ell(w)+1\end{subarray}}\mathfrak{S}_{wt_{ik}} +\sum_{\begin{subarray}{c}i>k\\\ell(wt_{ik})=\ell(w)+1\end{subarray}}\mathfrak{S}_{wt_{ik}}. \] where \(\mathfrak{S}_w\) is the Schubert polynomial, and \(t_{ab}\) is the transposition of $a,b$. Dunkl element is the operator formally defined as above.


Example: Multi-sets

A multi-set with alphabet \(L\) of cardinality \(k\) is an \(S_k\)-orbit of \(L^k\). When \(L\) is ordered, we can uniquely write such multi-set by \(\{\!\{i_1\leq \cdots\leq i_k\}\!\}\) with all \(i_j\in L\).

Example: Standard Young Tableaux

Let \(\lambda\) be a partition. A standard Young tableau (SYT) of \(\lambda\) is a filling of its Young diagram with \(\{1,\cdots,n\}\) for \(n\) the number of boxes in \(\lambda\) such that (1) all numbers are used (exactly once), (2) each row is increasing, and (3) each column is increasing. We can similarly define (SYT) for skew shapes (difference of two partitions).


home