| Bsoft 2.0.5 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 }
40 Complex operator-() {
41 return Complex<Type>(-re, -im);
42 }
43 Complex operator+=(const Complex& c) {
44 re += c.re;
45 im += c.im;
46 return *this;
47 }
48 Complex operator+(const Complex& c) {
49 return Complex<Type>(re + c.re, im + c.im);
50 }
51 Complex operator-=(const Complex& c) {
52 re -= c.re;
53 im -= c.im;
54 return *this;
55 }
56 Complex operator-(const Complex& c) {
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 }
64 Complex operator*=(const Complex& c) {
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 }
72 Complex operator*(const Complex& c) {
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 }
80 Complex operator/=(const Complex& c) {
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 }
90 Complex operator/(const Complex& c) {
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; }
113 Complex unpack_first(Complex c) {
114 return Complex<Type>(0.5L*(re + c.re), 0.5L*(im - c.im));
115 }
116 Complex unpack_second(Complex c) {
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>
136 inline Complex<Type> complex_unpack_combined_first(Complex<Type> c1, Complex<Type> c2)
137 {
138 return (c1 + c2.conj()) * 0.5;
139 }
140
141 template <typename Type>
142 inline Complex<Type> complex_unpack_combined_second(Complex<Type> c1, Complex<Type> c2)
143 {
144 return Complex<Type>(0.5*(c2.imag() + c1.imag()), 0.5*(c2.real() - c1.real()));
145 }
146
147 #endif
148
Complex::amp
void amp(const double d)
Definition: Complex.h:110
Complex::unpack_second
Complex unpack_second(Complex c)
Definition: Complex.h:116
Complex::operator/=
Complex operator/=(const double d)
Definition: Complex.h:75
Complex::operator-
Complex operator-(const Complex &c)
Definition: Complex.h:56
Complex::phi
void phi(const double d)
Definition: Complex.h:111
Complex::Complex
Complex()
Definition: Complex.h:26
operator *=
void operator *=(vector< T1 > &v1, T2 v2)
Definition: Vector3.h:456
Complex::conj
Complex conj()
Definition: Complex.h:106
Complex::operator=
Complex operator=(const double &d)
Definition: Complex.h:30
Complex::Complex
Complex(const Complex &c)
Definition: Complex.h:27
complex_unpack_combined_first
Complex< Type > complex_unpack_combined_first(Complex< Type > c1, Complex< Type > c2)
Definition: Complex.h:136
Complex::shift_phi
void shift_phi(const double d)
Definition: Complex.h:112
complex_unpack_combined_second
Complex< Type > complex_unpack_combined_second(Complex< Type > c1, Complex< Type > c2)
Definition: Complex.h:142
isfinite
bool isfinite(complex< T > &value)
operator *
Complex< Type > operator *(const double d, Complex< Type > &c)
Definition: Complex.h:130
Complex::operator+
Complex operator+(const Complex &c)
Definition: Complex.h:48
Complex::amp
double amp()
Definition: Complex.h:104
Complex::imag
void imag(const Type d)
Definition: Complex.h:109
Complex::operator=
Complex operator=(const Complex &c)
Definition: Complex.h:35
Complex::is_finite
bool is_finite()
Definition: Complex.h:100
Complex
Definition: Complex.h:23
Complex::operator-
Complex operator-()
Definition: Complex.h:40
Complex::Complex
Complex(const Type r)
Definition: Complex.h:28
Complex::phi
double phi()
Definition: Complex.h:105
Complex::imag
Type imag()
Definition: Complex.h:102
operator<<
ostream & operator<<(ostream &output, Complex< Type > &c)
Definition: Complex.h:122
Complex::set
void set(const double a, const double p)
Definition: Complex.h:107
Complex::real
void real(const Type d)
Definition: Complex.h:108
Complex::unpack_first
Complex unpack_first(Complex c)
Definition: Complex.h:113
Complex::power
double power()
Definition: Complex.h:103
Complex::real
Type real()
Definition: Complex.h:101
Complex::operator/=
Complex operator/=(const Complex &c)
Definition: Complex.h:80
Complex::operator/
Complex operator/(const Complex &c)
Definition: Complex.h:90
Complex::operator[]
Type & operator[](const int i)
Definition: Complex.h:96
Complex::Complex
Complex(const Type r, const Type i)
Definition: Complex.h:29
Complex::operator+=
Complex operator+=(const Complex &c)
Definition: Complex.h:43
Complex::operator/
Complex operator/(const double d)
Definition: Complex.h:87
Complex::operator-=
Complex operator-=(const Complex &c)
Definition: Complex.h:51