10 Best Math Libraries for Python

Many times, when you write programs you need to use special functions that others have used before you. When this happens, open source comes to the rescue and gives you a library that covers that need. Python calls theirs modules, to use modules you need to import them.Modules for mathematics are especially useful when you have the theory ready but need to use standard math for your particular problem. The Mathematics module in the Python standard library has many features. It is useful to check if you can solve your problem easily with these functions. If you need to know what functions exist you need to go through the list. However, first realize that the module implements all the C standard functions.

The simplest use of Python for math is as a calculator. To do this, start Python on the terminal and use the print function.

The simple math is available without even activating the math module but beyond addition, subtraction, division and multiplication you need to import the math module. To make the code short, import as ‘m’. Now you put m and a dot in front of any functions you use. This works the same for all modules in Python. If you want to use complex numbers, use the cmath module.

For functions beyond that, below are some libraries specialized for certain needs.

The NumPy libraries handles the mathematical functions for arrays. Creating arrays of any type is possible and optimizing in memory is also supported. The N-dimensional array is fully covered. Functions that the library handles includes iteration, Fourier Transfom, linear algebra and financial functions. This library also implements a C-API so you can use the speed of C without translating your entire project.

SciPy is a collection of science related software, with mathematical tasks at the center. If you need to calculate anything, this is a good place to start. The collection includes integration, optimization and sparse eigenvalues.

Scikit-image is a great resource for manipulating and analysing images. The library has features for detecting lines, edges and features. It also has restoration features, for when you have images with defects on them. There are also many analysis tools available.

Scikit-learn is useful for getting machine learning code together. It contains modules for classification, regression, clustering and more. The web page is full of useful examples so you can easily get started.

Pandas is your goto resource for big data sets to do your data science on. Pandas supports data analysis and modeling and does it with simple and clear code. Many functions are translatable from R, so you can prototype with Pandas.

Statsmodels covers your needs for statistical models. This library handles many similar things like Panda but can also import Sata files and handle time series analysis. There is a sandbox included where you can experiment with different statistical models. That particular code is not tested yet but maybe it is close enough for you to finish the job.

Matplotlib: For plotting your graphs, includes animated plots.
The earlier libraries are great for the mathematics but they have deliberately stayed away from plotting. Instead they let libraries like matplotlib handle these
This has made matplotlib extensive and it also has many supporting software that covers mapping, plotting and electronic circuit design.

Gnuplot.py is an interface package to the popular gnuplot program. It has an object oriented design so you can add your own extensions.

Patsy describes statistical models in all its forms. It also has many functions that are common in R but with small differences, like how to denote exponentiation. Patsy will build matrices using formulas, very similar to the way it is done in S and R.

Sympy: When you want to print your mathematical formulas you use this library. It also has the capability to evaluate expressions. It is very useful for creating formulas in your LaTeX documents. You can even run Sympy live in your browser to test it out.

Now that you have learned what projects to use for mathematics you will soon be short on processing power. To remedy that situation parallel execution is the most common solution. There are several Python libraries for this purpose.

The mpi4py library provides bindings to the standard Message Passing Interface. You need to download a standard parallel library like mpich or openmpi. Both are available in the standard repositories.

The other library is parallel python or pp. Parallel Python creates a server and many clients that take jobs from your server. This project does not implement a standard, instead you use the server and client from this same package on all your machines. This is simpler in some ways but it requires more when your project becomes big and you need other people to lend you processing power.

These libraries are all good in their own right but make sure to pick the correct one for your needs.
The choice is not irreversible but will require quite a lot of work later in a project. Your source code will need to be changed to use a new library and new faults will occur so choose wisely.

If you want to do your calculations interactively, install and use Ipython as this is an enhanced version of the command line version of Python. Also, if you have not already, consider using Jupyter. It provides you with notebook, documents and a code console on the same workspace.

The framework acts as an IDE but is aimed more at exploring the problems and the software you are developing than traditional IDEs.

About the author

Mats Tage Axelsson

I am a freelance writer for Linux magazines. I enjoy finding out what is possible under Linux and how we can all chip in to improve it. I also cover renewable energy and the new way the grid operates. You can find more of my writing on my blog.