Bridge++  Ver. 1.2.x
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bridge_complex.h
Go to the documentation of this file.
1 
14 #ifndef BRIDGE_COMPLEX_INCLUDED
15 #define BRIDGE_COMPLEX_INCLUDED
16 // chose the complex type
17 // std::complex (c++) -> USE_STD_COMPLEX
18 // _Complex (C99) -> USE_C99_COMPLEX
19 // When you make original complex class, you have to implement such functions:
20 // cmplx(T real, T imag), return your complex type, real + i imag,
21 // real(complex& z), imag(complex& z), return real(imaginary) part of the complex number z.
22 // abs(complex& z), return absolute value |z|,
23 // norm(complex& z), return squared norm |z|^2,
24 // arg(complex& z), return phase angle of the complex number z,
25 // conj(complex& z), return complex conjugate of the complex number z.
26 
27 #if defined USE_STD_COMPLEX
28 // std::complex
29 #include <complex>
30 
31 typedef std::complex<int> icomplex;
32 typedef std::complex<float> fcomplex;
33 typedef std::complex<double> dcomplex;
34 //typedef std::complex<long double> lcomplex;
35 
36 #ifndef LIB_CPP11
37 int real(const icomplex& z) { return z.real(); }
38 int imag(const icomplex& z) { return z.imag(); }
39 #endif
40 inline icomplex cmplx(const int r, const int i) { return icomplex(r, i); }
41 
42 #ifndef LIB_CPP11
43 float real(const fcomplex& z) { return z.real(); }
44 float imag(const fcomplex& z) { return z.imag(); }
45 #endif
46 inline fcomplex cmplx(const float r, const float i) { return fcomplex(r, i); }
47 
48 #ifndef LIB_CPP11
49 double real(const dcomplex& z) { return z.real(); }
50 double imag(const dcomplex& z) { return z.imag(); }
51 #endif
52 inline dcomplex cmplx(const double r, const double i) { return dcomplex(r, i); }
53 
54 /*
55 long double real (const lcomplex& z) { return z.real(); }
56 long double imag (const lcomplex& z) { return z.imag(); }
57 inline lcomplex cmplx (const long double r, const long double i) {
58  return lcomplex(r, i);
59 }
60 */
61 
62 #elif defined USE_C99_COMPLEX
63 // C99 complex
64 #include <complex.h>
65 
66 class icomplex {
67  public:
68  icomplex() : m_r(0), m_i(0) {}
69  icomplex(const int r, const int i = 0) : m_r(r), m_i(i) {}
70  icomplex(const icomplex& rhs) : m_r(rhs.m_r), m_i(rhs.m_i) {}
71  ~icomplex() {}
72 
73  icomplex& operator=(const icomplex& rhs)
74  {
75  m_r = rhs.m_r;
76  m_i = rhs.m_i;
77  return *this;
78  }
79 
80  icomplex& operator*=(const icomplex& rhs)
81  {
82  double re = m_r;
83  double im = m_i;
84 
85  m_r = re * rhs.m_r - im * rhs.m_i;
86  m_i = re * rhs.m_i + im * rhs.m_r;
87  return *this;
88  }
89 
90  friend icomplex operator*(const icomplex& lhs, const icomplex& rhs)
91  {
92  return icomplex(lhs.m_r * rhs.m_r - lhs.m_i * rhs.m_i,
93  lhs.m_r * rhs.m_i + lhs.m_i * rhs.m_r);
94  }
95 
96  friend int real(const icomplex& z) { return z.m_r; }
97  friend int imag(const icomplex& z) { return z.m_i; }
98 
99  private:
100  int m_r;
101  int m_i;
102 };
103 
104 inline icomplex cmplx(const int real, const int imag)
105 {
106  return icomplex(real, imag);
107 }
108 
109 
110 typedef float _Complex fcomplex;
111 typedef double _Complex dcomplex;
112 //typedef long double _Complex lcomplex;
113 
114 inline float real(const fcomplex& z) { return crealf(z); }
115 inline float imag(const fcomplex& z) { return cimagf(z); }
116 inline float abs(const fcomplex& z) { return cabsf(z); }
117 inline fcomplex cmplx(const float real, const float imag)
118 {
119  return real + imag * _Complex_I;
120 }
121 
122 
123 inline double real(const dcomplex& z) { return creal(z); }
124 inline double imag(const dcomplex& z) { return cimag(z); }
125 inline double abs(const dcomplex& z) { return cabs(z); }
126 inline dcomplex cmplx(const double real, const double imag)
127 {
128  return real + imag * _Complex_I;
129 }
130 
131 
132 /*
133 inline long double real(const lcomplex& z) { return creall(z); }
134 inline long double imag(const lcomplex& z) { return cimagl(z); }
135 inline long double abs(const lcomplex& z) { return cabsl(z); }
136 inline lcomplex cmplx(const long double real, const long double imag) {
137  return real + imag * _Complex_I;
138 }
139 */
140 
141 /*
142 dcomplex operator-(const dcomplex& rhs)
143  {
144  return cmplx(-real(rhs),-imag(rhs));
145  }
146 */
147 
148 #endif
149 #endif // BRIDGE_COMPLEX_INCLUDED
Field operator*(const Field &v, const double s)
Definition: field.h:271