Tuesday, March 31, 2009

Linear Algebra and Matrices in C++ and Boost

Quite frequently you have to deal with matrices, eigenvectors and linear solvers. Boost offers (at least) two different and complementary approaches:
  1. uBLAS, A template interface over the traditional set of Blas libraries. This is very useful for people who are used to Fortran Blas and who wants to use it with C++. uBLAS provides BLAS level 1, 2, 3 functionality for dense, packed and sparse matrice. Linear algebra is supported by interfacing directly with underlying blas libraries. Here an Overview of Matrix and Vector Operations. Other bindings to different libraries are also available.
  • Pros: vector and matrix types are defined by using templates; Blas is very efficient;
  • Cons: Personally, I find the Blas notation a little difficult to use.
  1. MTL4, another template interface with can use either Blas or its internal pure C++ numerical algorithms.
  • Pros: I found MTL quite intuitive: matrices have many useful type definitions, and the matrix operations are rather complete. Performance is quite good, altought the distribution site is not reporting an estensive testing;
  • Cons: MTL is not stable and is not part of standard boost distribution.
I would love to hear what is your favourite solution for Linear Algebra and Matrices in C++.


  1. I use boost::ublas. It works like a charm. :)

  2. For my very modest Linear Algebra needs I use Eigen (http://eigen.tuxfamily.org)

  3. I need to use the Intel MKL cblas, (performance, accuracy) but it's quite an awkward C api, so I've started a project to provide a C++ wrapper here: