Bridge++  Ver. 2.0.2
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 #elif defined USE_C99_COMPLEX
47 // C99 complex
48 #include <complex.h>
49 
50 class icomplex {
51  public:
52  icomplex() : m_r(0), m_i(0) {}
53  icomplex(const int r, const int i = 0) : m_r(r), m_i(i) {}
54  icomplex(const icomplex& rhs) : m_r(rhs.m_r), m_i(rhs.m_i) {}
55  ~icomplex() {}
56 
57  icomplex& operator=(const icomplex& rhs)
58  {
59  m_r = rhs.m_r;
60  m_i = rhs.m_i;
61  return *this;
62  }
63 
64  icomplex& operator*=(const icomplex& rhs)
65  {
66  double re = m_r;
67  double im = m_i;
68 
69  m_r = re * rhs.m_r - im * rhs.m_i;
70  m_i = re * rhs.m_i + im * rhs.m_r;
71  return *this;
72  }
73 
74  friend icomplex operator*(const icomplex& lhs, const icomplex& rhs)
75  {
76  return icomplex(lhs.m_r * rhs.m_r - lhs.m_i * rhs.m_i,
77  lhs.m_r * rhs.m_i + lhs.m_i * rhs.m_r);
78  }
79 
80  friend int real(const icomplex& z) { return z.m_r; }
81  friend int imag(const icomplex& z) { return z.m_i; }
82 
83  private:
84  int m_r;
85  int m_i;
86 };
87 
88 inline icomplex cmplx(const int real, const int imag)
89 {
90  return icomplex(real, imag);
91 }
92 
93 
94 typedef float _Complex fcomplex;
95 typedef double _Complex dcomplex;
96 //typedef long double _Complex lcomplex;
97 
98 inline float real(const fcomplex& z) { return crealf(z); }
99 inline float imag(const fcomplex& z) { return cimagf(z); }
100 inline float abs(const fcomplex& z) { return cabsf(z); }
101 inline fcomplex cmplx(const float real, const float imag)
102 {
103  return real + imag * _Complex_I;
104 }
105 
106 
107 inline double real(const dcomplex& z) { return creal(z); }
108 inline double imag(const dcomplex& z) { return cimag(z); }
109 inline double abs(const dcomplex& z) { return cabs(z); }
110 inline dcomplex cmplx(const double real, const double imag)
111 {
112  return real + imag * _Complex_I;
113 }
114 
115 
116 inline dcomplex sqrt(const dcomplex& z) { return csqrt(z); }
117 
118 
119 /*
120 inline long double real(const lcomplex& z) { return creall(z); }
121 inline long double imag(const lcomplex& z) { return cimagl(z); }
122 inline long double abs(const lcomplex& z) { return cabsl(z); }
123 inline lcomplex cmplx(const long double real, const long double imag) {
124  return real + imag * _Complex_I;
125 }
126 */
127 
128 /*
129 dcomplex operator-(const dcomplex& rhs)
130  {
131  return cmplx(-real(rhs),-imag(rhs));
132  }
133 */
134 #endif
135 #endif // BRIDGE_COMPLEX_INCLUDED
SU_N::operator*
Mat_SU_N operator*(const Mat_SU_N &m1, const Mat_SU_N &m2)
Definition: mat_SU_N.h:610