Compressed Matrix Compression Basics

I am confused about how boost :: compress_matrix works. Suppose I declare a compressed_matrix as follows:

boost::numeric::ublas::compressed_matrix<double> T(1000, 1000, 3*1000); 

This sets aside space for 3 * 1000 elements in a 1000x1000 matrix. Now, how can I give it locations that are non-zero elements? When and how are non-zero elements set? Each time I assign an element in a matrix, for example. B (4.4) = 4, will it mark this element as non-zero?

I would really appreciate it if you could help me learn this using an example, if possible. Some understanding of internal implementation would be wonderful. I want to make sure that I am not writing programs that are not optimal, working guesswork.

Thank you!

+7
c ++ optimization boost matrix sparse-matrix
source share
2 answers

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()); } 
+3
source share

you can use the operator (i,j) to create a non-zero element implicitly or use the insert_element function to insert an element explicitly.

The best place actually looks inside the implementation:

http://www.tena-sda.org/doc/5.2.2/boost/d2/db7/matrix__sparse_8hpp-source.html#l02761

true_reference insert_element (size_type i, size_type j, const_reference t)

Inserts the value of t into the jth element of the i-th row. Duplicates are not allowed.


void erase_element (size_type i, size_type j)

Erases the value in the jth element of the i-th row.

+1
source share

All Articles