1 | // Usuall mathematical functions and operations on arrays
|
---|
2 | // R. Ansari, C.Magneville 03/2000
|
---|
3 |
|
---|
4 | #include "machdefs.h"
|
---|
5 | #include <math.h>
|
---|
6 | #include "matharr.h"
|
---|
7 |
|
---|
8 | // ----------------------------------------------------
|
---|
9 | // Application d'une fonction
|
---|
10 | // ----------------------------------------------------
|
---|
11 |
|
---|
12 | /*!
|
---|
13 | \class SOPHYA::MathArray
|
---|
14 | \ingroup TArray
|
---|
15 | Class for simple mathematical operation on arrays
|
---|
16 | \warning Instanciated only for \b real and \b double (r_4, r_8) type arrays
|
---|
17 | */
|
---|
18 |
|
---|
19 | //! Apply Function In Place (function double version)
|
---|
20 | /*!
|
---|
21 | \param a : array to be replaced in place
|
---|
22 | \param f : function for replacement
|
---|
23 | \return Return an array \b a filled with function f(a(i,j))
|
---|
24 | */
|
---|
25 | template <class T>
|
---|
26 | TArray<T>& MathArray<T>::ApplyFunctionInPlace(TArray<T> & a, Arr_DoubleFunctionOfX f)
|
---|
27 | {
|
---|
28 | if (a.NbDimensions() < 1)
|
---|
29 | throw RangeCheckError("MathArray<T>::ApplyFunctionInPlace(TArray<T> & a..) Not Allocated Array a !");
|
---|
30 | T * pe;
|
---|
31 | sa_size_t j,k;
|
---|
32 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
33 | sa_size_t step = a.AvgStep();
|
---|
34 | sa_size_t maxx = a.Size()*step;
|
---|
35 | pe = a.Data();
|
---|
36 | for(k=0; k<maxx; k+=step ) pe[k] = (T)(f((double)pe[k]));
|
---|
37 | }
|
---|
38 | else { // Non regular data spacing ...
|
---|
39 | int_4 ka = a.MaxSizeKA();
|
---|
40 | sa_size_t step = a.Step(ka);
|
---|
41 | sa_size_t gpas = a.Size(ka)*step;
|
---|
42 | sa_size_t naxa = a.Size()/a.Size(ka);
|
---|
43 | for(j=0; j<naxa; j++) {
|
---|
44 | pe = a.DataBlock().Begin()+a.Offset(ka,j);
|
---|
45 | for(k=0; k<gpas; k+=step) pe[k] = (T)(f((double)pe[k]));
|
---|
46 | }
|
---|
47 | }
|
---|
48 | return(a);
|
---|
49 | }
|
---|
50 |
|
---|
51 | //! Apply Function In Place (function float version)
|
---|
52 | /*!
|
---|
53 | \param a : array to be replaced in place
|
---|
54 | \param f : function for replacement
|
---|
55 | \return Return an array \b a filled with function f(a(i,j))
|
---|
56 | */
|
---|
57 | template <class T>
|
---|
58 | TArray<T>& MathArray<T>::ApplyFunctionInPlace(TArray<T> & a, Arr_FloatFunctionOfX f)
|
---|
59 | {
|
---|
60 | if (a.NbDimensions() < 1)
|
---|
61 | throw RangeCheckError("MathArray<T>::ApplyFunctionInPlace(TArray<T> & a..) Not Allocated Array a !");
|
---|
62 | T * pe;
|
---|
63 | sa_size_t j,k;
|
---|
64 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
65 | sa_size_t step = a.AvgStep();
|
---|
66 | sa_size_t maxx = a.Size()*step;
|
---|
67 | pe = a.Data();
|
---|
68 | for(k=0; k<maxx; k+=step ) pe[k] = (T)(f((float)pe[k]));
|
---|
69 | }
|
---|
70 | else { // Non regular data spacing ...
|
---|
71 | int_4 ka = a.MaxSizeKA();
|
---|
72 | sa_size_t step = a.Step(ka);
|
---|
73 | sa_size_t gpas = a.Size(ka)*step;
|
---|
74 | sa_size_t naxa = a.Size()/a.Size(ka);
|
---|
75 | for(j=0; j<naxa; j++) {
|
---|
76 | pe = a.DataBlock().Begin()+a.Offset(ka,j);
|
---|
77 | for(k=0; k<gpas; k+=step) pe[k] = (T)(f((float)pe[k]));
|
---|
78 | }
|
---|
79 | }
|
---|
80 | return(a);
|
---|
81 | }
|
---|
82 |
|
---|
83 |
|
---|
84 | //! Apply Function (function double version)
|
---|
85 | /*!
|
---|
86 | \param a : argument array of the function
|
---|
87 | \param f : function for replacement
|
---|
88 | \return Return a new array filled with function f(a(i,j))
|
---|
89 | */
|
---|
90 | template <class T>
|
---|
91 | TArray<T> MathArray<T>::ApplyFunction(TArray<T> const & a, Arr_DoubleFunctionOfX f)
|
---|
92 | {
|
---|
93 | TArray<T> ra;
|
---|
94 | ra = a;
|
---|
95 | ApplyFunctionInPlace(ra, f);
|
---|
96 | return(ra);
|
---|
97 | }
|
---|
98 |
|
---|
99 | //! Apply Function (function float version)
|
---|
100 | /*!
|
---|
101 | \param a : argument array of the function
|
---|
102 | \param f : function for replacement
|
---|
103 | \return Return a new array filled with function f(a(i,j))
|
---|
104 | */
|
---|
105 | template <class T>
|
---|
106 | TArray<T> MathArray<T>::ApplyFunction(TArray<T> const & a, Arr_FloatFunctionOfX f)
|
---|
107 | {
|
---|
108 | TArray<T> ra;
|
---|
109 | ra = a;
|
---|
110 | ApplyFunctionInPlace(ra, f);
|
---|
111 | return(ra);
|
---|
112 | }
|
---|
113 |
|
---|
114 | //! Compute \b mean and \b sigma of elements of array \b a, return \b mean
|
---|
115 | template <class T>
|
---|
116 | double MathArray<T>::MeanSigma(TArray<T> const & a, double & mean, double & sig)
|
---|
117 | {
|
---|
118 | if (a.NbDimensions() < 1)
|
---|
119 | throw RangeCheckError("MathArray<T>::MeanSigma(TArray<T> const & a..) Not Allocated Array a !");
|
---|
120 | const T * pe;
|
---|
121 | sa_size_t j,k;
|
---|
122 | mean=0.;
|
---|
123 | sig = 0.;
|
---|
124 | double valok;
|
---|
125 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
126 | sa_size_t step = a.AvgStep();
|
---|
127 | sa_size_t maxx = a.Size()*step;
|
---|
128 | pe = a.Data();
|
---|
129 | for(k=0; k<maxx; k+=step ) {
|
---|
130 | valok = (double) pe[k];
|
---|
131 | mean += valok; sig += valok*valok;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | else { // Non regular data spacing ...
|
---|
135 | int_4 ka = a.MaxSizeKA();
|
---|
136 | sa_size_t step = a.Step(ka);
|
---|
137 | sa_size_t gpas = a.Size(ka)*step;
|
---|
138 | sa_size_t naxa = a.Size()/a.Size(ka);
|
---|
139 | for(j=0; j<naxa; j++) {
|
---|
140 | pe = a.DataBlock().Begin()+a.Offset(ka,j);
|
---|
141 | for(k=0; k<gpas; k+=step) {
|
---|
142 | valok = (double) pe[k];
|
---|
143 | mean += valok; sig += valok*valok;
|
---|
144 | }
|
---|
145 | }
|
---|
146 | }
|
---|
147 | double dsz = (double)(a.Size());
|
---|
148 | mean /= dsz;
|
---|
149 | sig = sig/dsz - mean*mean;
|
---|
150 | #if !defined(__GNUG__) && !defined (__MWERKS__)
|
---|
151 | if (sig >= 0.) sig = sqrt(sig);
|
---|
152 | #else
|
---|
153 | #if defined (__MWERKS__)
|
---|
154 | // le std pour que CW comprenne que sqrt(double) vient de math.h!
|
---|
155 | if (sig >= 0.) sig = std::sqrt(sig);
|
---|
156 | #else
|
---|
157 | // va comprendre pourquoi g++ veut ca pour faire la generation
|
---|
158 | // de template !!!!
|
---|
159 | if (sig >= 0.) sig = _Sqrt_(sig);
|
---|
160 | #endif
|
---|
161 | #endif
|
---|
162 | return(mean);
|
---|
163 | }
|
---|
164 |
|
---|
165 | ///////////////////////////////////////////////////////////////
|
---|
166 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
167 | #pragma define_template MathArray<r_4>
|
---|
168 | #pragma define_template MathArray<r_8>
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
172 | template class MathArray<r_4>;
|
---|
173 | template class MathArray<r_8>;
|
---|
174 | #endif
|
---|