kaskr / adcomp

AD computation with Template Model Builder (TMB)
Other
178 stars 81 forks source link

Sparse matrix change in new Matrix library #371

Closed Craig44 closed 1 year ago

Craig44 commented 1 year ago

When using the the function Matrix::.bdiag I now get a class type dsTMatrix which used to create a type dgTMatrix. This now means the MakeADFun errors out.

Reproducible Steps:

With the package versions and OS outlined below. If I re-run Hans's pSplines example https://github.com/skaug/tmb-case-studies/tree/master/pSplines I get an error about the sparse matrix data type

Current Output:

Error in getParameterOrder(data, parameters, new.env(), DLL = DLL) : 
  Error when reading the variable: 'S'. Please check data and parameters.
In addition: Warning message:
In getParameterOrder(data, parameters, new.env(), DLL = DLL) :
  Expected sparse matrix of class 'dgTMatrix'.

When I query the class of S I get

class(data$S)
[1] "dsTMatrix"
attr(,"package")
[1] "Matrix"

It seems like the Matrix library might be depreciating dgTMatrix in favor of dsTMatrix

as(<dgCMatrix>, "dgTMatrix") is deprecated since Matrix 1.5-0; do as(., "TsparseMatrix") instead

Expected Output:

This used to work with no issues

TMB Version:

packageVersion("TMB")
[1] ‘1.9.1’
packageVersion('Matrix')
[1] ‘1.5.3’

R Version:

R.version.string
[1] "R version 4.2.1 (2022-06-23 ucrt)"

Operating System:

Windows 10

kaskr commented 1 year ago

I can confirm this problem. It used to be that one could go from any of the Matrix classes to 'dgTMatrix' (the only one supported by TMB) using as(.,"dgTMatrix"). Now, that doesn't work anymore and I've been struggling to find an equivalent to be used by datasanitize() - see https://github.com/kaskr/adcomp/commit/10cb50eced22e756a04e5a90d78b8a85ac8a7da1 - and came up with x <- as(.,"TsparseMatrix"). But as your example demonstrates this coercion doesn't seem to be enough.

If in addition you do

data$S <- as(data$S, "generalMatrix")
class(data$S)
## "dgTMatrix"

it seems to work. If there are no side-effects I should probably change datasanitize() to use as(as(.,"TsparseMatrix"), "generalMatrix").

Craig44 commented 1 year ago

Thanks for that response. Your code did allow the TMB model to build and run.