kaskr / adcomp

AD computation with Template Model Builder (TMB)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Misuse of `M_chm_sparse_to_SEXP` by `tmb_invQ_tril_halfdiag`

jaganmn opened this issue · comments

Forthcoming improvements to subscript methods in Matrix have uncovered that you are misusing M_chm_sparse_to_SEXP in tmb_invQ_tril_halfdiag:

return M_chm_sparse_to_SEXP(iQ, 1 /* Free */ , -1 /* uplo="L" */ , 0, "", R_NilValue);

Here, the fifth argument is used to set the diag slot of the resulting dtCMatrix and so must be "N" (non-unit triangular) or "U" (unit triangular) but never "". Please correct, then test your patch against Matrix-devel:

$ svn checkout svn://svn.r-forge.r-project.org/svnroot/matrix/pkg/Matrix
$ R CMD build Matrix
$ R CMD INSTALL Matrix_1.5-5.tar.gz

Trouble with the invalid ihessian (having diag = "") seems to start inside of this lookup call and result in a gradient with NA in the "simple" example (and for several packages depending on TMB):

e$ind1 <- lookup(hessian,ihessian,iperm) ## Same dimensions

BTW, going forward the subscript method for CsparseMatrix will not construct an intermediate dgCMatrix for <dsCMatrix>[i, i] when i is strictly increasing or strictly decreasing. So lookup can in principle be made faster if you know that r is monotone.

Somehow, I didn't notice that the bug appears twice, i.e., also here:

return M_chm_sparse_to_SEXP(iQ, 1 /* Free */ , 0, 0, "", R_NilValue);