From f6b3169740a3bdc18ecf67ddca7a79e09d8037e8 Mon Sep 17 00:00:00 2001 From: Benjamin Christoffersen Date: Wed, 15 Apr 2020 08:33:37 +0200 Subject: [PATCH] simplify asDouble and avoid the need for WITH_LIBTMB with by using inline change loop order in asSEXP and asMatrix (R matrices a in column major order) fix https://github.com/kaskr/adcomp/issues/309 --- TMB/inst/include/convert.hpp | 31 ++++++++++++++----------------- TMB/inst/include/tmb_core.hpp | 2 +- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/TMB/inst/include/convert.hpp b/TMB/inst/include/convert.hpp index 1b9cd73e8..3347ae607 100644 --- a/TMB/inst/include/convert.hpp +++ b/TMB/inst/include/convert.hpp @@ -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 x); -double asDouble(AD > x); -double asDouble(AD > > x); -#else -double asDouble(int x){return double(x);} -double asDouble(double x){return x;} -double asDouble(AD x){return CppAD::Value(x);} -double asDouble(AD > x){return CppAD::Value(CppAD::Value(x));} -double asDouble(AD > > 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 +double asDouble(AD const x){ + return asDouble(CppAD::Value(x)); +} /** \brief Convert TMB matrix, vector, scalar or int to R style */ template @@ -28,8 +25,8 @@ SEXP asSEXP(const matrix &a) SEXP val; PROTECT(val = Rf_allocMatrix(REALSXP, nr, nc)); double *p = REAL(val); - for(R_xlen_t i=0; i asMatrix(SEXP x) R_xlen_t nr = Rf_nrows(x); // nrows is int R_xlen_t nc = Rf_ncols(x); // ncols is int matrix y(nr, nc); - for(R_xlen_t i=0; i0)&(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 cols(ncols); vector cols0(ncols); vector rows(nrows);