3.10.2013 - PIPeer release, PIConsole now can work as server and remote client. Remote console test program in directory "remote_console"
This commit is contained in:
195
pimath.h
195
pimath.h
@@ -35,6 +35,18 @@
|
||||
#ifndef M_LN10
|
||||
# define M_LN10 2.30258509299404568402
|
||||
#endif
|
||||
#ifndef M_SQRT2
|
||||
# define M_SQRT2 1.41421356237309514547
|
||||
#endif
|
||||
#ifndef M_SQRT3
|
||||
# define M_SQRT3 1.73205080756887719318
|
||||
#endif
|
||||
#ifndef M_1_SQRT2
|
||||
# define M_1_SQRT2 0.70710678118654746172
|
||||
#endif
|
||||
#ifndef M_1_SQRT3
|
||||
# define M_1_SQRT3 0.57735026918962584208
|
||||
#endif
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
@@ -53,6 +65,12 @@
|
||||
#ifndef M_PI_180
|
||||
# define M_PI_180 1.74532925199432957692e-2
|
||||
#endif
|
||||
#ifndef M_E
|
||||
# define M_E 2.7182818284590452353602874713527
|
||||
#endif
|
||||
#ifndef M_LIGHT_SPEED
|
||||
# define M_LIGHT_SPEED 2.99792458e+8
|
||||
#endif
|
||||
|
||||
using std::complex;
|
||||
|
||||
@@ -70,7 +88,12 @@ const complexd complexd_1(1.);
|
||||
const double deg2rad = M_PI_180;
|
||||
const double rad2deg = M_180_PI;
|
||||
|
||||
inline int sign(const float & x) {return (x < 0.) ? -1 : (x > 0. ? 1 : 0);}
|
||||
inline int sign(const double & x) {return (x < 0.) ? -1 : (x > 0. ? 1 : 0);}
|
||||
inline complexd sign(const complexd & x) {return complexd(sign(x.real()), sign(x.imag()));}
|
||||
inline int pow2(const int p) {return 1 << p;}
|
||||
inline double sqr(const int v) {return v * v;}
|
||||
inline double sqr(const float & v) {return v * v;}
|
||||
inline double sqr(const double & v) {return v * v;}
|
||||
inline double sinc(const double & v) {if (v == 0.) return 1.; double t = M_PI * v; return sin(t) / t;}
|
||||
inline complexd round(const complexd & c) {return complexd(piRound<double>(c.real()), piRound<double>(c.imag()));}
|
||||
@@ -79,23 +102,42 @@ inline complexd ceil(const complexd & c) {return complexd(ceil(c.real()), ceil(c
|
||||
inline complexd atanc(const complexd & c) {return -complexd(-0.5, 1.) * log((complexd_1 + complexd_i * c) / (complexd_1 - complexd_i * c));}
|
||||
inline complexd asinc(const complexd & c) {return -complexd_i * log(complexd_i * c + sqrt(complexd_1 - c * c));}
|
||||
inline complexd acosc(const complexd & c) {return -complexd_i * log(c + complexd_i * sqrt(complexd_1 - c * c));}
|
||||
#if CC_GCC_VERSION <= 0x025F
|
||||
#ifdef CC_GCC
|
||||
# if CC_GCC_VERSION <= 0x025F
|
||||
inline complexd tan(const complexd & c) {return sin(c) / cos(c);}
|
||||
inline complexd tanh(const complexd & c) {return sinh(c) / cosh(c);}
|
||||
inline complexd log2(const complexd & c) {return log(c) / M_LN2;}
|
||||
inline complexd log10(const complexd & c) {return log(c) / M_LN10;}
|
||||
inline double j0(const double & v) {return v;}
|
||||
inline double j1(const double & v) {v;}
|
||||
inline double jn(const int & n, const double & v) {return v;}
|
||||
inline double y0(const double & v) {return v;}
|
||||
inline double y1(const double & v) {return v;}
|
||||
inline double yn(const int & n, const double & v) {return v;}
|
||||
# endif
|
||||
#endif
|
||||
#ifndef PIP_MATH_J0
|
||||
__attribute__ ((unused)) static double j0(const double & v);
|
||||
#endif
|
||||
#ifndef PIP_MATH_J1
|
||||
__attribute__ ((unused)) static double j1(const double & v);
|
||||
#endif
|
||||
#ifndef PIP_MATH_JN
|
||||
__attribute__ ((unused)) static double jn(const int & n, const double & v);
|
||||
#endif
|
||||
#ifndef PIP_MATH_Y0
|
||||
__attribute__ ((unused)) static double y0(const double & v);
|
||||
#endif
|
||||
#ifndef PIP_MATH_Y1
|
||||
__attribute__ ((unused)) static double y1(const double & v);
|
||||
#endif
|
||||
#ifndef PIP_MATH_YN
|
||||
__attribute__ ((unused)) static double yn(const int & n, const double & v);
|
||||
#endif
|
||||
inline double toDb(double val) {return 10. * log10(val);}
|
||||
inline double fromDb(double val) {return pow(10., val / 10.);}
|
||||
inline double toRad(double deg) {return deg * M_PI_180;}
|
||||
inline double toDeg(double rad) {return rad * M_180_PI;}
|
||||
|
||||
// [-1 ; 1]
|
||||
inline double randomd() {return (double)random() / RAND_MAX * 2. - 1.;}
|
||||
// [-1 ; 1] normal
|
||||
double randomn(double dv = 0., double sv = 1.);
|
||||
|
||||
inline PIVector<double> abs(const PIVector<complexd> & v) {
|
||||
PIVector<double> result;
|
||||
result.resize(v.size());
|
||||
@@ -152,7 +194,7 @@ public:
|
||||
Type at(uint index) const {return c[index];}
|
||||
Type & operator [](uint index) {return c[index];}
|
||||
Type operator [](uint index) const {return c[index];}
|
||||
void operator =(const _CVector & v) {c = v.c;}
|
||||
_CVector & operator =(const _CVector & v) {c = v.c; return *this;}
|
||||
bool operator ==(const _CVector & v) const {PIMV_FOR(i, 0) if (c[i] != v[i]) return false; return true;}
|
||||
bool operator !=(const _CVector & v) const {return !(*this == c);}
|
||||
void operator +=(const _CVector & v) {PIMV_FOR(i, 0) c[i] += v[i];}
|
||||
@@ -166,6 +208,7 @@ public:
|
||||
_CVector operator -(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] -= v[i]; return tv;}
|
||||
_CVector operator *(const Type & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] *= v; return tv;}
|
||||
_CVector operator /(const Type & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] /= v; return tv;}
|
||||
_CVector operator /(const _CVector & v) const {_CVector tv = _CVector(*this); PIMV_FOR(i, 0) tv[i] /= v[i]; return tv;}
|
||||
_CVector operator *(const _CVector & v) const {if (Size > 3) return _CVector(); _CVector tv; tv.fill(Type(1)); tv[0] = c[1]*v[2] - v[1]*c[2]; tv[1] = v[0]*c[2] - c[0]*v[2]; tv[2] = c[0]*v[1] - v[0]*c[1]; return tv;}
|
||||
Type operator ^(const _CVector & v) const {Type tv(0); PIMV_FOR(i, 0) tv += c[i] * v[i]; return tv;}
|
||||
|
||||
@@ -187,7 +230,13 @@ private:
|
||||
template<uint Size, typename Type>
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIMathVectorT<Size, Type> & v) {s << '{'; PIMV_FOR(i, 0) {s << v[i]; if (i < Size - 1) s << ", ";} s << '}'; return s;}
|
||||
template<uint Size, typename Type>
|
||||
inline PICout operator <<(PICout s, const PIMathVectorT<Size, Type> & v) {s << '{'; PIMV_FOR(i, 0) {s << v[i]; if (i < Size - 1) s << ", ";} s << '}'; return s;}
|
||||
template<uint Size, typename Type>
|
||||
inline bool operator ||(const PIMathVectorT<Size, Type> & f, const PIMathVectorT<Size, Type> & s) {return (f * s).isNull();}
|
||||
template<uint Size, typename Type>
|
||||
inline PIMathVectorT<Size, Type> sqrt(const PIMathVectorT<Size, Type> & v) {PIMathVectorT<Size, Type> ret; PIMV_FOR(i, 0) {ret[i] = sqrt(v[i]);} return ret;}
|
||||
template<uint Size, typename Type>
|
||||
inline PIMathVectorT<Size, Type> sqr(const PIMathVectorT<Size, Type> & v) {PIMathVectorT<Size, Type> ret; PIMV_FOR(i, 0) {ret[i] = sqr(v[i]);} return ret;}
|
||||
|
||||
//template<uint Size0, typename Type0 = double, uint Size1 = Size0, typename Type1 = Type0> /// vector {Size0, Type0} to vector {Size1, Type1}
|
||||
//inline operator PIMathVectorT<Size1, Type1>(const PIMathVectorT<Size0, Type0> & v) {PIMathVectorT<Size1, Type1> tv; uint sz = piMin<uint>(Size0, Size1); for (uint i = 0; i < sz; ++i) tv[i] = v[i]; return tv;}
|
||||
@@ -220,6 +269,10 @@ public:
|
||||
PIMathMatrixT(const PIVector<Type> & val) {resize(Cols, Rows); int i = 0; PIMM_FOR_I_WB(c, r) m[c][r] = val[i++];}
|
||||
|
||||
static _CMatrix identity() {_CMatrix tm = _CMatrix(); PIMM_FOR_WB(c, r) tm.m[c][r] = (c == r ? Type(1) : Type(0)); return tm;}
|
||||
static _CMatrix rotation(double angle) {return _CMatrix();}
|
||||
static _CMatrix rotationX(double angle) {return _CMatrix();}
|
||||
static _CMatrix rotationY(double angle) {return _CMatrix();}
|
||||
static _CMatrix rotationZ(double angle) {return _CMatrix();}
|
||||
|
||||
uint cols() const {return Cols;}
|
||||
uint rows() const {return Rows;}
|
||||
@@ -360,8 +413,16 @@ private:
|
||||
|
||||
};
|
||||
|
||||
|
||||
template<> inline PIMathMatrixT<2u, 2u> PIMathMatrixT<2u, 2u>::rotation(double angle) {double c = cos(angle), s = sin(angle); PIMathMatrixT<2u, 2u> tm; tm[0][0] = tm[1][1] = c; tm[0][1] = -s; tm[1][0] = s; return tm;}
|
||||
template<> inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::rotationX(double angle) {double c = cos(angle), s = sin(angle); PIMathMatrixT<3u, 3u> tm; tm[0][0] = 1.; tm[1][1] = tm[2][2] = c; tm[2][1] = -s; tm[1][2] = s; return tm;}
|
||||
template<> inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::rotationY(double angle) {double c = cos(angle), s = sin(angle); PIMathMatrixT<3u, 3u> tm; tm[1][1] = 1.; tm[0][0] = tm[2][2] = c; tm[2][0] = s; tm[0][2] = -s; return tm;}
|
||||
template<> inline PIMathMatrixT<3u, 3u> PIMathMatrixT<3u, 3u>::rotationZ(double angle) {double c = cos(angle), s = sin(angle); PIMathMatrixT<3u, 3u> tm; tm[2][2] = 1.; tm[0][0] = tm[1][1] = c; tm[1][0] = -s; tm[0][1] = s; return tm;}
|
||||
|
||||
template<uint Cols, uint Rows, typename Type>
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIMathMatrixT<Cols, Rows, Type> & m) {s << '{'; PIMM_FOR_I(c, r) s << m[c][r]; if (c < Cols - 1 || r < Rows - 1) s << ", ";} if (r < Rows - 1) s << endl << ' ';} s << '}'; return s;}
|
||||
template<uint Cols, uint Rows, typename Type>
|
||||
inline PICout operator <<(PICout s, const PIMathMatrixT<Cols, Rows, Type> & m) {s << '{'; PIMM_FOR_I(c, r) s << m[c][r]; if (c < Cols - 1 || r < Rows - 1) s << ", ";} if (r < Rows - 1) s << NewLine << ' ';} s << '}'; return s;}
|
||||
|
||||
/// Multiply matrices {CR x Rows0} on {Cols1 x CR}, result is {Cols1 x Rows0}
|
||||
template<uint CR, uint Rows0, uint Cols1, typename Type>
|
||||
@@ -487,6 +548,8 @@ private:
|
||||
|
||||
template<typename Type>
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIMathVector<Type> & v) {s << '{'; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << '}'; return s;}
|
||||
template<typename Type>
|
||||
inline PICout operator <<(PICout s, const PIMathVector<Type> & v) {s << '{'; for (uint i = 0; i < v.size(); ++i) {s << v[i]; if (i < v.size() - 1) s << ", ";} s << '}'; return s;}
|
||||
|
||||
typedef PIMathVector<int> PIMathVectori;
|
||||
typedef PIMathVector<double> PIMathVectord;
|
||||
@@ -658,6 +721,8 @@ private:
|
||||
|
||||
template<typename Type>
|
||||
inline std::ostream & operator <<(std::ostream & s, const PIMathMatrix<Type> & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m[c][r]; if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << endl << ' ';} s << '}'; return s;}
|
||||
template<typename Type>
|
||||
inline PICout operator <<(PICout s, const PIMathMatrix<Type> & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m[c][r]; if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << NewLine << ' ';} s << '}'; return s;}
|
||||
|
||||
/// Multiply matrices {CR x Rows0} on {Cols1 x CR}, result is {Cols1 x Rows0}
|
||||
template<typename Type>
|
||||
@@ -829,14 +894,116 @@ private:
|
||||
};
|
||||
|
||||
|
||||
template <typename T>
|
||||
class PIP_EXPORT PIStatistic {
|
||||
public:
|
||||
PIStatistic();
|
||||
bool calculate(const PIVector<double> &val);
|
||||
double mean;
|
||||
double variance;
|
||||
double skewness;
|
||||
double kurtosis;
|
||||
PIStatistic() {
|
||||
mean = T();
|
||||
variance = T();
|
||||
skewness = T();
|
||||
kurtosis = T();
|
||||
}
|
||||
bool calculate(const PIVector<T> &val) {
|
||||
T v = T(), v1 = T(), v2 = T(), stddev = T();
|
||||
int i, n = val.size();
|
||||
mean = T();
|
||||
variance = T();
|
||||
skewness = T();
|
||||
kurtosis = T();
|
||||
if (n < 2)
|
||||
return false;
|
||||
/*
|
||||
* Mean
|
||||
*/
|
||||
for (i = 0; i < n; i++)
|
||||
mean += val[i];
|
||||
mean /= n;
|
||||
/*
|
||||
* Variance (using corrected two-pass algorithm)
|
||||
*/
|
||||
for (i = 0; i < n; i++) {
|
||||
v1 += sqr(val[i] - mean);
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
v2 += val[i] - mean;
|
||||
v2 = sqr(v2) / n;
|
||||
variance = (v1 - v2) / (n - 1);
|
||||
if (variance < T())
|
||||
variance = T();
|
||||
stddev = sqrt(variance);
|
||||
/*
|
||||
* Skewness and kurtosis
|
||||
*/
|
||||
if (stddev != T()) {
|
||||
for (i = 0; i < n; i++) {
|
||||
v = (val[i] - mean) / stddev;
|
||||
v2 = sqr(v);
|
||||
skewness = skewness + v2 * v;
|
||||
kurtosis = kurtosis + sqr(v2);
|
||||
}
|
||||
skewness /= n;
|
||||
kurtosis = kurtosis / n - 3.;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
T mean;
|
||||
T variance;
|
||||
T skewness;
|
||||
T kurtosis;
|
||||
};
|
||||
|
||||
typedef PIStatistic<int> PIStatistici;
|
||||
typedef PIStatistic<float> PIStatisticf;
|
||||
typedef PIStatistic<double> PIStatisticd;
|
||||
|
||||
|
||||
template <typename T>
|
||||
bool OLS_Linear(const PIVector<PIPair<T, T> > & input, T * out_a, T * out_b) {
|
||||
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]);
|
||||
a_t0 += cv.first * cv.second;
|
||||
a_t1 += cv.first;
|
||||
a_t2 += cv.second;
|
||||
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;
|
||||
b = (a_t2 - a * a_t1) / n;
|
||||
if (out_a != 0) *out_a = a;
|
||||
if (out_b != 0) *out_b = b;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
bool WLS_Linear(const PIVector<PIPair<T, T> > & input, const PIVector<T> & weights, T * out_a, T * out_b) {
|
||||
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];
|
||||
const PIPair<T, T> & cv(input[i]);
|
||||
a_t0 += cv.first * cv.second * cp;
|
||||
a_t1 += cv.first * cp;
|
||||
a_t2 += cv.second * cp;
|
||||
a_t3 += cv.first * cv.first * cp;
|
||||
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;
|
||||
b = (a_t2 - a * a_t1) / a_n;
|
||||
if (out_a != 0) *out_a = a;
|
||||
if (out_b != 0) *out_b = b;
|
||||
return true;
|
||||
}
|
||||
|
||||
#endif // PIMATH_H
|
||||
|
||||
Reference in New Issue
Block a user