FIMS  v0.8.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
20namespace fims_math {
21#ifdef STD_LIB
22
30template <class Type>
31inline const Type exp(const Type &x) {
32 return std::exp(x);
33}
34
41template <class Type>
42inline const Type log(const Type &x) {
43 return std::log(x);
44}
45
46template <class Type>
47inline const Type cos(const Type &x) {
48 return std::cos(x);
49}
50
51template <class Type>
52inline const Type sqrt(const Type &x) {
53 return std::sqrt(x);
54}
55
56template <class Type>
57inline const Type pow(const Type &x, const Type &y) {
58 return std::pow(x, y);
59}
60
61template <class Type>
62inline const Type lgamma(const Type &x) {
63 return std::lgamma(x);
64}
65#endif
66
67#ifdef TMB_MODEL
68
69// Add the following line to CMakeLists.txt to enable documentation of TMB_MODEL
70// in doxygen or none of the following is rendered.
71// set(DOXYGEN_PREDEFINED "TMB_MODEL=1" "ENABLE_TMB_CODE")
72
84template <class Type>
85inline const Type exp(const Type &x) {
86 // use std::exp for double type, look for TMB version of exp if AD type
87 using std::exp;
88 return exp(x);
89}
90
103template <class Type>
104inline const Type log(const Type &x) {
105 // use std::log for double type, look for TMB version of log if AD type
106 using std::log;
107 return log(x);
108}
109
122template <class Type>
123inline const Type cos(const Type &x) {
124 // use std::cos for double type, look for TMB version of cos if AD type
125 using std::cos;
126 return cos(x);
127}
128
140template <class Type>
141inline const Type sqrt(const Type &x) {
142 // use std::std for double type, look for TMB version of std if AD type
143 using std::sqrt;
144 return sqrt(x);
145}
146
159template <class Type>
160inline const Type pow(const Type &x, const Type &y) {
161 // use std::pow for double type, look for TMB version of pow if AD type
162 using std::pow;
163 return pow(x, y);
164}
165
181template <class Type>
182inline const Type lgamma(const Type &x) {
183 // use std::lgamma for double type, look for TMB version of lgamma if AD type
184 using std::lgamma;
185 return lgamma(x);
186}
187
188#endif
189
207template <class Type>
208inline const Type logistic(const Type &inflection_point, const Type &slope,
209 const Type &x) {
210 return static_cast<Type>(1.0) /
211 (static_cast<Type>(1.0) +
212 exp(Type(-1.0) * slope * (x - inflection_point)));
213}
214
225template <class Type>
226inline const Type logit(const Type &a, const Type &b, const Type &x) {
227 return -fims_math::log(b - x) + fims_math::log(x - a);
228}
229
240template <class Type>
241inline const Type inv_logit(const Type &a, const Type &b, const Type &logit_x) {
242 return a + (b - a) / (static_cast<Type>(1.0) + fims_math::exp(-logit_x));
243}
244
265template <class Type>
266inline const Type double_logistic(const Type &inflection_point_asc,
267 const Type &slope_asc,
268 const Type &inflection_point_desc,
269 const Type &slope_desc, const Type &x) {
270 return (static_cast<Type>(1.0)) /
271 (static_cast<Type>(1.0) +
272 exp(Type(-1.0) * slope_asc * (x - inflection_point_asc))) *
273 (static_cast<Type>(1.0) -
274 (static_cast<Type>(1.0)) /
275 (static_cast<Type>(1.0) +
276 exp(Type(-1.0) * slope_desc * (x - inflection_point_desc))));
277}
278
292template <class Type>
293const Type ad_fabs(const Type &x, Type C = 1e-5) {
294 return sqrt((x * x) + C);
295}
296
312template <typename Type>
313inline const Type ad_min(const Type &a, const Type &b, Type C = 1e-5) {
314 return (a + b - fims_math::ad_fabs(a - b, C)) * static_cast<Type>(0.5);
315}
316
329template <typename Type>
330inline const Type ad_max(const Type &a, const Type &b, Type C = 1e-5) {
331 return (a + b + fims_math::ad_fabs(a - b, C)) * static_cast<Type>(.5);
332}
333
342template <class T>
343T sum(const std::vector<T> &v) {
344 T ret = 0.0;
345 for (size_t i = 0; i < v.size(); i++) {
346 ret += v[i];
347 }
348 return ret;
349}
350
359template <class T>
360T sum(const fims::Vector<T> &v) {
361 T ret = 0.0;
362 for (size_t i = 0; i < v.size(); i++) {
363 ret += v[i];
364 }
365 return ret;
366}
367
368} // namespace fims_math
369
370#endif /* FIMS_MATH_HPP */
Definition fims_vector.hpp:27
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:273
const Type logit(const Type &a, const Type &b, const Type &x)
A logit function for bounding of parameters.
Definition fims_math.hpp:226
T sum(const std::vector< T > &v)
Definition fims_math.hpp:343
const Type ad_min(const Type &a, const Type &b, Type C=1e-5)
Definition fims_math.hpp:313
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:266
const Type ad_max(const Type &a, const Type &b, Type C=1e-5)
Definition fims_math.hpp:330
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:241
const Type ad_fabs(const Type &x, Type C=1e-5)
Definition fims_math.hpp:293
const Type logistic(const Type &inflection_point, const Type &slope, const Type &x)
The general logistic function.
Definition fims_math.hpp:208
Establishes the FIMS Vector class.