When it comes to solving Maths equation in Linux, we have previously discussed GeoGebra, a software for geometry and 2D graphs. However, if you are looking for a more powerful tools, you should give Sage a try. We’ve all heard of Matlab or Magma, but Sage offers an open source alternative to those two, and is, to my opinion, simpler to use.
Installation
Sage is always under heavy development, which means new enhancements are added very frequently. To install it from source:
First, you will need to satisfy a few dependencies.
You can then download the sources from the official page. Extract them and from a terminal, launch the script
Note: You will need around 3GB of free space for Sage to work properly. Be sure to have them before launching the compilation (which will take some time).
If you prefer the binary version, there is also a PPA available for Ubuntu:
You can also install all the optional packages:
Usage
To launch Sage, you can do it with the command
from the downloaded directory.
If you want to launch sage from anywhere, you can create a symbolic link with
or even make an alias in your ~/.bashrc file like I did:
Sage is a non-graphical tool by default. At launch, you should get something like this
You then have the choice: continue with the console, or go for the browser-based GUI by typing
Both are pretty much equivalent in terms of functionality. The GUI is more intuitive, especially for plots and graphs, but the terminal works pretty fine as well. With the GUI, try the command
You will see a yellow circle appearing under it.
If you do the same in the console, your default picture viewer will open and show the same circle.
Note: Both the GUI and the console have an auto-completion feature.
Now that you have chosen sage’s interface, we can start talking about the functions themselves. Sage is absolutely huge, and I mean it. There is just no way that we can discuss every aspect of it in one article. Instead, I will show you a few common applications that you may want to try.
1. Calculator
Yes, Sage can do some basic maths. Addition, multiplication, etc. Since it is written mainly in Python, you can also use the python syntax to do stuff like integer division.
2. Polynomial Roots
It is pretty common to be given a polynomial and to ask for its roots. With Sage, you first create a polynomial ring with
where x will be the generator, and RR stands for the real numbers. If you want your ring to be over the rational numbers for example, replace RR by QQ. You then copy your polynomial:
And you then ask for the roots
Note: this will return the roots in the base ring of the polynomial and their multiplicities. If you want the roots to be from another base ring, put it as an argument for the roots() function.
3. Matrix Inverse
If you are a bit into trading or economy, you may have seen matrices at some point in your life. Generally, there is a whole process for finding the inverses of these matrices. Well, Sage does it for you very simply. Create your matrix over the base ring that you want.
And to look for its inverse:
4. Graphs
Sage has some graphing capacities too. Earlier we draw a circle with
I’m pretty sure that you understood that the first parameter was the circle’s center, the second was the radius, and the last was the color of the circle in RGB standard. Well, you can also plot basic functions by first declaring a variable:
And then use the plot() function:
The first argument is the function equation, the second is the span. So here I was asking for the cubic function shown from -10 to 10 on the x-plot.
Note: If you have Jmol installed, you can even do 3D graphs by declaring two variables
And then use the plot3d() function:
5. Solving Equations
Sometimes we want a solution for an equation, but the calculator only gives us an approximation. It is even worse if there are some other variables in that equation and that we want the solution be expressed in function of them. For years in high school, I was stuck doing that manually. How foolish! Sage does it perfectly. Declare all your variables
And then just use the solve() function, with the equation(s) as the first argument, and the variable(s) that you want to be expressed after:
I wish that I knew that back in high school…
6. Differentiation and Integration
Here’s the ultimate application when you are really lazy. You can use this program to differentiate and integrate functions. As usual, declare your variables
And then use either the diff() or the integrate() function.
for differentiating and
for integrating.
Conclusion
Sage is really an incredible tool. The syntax is quick to learn and stays fairly standard. The drawback is that the documentation is really something you should read in order to get a grasp at what you can really do. The applications are so numerous that it is easy to get lost. You can do differential equations along with cryptography. And it gets so specific that right now a function to find the splitting field of polynomials over finite field is under development (yes I have no idea what it means either).
Do you have any other example to propose? A question about Sage? Please let us know in the comments.
Image credit: Maths by Big Stock Photo.
Adrien is a young but passionate Linux aficionado. Command line, encryption, obscure distributions… you name it, he tried it. Always improving his system, he encountered multiple tricks and hacks and is ready to share them. Best things in the world? Math, computers and peanut butter!
Our latest tutorials delivered straight to your inbox