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