Chombo + EB + MF  3.2
CH_Complex.H
Go to the documentation of this file.
1 #ifdef CH_LANG_CC
2 /*
3  * _______ __
4  * / ___/ / ___ __ _ / / ___
5  * / /__/ _ \/ _ \/ V \/ _ \/ _ \
6  * \___/_//_/\___/_/_/_/_.__/\___/
7  * Please refer to Copyright.txt, in Chombo's root directory.
8  */
9 #endif
10 
11 #ifndef _CH_COMPLEX_H_
12 #define _CH_COMPLEX_H_
13 
14 #ifdef CH_USE_COMPLEX
15 
16 #include <complex>
17 using std::complex;
18 
19 #include "REAL.H"
20 #include "BaseNamespaceHeader.H"
21 using std::complex;
22 
23 /// Complex numbers suitable for use in FABs
24 /** Chombo has its own complex number class so it can:
25  * 1) provide the same class name regardless of the current precision
26  * (just like the Chombo "Real" type)
27  * 2) provide write access to the real and imaginary parts of the
28  * complex number without going through a constructor (this is not
29  * allowed by the C++ 1999 standard).
30  */
31 
32 class Complex : public complex<Real>
33 {
34 public:
35 
36  inline Complex()
37  {
38  }
39 
40  inline Complex(const complex<Real>& a_arg):complex<Real>(a_arg)
41  {
42  }
43 
44  ///
45  inline Complex(const Real& re, const Real& im):complex<Real>(re, im)
46  {
47  }
48 
49  /// return a non-const reference to the real part of the complex number
50  inline Real & re();
51 
52  /// return a copy of the real part of the complex number
53  inline Real re() const
54  {
55  return real();
56  };
57 
58  /// return a non-const reference to the imaginary part of the complex number
59  inline Real & im();
60 
61  /// return a copy of the imaginary part of the complex number
62  inline Real im() const
63  {
64  return imag();
65  };
66 
67  inline Complex& operator=(const Complex& rhs)
68  {
69  reinterpret_cast<Real(&)[2]>(*this)[0] = reinterpret_cast<const Real(&)[2]>(rhs)[0];
70  reinterpret_cast<Real(&)[2]>(*this)[1] = reinterpret_cast<const Real(&)[2]>(rhs)[1];
71  //complex<Real>::operator=(rhs);
72  return *this;
73  }
74 
75  inline Complex& operator*=(Real s)
76  {
77  reinterpret_cast<Real(&)[2]>(*this)[0] *=s;
78  reinterpret_cast<Real(&)[2]>(*this)[1] *=s;
79  return *this;
80  }
81 
82  inline Complex& operator*=(const Complex& in)
83  {
84  Real a = reinterpret_cast<const Real(&)[2]>(in)[0];
85  Real b = reinterpret_cast<const Real(&)[2]>(in)[1];
86  Real c = reinterpret_cast<const Real(&)[2]>(*this)[0];
87  Real d = reinterpret_cast<const Real(&)[2]>(*this)[1];
88  reinterpret_cast<Real(&)[2]>(*this)[0] = a*c - b*d;
89  reinterpret_cast<Real(&)[2]>(*this)[1] = a*b + b*c;
90  return *this;
91  }
92 
93  inline Complex operator*(const Complex& in) const
94  {
95  Complex rtn;
96  Real a = reinterpret_cast<const Real(&)[2]>(in)[0];
97  Real b = reinterpret_cast<const Real(&)[2]>(in)[1];
98  Real c = reinterpret_cast<const Real(&)[2]>(*this)[0];
99  Real d = reinterpret_cast<const Real(&)[2]>(*this)[1];
100  reinterpret_cast<Real(&)[2]>(rtn)[0] = a*c - b*d;
101  reinterpret_cast<Real(&)[2]>(rtn)[1] = a*b + b*c;
102  return rtn;
103  }
104  static inline Complex Zero() {return Complex(0,0);}
105 };
106 
107 //XXX dont check the compiler -- just assume this will work
108 //XXX#if defined(__GNUC__) || defined(__INTEL_COMPILER__) || defined(__IBMCPP__) || defined(__HP_aCC) || defined(__DECCXX)
109 
110 // The compilers we've seen implement complex as a 2-array of
111 // numbers of the appropriate type, with the real part first.
112 // Run the test program "Chombo/lib/test/BoxTools/test_complex.cpp" to check.
113 // This is the most likely implementation, and the only real problems are
114 // whether the compiler puts the data at the beginning of the class's
115 // memory, and whether it will allow the cast.
116 inline Real & Complex::re()
117 {
118  return (((Real*)this)[0]);
119 }
120 
121 inline Real & Complex::im()
122 {
123  return (((Real*)this)[1]);
124 }
125 
126 //XXX#else
127 //XXX#error Complex type is not implemented for this compiler; contact the Chombo developers.
128 //XXX#endif
129 
130 #include "BaseNamespaceFooter.H"
131 #endif //CH_USE_COMPLEX
132 #endif //_CH_COMPLEX_H_
double Real
Definition: REAL.H:33
C::self_type operator*(const C &, const C &)
Definition: GenericArithmeticI.H:128