Decent C++ library for matrix manipulation and calculating Eigenvalues/vectors

Discussion in 'C++' started by Havatcha, May 10, 2005.

  1. Havatcha

    Havatcha Guest

    Does anyone know of a decent (free/easy to use) C++ library for
    manipulating matrices and caculating eigenvalues, eigenvectors and so
    on? I intend to add some Principal Component Analysis functionality to
    some software.

    Any recommendations appreciated.
    Havatcha, May 10, 2005
    1. Advertisements

  2. what platform and compiler are you using?
    Bill Shortall, May 10, 2005
    1. Advertisements

  3. Havatcha

    Mark P Guest

    There's a collection of Fortran libraries for linear algebra called
    LAPACK. I'm pretty sure it's free though I can't testify to its ease of
    use. At the very least you'll have to get your compiler to link C++
    code to Fortran objects.

    A google search reveals that there are packages LAPACK++ and CLAPACK
    which appear to be C++ and C code.
    Mark P, May 10, 2005
  4. Take a look at
    The C++ Scalar, Vector, Matrix and Tensor class Library

    Then take a look at
    The Object-Oriented Numerics Page
    E. Robert Tisdale, May 10, 2005
  5. Try this:

    The Matrix Template Library (MTL)

    The Matrix Template Library (MTL) is a high-performance generic
    component library that provides comprehensive linear algebra
    functionality for a wide variety of matrix formats.

    As with the Standard Template Library (STL), MTL uses a five-fold
    approach, consisting of generic functions, containers, iterators,
    adaptors, and function objects, all developed specifically for high
    performance numerical linear algebra. Within this framework, MTL
    provides generic algorithms corresponding to the mathematical operations
    that define linear algebra. Similarly, the containers, adaptors, and
    iterators are used to represent and to manipulate concrete linear
    algebra objects such as matrices and vectors.

    To many scientific computing users, however, the advantages of an
    elegant programming interface are secondary to issues of performance.
    Generic programming is a powerful tool in this regard as well -
    performance tuning can itself be described in a generic fashion. These
    performance tuning abstractions are realized in a generic low-level
    library - the Basic Linear Algebra Instruction Set (BLAIS). Experimental
    results show that MTL with the BLAIS achieves performance that is as
    good as, or better than, vendor-tuned libraries. Thus, MTL demonstrates
    that the proper abstractions can be used to achieve high levels of
    performance, contrary to conventional wisdom. In addition, MTL requires
    orders of magnitude fewer lines of code for its implementation, with the
    concomitant savings in development and maintenance effort.

    Yuriy Solodkyy, May 11, 2005
  6. Havatcha

    Havatcha Guest

    Good point, GCC3.3 and Linux.

    Looks like there are a few interesting libraries to choose from already
    on the thread, thanks everyone for your help.
    Havatcha, May 11, 2005
  7. Linear algebra libs and eigenvectors rank close to root canal work
    in the

    heirarcy of assignments C++ programmers avoid. There isn't even the most

    remote possibility you will find a library that is

    1 written entirely in C++
    2 fully featured i.e. has equation solving, decompositions like the
    SVD and QR , and eigenvalues symmetric & non symmetric
    3 includes the source code
    4 free.

    There is however the very good probability that you will spend a LOT of

    time messing arround with the libs out there and get nothing for your

    Many of the libs write the kernel in C or C++ and then try to link it to
    the old

    LAPACK libs written in fortran. Unfortunately they don't include the final

    file so you have to get yourself a fortran compiler, C++ compiler, and

    compatable linker for the two .. not easy. LAPACK has many many thousands
    of lines

    All of the free libs out there have some sort of problem. My advise:

    1. buy MATLAB and the toolbox you need for your problem.

    2. try the Gnu Scientific Library ( GSL) it's written in C not C++
    and it won't compile on windows; but it has the symmetric eigenvalues
    and SVD you need for your SCA. It even has a big thick manual in print.

    3. Look at Quantlib. It's in C++, free ,very well written, and does have
    SVD ( not eigens) but its not exactly a Linear Algebra Package .
    You will have to pick those parts out. You might get it running in two

    4. talk to the people who have writen C++ LAPACK combinations and see if
    will do the fortran compilations and linking for you

    5. Do not let the thought of writing it yourself reside in your memory for
    more than a nanosecond.

    Good luck,.... Bill
    Bill Shortall, May 11, 2005
  8. Havatcha

    Greg Schmidt Guest

    You may find some useful resources at
    Greg Schmidt, May 11, 2005
  9. I think newmat11 is good for this kind of thing, but since I wrote it
    this is not an unbiased opinion.


    Robert Davies, May 12, 2005
  10. I think so too.
    Your bias is shared by lots of people.
    E. Robert Tisdale, May 12, 2005
  11. Havatcha

    Lionel B Guest

    I'm with you so far...
    Why? The learning curve may be steep but it's not rocket science...
    .... actually, LAPACK is probably very widely used in rocket science ;-)

    Anyhow, old it may be, but LAPACK (with the BLAS linear algebra kernel) is *very* efficient, exceedingly well debugged,
    stable and probably among the most "tried and tested" software in existence. LAPACK still represents the state of the
    art in efficient linear algebra manipulation. It is not pretty, nor is the programming interface particularly friendly
    (that may be an understatement), but if you are serious about linear numerics it may well be worth getting to grips
    with. You will certainly end up learning (possibly more than you wanted to know) about linear algebra computing.
    Seeing as the OP has GCC he/she already has a fortran compiler (g77), C compiler (gcc), C++ compiler (g++), compatible
    linker (ld) and will indeed be able to build and link with fortran LAPACK.

    Many processor/compiler vendors provide highly optimised BLAS and/or LAPACK implementations. There is also the open
    source ATLAS project ( which can build BLAS + (a subset of) LAPACK libraries -
    automatically tuned for your platform - and includes a C (although not C++) interface. It is simple to build with GCC (I
    have done so under Linux and also under Cygwin on Win32).

    Lionel B, May 12, 2005
  12. Havatcha

    Havatcha Guest

    I had started to come to that conclusion, and since time is pressing, I
    will be using Octave ( instead for the calculations with
    a few system() calls and string parsing. Clunky, but less of a headache.
    Havatcha, May 12, 2005
  13. *very* efficient, exceedingly well debugged,
    existence. LAPACK still represents the state of the
    programming interface particularly friendly
    numerics it may well be worth getting to grips
    know) about linear algebra computing.
    compiler (gcc), C++ compiler (g++), compatible
    LAPACK implementations. There is also the open
    BLAS + (a subset of) LAPACK libraries -
    C++) interface. It is simple to build with GCC (I
    LAPACK is a great system; but I believe
    that getting it up and running is too
    much to expect for a C++ programmer especially if his Fortran is rusty,

    You didn't say which C++ main library you linked to - MTL, IT++ ?? How
    long did it take you to do it? What are the problems.?

    Bill Shortall, May 12, 2005
  14. Havatcha

    Lionel B Guest

    I found getting ATLAS[*] up and running to be as simple as typing "make" and "make install" and answering a few prompts
    (it can be a rather lengthy process - about 6-8 hours on my P4 (!) - as ATLAS tunes itself by running screeds of timing
    tests). At the end of the install you're set up with the atlas/blas/lapack libraries and C headers cblas.h and
    clapack.h. The function calls in these headers are reasonably documented and correspond exactly to the legacy Fortran
    BLAS and LAPACK interface - but you don't need to know any Fortran at all - it's straight C (you'll need to wrap the
    headers in extern "C" to use in C++).
    The point to note is that BLAS/LAPACK handle (dense) matrices as simple 1-d arrays (of float, double or complex type);
    as long as your C++ matrix library can handle this storage format (row- or column-major is ok), implementing an
    interface shouldn't be too problematic. I generally use my own rather basic matrix library, but I have in the past
    successfully hacked Blitz++ ( to interface to a few BLAS/LAPACK routines - that wasn't
    too difficult. The trickiest part is probably matching up the strides, transpose states, order, etc. with your library.

    Efficiency-wise alone it pays off; I've found straight matrix multiplication via the ATLAS/BLAS gemm() call to be orders
    of magnitude faster than I could code it in pure C or C++. Even beats Matlab hands down on my system...

    [*] Be aware that ATLAS doesn't implement the full LAPACK (although I believe there's work underway to extend it).
    Enough to do eigenvalues, solve equations, invert matrices, etc, though. [There are instructions in the ATLAS docs on
    how to set up a full Fortran LAPACK so that it will use the ATLAS BLAS, but that sounded a little hairy... haven't tried

    Lionel B, May 12, 2005
  15. Havatcha


    Jun 8, 2007
    Likes Received:
    how to use in VC++

    I am in a need of matrix operations in my project which is a Dialog Based MFC project in VC++.

    I tried using MTL. But since I wanted to use geev() for calculating Eigen Values, it requires to include mtl2lapack.h. VC++ says there's no tool available to compile the .h file. How do I solve this problem??

    I also tried using NewMat. It gives me a lot of linkage error. Can somebody help me on how to use NewMat functions in already existing VC++ project/wprkspace?

    Awaiting Reply.

    br.ramanand, Jun 8, 2007
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.