On Sat, Dec 8, 2012 at 10:35 AM, c s <span dir="ltr"><<a href="mailto:conradsand.arma@gmail.com" target="_blank">conradsand.arma@gmail.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Armadillo sparse matrices are stored in Compressed Sparse Column format:<br><br><a href="http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_column_.28CSC_or_CCS.29" target="_blank">http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_column_.28CSC_or_CCS.29</a><br>
<br>This layout is used by a majority of external solvers. <br><br>It would be far more efficient to take this layout into account when copying matrices, instead of blindly (and slowly) copying element by element. <br></blockquote>
<div><br></div><div>dgCMatrix objects also use compressed sparse column format with zero-based indices so it is likely that it would only be necessary to copy the contents of the arrays of column pointers (the "p" slot), row indices (the "i" slot) and values of the non-zeros (the "x" slot). It may even be possible to just map the pointers for a read-only sparse matrix.</div>
<div><br></div><div>Romain:</div><div> In general it is very slow to insert non-zero elements into this format. In the worst case the entire structure must be copied and extended for each insertion. We have to keep telling people about this when they tell us that sparse matrices in R are so slow to work with.</div>
<div><br></div><div> You have seen what the layout of a dgCMatrix is so it is only necessary to find the desired components of a arma::sp_mat object. I'll take a look once I get some things installed. If it is desirable the as and wrap methods for the Eigen::SparseMatrix<T> and Eigen::MappedSparseMatrix<T> classes should be fairly easy to adapt for arma::sp_mat class.</div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">
<br>On Sunday, December 9, 2012, Romain Francois <<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a>> wrote:<br>> Le 08/12/12 09:45, Søren Højsgaard a écrit :<br>>><br>>> Dear all,<br>
>><br>>> I want to use a matrix (of type "dgCMatrix" from the Matrix package) in RcppArmadillo, so I do:<br>>><br>>> library(inline)<br>>> src <- '<br>>> using namespace arma;<br>
>> using namespace Rcpp;<br>>> SpMat<double> X = as<SpMat<double> >(XX_);<br>>> '<br>>> foo <- cxxfunction(signature(XX_=""), body=src, plugin="RcppArmadillo")<br>
>><br>>> - but this fails. It seems to me (browsing the web) that SpMat are supported, but I might be wrong here. I have no indication that dgCMatrix matrices can be "converted" to SpMat's. I know that I can work with dgCMatrix matrices with RcppEigen, but what I need is to extract submatrices and that is very easy to do with RcppArmadillo.<br>
>><br>>> Any thoughts? Thanks in advance.<br>>> Regards<br>>> Søren<br>><br>> Doug might know better about the internals of Matrix types. This is just following the recipee from how these are handled in RcppEigen:<br>
><br>> #include <RcppArmadillo.h><br>> // [[Rcpp::depends(RcppArmadillo)]]<br>> using namespace Rcpp ;<br>><br>> // [[Rcpp::export]]<br>> void convert(S4 mat){<br>> IntegerVector dims = mat.slot( "Dim" ) ;<br>
> IntegerVector i = mat.slot( "i" ) ;<br>> IntegerVector p = mat.slot( "p" ) ;<br>> NumericVector x = mat.slot( "x" ) ;<br>><br>> int nrow = dims[0], ncol = dims[1] ;<br>
> arma::sp_mat res( nrow, ncol) ;<br>> for(int j = 0; j < ncol; ++j) {<br>> for (int k = p[j]; k < p[j + 1]; ++k) res( i[k], j ) = x[k];<br>> }<br>> std::cout << res << std::endl ;<br>
><br>> }<br>><br>><br>> /*** R<br>> require(Matrix)<br>> i <- c(1,3:8); j <- c(2,9,6:10); x <- 7 * (1:7)<br>> ( A <- sparseMatrix(i, j, x = x) )<br>><br>> convert(A)<br>
> ***/<br>><br>> I don't think there is a better way to fill multiple values ina SpMat, maybe Conrad has insights.<br>><br>> Romain<br>><br>> --<br>> Romain Francois<br>> Professional R Enthusiast<br>
> <a href="tel:%2B33%280%29%206%2028%2091%2030%2030" value="+33628913030" target="_blank">+33(0) 6 28 91 30 30</a><br>><br>> R Graph Gallery: <a href="http://gallery.r-enthusiasts.com" target="_blank">http://gallery.r-enthusiasts.com</a><br>
><br>> blog: <a href="http://romainfrancois.blog.free.fr" target="_blank">http://romainfrancois.blog.free.fr</a><br>
> |- <a href="http://bit.ly/RE6sYH" target="_blank">http://bit.ly/RE6sYH</a> : OOP with Rcpp modules<br>> `- <a href="http://bit.ly/Thw7IK" target="_blank">http://bit.ly/Thw7IK</a> : Rcpp modules more flexible<br>><br>
> _______________________________________________<br>
> Rcpp-devel mailing list<br>> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
>
</div></div><br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br></div>