RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
complex.h
Go to the documentation of this file.
1 //
2 // file = complex.h
3 //
4 
5 #ifndef _COMPLEX_H_
6 #define _COMPLEX_H_
7 
8 #include <math.h>
9 #undef complex
10 #undef cabs
11 
12 class complex
13 {
14 public:
15  complex(double _r, double _i = 0.0);
16  complex(float _r, float _i);
17  complex();
18  void operator+=(const complex);
19  void operator-=(const complex);
20  void operator*=(const complex);
21  void operator*=(double);
22  void operator/=(double);
23  // void operator=(const complex&);
24  // void operator=(const complex_vector&);
25 
26  friend double real(const complex&);
27  friend double imag(const complex&);
28  friend complex conj(const complex);
29  friend complex sqrt(const complex);
30  friend double norm(const complex);
31  friend double cabs(const complex);
32  friend double mag(const complex);
33  friend double arg(const complex);
34  friend complex cexp(const complex);
35  friend double mag_sqrd(const complex);
36  friend complex operator-(const complex);
37  friend complex operator+(const complex, const complex);
38  friend complex operator-(const complex, const complex);
39  friend complex operator*(const complex, const complex);
40  friend complex operator*(const complex, double);
41  friend complex operator*(double, const complex);
42  friend complex operator/(const complex, const complex);
43  friend complex operator/(const complex, double);
44  friend complex operator/(double, const complex);
45 
46 private:
47  double re, im;
48 };
49 
50 inline complex::complex(double _r, double _i)
51  : re(_r)
52  , im(_i)
53 {
54 }
55 inline complex::complex(float _r, float _i)
56  : re((double)_r)
57  , im((double)_i)
58 {
59 }
61  : re(0.0)
62  , im(0.0)
63 {
64 }
65 inline void
67 {
68  re += _z.re;
69  im += _z.im;
70 }
71 inline void
73 {
74  re -= _z.re;
75  im -= _z.im;
76 }
77 inline void
79 {
80  re *= _v;
81  im *= _v;
82 }
83 inline void
85 {
86  double real_part;
87  re = (real_part = re) * value.re - im * value.im;
88  im = real_part * value.im + im * value.re;
89 }
90 /*
91 inline void complex::operator= (const complex &right)
92 {
93  re = right.re;
94  im = right.im;
95 }
96 */
97 inline double
98 real(const complex& _z)
99 {
100  return _z.re;
101 }
102 inline double
103 imag(const complex& _z)
104 {
105  return _z.im;
106 }
107 inline complex
108 conj(const complex _z)
109 {
110  return complex(_z.re, -_z.im);
111 }
112 inline double
113 norm(const complex _z)
114 {
115  return sqrt(_z.re * _z.re + _z.im * _z.im);
116 }
117 inline double
118 cabs(const complex _z)
119 {
120  return sqrt(_z.re * _z.re + _z.im * _z.im);
121 }
122 inline double
123 mag(const complex _z)
124 {
125  return sqrt(_z.re * _z.re + _z.im * _z.im);
126 }
127 inline double
128 mag_sqrd(const complex _z)
129 {
130  return _z.re * _z.re + _z.im * _z.im;
131 }
132 inline double
133 arg(const complex _z)
134 {
135  return (double)atan2((double)_z.im, (double)_z.re);
136 }
137 
138 inline complex
139 cexp(const complex _z)
140 {
141  double re_res, im_res;
142  re_res = exp(_z.re) * cos(_z.im);
143  im_res = exp(_z.im) * sin(_z.im);
144  return (complex(re_res, im_res));
145 }
146 
147 inline complex
148 sqrt(const complex _z)
149 {
150  double r, theta, re_res, im_res;
151  r = sqrt(sqrt(_z.re * _z.re + _z.im * _z.im));
152  theta = atan2(_z.im, _z.re) / 2.0;
153  re_res = r * cos(theta);
154  im_res = r * sin(theta);
155  return (complex(re_res, im_res));
156 }
157 inline complex
159 {
160  return complex(-_z.re, -_z.im);
161 }
162 
163 inline complex
164 operator+(const complex _z1, const complex _z2)
165 {
166  return complex(_z1.re + _z2.re, _z1.im + _z2.im);
167 }
168 
169 inline complex
170 operator-(const complex _z1, const complex _z2)
171 {
172  return complex(_z1.re - _z2.re, _z1.im - _z2.im);
173 }
174 
175 inline complex operator*(const complex _z1, double _v2)
176 {
177  return complex(_z1.re * _v2, _z1.im * _v2);
178 }
179 
180 inline complex operator*(double _v1, const complex _z2)
181 {
182  return complex(_z2.re * _v1, _z2.im * _v1);
183 }
184 
185 inline complex
186 operator/(const complex z1, double v2)
187 {
188  return complex(z1.re / v2, z1.im / v2);
189 }
190 
191 inline complex operator*(const complex value1, const complex value2)
192 {
193  double real, imag;
194  real = value1.re * value2.re - value1.im * value2.im;
195  imag = value1.re * value2.im + value1.im * value2.re;
196  return complex(real, imag);
197 }
198 
199 #include <iostream>
200 
201 std::ostream& operator<<(std::ostream&, const complex&);
202 std::istream& operator>>(std::istream&, complex&);
203 
204 static const complex complex_zero(0.0, 0.0);
205 
206 #endif // _complex_H_
friend double arg(const complex)
Definition: complex.h:133
complex conj(const complex _z)
Definition: complex.h:108
friend complex operator/(const complex, const complex)
Definition: complex.cpp:38
friend double real(const complex &)
Definition: complex.h:98
complex operator/(const complex z1, double v2)
Definition: complex.h:186
friend complex operator+(const complex, const complex)
Definition: complex.h:164
friend complex cexp(const complex)
Definition: complex.h:139
friend double mag_sqrd(const complex)
Definition: complex.h:128
friend complex sqrt(const complex)
Definition: complex.h:148
double arg(const complex _z)
Definition: complex.h:133
double mag(const complex _z)
Definition: complex.h:123
complex operator+(const complex _z1, const complex _z2)
Definition: complex.h:164
double cabs(const complex _z)
Definition: complex.h:118
complex sqrt(const complex _z)
Definition: complex.h:148
void operator/=(double)
friend double norm(const complex)
Definition: complex.h:113
complex cexp(const complex _z)
Definition: complex.h:139
std::istream & operator>>(std::istream &, complex &)
Definition: complex.cpp:18
friend complex operator*(const complex, const complex)
Definition: complex.h:191
void operator-=(const complex)
Definition: complex.h:72
double mag_sqrd(const complex _z)
Definition: complex.h:128
double re
Definition: complex.h:47
friend complex operator-(const complex)
Definition: complex.h:158
friend double imag(const complex &)
Definition: complex.h:103
friend complex conj(const complex)
Definition: complex.h:108
void operator*=(const complex)
Definition: complex.h:84
complex()
Definition: complex.h:60
friend double cabs(const complex)
Definition: complex.h:118
std::ostream & operator<<(std::ostream &, const complex &)
Definition: complex.cpp:11
double im
Definition: complex.h:47
complex operator*(const complex _z1, double _v2)
Definition: complex.h:175
void operator+=(const complex)
Definition: complex.h:66
double imag(const complex &_z)
Definition: complex.h:103
double real(const complex &_z)
Definition: complex.h:98
complex operator-(const complex _z)
Definition: complex.h:158
friend double mag(const complex)
Definition: complex.h:123
double norm(const complex _z)
Definition: complex.h:113