I am confused on how the boost::compressed_matrix works. Suppose I declare the compressed_matrix like this:
boost::numeric::ublas::compressed_matrix<double> T(1000, 1000, 3*1000);
This allocates space for 3*1000 elements in a 1000×1000 matrix. Now how do I give it the locations which are the non-zero elements? When and how are the non-zero elements set? Is it each time I assign an element in the matrix, e.g. B(4,4)=4, it would mark that element as non-zero?
I would really appreciate if you could help me learn this using an example if possible. Some insight into the internal implementation would be great. I want to make sure I don’t write programs that are sub-optimal by guess work.
thank you!
compressed matrix has an underlying linear container (
unbounded_arrayby default, but you can make itbounded_arrayorstd::vectorif you want), which contains all non-zero elements of the matrix, in row-major (by default) order. That means that whenever you write a new non-zero element to compressed matrix, it is inserted into that underlying array. If you’re not filling the matrix in (row-major) order, every insert will be O(n). When you’re changing an existing non-zero element, it is simply changed in the underlying array.Here’s a simple test to see what the underlying structure looks like: