Sign in to follow this  
Storyyeller

Is there a good matrix class?

Recommended Posts

I am currently trying to implement a version of Whole History Rating (http://remi.coulom.free.fr/WHR/WHR.pdf), although my version will be slightly different because it is designed for a team game. Anyway, I'm not sure what to do for the matrices and vectors. Are there existing classes that are suitable, or should I attempt to write my own? All of the matrices will by tridiagonal, so ideally, the class should be able to take advantage of that and optimize it. One other question I had thought of is, if a class has an object as a member variable, for example, a std::map, can I safely assume that it has been initialized with the default constructor? My code seems to be working so far, but I don't want to accidenttally invoke undefined behavior.

Share this post


Link to post
Share on other sites
Quote:
Original post by Storyyeller
One other question I had thought of is, if a class has an object as a member variable, for example, a std::map, can I safely assume that it has been initialized with the default constructor?

No. Inside the body of the constructor, the member initializers for any members declared after that variable or any member functions you can assume that it's been properly constructed, but you can't assume that the constructor used was the default constructor.

Share this post


Link to post
Share on other sites
You know, when I punch "C++ matrix class" into Google I get a lot more than just Sparselib++ back.

Share this post


Link to post
Share on other sites
A matrix? Uh... try an array, it's literally what I computer matrix is.

You can have single or multi-dimensional arrays in which you can place any kind of data type in. Examples:

1D Array (1x4)
[Integer] [Integer] [Integer] [Integer]

2D Array (3x4)
[Object] [Object] [Object] [Object]
[Object] [Object] [Object] [Object]
[Object] [Object] [Object] [Object]

Maybe you get the point. ? I also assume you're using C++, here's a clicky link. And another. And yet another.

Hope I was a help, otherwise, sorry for wasting your time! :D

Share this post


Link to post
Share on other sites
Quote:
Original post by mntlinstituteflr
A matrix? Uh... try an array, it's literally what I computer matrix is.

You can have single or multi-dimensional arrays in which you can place any kind of data type in. Examples:

1D Array (1x4)
[Integer] [Integer] [Integer] [Integer]

2D Array (3x4)
[Object] [Object] [Object] [Object]
[Object] [Object] [Object] [Object]
[Object] [Object] [Object] [Object]

Maybe you get the point. ? I also assume you're using C++, here's a clicky link. And another. And yet another.

Hope I was a help, otherwise, sorry for wasting your time! :D
I'm not sure how relevant any of the above is to the stated problem. A proper matrix class is very different than a simple 1-d or 2-d array, and can include a more or less arbitrary amount of functionality (either directly or indirectly) beyond that provided by a simple array. (I'm assuming this is what the OP is looking for - a linear algebra library that includes classes for matrices and vectors, and is specifically suited to a particular application or set of applications.)

Share this post


Link to post
Share on other sites
There are a TON of C++ matrix libraries, and which one to use depends on your needs. Some of the following come to mind uBLAS, newmat, blitz++ (full fledged array library), SparseLib++ (as you found), MTL, eigen ... I'm getting tired already. What are your requirements (dense/sparse) what operations do you need? You need to give us more info if you want a thorough answer.

Share this post


Link to post
Share on other sites
The problem with an array is you can't perform many operations on it. So that's why you have matrix classes, so you can for example do things as:

m1(2,2) = 4; // Assign value to position (2,2)
m3 = m1 * m2; // Multiply matrices
m2 = -m1; // Invert matrix

I'm sure there are tons of classes available, otherwise you can easily make one yourself as the information on how to perform these operations can easily be found.

Share this post


Link to post
Share on other sites
I think Storyyeller should pay attention to jyk's signature. I've been watching the Configurable Math Library for some time, and while I haven't tried it out myself (I keep meaning to though!), it sounds like it might suit your needs. Though of course you're the only one who knows exactly what you need, and jyk is the one who would know best if the CML provides the functionality you're looking for. Anyway, I suggest you check it out at least.

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
I think Storyyeller should pay attention to jyk's signature. I've been watching the Configurable Math Library for some time, and while I haven't tried it out myself (I keep meaning to though!), it sounds like it might suit your needs. Though of course you're the only one who knows exactly what you need, and jyk is the one who would know best if the CML provides the functionality you're looking for. Anyway, I suggest you check it out at least.
Thanks for the mention! :) To be fair to the OP though, I should mention that the CML is primarily targeted towards games and graphics (whereas it sounds like the OP needs a general linear algebra library).

Share this post


Link to post
Share on other sites
I can personally confirm that the CML is the code equivalent of bottled awesomesauce; check it out. My only gripe is that it does not include any SIMD functionality by default, though this is a long ways from an unsolvable problem.

Share this post


Link to post
Share on other sites
If you want optimizations for tridiagonal matrices you might be stuck/better off rolling your own. LAPACK++ has a special class for them (storing the diagonals only as three vectors), but as far as I can tell doesn't allow most regular matrix operations.

If you don't need a whole lot of functionality, writing your own might take less time than learning a difficult API, and run faster. You could:

1. Store the matrix as three vectors
2. Use the tridiagonal matrix algorithm for Gaussian elimination if you need it
3. Simplify the various matrix-matrix and matrix-vector operations by removing unnecessary operations (adding zero to zero and multiplying by zero)

If you need a great deal of functionality, I suggest searching for "linear algebra C++ library" instead. After a quick survey I'd say check out Seldon and Armadillo, as their syntax seems to be pretty straightforward.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this