[787] | 1 | // This may look like C code, but it is really -*- C++ -*-
|
---|
| 2 | // Usuall mathematical functions and operations on arrays
|
---|
| 3 | // R. Ansari, C.Magneville 03/2000
|
---|
| 4 |
|
---|
| 5 | #ifndef MathArray_SEEN
|
---|
| 6 | #define MathArray_SEEN
|
---|
| 7 |
|
---|
| 8 | #include "machdefs.h"
|
---|
[882] | 9 | #include <math.h>
|
---|
[787] | 10 | #include "tarray.h"
|
---|
| 11 |
|
---|
| 12 | namespace SOPHYA {
|
---|
| 13 |
|
---|
[894] | 14 | //! Class for simple mathematical operation on arrays
|
---|
[787] | 15 | template <class T>
|
---|
| 16 | class MathArray {
|
---|
| 17 | public:
|
---|
| 18 | // Applying a function
|
---|
| 19 | // Replaces the input array content with the result f(x)
|
---|
| 20 | virtual TArray<T>& ApplyFunctionInPlace(TArray<T> & a, Arr_DoubleFunctionOfX f);
|
---|
| 21 | virtual TArray<T>& ApplyFunctionInPlace(TArray<T> & a, Arr_FloatFunctionOfX f);
|
---|
| 22 | // Creates a new array and fills it with f(x)
|
---|
| 23 | virtual TArray<T> ApplyFunction(TArray<T> const & a, Arr_DoubleFunctionOfX f);
|
---|
| 24 | virtual TArray<T> ApplyFunction(TArray<T> const & a, Arr_FloatFunctionOfX f);
|
---|
[804] | 25 | // Computing Mean-Sigma
|
---|
| 26 | virtual double MeanSigma(TArray<T> const & a, double & mean, double & sig);
|
---|
[787] | 27 | };
|
---|
| 28 |
|
---|
| 29 | ////////////////////////////////////////////////
|
---|
| 30 | // Calcul de fonctions usuelles
|
---|
[882] | 31 |
|
---|
| 32 | #if !defined(OS_LINUX) && !defined (__KCC__)
|
---|
| 33 | // see below for g++ on Linux
|
---|
[787] | 34 | template <class T>
|
---|
[882] | 35 | inline TArray<T> fabs(const TArray<T>& a)
|
---|
| 36 | { MathArray<T> ma; return( ma.ApplyFunction(a, fabs) ); }
|
---|
| 37 |
|
---|
| 38 | template <class T>
|
---|
| 39 | inline TArray<T> sqrt(const TArray<T>& a)
|
---|
| 40 | { MathArray<T> ma; return( ma.ApplyFunction(a, sqrt) ); }
|
---|
| 41 |
|
---|
| 42 | template <class T>
|
---|
[787] | 43 | inline TArray<T> sin(const TArray<T>& a)
|
---|
| 44 | { MathArray<T> ma; return( ma.ApplyFunction(a, sin) ); }
|
---|
| 45 |
|
---|
| 46 | template <class T>
|
---|
| 47 | inline TArray<T> cos(const TArray<T>& a)
|
---|
| 48 | { MathArray<T> ma; return( ma.ApplyFunction(a, cos) ); }
|
---|
| 49 |
|
---|
| 50 | template <class T>
|
---|
| 51 | inline TArray<T> tan(const TArray<T>& a)
|
---|
| 52 | { MathArray<T> ma; return( ma.ApplyFunction(a, tan) ); }
|
---|
| 53 |
|
---|
[804] | 54 | template <class T>
|
---|
| 55 | inline TArray<T> asin(const TArray<T>& a)
|
---|
| 56 | { MathArray<T> ma; return( ma.ApplyFunction(a, asin) ); }
|
---|
| 57 |
|
---|
| 58 | template <class T>
|
---|
| 59 | inline TArray<T> acos(const TArray<T>& a)
|
---|
| 60 | { MathArray<T> ma; return( ma.ApplyFunction(a, acos) ); }
|
---|
| 61 |
|
---|
| 62 | template <class T>
|
---|
| 63 | inline TArray<T> atan(const TArray<T>& a)
|
---|
| 64 | { MathArray<T> ma; return( ma.ApplyFunction(a, atan) ); }
|
---|
| 65 |
|
---|
| 66 | template <class T>
|
---|
| 67 | inline TArray<T> exp(const TArray<T>& a)
|
---|
| 68 | { MathArray<T> ma; return( ma.ApplyFunction(a, exp) ); }
|
---|
| 69 |
|
---|
| 70 | template <class T>
|
---|
| 71 | inline TArray<T> log(const TArray<T>& a)
|
---|
| 72 | { MathArray<T> ma; return( ma.ApplyFunction(a, log) ); }
|
---|
| 73 |
|
---|
| 74 | template <class T>
|
---|
| 75 | inline TArray<T> log10(const TArray<T>& a)
|
---|
| 76 | { MathArray<T> ma; return( ma.ApplyFunction(a, log10) ); }
|
---|
| 77 |
|
---|
[882] | 78 | #else
|
---|
| 79 | // g++ (up to 2.95.1) doesn't handle these inline template
|
---|
| 80 | // functions correctly - with math functions defined in math.h
|
---|
| 81 | #include "gcc_arrmath_pb.h"
|
---|
| 82 | #endif
|
---|
| 83 |
|
---|
[894] | 84 | //! Return \b mean and \b sigma of elements of array \b a
|
---|
[804] | 85 | template <class T>
|
---|
| 86 | inline double MeanSigma(const TArray<T>& a, double & mean, double & sig)
|
---|
| 87 | { MathArray<T> ma; return( ma.MeanSigma(a, mean, sig) ); }
|
---|
| 88 |
|
---|
[894] | 89 | //! Return \b mean of elements of array \b a
|
---|
[804] | 90 | template <class T>
|
---|
| 91 | inline double Mean(const TArray<T>& a)
|
---|
| 92 | { MathArray<T> ma; double mean, sig; return( ma.MeanSigma(a, mean, sig) ); }
|
---|
| 93 |
|
---|
[787] | 94 | } // Fin du namespace
|
---|
| 95 |
|
---|
| 96 | #endif
|
---|