| 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>&  ApplyFunctionInPlaceF(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>  ApplyFunctionF(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 | // see below for g++   
 | 
|---|
| 33 | /*! \ingroup TArray \fn Fabs(const TArray<T>& a)
 | 
|---|
| 34 |   \brief computation on TArray */
 | 
|---|
| 35 | template <class T>
 | 
|---|
| 36 | inline TArray<T> Fabs(const TArray<T>& a)
 | 
|---|
| 37 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, fabs) ); }
 | 
|---|
| 38 | 
 | 
|---|
| 39 | /*! \ingroup TArray \fn Sqrt(const TArray<T>& a)
 | 
|---|
| 40 |   \brief computation on TArray */
 | 
|---|
| 41 | template <class T>
 | 
|---|
| 42 | inline TArray<T> Sqrt(const TArray<T>& a)
 | 
|---|
| 43 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, sqrt) ); }
 | 
|---|
| 44 | 
 | 
|---|
| 45 | /*! \ingroup TArray \fn Sin(const TArray<T>& a)
 | 
|---|
| 46 |   \brief computation on TArray */
 | 
|---|
| 47 | template <class T>
 | 
|---|
| 48 | inline TArray<T> Sin(const TArray<T>& a)
 | 
|---|
| 49 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, sin) ); }
 | 
|---|
| 50 | 
 | 
|---|
| 51 | /*! \ingroup TArray \fn Cos(const TArray<T>& a)
 | 
|---|
| 52 |   \brief computation on TArray */
 | 
|---|
| 53 | template <class T>
 | 
|---|
| 54 | inline TArray<T> Cos(const TArray<T>& a)
 | 
|---|
| 55 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, cos) ); }
 | 
|---|
| 56 | 
 | 
|---|
| 57 | /*! \ingroup TArray \fn tan(const TArray<T>& a)
 | 
|---|
| 58 |   \brief computation on TArray */
 | 
|---|
| 59 | template <class T>
 | 
|---|
| 60 | inline TArray<T> Tan(const TArray<T>& a)
 | 
|---|
| 61 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, tan) ); }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | /*! \ingroup TArray \fn Asin(const TArray<T>& a)
 | 
|---|
| 64 |   \brief computation on TArray */
 | 
|---|
| 65 | template <class T>
 | 
|---|
| 66 | inline TArray<T> Asin(const TArray<T>& a)
 | 
|---|
| 67 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, asin) ); }
 | 
|---|
| 68 | 
 | 
|---|
| 69 | /*! \ingroup TArray \fn Acos(const TArray<T>& a)
 | 
|---|
| 70 |   \brief computation on TArray */
 | 
|---|
| 71 | template <class T>
 | 
|---|
| 72 | inline TArray<T> Acos(const TArray<T>& a)
 | 
|---|
| 73 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, acos) ); }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | /*! \ingroup TArray \fn Atan(const TArray<T>& a)
 | 
|---|
| 76 |   \brief computation on TArray */
 | 
|---|
| 77 | template <class T>
 | 
|---|
| 78 | inline TArray<T> Atan(const TArray<T>& a)
 | 
|---|
| 79 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, atan) ); }
 | 
|---|
| 80 | 
 | 
|---|
| 81 | /*! \ingroup TArray \fn Exp(const TArray<T>& a)
 | 
|---|
| 82 |   \brief computation on TArray */
 | 
|---|
| 83 | template <class T>
 | 
|---|
| 84 | inline TArray<T> Exp(const TArray<T>& a)
 | 
|---|
| 85 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, exp) ); }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | /*! \ingroup TArray \fn Log(const TArray<T>& a)
 | 
|---|
| 88 |   \brief computation on TArray */
 | 
|---|
| 89 | template <class T>
 | 
|---|
| 90 | inline TArray<T> Log(const TArray<T>& a)
 | 
|---|
| 91 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, log) ); }
 | 
|---|
| 92 | 
 | 
|---|
| 93 | /*! \ingroup TArray \fn Log10(const TArray<T>& a)
 | 
|---|
| 94 |   \brief computation on TArray */
 | 
|---|
| 95 | template <class T>
 | 
|---|
| 96 | inline TArray<T> Log10(const TArray<T>& a)
 | 
|---|
| 97 |   { MathArray<T> ma;   return( ma.ApplyFunction(a, log10) ); }
 | 
|---|
| 98 | 
 | 
|---|
| 99 | 
 | 
|---|
| 100 | /*! \ingroup TArray \fn MeanSigma(const TArray<T>&,double&,double&)
 | 
|---|
| 101 |   \brief Return \b mean and \b sigma of elements of array \b a */
 | 
|---|
| 102 | template <class T>
 | 
|---|
| 103 | inline double MeanSigma(const TArray<T>& a, double & mean, double & sig)
 | 
|---|
| 104 |   { MathArray<T> ma;   return( ma.MeanSigma(a, mean, sig) ); }
 | 
|---|
| 105 | 
 | 
|---|
| 106 | /*! \ingroup TArray \fn Mean(const TArray<T>&)
 | 
|---|
| 107 |   \brief Return \b mean of elements of array \b a */
 | 
|---|
| 108 | template <class T>
 | 
|---|
| 109 | inline double Mean(const TArray<T>& a)
 | 
|---|
| 110 |   { MathArray<T> ma;  double mean, sig;  return( ma.MeanSigma(a, mean, sig) ); }
 | 
|---|
| 111 | 
 | 
|---|
| 112 | 
 | 
|---|
| 113 | //! Class for simple mathematical operation on complex arrays 
 | 
|---|
| 114 | template <class T>
 | 
|---|
| 115 | class ComplexMathArray {
 | 
|---|
| 116 | public:
 | 
|---|
| 117 | // Applying a function 
 | 
|---|
| 118 |   // Replaces the input array content with the result f(x)
 | 
|---|
| 119 |   virtual TArray< complex<T> >& ApplyFunctionInPlace(TArray< complex<T> >& a, 
 | 
|---|
| 120 |                                                      Arr_ComplexDoubleFunctionOfX f);
 | 
|---|
| 121 |   // Creates a new array and fills it with f(x)
 | 
|---|
| 122 |   virtual TArray< complex<T> > ApplyFunction(TArray< complex<T> > const & a, 
 | 
|---|
| 123 |                                              Arr_ComplexDoubleFunctionOfX f);
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   // Creates a new array and fills it with f(x)
 | 
|---|
| 126 |   virtual TArray< complex<T> > FillFrom(TArray<T> const & p_real,
 | 
|---|
| 127 |                                         TArray<T> const & p_imag);
 | 
|---|
| 128 | 
 | 
|---|
| 129 |   // Returns real-imaginary part of the complex array
 | 
|---|
| 130 |   virtual TArray<T> real(TArray< complex<T> >  const & a);
 | 
|---|
| 131 |   virtual TArray<T> imag(TArray< complex<T> >  const & a);
 | 
|---|
| 132 |   
 | 
|---|
| 133 |   // Returns module and phase of the complex input array 
 | 
|---|
| 134 |   virtual TArray<T> module2(TArray< complex<T> >  const & a);
 | 
|---|
| 135 |   virtual TArray<T> module(TArray< complex<T> >  const & a);
 | 
|---|
| 136 |   virtual TArray<T> phase(TArray< complex<T> >  const & a);  
 | 
|---|
| 137 | };
 | 
|---|
| 138 | 
 | 
|---|
| 139 | /*! \ingroup TArray \fn real(const TArray< complex<T> >&)
 | 
|---|
| 140 |   \brief Return the \b real part of the input complex array \b a */
 | 
|---|
| 141 | template <class T>
 | 
|---|
| 142 | inline TArray<T> real(const TArray< complex<T> >& a)
 | 
|---|
| 143 |   { ComplexMathArray<T> cma;  return( cma.real(a) ); }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | /*! \ingroup TArray \fn imag(const TArray< complex<T> >&)
 | 
|---|
| 146 |   \brief Return the \b imaginary part of the input complex array \b a */
 | 
|---|
| 147 | template <class T>
 | 
|---|
| 148 | inline TArray<T> imag(const TArray< complex<T> >& a)
 | 
|---|
| 149 |   { ComplexMathArray<T> cma;  return( cma.imag(a) ); }
 | 
|---|
| 150 | 
 | 
|---|
| 151 | /*! \ingroup TArray \fn module2(const TArray< complex<T> >&)
 | 
|---|
| 152 |   \brief Return the \b module squared of the input complex array \b a */
 | 
|---|
| 153 | template <class T>
 | 
|---|
| 154 | inline TArray<T> module2(const TArray< complex<T> >& a)
 | 
|---|
| 155 |   { ComplexMathArray<T> cma;  return( cma.module2(a) ); }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | /*! \ingroup TArray \fn module(const TArray< complex<T> >&)
 | 
|---|
| 158 |   \brief Return the \b module of the input complex array \b a */
 | 
|---|
| 159 | template <class T>
 | 
|---|
| 160 | inline TArray<T> module(const TArray< complex<T> >& a)
 | 
|---|
| 161 |   { ComplexMathArray<T> cma;  return( cma.module(a) ); }
 | 
|---|
| 162 | 
 | 
|---|
| 163 | /*! \ingroup TArray \fn phase(const TArray< complex<T> >&)
 | 
|---|
| 164 |   \brief Return the \b phase of the input complex array \b a */
 | 
|---|
| 165 | template <class T>
 | 
|---|
| 166 | inline TArray<T> phase(const TArray< complex<T> >& a)
 | 
|---|
| 167 |   { ComplexMathArray<T> cma;  return( cma.phase(a) ); }
 | 
|---|
| 168 | 
 | 
|---|
| 169 | /*! \ingroup TArray \fn ComplexArray(const TArray<T> &, const TArray<T> &)
 | 
|---|
| 170 |   \brief Return a complex array, with real and imaginary parts filled from the arguments  */
 | 
|---|
| 171 | template <class T>
 | 
|---|
| 172 | inline TArray< complex<T> > ComplexArray(const TArray<T> & p_real,
 | 
|---|
| 173 |                                          const TArray<T> & p_imag)
 | 
|---|
| 174 |   { ComplexMathArray<T> cma;  return( cma.FillFrom(p_real, p_imag) ); }
 | 
|---|
| 175 | 
 | 
|---|
| 176 | 
 | 
|---|
| 177 | } // Fin du namespace
 | 
|---|
| 178 | 
 | 
|---|
| 179 | #endif
 | 
|---|