nmpp
tcmplx.h
1
2// //
3// Header file of template class of complex numbers //
4// //
5// //
7
8#ifndef _TCMPLX_H_INCLUDED_
9#define _TCMPLX_H_INCLUDED_
10
11#ifdef __cplusplus
12
13#include <assert.h>
14#include <math.h>
15
16
17
18//*****************************************************************************
49//*****************************************************************************
50
51
52template <class T> class cmplx
53{
54public:
55 T re;
56 T im;
57
58 cmplx()
59 :re(0),im(0){}
60 cmplx(T _re)
61 :re(_re),im(0){}
62 cmplx(T _re,T _im)
63 :re(_re),im(_im){}
64 cmplx(const cmplx<T>& c)
65 :re(c.re),im(c.im){}
66 explicit cmplx(cmplx<long long>& c)
67 :re(c.re),im(c.im){}
68 //------------ Z=Y --------------------------------------------------
69/*
70 cmplx<T>& operator= ( cmplx<T>& y)
71 {
72 re=y.re;
73 im=y.im;
74 return (*this);
75 }
76
77 cmplx<T>& operator= (const cmplx<T>& y)
78 {
79 re=y.re;
80 im=y.im;
81 return (*this);
82 }
83
84 //------------ Z=Y --------------------------------------------------
85 cmplx<T>& operator= (const double y)
86 {
87 re=y;
88 im=0;
89 return (*this);
90 }
91 */
92 //------------ Z=Y --------------------------------------------------
93 cmplx<T>& operator= (const int y)
94 {
95 re=y;
96 im=0;
97 return (*this);
98 }
99
100 cmplx<T>& operator+= (const cmplx<T>& y);
101 cmplx<T> operator+ (const cmplx<T>& y) const;
102 cmplx<T>& operator-= (const cmplx<T>& y);
103 cmplx<T> operator- (const cmplx<T>& y) const;
104 //------------ Z*=Y --------------------------------------------------
105 cmplx<T>& operator*= (const cmplx<T>& y)
106 {
107 cmplx<T> x(*this);
108 re=x.re*y.re-x.im*y.im;
109 im=x.re*y.im+x.im*y.re;
110 return (*this);
111 }
112 //------------ Z=X*Y --------------------------------------------------
113 template <class P> cmplx<P> operator* (const cmplx<P>& y) const
114 {
115 cmplx<P> z;
116 z.re=re*y.re-im*y.im;
117 z.im=re*y.im+im*y.re;
118 return z;
119 }
120 cmplx<T>& operator/= (const cmplx<T>& y);
121 cmplx<T> operator/ (const cmplx<T>& y) const;
122 cmplx<T> operator- (void) const;
123 //------- Z>>=y -----------------------------------------------------------
124 cmplx<T>& operator>>= (const int y)
125 {
126 re>>=y;
127 im>>=y;
128 return (*this);
129 }
130
131 //------- Z=X>>y -----------------------------------------------------------
132 cmplx<T> operator>> (const int y) const
133 {
134 cmplx<T> z(*this);
135 z>>=y;
136 return z;
137 }
138 //------- Z<<=y -----------------------------------------------------------
139 cmplx<T>& operator<<= (const int y)
140 {
141 re<<=y;
142 im<<=y;
143 return (*this);
144 }
145 //------- Z=X<<y -----------------------------------------------------------
146 cmplx<T> operator<< (const int y) const
147 {
148 cmplx<T> z(*this);
149 z<<=y;
150 return z;
151 }
152
153 bool operator== (const cmplx<T>& y) const
154 {
155 return ((re==y.re)&&(im==y.im));
156 }
157
158 bool operator!= (const cmplx<T>& y) const
159 {
160 return ((re!=y.re)||(im!=y.im));
161 }
162
163 cmplx<T> conjg () const;
164 T real () const;
165 void real (const T& Re);
166 T imag () const;
167 void imag (const T& Im);
168
169};
170
171//
172//------------ Z=Y --------------------------------------------------
173/*
174template <class T> cmplx<T>::operator cmplx<double> ()
175{
176 cmplx<Double> DblComplex;
177 DblComplex.re=re;
178 DblComplex.im=im;
179 return (DblComplex);
180}
181*/
182//------------ Z+=Y --------------------------------------------------
183template <class T> cmplx<T>& cmplx<T>::operator+= (const cmplx<T>& y)
184{
185 re+=y.re;
186 im+=y.im;
187 return (*this);
188}
189//------------ Z=X+Y --------------------------------------------------
190template <class T> cmplx<T> cmplx<T>::operator+ (const cmplx<T>& y) const
191{
192 cmplx<T> z(*this);
193 z+=y;
194 return z;
195}
196//------------ Z-=Y --------------------------------------------------
197template <class T> cmplx<T>& cmplx<T>::operator-= (const cmplx<T>& y)
198{
199 re-=y.re;
200 im-=y.im;
201 return (*this);
202}
203//------------ Z=X-Y --------------------------------------------------
204template <class T> cmplx<T> cmplx<T>::operator- (const cmplx<T>& y) const
205{
206 cmplx<T> z(*this);
207 z-=y;
208 return z;
209}
210//------------ Z=-Y --------------------------------------------------
211template <class T> cmplx<T> cmplx<T>::operator- (void) const
212{
213 cmplx<T> z(*this);
214 z.re=-z.re;
215 z.im=-z.im;
216 return z;
217}
218//------- Z/=Y -------------------------------------------------------------
219template <class T> cmplx<T>& cmplx<T>::operator/=(const cmplx<T>& y)
220{
221 //_ASSERTE((y.re!=(T)0)||(y.im!=(T)0));
222 //cmplx<T> Conjg;
223 //Conjg=y.conjg();
224 if (y.im==(T)0)
225 {
226 re/=y.re;
227 im/=y.re;
228 }
229 else
230 {
231 (*this)*=y.conjg();
232 T denum=y.re*y.re+y.im*y.im;
233 re/=denum;
234 im/=denum;
235 }
236 return (*this);
237}
238//------- Z=X/y -------------------------------------------------------------
239template <class T> cmplx<T> cmplx<T>::operator/ (const cmplx<T>& y) const
240{
241 cmplx<T> z(*this);
242 z/=y;
243 return z;
244}
245//*****************************************************************************
246//* Comparision operators *
247//*****************************************************************************
248
249//------------------------------------------------------------------------------
250
251//------------ Z=conjg(X) --------------------------------------------------
252template <class T> cmplx<T> cmplx<T>::conjg() const
253{
254 cmplx<T> z(*this);
255 z.im=-z.im;
256 return z;
257}
258
259template <class T> T cmplx<T>::real() const
260{ return re;}
261template <class T> void cmplx<T>::real(const T& Re)
262{ re=Re; }
263template <class T> T cmplx<T>::imag() const
264{ return im;}
265template <class T> void cmplx<T>::imag(const T& Im)
266{ im=Im; }
267/*
268template <class T> void Out(const cmplx<T>& x)
269{
270 printf("[");
271 Out(x.re);
272 Out(x.im);
273 printf("]");
274}
275*/
276
277//*****************************************************************************
278//* Type conversion *
279//*****************************************************************************
280/*
281template <class T> __INLINE__ void double_(cmplx<T> &X, double* &pDstVec)
282{
283 double_(X.re,pDstVec);
284 double_(X.im,pDstVec);
285}
286*/
287
288
289
290template <class T> double abs2(const cmplx<T>& x)
291{
292 return (double)(x.re*x.re+x.im*x.im);
293}
294
295template <class T, class T2> void abs2(const cmplx<T>& x, T2 &res)
296{
297 res =(T2)(x.re*x.re+x.im*x.im);
298}
299
300template <class T> double abs(const cmplx<T>& x)
301{
302 return (double)(sqrt(x.re*x.re+x.im*x.im));
303}
304
305template <class T> cmplx<T> exp(const cmplx<T> &arg)
306{
307 cmplx<T> Res(arg);
308 T ExpRe;
309 ExpRe=exp(arg.re);
310 Res.re=ExpRe*cos(arg.im);
311 Res.im=ExpRe*sin(arg.im);
312 return Res;
313}
314#endif
315
316#endif
__INLINE__ ostream & operator<<(ostream &s, mtr< unsigned char > &mtr)
Definition: nmtlio.h:64