18.03.2013 - Bug fixes, add in/out speed diagnostic to PIProtocol, fixed PIConsole tab switch segfault, PIObject EVENT / EVENT_HANDLER mechanism update - new EVENT macros that use EVENT_HANDLER with raiseEvent implementation.
This allow compile check event for CONNECT and use EVENT as CONNECT target, also raise event now is simple execute EVENT function.
This commit is contained in:
203
pimath.h
Executable file → Normal file
203
pimath.h
Executable file → Normal file
@@ -1,20 +1,20 @@
|
||||
/*
|
||||
PIP - Platform Independent Primitives
|
||||
Math
|
||||
Copyright (C) 2012 Ivan Pelipenko peri4ko@gmail.com
|
||||
PIP - Platform Independent Primitives
|
||||
Math
|
||||
Copyright (C) 2013 Ivan Pelipenko peri4ko@gmail.com
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef PIMATH_H
|
||||
@@ -22,18 +22,37 @@
|
||||
|
||||
#include "picontainers.h"
|
||||
#ifndef QNX
|
||||
# include <cmath>
|
||||
# include <complex>
|
||||
# include <complex>
|
||||
# include <cmath>
|
||||
#else
|
||||
# include <math.h>
|
||||
# include <complex.h>
|
||||
#endif
|
||||
#ifdef CC_VC
|
||||
#define M_PI 3.14159265358979323846
|
||||
# include <complex.h>
|
||||
# include <math.h>
|
||||
#endif
|
||||
|
||||
#define M_2PI 6.28318530717958647692
|
||||
#define M_PI_3 1.04719755119659774615
|
||||
#ifndef M_LN2
|
||||
# define M_LN2 0.69314718055994530942
|
||||
#endif
|
||||
#ifndef M_LN10
|
||||
# define M_LN10 2.30258509299404568402
|
||||
#endif
|
||||
#ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
#ifndef M_2PI
|
||||
# define M_2PI 6.28318530717958647692
|
||||
#endif
|
||||
#ifndef M_PI_3
|
||||
# define M_PI_3 1.04719755119659774615
|
||||
#endif
|
||||
#ifndef M_2PI_3
|
||||
# define M_2PI_3 2.0943951023931954923
|
||||
#endif
|
||||
#ifndef M_180_PI
|
||||
# define M_180_PI 57.2957795130823208768
|
||||
#endif
|
||||
#ifndef M_PI_180
|
||||
# define M_PI_180 1.74532925199432957692e-2
|
||||
#endif
|
||||
|
||||
using std::complex;
|
||||
|
||||
@@ -48,30 +67,47 @@ const complexd complexd_i(0., 1.);
|
||||
const complexd complexd_0(0.);
|
||||
const complexd complexd_1(1.);
|
||||
|
||||
const double deg2rad = atan(1.) / 45.;
|
||||
const double rad2deg = 45. / atan(1.);
|
||||
const double deg2rad = M_PI_180;
|
||||
const double rad2deg = M_180_PI;
|
||||
|
||||
inline int pow2(const int p) {return 1 << p;}
|
||||
inline double sqr(const double & v) {return v * v;}
|
||||
inline double sinc(const double & v) {double t = M_PI * v; return sin(t) / t;}
|
||||
inline complexd round(const complexd & c) {return complexd(round(c.real()), round(c.imag()));}
|
||||
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()));}
|
||||
inline complexd floor(const complexd & c) {return complexd(floor(c.real()), floor(c.imag()));}
|
||||
inline complexd ceil(const complexd & c) {return complexd(ceil(c.real()), ceil(c.imag()));}
|
||||
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));}
|
||||
#ifdef QNX
|
||||
#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) {v;}
|
||||
inline double j0(const double & v) {return v;}
|
||||
inline double j1(const double & v) {v;}
|
||||
inline double jn(const int & n, const double & v) {v;}
|
||||
inline double y0(const double & v) {v;}
|
||||
inline double y1(const double & v) {v;}
|
||||
inline double yn(const int & n, 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
|
||||
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;}
|
||||
|
||||
inline PIVector<double> abs(const PIVector<complexd> &v) {
|
||||
PIVector<double> result;
|
||||
result.resize(v.size());
|
||||
for(uint i=0; i<v.size(); i++) result[i] = abs(v.at(i));
|
||||
return result;
|
||||
}
|
||||
inline PIVector<double> abs(const PIVector<double> &v) {
|
||||
PIVector<double> result;
|
||||
result.resize(v.size());
|
||||
for(uint i=0; i<v.size(); i++) result[i] = abs(v.at(i));
|
||||
return result;
|
||||
}
|
||||
|
||||
template<uint Cols, uint Rows, typename Type>
|
||||
class PIMathMatrixT;
|
||||
@@ -103,7 +139,7 @@ public:
|
||||
Type angleCos(const _CVector & v) const {Type tv = v.length() * length(); return (tv == Type(0) ? Type(0) : ((*this) ^ v) / tv);}
|
||||
Type angleSin(const _CVector & v) const {Type tv = angleCos(v); return sqrt(Type(1) - tv * tv);}
|
||||
Type angleRad(const _CVector & v) const {return acos(angleCos(v));}
|
||||
Type angleDeg(const _CVector & v) const {return acos(angleCos(v)) * rad2deg;}
|
||||
Type angleDeg(const _CVector & v) const {return toGrad(acos(angleCos(v)));}
|
||||
_CVector projection(const _CVector & v) {Type tv = v.length(); return (tv == Type(0) ? _CVector() : v * (((*this) ^ v) / tv));}
|
||||
_CVector & normalize() {Type tv = length(); if (tv == Type(1)) return *this; PIMV_FOR(i, 0) c[i] /= tv; return *this;}
|
||||
_CVector normalized() {_CVector tv(*this); tv.normalize(); return tv;}
|
||||
@@ -404,7 +440,7 @@ public:
|
||||
Type angleCos(const _CVector & v) const {Type tv = v.length() * length(); return (tv == Type(0) ? Type(0) : ((*this) ^ v) / tv);}
|
||||
Type angleSin(const _CVector & v) const {Type tv = angleCos(v); return sqrt(Type(1) - tv * tv);}
|
||||
Type angleRad(const _CVector & v) const {return acos(angleCos(v));}
|
||||
Type angleDeg(const _CVector & v) const {return acos(angleCos(v)) * rad2deg;}
|
||||
Type angleDeg(const _CVector & v) const {return toGrad(acos(angleCos(v)));}
|
||||
_CVector projection(const _CVector & v) {Type tv = v.length(); return (tv == Type(0) ? _CVector() : v * (((*this) ^ v) / tv));}
|
||||
_CVector & normalize() {Type tv = length(); if (tv == Type(1)) return *this; PIMV_FOR(i, 0) c[i] /= tv; return *this;}
|
||||
_CVector normalized() {_CVector tv(*this); tv.normalize(); return tv;}
|
||||
@@ -624,7 +660,7 @@ inline std::ostream & operator <<(std::ostream & s, const PIMathMatrix<Type> & m
|
||||
/// Multiply matrices {CR x Rows0} on {Cols1 x CR}, result is {Cols1 x Rows0}
|
||||
template<typename Type>
|
||||
inline PIMathMatrix<Type> operator *(const PIMathMatrix<Type> & fm,
|
||||
const PIMathMatrix<Type> & sm) {
|
||||
const PIMathMatrix<Type> & sm) {
|
||||
uint cr = fm.cols(), rows0 = fm.rows(), cols1 = sm.cols();
|
||||
PIMathMatrix<Type> tm(cols1, rows0);
|
||||
if (fm.cols() != sm.rows()) return tm;
|
||||
@@ -643,7 +679,7 @@ inline PIMathMatrix<Type> operator *(const PIMathMatrix<Type> & fm,
|
||||
/// Multiply matrix {Cols x Rows} on vector {Cols}, result is vector {Rows}
|
||||
template<typename Type>
|
||||
inline PIMathVector<Type> operator *(const PIMathMatrix<Type> & fm,
|
||||
const PIMathVector<Type> & sv) {
|
||||
const PIMathVector<Type> & sv) {
|
||||
uint c = fm.cols(), r = fm.rows();
|
||||
PIMathVector<Type> tv(r);
|
||||
if (c != sv.size()) return tv;
|
||||
@@ -669,13 +705,6 @@ typedef PIMathMatrix<double> PIMathMatrixd;
|
||||
#undef PIMM_FOR_R
|
||||
|
||||
|
||||
/*
|
||||
Fast Fourier Transformation: direct (complement= false)
|
||||
and complement (complement = true). 'x' is data source.
|
||||
'x' contains 2^T items.
|
||||
*/
|
||||
void fft(complexd * x, int T, bool complement);
|
||||
|
||||
/// Differential evaluations
|
||||
|
||||
struct TransferFunction { // Для задания передаточной функции
|
||||
@@ -691,18 +720,18 @@ class Solver
|
||||
{
|
||||
public:
|
||||
enum Method {Global = -1,
|
||||
Eyler_1 = 01,
|
||||
Eyler_2 = 02,
|
||||
EylerKoshi = 03,
|
||||
RungeKutta_4 = 14,
|
||||
AdamsBashfortMoulton_2 = 22,
|
||||
AdamsBashfortMoulton_3 = 23,
|
||||
AdamsBashfortMoulton_4 = 24,
|
||||
PolynomialApproximation_2 = 32,
|
||||
PolynomialApproximation_3 = 33,
|
||||
PolynomialApproximation_4 = 34,
|
||||
PolynomialApproximation_5 = 35
|
||||
};
|
||||
Eyler_1 = 01,
|
||||
Eyler_2 = 02,
|
||||
EylerKoshi = 03,
|
||||
RungeKutta_4 = 14,
|
||||
AdamsBashfortMoulton_2 = 22,
|
||||
AdamsBashfortMoulton_3 = 23,
|
||||
AdamsBashfortMoulton_4 = 24,
|
||||
PolynomialApproximation_2 = 32,
|
||||
PolynomialApproximation_3 = 33,
|
||||
PolynomialApproximation_4 = 34,
|
||||
PolynomialApproximation_5 = 35
|
||||
};
|
||||
|
||||
Solver() {times.resize(4); step = 0;}
|
||||
|
||||
@@ -744,4 +773,68 @@ private:
|
||||
};
|
||||
|
||||
|
||||
|
||||
class PIFFT
|
||||
{
|
||||
public:
|
||||
PIFFT();
|
||||
// const PIVector<uint> & getIndexes() {return indexes;}
|
||||
// const PIVector<complexd> & getCoefs() {return coefs;}
|
||||
PIVector<complexd> * calcFFT(const PIVector<complexd> &val);
|
||||
PIVector<complexd> * calcFFT(const PIVector<double> &val);
|
||||
PIVector<complexd> * calcFFTinverse(const PIVector<complexd> &val);
|
||||
PIVector<complexd> * calcHilbert(const PIVector<double> &val);
|
||||
PIVector<double> getAmplitude();
|
||||
private:
|
||||
// PIVector<uint> indexes;
|
||||
// PIVector<complexd> coefs;
|
||||
PIVector<complexd> result;
|
||||
// uint iterations;
|
||||
bool prepared;
|
||||
// uint out_size;
|
||||
typedef ptrdiff_t ae_int_t;
|
||||
void calc_coefs(uint cnt2);
|
||||
void calc_indexes(uint cnt2, uint deep2);
|
||||
complexd coef(uint n, uint k);
|
||||
|
||||
struct ftplan {
|
||||
PIVector<int> plan;
|
||||
PIVector<double> precomputed;
|
||||
PIVector<double> tmpbuf;
|
||||
PIVector<double> stackbuf;
|
||||
};
|
||||
|
||||
ftplan curplan;
|
||||
|
||||
void fftc1d(const PIVector<complexd> &a, uint n);
|
||||
void fftc1r(const PIVector<double> &a, uint n);
|
||||
void fftc1dinv(const PIVector<complexd> &a, uint n);
|
||||
|
||||
void createPlan(uint n);
|
||||
void ftbasegeneratecomplexfftplan(uint n, ftplan *plan);
|
||||
void ftbase_ftbasegenerateplanrec(int n, int tasktype, ftplan *plan, int *plansize, int *precomputedsize, int *planarraysize, int *tmpmemsize, int *stackmemsize, ae_int_t stackptr, int debugi=0);
|
||||
void ftbase_ftbaseprecomputeplanrec(ftplan *plan, int entryoffset, ae_int_t stackptr);
|
||||
void ftbasefactorize(int n, int *n1, int *n2);
|
||||
void ftbase_ftbasefindsmoothrec(int n, int seed, int leastfactor, int *best);
|
||||
int ftbasefindsmooth(int n);
|
||||
void ftbaseexecuteplan(PIVector<double> *a, int aoffset, int n, ftplan *plan);
|
||||
void ftbaseexecuteplanrec(PIVector<double> *a, int aoffset, ftplan *plan, int entryoffset, ae_int_t stackptr);
|
||||
void ftbase_internalcomplexlintranspose(PIVector<double> *a, int m, int n, int astart, PIVector<double> *buf);
|
||||
void ftbase_ffticltrec(PIVector<double> *a, int astart, int astride, PIVector<double> *b, int bstart, int bstride, int m, int n);
|
||||
void ftbase_internalreallintranspose(PIVector<double> *a, int m, int n, int astart, PIVector<double> *buf);
|
||||
void ftbase_fftirltrec(PIVector<double> *a, int astart, int astride, PIVector<double> *b, int bstart, int bstride, int m, int n);
|
||||
void ftbase_ffttwcalc(PIVector<double> *a, int aoffset, int n1, int n2);
|
||||
};
|
||||
|
||||
|
||||
class PIStatistic {
|
||||
public:
|
||||
PIStatistic();
|
||||
bool calculate(const PIVector<double> &val);
|
||||
double mean;
|
||||
double variance;
|
||||
double skewness;
|
||||
double kurtosis;
|
||||
};
|
||||
|
||||
#endif // PIMATH_H
|
||||
|
||||
Reference in New Issue
Block a user