6 #include <KrisLibrary/errors.h> 25 inline bool operator == (
const Complex&)
const;
26 inline bool operator != (
const Complex&)
const;
28 inline void operator += (
const Complex&);
29 inline void operator -= (
const Complex&);
30 inline void operator *= (
const Complex&);
31 inline void operator /= (
const Complex&);
32 inline const Complex& operator = (Real);
33 inline void operator += (Real);
34 inline void operator -= (Real);
35 inline void operator *= (Real);
36 inline void operator /= (Real);
48 inline void add(
const Complex& a, Real);
49 inline void sub(
const Complex& a, Real);
50 inline void mul(
const Complex& a, Real);
51 inline void div(
const Complex& a, Real);
52 inline void madd(
const Complex& a, Real);
54 inline void set(
const Complex&);
55 inline void set(Real x);
56 inline void set(Real x, Real y);
57 inline void setZero();
58 inline void setNegative(
const Complex&);
59 inline void setNormalized(
const Complex&);
60 inline void setConjugate(
const Complex&);
61 inline void setPolar(Real r, Real theta);
62 inline bool setInverse(
const Complex&);
64 bool setLog (
const Complex&,
int n = 0);
65 void setPow (
const Complex&, Real);
68 inline void get(
Complex&)
const;
69 inline void get(Real& x, Real& y)
const;
70 inline void getNegative(
Complex&)
const;
71 inline void getNormalized(
Complex&)
const;
72 inline void getConjugate(
Complex&)
const;
73 inline void getPolar(Real& r, Real& theta);
74 inline bool getInverse(
Complex&)
const;
76 inline void inplaceNegative();
77 inline void inplaceScale(Real);
78 inline void inplaceNormalize();
79 inline void inplaceConjugate();
80 inline bool inplaceInverse();
82 inline bool isInvertible()
const;
83 inline Real norm()
const;
84 inline Real normSquared()
const;
85 inline Real arg()
const;
87 inline const Real& Re()
const;
88 inline const Real& Im()
const;
93 bool Write(
File&)
const;
108 inline Real Abs(
const Complex& x);
122 inline bool FuzzyEquals(
const Complex& a,
const Complex& b, Real eps=Epsilon);
123 inline bool FuzzyZero(
const Complex& a, Real eps=Epsilon);
142 inline bool operator == (
const Quaternion&)
const;
143 inline bool operator != (
const Quaternion&)
const;
150 inline void operator += (Real);
151 inline void operator -= (Real);
152 inline void operator *= (Real);
153 inline void operator /= (Real);
164 inline void madd(
const Quaternion& a, Real b);
167 inline void set(Real w);
168 inline void set(Real w,
const Real* im);
169 inline void set(Real w, Real x, Real y, Real z);
170 inline void setZero();
180 inline void get(Real& w, Real* im)
const;
181 inline void get(Real& w, Real& x, Real& y, Real& z)
const;
187 inline void inplaceNegative();
188 inline void inplaceScale(Real);
189 inline void inplaceNormalize();
190 inline void inplaceConjugate();
191 inline bool inplaceInverse();
193 inline bool isInvertible()
const;
194 inline Real norm()
const;
195 inline Real normSquared()
const;
196 inline Real imNorm()
const;
199 bool Write(
File&)
const;
203 struct { Real w, x, y, z; };
219 inline bool Complex::operator == (
const Complex& a)
const {
return a.x == x && a.y == y; }
220 inline bool Complex::operator != (
const Complex& a)
const {
return a.x != x || a.y != y; }
221 inline const Complex& Complex::operator = (
const Complex& z) {
set(z);
return *
this; }
222 inline void Complex::operator += (
const Complex& z) { x += z.x; y += z.y; }
223 inline void Complex::operator -= (
const Complex& z) { x -= z.x; y -= z.y; }
224 inline void Complex::operator *= (
const Complex& z) { Real tmp = x*z.x - y*z.y; y = x*z.y + y*z.x; x = tmp; }
225 inline void Complex::operator /= (
const Complex& z) { div(*
this, z); }
226 inline const Complex& Complex::operator = (Real a) { x = a; y = Zero;
return *
this; }
227 inline void Complex::operator += (Real a) { x += a; }
228 inline void Complex::operator -= (Real a) { x -= a; }
229 inline void Complex::operator *= (Real a) { x *= a; y *= a; }
230 inline void Complex::operator /= (Real a) { Real ainv = Inv(a); operator *= (ainv); }
231 inline void Complex::add(
const Complex& a,
const Complex& b) { x = a.x + b.x; y = a.y + b.y; }
232 inline void Complex::sub(
const Complex& a,
const Complex& b) { x = a.x - b.x; y = a.y - b.y; }
233 inline void Complex::mul(
const Complex& a,
const Complex& b) { Real tmp = a.x*b.x - a.y*b.y; y = a.x*b.y + a.y*b.x; x = tmp; }
234 inline void Complex::madd(
const Complex& a,
const Complex& b) { Real tmp = a.x*b.x - a.y*b.y; y += a.x*b.y + a.y*b.x; x += tmp; }
235 inline void Complex::div(
const Complex& a,
const Complex& b) { Real l = Inv(b.normSquared()); Real tmp = a.x*b.x + a.y*b.y; y = a.y*b.x - a.x*b.y; x = tmp; x*=l; y*=l; }
236 inline void Complex::addConjugateB(
const Complex& a,
const Complex& b) { x = a.x + b.x; y = a.y - b.y; }
237 inline void Complex::subConjugateB(
const Complex& a,
const Complex& b) { x = a.x - b.x; y = a.y + b.y; }
238 inline void Complex::mulConjugateB(
const Complex& a,
const Complex& b) { Real tmp = a.x*b.x + a.y*b.y; y = -a.x*b.y + a.y*b.x; x = tmp; }
239 inline void Complex::maddConjugateB(
const Complex& a,
const Complex& b) { Real tmp = a.x*b.x + a.y*b.y; y += -a.x*b.y + a.y*b.x; x += tmp; }
240 inline void Complex::divConjugateB(
const Complex& a,
const Complex& b) { Real l = Inv(b.normSquared()); Real tmp = a.x*b.x - a.y*b.y; y = a.y*b.x + a.x*b.y; x = tmp; x*=l; y*=l; }
241 inline void Complex::add(
const Complex& a, Real b) { x = a.x + b; }
242 inline void Complex::sub(
const Complex& a, Real b) { x = a.x - b; }
243 inline void Complex::mul(
const Complex& a, Real b) { x = a.x*b; y = a.y*b; }
244 inline void Complex::div(
const Complex& a, Real b) { Real binv = Inv(b); mul(a,binv); }
245 inline void Complex::madd(
const Complex& a, Real b) { x += a.x*b; y += a.y*b; }
246 inline void Complex::set(
const Complex& z) { x = z.x; y = z.y; }
247 inline void Complex::set(Real _x) { x = _x; y = Zero; }
248 inline void Complex::set(Real _x, Real _y) { x = _x; y = _y; }
249 inline void Complex::setZero() { x = Zero; y = Zero; }
250 inline void Complex::setNegative(
const Complex& z) { x = -z.x; y = -z.y; }
251 inline void Complex::setNormalized(
const Complex& z) { div(z,z.norm()); }
252 inline void Complex::setConjugate(
const Complex& z) { x = z.x; y = -z.y; }
253 inline void Complex::setPolar(Real r, Real theta) { x=Cos(theta)*r; y=Sin(theta)*r; }
254 inline bool Complex::setInverse(
const Complex& z) { Real n2 = z.normSquared();
if(n2==Zero)
return false; div(z,n2); inplaceConjugate();
return true;}
255 inline void Complex::get(
Complex& z)
const { z.x = x; z.y = y; }
256 inline void Complex::get(Real& _x, Real& _y)
const { _x = x; _y = y; }
257 inline void Complex::getNegative(
Complex& z)
const { z.setNegative(*
this); }
258 inline void Complex::getNormalized(
Complex& z)
const { z.setNormalized(*
this); }
259 inline void Complex::getConjugate(
Complex& z)
const { z.setConjugate(*
this); }
260 inline void Complex::getPolar(Real& r, Real& theta) { r = norm(); theta=arg(); }
261 inline bool Complex::getInverse(
Complex& z)
const {
return z.setInverse(*
this); }
262 inline void Complex::inplaceNegative() { x = -x; y = -y; }
263 inline void Complex::inplaceScale(Real c) { x*=c; y*=c; }
264 inline void Complex::inplaceNormalize() { inplaceScale(Inv(norm())); }
265 inline void Complex::inplaceConjugate() { y = -y; }
266 inline bool Complex::inplaceInverse() { Real n2 = normSquared();
if(n2==Zero)
return false; inplaceScale(Inv(n2)); inplaceConjugate();
return true; }
267 inline bool Complex::isInvertible()
const {
return !(x == Zero && y == Zero); }
268 inline Real Complex::normSquared()
const {
return Sqr(x)+Sqr(y); }
269 inline Real Complex::norm()
const {
return Sqrt(normSquared()); }
270 inline Real Complex::arg()
const {
return Atan2(y,x); }
271 inline const Real& Complex::Re()
const {
return x; }
272 inline const Real& Complex::Im()
const {
return y; }
273 inline Real& Complex::Re() {
return x; }
274 inline Real& Complex::Im() {
return y; }
277 inline bool Quaternion::operator == (
const Quaternion& a)
const {
return a.x == x && a.y == y && a.z == z && a.w == w; }
278 inline bool Quaternion::operator != (
const Quaternion& a)
const {
return a.x != x || a.y != y || a.z != z || a.w != w; }
280 inline void Quaternion::operator += (
const Quaternion& q) { w+=q.w; x+=q.x; y+=q.y; z+=q.z; }
281 inline void Quaternion::operator -= (
const Quaternion& q) { w-=q.w; x-=q.x; y-=q.y; z-=q.z; }
282 inline void Quaternion::operator *= (
const Quaternion& q) { mul(*
this,q); }
283 inline void Quaternion::operator /= (
const Quaternion& q) { div(*
this,q); }
284 inline const Quaternion& Quaternion::operator = (Real s) {
set(s);
return *
this; }
285 inline void Quaternion::operator += (Real s) { w += s; }
286 inline void Quaternion::operator -= (Real s) { w -= s; }
287 inline void Quaternion::operator *= (Real s) { w*=s; x*=s; y*=s; z*=s; }
288 inline void Quaternion::operator /= (Real s) { Real s_inv = Inv(s); operator *= (s_inv); }
289 inline void Quaternion::add(
const Quaternion& a, Real b) { w=a.w+b; x=a.x; y=a.y; z=a.z; }
290 inline void Quaternion::sub(
const Quaternion& a, Real b) { w=a.w-b; x=a.x; y=a.y; z=a.z; }
291 inline void Quaternion::add(
const Quaternion& a,
const Quaternion& b) { w=a.w+b.w; x=a.x+b.x; y=a.y+b.y; z=a.z+b.z; }
292 inline void Quaternion::sub(
const Quaternion& a,
const Quaternion& b) { w=a.w-b.w; x=a.x-b.x; y=a.y-b.y; z=a.z-b.z; }
293 inline void Quaternion::mul(
const Quaternion& a, Real b) { w=a.w*b; x=a.x*b; y=a.y*b; z=a.z*b; }
294 inline void Quaternion::div(
const Quaternion& a, Real b) { Real binv = Inv(b); mul(a,binv); }
295 inline void Quaternion::madd(
const Quaternion& a, Real b) { w+=a.w*b; x+=a.x*b; y+=a.y*b; z+=a.z*b; }
296 inline void Quaternion::set(
const Quaternion& q) { w=q.w; x=q.x; y=q.y; z=q.z; }
297 inline void Quaternion::set(Real _w) { w=_w; x = y = z = Zero; }
298 inline void Quaternion::set(Real _w,
const Real* im) { w=_w; x=im[0]; y=im[1]; z=im[2]; }
299 inline void Quaternion::set(Real _w, Real _x, Real _y, Real _z) { w=_w; x=_x; y=_y; z=_z; }
300 inline void Quaternion::setZero() { w = x = y = z = Zero; }
301 inline void Quaternion::setNegative(
const Quaternion& q) { w=-q.w; x=-q.x; y=-q.y; z=-q.z; }
302 inline void Quaternion::setNormalized(
const Quaternion& q) { div(q,q.norm()); }
303 inline void Quaternion::setConjugate (
const Quaternion& q) { w = q.w; x = -q.x; y = -q.y; z = -q.z; }
304 inline bool Quaternion::setInverse (
const Quaternion& q) { Real n2=q.normSquared();
if(n2==Zero)
return false; div(q,Inv(n2)); inplaceConjugate();
return true; }
305 inline void Quaternion::get(
Quaternion& q)
const { q.set(*
this); }
306 inline void Quaternion::get(Real& _w, Real* im)
const { _w=w; im[0]=x; im[1]=y; im[2]=z; }
307 inline void Quaternion::get(Real& _w, Real& _x, Real& _y, Real& _z)
const { _w=w; _x=x; _y=y; _z=z; }
308 inline void Quaternion::getNegative(
Quaternion& q)
const { q.setNegative(*
this); }
309 inline void Quaternion::getNormalized(
Quaternion& q)
const { q.setNormalized(*
this); }
310 inline void Quaternion::getConjugate (
Quaternion& q)
const { q.setConjugate(*
this); }
311 inline bool Quaternion::getInverse (
Quaternion& q)
const {
return q.setInverse(*
this); }
312 inline void Quaternion::inplaceNegative() { w=-w; x=-x; y=-y; z=-z; }
313 inline void Quaternion::inplaceScale(Real c) { w*=c; x*=c; y*=c; z*=c; }
314 inline void Quaternion::inplaceNormalize() { inplaceScale(Inv(norm())); }
315 inline void Quaternion::inplaceConjugate() { x=-x; y=-y; z=-z; }
316 inline bool Quaternion::inplaceInverse() { Real n2 = normSquared();
if(n2==Zero)
return false; inplaceScale(Inv(n2)); inplaceConjugate();
return true; }
317 inline bool Quaternion::isInvertible()
const {
return !(w == Zero && x == Zero && y == Zero && z == Zero); }
318 inline Real Quaternion::norm()
const {
return Sqrt(normSquared()); }
319 inline Real Quaternion::normSquared()
const {
return Sqr(w)+Sqr(x)+Sqr(y)+Sqr(z); }
320 inline Real Quaternion::imNorm()
const {
return Sqr(x)+Sqr(y)+Sqr(z); }
330 return a.x*b.x + a.y*b.y;
396 inline Real Abs(
const Complex& x) {
return x.norm(); }
405 Real ey = Exp(z.y), e_y = Inv(ey);
406 Real sx = Sin(z.x), cx = Cos(z.x);
408 r.x = Half*(ey+e_y)*sx;
409 r.y = Half*(ey-e_y)*cx;
414 Real ey = Exp(z.y), e_y = Inv(ey);
415 Real sx = Sin(z.x), cx = Cos(z.x);
417 r.x = Half*(ey+e_y)*cx;
418 r.y = Half*(e_y-ey)*sx;
423 Real ey = Exp(z.y), e_y = Inv(ey);
424 Real sx = Sin(z.x), cx = Cos(z.x);
426 sz.x = Half*(ey+e_y)*cx;
427 sz.y = Half*(e_y-ey)*sx;
428 cz.x = Half*(ey+e_y)*sx;
429 cz.y = Half*(ey-e_y)*cx;
434 Real ex = Exp(z.x), e_x = Inv(ex);
435 Real sy = Sin(z.y), cy = Cos(z.y);
437 r.x = Half*(ex-e_x)*cy;
438 r.y = Half*(ex+e_x)*sy;
443 Real ex = Exp(z.x), e_x = Inv(ex);
444 Real sy = Sin(z.y), cy = Cos(z.y);
446 r.x = Half*(ex+e_x)*cy;
447 r.y = Half*(ex-e_x)*sy;
453 Real ex = Exp(z.x), e_x = Inv(ex);
454 Real sy = Sin(z.y), cy = Cos(z.y);
456 sz.x = Half*(ex-e_x)*cy;
457 sz.y = Half*(ex+e_x)*sy;
458 cz.x = Half*(ex+e_x)*cy;
459 cz.y = Half*(ex-e_x)*sy;
464 inline bool FuzzyEquals(
const Complex& a,
const Complex& b, Real eps) {
return Abs(a-b) <= eps; }
465 inline bool FuzzyZero(
const Complex& a, Real eps) {
return Abs(a) <= eps; }
466 inline Complex PseudoInv(
const Complex& x,Real eps) {
if(Abs(x)<eps)
return Complex(Zero);
return Inv(x); }
467 inline bool FuzzyEquals(
const Complex& a,
const Complex& b,
const Complex& eps) { Assert(eps.y==Zero);
return FuzzyEquals(a,b,eps.x); }
468 inline bool FuzzyZero(
const Complex& a,
const Complex& eps) { Assert(eps.y==Zero);
return FuzzyZero(a,eps.x); }
469 inline Complex PseudoInv(
const Complex& x,
const Complex& eps) { Assert(eps.y==Zero);
return PseudoInv(x,eps.x); }
475 return a.w*b.w + a.x*b.x + a.y*b.y + a.z*b.z;
544 std::ostream& operator << (std::ostream&,
const Complex&);
545 std::istream& operator >> (std::istream&,
Complex&);
547 std::ostream& operator << (std::ostream&,
const Quaternion&);
548 std::istream& operator >> (std::istream&,
Quaternion&);
Common math typedefs, constants, functions.
Complex number class (x + iy).
Definition: complex.h:17
Complex quaternion class (w + ix + jy + kz).
Definition: complex.h:133
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12
A cross-platform class for reading/writing binary data.
Definition: File.h:47