views:

491

answers:

4

I need to solve a system of linear equations in my program. Is there a simple linear algebra library for C++, preferably comprised of no more than a few headers? I've been looking for nearly an hour, and all the ones I found require messing around with Linux, compiling DLLs in MinGW, etc. etc. etc. (I'm using Visual Studio 2008.)

+5  A: 

Boost has some basic linear algebra stuff.

ast4
Here's a more precise link: http://www.boost.org/doc/libs/1_42_0/libs/numeric/ublas/doc/index.htm
Potatoswatter
I can't find anything in the documentation about inverting a matrix or solving linear equations. Am I missing it?
Archagon
@Archagon: Basically, Boost provides containers for vectors, matrices, and basic BLAS functionality (it supports all three BLAS levels). For more advanced linear algebra, you're likely to want to get some implementation of LAPACK. There are bindings for Boost, but they aren't in the main distribution (you'll have to hunt for them). You don't _need_ the bindings, but they make things a bit easier (theoretically).
James McNellis
+3  A: 

You can use the C++ bindings of LAPACK. A quick google turned up this link to some LAPACK for Windows libraries. Depending on the how big your systems are all this might be overkill though.

honk
+2  A: 

I am a big fan of Armadillo but your compiler may be an issue here according to the the end of the download page:

Windows

The development and testing of Armadillo has so far been done mainly on UNIX-like platforms, however there should be little or no platform specific code. While rudimentary tests were done on a Windows machine, the developers are interested in hearing how well Armadillo works in more thorough tests.

If you're having trouble with the 'MS Visual C++ 2008 Express Edition' compiler (due to its incomplete support for the C++ standard), you may want to try the following alternative free compilers:

  • Intel's C++ compiler
  • GCC (part of MinGW)
  • GCC (part of CygWin)

It is worth trying out as this is a well-designated (and mostly templated) library.

Otherwise, maybe try Eigen2 which lists your compiler as supported.

Edit: In response to the comment, Armadillo does not require Lapack but works better with it (and better still with tuned Blas):

Q: What other libraries do I need to make full use of Armadillo ?
A: Armadillo can work without external libraries. However it is recommended to install the LAPACK and ATLAS libraries in order to get added functionality. Armadillo will use ATLAS routines in lieu of LAPACK wherever possible.

Q: How well will Armadillo work without LAPACK/ATLAS ?
A: Basic functionality will be available (e.g. matrix addition and multiplication), but things like eigen decomposition will not be. Matrix multiplication (mainly for big matrices) will not be as fast.

Dirk Eddelbuettel
I tried Armadillo. It needs LAPACK to solve linear equations, which requires compiling a DLL.
Archagon
No, see my edit. It is *better* with Lapack but says it can live without. I Have not tried that configuration as I have Lapack...
Dirk Eddelbuettel
I thought so too, but whenever I use the solve() function, it tells me I need LAPACK installed...
Archagon
+2  A: 

I think Eigen is what you're looking for.

http://eigen.tuxfamily.org/index.php?title=Main_Page

It is a headers only library and compiles on many compilers. It even uses exotic assembly for faster math.

This is the page that shows off the linear solver api.

http://eigen.tuxfamily.org/dox/TutorialAdvancedLinearAlgebra.html

It has a few solvers with a simple api.

Chris H
This seems to work. Thanks!
Archagon