1 #ifndef GCP_UTIL_COMPLEX_H
2 #define GCP_UTIL_COMPLEX_H
18 #define radiansToDegrees_ 180.0/M_PI
29 std::ostream& operator<<(std::ostream& os,
32 std::ostringstream& operator<<(std::ostringstream& os,
61 Complex(std::complex<Type> cVal) {
62 data_.real_ = cVal.real();
63 data_.imag_ = cVal.imag();
70 data_.real_ = data.real_;
71 data_.imag_ = data.imag_;
76 Complex(
const Complex<Type>& complx)
78 data_.real_ = complx.data_.real_;
79 data_.imag_ = complx.data_.imag_;
91 void setReal(Type real) {
95 void setImag(Type imag) {
113 inline double amp() {
117 inline double amplitude() {
118 Type real = data_.real_;
119 Type imag = data_.imag_;
120 return sqrt((
double)(real*real + imag*imag));
123 inline double squaredAmplitude()
const {
124 Type real = data_.real_;
125 Type imag = data_.imag_;
126 return (real*real + imag*imag);
131 Complex<Type> conjugate()
const {
132 Complex<Type> conj(data_.real_, -data_.imag_);
138 inline double phaseInRadians() {
139 if((
double)data_.real_ == 0.0 && (
double)data_.imag_ == 0.0)
142 Type real = data_.real_;
143 Type imag = data_.imag_;
144 return atan2((
double)imag, (
double)real);
148 inline double phaseInDegrees() {
149 return phaseInRadians() * radiansToDegrees_;
152 inline void initialize() {
153 data_.real_ = (Type)0;
154 data_.imag_ = (Type)0;
159 void operator=(Data& data) {
160 data_.real_ = data.real_;
161 data_.imag_ = data.imag_;
164 void operator=(
const Complex<Type>& cmplx) {
165 data_.real_ = cmplx.data_.real_;
166 data_.imag_ = cmplx.data_.imag_;
169 void operator+=(
const Complex<Type>& cmplx) {
170 data_.real_ += cmplx.data_.real_;
171 data_.imag_ += cmplx.data_.imag_;
174 void operator-=(
const Complex<Type>& cmplx) {
175 data_.real_ -= cmplx.data_.real_;
176 data_.imag_ -= cmplx.data_.imag_;
179 Complex<Type> operator+(
const Complex<Type>& cmplx) {
180 Complex<Type> sum(data_.real_ + cmplx.data_.real_,
181 data_.imag_ + cmplx.data_.imag_);
185 Complex<Type> operator-(
const Complex<Type>& cmplx) {
186 Complex<Type> diff(data_.real_ - cmplx.data_.real_,
187 data_.imag_ - cmplx.data_.imag_);
191 Complex<Type> operator*(
const Complex<Type>& cmplx) {
194 re = data_.real_ * cmplx.data_.real_ - data_.imag_ * cmplx.data_.imag_;
195 im = data_.imag_ * cmplx.data_.real_ + data_.real_ * cmplx.data_.imag_;
197 Complex<Type> product(re, im);
201 Complex<Type> operator/(
const Complex<Type>& cmplx) {
202 return (*
this) * (cmplx.conjugate()/cmplx.squaredAmplitude());
205 Complex<Type> operator/(
unsigned int divisor){
206 Complex<Type> div(data_.real_ / divisor,
207 data_.imag_ / divisor);
211 Complex<Type> operator/(
double divisor){
212 Complex<Type> div(data_.real_ / divisor,
213 data_.imag_ / divisor);
219 bool operator==(Complex<Type>& comp) {
220 return (real() == comp.real()) && (imag() == comp.imag());
223 bool operator>(Complex<Type>& comp) {
224 return amp() > comp.amp();
227 bool operator>=(Complex<Type>& comp) {
228 return amp() >= comp.amp();
231 bool operator<(Complex<Type>& comp) {
232 return amp() < comp.amp();
235 bool operator<=(Complex<Type>& comp) {
236 return amp() <= comp.amp();
247 friend std::ostream& gcp::util::operator << <>
248 (std::ostream& os, Complex<Type>& val);
250 friend std::ostringstream& gcp::util::operator << <>
251 (std::ostringstream& os, Complex<Type>& val);
269 std::ostream& operator<<(std::ostream& os,
272 Type real = val.real();
273 Type imag = val.imag();
275 os << real << (imag < 0 ?
" - " :
" + ") <<
"i " << fabs(imag);
282 std::ostringstream& operator<<(std::ostringstream& os,
285 Type real = val.real();
286 Type imag = val.imag();
288 os << real() << (imag < 0 ?
"-" :
"+") <<
"i" << abs(imag);
298 #endif // End #ifndef GCP_UTIL_COMPLEX_H
virtual ~Complex()
Definition: Complex.h:89
Complex(Type real, Type imag)
Definition: Complex.h:56