FIMS  v0.10.0
Loading...
Searching...
No Matches
fims_math.hpp
Go to the documentation of this file.
1
8#ifndef FIMS_MATH_HPP
9#define FIMS_MATH_HPP
10
11// note: this is modeling platform specific, must be controlled by
12// preprocessing macros
13#include <cmath>
14#include <random>
15#include <sstream>
16
17#include "../interface/interface.hpp"
18#include "fims_vector.hpp"
19#include "def.hpp"
20
21namespace fims_math {
22#ifdef STD_LIB
23
31template <class Type>
32inline const Type exp(const Type &x) {
33 return std::exp(x);
34}
35
42template <class Type>
43inline const Type log(const Type &x) {
44 return std::log(x);
45}
46
47template <class Type>
48inline const Type cos(const Type &x) {
49 return std::cos(x);
50}
51
52template <class Type>
53inline const Type sqrt(const Type &x) {
54 return std::sqrt(x);
55}
56
57template <class Type>
58inline const Type pow(const Type &x, const Type &y) {
59 return std::pow(x, y);
60}
61
62template <class Type>
63inline const Type lgamma(const Type &x) {
64 return std::lgamma(x);
65}
66
67inline double Value(const double &x) { return x; }
68
78template <class Type>
79inline const Type pnorm(const Type &x, const Type &mean, const Type &sd) {
80 const double z = static_cast<double>(x - mean) /
81 (static_cast<double>(sd) * std::sqrt(2.0));
82 return static_cast<Type>(0.5 * (1.0 + std::erf(z)));
83}
84
85#endif
86
87#ifdef TMB_MODEL
88
89inline double Value(const TMBAD_FIMS_TYPE &x) { return x.Value(); }
90
91inline double Value(const double &x) { return x; }
92
93// Add the following line to CMakeLists.txt to enable documentation of TMB_MODEL
94// in doxygen or none of the following is rendered.
95// set(DOXYGEN_PREDEFINED "TMB_MODEL=1" "ENABLE_TMB_CODE")
96
108template <class Type>
109inline const Type exp(const Type &x) {
110 // use std::exp for double type, look for TMB version of exp if AD type
111 using std::exp;
112 return exp(x);
113}
114
127template <class Type>
128inline const Type log(const Type &x) {
129 // use std::log for double type, look for TMB version of log if AD type
130 using std::log;
131 return log(x);
132}
133
146template <class Type>
147inline const Type cos(const Type &x) {
148 // use std::cos for double type, look for TMB version of cos if AD type
149 using std::cos;
150 return cos(x);
151}
152
164template <class Type>
165inline const Type sqrt(const Type &x) {
166 // use std::std for double type, look for TMB version of std if AD type
167 using std::sqrt;
168 return sqrt(x);
169}
170
183template <class Type>
184inline const Type pow(const Type &x, const Type &y) {
185 // use std::pow for double type, look for TMB version of pow if AD type
186 using std::pow;
187 return pow(x, y);
188}
189
205template <class Type>
206inline const Type lgamma(const Type &x) {
207 // use std::lgamma for double type, look for TMB version of lgamma if AD type
208 using std::lgamma;
209 return lgamma(x);
210}
211
215template <class Type>
216inline const Type pnorm(const Type &x, const Type &mean, const Type &sd) {
217 // Use :: to call TMB's global pnorm; an unqualified call recurses into this
218 // wrapper and can hang CreateTMBModel() when growth uses normal
219 // probabilities.
220 return ::pnorm(x, mean, sd);
221}
222
223#endif
224
242template <class Type>
243inline const Type logistic(const Type &inflection_point, const Type &slope,
244 const Type &x) {
245 return static_cast<Type>(1.0) /
246 (static_cast<Type>(1.0) +
247 exp(Type(-1.0) * slope * (x - inflection_point)));
248}
249
260template <class Type>
261inline const Type logit(const Type &a, const Type &b, const Type &x) {
262 return -fims_math::log(b - x) + fims_math::log(x - a);
263}
264
275template <class Type>
276inline const Type inv_logit(const Type &a, const Type &b, const Type &logit_x) {
277 return a + (b - a) / (static_cast<Type>(1.0) + fims_math::exp(-logit_x));
278}
279
300template <class Type>
301inline const Type double_logistic(const Type &inflection_point_asc,
302 const Type &slope_asc,
303 const Type &inflection_point_desc,
304 const Type &slope_desc, const Type &x) {
305 return (static_cast<Type>(1.0)) /
306 (static_cast<Type>(1.0) +
307 exp(Type(-1.0) * slope_asc * (x - inflection_point_asc))) *
308 (static_cast<Type>(1.0) -
309 (static_cast<Type>(1.0)) /
310 (static_cast<Type>(1.0) +
311 exp(Type(-1.0) * slope_desc * (x - inflection_point_desc))));
312}
313
327template <class Type>
328const Type ad_fabs(const Type &x, Type C = 1e-5) {
329 return sqrt((x * x) + C);
330}
331
347template <typename Type>
348inline const Type ad_min(const Type &a, const Type &b, Type C = 1e-5) {
349 return (a + b - fims_math::ad_fabs(a - b, C)) * static_cast<Type>(0.5);
350}
351
364template <typename Type>
365inline const Type ad_max(const Type &a, const Type &b, Type C = 1e-5) {
366 return (a + b + fims_math::ad_fabs(a - b, C)) * static_cast<Type>(.5);
367}
368
377template <class T>
378T sum(const std::vector<T> &v) {
379 T ret = 0.0;
380 for (size_t i = 0; i < v.size(); i++) {
381 ret += v[i];
382 }
383 return ret;
384}
385
394template <class T>
395T sum(const fims::Vector<T> &v) {
396 T ret = 0.0;
397 for (size_t i = 0; i < v.size(); i++) {
398 ret += v[i];
399 }
400 return ret;
401}
402
403} // namespace fims_math
404
405#endif /* FIMS_MATH_HPP */
Definition fims_vector.hpp:27
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:299
Platform macros and the core FIMS logging system.
const Type logit(const Type &a, const Type &b, const Type &x)
A logit function for bounding of parameters.
Definition fims_math.hpp:261
T sum(const std::vector< T > &v)
Definition fims_math.hpp:378
const Type ad_min(const Type &a, const Type &b, Type C=1e-5)
Definition fims_math.hpp:348
const Type double_logistic(const Type &inflection_point_asc, const Type &slope_asc, const Type &inflection_point_desc, const Type &slope_desc, const Type &x)
The general double logistic function.
Definition fims_math.hpp:301
const Type ad_max(const Type &a, const Type &b, Type C=1e-5)
Definition fims_math.hpp:365
const Type inv_logit(const Type &a, const Type &b, const Type &logit_x)
An inverse logit function for bounding of parameters.
Definition fims_math.hpp:276
const Type ad_fabs(const Type &x, Type C=1e-5)
Definition fims_math.hpp:328
const Type logistic(const Type &inflection_point, const Type &slope, const Type &x)
The general logistic function.
Definition fims_math.hpp:243
Establishes the FIMS Vector class.