-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblNumericFunctions.hpp
More file actions
300 lines (230 loc) · 9.53 KB
/
blNumericFunctions.hpp
File metadata and controls
300 lines (230 loc) · 9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#ifndef BL_NUMERICFUNCTIONS_HPP
#define BL_NUMERICFUNCTIONS_HPP
///-------------------------------------------------------------------
///
///
///
/// PURPOSE: A collection of simple numeric functions that
/// come up all the time and might be useful
///
/// AUTHOR: Vincenzo Barbato
/// navyenzo@gmail.com
///
/// NOTE: All things in this library are defined within the
/// blMathAPI namespace
///
/// LISENSE: MIT-LICENCE
/// http://www.opensource.org/licenses/mit-license.php
///
///
///
///-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes needed for this file
//-------------------------------------------------------------------
#include <limits>
#include <complex>
#include <cstdint>
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NOTE: This class is defined within the blMathAPI namespace
//-------------------------------------------------------------------
namespace blMathAPI
{
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following functions check if
// a number is infinite or if it's not
// a number
//-------------------------------------------------------------------
template<typename blNumberType>
inline bool isNaN(const blNumberType& number)
{
return (number != number);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
template<typename blNumberType>
inline bool isInf(const blNumberType& number)
{
return (number <= std::numeric_limits<blNumberType>::min() &&
number >= std::numeric_limits<blNumberType>::max());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following functions return
// the sign of a number
//-------------------------------------------------------------------
template <typename blNumberType>
inline blNumberType sign(const blNumberType& number)
{
return static_cast<blNumberType>( (static_cast<blNumberType>(0) < number) - (number < static_cast<blNumberType>(0)) );
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::complex<blNumberType> sign(const std::complex<blNumberType>& number)
{
return std::complex<blNumberType>(blNumberType( (blNumberType(0) < number.real()) - (number.real() < blNumberType(0)) ),0);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Returns the absolute value of a function
// This function requires the argument type to
// define the < comparator and the - operator as
// it compares against zero and then negates the
// number if it is less than zero
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType abs(const blNumberType& number)
{
if(number < static_cast<blNumberType>(0))
return -number;
else
return number;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following function raises any entity to
// the power of another entity no matter the type
// For example a matrix raised to the power of an integer
// The only requirement is that the multiplication operator
// be defined for the first entity
//-------------------------------------------------------------------
template <typename blNumberType>
inline blNumberType power(const blNumberType& base,const int& exponent)
{
blNumberType result = base;
for(int i = 1; i < exponent; ++i)
result *= base;
return result;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following functions round off
// a number to the specified precision
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType roundOff(const blNumberType& number,
const int& precision)
{
blNumberType multiplier = blMathAPI::power(blNumberType(10),blNumberType(precision));
return ( blNumberType( int(multiplier * number + blNumberType(0.5)) ) / blNumberType(multiplier) );
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::complex<blNumberType> roundOff(const std::complex<blNumberType>& number,
const int& precision)
{
// For a std::complex
// number, we need to
// round off both the
// real and imaginary
// parts
return std::complex<blNumberType>(roundOff<blNumberType>(std::real(number),precision),
roundOff<blNumberType>(std::imag(number),precision));
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following functions separate
// a number into its integral part and
// its fractional part
//-------------------------------------------------------------------
template<typename blNumberType,
typename blIntegralPartType,
typename blFractionalPartType>
inline void modf(const blNumberType& number,
blIntegralPartType& integralPart,
blFractionalPartType& fractionalPart)
{
integralPart = static_cast<blIntegralPartType>(static_cast<int64_t>(number));
fractionalPart = static_cast<blFractionalPartType>(number - static_cast<blNumberType>(integralPart));
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following function calculates
// the greatest common divisor
//-------------------------------------------------------------------
template<typename blIntegerType>
inline blIntegerType gcd(const blIntegerType& a,
const blIntegerType& b)
{
if(b == 0)
return a;
else if(a == 0)
return b;
blIntegerType c = a % b;
if(c == 0)
return b;
blIntegerType d1 = b;
blIntegerType d2;
while(c > 0)
{
d2 = c;
c = d1 % c;
d1 = d2;
}
return d1;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following function calculates
// the least common multiple
//-------------------------------------------------------------------
template<typename blIntegerType>
inline blIntegerType lcm(const blIntegerType& a,
const blIntegerType& b)
{
return blMathAPI::abs(a * b) / gcd(a,b);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following function calculates if
// a number is zero
//-------------------------------------------------------------------
template<typename blNumberType>
inline bool isZero(const blNumberType& number,
const blNumberType howManyEpsilons)
{
if(blMathAPI::abs(number) <= howManyEpsilons * std::numeric_limits<blNumberType>::epsilon())
return true;
else
return false;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following functions calculates
// the factorial of a number
//-------------------------------------------------------------------
template<typename blIntegerType>
inline blIntegerType factorial(const blIntegerType& number)
{
auto result = number;
for(auto i = (number - 1); i > 1; --i)
result *= i;
return result;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// The following function maps a
// value from the input domain to
// the output range
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType linearMap(const blNumberType& valueToMap,
const blNumberType& lowInput,
const blNumberType& highInput,
const blNumberType& lowOutput,
const blNumberType& highOutput)
{
auto slope = (highOutput - lowOutput) / (highInput - lowInput);
auto intercept = highOutput - slope * highInput;
return ( slope * valueToMap + intercept );
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// End of the blMathAPI namespace
}
//-------------------------------------------------------------------
#endif // BL_NUMERICFUNCTIONS_HPP