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 | //! Apply Function In Place (function double version)
|
---|
14 | /*!
|
---|
15 | \param a : array to be replaced in place
|
---|
16 | \param f : function for replacement
|
---|
17 | \return Return an array \b a filled with function f(a(i,j))
|
---|
18 | */
|
---|
19 | template <class T>
|
---|
20 | TArray<T>& MathArray<T>::ApplyFunctionInPlace(TArray<T> & a, Arr_DoubleFunctionOfX f)
|
---|
21 | {
|
---|
22 | if (a.NbDimensions() < 1)
|
---|
23 | throw RangeCheckError("MathArray<T>::ApplyFunctionInPlace(TArray<T> & a..) Not Allocated Array a !");
|
---|
24 | T * pe;
|
---|
25 | uint_8 j,k;
|
---|
26 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
27 | uint_8 step = a.AvgStep();
|
---|
28 | uint_8 maxx = a.Size()*step;
|
---|
29 | pe = a.Data();
|
---|
30 | for(k=0; k<maxx; k+=step ) pe[k] = (T)(f((double)pe[k]));
|
---|
31 | }
|
---|
32 | else { // Non regular data spacing ...
|
---|
33 | uint_4 ka = a.MaxSizeKA();
|
---|
34 | uint_8 step = a.Step(ka);
|
---|
35 | uint_8 gpas = a.Size(ka)*step;
|
---|
36 | uint_8 naxa = a.Size()/a.Size(ka);
|
---|
37 | for(j=0; j<naxa; j++) {
|
---|
38 | pe = a.DataBlock().Begin()+a.Offset(ka,j);
|
---|
39 | for(k=0; k<gpas; k+=step) pe[k] = (T)(f((double)pe[k]));
|
---|
40 | }
|
---|
41 | }
|
---|
42 | return(a);
|
---|
43 | }
|
---|
44 |
|
---|
45 | //! Apply Function In Place (function float version)
|
---|
46 | /*!
|
---|
47 | \param a : array to be replaced in place
|
---|
48 | \param f : function for replacement
|
---|
49 | \return Return an array \b a filled with function f(a(i,j))
|
---|
50 | */
|
---|
51 | template <class T>
|
---|
52 | TArray<T>& MathArray<T>::ApplyFunctionInPlace(TArray<T> & a, Arr_FloatFunctionOfX f)
|
---|
53 | {
|
---|
54 | if (a.NbDimensions() < 1)
|
---|
55 | throw RangeCheckError("MathArray<T>::ApplyFunctionInPlace(TArray<T> & a..) Not Allocated Array a !");
|
---|
56 | T * pe;
|
---|
57 | uint_8 j,k;
|
---|
58 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
59 | uint_8 step = a.AvgStep();
|
---|
60 | uint_8 maxx = a.Size()*step;
|
---|
61 | pe = a.Data();
|
---|
62 | for(k=0; k<maxx; k+=step ) pe[k] = (T)(f((float)pe[k]));
|
---|
63 | }
|
---|
64 | else { // Non regular data spacing ...
|
---|
65 | uint_4 ka = a.MaxSizeKA();
|
---|
66 | uint_8 step = a.Step(ka);
|
---|
67 | uint_8 gpas = a.Size(ka)*step;
|
---|
68 | uint_8 naxa = a.Size()/a.Size(ka);
|
---|
69 | for(j=0; j<naxa; j++) {
|
---|
70 | pe = a.DataBlock().Begin()+a.Offset(ka,j);
|
---|
71 | for(k=0; k<gpas; k+=step) pe[k] = (T)(f((float)pe[k]));
|
---|
72 | }
|
---|
73 | }
|
---|
74 | return(a);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | //! Apply Function (function double version)
|
---|
79 | /*!
|
---|
80 | \param a : argument array of the function
|
---|
81 | \param f : function for replacement
|
---|
82 | \return Return a new array filled with function f(a(i,j))
|
---|
83 | */
|
---|
84 | template <class T>
|
---|
85 | TArray<T> MathArray<T>::ApplyFunction(TArray<T> const & a, Arr_DoubleFunctionOfX f)
|
---|
86 | {
|
---|
87 | TArray<T> ra;
|
---|
88 | ra = a;
|
---|
89 | ApplyFunctionInPlace(ra, f);
|
---|
90 | return(ra);
|
---|
91 | }
|
---|
92 |
|
---|
93 | //! Apply Function (function float version)
|
---|
94 | /*!
|
---|
95 | \param a : argument array of the function
|
---|
96 | \param f : function for replacement
|
---|
97 | \return Return a new array filled with function f(a(i,j))
|
---|
98 | */
|
---|
99 | template <class T>
|
---|
100 | TArray<T> MathArray<T>::ApplyFunction(TArray<T> const & a, Arr_FloatFunctionOfX f)
|
---|
101 | {
|
---|
102 | TArray<T> ra;
|
---|
103 | ra = a;
|
---|
104 | ApplyFunctionInPlace(ra, f);
|
---|
105 | return(ra);
|
---|
106 | }
|
---|
107 |
|
---|
108 | //! Compute \b mean and \b sigma of elements of array \b a, return \b mean
|
---|
109 | template <class T>
|
---|
110 | double MathArray<T>::MeanSigma(TArray<T> const & a, double & mean, double & sig)
|
---|
111 | {
|
---|
112 | if (a.NbDimensions() < 1)
|
---|
113 | throw RangeCheckError("MathArray<T>::MeanSigma(TArray<T> const & a..) Not Allocated Array a !");
|
---|
114 | const T * pe;
|
---|
115 | uint_8 j,k;
|
---|
116 | mean=0.;
|
---|
117 | sig = 0.;
|
---|
118 | double valok;
|
---|
119 | if (a.AvgStep() > 0) { // regularly spaced elements
|
---|
120 | uint_8 step = a.AvgStep();
|
---|
121 | uint_8 maxx = a.Size()*step;
|
---|
122 | pe = a.Data();
|
---|
123 | for(k=0; k<maxx; k+=step ) {
|
---|
124 | valok = (double) pe[k];
|
---|
125 | mean += valok; sig += valok*valok;
|
---|
126 | }
|
---|
127 | }
|
---|
128 | else { // Non regular data spacing ...
|
---|
129 | uint_4 ka = a.MaxSizeKA();
|
---|
130 | uint_8 step = a.Step(ka);
|
---|
131 | uint_8 gpas = a.Size(ka)*step;
|
---|
132 | uint_8 naxa = a.Size()/a.Size(ka);
|
---|
133 | for(j=0; j<naxa; j++) {
|
---|
134 | pe = a.DataBlock().Begin()+a.Offset(ka,j);
|
---|
135 | for(k=0; k<gpas; k+=step) {
|
---|
136 | valok = (double) pe[k];
|
---|
137 | mean += valok; sig += valok*valok;
|
---|
138 | }
|
---|
139 | }
|
---|
140 | }
|
---|
141 | double dsz = (double)(a.Size());
|
---|
142 | mean /= dsz;
|
---|
143 | sig = sig/dsz - mean*mean;
|
---|
144 | #if !defined(OS_LINUX) && !defined (__KCC__)
|
---|
145 | if (sig >= 0.) sig = sqrt(sig);
|
---|
146 | #else
|
---|
147 | // va comprendre pourquoi g++ (sur Linux) veut ca pour faire la generation
|
---|
148 | // de template !!!!
|
---|
149 | if (sig >= 0.) sig = _Sqrt_(sig);
|
---|
150 | #endif
|
---|
151 | return(mean);
|
---|
152 | }
|
---|
153 |
|
---|
154 | ///////////////////////////////////////////////////////////////
|
---|
155 | #ifdef __CXX_PRAGMA_TEMPLATES__
|
---|
156 | #pragma define_template MathArray<r_4>
|
---|
157 | #pragma define_template MathArray<r_8>
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | #if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
|
---|
161 | template class MathArray<r_4>;
|
---|
162 | template class MathArray<r_8>;
|
---|
163 | #endif
|
---|