the compressed matrix has a basic linear container ( unbounded_array by default, but you can make it bounded_array or std::vector if you want), which contains all the non-zero elements of the matrix, in the main row (by default). This means that whenever you write a new nonzero element into a compressed matrix, it is inserted into this base array. If you do not fill the matrix in order (row-major), each insert will be O (n). When you change an existing non-zero element, it just changes in the underlying array.
Here is a simple test to see what the basic structure looks like:
#include <boost/numeric/ublas/matrix_sparse.hpp> #include <boost/numeric/ublas/storage.hpp> namespace ublas = boost::numeric::ublas; void show_array(const ublas::unbounded_array<double>& a) { for(size_t i=0; i<a.size(); ++i) std::cout << a[i] << ' '; std::cout << '\n'; } int main() { ublas::compressed_matrix<double> m (10, 10, 3 * 10); m(0, 5) = 1; // underlying array is {1, 0, 0, 0, ...} show_array(m.value_data()); m(0, 6) = 2; // underlying array is {1, 2, 0, 0, ...} show_array(m.value_data()); m(0, 4) = 3; // underlying array is {3, 1, 2, 0, ...} show_array(m.value_data()); m(0, 4) = 7; // underlying array is {7, 1, 2, 0, ...} show_array(m.value_data()); }
Cubbi
source share