Triangle and sparse matrixes in C++ - c++

This is a very simple question - what is the best practice to work with triangle matrixes and to work with sparse matrices in C++?
For triangle matrix I suggest a data format as easy as
double* myMatrix;
int dimension;
as data structure in custom class. (I suggest that it was a square matrix in the full form.) And there will be methods for setting and accessing elements.
For sparse matrices - I know a couple of methods like saving just positions of elements in the row/column and their values. It's the question for your experience - which implementation of sparse matrix will be the best one?
P.S. Less memory, less CPU usage - that is my target, I am looking for the best solution, not the simplest one. All matrices will be used for solving systems of linear equations. And the size of matrices will be huge.
Thanks a lot for every advice!

If you have no idea about the structure of the matrix, then it is basically the same as a map. You could use std::map<std::pair<int,int>,double>. Or perhaps std::unordered_map if you have it.

Related

Representation of a symmetric diagonal matrix

Lets assume we have a huge symmetric diagonal matrix. What is the efficient way to implement this?
The only way that i could think of is that by using the symmetric property where Xij = Xji, we can reduce the size of this matrix by half. But then representing this matrix using a 2D array would be inefficient, since we cant reduce the matrix size by using arrays.
Another thing representing this matrix using adjacency list also would be inefficient, because relating this matrix to a graph. It would be a density graph. And the operation of adj list takes lots of time such as removing, inserting and searching.
But what about using heaps?
There is no one answer until you decide what you are going to do with this matrix (or maybe matrices?).
If you are just going to store and remember it, then just store it sequentially, leaving out the redundant entries. (Your code knows how to access it, because that is all it does, right?)
More probably, you want to do normal matrix operations on it. In that case, are you trying to make the storage efficient, or the execution? In the later case, I don't see many opportunities based on it being symmetric--the multiplies are the expensive thing and you probably still need all of those. If it is the storage, then are you limiting yourself to operations that only take symmetric in and symmetric out? Sounds awfully specific. If so, then you only need to do the calculations for the part you are storing, because, by definition the other entries are symmetric, so just write your code to generate that part of the matrix and you are done.

4 dimensional matrix in Armadillo

I started using Armadillo relatively recently, and although I like it a lot, it would be useful if it had ways of storing 4D matrices. Is there something I'm missing, or a workaround for this?
The last dimension would just have a size of three, so in theory I could have something like:
std::vector<arma::cube> 4Dmatrix(3);
for (int index=0; index<3; index++)
4Dmatrix[index] = cube(size1, size2, size3);
However, it feels like there must be a better way.
Thanks in advance!
You could potentially use the field class which stores arbitrary objects as elements in a vector, matrix or cube structure. E.g. (from the documentation) to create a field containing vec vectors:
// create a field containing vectors
field<vec> F(3,2);
No, there is no built-in way. If you look at the source, you will see that there are individual implementations for vectors, matrices and cubes.
Maybe a Boost multi array could be of use to you.

Storing Matrix information in STL vector. Which is better vector or vector of vectors?

I've created my own Matrix class were inside the class the information regarding the Matrix is stored in a STL vector. I've notice that while searching the web some people work with a vector of vectors to represent the Matrix information. My best guess tells me that so long as the matrix is small or skinny (row_num >> column_num) the different should be small, but what about if the matrix is square or fat (row_num << column_num)? If I were to create a very large matrix would I see a difference a run time? Are there other factors that need to be considered?
Thanks
Have you considered using an off-the-shelf matrix representation such as boost's instead of reinventing the wheel?
If you have a lot of empty rows for example, using the nested representation could save a lot of space. Unless you have specific information in actual use cases showing one way isn't meeting your requirements, code the way that's easiest to maintain and implement properly.
There are too many variables to answer your question.
Create an abstraction so that your code does not care how the matrix is represented. Then write your code using any implementation. Then profile it.
If your matrix is dense, the "vector of vectors" is very unlikely to be faster than a single big memory block and could be slower. (Chasing two pointers for random access + worse locality.)
If your matrices are large and sparse, the right answer to your question is probably "neither".
So create an abstract interface, code something up, and profile it. (And as #Mark says, there are lots of third-party libraries you should probably consider.)
If you store everything in a single vector, an iterator will traverse the entire matrix. If you use a vector of vectors, an iterator will only traverse a single dimension.

Efficiant multidimensional data storage in C++

I'm trying to write a C++ program that needs to store and adjust data in a 3D array. The size is given by the user and doesn't change throughout the run, and I don't need to perform any complicated matrix operations on it. I just need it to be optimized to set and get from given 3D coordinates (I do quite some iterations over all the members, and it's a big array). What's the best way to go about defining that array? Vector of vector of vector? Arrays of vectors? CvMat/IplImage with multi channels? Should I even keep it as 3D or just turn it into one very long interleaved vector and calculate indexes accordingly?
Thanks!
I would go with your last option, a single large array with transformed indices. If all you want to do is read and write known indices, this is probably the most efficient structure, both in terms of storage and speed. You can also wrap this in a class and overload operator () to make it easy to access 3D coordinates, for eg. you could write a(1,2,3) = 10; and the overloaded operator could take care transforming the 3D coordinates into a linear index. Iterating over such an array would also be quite simple since there's only one dimension.
It depends on what you mean by efficient, but have you looked at KD Trees?

How to invert a 3x3 matrix with each element being a 3x3 matrix?

I am trying to implement a Kalman filter for data fusion in C++. As part of the project, I need to implement a function to calculate the inverse of a 3x3 matrix that has each element being a 3x3 matrix itself. Could you help me solve this problem? I would prefer a solution that requires the least amount of calculations (most CPU efficient).
Also another question, since the Kalman filter depends on the inverse matrix, how should I handle the case when the matrix is not invertible?
Thanks for your helps.
You can do a "small matrix" which is each element of a "big matrix", which contains pointers to the "small matrix", so reversing the "big matrix" will take as long as reversing a normal matrix of integers.
Probably this is the fastest algorithm you can do but does it fit in your implementation? How are your matrix declared?