KrisLibrary  1.0.0
complex.h
1 #ifndef MATH_COMPLEX_H
2 #define MATH_COMPLEX_H
3 
4 #include "math.h"
5 #include <iosfwd>
6 #include <KrisLibrary/errors.h>
7 class File;
8 
9 namespace Math {
10 
11 class Complex;
12 class Quaternion;
13 
17 class Complex
18 {
19  public:
20  Complex();
21  Complex(const Complex&);
22  Complex(Real x);
23  Complex(Real x, Real y);
24 
25  inline bool operator == (const Complex&) const;
26  inline bool operator != (const Complex&) const;
27  inline const Complex& operator = (const Complex&);
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);
37 
38  inline void add(const Complex& a, const Complex& b);
39  inline void sub(const Complex& a, const Complex& b);
40  inline void mul(const Complex& a, const Complex& b);
41  inline void div(const Complex& a, const Complex& b);
42  inline void madd(const Complex& a, const Complex& b);
43  inline void addConjugateB(const Complex& a, const Complex& b);
44  inline void subConjugateB(const Complex& a, const Complex& b);
45  inline void mulConjugateB(const Complex& a, const Complex& b);
46  inline void divConjugateB(const Complex& a, const Complex& b);
47  inline void maddConjugateB(const Complex& a, const Complex& b);
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);
53 
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&);
63  void setExp (const Complex&);
64  bool setLog (const Complex&, int n = 0);
65  void setPow (const Complex&, Real);
66  void setPow (const Complex&, const Complex&);
67 
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;
75 
76  inline void inplaceNegative();
77  inline void inplaceScale(Real);
78  inline void inplaceNormalize();
79  inline void inplaceConjugate();
80  inline bool inplaceInverse();
81 
82  inline bool isInvertible() const;
83  inline Real norm() const;
84  inline Real normSquared() const;
85  inline Real arg() const;
86 
87  inline const Real& Re() const;
88  inline const Real& Im() const;
89  inline Real& Re();
90  inline Real& Im();
91 
92  bool Read(File&);
93  bool Write(File&)const;
94 
95  Real x, y;
96 };
97 
98 inline Real dot(const Complex& a, const Complex& b);
99 inline Complex operator - (const Complex& a);
100 inline Complex operator + (const Complex& a, const Complex& b);
101 inline Complex operator - (const Complex& a, const Complex& b);
102 inline Complex operator * (const Complex& a, const Complex& b);
103 inline Complex operator / (const Complex& a, const Complex& b);
104 inline Complex operator + (const Complex& a, Real b);
105 inline Complex operator - (const Complex& a, Real b);
106 inline Complex operator * (const Complex& a, Real b);
107 inline Complex operator / (const Complex& a, Real b);
108 inline Real Abs(const Complex& x);
109 inline Complex Sqr(const Complex& x);
110 inline Complex Sqrt(const Complex& x);
111 inline Complex Exp(const Complex& x);
112 inline Complex Log(const Complex& x);
113 inline Complex Pow(const Complex& x, Real y);
114 inline Complex Pow(const Complex& x, const Complex& y);
115 inline Complex Sin(const Complex& z);
116 inline Complex Cos(const Complex& z);
117 inline Complex Tan(const Complex& z);
118 inline Complex Sinh(const Complex& z);
119 inline Complex Cosh(const Complex& z);
120 inline Complex Tanh(const Complex& z);
121 inline Complex Inv(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);
124 inline Complex PseudoInv(const Complex& x,Real eps=Epsilon);
125 inline bool FuzzyEquals(const Complex& a, const Complex& b, const Complex& eps);
126 inline bool FuzzyZero(const Complex& a, const Complex& eps);
127 inline Complex PseudoInv(const Complex& x,const Complex& eps);
128 
129 
134 {
135  public:
136  Quaternion();
137  Quaternion(const Quaternion&);
138  Quaternion(Real w);
139  Quaternion(Real w, const Real* im);
140  Quaternion(Real w, Real x, Real y, Real z);
141 
142  inline bool operator == (const Quaternion&) const;
143  inline bool operator != (const Quaternion&) const;
144  inline const Quaternion& operator = (const Quaternion&);
145  inline void operator += (const Quaternion&);
146  inline void operator -= (const Quaternion&);
147  inline void operator *= (const Quaternion&);
148  inline void operator /= (const Quaternion&);
149  inline const Quaternion& operator = (Real);
150  inline void operator += (Real);
151  inline void operator -= (Real);
152  inline void operator *= (Real);
153  inline void operator /= (Real);
154 
155  inline void add(const Quaternion& a, const Quaternion& b);
156  inline void sub(const Quaternion& a, const Quaternion& b);
157  void mul(const Quaternion& a, const Quaternion& b);
158  void div(const Quaternion& a, const Quaternion& b);
159  void madd(const Quaternion& a, const Quaternion& b);
160  inline void add(const Quaternion& a, Real b);
161  inline void sub(const Quaternion& a, Real b);
162  inline void mul(const Quaternion& a, Real b);
163  inline void div(const Quaternion& a, Real b);
164  inline void madd(const Quaternion& a, Real b);
165 
166  inline void set(const Quaternion&);
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();
171  inline void setNegative(const Quaternion&);
172  inline void setNormalized(const Quaternion&);
173  inline void setConjugate (const Quaternion&);
174  inline bool setInverse (const Quaternion&);
175  void setExp (const Quaternion&);
176  bool setLog (const Quaternion&, int n = 0);
177  void setPow (const Quaternion&, Real);
178 
179  inline void get(Quaternion&) const;
180  inline void get(Real& w, Real* im) const;
181  inline void get(Real& w, Real& x, Real& y, Real& z) const;
182  inline void getNegative(Quaternion&) const;
183  inline void getNormalized(Quaternion&) const;
184  inline void getConjugate (Quaternion&) const;
185  inline bool getInverse (Quaternion&) const;
186 
187  inline void inplaceNegative();
188  inline void inplaceScale(Real);
189  inline void inplaceNormalize();
190  inline void inplaceConjugate();
191  inline bool inplaceInverse();
192 
193  inline bool isInvertible() const;
194  inline Real norm() const;
195  inline Real normSquared() const;
196  inline Real imNorm() const; //norm of imaginary part
197 
198  bool Read(File&);
199  bool Write(File&)const;
200 
201  union {
202  Real data [4];
203  struct { Real w, x, y, z; };
204  };
205 };
206 
207 inline Real dot(const Quaternion& a, const Quaternion& b);
208 inline Quaternion operator - (const Quaternion& a);
209 inline Quaternion operator + (const Quaternion& a, const Quaternion& b);
210 inline Quaternion operator - (const Quaternion& a, const Quaternion& b);
211 inline Quaternion operator * (const Quaternion& a, const Quaternion& b);
212 inline Quaternion operator / (const Quaternion& a, const Quaternion& b);
213 inline Quaternion operator + (const Quaternion& a, Real b);
214 inline Quaternion operator - (const Quaternion& a, Real b);
215 inline Quaternion operator * (const Quaternion& a, Real b);
216 inline Quaternion operator / (const Quaternion& a, Real b);
217 
218 
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; }
275 
276 
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; }
279 inline const Quaternion& Quaternion::operator = (const Quaternion& q) { set(q); return *this; }
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); }
321 
322 //inlined standalone functions/operators (often less efficient than the member functions)
323 
324 
325 
326 //Complex
327 
328 inline Real dot(const Complex& a, const Complex& b)
329 {
330  return a.x*b.x + a.y*b.y;
331 }
332 
333 inline Complex operator - (const Complex& a)
334 {
335  Complex temp;
336  temp.setNegative(a);
337  return temp;
338 }
339 
340 inline Complex operator + (const Complex& a, const Complex& b)
341 {
342  Complex temp;
343  temp.add(a,b);
344  return temp;
345 }
346 
347 inline Complex operator - (const Complex& a, const Complex& b)
348 {
349  Complex temp;
350  temp.sub(a,b);
351  return temp;
352 }
353 
354 inline Complex operator * (const Complex& a, const Complex& b)
355 {
356  Complex temp;
357  temp.mul(a,b);
358  return temp;
359 }
360 
361 inline Complex operator / (const Complex& a, const Complex& b)
362 {
363  Complex temp;
364  temp.div(a,b);
365  return temp;
366 }
367 
368 inline Complex operator + (const Complex& a, Real b)
369 {
370  Complex temp;
371  temp.add(a,b);
372  return temp;
373 }
374 
375 inline Complex operator - (const Complex& a, Real b)
376 {
377  Complex temp;
378  temp.sub(a,b);
379  return temp;
380 }
381 
382 inline Complex operator * (const Complex& a, Real b)
383 {
384  Complex temp;
385  temp.mul(a,b);
386  return temp;
387 }
388 
389 inline Complex operator / (const Complex& a, Real b)
390 {
391  Complex temp;
392  temp.div(a,b);
393  return temp;
394 }
395 
396 inline Real Abs(const Complex& x) { return x.norm(); }
397 inline Complex Sqr(const Complex& x) { return x*x; }
398 inline Complex Sqrt(const Complex& x) { Complex z; z.setPow(x,Half); return z; }
399 inline Complex Exp(const Complex& x) { Complex z; z.setExp(x); return z; }
400 inline Complex Log(const Complex& x) { Complex z; z.setLog(x); return z; }
401 inline Complex Pow(const Complex& x, Real y) { Complex z; z.setPow(x,y); return z; }
402 inline Complex Pow(const Complex& x,const Complex& y) { Complex z; z.setPow(x,y); return z; }
403 inline Complex Sin(const Complex& z)
404 {
405  Real ey = Exp(z.y), e_y = Inv(ey);
406  Real sx = Sin(z.x), cx = Cos(z.x);
407  Complex r;
408  r.x = Half*(ey+e_y)*sx;
409  r.y = Half*(ey-e_y)*cx;
410  return r;
411 }
412 inline Complex Cos(const Complex& z)
413 {
414  Real ey = Exp(z.y), e_y = Inv(ey);
415  Real sx = Sin(z.x), cx = Cos(z.x);
416  Complex r;
417  r.x = Half*(ey+e_y)*cx;
418  r.y = Half*(e_y-ey)*sx;
419  return r;
420 }
421 inline Complex Tan(const Complex& z)
422 {
423  Real ey = Exp(z.y), e_y = Inv(ey);
424  Real sx = Sin(z.x), cx = Cos(z.x);
425  Complex sz,cz;
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;
430  return sz/cz;
431 }
432 inline Complex Sinh(const Complex& z)
433 {
434  Real ex = Exp(z.x), e_x = Inv(ex);
435  Real sy = Sin(z.y), cy = Cos(z.y);
436  Complex r;
437  r.x = Half*(ex-e_x)*cy;
438  r.y = Half*(ex+e_x)*sy;
439  return r;
440 }
441 inline Complex Cosh(const Complex& z)
442 {
443  Real ex = Exp(z.x), e_x = Inv(ex);
444  Real sy = Sin(z.y), cy = Cos(z.y);
445  Complex r;
446  r.x = Half*(ex+e_x)*cy;
447  r.y = Half*(ex-e_x)*sy;
448  return r;
449 }
450 
451 inline Complex Tanh(const Complex& z)
452 {
453  Real ex = Exp(z.x), e_x = Inv(ex);
454  Real sy = Sin(z.y), cy = Cos(z.y);
455  Complex sz,cz;
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;
460  return sz/cz;
461 }
462 
463 inline Complex Inv(const Complex& x) { Complex z; z.setInverse(x); return z; }
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); }
470 
471 //Quaternion
472 
473 inline Real dot(const Quaternion& a, const Quaternion& b)
474 {
475  return a.w*b.w + a.x*b.x + a.y*b.y + a.z*b.z;
476 }
477 
478 inline Quaternion operator - (const Quaternion& a)
479 {
480  Quaternion temp;
481  temp.setNegative(a);
482  return temp;
483 }
484 
485 inline Quaternion operator + (const Quaternion& a, const Quaternion& b)
486 {
487  Quaternion temp;
488  temp.add(a,b);
489  return temp;
490 }
491 
492 inline Quaternion operator - (const Quaternion& a, const Quaternion& b)
493 {
494  Quaternion temp;
495  temp.sub(a,b);
496  return temp;
497 }
498 
499 inline Quaternion operator * (const Quaternion& a, const Quaternion& b)
500 {
501  Quaternion temp;
502  temp.mul(a,b);
503  return temp;
504 }
505 
506 inline Quaternion operator / (const Quaternion& a, const Quaternion& b)
507 {
508  Quaternion temp;
509  temp.div(a,b);
510  return temp;
511 }
512 
513 inline Quaternion operator + (const Quaternion& a, Real b)
514 {
515  Quaternion temp;
516  temp.add(a,b);
517  return temp;
518 }
519 
520 inline Quaternion operator - (const Quaternion& a, Real b)
521 {
522  Quaternion temp;
523  temp.sub(a,b);
524  return temp;
525 }
526 
527 inline Quaternion operator * (const Quaternion& a, Real b)
528 {
529  Quaternion temp;
530  temp.mul(a,b);
531  return temp;
532 }
533 
534 inline Quaternion operator / (const Quaternion& a, Real b)
535 {
536  Quaternion temp;
537  temp.div(a,b);
538  return temp;
539 }
540 
541 
542 //IO functions
543 
544 std::ostream& operator << (std::ostream&, const Complex&);
545 std::istream& operator >> (std::istream&, Complex&);
546 
547 std::ostream& operator << (std::ostream&, const Quaternion&);
548 std::istream& operator >> (std::istream&, Quaternion&);
549 
550 }
551 
552 #endif
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