12#ifndef CCGL_UTILS_MATH_H
13#define CCGL_UTILS_MATH_H
20#define M_E 2.7182818284590452354
23#define M_LOG2E 1.4426950408889634074
26#define M_LOG10E 0.43429448190325182765
29#define M_LN2 0.69314718055994530942
32#define M_LN10 2.30258509299404568402
35#define M_PI 3.14159265358979323846
38#define M_PI_2 1.57079632679489661923
41#define M_PI_4 0.78539816339744830962
44#define M_1_PI 0.31830988618379067154
47#define M_2_PI 0.63661977236758134308
50#define M_2_SQRTPI 1.12837916709551257390
53#define M_SQRT2 1.41421356237309504880
56#define M_SQRT1_2 0.70710678118654752440
59#define M_2POW23 8388608.0f
63#define HUGE ((float)3.40282347e+38)
66#define MAXFLOAT ((float)3.40282347e+38)
69#define MINFLOAT ((float)1.175494351e-38)
74#define M_SQRT3 1.732051f
77#define M_TC 0.63212055882855767840f
78#define M_PI2 6.283185f
79#define M_GOLDEN 1.618034f
80#define M_1_PI2 0.15915494309189534561f
84#define M_DIV3 0.3333333333333333333f
87#define M_DIV6 0.1666666666666666666f
88#define M_DIV7 0.142857143f
90#define M_DIV9 0.1111111111111111111f
92#define M_DIV11 0.090909091f
93#define M_DIV12 0.0833333333333333333f
94#define M_DIV13 0.076923077f
95#define M_DIV14 0.071428571f
96#define M_DIV15 0.0666666666666666666f
97#define M_DIV16 0.0625f
98#define M_DIV_LN10 0.43429448190325182765f
101#define PH_C ((float)299792458)
102#define PH_M0 ((float)1.2566370614359172950)
103#define PH_H ((float)6.62606957e-34)
104#define PH_HBAR ((float)1.05457172e-34)
105#define PH_K ((float)1.3806488e-23)
106#define PH_ME ((float)9.10938291e-31)
107#define PH_MP ((float)1.672614e-27)
108#define PH_MN ((float)1.674920e-27)
109#define PH_EC ((float)1.6021917e-19)
110#define PH_F ((float)9.648670e4)
111#define PH_G ((float)6.6732e-11)
112#define PH_AVO ((float)6.022169e23)
120namespace utils_math {
123#define Max(a, b) ((a) >= (b) ? (a) : (b))
127#define Min(a, b) ((a) >= (b) ? (b) : (a))
131#define Abs(x) ((x) >= 0 ? (x) : -(x))
140template <
typename T1,
typename T2>
148float Expo(
float xx,
float upper = 20.f,
float lower = -20.f);
153float Power(
float a,
float n);
179T
Sum(
int row,
const T* data);
190T
Sum(
int row,
int*& idx,
const T* data);
213 double*** derivedvalues, T exclude =
static_cast<T
>(
NODATA_VALUE));
233#if defined(USE_APPR_PAL_MATH)
248static inline T __p_exp_ln2(
const T x) {
249 const T a1 =
static_cast<T
>(-0.9998684);
250 const T a2 =
static_cast<T
>(0.4982926);
251 const T a3 =
static_cast<T
>(-0.1595332);
252 const T a4 =
static_cast<T
>(0.0293641);
253 T exp_x =
static_cast<T
>(1.0) +
258 return static_cast<T
>(1.0) / exp_x;
267static inline T __p_exp_pos(
const T x) {
269 static const T ln2 =
static_cast<T
>(M_LN2);
273 x_ = x -
static_cast<T
>(k) * ln2;
274 return static_cast<T
>(twok) * __p_exp_ln2(x_);
278static inline T ApprExp(
const T x) {
279 if (x >=
static_cast<T
>(0.0))
280 return __p_exp_pos(x);
282 return static_cast<T
>(1.0) / __p_exp_pos(-x);
287#if defined(USE_APPR_PAL_MATH)
303#if defined(USE_APPR_PAL_MATH)
315float pow_lookup(
const float exp,
const float log_base);
328template<
typename T1,
typename T2>
329double CalPow(T1 a, T2 b) {
330#if defined(USE_APPR_PAL_MATH)
341 for (
int i = 1; i < n; i++) {
352 for (
int i = 1; i < n; i++) {
361T
Sum(
const int row,
const T* data) {
363#pragma omp parallel for reduction(+:tmp)
364 for (
int i = 0; i < row; i++) {
371T
Sum(
const int row,
int*& idx,
const T* data) {
373#pragma omp parallel for reduction(+:tmp)
374 for (
int i = 0; i < row; i++) {
384 double* tmpstats =
new double[6];
390 for (
int i = 0; i < num; i++) {
392 if (maxv < values[i]) maxv = values[i];
393 if (minv > values[i]) minv = values[i];
397 tmpstats[0] =
CVT_DBL(validnum);
398 double mean = sumv / tmpstats[0];
399#pragma omp parallel for reduction(+:std)
400 for (
int i = 0; i < num; i++) {
402 std += (values[i] - mean) * (values[i] - mean);
405 std = sqrt(std / tmpstats[0]);
410 tmpstats[5] = maxv - minv;
411 *derivedvalues = tmpstats;
416 double*** derivedvalues, T exclude ) {
417 double** tmpstats =
new double *[6];
418 for (
int i = 0; i < 6; i++) {
419 tmpstats[i] =
new double[lyrs];
421 for (
int j = 0; j < lyrs; j++) {
429 double* sumv =
nullptr;
431 for (
int i = 0; i < num; i++) {
432 for (
int j = 0; j < lyrs; j++) {
433 if (
FloatEqual(values[i][j], exclude))
continue;
434 if (tmpstats[2][j] < values[i][j]) tmpstats[2][j] = values[i][j];
435 if (tmpstats[3][j] > values[i][j]) tmpstats[3][j] = values[i][j];
437 sumv[j] += values[i][j];
441 for (
int j = 0; j < lyrs; j++) {
442 tmpstats[5][j] = tmpstats[2][j] - tmpstats[3][j];
443 tmpstats[1][j] = sumv[j] / tmpstats[0][j];
445 for (
int j = 0; j < lyrs; j++) {
447#pragma omp parallel for reduction(+:tmpstd)
448 for (
int i = 0; i < num; i++) {
450 tmpstd += (values[i][j] - tmpstats[1][j]) * (values[i][j] - tmpstats[1][j]);
453 tmpstats[4][j] = tmpstd;
455 for (
int j = 0; j < lyrs; j++) {
456 tmpstats[4][j] = sqrt(tmpstats[4][j] / tmpstats[0][j]);
459 *derivedvalues = tmpstats;
#define MAXIMUMFLOAT
Maximum float value.
Definition: basic.h:255
#define MISSINGFLOAT
Missing float value.
Definition: basic.h:250
#define CVT_DBL(param)
Convert to double double
Definition: basic.h:331
#define NODATA_VALUE
Global utility definitions.
Definition: basic.h:245
#define CVT_FLT(param)
Convert to float float
Definition: basic.h:329
void Release1DArray(T *&data)
Release DT_Array1D data.
Definition: utils_array.h:460
bool Initialize1DArray(int row, T *&data, INI_T init_value)
Initialize DT_Array1D data.
Definition: utils_array.h:331
T MaxInArray(const T *a, int n)
Get maximum value in a numeric array with size n.
Definition: utils_math.h:339
float ApprPow(float a, float b)
Approximates pow(a, b) based on the work of Harrison Ainsworth.
Definition: utils_math.h:323
float Expo(float xx, float upper, float lower)
Check the argument against upper and lower boundary values prior to doing Exponential function.
Definition: utils_math.cpp:5
float pow_lookup(const float exp, const float log_base)
lookup for pow(a, b) function
Definition: utils_math.cpp:356
T MinInArray(const T *a, int n)
Get minimum value in a numeric array with size n.
Definition: utils_math.h:350
T Sum(int row, const T *data)
Sum of a numeric array Get sum value of a double array with size row.
Definition: utils_math.h:361
float ApprSqrt(const float z)
approximate sqrt
Definition: utils_math.cpp:18
float Power(const float a, const float n)
deal with positive and negative float numbers
Definition: utils_math.cpp:11
float ApprLn(const float z)
Approximates the natural logarithm, (where the base is 'e'=2.71828)
Definition: utils_math.cpp:59
void BasicStatistics(const T *values, int num, double **derivedvalues, T exclude=static_cast< T >(NODATA_VALUE))
calculate basic statistics at one time_funcs
Definition: utils_math.h:382
bool FloatEqual(T1 v1, T2 v2)
Whether v1 is equal to v2.
Definition: utils_math.h:141
Common Cross-platform Geographic Library (CCGL)
Definition: basic.cpp:17
Template functions to initialize and release arrays.
#define Abs(x)
Return absolute value.
Definition: utils_math.h:131