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

FXComplexf.h
1 /********************************************************************************
2 * *
3 * S i n g 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 FXCOMPLEXF_H
22 #define FXCOMPLEXF_H
23 
24 
25 namespace FX {
26 
27 
29 class FXAPI FXComplexf {
30 public:
31  FXfloat re;
32  FXfloat im;
33 public:
34 
37 
39  FXComplexf(FXfloat r):re(r),im(0.0f){ }
40 
42  FXComplexf(FXfloat r,FXfloat i):re(r),im(i){ }
43 
45  FXComplexf(const FXComplexf& c):re(c.re),im(c.im){ }
46 
48  FXComplexf& set(FXfloat r){ re=r; im=0.0f; return *this; }
49 
51  FXComplexf& set(FXfloat r,FXfloat i){ re=r; im=i; return *this;}
52 
54  FXComplexf& set(const FXComplexf& c){ re=c.re; im=c.im; return *this;}
55 
57  FXbool operator!() const { return (re==0.0f) && (im==0.0f); }
58 
60  FXfloat& real(){ return re; }
61  const FXfloat& real() const { return re; }
62 
64  FXfloat& imag(){ return im; }
65  const FXfloat& imag() const { return im; }
66 
68  FXfloat modulus2() const { return re*re+im*im; }
69 
71  FXfloat modulus() const { return Math::sqrt(modulus2()); }
72 
74  FXfloat argument() const { return Math::atan2(im,re); }
75 
77  FXfloat& operator[](FXint i){ return (&re)[i]; }
78 
80  const FXfloat& operator[](FXint i) const { return (&re)[i]; }
81 
83  FXComplexf operator+() const { return *this; }
84  FXComplexf operator-() const { return FXComplexf(-re,-im); }
85 
87  FXComplexf& operator=(const FXfloat r){ return set(r); }
88 
90  FXComplexf& operator=(const FXComplexf& c){ return set(c); }
91 
93  FXComplexf& operator+=(FXfloat r){ re+=r; return *this; }
94  FXComplexf& operator-=(FXfloat r){ re-=r; return *this; }
95  FXComplexf& operator*=(FXfloat r){ re*=r; im*=r; return *this; }
96  FXComplexf& operator/=(FXfloat r){ re/=r; im/=r; return *this; }
97 
99  FXComplexf& operator+=(const FXComplexf& c){ return set(re+c.re,im+c.im); }
100  FXComplexf& operator-=(const FXComplexf& c){ return set(re-c.re,im-c.im); }
101  FXComplexf& operator*=(const FXComplexf& c){ return set(re*c.re-im*c.im,re*c.im+im*c.re); }
102  FXComplexf& operator/=(const FXComplexf& c){ FXfloat 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 FXComplexf conj(const FXComplexf& c){ return FXComplexf(c.real(),-c.imag()); }
111 
113 static inline FXComplexf polar(FXfloat mod,FXfloat arg){ return FXComplexf(Math::cos(arg)*mod,Math::sin(arg)*mod); }
114 
116 static inline FXfloat norm(const FXComplexf& c){ return c.real()*c.real()+c.imag()*c.imag(); }
117 
119 static inline FXfloat abs(const FXComplexf& c){ return Math::sqrt(norm(c)); }
120 
122 static inline FXfloat arg(const FXComplexf& c){ return Math::atan2(c.imag(),c.real()); }
123 
125 static inline FXComplexf exp(const FXComplexf& c){ return polar(Math::exp(c.real()),c.imag()); }
126 
128 static inline FXComplexf log(const FXComplexf& c){ return FXComplexf(Math::log(abs(c)),arg(c)); }
129 
130 
132 static inline FXbool operator==(const FXComplexf& c,FXfloat r){ return c.real()==r && c.imag()==0.0f; }
133 static inline FXbool operator!=(const FXComplexf& c,FXfloat r){ return c.real()!=r || c.imag()!=0.0f; }
134 
136 static inline FXbool operator==(FXfloat r,const FXComplexf& c){ return r==c.real() && c.imag()==0.0f; }
137 static inline FXbool operator!=(FXfloat r,const FXComplexf& c){ return r!=c.real() || c.imag()!=0.0f; }
138 
140 static inline FXbool operator==(const FXComplexf& a,const FXComplexf& b){ return a.real()==b.real() && a.imag()==b.imag(); }
141 static inline FXbool operator!=(const FXComplexf& a,const FXComplexf& b){ return a.real()!=b.real() || a.imag()!=b.imag(); }
142 
144 static inline FXComplexf operator+(const FXComplexf& a,FXfloat b){ return FXComplexf(a.real()+b,a.imag()); }
145 static inline FXComplexf operator-(const FXComplexf& a,FXfloat b){ return FXComplexf(a.real()-b,a.imag()); }
146 static inline FXComplexf operator*(const FXComplexf& a,FXfloat b){ return FXComplexf(a.real()*b,a.imag()*b); }
147 static inline FXComplexf operator/(const FXComplexf& a,FXfloat b){ return FXComplexf(a.real()/b,a.imag()/b); }
148 
150 static inline FXComplexf operator+(FXfloat a,const FXComplexf& b){ return FXComplexf(a+b.real(),b.imag()); }
151 static inline FXComplexf operator-(FXfloat a,const FXComplexf& b){ return FXComplexf(a-b.real(),-b.imag()); }
152 static inline FXComplexf operator*(FXfloat a,const FXComplexf& b){ return FXComplexf(a*b.real(),a*b.imag()); }
153 static inline FXComplexf operator/(FXfloat a,const FXComplexf& b){ FXfloat m=norm(b); return FXComplexf((a*b.real())/m,(-a*b.imag())/m); }
154 
156 static inline FXComplexf operator+(const FXComplexf& a,const FXComplexf& b){ return FXComplexf(a.real()+b.real(),a.imag()+b.imag()); }
157 static inline FXComplexf operator-(const FXComplexf& a,const FXComplexf& b){ return FXComplexf(a.real()-b.real(),a.imag()-b.imag()); }
158 static inline FXComplexf operator*(const FXComplexf& a,const FXComplexf& b){ return FXComplexf(a.real()*b.real()-a.imag()*b.imag(),a.real()*b.imag()+a.imag()*b.real()); }
159 static inline FXComplexf operator/(const FXComplexf& a,const FXComplexf& b){ FXfloat m=norm(b); return FXComplexf((a.real()*b.real()+a.imag()*b.imag())/m,(a.imag()*b.real()-a.real()*b.imag())/m); }
160 
162 static inline FXComplexf lerp(const FXComplexf& u,const FXComplexf& v,FXfloat f){ return (v-u)*f+u; }
163 
165 extern FXAPI FXComplexf csqrt(const FXComplexf& c);
166 
168 extern FXAPI FXComplexf csin(const FXComplexf& c);
169 
171 extern FXAPI FXComplexf ccos(const FXComplexf& c);
172 
174 extern FXAPI FXComplexf ctan(const FXComplexf& c);
175 
177 extern FXAPI FXComplexf csinh(const FXComplexf& c);
178 
180 extern FXAPI FXComplexf ccosh(const FXComplexf& c);
181 
183 extern FXAPI FXComplexf ctanh(const FXComplexf& c);
184 
186 extern FXAPI FXStream& operator<<(FXStream& store,const FXComplexf& c);
187 
189 extern FXAPI FXStream& operator>>(FXStream& store,FXComplexf& c);
190 
191 }
192 
193 #endif
FXfloat modulus() const
Modulus or absolute value of complex.
Definition: FXComplexf.h:71
FXComplexf(FXfloat r, FXfloat i)
Construct from components.
Definition: FXComplexf.h:42
FXfloat modulus2() const
Squared modulus.
Definition: FXComplexf.h:68
FXComplexf & operator+=(const FXComplexf &c)
Assigning operators with another complex.
Definition: FXComplexf.h:99
~FXComplexf()
Destructor.
Definition: FXComplexf.h:105
FXComplexf(const FXComplexf &c)
Initialize from another complex.
Definition: FXComplexf.h:45
FXComplexf & operator+=(FXfloat r)
Assigning operators with real.
Definition: FXComplexf.h:93
FXComplexf & operator=(const FXfloat r)
Assignment from real.
Definition: FXComplexf.h:87
Single-precision complex.
Definition: FXComplexf.h:29
FXComplexf()
Default constructor; value is not initialized.
Definition: FXComplexf.h:36
FXComplexf & operator=(const FXComplexf &c)
Assignment from another complex.
Definition: FXComplexf.h:90
Definition: FX4Splitter.h:28
FXfloat & operator[](FXint i)
Return a non-const reference to the ith element.
Definition: FXComplexf.h:77
FXfloat argument() const
Argument of complex.
Definition: FXComplexf.h:74
FXfloat & real()
Access real part.
Definition: FXComplexf.h:60
FXComplexf(FXfloat r)
Construct from real.
Definition: FXComplexf.h:39
FXbool operator!() const
Test if zero.
Definition: FXComplexf.h:57
const FXfloat & operator[](FXint i) const
Return a const reference to the ith element.
Definition: FXComplexf.h:80
FXfloat & imag()
Access imaginary part.
Definition: FXComplexf.h:64
FXComplexf operator+() const
Unary.
Definition: FXComplexf.h:83

Copyright © 1997-2022 Jeroen van der Zijp