What Are the Most Widely Used C++ Vector/Matrix Math/Linear Algebra Libraries, and Their Cost and Benefit Tradeoffs

What are the most widely used C++ vector/matrix math/linear algebra libraries, and their cost and benefit tradeoffs?

There are quite a few projects that have settled on the Generic Graphics Toolkit for this. The GMTL in there is nice - it's quite small, very functional, and been used widely enough to be very reliable. OpenSG, VRJuggler, and other projects have all switched to using this instead of their own hand-rolled vertor/matrix math.

I've found it quite nice - it does everything via templates, so it's very flexible, and very fast.


Edit:

After the comments discussion, and edits, I thought I'd throw out some more information about the benefits and downsides to specific implementations, and why you might choose one over the other, given your situation.

GMTL -

Benefits: Simple API, specifically designed for graphics engines. Includes many primitive types geared towards rendering (such as planes, AABB, quatenrions with multiple interpolation, etc) that aren't in any other packages. Very low memory overhead, quite fast, easy to use.

Downsides: API is very focused specifically on rendering and graphics. Doesn't include general purpose (NxM) matrices, matrix decomposition and solving, etc, since these are outside the realm of traditional graphics/geometry applications.

Eigen -

Benefits: Clean API, fairly easy to use. Includes a Geometry module with quaternions and geometric transforms. Low memory overhead. Full, highly performant solving of large NxN matrices and other general purpose mathematical routines.

Downsides: May be a bit larger scope than you are wanting (?). Fewer geometric/rendering specific routines when compared to GMTL (ie: Euler angle definitions, etc).

IMSL -

Benefits: Very complete numeric library. Very, very fast (supposedly the fastest solver). By far the largest, most complete mathematical API. Commercially supported, mature, and stable.

Downsides: Cost - not inexpensive. Very few geometric/rendering specific methods, so you'll need to roll your own on top of their linear algebra classes.

NT2 -

Benefits: Provides syntax that is more familiar if you're used to MATLAB. Provides full decomposition and solving for large matrices, etc.

Downsides: Mathematical, not rendering focused. Probably not as performant as Eigen.

LAPACK -

Benefits: Very stable, proven algorithms. Been around for a long time. Complete matrix solving, etc. Many options for obscure mathematics.

Downsides: Not as highly performant in some cases. Ported from Fortran, with odd API for usage.

Personally, for me, it comes down to a single question - how are you planning to use this. If you're focus is just on rendering and graphics, I like Generic Graphics Toolkit, since it performs well, and supports many useful rendering operations out of the box without having to implement your own. If you need general purpose matrix solving (ie: SVD or LU decomposition of large matrices), I'd go with Eigen, since it handles that, provides some geometric operations, and is very performant with large matrix solutions. You may need to write more of your own graphics/geometric operations (on top of their matrices/vectors), but that's not horrible.

Improving a maths function class for speed c++

  1. Perhaps the biggest gain in both speed and development time would be
    to use an existing linear algebra library instead of re-inventing
    the wheel, see

    • What are the most widely used C++ vector/matrix math/linear algebra libraries, and their cost and benefit
      tradeoffs?

    • Recommendations for a usable, fast C++ matrix library?

  2. Use BLAS and LAPACK that is tuned to your machine architecture. In
    particular, the vector instructions available on your machine and
    the cache sizes have a BIG impact on the performance.

  3. If your vectors are small enough, you may get a significant
    performance gain by allocating them on the stack. Now, you are
    allocating them on the heap.

  4. By using template metaprogramming techniques you can eliminate
    many of the temporaries and unnecessary loops at compile time. To
    repeat the Wikipedia example here:

    Say you have Vec x = alpha*(u -
    v);
    where alpha is a scalar and u and v are Vecs.

    If you
    implement it in the fashion you are doing it, it will cost you at
    least 2 temporary vectors (one for u-v and one for the
    multiplication with alpha) and 2 passing through the memory
    (2 or 3 loops: one for u-v, one for the
    multiplication with alpha and one more for the assignment if it is not optimized away).

    If you do template metaprogramming, Vec x = alpha*(u -
    v);
    will boil down to a single loop with no temporaries and that is the best you can get. The gain becomes even bigger with more complicated expressions.

    At the
    moment you don't have these operations but I guess it is only a matter
    of time that you will need them (weightValueVector() is an indication).

Of course, if you use a linear algebra library, you don't have to know / worry about any of these but you can concentrate on your application instead and get blazing fast code.

Favorite/Best A.X=B Solver that Supports Long Doubles?

Many C++ linear algebra libraries are based on templates, including NT2, Boost.uBLAS, Eigen (see What are the most widely used C++ vector/matrix math/linear algebra libraries, and their cost and benefit tradeoffs? for links). Thus, they should be able to support quads if your compiler/library can do maths with quads. For instance, in Eigen the type Eigen::Matrix<long double, Dynamic, Dynamic> denotes a matrix of arbitrary size containing long doubles, and you can use the standard functions to solve with such matrices.

Matrix library for large matrices?

You may use block matrix multiplication and inversion in order to reduce swapping.

If your matrices are sparse (i.e. they have a lot of elements that are zero), you can save memory by using a special storage approach.



Related Topics



Leave a reply



Submit