At present, it seems that we need the sparse matrix format that is passed from R to be dgCMatrix.
dgSMatrix is a type for symmetric matrices and can be automatically produced from Matrix-based manipulations:
Q <- matrix(0, 4, 4)
Q[1,2] <- Q[2,1] <- Q[2,3] <- Q[3,2] <- Q[3,4] <- Q[4,3] <- -1
diag(Q) <- c(1,2,2,1)
Q <- Matrix(Q, sparse = TRUE)
class(Q)
## [1] "dsCMatrix"
myfun <- nFunction(
fun = function(x = 'numericVector', Q = 'nSparseMatrix') {
loglik <- sum(x * (Q%*%x))
return(loglik)
}, returnType = 'numericScalar')
cmyfun <- nCompile(myfun)
cmyfun(x, Q)
# Error: Need S4 class dgCMatrix for a sparse matrix
cmyfun(x, as(Q, 'dgCMatrix')) # works
Given the ease of converting to dgCMatrix and the helpful error message, this isn't a priority, but it would be nice to allow symmetric matrices at some point for user flexibility and presumably a bit of additional computational efficiency. There might also be cases where what Eigen does under the hood in terms of algorithms matters if symmetry is known.
At present, it seems that we need the sparse matrix format that is passed from R to be
dgCMatrix.dgSMatrixis a type for symmetric matrices and can be automatically produced from Matrix-based manipulations:Given the ease of converting to
dgCMatrixand the helpful error message, this isn't a priority, but it would be nice to allow symmetric matrices at some point for user flexibility and presumably a bit of additional computational efficiency. There might also be cases where what Eigen does under the hood in terms of algorithms matters if symmetry is known.