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

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

Amelioation / debugging de la classe TArray<T> - TVector et TMatrix

heritent maintenant de TArray<T> - Classe RCMatrix rendu prive au fichier
sopemtx.cc - linfit.cc integre a sopemtx.cc

Reza 03/04/2000

File size: 3.5 KB
Line 
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
12template <class T>
13TArray<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
37template <class T>
38TArray<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
63template <class T>
64TArray<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
72template <class T>
73TArray<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
81template <class T>
82double 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)
126template class MathArray<r_4>;
127template class MathArray<r_8>;
128#endif
Note: See TracBrowser for help on using the repository browser.