Bridge++  Version 1.5.4
 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 
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 using std::abs;
33 
34 typedef std::complex<int> icomplex;
35 typedef std::complex<float> fcomplex;
36 typedef std::complex<double> dcomplex;
37 // typedef std::complex<long double> lcomplex;
38 
39 inline icomplex cmplx(const int r, const int i) { return icomplex(r, i); }
40 inline fcomplex cmplx(const float r, const float i) { return fcomplex(r, i); }
41 inline dcomplex cmplx(const double r, const double i) { return dcomplex(r, i); }
42 // inline lcomplex cmplx (const long double r, const long double i) {
43 // return lcomplex(r, i);
44 // }
45 
46 #ifdef LIB_CPP11
47 // do nothing
48 #else
49 inline int real(const icomplex& z) { return z.real(); }
50 inline int imag(const icomplex& z) { return z.imag(); }
51 
52 inline float real(const fcomplex& z) { return z.real(); }
53 inline float imag(const fcomplex& z) { return z.imag(); }
54 
55 inline double real(const dcomplex& z) { return z.real(); }
56 inline double imag(const dcomplex& z) { return z.imag(); }
57 
58 // inline long double real (const lcomplex& z) { return z.real(); }
59 // inline long double imag (const lcomplex& z) { return z.imag(); }
60 #endif
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 #endif
148 #endif // BRIDGE_COMPLEX_INCLUDED
Mat_SU_N operator*(const Mat_SU_N &m1, const Mat_SU_N &m2)
Definition: mat_SU_N.h:550