source: Sophya/trunk/SophyaLib/TArray/matharr.h@ 3589

Last change on this file since 3589 was 3340, checked in by cmv, 18 years ago

add virtual destructor for g++ >4. cmv 05/10/2007

File size: 6.5 KB
Line 
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
12namespace SOPHYA {
13
14//! Class for simple mathematical operation on arrays
15template <class T>
16class MathArray {
17public:
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 */
37template <class T>
38inline 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 */
43template <class T>
44inline 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 */
49template <class T>
50inline 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 */
55template <class T>
56inline 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 */
61template <class T>
62inline 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 */
67template <class T>
68inline 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 */
73template <class T>
74inline 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 */
79template <class T>
80inline 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 */
85template <class T>
86inline 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 */
91template <class T>
92inline 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 */
97template <class T>
98inline 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 */
104template <class T>
105inline 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 */
110template <class T>
111inline 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
116template <class T>
117class ComplexMathArray {
118public:
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 */
145template <class T>
146inline 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 */
151template <class T>
152inline 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 */
157template <class T>
158inline 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 */
163template <class T>
164inline 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 */
169template <class T>
170inline 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 */
175template <class T>
176inline 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
Note: See TracBrowser for help on using the repository browser.