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,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 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 inline FXComplexf conj(const FXComplexf& c){ return FXComplexf(c.real(),-c.imag()); }
111 
113 inline FXComplexf polar(FXfloat mod,FXfloat arg){ return FXComplexf(Math::cos(arg)*mod,Math::sin(arg)*mod); }
114 
116 inline FXfloat norm(const FXComplexf& c){ return c.real()*c.real()+c.imag()*c.imag(); }
117 
119 inline FXfloat abs(const FXComplexf& c){ return Math::sqrt(norm(c)); }
120 
122 inline FXfloat arg(const FXComplexf& c){ return Math::atan2(c.imag(),c.real()); }
123 
125 inline FXComplexf exp(const FXComplexf& c){ return polar(Math::exp(c.real()),c.imag()); }
126 
128 inline FXComplexf log(const FXComplexf& c){ return FXComplexf(Math::log(abs(c)),arg(c)); }
129 
130 
131 
133 inline FXbool operator==(const FXComplexf& c,FXfloat r){ return c.real()==r && c.imag()==0.0f; }
134 inline FXbool operator!=(const FXComplexf& c,FXfloat r){ return c.real()!=r || c.imag()!=0.0f; }
135 
137 inline FXbool operator==(FXfloat r,const FXComplexf& c){ return r==c.real() && c.imag()==0.0f; }
138 inline FXbool operator!=(FXfloat r,const FXComplexf& c){ return r!=c.real() || c.imag()!=0.0f; }
139 
141 inline FXbool operator==(const FXComplexf& a,const FXComplexf& b){ return a.real()==b.real() && a.imag()==b.imag(); }
142 inline FXbool operator!=(const FXComplexf& a,const FXComplexf& b){ return a.real()!=b.real() || a.imag()!=b.imag(); }
143 
145 inline FXComplexf operator+(const FXComplexf& a,FXfloat b){ return FXComplexf(a.real()+b,a.imag()); }
146 inline FXComplexf operator-(const FXComplexf& a,FXfloat b){ return FXComplexf(a.real()-b,a.imag()); }
147 inline FXComplexf operator*(const FXComplexf& a,FXfloat b){ return FXComplexf(a.real()*b,a.imag()*b); }
148 inline FXComplexf operator/(const FXComplexf& a,FXfloat b){ return FXComplexf(a.real()/b,a.imag()/b); }
149 
151 inline FXComplexf operator+(FXfloat a,const FXComplexf& b){ return FXComplexf(a+b.real(),b.imag()); }
152 inline FXComplexf operator-(FXfloat a,const FXComplexf& b){ return FXComplexf(a-b.real(),-b.imag()); }
153 inline FXComplexf operator*(FXfloat a,const FXComplexf& b){ return FXComplexf(a*b.real(),a*b.imag()); }
154 inline FXComplexf operator/(FXfloat a,const FXComplexf& b){ FXfloat m=norm(b); return FXComplexf((a*b.real())/m,(-a*b.imag())/m); }
155 
157 inline FXComplexf operator+(const FXComplexf& a,const FXComplexf& b){ return FXComplexf(a.real()+b.real(),a.imag()+b.imag()); }
158 inline FXComplexf operator-(const FXComplexf& a,const FXComplexf& b){ return FXComplexf(a.real()-b.real(),a.imag()-b.imag()); }
159 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()); }
160 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); }
161 
163 extern FXAPI FXComplexf csqrt(const FXComplexf& c);
164 
166 extern FXAPI FXComplexf csin(const FXComplexf& c);
167 
169 extern FXAPI FXComplexf ccos(const FXComplexf& c);
170 
172 extern FXAPI FXComplexf ctan(const FXComplexf& c);
173 
175 extern FXAPI FXComplexf csinh(const FXComplexf& c);
176 
178 extern FXAPI FXComplexf ccosh(const FXComplexf& c);
179 
181 extern FXAPI FXComplexf ctanh(const FXComplexf& c);
182 
184 extern FXAPI FXStream& operator<<(FXStream& store,const FXComplexf& c);
185 
187 extern FXAPI FXStream& operator>>(FXStream& store,FXComplexf& c);
188 
189 }
190 
191 #endif

Copyright © 1997-2016 Jeroen van der Zijp