views:

542

answers:

6

I'm doing some linear algebra math, and was looking for some really lightweight and simple to use matrix class that could handle different dimensions: 2x2, 2x1, 3x1 and 1x2 basically. I presume such class could be implemented with templates and using some specialization in some cases, for performance. Anybody know of any simple implementation available for use? I don't want "bloated" implementations, as I'll running this in an embedded environment where memory is constrained.

Thanks

+6  A: 

You could try Blitz++ -- or Boost's uBLAS

chrispy
Both of these are fine. I use Boost's version since the syntax is simpler; though I believe blitz is still somewhat faster. Note that most benchmarks focus on large matrices, where fancy memory access patterns matter due to caching - for this kind of tiny matrix it probably won't. Both are in any case fairly light-weight: the extensive api is almost all compile-time templating: your compiled code won't contain any unnecessary extra fields, checks or code.
Eamon Nerbonne
Thanks, but those are somewhat bigger than what I'd need. I just need some few cases only (4 as I described above) so using a complete lib is too much. Probably I'm looking for some more hobby-like implementation, that provided the basic cases. Maybe I'll have to do my own..
Al
It's up to you, but I'd note it's almost always quicker to use a library. (Though for a hobby, you'll learn more doing it yourself, of course.) Boost has a fantastic reputation, and you'll get more than just uBLAS -- I recommend having a look at their other offerings. BOOST_FOREACH alone is worth the download.
chrispy
Sounds like a weekend project.
sbi
@AI: All these libraries are pure template libraries, so in terms of memory you only pay for the parts you use. Caveat: I'm pretty much a beginner with C++ templates, so you may wish to check.
Jitse Niesen
+1  A: 

std::valarray is pretty lightweight.

nhaugo
I'd need something more than raw arrays, like dimension checking (ie. multiplying 1x2 with 2x2 matrix is ok, but 2x1 with 2x2 is not), otherwise I'd almost do it manually..
Al
A: 

I use Newmat libraries for matrix computations. It's open source and easy to use, although I'm not sure it fits your definition of lightweight (it includes over 50 source files which Visual Studio compiles it into a 1.8MB static library).

Graphics Noob
+1  A: 

CML matrix is pretty good, but may not be lightweight enough for an embedded environment. Check it out anyway: http://cmldev.net/?p=418

dvide
A: 

I for one wasn't able to find simple enough library so I wrote it myself: http://koti.welho.com/aarpikar/lib/

I think it should be able to handle different matrix dimensions (2x2, 3x3, 3x1, etc) by simply setting some rows or columns to zero. It won't be the most fastest approach since internally all operations will be done with 4x4 matrices. Although in theory there might exist that kind of processors that can handle 4x4-operations in one tick. At least I would much rather believe in existence of such processors that than go optimizing those low level matrix calculations. :)

AareP
A: 

How about just store the matrix in an array, like

2x3 matrix = {2,3,val1,val2,...,val6}

This is really simple, and addition operations are trivial. However, you need to write your own multiplication function.

Paxinum