Python | Mathematics

SageMath: Doing Math in Python

One of the best plug and play math libraries for Python

Pau Blasco i Roca
Dev Genius
Published in
3 min readFeb 8, 2022

--

You have probably heard about Wolfram Mathematica, GNU Octave and Mathworks Matlab. These three are — apart from R Programming and Maple — the triad of mathematic tools for scientists and engineers from different fields. But I bet you hadn’t heard about SageMath yet.

What is SageMath?

SageMath is a free Open-Source mathematics library for Python. It’s based on other Python packages you already know, like NumPy, SciPy, Matplotlib, Maxima, and many others.

It is available for Windows, macOS and Linux distributions. You can download it here, from its official page.

How do you launch it?

From both Windows and macOS, you can just click the direct access to the notebook (which will run on Jupyter). You can also use the command line, but I don’t recommend it. From Linux, you can use both the terminal and the direct access to launch SageMath.

This will open a Python-based locally hosted notebook in your browser, which will look like this:

Initial screen after launching SageMath notebook server

Then we can either create a new file or open an existing one. Note that Jupyter Notebook files have the extension “.ipynb”. Once we do that, we’ll find ourselves with the classical Jupyter interface.

Snapshot from one of my Notebooks, a 3D reconstruction of an object.

Getting started with SageMath

You should think about SageMath like it's Python with extra stuff. So don’t be scared to try out things you think they might work. Here’s a quick example on how to create a matrix:

l=[[1,2,3],[4,5,6],[7,8,9]] #declaring a 2D array
M = matrix(l) #converting that array to a matrix
M #printing M, equivalent to print(M)
#output:
[1 2 3]
[4 5 6]
[7 8 9]

A more elegant way to represent M graphically would be show(M), which uses LaTeX-like markdown text.

As you can see, the function matrix(), which belongs to the SageMath library, accepts regular Python arrays as arguments. Another example would be the following:

v1=vector([1,-2,4])
v2=vector([-1,0,1])
v3=v2.cross_product(v1)
v3
#output:
(2, 5, 2)

Again, for a more “beautiful” output, we can use the show() function.

What can you do with Sage?

SageMath works like a charm when solving algebra problems, for example, like finding kernels or eigenspaces of matrices. But is also a very good tool for data analysis, 2D and 3D plotting, and symbolic expression manipulation.

Feel free to check the documentation, where you will find practically everything you need.

The one and only issue with SageMath

SageMath is not as popular as other applications, as I stated at the beginning of the article. The concern with this is that there aren’t many video tutorials or forum answers related to this library, which might slow down the learning process. But for an experienced programmer or engineer, this should not be an issue.

But there’s more

There is much more to SageMath than I can cover in a single article. I might write in-depth articles about certain groups of functions in SageMath if this one works out. Feel free to comment down below what you think about this Python library!

--

--

Computational Mathematics | Research Intern at CVC Barcelona | Ex Amazon BI