| 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 |   virtual ~MathArray(void) {};
 | 
|---|
| 19 | 
 | 
|---|
| 20 | // Applying a function  
 | 
|---|
| 21 |   // Replaces the input array content with the result f(x)
 | 
|---|
| 22 |   virtual TArray<T>&  ApplyFunctionInPlace(TArray<T> & a, Arr_DoubleFunctionOfX f);
 | 
|---|
| 23 |   virtual TArray<T>&  ApplyFunctionInPlaceF(TArray<T> & a, Arr_FloatFunctionOfX f);
 | 
|---|
| 24 |   // Creates a new array and fills it with f(x)
 | 
|---|
| 25 |   virtual TArray<T>  ApplyFunction(TArray<T> const & a, Arr_DoubleFunctionOfX f);
 | 
|---|
| 26 |   virtual TArray<T>  ApplyFunctionF(TArray<T> const & a, Arr_FloatFunctionOfX f);
 | 
|---|
| 27 |   // Computing Mean-Sigma 
 | 
|---|
| 28 |   virtual double     MeanSigma(TArray<T> const & a, double & mean, double & sig);
 | 
|---|
| 29 | };
 | 
|---|
| 30 | 
 | 
|---|
| 31 | ////////////////////////////////////////////////
 | 
|---|
| 32 | //  Calcul de fonctions usuelles 
 | 
|---|
| 33 | 
 | 
|---|
| 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 Log(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 | 
 | 
|---|
| 102 | /*! \ingroup TArray \fn MeanSigma(const TArray<T>&,double&,double&)
 | 
|---|
| 103 |   \brief Return \b mean and \b sigma of elements of array \b a */
 | 
|---|
| 104 | template <class T>
 | 
|---|
| 105 | inline double MeanSigma(const TArray<T>& a, double & mean, double & sig)
 | 
|---|
| 106 |   { MathArray<T> ma;   return( ma.MeanSigma(a, mean, sig) ); }
 | 
|---|
| 107 | 
 | 
|---|
| 108 | /*! \ingroup TArray \fn Mean(const TArray<T>&)
 | 
|---|
| 109 |   \brief Return \b mean of elements of array \b a */
 | 
|---|
| 110 | template <class T>
 | 
|---|
| 111 | inline double Mean(const TArray<T>& a)
 | 
|---|
| 112 |   { MathArray<T> ma;  double mean, sig;  return( ma.MeanSigma(a, mean, sig) ); }
 | 
|---|
| 113 | 
 | 
|---|
| 114 | 
 | 
|---|
| 115 | //! Class for simple mathematical operation on complex arrays 
 | 
|---|
| 116 | template <class T>
 | 
|---|
| 117 | class ComplexMathArray {
 | 
|---|
| 118 | public:
 | 
|---|
| 119 |   virtual ~ComplexMathArray(void) {};
 | 
|---|
| 120 | 
 | 
|---|
| 121 | // Applying a function 
 | 
|---|
| 122 |   // Replaces the input array content with the result f(x)
 | 
|---|
| 123 |   virtual TArray< complex<T> >& ApplyFunctionInPlace(TArray< complex<T> >& a, 
 | 
|---|
| 124 |                                                      Arr_ComplexDoubleFunctionOfX f);
 | 
|---|
| 125 |   // Creates a new array and fills it with f(x)
 | 
|---|
| 126 |   virtual TArray< complex<T> > ApplyFunction(TArray< complex<T> > const & a, 
 | 
|---|
| 127 |                                              Arr_ComplexDoubleFunctionOfX f);
 | 
|---|
| 128 | 
 | 
|---|
| 129 |   // Creates a new array and fills it with f(x)
 | 
|---|
| 130 |   virtual TArray< complex<T> > FillFrom(TArray<T> const & p_real,
 | 
|---|
| 131 |                                         TArray<T> const & p_imag);
 | 
|---|
| 132 | 
 | 
|---|
| 133 |   // Returns real-imaginary part of the complex array
 | 
|---|
| 134 |   virtual TArray<T> real(TArray< complex<T> >  const & a);
 | 
|---|
| 135 |   virtual TArray<T> imag(TArray< complex<T> >  const & a);
 | 
|---|
| 136 |   
 | 
|---|
| 137 |   // Returns module and phase of the complex input array 
 | 
|---|
| 138 |   virtual TArray<T> module2(TArray< complex<T> >  const & a);
 | 
|---|
| 139 |   virtual TArray<T> module(TArray< complex<T> >  const & a);
 | 
|---|
| 140 |   virtual TArray<T> phase(TArray< complex<T> >  const & a);  
 | 
|---|
| 141 | };
 | 
|---|
| 142 | 
 | 
|---|
| 143 | /*! \ingroup TArray \fn real(const TArray< complex<T> >&)
 | 
|---|
| 144 |   \brief Return the \b real part of the input complex array \b a */
 | 
|---|
| 145 | template <class T>
 | 
|---|
| 146 | inline TArray<T> real(const TArray< complex<T> >& a)
 | 
|---|
| 147 |   { ComplexMathArray<T> cma;  return( cma.real(a) ); }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | /*! \ingroup TArray \fn imag(const TArray< complex<T> >&)
 | 
|---|
| 150 |   \brief Return the \b imaginary part of the input complex array \b a */
 | 
|---|
| 151 | template <class T>
 | 
|---|
| 152 | inline TArray<T> imag(const TArray< complex<T> >& a)
 | 
|---|
| 153 |   { ComplexMathArray<T> cma;  return( cma.imag(a) ); }
 | 
|---|
| 154 | 
 | 
|---|
| 155 | /*! \ingroup TArray \fn module2(const TArray< complex<T> >&)
 | 
|---|
| 156 |   \brief Return the \b module squared of the input complex array \b a */
 | 
|---|
| 157 | template <class T>
 | 
|---|
| 158 | inline TArray<T> module2(const TArray< complex<T> >& a)
 | 
|---|
| 159 |   { ComplexMathArray<T> cma;  return( cma.module2(a) ); }
 | 
|---|
| 160 | 
 | 
|---|
| 161 | /*! \ingroup TArray \fn module(const TArray< complex<T> >&)
 | 
|---|
| 162 |   \brief Return the \b module of the input complex array \b a */
 | 
|---|
| 163 | template <class T>
 | 
|---|
| 164 | inline TArray<T> module(const TArray< complex<T> >& a)
 | 
|---|
| 165 |   { ComplexMathArray<T> cma;  return( cma.module(a) ); }
 | 
|---|
| 166 | 
 | 
|---|
| 167 | /*! \ingroup TArray \fn phase(const TArray< complex<T> >&)
 | 
|---|
| 168 |   \brief Return the \b phase of the input complex array \b a */
 | 
|---|
| 169 | template <class T>
 | 
|---|
| 170 | inline TArray<T> phase(const TArray< complex<T> >& a)
 | 
|---|
| 171 |   { ComplexMathArray<T> cma;  return( cma.phase(a) ); }
 | 
|---|
| 172 | 
 | 
|---|
| 173 | /*! \ingroup TArray \fn ComplexArray(const TArray<T> &, const TArray<T> &)
 | 
|---|
| 174 |   \brief Return a complex array, with real and imaginary parts filled from the arguments  */
 | 
|---|
| 175 | template <class T>
 | 
|---|
| 176 | inline TArray< complex<T> > ComplexArray(const TArray<T> & p_real,
 | 
|---|
| 177 |                                          const TArray<T> & p_imag)
 | 
|---|
| 178 |   { ComplexMathArray<T> cma;  return( cma.FillFrom(p_real, p_imag) ); }
 | 
|---|
| 179 | 
 | 
|---|
| 180 | 
 | 
|---|
| 181 | } // Fin du namespace
 | 
|---|
| 182 | 
 | 
|---|
| 183 | #endif
 | 
|---|