9 double asDouble(
int x);
10 double asDouble(
double x);
11 double asDouble(AD<double> x);
12 double asDouble(AD<AD<double> > x);
13 double asDouble(AD<AD<AD<double> > > x);
14 #ifdef TMBAD_FRAMEWORK 18 double asDouble(
int x){
return double(x);}
19 double asDouble(
double x){
return x;}
20 double asDouble(AD<double> x){
return CppAD::Value(x);}
21 double asDouble(AD<AD<double> > x){
return CppAD::Value(CppAD::Value(x));}
22 double asDouble(AD<AD<AD<double> > > x){
return CppAD::Value(CppAD::Value(CppAD::Value(x)));}
23 #ifdef TMBAD_FRAMEWORK 32 R_xlen_t nr = a.rows();
33 R_xlen_t nc = a.cols();
35 PROTECT(val = Rf_allocMatrix(REALSXP, nr, nc));
36 double *p = REAL(val);
37 for(R_xlen_t j=0; j<nc; j++)
38 for(R_xlen_t i=0; i<nr; i++)
39 p[i + j * nr] = asDouble(a(i,j));
45 #define asSEXP_VECTOR_OF_NUMERIC(Type, ns) \ 46 SEXP asSEXP(const ns::vector<Type> &a) CSKIP( \ 48 R_xlen_t size = a.size(); \ 50 PROTECT(val = Rf_allocVector(REALSXP,size)); \ 51 double *p = REAL(val); \ 52 for (R_xlen_t i = 0; i < size; i++) \ 53 p[i] = asDouble(a[i]); \ 57 asSEXP_VECTOR_OF_NUMERIC(
int,
tmbutils)
58 asSEXP_VECTOR_OF_NUMERIC(
double,
tmbutils)
59 asSEXP_VECTOR_OF_NUMERIC(
double, std)
60 #ifdef TMBAD_FRAMEWORK 64 asSEXP_VECTOR_OF_NUMERIC(AD<Type>,
tmbutils)
65 #undef asSEXP_VECTOR_OF_NUMERIC 70 R_xlen_t size = a.size();
72 PROTECT(val = Rf_allocVector(VECSXP, size));
73 for (R_xlen_t i = 0; i < size; i++)
74 SET_VECTOR_ELT(val, i,
asSEXP(a[i]));
79 SEXP
asSEXP(
const double &a) CSKIP(
82 PROTECT(val=Rf_allocVector(REALSXP,1));
87 SEXP
asSEXP(
const int &a) CSKIP(
90 PROTECT(val=Rf_allocVector(INTSXP,1));
97 SEXP
asSEXP(
const AD<Type> &a){
98 return asSEXP(CppAD::Value(a));
100 #ifdef TMBAD_FRAMEWORK 107 template <class Type>
110 if(!Rf_isReal(x)) Rf_error(
"NOT A VECTOR!");
111 R_xlen_t n = XLENGTH(x);
112 typedef Eigen::Map<Eigen::Matrix<double,Eigen::Dynamic,1> > MapVector;
113 MapVector tmp(REAL(x), n);
128 template <
class Type>
132 Rf_error(
"x must be a matrix in 'asMatrix(x)'");
133 R_xlen_t nr = Rf_nrows(x);
134 R_xlen_t nc = Rf_ncols(x);
137 for(R_xlen_t j=0; j<nc; j++)
138 for(R_xlen_t i=0; i<nr; i++)
139 y(i, j) = Type(p[i + nr * j]);
149 PROTECT(dim = Rf_allocVector(INTSXP, a.dim.size()));
150 for(
int i=0; i<a.dim.size(); i++)
151 INTEGER(dim)[i] = a.dim[i];
152 Rf_setAttrib(val, R_DimSymbol, dim);
159 SEXP
asSEXP(Eigen::SparseMatrix<Type> x){
160 typedef typename Eigen::SparseMatrix<Type>::InnerIterator Iterator;
162 R_xlen_t nnz = x.nonZeros();
163 SEXP cls = PROTECT(R_do_MAKE_CLASS(
"dgTMatrix"));
164 SEXP ans = PROTECT(R_do_new_object(cls));
165 SEXP dim = PROTECT(Rf_allocVector(INTSXP, 2));
166 SEXP dimnames = PROTECT(Rf_allocVector(VECSXP, 2));
167 SEXP values = PROTECT(Rf_allocVector(REALSXP, nnz));
168 SEXP i = PROTECT(Rf_allocVector(INTSXP, nnz));
169 SEXP j = PROTECT(Rf_allocVector(INTSXP, nnz));
170 SEXP factors = PROTECT(Rf_allocVector(VECSXP, 0));
171 R_do_slot_assign(ans, Rf_install(
"i"), i);
172 R_do_slot_assign(ans, Rf_install(
"j"), j);
173 R_do_slot_assign(ans, Rf_install(
"Dim"), dim);
174 R_do_slot_assign(ans, Rf_install(
"Dimnames"), dimnames);
175 R_do_slot_assign(ans, Rf_install(
"x"), values);
176 R_do_slot_assign(ans, Rf_install(
"factors"), factors);
178 INTEGER(dim)[0] = x.rows();
179 INTEGER(dim)[1] = x.cols();
181 for (R_xlen_t cx=0; cx<x.outerSize(); cx++)
183 for (Iterator itx(x,cx); itx; ++itx)
185 INTEGER(i)[k] = itx.row();
186 INTEGER(j)[k] = itx.col();
187 REAL(values)[k] = asDouble(itx.value());
Vector class used by TMB.
Matrix class used by TMB.
Scalar Value() const
Return the underlying scalar value of this ad_aug.
vector< Type > asVector(SEXP x)
Construct c++-vector from SEXP object.
SEXP asSEXP(const matrix< Type > &a)
Convert TMB matrix, vector, scalar or int to R style.
matrix< Type > asMatrix(const vector< Type > &x, int nr, int nc)
Vector <-> Matrix conversion (for row-major matrices)
Utility functions for TMB (automatically included)