1 : |
agomez |
1 |
#ifndef mathfunc_h
|
2 : |
|
|
#define mathfunc_h
|
3 : |
|
|
|
4 : |
|
|
#include "gadget.h" |
5 : |
|
|
|
6 : |
|
|
/**
|
7 : |
|
|
* \brief This is the value of pi used in the simulation
|
8 : |
|
|
*/
|
9 : |
|
|
const double pivalue = atan(1.0) * 4.0;
|
10 : |
|
|
|
11 : |
|
|
/**
|
12 : |
|
|
* \brief This function will calculate the maximum of 2 numbers (integers)
|
13 : |
|
|
* \param a is the first number
|
14 : |
|
|
* \param b is the second number
|
15 : |
|
|
* \return a if a is greater than b, b otherwise
|
16 : |
|
|
*/
|
17 : |
|
|
inline int max(int a, int b) {
|
18 : |
|
|
return ((a > b) ? a : b);
|
19 : |
|
|
}
|
20 : |
|
|
|
21 : |
|
|
/**
|
22 : |
|
|
* \brief This function will calculate the maximum of 2 numbers (doubles)
|
23 : |
|
|
* \param a is the first number
|
24 : |
|
|
* \param b is the second number
|
25 : |
|
|
* \return a if a is greater than b, b otherwise
|
26 : |
|
|
*/
|
27 : |
|
|
inline double max(double a, double b) {
|
28 : |
|
|
return ((a > b) ? a : b);
|
29 : |
|
|
}
|
30 : |
|
|
|
31 : |
|
|
/**
|
32 : |
|
|
* \brief This function will calculate the minimum of 2 numbers (integers)
|
33 : |
|
|
* \param a is the first number
|
34 : |
|
|
* \param b is the second number
|
35 : |
|
|
* \return a if a is less than b, b otherwise
|
36 : |
|
|
*/
|
37 : |
|
|
inline int min(int a, int b) {
|
38 : |
|
|
return ((a < b) ? a : b);
|
39 : |
|
|
}
|
40 : |
|
|
|
41 : |
|
|
/**
|
42 : |
|
|
* \brief This function will calculate the minimum of 2 numbers (doubles)
|
43 : |
|
|
* \param a is the first number
|
44 : |
|
|
* \param b is the second number
|
45 : |
|
|
* \return a if a is less than b, b otherwise
|
46 : |
|
|
*/
|
47 : |
|
|
inline double min(double a, double b) {
|
48 : |
|
|
return ((a < b) ? a : b);
|
49 : |
|
|
}
|
50 : |
|
|
|
51 : |
|
|
/**
|
52 : |
|
|
* \brief This function will check to see if a number is very close to zero
|
53 : |
|
|
* \param a is the number that is being checked
|
54 : |
|
|
* \return 1 if the number is very close to zero, 0 otherwise
|
55 : |
|
|
* \note This function replaces 'a == 0' to take account of numerical inaccuracies when calculating the exact value of a double
|
56 : |
|
|
*/
|
57 : |
|
|
inline int isZero(double a) {
|
58 : |
|
|
return ((fabs(a) < verysmall) ? 1 : 0);
|
59 : |
|
|
}
|
60 : |
|
|
|
61 : |
|
|
/**
|
62 : |
|
|
* \brief This function will check to see if two numbers are equal
|
63 : |
|
|
* \param a is the first number that is being checked
|
64 : |
|
|
* \param b is the second number that is being checked
|
65 : |
|
|
* \return 1 if the two numbers are equal, 0 otherwise
|
66 : |
|
|
* \note This function replaces 'a == b' to take account of numerical inaccuracies when calculating the exact value of a double
|
67 : |
|
|
*/
|
68 : |
|
|
inline int isEqual(double a, double b) {
|
69 : |
|
|
return ((fabs(a - b) < verysmall) ? 1 : 0);
|
70 : |
|
|
}
|
71 : |
|
|
|
72 : |
|
|
/**
|
73 : |
|
|
* \brief This function will check to see if a number is rather small (or zero)
|
74 : |
|
|
* \param a is the number that is being checked
|
75 : |
|
|
* \return 1 if the number is rather small, 0 otherwise
|
76 : |
|
|
*/
|
77 : |
|
|
inline int isSmall(double a) {
|
78 : |
|
|
return ((fabs(a) < rathersmall) ? 1 : 0);
|
79 : |
|
|
}
|
80 : |
|
|
|
81 : |
|
|
/**
|
82 : |
|
|
* \brief This function will calculate the value of the logarithm of n factorial
|
83 : |
|
|
* \param n is the number that the logfactorial will be calculated for
|
84 : |
|
|
* \return the value of log(n!)
|
85 : |
|
|
* \note This function is not an ANSI function and so will not compile correctly if gadget is compiled with the \c -ansi flag, unless we include \c \#define \c __GNU_SOURCE in this file
|
86 : |
|
|
*/
|
87 : |
|
|
inline double logFactorial(double n) {
|
88 : |
|
|
return lgamma(n + 1.0);
|
89 : |
|
|
}
|
90 : |
|
|
|
91 : |
|
|
/**
|
92 : |
|
|
* \brief This function will calculate the value of the exponential of n
|
93 : |
|
|
* \param n is the number that the exponential will be calculated for
|
94 : |
|
|
* \return the value of exp (n)
|
95 : |
|
|
* \note This function replaces 'exp' to return a value in the range 0.0 to 1.0
|
96 : |
|
|
*/
|
97 : |
|
|
inline double expRep(double n) {
|
98 : |
|
|
if (n > verysmall)
|
99 : |
|
|
return 1.0;
|
100 : |
|
|
else if (n < -25.0)
|
101 : |
|
|
return rathersmall;
|
102 : |
|
|
return exp(n);
|
103 : |
|
|
}
|
104 : |
|
|
|
105 : |
|
|
/**
|
106 : |
|
|
* \brief This function will generate a random number in the range 0.0 to 1.0
|
107 : |
|
|
* \return random number
|
108 : |
|
|
* \note This function generates uniformly-distributed doubles in the range 0.0 to 1.0
|
109 : |
|
|
*/
|
110 : |
|
|
inline double randomNumber() {
|
111 : |
|
|
int r = rand();
|
112 : |
|
|
double k = r % 32767;
|
113 : |
|
|
return (k / 32767.0);
|
114 : |
|
|
}
|
115 : |
|
|
|
116 : |
ulcessvp |
12 |
/**
|
117 : |
|
|
* \brief This function will generate a random number in the range 0.0 to 1.0 from a seed
|
118 : |
|
|
* \param seed the seed used to calculate the rando number
|
119 : |
|
|
* \return random number
|
120 : |
|
|
* \note This function generates uniformly-distributed doubles in the range 0.0 to 1.0
|
121 : |
|
|
*/
|
122 : |
ulcessvp |
11 |
inline double randomNumber(unsigned* seed) {
|
123 : |
|
|
int r = rand_r(seed);
|
124 : |
|
|
double k = r % 32767;
|
125 : |
|
|
return (k / 32767.0);
|
126 : |
|
|
}
|
127 : |
|
|
|
128 : |
agomez |
1 |
/**
|
129 : |
|
|
* \brief This function will calculate the calculate the effective annual mortality caused by a given predation on a specified population during a timestep
|
130 : |
|
|
* \param pred is the number that is removed from the population by the predation
|
131 : |
|
|
* \param pop is the population size (before predation)
|
132 : |
|
|
* \param t is the inverse of proportion of the year covered by the current timestep
|
133 : |
|
|
* \return calculated mortality
|
134 : |
|
|
*/
|
135 : |
|
|
inline double calcMortality(double pred, double pop, double t) {
|
136 : |
|
|
if (pred < verysmall)
|
137 : |
|
|
return 0.0;
|
138 : |
|
|
else if (pred > pop)
|
139 : |
|
|
return verybig;
|
140 : |
|
|
return (-log(1.0 - (pred / pop)) * t);
|
141 : |
|
|
}
|
142 : |
|
|
|
143 : |
|
|
#endif
|