1 #ifndef SZA_UTIL_COMPLEX_H
2 #define SZA_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_ = (Type)((
unsigned)data_.real_ | (unsigned)cmplx.data_.real_);
171 data_.imag_ = (Type)((
unsigned)data_.imag_ | (unsigned)cmplx.data_.imag_);
174 void operator+=(
const Complex<Type>& cmplx) {
175 data_.real_ += cmplx.data_.real_;
176 data_.imag_ += cmplx.data_.imag_;
179 void operator-=(
const Complex<Type>& cmplx) {
180 data_.real_ -= cmplx.data_.real_;
181 data_.imag_ -= cmplx.data_.imag_;
184 Complex<Type> operator+(
const Complex<Type>& cmplx) {
185 Complex<Type> sum(data_.real_ + cmplx.data_.real_,
186 data_.imag_ + cmplx.data_.imag_);
190 Complex<Type> operator-(
const Complex<Type>& cmplx) {
191 Complex<Type> diff(data_.real_ - cmplx.data_.real_,
192 data_.imag_ - cmplx.data_.imag_);
196 Complex<Type> operator*(
const Complex<Type>& cmplx) {
199 re = data_.real_ * cmplx.data_.real_ - data_.imag_ * cmplx.data_.imag_;
200 im = data_.imag_ * cmplx.data_.real_ + data_.real_ * cmplx.data_.imag_;
202 Complex<Type> product(re, im);
206 Complex<Type> operator|(
const Complex<Type>& cmplx) {
209 re = (Type)((
unsigned)data_.real_ | (unsigned)cmplx.data_.real_);
210 im = (Type)((
unsigned)data_.imag_ | (unsigned)cmplx.data_.imag_);
212 Complex<Type> bitwiseOr(re, im);
216 Complex<Type> operator/(
const Complex<Type>& cmplx) {
217 return (*
this) * (cmplx.conjugate()/cmplx.squaredAmplitude());
220 Complex<Type> operator/(
unsigned int divisor){
221 Complex<Type> div(data_.real_ / divisor,
222 data_.imag_ / divisor);
226 Complex<Type> operator/(
double divisor){
227 Complex<Type> div(data_.real_ / divisor,
228 data_.imag_ / divisor);
234 bool operator==(Complex<Type>& comp) {
235 return (real() == comp.real()) && (imag() == comp.imag());
238 bool operator>(Complex<Type>& comp) {
239 return amp() > comp.amp();
242 bool operator>=(Complex<Type>& comp) {
243 return amp() >= comp.amp();
246 bool operator<(Complex<Type>& comp) {
247 return amp() < comp.amp();
250 bool operator<=(Complex<Type>& comp) {
251 return amp() <= comp.amp();
262 friend std::ostream& operator << <>
263 (std::ostream& os, Complex<Type>& val);
265 friend std::ostringstream& operator << <>
266 (std::ostringstream& os, Complex<Type>& val);
284 std::ostream& operator<<(std::ostream& os,
287 Type real = val.real();
288 Type imag = val.imag();
290 os << real << (imag < 0 ?
" - " :
" + ") <<
"i " << fabs(imag);
297 std::ostringstream& operator<<(std::ostringstream& os,
300 Type real = val.real();
301 Type imag = val.imag();
303 os << real() << (imag < 0 ?
"-" :
"+") <<
"i" << abs(imag);
312 #endif // End #ifndef SZA_UTIL_COMPLEX_H
float amp(std::complex< float > visibility)
Compute the visibility amplitude from a complex visibility.
std::complex< double > Complex
convenient definition of a complex number.