C++ main module for emicrom Package  1.0
MATSGN_Complex.h
Go to the documentation of this file.
1 #ifndef MATSGN_Complex_H
2 #define MATSGN_Complex_H
3 
4 #include "CORE_Object.h"
5 #include "CORE_Exception.h"
6 #include "CORE_Real.h"
7 #include "fftw3.h"
8 
16 class MATSGN_Complex : public virtual CORE_Object {
18  // ATTRIBUTES
19 
20 
21 private:
22  fftw_complex mValue;
23  mutable tDouble mWork;
24  // ASSOCIATIONS
25 
26 
27 public:
28  // METHODS
29 
30  // CONSTRUCTORS
31 
33  MATSGN_Complex(void);
35  MATSGN_Complex(const tDouble& a,const tDouble& b);
37  MATSGN_Complex(const fftw_complex& f);
38 
41 
42 
43 
44 
45  // DESTRUCTORS
46 
47 
50  virtual ~MATSGN_Complex(void);
51 
52 public:
53  // operators
54  // =========
59  inline tDouble& operator[](const tUSInt& i) {
60  ASSERT_IN(i<2);
61  return mValue[i];
62  };
67  inline const tDouble& operator[](const tUSInt& i) const {
68  ASSERT_IN(i<2);
69  return mValue[i];
70  };
71 
77  mValue[0]=s[0];
78  mValue[1]=s[1];
79  return (*this);
80  };
85  inline MATSGN_Complex& operator=(const fftw_complex& s) {
86  mValue[0]=s[0];
87  mValue[1]=s[1];
88  return (*this);
89  };
90 
95  inline MATSGN_Complex& operator=(const tReal& s) {
96  mValue[0]=s;
97  mValue[1]=0;
98  return (*this);
99  };
100 
105  inline MATSGN_Complex& operator*=(const tReal& s) {
106  mValue[0]*=s;
107  mValue[1]*=s;
108  return (*this);
109  };
115  mWork=mValue[0]*s[0]-mValue[1]*s[1];
116  mValue[1]=mValue[0]*s[1]+mValue[1]*s[0];
117  mValue[0]=mWork;
118  return (*this);
119  };
124  inline MATSGN_Complex& operator*=(const fftw_complex& s) {
125  mWork=mValue[0]*s[0]-mValue[1]*s[1];
126  mValue[1]=mValue[0]*s[1]+mValue[1]*s[0];
127  mValue[0]=mWork;
128  return (*this);
129  };
130 
135  inline MATSGN_Complex& operator/=(const tReal& s) {
136  if (s==0) throw CORE_Exception("math/signam","MATSGN_Complex/=","division by 0");
137  mValue[0]/=s;
138  mValue[1]/=s;
139  return (*this);
140  };
146  mWork=mValue[0]*s[0]+mValue[1]*s[1];
147  mValue[1]=(-mValue[0]*s[1]+mValue[1]*s[0]);
148  mValue[0]=mWork;
149  mWork=s.module2();
150  if (mWork==0) throw CORE_Exception("math/signam","MATSGN_Complex/=","division by 0");
151  mValue[0]/=mWork;
152  mValue[1]/=mWork;
153  return (*this);
154  };
160  mValue[0]+=s[0];
161  mValue[1]+=s[1];
162  return (*this);
163  };
168  inline MATSGN_Complex& operator+=(const fftw_complex& s) {
169  mValue[0]+=s[0];
170  mValue[1]+=s[1];
171  return (*this);
172  };
178  mValue[0]-=s[0];
179  mValue[1]-=s[1];
180  return (*this);
181  };
186  inline MATSGN_Complex& operator-=(const fftw_complex& s) {
187  mValue[0]-=s[0];
188  mValue[1]-=s[1];
189  return (*this);
190  };
191  // SET methods
192 
193 
194 
195 
200  inline void setValue(const tDouble& x,const tDouble& y) {
201  mValue[0]=x;
202  mValue[1]=y;
203  }
204 
205  // GET methods
206 
210  inline const fftw_complex& getValue() const {
211  return mValue;
212  }
216  inline fftw_complex& getValue() {
217  return mValue;
218  }
219 
222  inline void conjugate() {
223  mValue[1]*=-1;
224  }
228  inline tDouble module2() const {
229  return module2(mValue);
230  }
234  inline tDouble module() const {
235  return module(mValue);
236  }
241  inline static tDouble module2(const fftw_complex& f) {
242  return f[0]*f[0]+f[1]*f[1];
243  }
248  inline static tDouble module(const fftw_complex& f) {
249  return sqrt(module2(f));
250  }
251 
252  /* \brief complex product a*=b;
253  * @param a: the a value
254  * @param b: the b value
255  */
256  inline static void product(fftw_complex& a,const fftw_complex& b) {
257  tDouble temp;
258  product(a,b,temp);
259  }
260  /* \brief complex product a*=b;
261  * @param a: the a value
262  * @param b: the b value
263  */
264  inline static void product(fftw_complex& a,const fftw_complex& b,tDouble& temp) {
265  temp=a[0]*b[0]-a[1]*b[1];
266  a[1]=a[0]*b[1]+a[1]*b[0];
267  a[0]=temp;
268  }
269 
270  /* \brief complex product r=a*b;
271  * @param a: the a value
272  * @param b: the b value
273  * @param r: the result value
274  * @param temp: work value
275  */
276  inline static void product(const fftw_complex& a,const fftw_complex& b,fftw_complex& r,tDouble& temp) {
277  temp=a[0]*b[0]-a[1]*b[1];
278  r[1]=a[0]*b[1]+a[1]*b[0];
279  r[0]=temp;
280  }
281  /* \brief complex product r=a*b
282  * @param a: the a value
283  * @param b: the b value
284  * @param r: the result value
285  */
286  inline static void product(const fftw_complex& a,const fftw_complex& b,fftw_complex& r) {
287  ASSERT_IN(&b!=&r);
288  ASSERT_IN(&a!=&r);
289  r[0]=a[0]*b[0]-a[1]*b[1];
290  r[1]=a[0]*b[1]+a[1]*b[0];
291  }
292  /* \brief complex product r=a*b;
293  * @param a: the a value
294  * @param b: the b value
295  * @param r: the result value
296  */
297  inline static void product(const MATSGN_Complex& a,const MATSGN_Complex& b,MATSGN_Complex& r) {
298  const tDouble &ra=a[0];
299  const tDouble &ia=a[1];
300  const tDouble &rb=b[0];
301  const tDouble &ib=b[1];
302  tDouble tmp=ra*rb-ia*ib;
303  r[1]=ia*rb+ra*ib;
304  r[0]=tmp;
305  }
306  /* \brief complex product r=(a+ib)*(x+i*y)
307  * @param a: real part
308  * @param b: imaginary part
309  * @param x: real part
310  * @param y: imaginary part
311  * @param r: result
312  */
313  inline static void product(const tDouble& a,const tDouble& b,const tDouble& x,const tDouble& y,fftw_complex& r) {
314  r[0]=a*x-b*y;
315  r[1]=a*y+b*x;
316  }
317  /* \brief complex add a+=b
318  * @param a: the a value
319  * @param b: the b value
320  */
321  inline static void add(fftw_complex& a,const fftw_complex& b) {
322  a[0]+=b[0];
323  a[1]+=b[1];
324  }
325  /* \brief complex add r=a+b;
326  * @param a: the a value
327  * @param b: the b value
328  * @param r: the result value
329  */
330  inline static void add(const fftw_complex& a,const fftw_complex& b,fftw_complex& r) {
331  r[0]=a[0]+b[0];
332  r[1]=a[0]+b[1];
333  }
334  /* \brief complex sub a-=b;
335  * @param a: the a value
336  * @param b: the b value
337  */
338  inline static void sub(fftw_complex& a,const fftw_complex& b) {
339  a[0]-=b[0];
340  a[1]-=b[1];
341  }
342  /* \brief complex sub : r=a-b;
343  * @param a: the a value
344  * @param b : the b value
345  * @param r: the result value
346  */
347  inline static void sub(const fftw_complex& a,const fftw_complex& b,fftw_complex& r) {
348  r[0]=a[0]-b[0];
349  r[1]=a[1]-b[1];
350  }
351  /* \brief swap a & b
352  * @param a: the value to swap
353  * @param b: the value to swap
354  */
355  inline static void swap(fftw_complex& a,fftw_complex& b) {
356  std::swap(a[0],b[0]);
357  std::swap(a[1],b[1]);
358  }
359 
363  inline static tDouble norm2(const fftw_complex& a,const fftw_complex& b) {
364 
365  return (a[0]-b[0])*(a[0]-b[0])+(a[1]-b[1])*(a[1]-b[1]);
366  }
369  inline static tDouble norm(const fftw_complex& a,const fftw_complex& b) {
370 
371  return sqrt(norm2(a,b));
372  }
373 
374 
375 
378  virtual tString toString() const {
379  return toString(mValue);
380  }
383  inline static tString toString(const fftw_complex& f) {
384  tString ret="";
385  ret+=CORE_Real::toString(f[0]);
386  if (f[1]>0) {
387  ret+="+"+CORE_Real::toString(f[1])+"i";
388  } else if (f[1]<0) {
389  ret+=CORE_Real::toString(f[1])+"i";
390  }
391  return ret;
392  }
395  inline static tString toString(const fftw_complex& f,const tUSInt& nDigits) {
396  tString ret="";
397  ret+=CORE_Real::toString(f[0],nDigits);
398  if (f[1]>0) {
399  ret+="+"+CORE_Real::toString(f[1],nDigits)+"i";
400  } else if (f[1]<0) {
401  ret+=CORE_Real::toString(f[1],nDigits)+"i";
402  }
403  return ret;
404  }
405 
406 
407 
408 
409 
410 
411 };
412 
413 #endif
static tString toString(const fftw_complex &f)
to string
Definition: MATSGN_Complex.h:383
static void add(fftw_complex &a, const fftw_complex &b)
Definition: MATSGN_Complex.h:321
static void product(const tDouble &a, const tDouble &b, const tDouble &x, const tDouble &y, fftw_complex &r)
Definition: MATSGN_Complex.h:313
MATSGN_Complex & operator-=(const MATSGN_Complex &s)
sub operator
Definition: MATSGN_Complex.h:177
#define tDouble
Definition: types.h:52
tDouble & operator[](const tUSInt &i)
get the value for reading & writing
Definition: MATSGN_Complex.h:59
MATSGN_Complex & operator+=(const fftw_complex &s)
add operator
Definition: MATSGN_Complex.h:168
static tDouble module(const fftw_complex &f)
compute the module of f
Definition: MATSGN_Complex.h:248
static tDouble module2(const fftw_complex &f)
compute the square of module of f
Definition: MATSGN_Complex.h:241
void conjugate()
conjugate the complex
Definition: MATSGN_Complex.h:222
static void add(const fftw_complex &a, const fftw_complex &b, fftw_complex &r)
Definition: MATSGN_Complex.h:330
static void product(const fftw_complex &a, const fftw_complex &b, fftw_complex &r, tDouble &temp)
Definition: MATSGN_Complex.h:276
static void swap(fftw_complex &a, fftw_complex &b)
Definition: MATSGN_Complex.h:355
MATSGN_Complex & operator*=(const fftw_complex &s)
multiply operator
Definition: MATSGN_Complex.h:124
#define tUSInt
Definition: types.h:28
MATSGN_Complex(void)
create a FFT complex
Definition: MATSGN_Complex.cpp:4
fftw_complex & getValue()
get values for reading or writing
Definition: MATSGN_Complex.h:216
void setValue(const tDouble &x, const tDouble &y)
set the value at index
Definition: MATSGN_Complex.h:200
MATSGN_Complex & operator/=(const MATSGN_Complex &s)
divide operator
Definition: MATSGN_Complex.h:145
MATSGN_Complex & operator-=(const fftw_complex &s)
sub operator
Definition: MATSGN_Complex.h:186
DEFINE_SPTR(MATSGN_Complex)
MATSGN_Complex & operator/=(const tReal &s)
divide operator
Definition: MATSGN_Complex.h:135
const fftw_complex & getValue() const
get values for reading only
Definition: MATSGN_Complex.h:210
tDouble module2() const
compute the square of module of this
Definition: MATSGN_Complex.h:228
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:15
tDouble mWork
Definition: MATSGN_Complex.h:23
virtual tString toString() const
Definition: MATSGN_Complex.h:378
MATSGN_Complex & operator*=(const MATSGN_Complex &s)
multiply operator
Definition: MATSGN_Complex.h:114
const tDouble & operator[](const tUSInt &i) const
get the value for reading only
Definition: MATSGN_Complex.h:67
virtual ~MATSGN_Complex(void)
destroy an FFT complex array
Definition: MATSGN_Complex.cpp:26
static void product(fftw_complex &a, const fftw_complex &b)
Definition: MATSGN_Complex.h:256
abstract base class for most classes.
Definition: CORE_Object.h:53
static void product(const MATSGN_Complex &a, const MATSGN_Complex &b, MATSGN_Complex &r)
Definition: MATSGN_Complex.h:297
#define tString
Definition: types.h:135
static tDouble norm(const fftw_complex &a, const fftw_complex &b)
return the norm of the difference
Definition: MATSGN_Complex.h:369
static tString toString(const fftw_complex &f, const tUSInt &nDigits)
to string
Definition: MATSGN_Complex.h:395
MATSGN_Complex & operator*=(const tReal &s)
multiply operator
Definition: MATSGN_Complex.h:105
MATSGN_Complex & operator=(const tReal &s)
copy the real
Definition: MATSGN_Complex.h:95
MATSGN_Complex & operator+=(const MATSGN_Complex &s)
add operator
Definition: MATSGN_Complex.h:159
MATSGN_Complex & operator=(const MATSGN_Complex &s)
copy the complex
Definition: MATSGN_Complex.h:76
tString toString() const
return the string associated to the real
Definition: CORE_Real.h:97
This class describes complex based on fftw_complex sructure.
Definition: MATSGN_Complex.h:16
static void sub(const fftw_complex &a, const fftw_complex &b, fftw_complex &r)
Definition: MATSGN_Complex.h:347
static tDouble norm2(const fftw_complex &a, const fftw_complex &b)
return the norm of the difference
Definition: MATSGN_Complex.h:363
fftw_complex mValue
Definition: MATSGN_Complex.h:22
tDouble module() const
compute the module of this
Definition: MATSGN_Complex.h:234
#define tReal
Definition: types.h:118
SP_OBJECT(MATSGN_Complex)
static void product(fftw_complex &a, const fftw_complex &b, tDouble &temp)
Definition: MATSGN_Complex.h:264
static void sub(fftw_complex &a, const fftw_complex &b)
Definition: MATSGN_Complex.h:338
MATSGN_Complex & operator=(const fftw_complex &s)
copy the complex
Definition: MATSGN_Complex.h:85
static void product(const fftw_complex &a, const fftw_complex &b, fftw_complex &r)
Definition: MATSGN_Complex.h:286
#define ASSERT_IN(a)
Definition: types.h:196