ese 519 include files

Dependents:   PROJECT_3D_AUDIO COG4050_adxl355_tilt COG4050_adxl355_tilt COG4050_adxl355_tilt_4050

Committer:
niv17
Date:
Tue Apr 07 21:09:51 2015 +0000
Revision:
0:5347612e39a3
april_7 _ sonic start

Who changed what in which revision?

UserRevisionLine numberNew contents of line
niv17 0:5347612e39a3 1 // complex.h - declaration of class
niv17 0:5347612e39a3 2 // of complex number
niv17 0:5347612e39a3 3 //
niv17 0:5347612e39a3 4 // The code is property of LIBROW
niv17 0:5347612e39a3 5 // You can use it on your own
niv17 0:5347612e39a3 6 // When utilizing credit LIBROW site
niv17 0:5347612e39a3 7
niv17 0:5347612e39a3 8 #ifndef COMPLEX_H
niv17 0:5347612e39a3 9 #define COMPLEX_H
niv17 0:5347612e39a3 10
niv17 0:5347612e39a3 11 class complex
niv17 0:5347612e39a3 12 {
niv17 0:5347612e39a3 13 protected:
niv17 0:5347612e39a3 14 // Internal presentation - real and imaginary parts
niv17 0:5347612e39a3 15 double m_re;
niv17 0:5347612e39a3 16 double m_im;
niv17 0:5347612e39a3 17
niv17 0:5347612e39a3 18 public:
niv17 0:5347612e39a3 19 // Imaginary unity
niv17 0:5347612e39a3 20 static const complex i;
niv17 0:5347612e39a3 21 static const complex j;
niv17 0:5347612e39a3 22
niv17 0:5347612e39a3 23 // Constructors
niv17 0:5347612e39a3 24 complex(): m_re(0.), m_im(0.) {}
niv17 0:5347612e39a3 25 complex(double re, double im): m_re(re), m_im(im) {}
niv17 0:5347612e39a3 26 complex(double val): m_re(val), m_im(0.) {}
niv17 0:5347612e39a3 27
niv17 0:5347612e39a3 28 // Assignment
niv17 0:5347612e39a3 29 complex& operator= (const double val)
niv17 0:5347612e39a3 30 {
niv17 0:5347612e39a3 31 m_re = val;
niv17 0:5347612e39a3 32 m_im = 0.;
niv17 0:5347612e39a3 33 return *this;
niv17 0:5347612e39a3 34 }
niv17 0:5347612e39a3 35
niv17 0:5347612e39a3 36 // Basic operations - taking parts
niv17 0:5347612e39a3 37 double re() const { return m_re; }
niv17 0:5347612e39a3 38 double im() const { return m_im; }
niv17 0:5347612e39a3 39
niv17 0:5347612e39a3 40 // Conjugate number
niv17 0:5347612e39a3 41 complex conjugate() const
niv17 0:5347612e39a3 42 {
niv17 0:5347612e39a3 43 return complex(m_re, -m_im);
niv17 0:5347612e39a3 44 }
niv17 0:5347612e39a3 45
niv17 0:5347612e39a3 46 // Norm
niv17 0:5347612e39a3 47 double norm() const
niv17 0:5347612e39a3 48 {
niv17 0:5347612e39a3 49 return m_re * m_re + m_im * m_im;
niv17 0:5347612e39a3 50 }
niv17 0:5347612e39a3 51
niv17 0:5347612e39a3 52 // Arithmetic operations
niv17 0:5347612e39a3 53 complex operator+ (const complex& other) const
niv17 0:5347612e39a3 54 {
niv17 0:5347612e39a3 55 return complex(m_re + other.m_re, m_im + other.m_im);
niv17 0:5347612e39a3 56 }
niv17 0:5347612e39a3 57
niv17 0:5347612e39a3 58 complex operator- (const complex& other) const
niv17 0:5347612e39a3 59 {
niv17 0:5347612e39a3 60 return complex(m_re - other.m_re, m_im - other.m_im);
niv17 0:5347612e39a3 61 }
niv17 0:5347612e39a3 62
niv17 0:5347612e39a3 63 complex operator* (const complex& other) const
niv17 0:5347612e39a3 64 {
niv17 0:5347612e39a3 65 return complex(m_re * other.m_re - m_im * other.m_im,
niv17 0:5347612e39a3 66 m_re * other.m_im + m_im * other.m_re);
niv17 0:5347612e39a3 67 }
niv17 0:5347612e39a3 68
niv17 0:5347612e39a3 69 complex operator/ (const complex& other) const
niv17 0:5347612e39a3 70 {
niv17 0:5347612e39a3 71 const double denominator = other.m_re * other.m_re + other.m_im * other.m_im;
niv17 0:5347612e39a3 72 return complex((m_re * other.m_re + m_im * other.m_im) / denominator,
niv17 0:5347612e39a3 73 (m_im * other.m_re - m_re * other.m_im) / denominator);
niv17 0:5347612e39a3 74 }
niv17 0:5347612e39a3 75
niv17 0:5347612e39a3 76 complex& operator+= (const complex& other)
niv17 0:5347612e39a3 77 {
niv17 0:5347612e39a3 78 m_re += other.m_re;
niv17 0:5347612e39a3 79 m_im += other.m_im;
niv17 0:5347612e39a3 80 return *this;
niv17 0:5347612e39a3 81 }
niv17 0:5347612e39a3 82
niv17 0:5347612e39a3 83 complex& operator-= (const complex& other)
niv17 0:5347612e39a3 84 {
niv17 0:5347612e39a3 85 m_re -= other.m_re;
niv17 0:5347612e39a3 86 m_im -= other.m_im;
niv17 0:5347612e39a3 87 return *this;
niv17 0:5347612e39a3 88 }
niv17 0:5347612e39a3 89
niv17 0:5347612e39a3 90 complex& operator*= (const complex& other)
niv17 0:5347612e39a3 91 {
niv17 0:5347612e39a3 92 const double temp = m_re;
niv17 0:5347612e39a3 93 m_re = m_re * other.m_re - m_im * other.m_im;
niv17 0:5347612e39a3 94 m_im = m_im * other.m_re + temp * other.m_im;
niv17 0:5347612e39a3 95 return *this;
niv17 0:5347612e39a3 96 }
niv17 0:5347612e39a3 97
niv17 0:5347612e39a3 98 complex& operator/= (const complex& other)
niv17 0:5347612e39a3 99 {
niv17 0:5347612e39a3 100 const double denominator = other.m_re * other.m_re + other.m_im * other.m_im;
niv17 0:5347612e39a3 101 const double temp = m_re;
niv17 0:5347612e39a3 102 m_re = (m_re * other.m_re + m_im * other.m_im) / denominator;
niv17 0:5347612e39a3 103 m_im = (m_im * other.m_re - temp * other.m_im) / denominator;
niv17 0:5347612e39a3 104 return *this;
niv17 0:5347612e39a3 105 }
niv17 0:5347612e39a3 106
niv17 0:5347612e39a3 107 complex& operator++ ()
niv17 0:5347612e39a3 108 {
niv17 0:5347612e39a3 109 ++m_re;
niv17 0:5347612e39a3 110 return *this;
niv17 0:5347612e39a3 111 }
niv17 0:5347612e39a3 112
niv17 0:5347612e39a3 113 complex operator++ (int)
niv17 0:5347612e39a3 114 {
niv17 0:5347612e39a3 115 complex temp(*this);
niv17 0:5347612e39a3 116 ++m_re;
niv17 0:5347612e39a3 117 return temp;
niv17 0:5347612e39a3 118 }
niv17 0:5347612e39a3 119
niv17 0:5347612e39a3 120 complex& operator-- ()
niv17 0:5347612e39a3 121 {
niv17 0:5347612e39a3 122 --m_re;
niv17 0:5347612e39a3 123 return *this;
niv17 0:5347612e39a3 124 }
niv17 0:5347612e39a3 125
niv17 0:5347612e39a3 126 complex operator-- (int)
niv17 0:5347612e39a3 127 {
niv17 0:5347612e39a3 128 complex temp(*this);
niv17 0:5347612e39a3 129 --m_re;
niv17 0:5347612e39a3 130 return temp;
niv17 0:5347612e39a3 131 }
niv17 0:5347612e39a3 132
niv17 0:5347612e39a3 133 complex operator+ (const double val) const
niv17 0:5347612e39a3 134 {
niv17 0:5347612e39a3 135 return complex(m_re + val, m_im);
niv17 0:5347612e39a3 136 }
niv17 0:5347612e39a3 137
niv17 0:5347612e39a3 138 complex operator- (const double val) const
niv17 0:5347612e39a3 139 {
niv17 0:5347612e39a3 140 return complex(m_re - val, m_im);
niv17 0:5347612e39a3 141 }
niv17 0:5347612e39a3 142
niv17 0:5347612e39a3 143 complex operator* (const double val) const
niv17 0:5347612e39a3 144 {
niv17 0:5347612e39a3 145 return complex(m_re * val, m_im * val);
niv17 0:5347612e39a3 146 }
niv17 0:5347612e39a3 147
niv17 0:5347612e39a3 148 complex operator/ (const double val) const
niv17 0:5347612e39a3 149 {
niv17 0:5347612e39a3 150 return complex(m_re / val, m_im / val);
niv17 0:5347612e39a3 151 }
niv17 0:5347612e39a3 152
niv17 0:5347612e39a3 153 complex& operator+= (const double val)
niv17 0:5347612e39a3 154 {
niv17 0:5347612e39a3 155 m_re += val;
niv17 0:5347612e39a3 156 return *this;
niv17 0:5347612e39a3 157 }
niv17 0:5347612e39a3 158
niv17 0:5347612e39a3 159 complex& operator-= (const double val)
niv17 0:5347612e39a3 160 {
niv17 0:5347612e39a3 161 m_re -= val;
niv17 0:5347612e39a3 162 return *this;
niv17 0:5347612e39a3 163 }
niv17 0:5347612e39a3 164
niv17 0:5347612e39a3 165 complex& operator*= (const double val)
niv17 0:5347612e39a3 166 {
niv17 0:5347612e39a3 167 m_re *= val;
niv17 0:5347612e39a3 168 m_im *= val;
niv17 0:5347612e39a3 169 return *this;
niv17 0:5347612e39a3 170 }
niv17 0:5347612e39a3 171
niv17 0:5347612e39a3 172 complex& operator/= (const double val)
niv17 0:5347612e39a3 173 {
niv17 0:5347612e39a3 174 m_re /= val;
niv17 0:5347612e39a3 175 m_im /= val;
niv17 0:5347612e39a3 176 return *this;
niv17 0:5347612e39a3 177 }
niv17 0:5347612e39a3 178
niv17 0:5347612e39a3 179 friend complex operator+ (const double left, const complex& right)
niv17 0:5347612e39a3 180 {
niv17 0:5347612e39a3 181 return complex(left + right.m_re, right.m_im);
niv17 0:5347612e39a3 182 }
niv17 0:5347612e39a3 183
niv17 0:5347612e39a3 184 friend complex operator- (const double left, const complex& right)
niv17 0:5347612e39a3 185 {
niv17 0:5347612e39a3 186 return complex(left - right.m_re, -right.m_im);
niv17 0:5347612e39a3 187 }
niv17 0:5347612e39a3 188
niv17 0:5347612e39a3 189 friend complex operator* (const double left, const complex& right)
niv17 0:5347612e39a3 190 {
niv17 0:5347612e39a3 191 return complex(left * right.m_re, left * right.m_im);
niv17 0:5347612e39a3 192 }
niv17 0:5347612e39a3 193
niv17 0:5347612e39a3 194 friend complex operator/ (const double left, const complex& right)
niv17 0:5347612e39a3 195 {
niv17 0:5347612e39a3 196 const double denominator = right.m_re * right.m_re + right.m_im * right.m_im;
niv17 0:5347612e39a3 197 return complex(left * right.m_re / denominator,
niv17 0:5347612e39a3 198 -left * right.m_im / denominator);
niv17 0:5347612e39a3 199 }
niv17 0:5347612e39a3 200
niv17 0:5347612e39a3 201 // Boolean operators
niv17 0:5347612e39a3 202 bool operator== (const complex &other) const
niv17 0:5347612e39a3 203 {
niv17 0:5347612e39a3 204 return m_re == other.m_re && m_im == other.m_im;
niv17 0:5347612e39a3 205 }
niv17 0:5347612e39a3 206
niv17 0:5347612e39a3 207 bool operator!= (const complex &other) const
niv17 0:5347612e39a3 208 {
niv17 0:5347612e39a3 209 return m_re != other.m_re || m_im != other.m_im;
niv17 0:5347612e39a3 210 }
niv17 0:5347612e39a3 211
niv17 0:5347612e39a3 212 bool operator== (const double val) const
niv17 0:5347612e39a3 213 {
niv17 0:5347612e39a3 214 return m_re == val && m_im == 0.;
niv17 0:5347612e39a3 215 }
niv17 0:5347612e39a3 216
niv17 0:5347612e39a3 217 bool operator!= (const double val) const
niv17 0:5347612e39a3 218 {
niv17 0:5347612e39a3 219 return m_re != val || m_im != 0.;
niv17 0:5347612e39a3 220 }
niv17 0:5347612e39a3 221
niv17 0:5347612e39a3 222 friend bool operator== (const double left, const complex& right)
niv17 0:5347612e39a3 223 {
niv17 0:5347612e39a3 224 return left == right.m_re && right.m_im == 0.;
niv17 0:5347612e39a3 225 }
niv17 0:5347612e39a3 226
niv17 0:5347612e39a3 227 friend bool operator!= (const double left, const complex& right)
niv17 0:5347612e39a3 228 {
niv17 0:5347612e39a3 229 return left != right.m_re || right.m_im != 0.;
niv17 0:5347612e39a3 230 }
niv17 0:5347612e39a3 231 };
niv17 0:5347612e39a3 232
niv17 0:5347612e39a3 233 #endif