TMB Documentation  v1.9.11
TMB.hpp
Go to the documentation of this file.
1 // Copyright (C) 2013-2015 Kasper Kristensen
2 // License: GPL-2
3 /* Utility: Compile time test for Type=double */
4 template<class Type>
5 struct isDouble{
6  enum{value=false};
7 };
8 template<>
9 struct isDouble<double>{
10  enum{value=true};
11 };
12 
16 /* To be removed */
17 #define TMB_DEBUG 0
18 #define TMB_PRINT(x)std::cout << #x << ": " << x << "\n"; std::cout.flush();
19 
20 /* Conditionally skip compilation */
21 #ifdef WITH_LIBTMB
22 #define CSKIP(...) ;
23 #define TMB_EXTERN extern
24 #else
25 #define CSKIP(...) __VA_ARGS__
26 #define TMB_EXTERN
27 #endif
28 #ifdef TMB_PRECOMPILE_ATOMICS
29 #define IF_TMB_PRECOMPILE_ATOMICS(...) __VA_ARGS__
30 #else
31 #define IF_TMB_PRECOMPILE_ATOMICS(...)
32 #endif
33 #ifdef HAVE_PRECOMPILED_ATOMICS
34 #define CSKIP_ATOMIC(...) ;
35 #else
36 #define CSKIP_ATOMIC(...) __VA_ARGS__
37 #endif
38 
39 /* Must come before Rinternals.h */
40 #ifdef _OPENMP
41 #include <omp.h>
42 #endif
43 
44 /* Early inclusion of Rprintf and REprintf */
45 #include <R_ext/Print.h>
46 #include "Rstream.hpp"
47 
48 /* Flag to bypass abort() */
49 #ifndef TMB_ABORT
50 #define TMB_ABORT abort()
51 #endif
52 
53 /* Include the Eigen library. */
54 #ifdef TMB_SAFEBOUNDS
55 #undef NDEBUG
56 #undef eigen_assert
57 void eigen_REprintf(const char* x);
58 #define eigen_assert(x) if (!(x)) { eigen_REprintf("TMB has received an error from Eigen. "); \
59  eigen_REprintf("The following condition was not met:\n"); \
60  eigen_REprintf(#x); \
61  eigen_REprintf("\nPlease check your matrix-vector bounds etc., "); \
62  eigen_REprintf("or run your program through a debugger.\n"); \
63  TMB_ABORT;}
64 #define TMBAD_ASSERT2(x,msg) \
65 if (!(x)) { \
66  Rcerr << "TMBad assertion failed.\n"; \
67  Rcerr << "The following condition was not met: " << #x << "\n"; \
68  Rcerr << "Possible reason: " msg << "\n"; \
69  Rcerr << "For more info run your program through a debugger.\n"; \
70  TMB_ABORT; \
71 }
72 #define TMBAD_ASSERT(x) TMBAD_ASSERT2(x,"Unknown")
73 #else
74 #undef NDEBUG
75 #define NDEBUG 1
76 #define TMBAD_ASSERT2(x,msg) (void) (x);
77 #define TMBAD_ASSERT(x) (void) (x);
78 #endif
79 /* Provide access to file 'DisableStupidWarnings.h' which has been
80  patched by RcppEigen to satisfy CRAN policy. This file may need
81  regular updating. The renaming is to aviod a CRAN note. */
82 #ifdef TMB_EIGEN_DISABLE_WARNINGS
83 #ifndef EIGEN_PERMANENTLY_DISABLE_STUPID_WARNINGS
84 #define EIGEN_PERMANENTLY_DISABLE_STUPID_WARNINGS 1
85 #endif
86 #include "EigenWarnings/DisableStupidWarnings"
87 #endif
88 /* We cannot use Eigen's parallel matrix multiply for AD types (GH390). */
89 #ifndef EIGEN_DONT_PARALLELIZE
90 #define EIGEN_DONT_PARALLELIZE
91 #endif
92 #include <Eigen/Dense>
93 
94 // Default: Include Eigen/Sparse normally
95 #ifndef TMB_SPARSE_STORAGE_INDEX
96 #include <Eigen/Sparse>
97 #else
98 // Alternative: Include Eigen/Sparse with custom sparse matrix integer type
99 #define SparseMatrix SparseMatrix_rename
100 #include <Eigen/Sparse>
101 #undef SparseMatrix
102 namespace Eigen {
103 template<class T, int Flags = 0, class StorageIndex = TMB_SPARSE_STORAGE_INDEX>
104 using SparseMatrix = SparseMatrix_rename<T, Flags, StorageIndex>;
105 }
106 #endif
107 
108 /* Workaround side effect when -DEIGEN_USE_LAPACKE is set */
109 #undef I
110 
111 /* Select AD framework: TMBAD or CPPAD */
112 #ifndef CPPAD_FRAMEWORK
113 #ifndef TMBAD_FRAMEWORK
114 #define CPPAD_FRAMEWORK
115 #endif
116 #endif
117 
118 /* Include the CppAD library. (Always turn off debug for cppad) */
119 #undef NDEBUG
120 #define NDEBUG 1
121 #include "cppad/cppad.hpp"
122 #ifdef TMBAD_FRAMEWORK
123 #include "TMBad/TMBad.hpp"
124 #include "TMBad/tmbad_allow_comparison.hpp"
125 #include "TMBad/eigen_numtraits.hpp"
126 #undef error
127 #include "TMBad/compile.hpp"
128 #include "TMBad/graph2dot.hpp"
129 #include "TMBad/compression.hpp"
130 #include "TMBad/ad_blas.hpp"
131 #ifndef WITH_LIBTMB
132 #include "TMBad/TMBad.cpp"
133 #endif
134 #define error Rf_error
135 // Workaround to make CppAD::Integer working with TMBad
136 namespace CppAD {
137 int Integer(const TMBad::ad_aug &x) CSKIP ({ return (int) x.Value(); })
138 TMBad::ad_aug abs(const TMBad::ad_aug &x) CSKIP ({ return TMBad::fabs(x); })
139 #define TMBAD_CONDEXP(NAME) \
140 TMBad::ad_aug CondExp ## NAME( \
141  const TMBad::ad_aug &x0, \
142  const TMBad::ad_aug &x1, \
143  const TMBad::ad_aug &x2, \
144  const TMBad::ad_aug &x3) CSKIP ( { \
145  return TMBad::CondExp ## NAME(x0, x1, x2, x3); \
146 })
147 TMBAD_CONDEXP(Eq)
148 TMBAD_CONDEXP(Ne)
149 TMBAD_CONDEXP(Lt)
150 TMBAD_CONDEXP(Gt)
151 TMBAD_CONDEXP(Le)
152 TMBAD_CONDEXP(Ge)
153 #undef TMBAD_CONDEXP
154 bool Variable(const TMBad::ad_aug &x) CSKIP ({ return !x.constant(); })
155 }
156 // FIXME: Move to TMBad source?
157 namespace TMBad {
158  /* Add 'isfinite', 'isinf' and 'isnan' to TMBad */
159  using std::isfinite;
160  bool isfinite(const TMBad::ad_aug &x)CSKIP({ return isfinite(x.Value()); })
161  using std::isinf;
162  bool isinf(const TMBad::ad_aug &x)CSKIP({ return isinf(x.Value()); })
163  using std::isnan;
164  bool isnan(const TMBad::ad_aug &x)CSKIP({ return isnan(x.Value()); })
165 }
166 #endif
167 
168 /* Include the R library _after_ Eigen and CppAD. Otherwise, the R
169  macros can cause conflicts (as they do not respect the Eigen and
170  CppAD namespace limits). E.g., the 'length' macro conflicts with
171  CppAD when compiling with '-std=c++11'. */
172 #include <R.h>
173 #include <Rinternals.h>
174 #include "toggle_thread_safe_R.hpp"
175 void eigen_REprintf(const char* x)CSKIP({REprintf("%s",x);})
176 
177 #include "tmbutils/tmbutils.hpp"
178 #include "tmbutils/vectorize.hpp"
179 using tmbutils::matrix;
180 using tmbutils::vector;
181 using CppAD::AD;
182 using CppAD::ADFun;
183 namespace CppAD{
184  /* Add to CppAD so that 'Variable' works for any 'Type' */
185  bool Variable(double x)CSKIP({ return false; })
186  /* Add 'isfinite', 'isinf' and 'isnan' to CppAD */
187  using std::isfinite;
188  template <class T>
189  bool isfinite(const AD<T> &x)CSKIP({ return isfinite(Value(x)); })
190  using std::isinf;
191  template <class T>
192  bool isinf(const AD<T> &x)CSKIP({ return isinf(Value(x)); })
193  using std::isnan;
194  template <class T>
195  bool isnan(const AD<T> &x)CSKIP({ return isnan(Value(x)); })
196 }
197 #include "convert.hpp" // asSEXP, asMatrix, asVector
198 #include "config.hpp"
199 #include "tmbutils/getListElement.hpp"
200 #include "atomic_math.hpp"
201 #include "expm.hpp"
202 #include "atomic_convolve.hpp"
203 #include "tiny_ad/atomic.hpp"
204 #include "tiny_ad/integrate/integrate.hpp"
205 #include "dynamic_data.hpp" // Requires atomic namespace
206 #include "Vectorize.hpp"
207 #include "dnorm.hpp" // harmless
208 #include "lgamma.hpp" // harmless
209 #include "start_parallel.hpp"
210 #include "tmbutils/newton.hpp" // Newton solver + Laplace used by TransformADFunObject
211 #ifndef TMB_SKINNY
212 #include "tmb_core.hpp"
213 #endif
214 #include "distributions_R.hpp"
215 #include "convenience.hpp" // Requires besselK
216 #include "tmbutils/tmbutils_extra.hpp"
217 #include "tmbutils/R_inla.hpp"
218 #include "tmbutils/sparse_matrix_exponential.hpp"
219 #include "tmbutils/concat.hpp"
220 #include "precompile.hpp" // Must come last
221 using tmbutils::array;
222 using Eigen::Matrix;
223 using Eigen::Array;
224 
225 /* Cleanup */
226 
227 // Nothing more to precompile
228 #undef CSKIP
229 #define CSKIP(...) __VA_ARGS__
230 #undef CSKIP_ATOMIC
231 #define CSKIP_ATOMIC(...) __VA_ARGS__
Automatic differentiation library designed for TMB.
Definition: TMB.hpp:157
Array class used by TMB.
Definition: tmbutils.hpp:23
Augmented AD type.
Definition: global.hpp:2831
Definition: TMB.hpp:136
Scalar Value() const
Return the underlying scalar value of this ad_aug.
Definition: TMBad.cpp:2188
Matrix class used by TMB.
Definition: tmbutils.hpp:102
Vector class used by TMB.
Definition: tmbutils.hpp:18
License: GPL v2