Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify asDouble and avoid the need for WITH_LIBTMB with by using #311

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 14 additions & 17 deletions TMB/inst/include/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@
\brief Convert vector/matrix-Types to double SEXP types
*/

#ifdef WITH_LIBTMB
double asDouble(int x);
double asDouble(double x);
double asDouble(AD<double> x);
double asDouble(AD<AD<double> > x);
double asDouble(AD<AD<AD<double> > > x);
#else
double asDouble(int x){return double(x);}
double asDouble(double x){return x;}
double asDouble(AD<double> x){return CppAD::Value(x);}
double asDouble(AD<AD<double> > x){return CppAD::Value(CppAD::Value(x));}
double asDouble(AD<AD<AD<double> > > x){return CppAD::Value(CppAD::Value(CppAD::Value(x)));}
#endif
inline double asDouble(double const x){
return x;
}
inline double asDouble(int const x){
return x;
}
template<class T>
double asDouble(AD<T> const x){
return asDouble(CppAD::Value(x));
}

/** \brief Convert TMB matrix, vector, scalar or int to R style */
template<class Type>
Expand All @@ -28,8 +25,8 @@ SEXP asSEXP(const matrix<Type> &a)
SEXP val;
PROTECT(val = Rf_allocMatrix(REALSXP, nr, nc));
double *p = REAL(val);
for(R_xlen_t i=0; i<nr; i++)
for(R_xlen_t j=0; j<nc; j++)
for(R_xlen_t j=0; j<nc; j++)
for(R_xlen_t i=0; i<nr; i++)
p[i + j * nr] = asDouble(a(i,j));
UNPROTECT(1);
return val;
Expand Down Expand Up @@ -118,8 +115,8 @@ matrix<Type> asMatrix(SEXP x)
R_xlen_t nr = Rf_nrows(x); // nrows is int
R_xlen_t nc = Rf_ncols(x); // ncols is int
matrix<Type> y(nr, nc);
for(R_xlen_t i=0; i<nr; i++)
for(R_xlen_t j=0; j<nc; j++)
for(R_xlen_t j=0; j<nc; j++)
for(R_xlen_t i=0; i<nr; i++)
y(i, j) = Type(REAL(x)[i + nr * j]);
return y;
}
Expand Down
2 changes: 1 addition & 1 deletion TMB/inst/include/tmb_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ SEXP EvalADFunObjectTemplate(SEXP f, SEXP theta, SEXP control)
SEXP hessianrows; // Hessian rows
PROTECT(hessianrows=getListElement(control,"hessianrows"));
int nrows=Rf_length(hessianrows);
if((nrows>0)&(nrows!=ncols))Rf_error("hessianrows and hessianrows must have same length");
if((nrows>0)&(nrows!=ncols))Rf_error("hessianrows and hessiancols must have same length");
vector<size_t> cols(ncols);
vector<size_t> cols0(ncols);
vector<size_t> rows(nrows);
Expand Down