Closed yaoGithub2018 closed 1 year ago
Hard without a reproducible example! Rprintf()
should work from within a TMB file (with syntax like that in R); try adding debugging statements to your code that prints out (e.g.) x.cols()
and y.rows()
every time you multiply a couple of matrices and see if you can find a place where there's a mismatch ...
Thanks!
Here is my code obj <- MakeADFun(tmb_data, tmb_par, random=c("B", paste0("re", 1:6)), DLL="mod_age")
I wonder any statements I can add for debugging in R directly?
The TMB.hpp codes are (these are codes developed by someone else and I'm trying to reuse):
// Copyright (C) 2013-2015 Kasper Kristensen
// License: GPL-2
/ Utility: Compile time test for Type=double /
template
/** \file
/ Conditionally skip compilation /
/ Must come before Rinternals.h /
/ Early inclusion of Rprintf and REprintf /
/ Include the Eigen library. /
void eigen_REprintf(const char* x);
eigen_REprintf("The following condition was not met:\n"); \
eigen_REprintf(#x); \
eigen_REprintf("\nPlease check your matrix-vector bounds etc., "); \
eigen_REprintf("or run your program through a debugger.\n"); \
abort();}
if (!(x)) { \ Rcerr << "TMBad assertion failed.\n"; \ Rcerr << "The following condition was not met: " << #x << "\n"; \ Rcerr << "Possible reason: " msg << "\n"; \ Rcerr << "For more info run your program through a debugger.\n"; \ abort(); \ }
/ Provide access to file 'DisableStupidWarnings.h' which has been patched by RcppEigen to satisfy CRAN policy. This file may need regular updating. The renaming is to aviod a CRAN note. /
// Default: Include Eigen/Sparse normally
// Alternative: Include Eigen/Sparse with custom sparse matrix integer type
namespace Eigen { template<class T, int Flags = 0, class StorageIndex = TMB_SPARSE_STORAGE_INDEX> using SparseMatrix = SparseMatrix_rename<T, Flags, StorageIndex>; }
/ Workaround side effect when -DEIGEN_USE_LAPACKE is set /
/ Select AD framework: TMBAD or CPPAD /
/ Include the CppAD library. (Always turn off debug for cppad) /
// Workaround to make CppAD::Integer working with TMBad namespace CppAD { int Integer(const TMBad::ad_aug &x) CSKIP ({ return (int) x.Value(); }) TMBad::ad_aug abs(const TMBad::ad_aug &x) CSKIP ({ return TMBad::fabs(x); })
TMBad::ad_aug CondExp ## NAME( \ const TMBad::ad_aug &x0, \ const TMBad::ad_aug &x1, \ const TMBad::ad_aug &x2, \ const TMBad::ad_aug &x3) CSKIP ( { \ return TMBad::CondExp ## NAME(x0, x1, x2, x3); \ }) TMBAD_CONDEXP(Eq) TMBAD_CONDEXP(Ne) TMBAD_CONDEXP(Lt) TMBAD_CONDEXP(Gt) TMBAD_CONDEXP(Le) TMBAD_CONDEXP(Ge)
bool Variable(const TMBad::ad_aug &x) CSKIP ({ return !x.constant(); }) } // FIXME: Move to TMBad source? namespace TMBad { / Add 'isfinite', 'isinf' and 'isnan' to TMBad / using std::isfinite; bool isfinite(const TMBad::ad_aug &x)CSKIP({ return isfinite(x.Value()); }) using std::isinf; bool isinf(const TMBad::ad_aug &x)CSKIP({ return isinf(x.Value()); }) using std::isnan; bool isnan(const TMBad::ad_aug &x)CSKIP({ return isnan(x.Value()); }) }
/ Include the R library after Eigen and CppAD. Otherwise, the R macros can cause conflicts (as they do not respect the Eigen and CppAD namespace limits). E.g., the 'length' macro conflicts with CppAD when compiling with '-std=c++11'. /
void eigen_REprintf(const char* x)CSKIP({REprintf(x);})
using tmbutils::matrix;
using tmbutils::vector;
using CppAD::AD;
using CppAD::ADFun;
namespace CppAD{
/ Add to CppAD so that 'Variable' works for any 'Type' /
bool Variable(double x)CSKIP({ return false; })
/ Add 'isfinite', 'isinf' and 'isnan' to CppAD /
using std::isfinite;
template
using tmbutils::array; using Eigen::Matrix; using Eigen::Array;
/ Cleanup /
// Nothing more to precompile
Please post to tmb-users thanks.
Description:
I got an error saying from MakeADFun: ***** Make objective function TMB has received an error from Eigen. The following condition was not met: lhs.cols() == rhs.rows() && "invalid matrix product" && "if you wanted a coeff-wise or a dot product use the respective explicit functions" Please check your matrix-vector bounds etc., or run your program through a debugger.
Reproducible Steps:
Sorry I don't have a reproducible example here and I'm first time user of TMB. And I have no prior knowledge on C++.
TMB Version:
‘1.9.2’
R Version:
"R version 4.1.3 (2022-03-10)"
Operating System:
64-bit operating system, x64-based processor