| 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 | /*!
|
|---|
| 16 | \warning Instanciated only for \b real and \b double (r_4, r_8) type arrays
|
|---|
| 17 | */
|
|---|
| 18 | template <class T>
|
|---|
| 19 | class MathArray {
|
|---|
| 20 | public:
|
|---|
| 21 | // Applying a function
|
|---|
| 22 | // Replaces the input array content with the result f(x)
|
|---|
| 23 | virtual TArray<T>& ApplyFunctionInPlace(TArray<T> & a, Arr_DoubleFunctionOfX f);
|
|---|
| 24 | virtual TArray<T>& ApplyFunctionInPlace(TArray<T> & a, Arr_FloatFunctionOfX f);
|
|---|
| 25 | // Creates a new array and fills it with f(x)
|
|---|
| 26 | virtual TArray<T> ApplyFunction(TArray<T> const & a, Arr_DoubleFunctionOfX f);
|
|---|
| 27 | virtual TArray<T> ApplyFunction(TArray<T> const & a, Arr_FloatFunctionOfX f);
|
|---|
| 28 | // Computing Mean-Sigma
|
|---|
| 29 | virtual double MeanSigma(TArray<T> const & a, double & mean, double & sig);
|
|---|
| 30 | };
|
|---|
| 31 |
|
|---|
| 32 | ////////////////////////////////////////////////
|
|---|
| 33 | // Calcul de fonctions usuelles
|
|---|
| 34 |
|
|---|
| 35 | #if !defined(OS_LINUX) && !defined (__KCC__)
|
|---|
| 36 | // see below for g++ on Linux
|
|---|
| 37 | template <class T>
|
|---|
| 38 | inline TArray<T> fabs(const TArray<T>& a)
|
|---|
| 39 | { MathArray<T> ma; return( ma.ApplyFunction(a, fabs) ); }
|
|---|
| 40 |
|
|---|
| 41 | template <class T>
|
|---|
| 42 | inline TArray<T> sqrt(const TArray<T>& a)
|
|---|
| 43 | { MathArray<T> ma; return( ma.ApplyFunction(a, sqrt) ); }
|
|---|
| 44 |
|
|---|
| 45 | template <class T>
|
|---|
| 46 | inline TArray<T> sin(const TArray<T>& a)
|
|---|
| 47 | { MathArray<T> ma; return( ma.ApplyFunction(a, sin) ); }
|
|---|
| 48 |
|
|---|
| 49 | template <class T>
|
|---|
| 50 | inline TArray<T> cos(const TArray<T>& a)
|
|---|
| 51 | { MathArray<T> ma; return( ma.ApplyFunction(a, cos) ); }
|
|---|
| 52 |
|
|---|
| 53 | template <class T>
|
|---|
| 54 | inline TArray<T> tan(const TArray<T>& a)
|
|---|
| 55 | { MathArray<T> ma; return( ma.ApplyFunction(a, tan) ); }
|
|---|
| 56 |
|
|---|
| 57 | template <class T>
|
|---|
| 58 | inline TArray<T> asin(const TArray<T>& a)
|
|---|
| 59 | { MathArray<T> ma; return( ma.ApplyFunction(a, asin) ); }
|
|---|
| 60 |
|
|---|
| 61 | template <class T>
|
|---|
| 62 | inline TArray<T> acos(const TArray<T>& a)
|
|---|
| 63 | { MathArray<T> ma; return( ma.ApplyFunction(a, acos) ); }
|
|---|
| 64 |
|
|---|
| 65 | template <class T>
|
|---|
| 66 | inline TArray<T> atan(const TArray<T>& a)
|
|---|
| 67 | { MathArray<T> ma; return( ma.ApplyFunction(a, atan) ); }
|
|---|
| 68 |
|
|---|
| 69 | template <class T>
|
|---|
| 70 | inline TArray<T> exp(const TArray<T>& a)
|
|---|
| 71 | { MathArray<T> ma; return( ma.ApplyFunction(a, exp) ); }
|
|---|
| 72 |
|
|---|
| 73 | template <class T>
|
|---|
| 74 | inline TArray<T> log(const TArray<T>& a)
|
|---|
| 75 | { MathArray<T> ma; return( ma.ApplyFunction(a, log) ); }
|
|---|
| 76 |
|
|---|
| 77 | template <class T>
|
|---|
| 78 | inline TArray<T> log10(const TArray<T>& a)
|
|---|
| 79 | { MathArray<T> ma; return( ma.ApplyFunction(a, log10) ); }
|
|---|
| 80 |
|
|---|
| 81 | #else
|
|---|
| 82 | // g++ (up to 2.95.1) doesn't handle these inline template
|
|---|
| 83 | // functions correctly - with math functions defined in math.h
|
|---|
| 84 | #include "gcc_arrmath_pb.h"
|
|---|
| 85 | #endif
|
|---|
| 86 |
|
|---|
| 87 | //! Return \b mean and \b sigma of elements of array \b a
|
|---|
| 88 | template <class T>
|
|---|
| 89 | inline double MeanSigma(const TArray<T>& a, double & mean, double & sig)
|
|---|
| 90 | { MathArray<T> ma; return( ma.MeanSigma(a, mean, sig) ); }
|
|---|
| 91 |
|
|---|
| 92 | //! Return \b mean of elements of array \b a
|
|---|
| 93 | template <class T>
|
|---|
| 94 | inline double Mean(const TArray<T>& a)
|
|---|
| 95 | { MathArray<T> ma; double mean, sig; return( ma.MeanSigma(a, mean, sig) ); }
|
|---|
| 96 |
|
|---|
| 97 | } // Fin du namespace
|
|---|
| 98 |
|
|---|
| 99 | #endif
|
|---|