[787] | 1 | // Usuall mathematical functions and operations on arrays
|
---|
| 2 | // R. Ansari, C.Magneville 03/2000
|
---|
| 3 |
|
---|
| 4 | #include "machdefs.h"
|
---|
[882] | 5 | #include <math.h>
|
---|
[787] | 6 | #include "matharr.h"
|
---|
| 7 |
|
---|
| 8 | // ----------------------------------------------------
|
---|
| 9 | // Application d'une fonction
|
---|
| 10 | // ----------------------------------------------------
|
---|
| 11 |
|
---|
| 12 | template <class T>
|
---|
| 13 | TArray<T>& MathArray<T>::ApplyFunctionInPlace(TArray<T> & a, Arr_DoubleFunctionOfX f)
|
---|
| 14 | {
|
---|
[804] | 15 | if (a.NbDimensions() < 1)
|
---|
| 16 | throw RangeCheckError("MathArray<T>::ApplyFunctionInPlace(TArray<T> & a..) Not Allocated Array a !");
|
---|
[787] | 17 | T * pe;
|
---|
| 18 | uint_8 j,k;
|
---|
| 19 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
| 20 | uint_8 step = a.AvgStep();
|
---|
| 21 | uint_8 maxx = a.Size()*step;
|
---|
| 22 | pe = a.Data();
|
---|
| 23 | for(k=0; k<maxx; k+=step ) pe[k] = (T)(f((double)pe[k]));
|
---|
| 24 | }
|
---|
| 25 | else { // Non regular data spacing ...
|
---|
| 26 | uint_4 ka = a.MaxSizeKA();
|
---|
| 27 | uint_8 step = a.Step(ka);
|
---|
| 28 | uint_8 gpas = a.Size(ka)*step;
|
---|
[813] | 29 | uint_8 naxa = a.Size()/a.Size(ka);
|
---|
| 30 | for(j=0; j<naxa; j++) {
|
---|
| 31 | pe = a.DataBlock().Begin()+a.Offset(ka,j);
|
---|
[787] | 32 | for(k=0; k<gpas; k+=step) pe[k] = (T)(f((double)pe[k]));
|
---|
| 33 | }
|
---|
| 34 | }
|
---|
| 35 | return(a);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | template <class T>
|
---|
| 39 | TArray<T>& MathArray<T>::ApplyFunctionInPlace(TArray<T> & a, Arr_FloatFunctionOfX f)
|
---|
| 40 | {
|
---|
[804] | 41 | if (a.NbDimensions() < 1)
|
---|
| 42 | throw RangeCheckError("MathArray<T>::ApplyFunctionInPlace(TArray<T> & a..) Not Allocated Array a !");
|
---|
[787] | 43 | T * pe;
|
---|
| 44 | uint_8 j,k;
|
---|
| 45 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
| 46 | uint_8 step = a.AvgStep();
|
---|
| 47 | uint_8 maxx = a.Size()*step;
|
---|
| 48 | pe = a.Data();
|
---|
| 49 | for(k=0; k<maxx; k+=step ) pe[k] = (T)(f((float)pe[k]));
|
---|
| 50 | }
|
---|
| 51 | else { // Non regular data spacing ...
|
---|
| 52 | uint_4 ka = a.MaxSizeKA();
|
---|
| 53 | uint_8 step = a.Step(ka);
|
---|
| 54 | uint_8 gpas = a.Size(ka)*step;
|
---|
[813] | 55 | uint_8 naxa = a.Size()/a.Size(ka);
|
---|
| 56 | for(j=0; j<naxa; j++) {
|
---|
| 57 | pe = a.DataBlock().Begin()+a.Offset(ka,j);
|
---|
[787] | 58 | for(k=0; k<gpas; k+=step) pe[k] = (T)(f((float)pe[k]));
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | return(a);
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 |
|
---|
| 65 | template <class T>
|
---|
| 66 | TArray<T> MathArray<T>::ApplyFunction(TArray<T> const & a, Arr_DoubleFunctionOfX f)
|
---|
| 67 | {
|
---|
| 68 | TArray<T> ra;
|
---|
| 69 | ra = a;
|
---|
| 70 | ApplyFunctionInPlace(ra, f);
|
---|
| 71 | return(ra);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | template <class T>
|
---|
| 75 | TArray<T> MathArray<T>::ApplyFunction(TArray<T> const & a, Arr_FloatFunctionOfX f)
|
---|
| 76 | {
|
---|
| 77 | TArray<T> ra;
|
---|
| 78 | ra = a;
|
---|
| 79 | ApplyFunctionInPlace(ra, f);
|
---|
| 80 | return(ra);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[804] | 83 | template <class T>
|
---|
| 84 | double MathArray<T>::MeanSigma(TArray<T> const & a, double & mean, double & sig)
|
---|
| 85 | {
|
---|
| 86 | if (a.NbDimensions() < 1)
|
---|
| 87 | throw RangeCheckError("MathArray<T>::MeanSigma(TArray<T> const & a..) Not Allocated Array a !");
|
---|
| 88 | const T * pe;
|
---|
| 89 | uint_8 j,k;
|
---|
| 90 | mean=0.;
|
---|
| 91 | sig = 0.;
|
---|
| 92 | double valok;
|
---|
| 93 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
| 94 | uint_8 step = a.AvgStep();
|
---|
| 95 | uint_8 maxx = a.Size()*step;
|
---|
| 96 | pe = a.Data();
|
---|
| 97 | for(k=0; k<maxx; k+=step ) {
|
---|
| 98 | valok = (double) pe[k];
|
---|
| 99 | mean += valok; sig += valok*valok;
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | else { // Non regular data spacing ...
|
---|
| 103 | uint_4 ka = a.MaxSizeKA();
|
---|
| 104 | uint_8 step = a.Step(ka);
|
---|
| 105 | uint_8 gpas = a.Size(ka)*step;
|
---|
[813] | 106 | uint_8 naxa = a.Size()/a.Size(ka);
|
---|
| 107 | for(j=0; j<naxa; j++) {
|
---|
| 108 | pe = a.DataBlock().Begin()+a.Offset(ka,j);
|
---|
[804] | 109 | for(k=0; k<gpas; k+=step) {
|
---|
| 110 | valok = (double) pe[k];
|
---|
| 111 | mean += valok; sig += valok*valok;
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | double dsz = (double)(a.Size());
|
---|
| 116 | mean /= dsz;
|
---|
| 117 | sig = sig/dsz - mean*mean;
|
---|
[882] | 118 | #if !defined(OS_LINUX) && !defined (__KCC__)
|
---|
[804] | 119 | if (sig >= 0.) sig = sqrt(sig);
|
---|
[882] | 120 | #else
|
---|
| 121 | // va comprendre pourquoi g++ (sur Linux) veut ca pour faire la generation
|
---|
| 122 | // de template !!!!
|
---|
| 123 | if (sig >= 0.) sig = _Sqrt_(sig);
|
---|
| 124 | #endif
|
---|
[804] | 125 | return(mean);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[787] | 128 | ///////////////////////////////////////////////////////////////
|
---|
| 129 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
| 130 | #pragma define_template MathArray<r_4>
|
---|
| 131 | #pragma define_template MathArray<r_8>
|
---|
| 132 | #endif
|
---|
| 133 |
|
---|
| 134 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
| 135 | template class MathArray<r_4>;
|
---|
| 136 | template class MathArray<r_8>;
|
---|
| 137 | #endif
|
---|