TMB Documentation  v1.9.11
Macros
C++ Macros

C++ macros used to enable interchange of data structures between R and C++. More...

Macros

#define ADREPORT(name)   TMB_OBJECTIVE_PTR -> reportvector.push(name, #name);
 Report an expression (scalar, vector, matrix or array valued) back to R with derivative information. More...
 
#define DATA_ARRAY(name)
 Get data array from R and declare it as array<Type> More...
 
#define DATA_ARRAY_INDICATOR(name, obs)
 Declare an indicator array 'name' of same shape as 'obs'. By default, the indicator array is filled with ones indicating that all observations are enabled. More...
 
#define DATA_FACTOR(name)
 Get data vector of type "factor" from R and declare it as a zero-based integer vector. More...
 
#define DATA_IARRAY(name)
 Get data array from R and declare it as array<int>
 
#define DATA_IMATRIX(name)
 Get data matrix from R and declare it as matrix<int>
 
#define DATA_INTEGER(name)
 Get data scalar from R and declare it as int.
 
#define DATA_IVECTOR(name)
 Get data vector of type "integer" from R and declare it vector<int>. (DATA_INTEGER() is for a scalar integer)
 
#define DATA_MATRIX(name)
 Get data matrix from R and declare it as matrix<Type>
 
#define DATA_SCALAR(name)
 Get data scalar from R and declare it as Type.
 
#define DATA_SPARSE_MATRIX(name)
 Get sparse matrix from R and declare it as Eigen::SparseMatrix<Type>
 
#define DATA_STRING(name)
 Get string from R and declare it as std::string. More...
 
#define DATA_STRUCT(name, struct)   struct<Type> name(getListElement(TMB_OBJECTIVE_PTR -> data, #name));
 Get data list object from R and make it available in C++. More...
 
#define DATA_UPDATE(name)
 Update a data object without re-taping. More...
 
#define DATA_VECTOR(name)
 Get data vector from R and declare it as vector<Type> More...
 
#define DATA_VECTOR_INDICATOR(name, obs)
 Declare an indicator vector 'name' of same shape as 'obs'. By default, the indicator vector is filled with ones indicating that all observations are enabled. More...
 
#define NLEVELS(name)
 Get the number of levels of a data factor from R.
 
#define PARAMETER(name)
 Get parameter scalar from R and declare it as Type.
 
#define PARAMETER_ARRAY(name)
 Get parameter array from R and declare it as array<Type>
 
#define PARAMETER_MATRIX(name)
 Get parameter matrix from R and declare it as matrix<Type>
 
#define PARAMETER_VECTOR(name)
 Get parameter vector from R and declare it as vector<Type>
 
#define REPORT(name)
 Report scalar, vector or array back to R without derivative information. More...
 
#define SIMULATE   if(isDouble<Type>::value && TMB_OBJECTIVE_PTR -> do_simulate)
 Mark code that is only executed during simulation. More...
 
#define TMB_ATOMIC_VECTOR_FUNCTION( ATOMIC_NAME, OUTPUT_DIM, ATOMIC_DOUBLE, ATOMIC_REVERSE)
 Construct atomic vector function based on known derivatives. More...
 
#define TMB_OBJECTIVE_PTR   this
 Pointer to objective function used by DATA and PARAMETER macros. More...
 

Detailed Description

C++ macros used to enable interchange of data structures between R and C++.

Macro Definition Documentation

§ ADREPORT

#define ADREPORT (   name)    TMB_OBJECTIVE_PTR -> reportvector.push(name, #name);

Report an expression (scalar, vector, matrix or array valued) back to R with derivative information.

Typical use: obtain point estimate and standard deviation of the expression via the R function sdreport() (see details in R documentation). In the summary, the dimensions of the original expression is lost, and must be retrieved manually.

Warning
ADREPORT(name) must not be used before name has been assigned a value.
Examples:
ar1_4D.cpp, compois.cpp, laplace.cpp, linreg.cpp, longlinreg.cpp, orange_big.cpp, spde.cpp, TMBad/solver.cpp, TMBad/spa_gauss.cpp, TMBad/spde_epsilon.cpp, and validation/MVRandomWalkValidation.cpp.

Definition at line 342 of file tmb_core.hpp.

§ DATA_ARRAY

#define DATA_ARRAY (   name)
Value:
if (!Rf_isNull(getListElement(TMB_OBJECTIVE_PTR -> parameters,#name))){ \
name = TMB_OBJECTIVE_PTR -> fillShape(tmbutils::asArray<Type>( \
TMB_OBJECTIVE_PTR -> getShape(#name, &Rf_isArray)), #name); \
} else { \
name = tmbutils::asArray<Type>(getListElement( \
TMB_OBJECTIVE_PTR -> data, #name, &Rf_isArray)); \
}
Array class used by TMB.
Definition: tmbutils.hpp:23
#define TMB_OBJECTIVE_PTR
Pointer to objective function used by DATA and PARAMETER macros.
Definition: tmb_core.hpp:208

Get data array from R and declare it as array<Type>

Note
If name is found in the parameter list it will be read as a parameter array.
Examples:
matrix_arrays.cpp, mvrw.cpp, mvrw_sparse.cpp, sam.cpp, sdv_multi.cpp, sdv_multi_compact.cpp, TMBad/sam.cpp, and validation/MVRandomWalkValidation.cpp.

Definition at line 352 of file tmb_core.hpp.

§ DATA_ARRAY_INDICATOR

#define DATA_ARRAY_INDICATOR (   name,
  obs 
)
Value:
if (!Rf_isNull(getListElement(TMB_OBJECTIVE_PTR -> parameters,#name))){ \
name.fill( TMB_OBJECTIVE_PTR -> fillShape(asVector<Type>( \
TMB_OBJECTIVE_PTR -> getShape(#name, &Rf_isReal )), \
#name), \
Rf_getAttrib( \
TMB_OBJECTIVE_PTR -> getShape(#name, &Rf_isReal ), \
Rf_install("ord")) ); \
}
Utilities for OSA residuals.
Definition: tmb_core.hpp:441
#define TMB_OBJECTIVE_PTR
Pointer to objective function used by DATA and PARAMETER macros.
Definition: tmb_core.hpp:208

Declare an indicator array 'name' of same shape as 'obs'. By default, the indicator array is filled with ones indicating that all observations are enabled.

This is used in conjunction with one-step-ahead residuals - see ?oneStepPredict

Examples:
validation/MVRandomWalkValidation.cpp.

Definition at line 518 of file tmb_core.hpp.

§ DATA_FACTOR

#define DATA_FACTOR (   name)
Value:
vector<int> name(asVector<int>( \
getListElement(TMB_OBJECTIVE_PTR -> data, #name, &Rf_isReal )));
Vector class used by TMB.
Definition: vector.hpp:17
#define TMB_OBJECTIVE_PTR
Pointer to objective function used by DATA and PARAMETER macros.
Definition: tmb_core.hpp:208

Get data vector of type "factor" from R and declare it as a zero-based integer vector.

The following example (R code) shows what you have on the R side and what is being received by the C++ template:

> x=factor(letters[4:10])
> x
[1] d e f g h i j
Levels: d e f g h i j

# The zero-based integer vector that the C++ template sees
> unclass(x) - 1
[1] 0 1 2 3 4 5 6
Examples:
lr_test.cpp, matrix_arrays.cpp, nmix.cpp, orange_big.cpp, and socatt.cpp.

Definition at line 281 of file tmb_core.hpp.

§ DATA_STRING

#define DATA_STRING (   name)
Value:
std::string name = \
CHAR(STRING_ELT(getListElement(TMB_OBJECTIVE_PTR -> data, #name), 0));
#define TMB_OBJECTIVE_PTR
Pointer to objective function used by DATA and PARAMETER macros.
Definition: tmb_core.hpp:208

Get string from R and declare it as std::string.

Example (incomplete) of use:

options << "apple", "orange";
DATA_STRING(choice);
if(! (choice == options).any() )
Rf_error( ("'" + choice + "'" + " not valid").c_str() );
Examples:
compois.cpp.

Definition at line 394 of file tmb_core.hpp.

§ DATA_STRUCT

#define DATA_STRUCT (   name,
  struct 
)    struct<Type> name(getListElement(TMB_OBJECTIVE_PTR -> data, #name));

Get data list object from R and make it available in C++.

Example (incomplete) of use:

In R:

data <- list()
data$object <- list(a=1:10, b=matrix(1:6,2))
obj <- MakeADFun(data,........)

In C++:

// Corresponding list object on the C++ side
template<class Type>
struct my_list {
my_list(SEXP x){ // Constructor
a = asVector<Type>(getListElement(x,"a"));
b = asMatrix<Type>(getListElement(x,"b"));
}
};
template<class Type>
Type objective_function<Type>::operator() ()
{
DATA_STRUCT(object, my_list);
REPORT(object.a); // Now you can use "a" and "b" as you like
REPORT(object.b);
return 0;
}
Examples:
spde.cpp, spde_aniso.cpp, spde_aniso_speedup.cpp, TMBad/solver.cpp, and TMBad/spde_epsilon.cpp.

Definition at line 433 of file tmb_core.hpp.

§ DATA_UPDATE

#define DATA_UPDATE (   name)
Value:
atomic::dynamic_data::cpy(name, \
atomic::dynamic_data::sexp_to_vector( \
atomic::dynamic_data::list_lookup_by_name( \
atomic::dynamic_data::envir_lookup_by_name( \
atomic::dynamic_data::set_dependent( \
atomic::dynamic_data::sexp_to_double( \
ENCLOS(TMB_OBJECTIVE_PTR -> report) \
), \
TMB_OBJECTIVE_PTR -> theta[0] \
), \
"data" \
), \
#name \
) \
) \
);
#define TMB_OBJECTIVE_PTR
Pointer to objective function used by DATA and PARAMETER macros.
Definition: tmb_core.hpp:208

Update a data object without re-taping.

Placing DATA_UPDATE(x) after e.g. DATA_VECTOR(x) will allow to change x on the R-side (through obj$env$data$x) without re-taping. Updatable data items behave like parameters. In particular, if-else branching on such data should be avoided.

Note
Only works for DATA_VECTOR(), DATA_MATRIX(), DATA_ARRAY() and DATA_SCALAR().
Warning
It is the user's responsibility not to reshape the data from R (i.e. change length or dimension). Storage mode must also remain constant.

Definition at line 221 of file dynamic_data.hpp.

§ DATA_VECTOR

#define DATA_VECTOR (   name)
Value:
vector<Type> name; \
if (!Rf_isNull(getListElement(TMB_OBJECTIVE_PTR -> parameters,#name))){ \
name = TMB_OBJECTIVE_PTR -> fillShape(asVector<Type>( \
TMB_OBJECTIVE_PTR -> getShape(#name, &Rf_isReal )), #name); \
} else { \
name = asVector<Type>(getListElement( \
TMB_OBJECTIVE_PTR -> data,#name,&Rf_isReal )); \
}
Vector class used by TMB.
Definition: vector.hpp:17
#define TMB_OBJECTIVE_PTR
Pointer to objective function used by DATA and PARAMETER macros.
Definition: tmb_core.hpp:208

Get data vector from R and declare it as vector<Type>

Note
If name is found in the parameter list it will be read as a parameter vector.
Examples:
adaptive_integration.cpp, ar1_4D.cpp, compois.cpp, fft.cpp, hmm.cpp, laplace.cpp, linreg.cpp, linreg_parallel.cpp, longlinreg.cpp, lr_test.cpp, matern.cpp, matrix_arrays.cpp, nmix.cpp, orange_big.cpp, register_atomic.cpp, register_atomic_parallel.cpp, sam.cpp, sde_linear.cpp, spatial.cpp, spde.cpp, spde_aniso.cpp, spde_aniso_speedup.cpp, thetalog.cpp, TMBad/interpol.cpp, TMBad/sam.cpp, TMBad/spa_gauss.cpp, TMBad/spatial.cpp, TMBad/spde_epsilon.cpp, TMBad/thetalog.cpp, transform.cpp, transform2.cpp, transform_parallel.cpp, tweedie.cpp, validation/randomwalkvalidation.cpp, and validation/rickervalidation.cpp.

Definition at line 236 of file tmb_core.hpp.

§ DATA_VECTOR_INDICATOR

#define DATA_VECTOR_INDICATOR (   name,
  obs 
)
Value:
if (!Rf_isNull(getListElement(TMB_OBJECTIVE_PTR -> parameters,#name))){ \
name.fill( TMB_OBJECTIVE_PTR -> fillShape(asVector<Type>( \
TMB_OBJECTIVE_PTR -> getShape(#name, &Rf_isReal )), \
#name), \
Rf_getAttrib( \
TMB_OBJECTIVE_PTR -> getShape(#name, &Rf_isReal ), \
Rf_install("ord")) ); \
}
Utilities for OSA residuals.
Definition: tmb_core.hpp:441
#define TMB_OBJECTIVE_PTR
Pointer to objective function used by DATA and PARAMETER macros.
Definition: tmb_core.hpp:208

Declare an indicator vector 'name' of same shape as 'obs'. By default, the indicator vector is filled with ones indicating that all observations are enabled.

This is used in conjunction with one-step-ahead residuals - see ?oneStepPredict

Examples:
validation/randomwalkvalidation.cpp, and validation/rickervalidation.cpp.

Definition at line 534 of file tmb_core.hpp.

§ REPORT

#define REPORT (   name)
Value:
if( isDouble<Type>::value && \
TMB_OBJECTIVE_PTR -> current_parallel_region<0 ) \
{ \
SEXP _TMB_temporary_sexp_; \
PROTECT( _TMB_temporary_sexp_ = asSEXP(name) ); \
Rf_defineVar(Rf_install(#name), \
_TMB_temporary_sexp_, TMB_OBJECTIVE_PTR -> report); \
UNPROTECT(1); \
}
SEXP asSEXP(const matrix< Type > &a)
Convert TMB matrix, vector, scalar or int to R style.
Definition: convert.hpp:30
#define TMB_OBJECTIVE_PTR
Pointer to objective function used by DATA and PARAMETER macros.
Definition: tmb_core.hpp:208

Report scalar, vector or array back to R without derivative information.

Warning
REPORT(name) must not be used before name has been assigned a value.
Note
REPORT() does nothing in parallel mode (construction of R-objects is not allowed in parallel).
Examples:
ar1_4D.cpp, hmm.cpp, matrix_arrays.cpp, multivariate_distributions.cpp, sam.cpp, spde_aniso.cpp, spde_aniso_speedup.cpp, TMBad/interpol.cpp, TMBad/sam.cpp, and TMBad/spa_gauss.cpp.

Definition at line 313 of file tmb_core.hpp.

§ SIMULATE

#define SIMULATE   if(isDouble<Type>::value && TMB_OBJECTIVE_PTR -> do_simulate)

Mark code that is only executed during simulation.

Note
SIMULATE() does nothing in parallel mode.
Examples:
ar1_4D.cpp, sam.cpp, and TMBad/sam.cpp.

Definition at line 329 of file tmb_core.hpp.

§ TMB_ATOMIC_VECTOR_FUNCTION

#define TMB_ATOMIC_VECTOR_FUNCTION (   ATOMIC_NAME,
  OUTPUT_DIM,
  ATOMIC_DOUBLE,
  ATOMIC_REVERSE 
)
Value:
TMB_ATOMIC_VECTOR_FUNCTION_DECLARE(ATOMIC_NAME) \
TMB_ATOMIC_VECTOR_FUNCTION_DEFINE( \
ATOMIC_NAME, OUTPUT_DIM, \
ATOMIC_DOUBLE, \
ATOMIC_REVERSE \
)

Construct atomic vector function based on known derivatives.

This macro is used internally to define most atomic functions, see for example atomic::matmul. It works portably for both CppAD and TMBad.

The macro is actually composed of two other lower-level macros. One declares and another defines the atomic function. If two atomic functions depend on one another, the lower-level macros may be useful - see e.g the atomic::fft.

Definition at line 217 of file tmbad_atomic_macro.hpp.

§ TMB_OBJECTIVE_PTR

#define TMB_OBJECTIVE_PTR   this

Pointer to objective function used by DATA and PARAMETER macros.

PARAMETER and DATA objects can be accessed from other classes and functions by redefining this macro.

Example (incomplete) of use:

// Re-define TMB_OBJECTIVE_PTR
#undef TMB_OBJECTIVE_PTR
#define TMB_OBJECTIVE_PTR obj
template<class Type>
foo (objective_function<Type> *obj) {
return a+b;
}
// Restore default TMB_OBJECTIVE_PTR
#undef TMB_OBJECTIVE_PTR
#define TMB_OBJECTIVE_PTR this

Definition at line 208 of file tmb_core.hpp.

License: GPL v2