| 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" | 
|---|
| 9 | #include <math.h> | 
|---|
| 10 | #include "tarray.h" | 
|---|
| 11 |  | 
|---|
| 12 | namespace SOPHYA { | 
|---|
| 13 |  | 
|---|
| 14 | //! Class for simple mathematical operation on arrays | 
|---|
| 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); | 
|---|
| 25 | // Computing Mean-Sigma | 
|---|
| 26 | virtual double     MeanSigma(TArray<T> const & a, double & mean, double & sig); | 
|---|
| 27 | }; | 
|---|
| 28 |  | 
|---|
| 29 | //////////////////////////////////////////////// | 
|---|
| 30 | //  Calcul de fonctions usuelles | 
|---|
| 31 |  | 
|---|
| 32 | // #if !defined(OS_LINUX) && !defined (__KCC__) | 
|---|
| 33 | #if !defined(__GNUG__) | 
|---|
| 34 | // see below for g++ | 
|---|
| 35 | /*! \ingroup TArray \fn fabs(const TArray<T>& a) | 
|---|
| 36 | \brief computation on TArray */ | 
|---|
| 37 | template <class T> | 
|---|
| 38 | inline TArray<T> fabs(const TArray<T>& a) | 
|---|
| 39 | { MathArray<T> ma;   return( ma.ApplyFunction(a, fabs) ); } | 
|---|
| 40 |  | 
|---|
| 41 | /*! \ingroup TArray \fn sqrt(const TArray<T>& a) | 
|---|
| 42 | \brief computation on TArray */ | 
|---|
| 43 | template <class T> | 
|---|
| 44 | inline TArray<T> sqrt(const TArray<T>& a) | 
|---|
| 45 | { MathArray<T> ma;   return( ma.ApplyFunction(a, sqrt) ); } | 
|---|
| 46 |  | 
|---|
| 47 | /*! \ingroup TArray \fn sin(const TArray<T>& a) | 
|---|
| 48 | \brief computation on TArray */ | 
|---|
| 49 | template <class T> | 
|---|
| 50 | inline TArray<T> sin(const TArray<T>& a) | 
|---|
| 51 | { MathArray<T> ma;   return( ma.ApplyFunction(a, sin) ); } | 
|---|
| 52 |  | 
|---|
| 53 | /*! \ingroup TArray \fn cos(const TArray<T>& a) | 
|---|
| 54 | \brief computation on TArray */ | 
|---|
| 55 | template <class T> | 
|---|
| 56 | inline TArray<T> cos(const TArray<T>& a) | 
|---|
| 57 | { MathArray<T> ma;   return( ma.ApplyFunction(a, cos) ); } | 
|---|
| 58 |  | 
|---|
| 59 | /*! \ingroup TArray \fn tan(const TArray<T>& a) | 
|---|
| 60 | \brief computation on TArray */ | 
|---|
| 61 | template <class T> | 
|---|
| 62 | inline TArray<T> tan(const TArray<T>& a) | 
|---|
| 63 | { MathArray<T> ma;   return( ma.ApplyFunction(a, tan) ); } | 
|---|
| 64 |  | 
|---|
| 65 | /*! \ingroup TArray \fn asin(const TArray<T>& a) | 
|---|
| 66 | \brief computation on TArray */ | 
|---|
| 67 | template <class T> | 
|---|
| 68 | inline TArray<T> asin(const TArray<T>& a) | 
|---|
| 69 | { MathArray<T> ma;   return( ma.ApplyFunction(a, asin) ); } | 
|---|
| 70 |  | 
|---|
| 71 | /*! \ingroup TArray \fn acos(const TArray<T>& a) | 
|---|
| 72 | \brief computation on TArray */ | 
|---|
| 73 | template <class T> | 
|---|
| 74 | inline TArray<T> acos(const TArray<T>& a) | 
|---|
| 75 | { MathArray<T> ma;   return( ma.ApplyFunction(a, acos) ); } | 
|---|
| 76 |  | 
|---|
| 77 | /*! \ingroup TArray \fn atan(const TArray<T>& a) | 
|---|
| 78 | \brief computation on TArray */ | 
|---|
| 79 | template <class T> | 
|---|
| 80 | inline TArray<T> atan(const TArray<T>& a) | 
|---|
| 81 | { MathArray<T> ma;   return( ma.ApplyFunction(a, atan) ); } | 
|---|
| 82 |  | 
|---|
| 83 | /*! \ingroup TArray \fn exp(const TArray<T>& a) | 
|---|
| 84 | \brief computation on TArray */ | 
|---|
| 85 | template <class T> | 
|---|
| 86 | inline TArray<T> exp(const TArray<T>& a) | 
|---|
| 87 | { MathArray<T> ma;   return( ma.ApplyFunction(a, exp) ); } | 
|---|
| 88 |  | 
|---|
| 89 | /*! \ingroup TArray \fn lof(const TArray<T>& a) | 
|---|
| 90 | \brief computation on TArray */ | 
|---|
| 91 | template <class T> | 
|---|
| 92 | inline TArray<T> log(const TArray<T>& a) | 
|---|
| 93 | { MathArray<T> ma;   return( ma.ApplyFunction(a, log) ); } | 
|---|
| 94 |  | 
|---|
| 95 | /*! \ingroup TArray \fn log10(const TArray<T>& a) | 
|---|
| 96 | \brief computation on TArray */ | 
|---|
| 97 | template <class T> | 
|---|
| 98 | inline TArray<T> log10(const TArray<T>& a) | 
|---|
| 99 | { MathArray<T> ma;   return( ma.ApplyFunction(a, log10) ); } | 
|---|
| 100 |  | 
|---|
| 101 | #else | 
|---|
| 102 | // g++ (up to 2.95.1) doesn't handle these inline template | 
|---|
| 103 | // functions correctly - with math functions defined in math.h | 
|---|
| 104 | #include "gcc_arrmath_pb.h" | 
|---|
| 105 | #endif | 
|---|
| 106 |  | 
|---|
| 107 | /*! \ingroup TArray \fn MeanSigma(const TArray<T>&,double&,double&) | 
|---|
| 108 | \brief Return \b mean and \b sigma of elements of array \b a */ | 
|---|
| 109 | template <class T> | 
|---|
| 110 | inline double MeanSigma(const TArray<T>& a, double & mean, double & sig) | 
|---|
| 111 | { MathArray<T> ma;   return( ma.MeanSigma(a, mean, sig) ); } | 
|---|
| 112 |  | 
|---|
| 113 | /*! \ingroup TArray \fn Mean(const TArray<T>&) | 
|---|
| 114 | \brief Return \b mean of elements of array \b a */ | 
|---|
| 115 | template <class T> | 
|---|
| 116 | inline double Mean(const TArray<T>& a) | 
|---|
| 117 | { MathArray<T> ma;  double mean, sig;  return( ma.MeanSigma(a, mean, sig) ); } | 
|---|
| 118 |  | 
|---|
| 119 | } // Fin du namespace | 
|---|
| 120 |  | 
|---|
| 121 | #endif | 
|---|