HologramDepthmap Library
complex.h
Go to the documentation of this file.
1 
2 //-------------------------------------------------------------------------
3 // Complex Numbers
4 // Revision : $Rev$
5 // Last changed : $Date$
6 //
7 // Author : Seungtaik Oh
8 // Last Update : 19 JUL 2011
9 //-------------------------------------------------------------------------
10 #ifndef COMPLEX_
11 #define COMPLEX_
12 
13 #include <iostream>
14 #include <cmath>
15 
16 const double PI = 3.141592653589793238462643383279502884197169399375105820974944592308;
17 const double TWO_PI = 2.0*PI;
18 
22 class Complex
23 {
24 public:
25  Complex() : a(0.0), b(0.0) {}
26  Complex( double ta, double tb ) : a(ta), b(tb) {}
27  Complex( const Complex& p )
28  {
29  a = p.a;
30  b = p.b;
31  }
32 
33  double mag2() const { return a*a+b*b; }
34  double mag() const { return sqrt(a*a+b*b); }
35 
36  double arg() const
37  {
38  double r = mag();
39  double theta = acos( a/r );
40 
41  if( sin(theta)-b/r < 10e-6 )
42  return theta;
43  else
44  return 2.0*PI-theta;
45  }
46 
47  void euler( double& r, double& theta )
48  {
49  r = mag();
50  theta = arg();
51  }
52 
53  Complex conj() const { return Complex(a, -b); }
54 
55  // arithmetic
56  const Complex& operator= (const Complex& p )
57  {
58  a = p.a;
59  b = p.b;
60 
61  return *this;
62  }
63 
64  const Complex& operator+= (const Complex& p )
65  {
66  a += p.a;
67  b += p.b;
68 
69  return *this;
70  }
71 
72  const Complex& operator-= (const Complex& p)
73  {
74  a -= p.a;
75  b -= p.b;
76 
77  return *this;
78  }
79 
80  const Complex& operator*= (const double k)
81  {
82  a *= k;
83  b *= k;
84 
85  return *this;
86  }
87 
88  const Complex& operator*= (const Complex& p)
89  {
90  const double ta = a;
91  const double tb = b;
92 
93  a = ta*p.a-tb*p.b;
94  b = ta*p.b+tb*p.a;
95 
96  return *this;
97  }
98 
99  const Complex& operator/= (const double k)
100  {
101  a /= k;
102  b /= k;
103 
104  return *this;
105  }
106 
107  friend const Complex operator+ ( const Complex& p, const Complex& q)
108  {
109  return Complex(p) += q;
110  }
111 
112  friend const Complex operator- ( const Complex& p, const Complex& q )
113  {
114  return Complex(p) -= q;
115  }
116 
117  friend const Complex operator* ( const double k, const Complex& p )
118  {
119  return Complex(p) *= k;
120  }
121 
122  friend const Complex operator* ( const Complex& p, const double k )
123  {
124  return Complex(p) *= k;
125  }
126 
127  friend const Complex operator* ( const Complex& p, const Complex& q )
128  {
129  return Complex( p.a*q.a-p.b*q.b, p.a*q.b+p.b*q.a );
130  }
131 
132  friend const Complex operator/ ( const Complex& p, const Complex& q )
133  {
134  return Complex((1.0/q.mag2())*(p*q.conj()));
135  }
136 
137  // stream
138  friend std::ostream& operator << ( std::ostream& os, const Complex& p )
139  {
140  os << "(" << p.a << ", " << p.b << ")";
141  return os;
142  }
143 
144 public:
145  double a, b;
146 };
147 
148 
149 #endif
Complex conj() const
Definition: complex.h:53
const Complex & operator=(const Complex &p)
Definition: complex.h:56
friend const Complex operator*(const double k, const Complex &p)
Definition: complex.h:117
double a
Definition: complex.h:145
Complex()
Definition: complex.h:25
friend std::ostream & operator<<(std::ostream &os, const Complex &p)
Definition: complex.h:138
friend const Complex operator-(const Complex &p, const Complex &q)
Definition: complex.h:112
const Complex & operator+=(const Complex &p)
Definition: complex.h:64
double mag() const
Definition: complex.h:34
friend const Complex operator/(const Complex &p, const Complex &q)
Definition: complex.h:132
double arg() const
Definition: complex.h:36
const Complex & operator-=(const Complex &p)
Definition: complex.h:72
void euler(double &r, double &theta)
Definition: complex.h:47
Complex(double ta, double tb)
Definition: complex.h:26
Complex(const Complex &p)
Definition: complex.h:27
double b
Definition: complex.h:145
const Complex & operator/=(const double k)
Definition: complex.h:99
class for the complex number and its arithmetic.
Definition: complex.h:22
friend const Complex operator+(const Complex &p, const Complex &q)
Definition: complex.h:107
const double TWO_PI
Definition: complex.h:17
double mag2() const
Definition: complex.h:33
const double PI
Definition: complex.h:16
const Complex & operator*=(const double k)
Definition: complex.h:80