I'm in need of a lightweight library for 2d & 3d vectors and 3x3 & 4x4 matrices. In basic C. Just so I don't reinvent the wheel suboptimally.
Any suggestions?
I'm in need of a lightweight library for 2d & 3d vectors and 3x3 & 4x4 matrices. In basic C. Just so I don't reinvent the wheel suboptimally.
Any suggestions?
Meschach is a c-only vector/matrix library, significantly smaller than e.g. LAPACK (according to the FAQ, at least :)
Many people are telling you to use various BLAS libraries, but this is probably going to be very slow for you since you are working on small matrices. Most of them are optimized to chunk the matrix into fixed sizes (around 50ish elements - depends on cache sizes) and operate on the chunks with an optimized algorithm, then operate on the leftovers with a trivial algorithm. On small matrices this makes it even slower than just calling the trivial algorithm.
FWIW, when I needed to do this in fortran (2x2 and 4x4 square matrix mults) I just hardcoded fully unrolled versions and it worked well enough (about 20x the speed of builtin MATMUL on gfortran, but part of this was probably due to the fact that MATMUL is not in place and my version was). I could never find a good library to do this for me.
In C++ it would be fine since you would be able to use BLITZ but alas...
Assuming you're coding for x86, you might look into Intel's Integrated Performance Primitives (IPP) and Math Kernel Library (MKL). These provide super-duper-fast libraries (probably faster than anyone else could write, since they're optimized for non-public microarchitecture details) for lots of common operations, including vectors and matrices.
Try CIMG (cimg.sourceforge.net). It's image-processing focused, but is free, extremely lightweight (the entire library consists of a single .h file!), and has all the standard vector/matrix operations.
It is C++, so it won't work if you're doing straight C, but it's worth a look.
Perhaps you should try DirectX math lib (just Windows...). Vectors, matrices, operations on it, all you need is probably there. You can only use this, not whole DX. You can use version 9 or 10. It's fast :)
#include <D3DX9Math.h>
There are a lot of options at Mathtools.net. Object-Oriented Numerics also lists some packages that might work for you. Since I'm not sure exactly what you are doing (do you need a lot of optimized linear algebra? Or will simple operations suffice?), it's hard to be more specific.
In general, the Matrix Template Library is pretty well thought-of. And if you need some serious linear algebra grunt, you should look at BLAS and LAPACK.
You may also want to have a look at the Armadillo C++ library