1 | // Usuall mathematical functions and operations on arrays
|
---|
2 | // R. Ansari, C.Magneville 03/2000
|
---|
3 |
|
---|
4 | #include "machdefs.h"
|
---|
5 | #include <stdlib.h>
|
---|
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 | {
|
---|
15 | if (a.NbDimensions() < 1)
|
---|
16 | throw RangeCheckError("MathArray<T>::ApplyFunctionInPlace(TArray<T> & a..) Not Allocated Array a !");
|
---|
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;
|
---|
29 | for(j=0; j<a.Size(); j += a.Size(ka)) {
|
---|
30 | pe = a.DataBlock().Begin()+a.Offset(j);
|
---|
31 | for(k=0; k<gpas; k+=step) pe[k] = (T)(f((double)pe[k]));
|
---|
32 | }
|
---|
33 | }
|
---|
34 | return(a);
|
---|
35 | }
|
---|
36 |
|
---|
37 | template <class T>
|
---|
38 | TArray<T>& MathArray<T>::ApplyFunctionInPlace(TArray<T> & a, Arr_FloatFunctionOfX f)
|
---|
39 | {
|
---|
40 | if (a.NbDimensions() < 1)
|
---|
41 | throw RangeCheckError("MathArray<T>::ApplyFunctionInPlace(TArray<T> & a..) Not Allocated Array a !");
|
---|
42 | T * pe;
|
---|
43 | uint_8 j,k;
|
---|
44 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
45 | uint_8 step = a.AvgStep();
|
---|
46 | uint_8 maxx = a.Size()*step;
|
---|
47 | pe = a.Data();
|
---|
48 | for(k=0; k<maxx; k+=step ) pe[k] = (T)(f((float)pe[k]));
|
---|
49 | }
|
---|
50 | else { // Non regular data spacing ...
|
---|
51 | uint_4 ka = a.MaxSizeKA();
|
---|
52 | uint_8 step = a.Step(ka);
|
---|
53 | uint_8 gpas = a.Size(ka)*step;
|
---|
54 | for(j=0; j<a.Size(); j += a.Size(ka)) {
|
---|
55 | pe = a.DataBlock().Begin()+a.Offset(j);
|
---|
56 | for(k=0; k<gpas; k+=step) pe[k] = (T)(f((float)pe[k]));
|
---|
57 | }
|
---|
58 | }
|
---|
59 | return(a);
|
---|
60 | }
|
---|
61 |
|
---|
62 |
|
---|
63 | template <class T>
|
---|
64 | TArray<T> MathArray<T>::ApplyFunction(TArray<T> const & a, Arr_DoubleFunctionOfX f)
|
---|
65 | {
|
---|
66 | TArray<T> ra;
|
---|
67 | ra = a;
|
---|
68 | ApplyFunctionInPlace(ra, f);
|
---|
69 | return(ra);
|
---|
70 | }
|
---|
71 |
|
---|
72 | template <class T>
|
---|
73 | TArray<T> MathArray<T>::ApplyFunction(TArray<T> const & a, Arr_FloatFunctionOfX f)
|
---|
74 | {
|
---|
75 | TArray<T> ra;
|
---|
76 | ra = a;
|
---|
77 | ApplyFunctionInPlace(ra, f);
|
---|
78 | return(ra);
|
---|
79 | }
|
---|
80 |
|
---|
81 | template <class T>
|
---|
82 | double MathArray<T>::MeanSigma(TArray<T> const & a, double & mean, double & sig)
|
---|
83 | {
|
---|
84 | if (a.NbDimensions() < 1)
|
---|
85 | throw RangeCheckError("MathArray<T>::MeanSigma(TArray<T> const & a..) Not Allocated Array a !");
|
---|
86 | const T * pe;
|
---|
87 | uint_8 j,k;
|
---|
88 | mean=0.;
|
---|
89 | sig = 0.;
|
---|
90 | double valok;
|
---|
91 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
92 | uint_8 step = a.AvgStep();
|
---|
93 | uint_8 maxx = a.Size()*step;
|
---|
94 | pe = a.Data();
|
---|
95 | for(k=0; k<maxx; k+=step ) {
|
---|
96 | valok = (double) pe[k];
|
---|
97 | mean += valok; sig += valok*valok;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | else { // Non regular data spacing ...
|
---|
101 | uint_4 ka = a.MaxSizeKA();
|
---|
102 | uint_8 step = a.Step(ka);
|
---|
103 | uint_8 gpas = a.Size(ka)*step;
|
---|
104 | for(j=0; j<a.Size(); j += a.Size(ka)) {
|
---|
105 | pe = a.DataBlock().Begin()+a.Offset(j);
|
---|
106 | for(k=0; k<gpas; k+=step) {
|
---|
107 | valok = (double) pe[k];
|
---|
108 | mean += valok; sig += valok*valok;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|
112 | double dsz = (double)(a.Size());
|
---|
113 | mean /= dsz;
|
---|
114 | sig = sig/dsz - mean*mean;
|
---|
115 | if (sig >= 0.) sig = sqrt(sig);
|
---|
116 | return(mean);
|
---|
117 | }
|
---|
118 |
|
---|
119 | ///////////////////////////////////////////////////////////////
|
---|
120 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
121 | #pragma define_template MathArray<r_4>
|
---|
122 | #pragma define_template MathArray<r_8>
|
---|
123 | #endif
|
---|
124 |
|
---|
125 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
126 | template class MathArray<r_4>;
|
---|
127 | template class MathArray<r_8>;
|
---|
128 | #endif
|
---|