Sparse -> dense matrix conversion

102 views
Skip to first unread message

Mikael Jagan

unread,
Nov 28, 2020, 3:41:48 PM11/28/20
to TMB Users
Hi all,

I am multiplying a couple of sparse matrices and it happens that the product is dense. I'd like to convert the matrix product from sparse format to dense format before doing arithmetic with it.

TMB has the function asSparseMatrix() but no dense analogue. I did come across


causing me to try

matrix<Type> dmat = asMatrix(asSEXPR(spmat));

but compile failed. I know Eigen has MatrixXd(), but it returns double instead of Type.

Multiplying a sparse-format matrix by a dense-format identity matrix gets me what I want, but certainly there is a better way...

I've attached a minimal example. Any help would be appreciated!

Mikael

test.cpp
test.R

Hans Skaug

unread,
Nov 29, 2020, 4:48:06 AM11/29/20
to TMB Users
matrix<Type> dmat = spmat;

is at least compiling. Does it give the correct result?

Mikael Jagan

unread,
Nov 30, 2020, 12:16:22 AM11/30/20
to TMB Users
Yes, it does give the correct result. It seems I was overthinking...

Thank you.
Reply all
Reply to author
Forward
0 new messages