kaskr / adcomp

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

Log of a Matrix #231

Open mckenziej opened 7 years ago

mckenziej commented 7 years ago

Hi Is there a function/method for deriving the log of a matrix in TMB?

Tried using Eigen 3.3.3 c++ libraries (http://eigen.tuxfamily.org/index.php?title=Main_Page) as below {

include

include <unsupported/Eigen/MatrixFunctions> // stored under TMB include path

template

Type objective_function::operator() () { // data:

using namespace Eigen;

.... matrix L(nareas,nareas); .... matrix Llog = L.log(); // Eigen library matrix log function

...... }

compilation fails with following critical error: C:/PROGRA~1/R/R-33~1.3/library/TMB/include/unsupported/Eigen/src/MatrixFunctions/MatrixLogarithm.h:245:54: note: candidates are: C:/PROGRA~1/R/R-33~1.3/library/TMB/include/unsupported/Eigen/src/MatrixFunctions/MatrixLogarithm.h:71:12: note: int Eigen::internal::matrix_log_get_pade_degree(float) inline int matrix_log_get_pade_degree(float normTminusI) ^ C:/PROGRA~1/R/R-33~1.3/library/TMB/include/unsupported/Eigen/src/MatrixFunctions/MatrixLogarithm.h:71:12: note: no known conversion for argument 1 from 'RealScalar {aka CppAD::AD<CppAD::AD<CppAD::AD > >}' to 'float'

Many thanks

Jeremy McKenzie

kaskr commented 7 years ago

It appears from the compiler errors that this implementation is not suitable for AD types ( we do not allow conversion from the AD type CppAD::AD<CppAD::AD<CppAD::AD > > to float ).

In order to get matrix log working in TMB one would have to follow the same approach as used to implement atomic::expm.

mckenziej commented 7 years ago

Hi All, For those interested I have created a function to derive the log of a AD square matrix using eigenvalue and eigenvector values [link below].

{Basic Eigen library function code derived from 'calbertsen' GitHub issue #144 }

No guaranties on this, it works on AD parameter matrices in my code but I have not had time to test it thoroughly, so "use at your own risk".

I would like to put in a request for someone, more stats adept than I, to write a more generalized and integrated matrix log function for TMB along the lines of what kaskr has suggested.

Regards

Jeremy McKenzie

TMB matrix log function.pdf