Search code examples
c++matrixsparse-matrix

How to sum two spars matrix together in c++?


I want to add two sparse matrices together in C++?


Solution

  • If you use the boost::ublas library, then you can simply add them together, something like this.

    #include <boost/numeric/ublas/matrix_sparse.hpp>
    using namespace boost::numeric::ublas;
    compressed_matrix<double > A(30000, 100, 30000 ),B(30000, 100, 30000 ),C(30000, 100, 30000 );
    A(1,1) = 1.0;
    B(99,99) = 10.0;
    C = A + B;