Bsoft  2.1.0
Bernard's software package
Complex.h
Go to the documentation of this file.
1 
8 #include <cmath>
9 #include <iostream>
10 using namespace std;
11 
12 #ifndef _Complex_
13 #define _Complex_
14 #undef Complex // Complex is defined in X.h
15 /************************************************************************
16 @Object: class Complex
17 @Description:
18  Template class for complex numbers.
19 @Features:
20  The internal variables are two numbers, integer or floating point depending on the type.
21 *************************************************************************/
22 template <typename Type>
23 class Complex {
24  Type re, im;
25 public:
26  Complex() : re(0),im(0) {} // Constructors
27  Complex(const Complex& c) : re(c.re),im(c.im) {}
28  Complex(const Type r) : re(r),im(0) {}
29  Complex(const Type r, const Type i) : re(r),im(i) {}
30  Complex operator=(const double& d) { // Operators
31  re = d;
32  im = 0;
33  return *this;
34  }
35  Complex operator=(const Complex& c) { // Operators
36  re = c.re;
37  im = c.im;
38  return *this;
39  }
41  return Complex<Type>(-re, -im);
42  }
44  re += c.re;
45  im += c.im;
46  return *this;
47  }
49  return Complex<Type>(re + c.re, im + c.im);
50  }
52  re -= c.re;
53  im -= c.im;
54  return *this;
55  }
57  return Complex<Type>(re - c.re, im - c.im);
58  }
59  Complex operator*=(const double d) {
60  re = Type(re*d);
61  im = Type(im*d);
62  return *this;
63  }
65  Complex<Type> cn(re*c.re - im*c.im, re*c.im + im*c.re);
66  *this = cn;
67  return *this;
68  }
69  Complex operator*(const double d) {
70  return Complex<Type>(re*d, im*d);
71  }
73  return Complex<Type>(re*c.re - im*c.im, re*c.im + im*c.re);
74  }
75  Complex operator/=(const double d) {
76  re = Type(re/d);
77  im = Type(im/d);
78  return *this;
79  }
81  Complex<Type> cc(c);
82  Type d = (double)c.re*c.re + (double)c.im*c.im;
83  cc.im = -cc.im;
84  *this = (*this * cc)/d;
85  return *this;
86  }
87  Complex operator/(const double d) {
88  return Complex<Type>(re/d, im/d);
89  }
91  Complex<Type> cc(c);
92  double d = (double)c.re*c.re + (double)c.im*c.im;
93  cc.im = -cc.im;
94  return (*this * cc)/d;
95  }
96  Type& operator[](const int i) { return (i==0)? re: im; }
97  template <typename T2> operator Complex<T2>() const {
98  return Complex<T2>(re, im);
99  }
100  bool is_finite() { return isfinite(re) & isfinite(im); }
101  Type real() { return re; }
102  Type imag() { return im; }
103  double power() { return (double)re*re + (double)im*im; }
104  double amp() { return sqrt((double)re*re + (double)im*im); }
105  double phi() { return atan2((double)im, (double)re); }
106  Complex conj() { return Complex<Type>(re, -im); }
107  void set(const double a, const double p) { re = a*cos(p); im = a*sin(p); }
108  void real(const Type d) { re = d; }
109  void imag(const Type d) { im = d; }
110  void amp(const double d) { Type r = d/amp(); re *= r; im *= r; }
111  void phi(const double d) { Type a = amp(); re = a*cos(d); im = a*sin(d); }
112  void shift_phi(const double d) { Complex c((Type)cos(d), (Type)sin(d)); *this *= c; }
114  return Complex<Type>(0.5L*(re + c.re), 0.5L*(im - c.im));
115  }
117  return Complex<Type>(0.5L*(c.im + im), 0.5L*(c.re - re));
118  }
119 };
120 
121 template <typename Type>
122 ostream& operator<<(ostream& output, Complex<Type>& c) {
123  output.setf(ios::fixed, ios::floatfield);
124  output.precision(4);
125  output << "{" << c.real() << "," << c.imag() << "}";
126  return output;
127 }
128 
129 template <typename Type>
130 inline Complex<Type> operator*(const double d, Complex<Type>& c)
131 {
132  return c * d;
133 }
134 
135 template <typename Type>
137 {
138  return (c1 + c2.conj()) * 0.5;
139 }
140 
141 template <typename Type>
143 {
144  return Complex<Type>(0.5*(c2.imag() + c1.imag()), 0.5*(c2.real() - c1.real()));
145 }
146 
147 #endif
148 
Complex::operator*
Complex operator*(const Complex &c)
Definition: Complex.h:72
Complex::operator-
Complex operator-()
Definition: Complex.h:40
Complex::operator/=
Complex operator/=(const double d)
Definition: Complex.h:75
Complex::operator+=
Complex operator+=(const Complex &c)
Definition: Complex.h:43
Complex::operator*=
Complex operator*=(const double d)
Definition: Complex.h:59
Complex::operator+
Complex operator+(const Complex &c)
Definition: Complex.h:48
Complex
Definition: Complex.h:23
Complex::real
Type real()
Definition: Complex.h:101
Complex::Complex
Complex(const Type r)
Definition: Complex.h:28
Complex::real
void real(const Type d)
Definition: Complex.h:108
Complex::operator/=
Complex operator/=(const Complex &c)
Definition: Complex.h:80
Complex::operator*=
Complex operator*=(const Complex &c)
Definition: Complex.h:64
Complex::set
void set(const double a, const double p)
Definition: Complex.h:107
Complex::shift_phi
void shift_phi(const double d)
Definition: Complex.h:112
Complex::operator/
Complex operator/(const double d)
Definition: Complex.h:87
Complex::operator-
Complex operator-(const Complex &c)
Definition: Complex.h:56
operator<<
ostream & operator<<(ostream &output, Complex< Type > &c)
Definition: Complex.h:122
Complex::operator*
Complex operator*(const double d)
Definition: Complex.h:69
Complex::imag
void imag(const Type d)
Definition: Complex.h:109
complex_unpack_combined_first
Complex< Type > complex_unpack_combined_first(Complex< Type > c1, Complex< Type > c2)
Definition: Complex.h:136
Complex::amp
void amp(const double d)
Definition: Complex.h:110
Complex::imag
Type imag()
Definition: Complex.h:102
Complex::phi
double phi()
Definition: Complex.h:105
Complex::power
double power()
Definition: Complex.h:103
Complex::operator[]
Type & operator[](const int i)
Definition: Complex.h:96
Complex::operator/
Complex operator/(const Complex &c)
Definition: Complex.h:90
Complex::unpack_first
Complex unpack_first(Complex c)
Definition: Complex.h:113
isfinite
bool isfinite(complex< T > &value)
Complex::operator=
Complex operator=(const Complex &c)
Definition: Complex.h:35
Complex::conj
Complex conj()
Definition: Complex.h:106
Complex::unpack_second
Complex unpack_second(Complex c)
Definition: Complex.h:116
Complex::is_finite
bool is_finite()
Definition: Complex.h:100
complex_unpack_combined_second
Complex< Type > complex_unpack_combined_second(Complex< Type > c1, Complex< Type > c2)
Definition: Complex.h:142
Complex::operator=
Complex operator=(const double &d)
Definition: Complex.h:30
Complex::amp
double amp()
Definition: Complex.h:104
Complex::Complex
Complex(const Type r, const Type i)
Definition: Complex.h:29
Complex::operator-=
Complex operator-=(const Complex &c)
Definition: Complex.h:51
operator*
Complex< Type > operator*(const double d, Complex< Type > &c)
Definition: Complex.h:130
Complex::Complex
Complex(const Complex &c)
Definition: Complex.h:27
Complex::phi
void phi(const double d)
Definition: Complex.h:111
Complex::Complex
Complex()
Definition: Complex.h:26