aboutsummaryrefslogtreecommitdiffstats
path: root/Transceiver52M/Complex.h
blob: 32eb18f877f3a28db39212b9faa5ed82f5c975c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/**@file templates for Complex classes
unlike the built-in complex<> templates, these inline most operations for speed
*/

/*
* Copyright 2008 Free Software Foundation, Inc.
*
* This software is distributed under multiple licenses; see the COPYING file in the main directory for licensing information for this specific distribuion.
*
* This use of this software may be subject to additional restrictions.
* See the LEGAL file in the main directory for details.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

*/




#ifndef COMPLEXCPP_H
#define COMPLEXCPP_H

#include <math.h>
#include <ostream>


template<class Real> class Complex {

public:

  Real r, i;

  /**@name constructors */
  //@{
  /**@name from real */
  //@{
  Complex(Real real, Real imag) {r=real; i=imag;}	// x=complex(a,b)
  Complex(Real real) {r=real; i=0;}			// x=complex(a)
  //@}
  /**@name from nothing */
  //@{
  Complex() {r=(Real)0; i=(Real)0;}			// x=complex()
  //@}
  /**@name from other complex */
  //@{
  Complex(const Complex<float>& z) {r=z.r; i=z.i;}	// x=complex(z)
  Complex(const Complex<double>& z) {r=z.r; i=z.i;}	// x=complex(z)
  Complex(const Complex<long double>& z) {r=z.r; i=z.i;}	// x=complex(z)
  //@}
  //@}

  /**@name casting up from basic numeric types */
  //@{
  Complex& operator=(char a) { r=(Real)a; i=(Real)0; return *this; }
  Complex& operator=(int a) { r=(Real)a; i=(Real)0; return *this; }
  Complex& operator=(long int a) { r=(Real)a; i=(Real)0; return *this; }
  Complex& operator=(short a) { r=(Real)a; i=(Real)0; return *this; }
  Complex& operator=(float a) { r=(Real)a; i=(Real)0; return *this; }
  Complex& operator=(double a) { r=(Real)a; i=(Real)0; return *this; }
  Complex& operator=(long double a) { r=(Real)a; i=(Real)0; return *this; }
  //@}

  /**@name arithmetic */
  //@{
  /**@ binary operators */
  //@{
  Complex operator+(const Complex<Real>& a) const { return Complex<Real>(r+a.r, i+a.i); }
  Complex operator+(Real a) const { return Complex<Real>(r+a,i); }
  Complex operator-(const Complex<Real>& a) const { return Complex<Real>(r-a.r, i-a.i); }
  Complex operator-(Real a) const { return Complex<Real>(r-a,i); }
  Complex operator*(const Complex<Real>& a) const { return Complex<Real>(r*a.r-i*a.i, r*a.i+i*a.r); }
  Complex operator*(Real a) const { return Complex<Real>(r*a, i*a); }
  Complex operator/(const Complex<Real>& a) const { return operator*(a.inv()); }
  Complex operator/(Real a) const { return Complex<Real>(r/a, i/a); }
  //@}
  /*@name component-wise product */
  //@{
  Complex operator&(const Complex<Real>& a) const { return Complex<Real>(r*a.r, i*a.i); }
  //@}
  /*@name inplace operations */
  //@{
  Complex& operator+=(const Complex<Real>&);
  Complex& operator-=(const Complex<Real>&);
  Complex& operator*=(const Complex<Real>&);
  Complex& operator/=(const Complex<Real>&);
  Complex& operator+=(Real);
  Complex& operator-=(Real);
  Complex& operator*=(Real);
  Complex& operator/=(Real);
  //@}
  //@}

  /**@name comparisons */
  //@{
  bool operator==(const Complex<Real>& a) const { return ((i==a.i)&&(r==a.r)); }
  bool operator!=(const Complex<Real>& a) const { return ((i!=a.i)||(r!=a.r)); }
  bool operator<(const Complex<Real>& a) const { return norm2()<a.norm2(); }
  bool operator>(const Complex<Real>& a) const { return norm2()>a.norm2(); }
  //@}

  /// reciprocation
  Complex inv() const;	

  // unary functions -- inlined
  /**@name unary functions */
  //@{
  /**@name inlined */
  //@{
  Complex conj() const { return Complex<Real>(r,-i); }
  Real norm2() const { return i*i+r*r; }
  Complex flip() const { return Complex<Real>(i,r); }
  Real real() const { return r;}
  Real imag() const { return i;}
  Complex neg() const { return Complex<Real>(-r, -i); }
  bool isZero() const { return ((r==(Real)0) && (i==(Real)0)); }
  //@}
  /**@name not inlined due to outside calls */
  //@{
  Real abs() const { return ::sqrt(norm2()); }
  Real arg() const { return ::atan2(i,r); }
  float dB() const { return 10.0*log10(norm2()); }
  Complex exp() const { return expj(i)*(::exp(r)); }
  Complex unit() const; 			///< unit phasor with same angle
  Complex log() const { return Complex(::log(abs()),arg()); }
  Complex pow(double n) const { return expj(arg()*n)*(::pow(abs(),n)); }
  Complex sqrt() const { return pow(0.5); }
  //@}
  //@}

};


/**@name standard Complex manifestations */
//@{
typedef Complex<float> complex;
typedef Complex<double> dcomplex;
typedef Complex<short> complex16;
typedef Complex<long> complex32;
//@}


template<class Real> inline Complex<Real> Complex<Real>::inv() const
{
  Real nVal;

  nVal = norm2();
  return Complex<Real>(r/nVal, -i/nVal);
}

template<class Real> Complex<Real>& Complex<Real>::operator+=(const Complex<Real>& a)
{
  r += a.r;
  i += a.i;
  return *this;
}

template<class Real> Complex<Real>& Complex<Real>::operator*=(const Complex<Real>& a)
{
  operator*(a);
  return *this;
}

template<class Real> Complex<Real>& Complex<Real>::operator-=(const Complex<Real>& a)
{
  r -= a.r;
  i -= a.i;
  return *this;
}

template<class Real> Complex<Real>& Complex<Real>::operator/=(const Complex<Real>& a)
{
  operator/(a);
  return *this;
}


/* op= style operations with reals */

template<class Real> Complex<Real>& Complex<Real>::operator+=(Real a)
{
  r += a;
  return *this;
}

template<class Real> Complex<Real>& Complex<Real>::operator*=(Real a)
{
  r *=a;
  i *=a;
  return *this;
}

template<class Real> Complex<Real>& Complex<Real>::operator-=(Real a)
{
  r -= a;
  return *this;
}

template<class Real> Complex<Real>& Complex<Real>::operator/=(Real a)
{
  r /= a;
  i /= a;
  return *this;
}


template<class Real> Complex<Real> Complex<Real>::unit() const
{
  Real absVal = abs();
  return (Complex<Real>(r/absVal, i/absVal));
}



/**@name complex functions outside of the Complex<> class. */
//@{

/** this allows type-commutative multiplication */
template<class Real> Complex<Real> operator*(Real a, const Complex<Real>& z)
{
  return Complex<Real>(z.r*a, z.i*a);
}


/** this allows type-commutative addition */
template<class Real> Complex<Real> operator+(Real a, const Complex<Real>& z)
{
  return Complex<Real>(z.r+a, z.i);
}


/** this allows type-commutative subtraction */
template<class Real> Complex<Real> operator-(Real a, const Complex<Real>& z)
{
  return Complex<Real>(z.r-a, z.i);
}



/// e^jphi
template<class Real> Complex<Real> expj(Real phi)
{
  return Complex<Real>(cos(phi),sin(phi));
}

/// phasor expression of a complex number
template<class Real> Complex<Real> phasor(Real C, Real phi)
{
  return (expj(phi)*C);
}

/// formatted stream output
template<class Real> std::ostream& operator<<(std::ostream& os, const Complex<Real>& z)
{
  os << z.r << ' ';
  //os << z.r << ", ";
  //if (z.i>=0) { os << "+"; }
  os << z.i << "j";
  os << "\n";
  return os;
}

//@}


#endif