Tamas K Papp
2006-Nov-29 15:33 UTC
[Rd] Matrix*vector: coercing sparse to dense matrix for arithmetic
Hi, I have a sparse Matrix (kronecker product of spline design matrices), and I need to multiply each row by a number to get another matrix. If the matrix is A and the numbers are stored in a vector k, with plain vanilla matrices I would do A*k But when using the Matrix package (class of A is "dgCMatrix"), I get the warning "coercing sparse to dense matrix for arithmetic". The error message is perfectly reasonable, I am looking for a way to do it right (keeping operations in the realm of sparse matrices). Any help would be appreciated. Thanks, Tamas
Douglas Bates
2006-Nov-29 17:19 UTC
[Rd] Matrix*vector: coercing sparse to dense matrix for arithmetic
On 11/29/06, Tamas K Papp <tpapp at princeton.edu> wrote:> I have a sparse Matrix (kronecker product of spline design matrices), > and I need to multiply each row by a number to get another matrix. If > the matrix is A and the numbers are stored in a vector k, with plain > vanilla matrices I would do> A*k> But when using the Matrix package (class of A is "dgCMatrix"), I get > the warning "coercing sparse to dense matrix for arithmetic". The > error message is perfectly reasonable, I am looking for a way to do it > right (keeping operations in the realm of sparse matrices). Any help > would be appreciated.I would suggest premultiplying by a diagonal matrix. However, when I look at the method dispatch after an example it doesn't appear to be making a good choice. What I think should work well (and Martin or I will make the necessary changes to ensure that it does) is library(Matrix) data(KNex); mm <- KNex$mm system.time(Diagonal(x = rnorm(nrow(mm))) %*% mm)
Reasonably Related Threads
- Creating a Sparse Matrix from a Sparse Vector
- Creating sparse matrix of type "dgCMatrix" directly
- preserving sparse matrices (Matrix)
- Matrix: How create a _row-oriented_ sparse Matrix (=dgRMatrix)?
- Matrix: How create a _row-oriented_ sparse Matrix (=dgRMatrix)?