casacore
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>
35 #include <casacore/casa/BasicSL/Complexfwd.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 // The log10 should be in stl
228 // <group>
229 #if defined(NEEDS_LOG10_COMPLEX)
230 Complex log10(const Complex &val);
231 DComplex log10(const DComplex &val);
232 #endif
233 // </group>
234 
235 // ArrayMath::pow needs this pow function (on SGI).
236 inline Complex pow(const Complex& val, Double p) { return std::pow(val,Float(p)); }
237 
238 // QMath and scimath need these operators * and /
239 // <group>
240 inline Complex operator*(const Complex& val, Double f) { return val*Float(f); }
241 inline Complex operator*(Double f, const Complex& val) { return val*Float(f); }
242 inline Complex operator/(const Complex& val, Double f) { return val/Float(f); }
243 inline Complex operator/(Double f, const Complex& val) { return Float(f)/val; }
244 // </group>
245 // These operators are useful, otherwise both Float and Double are applicable
246 // for Ints.
247 // <group>
248 inline Complex operator*(const Complex& val, Int f) { return val*Float(f); }
249 inline Complex operator*(Int f, const Complex& val) { return val*Float(f); }
250 inline Complex operator/(const Complex& val, Int f) { return val/Float(f); }
251 inline Complex operator/(Int f, const Complex& val) { return Float(f)/val; }
252 // </group>
253 // </group>
254 
255 // <summary> The near functions </summary>
256 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
257 // </reviewed>
258 // <group name=near>
259 Bool near(const Complex &val1, const Complex &val2, Double tol=1.0e-5);
260 Bool near(const DComplex &val1, const DComplex &val2, Double tol=1.0e-13);
261 Bool nearAbs(const Complex &val1, const Complex &val2, Double tol=1.0e-5);
262 Bool nearAbs(const DComplex &val1, const DComplex &val2, Double tol=1.0e-13);
263 inline Bool allNear(const Complex &val1, const Complex &val2,
264  Double tol=1.0e-5)
265  { return near(val1, val2, tol); }
266 inline Bool allNear(const DComplex &val1, const DComplex &val2,
267  Double tol=1.0e-13)
268  { return near(val1, val2, tol); }
269 inline Bool allNearAbs(const Complex &val1, const Complex &val2,
270  Double tol=1.0e-5)
271  { return nearAbs(val1, val2, tol); }
272 inline Bool allNearAbs(const DComplex &val1, const DComplex &val2,
273  Double tol=1.0e-13)
274  { return nearAbs(val1, val2, tol); }
275 // </group>
276 
277 // <summary> Max and min, floor and ceil functions </summary>
278 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
279 // </reviewed>
280 // <group name=maxmin>
281 inline Complex max(const Complex &x, const Complex &y)
282  { return x >= y ? x : y; }
283 inline DComplex max(const DComplex &x, const DComplex &y)
284  { return x >= y ? x : y; }
285 
286 inline Complex min(const Complex &x, const Complex &y)
287  { return x <= y ? x : y; }
288 inline DComplex min(const DComplex &x, const DComplex &y)
289  { return x <= y ? x : y; }
290 
291 inline Complex floor(const Complex &x) {
292  return Complex(std::floor(x.real()), std::floor(x.imag())); }
293 inline DComplex floor(const DComplex &x) {
294  return DComplex(std::floor(x.real()), std::floor(x.imag())); }
295 
296 inline Complex ceil(const Complex &x) {
297  return Complex(std::ceil(x.real()), std::ceil(x.imag())); }
298 inline DComplex ceil(const DComplex &x) {
299  return DComplex(std::ceil(x.real()), std::ceil(x.imag())); }
300 // </group>
301 
302 // <summary> fmod </summary>
303 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
304 // </reviewed>
305 // <group name=fmod>
306 DComplex fmod(const DComplex &in, const DComplex &f);
307 Complex fmod(const Complex &in, const Complex &f);
308 // </group>
309 
310 // <summary> Inverse trigonometry </summary>
311 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
312 // </reviewed>
313 // <group name=inverse>
314 // atan not valid for z == -1
315 DComplex atan(const DComplex &in);
316 Complex atan(const Complex &in);
317 DComplex asin(const DComplex &in);
318 Complex asin(const Complex &in);
319 DComplex acos(const DComplex &in);
320 Complex acos(const Complex &in);
321 DComplex atan2(const DComplex &in, const DComplex &t2);
322 Complex atan2(const Complex &in, const Complex &t2);
323 // </group>
324 
325 // <summary> Error function </summary>
326 // <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
327 // </reviewed>
328 // <group name=erf>
329 // Preliminary to get Functionals working. erf(z) will return erf(real(z))
330 // only for now.
331 DComplex erf(const DComplex &in);
332 Complex erf(const Complex &in);
333 DComplex erfc(const DComplex &in);
334 Complex erfc(const Complex &in);
335 // </group>
336 
337 // </group>
338 
339 } //# NAMESPACE CASACORE - END
340 
341 // Define real & complex conjugation for non-complex types
342 // and put comparisons into std namespace.
343 // The new C++11 standard library already defines real and imag.
344 namespace std {
345  inline float conj(float x) { return x; }
346  inline double conj(double x) { return x; }
347 #if !(defined(AIPS_CXX11) || (defined(__APPLE_CC__) && __APPLE_CC__ > 5621))
348  inline float real(float x) { return x; }
349  inline double real(double x) { return x; }
350  inline float imag(float ) { return 0; }
351  inline double imag(double ) { return 0; }
352 #endif
353  using casacore::operator>;
354  using casacore::operator>=;
355  using casacore::operator<;
356  using casacore::operator<=;
357 }
358 
359 #endif
LatticeExprNode log10(const LatticeExprNode &expr)
Complex operator*(Double f, const Complex &val)
Definition: Complex.h:241
Complex operator/(const Complex &val, Int f)
Definition: Complex.h:250
int Int
Definition: aipstype.h:47
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
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 imag(const LatticeExprNode &expr)
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:240
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:243
Define real & complex conjugation for non-complex types and put comparisons into std namespace...
Definition: Complex.h:344
DComplex min(const DComplex &x, const DComplex &y)
Definition: Complex.h:288
LatticeExprNode floor(const LatticeExprNode &expr)
LatticeExprNode operator>=(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode conj(const LatticeExprNode &expr)
DComplex cube(const DComplex &val)
Definition: Complex.h:224
Float pow(Float f1, Float f2)
Definition: math.h:90
Complex cube(const Complex &val)
Definition: Complex.h:225
TableExprNode isInf(const TableExprNode &node)
Definition: ExprNode.h:1683
DComplex ceil(const DComplex &x)
Definition: Complex.h:298
double Double
Definition: aipstype.h:52
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:249
Complex operator*(const Complex &val, Int f)
These operators are useful, otherwise both Float and Double are applicable for Ints.
Definition: Complex.h:248
TableExprNode isFinite(const TableExprNode &node)
Function to test if a scalar or array is finite.
Definition: ExprNode.h:1687
LatticeExprNode atan(const LatticeExprNode &expr)
bool Bool
Define the standard types used by Casacore.
Definition: aipstype.h:39
float Float
Definition: aipstype.h:51
Complex operator/(const Complex &val, Double f)
Definition: Complex.h:242
const Bool False
Definition: aipstype.h:41
Complex min(const Complex &x, const Complex &y)
Definition: Complex.h:286
LatticeExprNode operator>(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode atan2(const LatticeExprNode &left, const LatticeExprNode &right)
Numerical 2-argument functions.
Complex pow(const Complex &val, Double p)
The log10 should be in stl.
Definition: Complex.h:236
Complex operator/(Int f, const Complex &val)
Definition: Complex.h:251
DComplex floor(const DComplex &x)
Definition: Complex.h:293
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
LatticeExprNode acos(const LatticeExprNode &expr)
TableExprNode nearAbs(const TableExprNode &left, const TableExprNode &right)
Definition: ExprNode.h:1316
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:40
this file contains all the compiler specific defines
Definition: mainpage.dox:28
LatticeExprNode real(const LatticeExprNode &expr)