source: Sophya/trunk/SophyaLib/TArray/matharr.cc@ 1013

Last change on this file since 1013 was 1013, checked in by ansari, 25 years ago

Correction des modifs de Dominique (DY) concernant portage Mac/CW - Reza 19/5/2000

File size: 5.0 KB
Line 
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*/
25template <class T>
26TArray<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 uint_8 j,k;
32 if (a.AvgStep() > 0) { // regularly spaced elements
33 uint_8 step = a.AvgStep();
34 uint_8 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 uint_4 ka = a.MaxSizeKA();
40 uint_8 step = a.Step(ka);
41 uint_8 gpas = a.Size(ka)*step;
42 uint_8 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*/
57template <class T>
58TArray<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 uint_8 j,k;
64 if (a.AvgStep() > 0) { // regularly spaced elements
65 uint_8 step = a.AvgStep();
66 uint_8 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 uint_4 ka = a.MaxSizeKA();
72 uint_8 step = a.Step(ka);
73 uint_8 gpas = a.Size(ka)*step;
74 uint_8 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*/
90template <class T>
91TArray<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*/
105template <class T>
106TArray<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
115template <class T>
116double 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 uint_8 j,k;
122 mean=0.;
123 sig = 0.;
124 double valok;
125 if (a.AvgStep() > 0) { // regularly spaced elements
126 uint_8 step = a.AvgStep();
127 uint_8 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 uint_4 ka = a.MaxSizeKA();
136 uint_8 step = a.Step(ka);
137 uint_8 gpas = a.Size(ka)*step;
138 uint_8 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(OS_LINUX) && !defined (__KCC__)
151#if !defined(__GNUG__) && !defined (__MWERKS__)
152 if (sig >= 0.) sig = sqrt(sig);
153#else
154#if defined (__MWERKS__)
155 // le std pour que CW comprenne que sqrt(double) vient de math.h!
156 if (sig >= 0.) sig = std::sqrt(sig);
157#else
158 // va comprendre pourquoi g++ veut ca pour faire la generation
159 // de template !!!!
160 if (sig >= 0.) sig = _Sqrt_(sig);
161#endif
162#endif
163 return(mean);
164}
165
166///////////////////////////////////////////////////////////////
167#ifdef __CXX_PRAGMA_TEMPLATES__
168#pragma define_template MathArray<r_4>
169#pragma define_template MathArray<r_8>
170#endif
171
172#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
173template class MathArray<r_4>;
174template class MathArray<r_8>;
175#endif
Note: See TracBrowser for help on using the repository browser.