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

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

Correction bug/amelioarions TArray,TMatrix,TVector - Reza 5/4/2000

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