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