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,2024 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 static inline FXComplexd conj(const FXComplexd& c){ return FXComplexd(c.real(),-c.imag()); }
111 
113 static inline FXComplexd polar(FXdouble mod,FXdouble arg){ return FXComplexd(Math::cos(arg)*mod,Math::sin(arg)*mod); }
114 
116 static inline FXdouble norm(const FXComplexd& c){ return c.real()*c.real()+c.imag()*c.imag(); }
117 
119 static inline FXdouble abs(const FXComplexd& c){ return Math::sqrt(norm(c)); }
120 
122 static inline FXdouble arg(const FXComplexd& c){ return Math::atan2(c.imag(),c.real()); }
123 
125 static inline FXComplexd exp(const FXComplexd& c){ return polar(Math::exp(c.real()),c.imag()); }
126 
128 static inline FXComplexd log(const FXComplexd& c){ return FXComplexd(Math::log(abs(c)),arg(c)); }
129 
130 
132 static inline FXbool operator==(const FXComplexd& c,FXdouble r){ return c.real()==r && c.imag()==0.0; }
133 static inline FXbool operator!=(const FXComplexd& c,FXdouble r){ return c.real()!=r || c.imag()!=0.0; }
134 
136 static inline FXbool operator==(FXdouble r,const FXComplexd& c){ return r==c.real() && c.imag()==0.0; }
137 static inline FXbool operator!=(FXdouble r,const FXComplexd& c){ return r!=c.real() || c.imag()!=0.0; }
138 
140 static inline FXbool operator==(const FXComplexd& a,const FXComplexd& b){ return a.real()==b.real() && a.imag()==b.imag(); }
141 static inline FXbool operator!=(const FXComplexd& a,const FXComplexd& b){ return a.real()!=b.real() || a.imag()!=b.imag(); }
142 
144 static inline FXComplexd operator+(const FXComplexd& a,FXdouble b){ return FXComplexd(a.real()+b,a.imag()); }
145 static inline FXComplexd operator-(const FXComplexd& a,FXdouble b){ return FXComplexd(a.real()-b,a.imag()); }
146 static inline FXComplexd operator*(const FXComplexd& a,FXdouble b){ return FXComplexd(a.real()*b,a.imag()*b); }
147 static inline FXComplexd operator/(const FXComplexd& a,FXdouble b){ return FXComplexd(a.real()/b,a.imag()/b); }
148 
150 static inline FXComplexd operator+(FXdouble a,const FXComplexd& b){ return FXComplexd(a+b.real(),b.imag()); }
151 static inline FXComplexd operator-(FXdouble a,const FXComplexd& b){ return FXComplexd(a-b.real(),-b.imag()); }
152 static inline FXComplexd operator*(FXdouble a,const FXComplexd& b){ return FXComplexd(a*b.real(),a*b.imag()); }
153 static inline FXComplexd operator/(FXdouble a,const FXComplexd& b){ FXdouble m=norm(b); return FXComplexd((a*b.real())/m,(-a*b.imag())/m); }
154 
156 static inline FXComplexd operator+(const FXComplexd& a,const FXComplexd& b){ return FXComplexd(a.real()+b.real(),a.imag()+b.imag()); }
157 static inline FXComplexd operator-(const FXComplexd& a,const FXComplexd& b){ return FXComplexd(a.real()-b.real(),a.imag()-b.imag()); }
158 static 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()); }
159 static 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); }
160 
162 static inline FXComplexd lerp(const FXComplexd& u,const FXComplexd& v,FXdouble f){ return (v-u)*f+u; }
163 
165 extern FXAPI FXComplexd csqrt(const FXComplexd& c);
166 
168 extern FXAPI FXComplexd csin(const FXComplexd& c);
169 
171 extern FXAPI FXComplexd ccos(const FXComplexd& c);
172 
174 extern FXAPI FXComplexd ctan(const FXComplexd& c);
175 
177 extern FXAPI FXComplexd csinh(const FXComplexd& c);
178 
180 extern FXAPI FXComplexd ccosh(const FXComplexd& c);
181 
183 extern FXAPI FXComplexd ctanh(const FXComplexd& c);
184 
186 extern FXAPI FXStream& operator<<(FXStream& store,const FXComplexd& c);
187 
189 extern FXAPI FXStream& operator>>(FXStream& store,FXComplexd& c);
190 
191 }
192 
193 #endif
FXComplexd(FXdouble r, FXdouble i)
Construct from components.
Definition: FXComplexd.h:42
FXComplexd(const FXComplexd &c)
Initialize from another complex.
Definition: FXComplexd.h:45
FXComplexd & operator=(const FXdouble r)
Assignment from real.
Definition: FXComplexd.h:87
FXComplexd & operator+=(const FXComplexd &c)
Assigning operators with another complex.
Definition: FXComplexd.h:99
FXComplexd operator+() const
Unary.
Definition: FXComplexd.h:83
FXdouble & imag()
Access imaginary part.
Definition: FXComplexd.h:64
~FXComplexd()
Destructor.
Definition: FXComplexd.h:105
FXdouble modulus() const
Modulus or absolute value of complex.
Definition: FXComplexd.h:71
FXComplexd()
Default constructor; value is not initialized.
Definition: FXComplexd.h:36
Definition: FX4Splitter.h:28
FXbool operator!() const
Test if zero.
Definition: FXComplexd.h:57
const FXdouble & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXComplexd.h:80
FXdouble argument() const
Argument of complex.
Definition: FXComplexd.h:74
FXComplexd(FXdouble r)
Construct from real.
Definition: FXComplexd.h:39
FXdouble modulus2() const
Squared modulus.
Definition: FXComplexd.h:68
FXdouble & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXComplexd.h:77
FXComplexd & operator=(const FXComplexd &c)
Assignment from another complex.
Definition: FXComplexd.h:90
FXComplexd & operator+=(FXdouble r)
Assigning operators with real.
Definition: FXComplexd.h:93
FXdouble & real()
Access real part.
Definition: FXComplexd.h:60
Double-precision complex.
Definition: FXComplexd.h:29

Copyright © 1997-2022 Jeroen van der Zijp