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#endif
69
70#ifdef TMB_MODEL
71
72inline double Value(const TMBAD_FIMS_TYPE &x) { return x.Value(); }
73
74inline double Value(const double &x) { return x; }
75
76// Add the following line to CMakeLists.txt to enable documentation of TMB_MODEL
77// in doxygen or none of the following is rendered.
78// set(DOXYGEN_PREDEFINED "TMB_MODEL=1" "ENABLE_TMB_CODE")
79
91template <class Type>
92inline const Type exp(const Type &x) {
93 // use std::exp for double type, look for TMB version of exp if AD type
94 using std::exp;
95 return exp(x);
96}
97
110template <class Type>
111inline const Type log(const Type &x) {
112 // use std::log for double type, look for TMB version of log if AD type
113 using std::log;
114 return log(x);
115}
116
129template <class Type>
130inline const Type cos(const Type &x) {
131 // use std::cos for double type, look for TMB version of cos if AD type
132 using std::cos;
133 return cos(x);
134}
135
147template <class Type>
148inline const Type sqrt(const Type &x) {
149 // use std::std for double type, look for TMB version of std if AD type
150 using std::sqrt;
151 return sqrt(x);
152}
153
166template <class Type>
167inline const Type pow(const Type &x, const Type &y) {
168 // use std::pow for double type, look for TMB version of pow if AD type
169 using std::pow;
170 return pow(x, y);
171}
172
188template <class Type>
189inline const Type lgamma(const Type &x) {
190 // use std::lgamma for double type, look for TMB version of lgamma if AD type
191 using std::lgamma;
192 return lgamma(x);
193}
194
195#endif
196
214template <class Type>
215inline const Type logistic(const Type &inflection_point, const Type &slope,
216 const Type &x) {
217 return static_cast<Type>(1.0) /
218 (static_cast<Type>(1.0) +
219 exp(Type(-1.0) * slope * (x - inflection_point)));
220}
221
232template <class Type>
233inline const Type logit(const Type &a, const Type &b, const Type &x) {
234 return -fims_math::log(b - x) + fims_math::log(x - a);
235}
236
247template <class Type>
248inline const Type inv_logit(const Type &a, const Type &b, const Type &logit_x) {
249 return a + (b - a) / (static_cast<Type>(1.0) + fims_math::exp(-logit_x));
250}
251
272template <class Type>
273inline const Type double_logistic(const Type &inflection_point_asc,
274 const Type &slope_asc,
275 const Type &inflection_point_desc,
276 const Type &slope_desc, const Type &x) {
277 return (static_cast<Type>(1.0)) /
278 (static_cast<Type>(1.0) +
279 exp(Type(-1.0) * slope_asc * (x - inflection_point_asc))) *
280 (static_cast<Type>(1.0) -
281 (static_cast<Type>(1.0)) /
282 (static_cast<Type>(1.0) +
283 exp(Type(-1.0) * slope_desc * (x - inflection_point_desc))));
284}
285
299template <class Type>
300const Type ad_fabs(const Type &x, Type C = 1e-5) {
301 return sqrt((x * x) + C);
302}
303
319template <typename Type>
320inline const Type ad_min(const Type &a, const Type &b, Type C = 1e-5) {
321 return (a + b - fims_math::ad_fabs(a - b, C)) * static_cast<Type>(0.5);
322}
323
336template <typename Type>
337inline const Type ad_max(const Type &a, const Type &b, Type C = 1e-5) {
338 return (a + b + fims_math::ad_fabs(a - b, C)) * static_cast<Type>(.5);
339}
340
349template <class T>
350T sum(const std::vector<T> &v) {
351 T ret = 0.0;
352 for (size_t i = 0; i < v.size(); i++) {
353 ret += v[i];
354 }
355 return ret;
356}
357
366template <class T>
367T sum(const fims::Vector<T> &v) {
368 T ret = 0.0;
369 for (size_t i = 0; i < v.size(); i++) {
370 ret += v[i];
371 }
372 return ret;
373}
374
375} // namespace fims_math
376
377#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:233
T sum(const std::vector< T > &v)
Definition fims_math.hpp:350
const Type ad_min(const Type &a, const Type &b, Type C=1e-5)
Definition fims_math.hpp:320
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:273
const Type ad_max(const Type &a, const Type &b, Type C=1e-5)
Definition fims_math.hpp:337
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:248
const Type ad_fabs(const Type &x, Type C=1e-5)
Definition fims_math.hpp:300
const Type logistic(const Type &inflection_point, const Type &slope, const Type &x)
The general logistic function.
Definition fims_math.hpp:215
Establishes the FIMS Vector class.