casacore
Loading...
Searching...
No Matches
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: casa-feedback@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
27#ifndef CASA_COMPLEX_H
28#define CASA_COMPLEX_H
29
30
31//# Includes
32#include <casacore/casa/aips.h>
33#include <casacore/casa/BasicSL/Complexfwd.h>
34#include <casacore/casa/complex.h>
35
36namespace casacore { //# NAMESPACE CASACORE - BEGIN
37
38// <summary>
39// Single and double precision complex numbers
40// </summary>
41// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
42// </reviewed>
43
44// <synopsis>
45// The class <src>Complex</src> is a straight typedef as the
46// standard library <src>complex<float></src>.
47//
48// In a similar way <src>DComplex</src> is typedef-ed as
49// <src>complex<double></src>.
50//
51// <linkto class=IComplex>IComplex</linkto> is defined as a specific class.
52// It is only used by the <src>FITS</src> classes.
53//
54// <src>lDComplex</src> has not been defined: <src>long double</src> is not
55// part of the standard Casacore data suite (yet)
56//
57// A set of global functions are added for historic reasons (they were present
58// in the original Casacore/gcc complex implementation).
59//
60// See the standard library documentation for the expected behaviour of
61// the <src>Complex</src> and <src>DComplex</src> classes.
62//
63// <note role=tip> In the following all references to <src>Complex</src>
64// can be replaced with <src>DComplex</src>. with simultaneous
65// replacement of <src>Float</src> with <src>Double</src>. </note>
66//
67// Complex numbers may be constructed and used in the following ways:
68// <dl>
69// <dt>Complex x;</dt>
70// <dd> Declares an uninitialized Complex. </dd>
71//
72// <dt>Complex x = 2; Complex y(2.0);</dt>
73// <dd> Set x and y to the Complex value (2.0, 0.0); </dd>
74//
75// <dt>Complex x(2, 3);</dt>
76// <dd> Sets x to the Complex value (2, 3); </dd>
77//
78// <dt>Complex u(x); Complex v = x;</dt>
79// <dd> Set u and v to the same value as x. </dd>
80//
81// <dt>Float real(Complex& x);</dt>
82// <dd> returns the real part of x. </dd>
83//
84// <dt>Float imag(Complex& x);</dt>
85// <dd> returns the imaginary part of x. </dd>
86//
87// <dt>Float abs(Complex& x);</dt>
88// <dd> returns the magnitude of x. </dd>
89//
90// <dt>Float norm(Complex& x);</dt>
91// <dd> returns the square of the magnitude of x. </dd>
92//
93// <dt>Float arg(Complex& x);</dt>
94// <dd> returns the argument (amplitude) of x. </dd>
95//
96// <dt>Complex polar(Float r, Float t = 0.0);</dt>
97// <dd> returns a Complex with abs of r and arg of t. </dd>
98//
99// <dt>Complex conj(Complex& x);</dt>
100// <dd> returns the complex conjugate of x </dd>
101//
102// <dt>Complex cos(Complex& x);</dt>
103// <dd> returns the complex cosine of x. </dd>
104//
105// <dt>Complex sin(Complex& x);</dt>
106// <dd> returns the complex sine of x. </dd>
107//
108// <dt>Complex cosh(Complex& x);</dt>
109// <dd> returns the complex hyperbolic cosine of x. </dd>
110//
111// <dt>Complex sinh(Complex& x);</dt>
112// <dd> returns the complex hyperbolic sine of x. </dd>
113//
114// <dt>Complex exp(Complex& x);</dt>
115// <dd> returns the exponential of x. </dd>
116//
117// <dt>Complex log(Complex& x);</dt>
118// <dd> returns the natural log of x. </dd>
119//
120// <dt>Complex pow(Complex& x, long p);</dt>
121// <dd> returns x raised to the p power. </dd>
122//
123// <dt>Complex pow(Complex& x, Complex& p);</dt>
124// <dd> returns x raised to the p power. </dd>
125//
126// <dt>Complex sqrt(Complex& x);</dt>
127// <dd> returns the square root of x. </dd>
128//
129// <dt> Complex min(Complex x,Complex y);
130// <dd> Returns the minumum of x,y (using operator<=, i.e. the norm).
131//
132// <dt> Complex max(Complex x,Complex y);
133// <dd> Returns the maximum of x,y (using operator>=, i.e. the norm).
134//
135// <dt>Bool near(Complex val1, Complex val2, Double tol = 1.0e-5);</dt>
136// <dd> returns whether val1 is relatively near val2 (see Math.h).
137// (Note the Double tolerance) </dd>
138//
139// <dt>Bool nearAbs(Complex val1, Complex val2, Double tol = 1.0e-5);</dt>
140// <dd> returns whether val1 is absolutely near val2 (see Math.h).
141// (Note the Double tolerance) </dd>
142//
143// <dt>ostream << x;</dt>
144// <dd> prints x in the form (re, im). </dd>
145//
146// <dt>istream >> x;</dt>
147// <dd> reads x in the form (re, im), or just (re) or re in which case the
148// imaginary part is set to zero. </dd>
149// </dl>
150// </synopsis>
151
152//# <todo asof="2000/11/27">
153//# </todo>
154
155// <group name="Complex_desc">
157// <summary>Complex NaN and Infinity</summary>
158// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
159// </reviewed>
160// <group name="Complex NaN and Infinity">
161Bool isNaN (const Complex& val);
162void setNaN(Complex& val);
163Bool isInf (const Complex& val);
164void setInf(Complex& val);
165Bool isFinite(const Complex& val);
166// </group>
167
168// <summary>Complex comparisons </summary>
169// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
170// </reviewed>
171// <group name="Complex comparisons">
172//# On Linux comparing the norm does not work well in debug mode
173//# for equal values. Therefore they are compared for equality first.
174inline Bool operator>= (const Complex& left, const Complex& right)
175 { return left==right ? True : norm(left) >= norm(right); }
176inline Bool operator> (const Complex& left, const Complex& right)
177 { return left==right ? False : norm(left) > norm(right); }
178inline Bool operator<= (const Complex& left, const Complex& right)
179 { return left==right ? True : norm(left) <= norm(right); }
180inline Bool operator< (const Complex& left, const Complex& right)
181 { return left==right ? False : norm(left) < norm(right); }
182// </group>
183
184
185// <summary>DComplex NaN and Infinity</summary>
186// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
187// </reviewed>
188// <group name="DComplex NaN and Infinity">
189Bool isNaN (const DComplex& val);
190void setNaN(DComplex& val);
191Bool isInf (const DComplex& val);
192void setInf(DComplex& val);
193Bool isFinite(const DComplex& val);
194// </group>
195
196// <summary> DComplex comparisons </summary>
197// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
198// </reviewed>
199// <group name="DComplex comparisons">
200inline Bool operator>= (const DComplex& left, const DComplex& right)
201 { return norm(left) >= norm(right); }
202inline Bool operator> (const DComplex& left, const DComplex& right)
203 { return norm(left) > norm(right); }
204inline Bool operator<= (const DComplex& left, const DComplex& right)
205 { return norm(left) <= norm(right); }
206inline Bool operator< (const DComplex& left, const DComplex& right)
207 { return norm(left) < norm(right); }
208// </group>
209
210
211//# Global functions
212// <summary> Additional complex mathematical functions </summary>
213// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
214// </reviewed>
215// <group name=math>
216inline Double fabs(const DComplex &val) { return std::abs(val); }
217inline Float fabs(const Complex &val) { return std::abs(val); }
219inline DComplex square(const DComplex &val) { return val*val; }
220inline Complex square(const Complex &val) { return val*val; }
221
222inline DComplex cube(const DComplex &val) { return val*val*val; }
223inline Complex cube(const Complex &val) { return val*val*val; }
224
225// ArrayMath::pow needs this pow function.
227
228// We have to explicitly implement these for different type operands
229inline DComplex operator+(const DComplex& d, const Complex& c) {
230 return (DComplex)c + d;
231}
232
233inline DComplex operator+(const Complex& c, const DComplex& d) {
234 return (DComplex)c + d;
235}
236
237inline DComplex operator-(const DComplex& d, const Complex& c) {
238 return d - (DComplex)c;
239}
240
241inline DComplex operator-(const Complex& c, const DComplex& d) {
242 return (DComplex)c - d;
243}
244
245// QMath and scimath need these operators * and /
246// <group>
247inline Complex operator*(const Complex& val, Double f) { return val*Float(f); }
248inline Complex operator*(Double f, const Complex& val) { return val*Float(f); }
249inline Complex operator/(const Complex& val, Double f) { return val/Float(f); }
250inline Complex operator/(Double f, const Complex& val) { return Float(f)/val; }
251// </group>
252// These operators are useful, otherwise both Float and Double are applicable
253// for Ints.
254// <group>
255inline Complex operator*(const Complex& val, Int f) { return val*Float(f); }
256inline Complex operator*(Int f, const Complex& val) { return val*Float(f); }
257inline Complex operator/(const Complex& val, Int f) { return val/Float(f); }
258inline Complex operator/(Int f, const Complex& val) { return Float(f)/val; }
259// </group>
260// </group>
261
262// <summary> The near functions </summary>
263// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
264// </reviewed>
265// <group name=near>
266Bool near(const Complex &val1, const Complex &val2, Double tol=1.0e-5);
267Bool near(const DComplex &val1, const DComplex &val2, Double tol=1.0e-13);
268Bool nearAbs(const Complex &val1, const Complex &val2, Double tol=1.0e-5);
269Bool nearAbs(const DComplex &val1, const DComplex &val2, Double tol=1.0e-13);
270inline Bool allNear(const Complex &val1, const Complex &val2,
271 Double tol=1.0e-5)
272 { return near(val1, val2, tol); }
273inline Bool allNear(const DComplex &val1, const DComplex &val2,
274 Double tol=1.0e-13)
275 { return near(val1, val2, tol); }
276inline Bool allNearAbs(const Complex &val1, const Complex &val2,
277 Double tol=1.0e-5)
278 { return nearAbs(val1, val2, tol); }
279inline Bool allNearAbs(const DComplex &val1, const DComplex &val2,
280 Double tol=1.0e-13)
281 { return nearAbs(val1, val2, tol); }
282// </group>
283
284// <summary> Max and min, floor and ceil functions </summary>
285// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
286// </reviewed>
287// <group name=maxmin>
288inline Complex max(const Complex &x, const Complex &y)
289 { return x >= y ? x : y; }
290inline DComplex max(const DComplex &x, const DComplex &y)
291 { return x >= y ? x : y; }
292
293inline Complex min(const Complex &x, const Complex &y)
294 { return x <= y ? x : y; }
295inline DComplex min(const DComplex &x, const DComplex &y)
296 { return x <= y ? x : y; }
297
298inline Complex floor(const Complex &x) {
299 return Complex(std::floor(x.real()), std::floor(x.imag())); }
300inline DComplex floor(const DComplex &x) {
301 return DComplex(std::floor(x.real()), std::floor(x.imag())); }
302
303inline Complex ceil(const Complex &x) {
304 return Complex(std::ceil(x.real()), std::ceil(x.imag())); }
305inline DComplex ceil(const DComplex &x) {
306 return DComplex(std::ceil(x.real()), std::ceil(x.imag())); }
307// </group>
308
309// <summary> fmod </summary>
310// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
311// </reviewed>
312// <group name=fmod>
313DComplex fmod(const DComplex &in, const DComplex &f);
314Complex fmod(const Complex &in, const Complex &f);
315// </group>
316
317// <summary> Inverse trigonometry </summary>
318// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
319// </reviewed>
320// <group name=inverse>
321// atan not valid for z == -1
322DComplex atan(const DComplex &in);
323Complex atan(const Complex &in);
324DComplex asin(const DComplex &in);
325Complex asin(const Complex &in);
326DComplex acos(const DComplex &in);
327Complex acos(const Complex &in);
328DComplex atan2(const DComplex &in, const DComplex &t2);
329Complex atan2(const Complex &in, const Complex &t2);
330// </group>
331
332// <summary> Error function </summary>
333// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
334// </reviewed>
335// <group name=erf>
336// Preliminary to get Functionals working. erf(z) will return erf(real(z))
337// only for now.
338DComplex erf(const DComplex &in);
339Complex erf(const Complex &in);
340DComplex erfc(const DComplex &in);
341Complex erfc(const Complex &in);
342// </group>
343
344// </group>
345
346} //# NAMESPACE CASACORE - END
347
348// Define real & complex conjugation for non-complex types
349// and put comparisons into std namespace.
350namespace std {
351 inline float conj(float x) { return x; }
352 inline double conj(double x) { return x; }
353 using casacore::operator>;
354 using casacore::operator>=;
355 using casacore::operator<;
356 using casacore::operator<=;
357}
358
359#endif
DComplex fmod(const DComplex &in, const DComplex &f)
fmod
Complex max(const Complex &x, const Complex &y)
Max and min, floor and ceil functions.
Definition Complex.h:289
Double fabs(const DComplex &val)
Additional complex mathematical functions.
Definition Complex.h:217
Bool isNaN(const DComplex &val)
DComplex NaN and Infinity.
Bool isNaN(const Complex &val)
Complex NaN and Infinity.
DComplex erf(const DComplex &in)
Error function.
DComplex atan(const DComplex &in)
Inverse trigonometry.
Bool near(const Complex &val1, const Complex &val2, Double tol=1.0e-5)
The near functions.
this file contains all the compiler specific defines
Definition mainpage.dox:28
const Bool False
Definition aipstype.h:42
LatticeExprNode isNaN(const LatticeExprNode &expr)
Test if a value is a NaN.
LatticeExprNode asin(const LatticeExprNode &expr)
LatticeExprNode fmod(const LatticeExprNode &left, const LatticeExprNode &right)
LatticeExprNode acos(const LatticeExprNode &expr)
LatticeExprNode max(const LatticeExprNode &left, const LatticeExprNode &right)
TableExprNode isFinite(const TableExprNode &node)
Function to test if a scalar or array is finite.
Definition ExprNode.h:1634
LatticeExprNode atan(const LatticeExprNode &expr)
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:52
TableExprNode nearAbs(const TableExprNode &left, const TableExprNode &right)
Definition ExprNode.h:1254
TableExprNode isInf(const TableExprNode &node)
Definition ExprNode.h:1630
T norm(const TableVector< T > &tv)
Definition TabVecMath.h:412
LatticeExprNode atan2(const LatticeExprNode &left, const LatticeExprNode &right)
Numerical 2-argument functions.
float Float
Definition aipstype.h:52
int Int
Definition aipstype.h:48
bool Bool
Define the standard types used by Casacore.
Definition aipstype.h:40
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:47
const Bool True
Definition aipstype.h:41
double Double
Definition aipstype.h:53
Bool near(const GaussianBeam &left, const GaussianBeam &other, const Double relWidthTol, const Quantity &absPaTol)
Define real & complex conjugation for non-complex types and put comparisons into std namespace.
Definition Complex.h:350
float conj(float x)
Definition Complex.h:351
Complex operator*(const Complex &val, Double f)
QMath and scimath need these operators * and /.
Definition Complex.h:247
DComplex operator+(const Complex &c, const DComplex &d)
Definition Complex.h:233
DComplex atan2(const DComplex &in, const DComplex &t2)
DComplex operator+(const DComplex &d, const Complex &c)
ArrayMath::pow needs this pow function.
Definition Complex.h:229
DComplex min(const DComplex &x, const DComplex &y)
Definition Complex.h:295
Complex operator/(Int f, const Complex &val)
Definition Complex.h:258
DComplex cube(const DComplex &val)
Definition Complex.h:222
Complex operator*(const Complex &val, Int f)
These operators are useful, otherwise both Float and Double are applicable for Ints.
Definition Complex.h:255
Complex min(const Complex &x, const Complex &y)
Definition Complex.h:293
Bool allNear(const Complex &val1, const Complex &val2, Double tol=1.0e-5)
Definition Complex.h:271
Bool nearAbs(const DComplex &val1, const DComplex &val2, Double tol=1.0e-13)
Complex operator/(const Complex &val, Double f)
Definition Complex.h:249
Complex fmod(const Complex &in, const Complex &f)
Complex atan2(const Complex &in, const Complex &t2)
DComplex operator-(const DComplex &d, const Complex &c)
Definition Complex.h:237
Bool allNearAbs(const DComplex &val1, const DComplex &val2, Double tol=1.0e-13)
Definition Complex.h:280
Bool nearAbs(const Complex &val1, const Complex &val2, Double tol=1.0e-5)
Bool allNear(const DComplex &val1, const DComplex &val2, Double tol=1.0e-13)
Definition Complex.h:274
DComplex operator-(const Complex &c, const DComplex &d)
Definition Complex.h:241
Complex operator*(Int f, const Complex &val)
Definition Complex.h:256
Complex operator/(Double f, const Complex &val)
Definition Complex.h:250
DComplex square(const DComplex &val)
Definition Complex.h:219
DComplex max(const DComplex &x, const DComplex &y)
Definition Complex.h:291
Bool allNearAbs(const Complex &val1, const Complex &val2, Double tol=1.0e-5)
Definition Complex.h:277
Complex operator/(const Complex &val, Int f)
Definition Complex.h:257
Bool near(const DComplex &val1, const DComplex &val2, Double tol=1.0e-13)
Complex operator*(Double f, const Complex &val)
Definition Complex.h:248