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.
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.
sudo apt-get install build-essential m4 gfortran libssl-dev dpkg-dev
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:
sudo apt-add-repository -y ppa:aims/sagemath sudo apt-get update sudo apt-get install sagemath-upstream-binary
You can also install all the optional packages:
sudo apt-get install texlive evince xpdf xdvi tk8.5-dev
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
ln -s [path to the sage launcher] /usr/local/bin/sage
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
circle((0,0), 1, rgbcolor=(1,1,0))
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.
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
P.<x> = PolynomialRing(RR)
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:
t = x^2 - 25
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.
m = matrix(QQ, [[1, 2], [3, 4]])
And to look for its inverse:
Sage has some graphing capacities too. Earlier we draw a circle with
circle((0,0), 1, rgbcolor=(1,1,0))
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:
x = var('x')
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
x, y = var('x,y')
And then use the plot3d() function:
plot3d(x^2 + y^2, (x,-2,2), (y,-2,2))
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
x, a, b, c = var('x a b c')
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:
solve([x - 3*a == 6, x + b*c == 10], x, b)
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
x = var('x')
And then use either the diff() or the integrate() function.
diff(x^3 + 4*x + 16, x)
for differentiating and
integral(x^3 + 4*x + 16, x)
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.
Our latest tutorials delivered straight to your inbox