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))
135# define NonNeg(x) ((x) >= 0 ? (x) : 0)
144template <
typename T1,
typename T2>
152float Expo(
float xx,
float upper = 20.f,
float lower = -20.f);
157float Power(
float a,
float n);
177bool IsVectorAllSame(
const vector<T>* a, T value);
180bool IsValueInVector(
const vector<T>* a, T value);
190T
Sum(
int row,
const T* data);
201T
Sum(
int row,
int*& idx,
const T* data);
224 double*** derivedvalues, T exclude =
static_cast<T
>(
NODATA_VALUE));
244#if defined(USE_APPR_PAL_MATH)
259static inline T __p_exp_ln2(
const T x) {
260 const T a1 =
static_cast<T
>(-0.9998684);
261 const T a2 =
static_cast<T
>(0.4982926);
262 const T a3 =
static_cast<T
>(-0.1595332);
263 const T a4 =
static_cast<T
>(0.0293641);
264 T exp_x =
static_cast<T
>(1.0) +
269 return static_cast<T
>(1.0) / exp_x;
278static inline T __p_exp_pos(
const T x) {
280 static const T ln2 =
static_cast<T
>(M_LN2);
284 x_ = x -
static_cast<T
>(k) * ln2;
285 return static_cast<T
>(twok) * __p_exp_ln2(x_);
289static inline T ApprExp(
const T x) {
290 if (x >=
static_cast<T
>(0.0))
291 return __p_exp_pos(x);
293 return static_cast<T
>(1.0) / __p_exp_pos(-x);
298#if defined(USE_APPR_PAL_MATH)
314#if defined(USE_APPR_PAL_MATH)
326float pow_lookup(
const float exp,
const float log_base);
339template<
typename T1,
typename T2>
340double CalPow(T1 a, T2 b) {
341#if defined(USE_APPR_PAL_MATH)
352 for (
int i = 1; i < n; i++) {
363 for (
int i = 1; i < n; i++) {
372bool IsVectorAllSame(
const vector<T>* a, T value){
373 for (
int i = 0; i < a->size(); i++) {
374 if (a->at(i) != value) {
382bool IsValueInVector(
const vector<T>* a, T value){
383 return std::find(a->begin(), a->end(), value) != a->end();
386bool IsValueInVectorVector(
const vector< vector<T>>* a, T value) {
387 for (
int i = 0; i < a->size(); i++) {
388 if (std::find(a->at(i).begin(), a->at(i).end(), value) != a->at(i).end()) {
395T
Sum(
const int row,
const T* data) {
397#pragma omp parallel for reduction(+:tmp)
398 for (
int i = 0; i < row; i++) {
405T
Sum(
const int row,
int*& idx,
const T* data) {
407#pragma omp parallel for reduction(+:tmp)
408 for (
int i = 0; i < row; i++) {
418 double* tmpstats =
new double[6];
424 for (
int i = 0; i < num; i++) {
426 if (maxv < values[i]) maxv = values[i];
427 if (minv > values[i]) minv = values[i];
431 tmpstats[0] =
CVT_DBL(validnum);
432 double mean = sumv / tmpstats[0];
433#pragma omp parallel for reduction(+:std)
434 for (
int i = 0; i < num; i++) {
436 std += (values[i] - mean) * (values[i] - mean);
439 std = sqrt(std / tmpstats[0]);
444 tmpstats[5] = maxv - minv;
445 *derivedvalues = tmpstats;
450 double*** derivedvalues, T exclude ) {
451 double** tmpstats =
new double *[6];
452 for (
int i = 0; i < 6; i++) {
453 tmpstats[i] =
new double[lyrs];
455 for (
int j = 0; j < lyrs; j++) {
463 double* sumv =
nullptr;
465 for (
int i = 0; i < num; i++) {
466 for (
int j = 0; j < lyrs; j++) {
467 if (
FloatEqual(values[i][j], exclude))
continue;
468 if (tmpstats[2][j] < values[i][j]) tmpstats[2][j] = values[i][j];
469 if (tmpstats[3][j] > values[i][j]) tmpstats[3][j] = values[i][j];
471 sumv[j] += values[i][j];
475 for (
int j = 0; j < lyrs; j++) {
476 tmpstats[5][j] = tmpstats[2][j] - tmpstats[3][j];
477 tmpstats[1][j] = sumv[j] / tmpstats[0][j];
479 for (
int j = 0; j < lyrs; j++) {
481#pragma omp parallel for reduction(+:tmpstd)
482 for (
int i = 0; i < num; i++) {
484 tmpstd += (values[i][j] - tmpstats[1][j]) * (values[i][j] - tmpstats[1][j]);
487 tmpstats[4][j] = tmpstd;
489 for (
int j = 0; j < lyrs; j++) {
490 tmpstats[4][j] = sqrt(tmpstats[4][j] / tmpstats[0][j]);
493 *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:476
bool Initialize1DArray(int row, T *&data, INI_T init_value)
Initialize DT_Array1D data.
Definition: utils_array.h:344
T MaxInArray(const T *a, int n)
Get maximum value in a numeric array with size n.
Definition: utils_math.h:350
float ApprPow(float a, float b)
Approximates pow(a, b) based on the work of Harrison Ainsworth.
Definition: utils_math.h:334
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:361
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:395
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:416
bool FloatEqual(T1 v1, T2 v2)
Whether v1 is equal to v2.
Definition: utils_math.h:145
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