Whatever you do, a specialized library for matrix operations will probably be many times faster than a "naïve" implementation. MKL is well-known and widely-used, and it is heavily optimized for x86/x64 processors. But you can use whichever library gets the job done for you. Personally, I haven't used Eigen, only MKL.
The crux is: Most libraries that deal with matrices require those matrices to be stored in a specific way. The libraries often provide specialized data types and functions for that purpose. There can even be different data types for different "types" of matrices. For example, "sparse" matrices are very often stored in a CRS (Compressed Row Storage) or CSR (Compressed Sparse Row) format. Since it would be
really bad if you had to convert, back and forth, between "your" matrix representation and the matrix representation used/required by the library
all the time, I think you should model "your" matrix representation in such a way that it is "compatible" with the library you intend to use. Or just use the matrix data types from the library right away!
For example, the Eigen library apparently has a ready-to-use Matrix class. Why not use it then?
https://eigen.tuxfamily.org/dox/group__TutorialMatrixClass.html
those matrices are composed using a vectors of vectors |
Now
that would be a
very inefficient way to store matrices, I think.
You would require one separate
std::vector object for each row, and then another
std::vector object where all those row-vectors are stored. Or you would require one separate
std::vector object for each column, and then another
std::vector object where all those column-vectors are stored. Either way, you get a whole lot of separate
std::vector objects with a lot of separate "small" memory allocations and unnecessary overhead.
Furthermore: Almost certainly, you'd have to "convert" your matrix in order to pass it into the library.
It is probably better to use just a
single std::vector object to store the
entire matrix in one continuous chunk of memory. Individual elements can be addressed using either
row-major indexing or
column-major indexing:
https://en.wikipedia.org/wiki/Row-_and_column-major_order
But even that would only make sense for "dense" matrices, I suppose.
As mentioned before, for "sparse" matrices, special optimized formats, such as CRS/CSR, are usually used.