Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 3 years ago.
Improve this question
I've been dealing with math-intensive code and require a good matrix library. I could use a two dimensional array, but a complete matrix library (with multiplication, addition etc.) would be much more convenient. I obviously already googled it, but this gave me a plethora of choices. I was hoping that the opinions of a few experienced programmers would help narrow it down.
What are some good free matrix/linear algebra libraries for C++?
You can try uBlas
Functionality
uBLAS provides templated C++ classes for dense, unit and sparse vectors, dense, identity, triangular, banded, symmetric, hermitian and sparse matrices. Views into vectors and matrices can be constructed via ranges, slices, adaptor classes and indirect arrays. The library covers the usual basic linear algebra operations on vectors and matrices: reductions like different norms, addition and subtraction of vectors and matrices and multiplication with a scalar, inner and outer products of vectors, matrix vector and matrix matrix products and triangular solver. The glue between containers, views and expression templated operations is a mostly STL conforming iterator interface.
Eigen is a very fast matrix manipulation and linear algebra library which uses hardware acceleration when available.
There is also IT++, which has an easy to use syntax, similar to Matlab. Armadillo also has a very similar syntax, but is known to be considerably faster than IT++. (Armadillo uses template meta-programming, while IT++ doesn't). Both Armadillo and IT++ provide eigen decomposition, singular value decomposition, matrix inverse, etc. In contrast, uBlas uses template meta-programming for speed, but can't do matrix inversion, etc.
The speed difference cones at the trade-off in terms of different sets of functionality in various areas, eg. IT++ has many functions for signal processing, while Armadillo almost exclusively focuses on linear algebra.
This brings up a related point: the speed of a library is only one factor in its overall usefulness or value. For example, you may have a very fast library, but it takes quite a while to learn its API/syntax, or the syntax is hard to use. Another issue is the amount of functionality already present within the library -- eg. you may need to write your own functions. You may also need to consider whether the purpose of the library is to ease the conversion of Matlab code to C++, or you're already familiar with Matlab syntax.
Given the above points, you can end up spending more time coding and debugging than running your code, which in the end defeats the purpose of a fast library. In other words, the raw execution speed is only one factor, and it shouldn't be relied upon as the overall deciding factor. Development time is also a very important factor (eg. "time to product"), not only from a cost perspective, but also as less time spent coding frees you to do other things.
In the past I have used Armadillo . I cannot speak for its speed capabilities - it was recommended to me by a friend as it exhibits some similarity to MATLAB functions and notation. I did not try anything very intensive with it but it served my purposes at the time I used it. I have also heard good things about uBlas as is also recommended here.