before formatting

This commit is contained in:
2022-12-14 13:56:19 +03:00
parent c74ba871cd
commit 430a41fefc
14 changed files with 912 additions and 623 deletions

View File

@@ -3,7 +3,7 @@
* \~\brief
* \~english Basic mathematical functions and defines
* \~russian Базовые математические функции и дефайны
*/
*/
/*
PIP - Platform Independent Primitives
Basic mathematical functions and defines
@@ -27,8 +27,8 @@
#define PIMATHBASE_H
#include "piinit.h"
#include "pivector.h"
#include "pipair.h"
#include "pivector.h"
#ifdef QNX
# undef PIP_MATH_J0
# undef PIP_MATH_J1
@@ -94,20 +94,25 @@
#endif
const double deg2rad = M_PI_180;
const double rad2deg = M_180_PI;
// clang-format off
inline int sign(const float & x) {return (x < 0.f) ? -1 : (x > 0.f ? 1 : 0);}
inline int sign(const double & x) {return (x < 0. ) ? -1 : (x > 0. ? 1 : 0);}
inline int sign(const ldouble & x) {return (x < 0.L) ? -1 : (x > 0.L ? 1 : 0);}
inline int pow2(const int p) {return 1 << p;}
inline float pow10(const int & e) {return powf(10.f, e);}
inline double pow10(const double & e) {return pow(10., e);}
inline int pow2 (const int p ) {return 1 << p;}
inline float pow10(const int & e) {return powf(10.f, e);}
inline double pow10(const double & e) {return pow (10. , e);}
inline ldouble pow10(const ldouble & e) {return powl(10.L, e);}
// clang-format on
inline double sinc(const double & v) {if (v == 0.) return 1.; double t = M_PI * v; return sin(t) / t;}
inline double sinc(const double & v) {
if (v == 0.) return 1.;
double t = M_PI * v;
return sin(t) / t;
}
PIP_EXPORT double piJ0(const double & v);
PIP_EXPORT double piJ1(const double & v);
@@ -116,22 +121,34 @@ PIP_EXPORT double piY0(const double & v);
PIP_EXPORT double piY1(const double & v);
PIP_EXPORT double piYn(int n, const double & v);
// clang-format off
inline constexpr float toRad(float deg) {return deg * M_PI_180;}
inline constexpr double toRad(double deg) {return deg * M_PI_180;}
inline constexpr ldouble toRad(ldouble deg) {return deg * M_PI_180;}
inline constexpr float toDeg(float rad) {return rad * M_180_PI;}
inline constexpr double toDeg(double rad) {return rad * M_180_PI;}
inline constexpr ldouble toDeg(ldouble rad) {return rad * M_180_PI;}
template <typename T> inline constexpr T sqr(const T & v) {return v * v;}
template <typename T> inline constexpr T toDb (T val) {return T(10.) * std::log10(val);}
template <typename T> inline constexpr T fromDb(T val) {return std::pow(T(10.), val / T(10.));}
// clang-format on
template<typename T>
inline constexpr T sqr(const T & v) {
return v * v;
}
template<typename T>
inline constexpr T toDb(T val) {
return T(10.) * std::log10(val);
}
template<typename T>
inline constexpr T fromDb(T val) {
return std::pow(T(10.), val / T(10.));
}
// [-1 ; 1]
PIP_EXPORT double randomd();
// [-1 ; 1] normal
PIP_EXPORT double randomn(double dv = 0., double sv = 1.);
template<typename T> inline PIVector<T> piAbs(const PIVector<T> & v) {
template<typename T>
inline PIVector<T> piAbs(const PIVector<T> & v) {
PIVector<T> result;
result.resize(v.size());
for (uint i = 0; i < v.size(); i++)
@@ -140,12 +157,11 @@ template<typename T> inline PIVector<T> piAbs(const PIVector<T> & v) {
}
template <typename T>
bool OLS_Linear(const PIVector<PIPair<T, T> > & input, T * out_a, T * out_b) {
template<typename T>
bool OLS_Linear(const PIVector<PIPair<T, T>> & input, T * out_a, T * out_b) {
static_assert(std::is_arithmetic<T>::value, "Type must be arithmetic");
if (input.size_s() < 2)
return false;
int n = input.size_s();
if (input.size_s() < 2) return false;
int n = input.size_s();
T a_t0 = T(), a_t1 = T(), a_t2 = T(), a_t3 = T(), a_t4 = T(), a = T(), b = T();
for (int i = 0; i < n; ++i) {
const PIPair<T, T> & cv(input[i]);
@@ -155,8 +171,7 @@ bool OLS_Linear(const PIVector<PIPair<T, T> > & input, T * out_a, T * out_b) {
a_t3 += cv.first * cv.first;
}
a_t4 = n * a_t3 - a_t1 * a_t1;
if (a_t4 != T())
a = (n * a_t0 - a_t1 * a_t2) / a_t4;
if (a_t4 != T()) a = (n * a_t0 - a_t1 * a_t2) / a_t4;
b = (a_t2 - a * a_t1) / n;
if (out_a != 0) *out_a = a;
if (out_b != 0) *out_b = b;
@@ -164,14 +179,12 @@ bool OLS_Linear(const PIVector<PIPair<T, T> > & input, T * out_a, T * out_b) {
}
template <typename T>
bool WLS_Linear(const PIVector<PIPair<T, T> > & input, const PIVector<T> & weights, T * out_a, T * out_b) {
template<typename T>
bool WLS_Linear(const PIVector<PIPair<T, T>> & input, const PIVector<T> & weights, T * out_a, T * out_b) {
static_assert(std::is_arithmetic<T>::value, "Type must be arithmetic");
if (input.size_s() < 2)
return false;
if (input.size_s() != weights.size_s())
return false;
int n = input.size_s();
if (input.size_s() < 2) return false;
if (input.size_s() != weights.size_s()) return false;
int n = input.size_s();
T a_t0 = T(), a_t1 = T(), a_t2 = T(), a_t3 = T(), a_t4 = T(), a_n = T(), a = T(), b = T();
for (int i = 0; i < n; ++i) {
T cp = weights[i];
@@ -183,8 +196,7 @@ bool WLS_Linear(const PIVector<PIPair<T, T> > & input, const PIVector<T> & weigh
a_n += cp;
}
a_t4 = a_n * a_t3 - a_t1 * a_t1;
if (a_t4 != T())
a = (a_n * a_t0 - a_t1 * a_t2) / a_t4;
if (a_t4 != T()) a = (a_n * a_t0 - a_t1 * a_t2) / a_t4;
b = (a_t2 - a * a_t1) / a_n;
if (out_a != 0) *out_a = a;
if (out_b != 0) *out_b = b;