Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXComplexd.h
1 /********************************************************************************
2 * *
3 * D o u b l e - P r e c i s i o n C o m p l e x N u m b e r *
4 * *
5 *********************************************************************************
6 * Copyright (C) 2006,2017 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published by *
10 * the Free Software Foundation; either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with this program. If not, see <http://www.gnu.org/licenses/> *
20 ********************************************************************************/
21 #ifndef FXCOMPLEXD_H
22 #define FXCOMPLEXD_H
23 
24 
25 namespace FX {
26 
27 
29 class FXAPI FXComplexd {
30 public:
31  FXdouble re;
32  FXdouble im;
33 public:
34 
37 
39  FXComplexd(FXdouble r):re(r),im(0.0){ }
40 
42  FXComplexd(FXdouble r,FXdouble i):re(r),im(i){ }
43 
45  FXComplexd(const FXComplexd& c):re(c.re),im(c.im){ }
46 
48  FXComplexd& set(FXdouble r){ re=r; im=0.0; return *this; }
49 
51  FXComplexd& set(FXdouble r,FXdouble i){ re=r; im=i; return *this;}
52 
54  FXComplexd& set(const FXComplexd& c){ re=c.re; im=c.im; return *this;}
55 
57  FXbool operator!() const { return (re==0.0) && (im==0.0); }
58 
60  FXdouble& real(){ return re; }
61  const FXdouble& real() const { return re; }
62 
64  FXdouble& imag(){ return im; }
65  const FXdouble& imag() const { return im; }
66 
68  FXdouble modulus2() const { return re*re+im*im; }
69 
71  FXdouble modulus() const { return Math::sqrt(modulus2()); }
72 
74  FXdouble argument() const { return Math::atan2(im,re); }
75 
77  FXdouble& operator[](FXint i){ return (&re)[i]; }
78 
80  const FXdouble& operator[](FXint i) const { return (&re)[i]; }
81 
83  FXComplexd operator+() const { return *this; }
84  FXComplexd operator-() const { return FXComplexd(-re,-im); }
85 
87  FXComplexd& operator=(const FXdouble r){ return set(r); }
88 
90  FXComplexd& operator=(const FXComplexd& c){ return set(c); }
91 
93  FXComplexd& operator+=(FXdouble r){ re+=r; return *this; }
94  FXComplexd& operator-=(FXdouble r){ re-=r; return *this; }
95  FXComplexd& operator*=(FXdouble r){ re*=r; im*=r; return *this; }
96  FXComplexd& operator/=(FXdouble r){ re/=r; im/=r; return *this; }
97 
99  FXComplexd& operator+=(const FXComplexd& c){ return set(re+c.re,im+c.im); }
100  FXComplexd& operator-=(const FXComplexd& c){ return set(re-c.re,im-c.im); }
101  FXComplexd& operator*=(const FXComplexd& c){ return set(re*c.re-im*c.im,re*c.im+im*c.re); }
102  FXComplexd& operator/=(const FXComplexd& c){ FXdouble m=c.re*c.re+c.im*c.im; return set((re*c.re+im*c.im)/m,(im*c.re-re*c.im)/m); }
103 
106  };
107 
108 
110 inline FXComplexd conj(const FXComplexd& c){ return FXComplexd(c.real(),-c.imag()); }
111 
113 inline FXComplexd polar(FXdouble mod,FXdouble arg){ return FXComplexd(Math::cos(arg)*mod,Math::sin(arg)*mod); }
114 
116 inline FXdouble norm(const FXComplexd& c){ return c.real()*c.real()+c.imag()*c.imag(); }
117 
119 inline FXdouble abs(const FXComplexd& c){ return Math::sqrt(norm(c)); }
120 
122 inline FXdouble arg(const FXComplexd& c){ return Math::atan2(c.imag(),c.real()); }
123 
125 inline FXComplexd exp(const FXComplexd& c){ return polar(Math::exp(c.real()),c.imag()); }
126 
128 inline FXComplexd log(const FXComplexd& c){ return FXComplexd(Math::log(abs(c)),arg(c)); }
129 
130 
131 
133 inline FXbool operator==(const FXComplexd& c,FXdouble r){ return c.real()==r && c.imag()==0.0; }
134 inline FXbool operator!=(const FXComplexd& c,FXdouble r){ return c.real()!=r || c.imag()!=0.0; }
135 
137 inline FXbool operator==(FXdouble r,const FXComplexd& c){ return r==c.real() && c.imag()==0.0; }
138 inline FXbool operator!=(FXdouble r,const FXComplexd& c){ return r!=c.real() || c.imag()!=0.0; }
139 
141 inline FXbool operator==(const FXComplexd& a,const FXComplexd& b){ return a.real()==b.real() && a.imag()==b.imag(); }
142 inline FXbool operator!=(const FXComplexd& a,const FXComplexd& b){ return a.real()!=b.real() || a.imag()!=b.imag(); }
143 
145 inline FXComplexd operator+(const FXComplexd& a,FXdouble b){ return FXComplexd(a.real()+b,a.imag()); }
146 inline FXComplexd operator-(const FXComplexd& a,FXdouble b){ return FXComplexd(a.real()-b,a.imag()); }
147 inline FXComplexd operator*(const FXComplexd& a,FXdouble b){ return FXComplexd(a.real()*b,a.imag()*b); }
148 inline FXComplexd operator/(const FXComplexd& a,FXdouble b){ return FXComplexd(a.real()/b,a.imag()/b); }
149 
151 inline FXComplexd operator+(FXdouble a,const FXComplexd& b){ return FXComplexd(a+b.real(),b.imag()); }
152 inline FXComplexd operator-(FXdouble a,const FXComplexd& b){ return FXComplexd(a-b.real(),-b.imag()); }
153 inline FXComplexd operator*(FXdouble a,const FXComplexd& b){ return FXComplexd(a*b.real(),a*b.imag()); }
154 inline FXComplexd operator/(FXdouble a,const FXComplexd& b){ FXdouble m=norm(b); return FXComplexd((a*b.real())/m,(-a*b.imag())/m); }
155 
157 inline FXComplexd operator+(const FXComplexd& a,const FXComplexd& b){ return FXComplexd(a.real()+b.real(),a.imag()+b.imag()); }
158 inline FXComplexd operator-(const FXComplexd& a,const FXComplexd& b){ return FXComplexd(a.real()-b.real(),a.imag()-b.imag()); }
159 inline FXComplexd operator*(const FXComplexd& a,const FXComplexd& b){ return FXComplexd(a.real()*b.real()-a.imag()*b.imag(),a.real()*b.imag()+a.imag()*b.real()); }
160 inline FXComplexd operator/(const FXComplexd& a,const FXComplexd& b){ FXdouble m=norm(b); return FXComplexd((a.real()*b.real()+a.imag()*b.imag())/m,(a.imag()*b.real()-a.real()*b.imag())/m); }
161 
163 extern FXAPI FXComplexd csqrt(const FXComplexd& c);
164 
166 extern FXAPI FXComplexd csin(const FXComplexd& c);
167 
169 extern FXAPI FXComplexd ccos(const FXComplexd& c);
170 
172 extern FXAPI FXComplexd ctan(const FXComplexd& c);
173 
175 extern FXAPI FXComplexd csinh(const FXComplexd& c);
176 
178 extern FXAPI FXComplexd ccosh(const FXComplexd& c);
179 
181 extern FXAPI FXComplexd ctanh(const FXComplexd& c);
182 
184 extern FXAPI FXStream& operator<<(FXStream& store,const FXComplexd& c);
185 
187 extern FXAPI FXStream& operator>>(FXStream& store,FXComplexd& c);
188 
189 }
190 
191 #endif

Copyright © 1997-2016 Jeroen van der Zijp