casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Complex.h
Go to the documentation of this file.
1 //# Complex.h: Single and double precision complex numbers
2 //# Copyright (C) 2000,2001,2002,2004
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id$
27 
28 
29 #ifndef CASA_COMPLEX_H
30 #define CASA_COMPLEX_H
31 
32 
33 //# Includes
34 #include <casacore/casa/aips.h>
36 #include <casacore/casa/complex.h>
37 
38 namespace casacore { //# NAMESPACE CASACORE - BEGIN
39 
40 // <summary>
41 // Single and double precision complex numbers
42 // </summary>
43 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
44 // </reviewed>
45 
46 // <synopsis>
47 // The class <src>Complex</src> is a straight typedef as the
48 // standard library <src>complex<float></src>.
49 //
50 // In a similar way <src>DComplex</src> is typedef-ed as
51 // <src>complex<double></src>.
52 //
53 // <linkto class=IComplex>IComplex</linkto> is defined as a specific class.
54 // It is only used by the <src>FITS</src> classes.
55 //
56 // <src>lDComplex</src> has not been defined: <src>long double</src> is not
57 // part of the standard Casacore data suite (yet)
58 //
59 // A set of global functions are added for historic reasons (they were present
60 // in the original Casacore/gcc complex implementation).
61 //
62 // See the standard library documentation for the expected behaviour of
63 // the <src>Complex</src> and <src>DComplex</src> classes.
64 //
65 // <note role=tip> In the following all references to <src>Complex</src>
66 // can be replaced with <src>DComplex</src>. with simultaneous
67 // replacement of <src>Float</src> with <src>Double</src>. </note>
68 //
69 // Complex numbers may be constructed and used in the following ways:
70 // <dl>
71 // <dt>Complex x;</dt>
72 // <dd> Declares an uninitialized Complex. </dd>
73 //
74 // <dt>Complex x = 2; Complex y(2.0);</dt>
75 // <dd> Set x and y to the Complex value (2.0, 0.0); </dd>
76 //
77 // <dt>Complex x(2, 3);</dt>
78 // <dd> Sets x to the Complex value (2, 3); </dd>
79 //
80 // <dt>Complex u(x); Complex v = x;</dt>
81 // <dd> Set u and v to the same value as x. </dd>
82 //
83 // <dt>Float real(Complex& x);</dt>
84 // <dd> returns the real part of x. </dd>
85 //
86 // <dt>Float imag(Complex& x);</dt>
87 // <dd> returns the imaginary part of x. </dd>
88 //
89 // <dt>Float abs(Complex& x);</dt>
90 // <dd> returns the magnitude of x. </dd>
91 //
92 // <dt>Float norm(Complex& x);</dt>
93 // <dd> returns the square of the magnitude of x. </dd>
94 //
95 // <dt>Float arg(Complex& x);</dt>
96 // <dd> returns the argument (amplitude) of x. </dd>
97 //
98 // <dt>Complex polar(Float r, Float t = 0.0);</dt>
99 // <dd> returns a Complex with abs of r and arg of t. </dd>
100 //
101 // <dt>Complex conj(Complex& x);</dt>
102 // <dd> returns the complex conjugate of x </dd>
103 //
104 // <dt>Complex cos(Complex& x);</dt>
105 // <dd> returns the complex cosine of x. </dd>
106 //
107 // <dt>Complex sin(Complex& x);</dt>
108 // <dd> returns the complex sine of x. </dd>
109 //
110 // <dt>Complex cosh(Complex& x);</dt>
111 // <dd> returns the complex hyperbolic cosine of x. </dd>
112 //
113 // <dt>Complex sinh(Complex& x);</dt>
114 // <dd> returns the complex hyperbolic sine of x. </dd>
115 //
116 // <dt>Complex exp(Complex& x);</dt>
117 // <dd> returns the exponential of x. </dd>
118 //
119 // <dt>Complex log(Complex& x);</dt>
120 // <dd> returns the natural log of x. </dd>
121 //
122 // <dt>Complex pow(Complex& x, long p);</dt>
123 // <dd> returns x raised to the p power. </dd>
124 //
125 // <dt>Complex pow(Complex& x, Complex& p);</dt>
126 // <dd> returns x raised to the p power. </dd>
127 //
128 // <dt>Complex sqrt(Complex& x);</dt>
129 // <dd> returns the square root of x. </dd>
130 //
131 // <dt> Complex min(Complex x,Complex y);
132 // <dd> Returns the minumum of x,y (using operator<=, i.e. the norm).
133 //
134 // <dt> Complex max(Complex x,Complex y);
135 // <dd> Returns the maximum of x,y (using operator>=, i.e. the norm).
136 //
137 // <dt>Bool near(Complex val1, Complex val2, Double tol = 1.0e-5);</dt>
138 // <dd> returns whether val1 is relatively near val2 (see Math.h).
139 // (Note the Double tolerance) </dd>
140 //
141 // <dt>Bool nearAbs(Complex val1, Complex val2, Double tol = 1.0e-5);</dt>
142 // <dd> returns whether val1 is absolutely near val2 (see Math.h).
143 // (Note the Double tolerance) </dd>
144 //
145 // <dt>ostream << x;</dt>
146 // <dd> prints x in the form (re, im). </dd>
147 //
148 // <dt>istream >> x;</dt>
149 // <dd> reads x in the form (re, im), or just (re) or re in which case the
150 // imaginary part is set to zero. </dd>
151 // </dl>
152 // </synopsis>
153 
154 //# <todo asof="2000/11/27">
155 //# </todo>
156 
157 // <group name="Complex_desc">
159 // <summary>Complex NaN and Infinity</summary>
160 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
161 // </reviewed>
162 // <group name="Complex NaN and Infinity">
163 Bool isNaN (const Complex& val);
164 void setNaN(Complex& val);
165 Bool isInf (const Complex& val);
166 void setInf(Complex& val);
167 Bool isFinite(const Complex& val);
168 // </group>
169 
170 // <summary>Complex comparisons </summary>
171 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
172 // </reviewed>
173 // <group name="Complex comparisons">
174 //# On Linux comparing the norm does not work well in debug mode
175 //# for equal values. Therefore they are compared for equality first.
176 inline Bool operator>= (const Complex& left, const Complex& right)
177  { return left==right ? True : norm(left) >= norm(right); }
178 inline Bool operator> (const Complex& left, const Complex& right)
179  { return left==right ? False : norm(left) > norm(right); }
180 inline Bool operator<= (const Complex& left, const Complex& right)
181  { return left==right ? True : norm(left) <= norm(right); }
182 inline Bool operator< (const Complex& left, const Complex& right)
183  { return left==right ? False : norm(left) < norm(right); }
184 // </group>
185 
186 
187 // <summary>DComplex NaN and Infinity</summary>
188 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
189 // </reviewed>
190 // <group name="DComplex NaN and Infinity">
191 Bool isNaN (const DComplex& val);
192 void setNaN(DComplex& val);
193 Bool isInf (const DComplex& val);
194 void setInf(DComplex& val);
195 Bool isFinite(const DComplex& val);
196 // </group>
197 
198 // <summary> DComplex comparisons </summary>
199 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
200 // </reviewed>
201 // <group name="DComplex comparisons">
202 inline Bool operator>= (const DComplex& left, const DComplex& right)
203  { return norm(left) >= norm(right); }
204 inline Bool operator> (const DComplex& left, const DComplex& right)
205  { return norm(left) > norm(right); }
206 inline Bool operator<= (const DComplex& left, const DComplex& right)
207  { return norm(left) <= norm(right); }
208 inline Bool operator< (const DComplex& left, const DComplex& right)
209  { return norm(left) < norm(right); }
210 // </group>
211 
212 
213 //# Global functions
214 // <summary> Additional complex mathematical functions </summary>
215 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
216 // </reviewed>
217 // <group name=math>
218 inline Double fabs(const DComplex &val) { return std::abs(val); }
219 inline Float fabs(const Complex &val) { return std::abs(val); }
221 inline DComplex square(const DComplex &val) { return val*val; }
222 inline Complex square(const Complex &val) { return val*val; }
223 
224 inline DComplex cube(const DComplex &val) { return val*val*val; }
225 inline Complex cube(const Complex &val) { return val*val*val; }
226 
227 // ArrayMath::pow needs this pow function.
229 
230 // We have to explicitly implement these for different type operands
231 inline DComplex operator+(const DComplex& d, const Complex& c) {
232  return (DComplex)c + d;
233 }
234 
235 inline DComplex operator+(const Complex& c, const DComplex& d) {
236  return (DComplex)c + d;
237 }
238 
239 inline DComplex operator-(const DComplex& d, const Complex& c) {
240  return d - (DComplex)c;
241 }
242 
243 inline DComplex operator-(const Complex& c, const DComplex& d) {
244  return (DComplex)c - d;
245 }
246 
247 // QMath and scimath need these operators * and /
248 // <group>
249 inline Complex operator*(const Complex& val, Double f) { return val*Float(f); }
250 inline Complex operator*(Double f, const Complex& val) { return val*Float(f); }
251 inline Complex operator/(const Complex& val, Double f) { return val/Float(f); }
252 inline Complex operator/(Double f, const Complex& val) { return Float(f)/val; }
253 // </group>
254 // These operators are useful, otherwise both Float and Double are applicable
255 // for Ints.
256 // <group>
257 inline Complex operator*(const Complex& val, Int f) { return val*Float(f); }
258 inline Complex operator*(Int f, const Complex& val) { return val*Float(f); }
259 inline Complex operator/(const Complex& val, Int f) { return val/Float(f); }
260 inline Complex operator/(Int f, const Complex& val) { return Float(f)/val; }
261 // </group>
262 // </group>
263 
264 // <summary> The near functions </summary>
265 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
266 // </reviewed>
267 // <group name=near>
268 Bool near(const Complex &val1, const Complex &val2, Double tol=1.0e-5);
269 Bool near(const DComplex &val1, const DComplex &val2, Double tol=1.0e-13);
270 Bool nearAbs(const Complex &val1, const Complex &val2, Double tol=1.0e-5);
271 Bool nearAbs(const DComplex &val1, const DComplex &val2, Double tol=1.0e-13);
272 inline Bool allNear(const Complex &val1, const Complex &val2,
273  Double tol=1.0e-5)
274  { return near(val1, val2, tol); }
275 inline Bool allNear(const DComplex &val1, const DComplex &val2,
276  Double tol=1.0e-13)
277  { return near(val1, val2, tol); }
278 inline Bool allNearAbs(const Complex &val1, const Complex &val2,
279  Double tol=1.0e-5)
280  { return nearAbs(val1, val2, tol); }
281 inline Bool allNearAbs(const DComplex &val1, const DComplex &val2,
282  Double tol=1.0e-13)
283  { return nearAbs(val1, val2, tol); }
284 // </group>
285 
286 // <summary> Max and min, floor and ceil functions </summary>
287 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
288 // </reviewed>
289 // <group name=maxmin>
290 inline Complex max(const Complex &x, const Complex &y)
291  { return x >= y ? x : y; }
292 inline DComplex max(const DComplex &x, const DComplex &y)
293  { return x >= y ? x : y; }
294 
295 inline Complex min(const Complex &x, const Complex &y)
296  { return x <= y ? x : y; }
297 inline DComplex min(const DComplex &x, const DComplex &y)
298  { return x <= y ? x : y; }
299 
300 inline Complex floor(const Complex &x) {
301  return Complex(std::floor(x.real()), std::floor(x.imag())); }
302 inline DComplex floor(const DComplex &x) {
303  return DComplex(std::floor(x.real()), std::floor(x.imag())); }
304 
305 inline Complex ceil(const Complex &x) {
306  return Complex(std::ceil(x.real()), std::ceil(x.imag())); }
307 inline DComplex ceil(const DComplex &x) {
308  return DComplex(std::ceil(x.real()), std::ceil(x.imag())); }
309 // </group>
310 
311 // <summary> fmod </summary>
312 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
313 // </reviewed>
314 // <group name=fmod>
315 DComplex fmod(const DComplex &in, const DComplex &f);
316 Complex fmod(const Complex &in, const Complex &f);
317 // </group>
318 
319 // <summary> Inverse trigonometry </summary>
320 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
321 // </reviewed>
322 // <group name=inverse>
323 // atan not valid for z == -1
324 DComplex atan(const DComplex &in);
325 Complex atan(const Complex &in);
326 DComplex asin(const DComplex &in);
327 Complex asin(const Complex &in);
328 DComplex acos(const DComplex &in);
329 Complex acos(const Complex &in);
330 DComplex atan2(const DComplex &in, const DComplex &t2);
331 Complex atan2(const Complex &in, const Complex &t2);
332 // </group>
333 
334 // <summary> Error function </summary>
335 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
336 // </reviewed>
337 // <group name=erf>
338 // Preliminary to get Functionals working. erf(z) will return erf(real(z))
339 // only for now.
340 DComplex erf(const DComplex &in);
341 Complex erf(const Complex &in);
342 DComplex erfc(const DComplex &in);
343 Complex erfc(const Complex &in);
344 // </group>
345 
346 // </group>
347 
348 } //# NAMESPACE CASACORE - END
349 
350 // Define real & complex conjugation for non-complex types
351 // and put comparisons into std namespace.
352 namespace std {
353  inline float conj(float x) { return x; }
354  inline double conj(double x) { return x; }
355  using casacore::operator>;
356  using casacore::operator>=;
357  using casacore::operator<;
358  using casacore::operator<=;
359 }
360 
361 #endif
Complex operator*(Double f, const Complex &val)
Definition: Complex.h:250
Complex operator/(const Complex &val, Int f)
Definition: Complex.h:259
int Int
Definition: aipstype.h:50
bool allNearAbs(const C1 &l, const C2 &r, U tolerance)
Test if all elements of the containers are absolutely near each other.
Definition: StdLogical.h:54
DComplex operator-(const DComplex &d, const Complex &c)
Definition: Complex.h:239
bool allNear(const C1 &l, const C2 &r, U tolerance)
Test if all elements of the containers are relatively near each other.
Definition: StdLogical.h:49
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
Complex operator*(const Complex &val, Double f)
QMath and scimath need these operators * and /.
Definition: Complex.h:249
Double fabs(const DComplex &val)
Additional complex mathematical functions.
Definition: Complex.h:219
Bool near(const GaussianBeam &left, const GaussianBeam &other, const Double relWidthTol, const Quantity &absPaTol)
Complex operator/(Double f, const Complex &val)
Definition: Complex.h:252
DComplex min(const DComplex &x, const DComplex &y)
Definition: Complex.h:297
float conj(float x)
Definition: Complex.h:353
LatticeExprNode floor(const LatticeExprNode &expr)
LatticeExprNode operator>=(const LatticeExprNode &left, const LatticeExprNode &right)
DComplex operator+(const DComplex &d, const Complex &c)
ArrayMath::pow needs this pow function.
Definition: Complex.h:231
DComplex cube(const DComplex &val)
Definition: Complex.h:224
Complex cube(const Complex &val)
Definition: Complex.h:225
TableExprNode isInf(const TableExprNode &node)
Definition: ExprNode.h:1626
DComplex ceil(const DComplex &x)
Definition: Complex.h:307
DComplex operator-(const Complex &c, const DComplex &d)
Definition: Complex.h:243
double Double
Definition: aipstype.h:55
LatticeExprNode abs(const LatticeExprNode &expr)
Numerical 1-argument functions which result in a real number regardless of input expression type...
DComplex square(const DComplex &val)
Definition: Complex.h:221
LatticeExprNode operator<=(const LatticeExprNode &left, const LatticeExprNode &right)
Complex operator*(Int f, const Complex &val)
Definition: Complex.h:258
Complex operator*(const Complex &val, Int f)
These operators are useful, otherwise both Float and Double are applicable for Ints.
Definition: Complex.h:257
TableExprNode isFinite(const TableExprNode &node)
Function to test if a scalar or array is finite.
Definition: ExprNode.h:1630
LatticeExprNode atan(const LatticeExprNode &expr)
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:42
DComplex operator+(const Complex &c, const DComplex &d)
Definition: Complex.h:235
float Float
Definition: aipstype.h:54
Complex operator/(const Complex &val, Double f)
Definition: Complex.h:251
const Bool False
Definition: aipstype.h:44
Complex min(const Complex &x, const Complex &y)
Definition: Complex.h:295
LatticeExprNode operator>(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode atan2(const LatticeExprNode &left, const LatticeExprNode &right)
Numerical 2-argument functions.
Complex operator/(Int f, const Complex &val)
Definition: Complex.h:260
DComplex floor(const DComplex &x)
Definition: Complex.h:302
LatticeExprNode fmod(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode asin(const LatticeExprNode &expr)
const Double e
e and functions thereof:
Complex square(const Complex &val)
Definition: Complex.h:222
const Double c
Fundamental physical constants (SI units):
LatticeExprNode acos(const LatticeExprNode &expr)
TableExprNode nearAbs(const TableExprNode &left, const TableExprNode &right)
Definition: ExprNode.h:1250
T norm(const TableVector< T > &tv)
Definition: TabVecMath.h:414
LatticeExprNode operator<(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode isNaN(const LatticeExprNode &expr)
Test if a value is a NaN.
LatticeExprNode ceil(const LatticeExprNode &expr)
const Bool True
Definition: aipstype.h:43