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