简体   繁体   中英

Can Armadillo efficiently multiply sparse-by-sparse and sparse-by-dense matrices into a dense result?

I am using Armadillo for some linear algebra problems. It has SpMat<float> for sparse matrices and Mat<float> for dense matrices.

Suppose I have sparse matrices S_a and S_b , and a dense matrix D . I need to compute the produces S_a*S_b and S_a*D , the results will be dense in both cases.

I can convert the sparse matrices into dense matrices and then multiply, but that will be inefficient (these matrices are very large). Is there a way to tell Armadillo to store the results into a dense matrix without performing an intermediate conversion step?

You can use the mat constructor which takes a sparse matrix and converts its data to a dense one:

arma::mat out1(S_a * S_b);
arma::mat out2(S_b * D);

Both multiplication operators for the sparse class (sparse-sparse and sparse-dense) will produce a sparse matrix object as output. (Whether or not it's really sparse will depend on the structure of the inputs.) This can be converted to a dense matrix using the dense matrix constructor with signature: arma::mat(arma::sp_mat) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM