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

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

Pb compil matharr.h sur g++/linux , Reza 11/4/2000

File size: 3.8 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
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 uint_8 naxa = a.Size()/a.Size(ka);
30 for(j=0; j<naxa; j++) {
31 pe = a.DataBlock().Begin()+a.Offset(ka,j);
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{
41 if (a.NbDimensions() < 1)
42 throw RangeCheckError("MathArray<T>::ApplyFunctionInPlace(TArray<T> & a..) Not Allocated Array a !");
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;
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);
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
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;
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);
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 !defined(OS_LINUX) && !defined (__KCC__)
119 if (sig >= 0.) sig = sqrt(sig);
120#else
121 // va comprendre pourquoi g++ (sur Linux) veut ca pour faire la generation
122 // de template !!!!
123 if (sig >= 0.) sig = _Sqrt_(sig);
124#endif
125 return(mean);
126}
127
128///////////////////////////////////////////////////////////////
129#ifdef __CXX_PRAGMA_TEMPLATES__
130#pragma define_template MathArray<r_4>
131#pragma define_template MathArray<r_8>
132#endif
133
134#if defined(ANSI_TEMPLATES) || defined(GNU_TEMPLATES)
135template class MathArray<r_4>;
136template class MathArray<r_8>;
137#endif
Note: See TracBrowser for help on using the repository browser.