casacore
Loading...
Searching...
No Matches
Math.h
Go to the documentation of this file.
1// # Math.h: Casacore interface to <math.h> and other scalar math functions
2// # Copyright (C) 1993,1994,1995,1996,1997,1998,1999,2000,2001
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_MATH_H
27#define CASA_MATH_H
28
29#include <casacore/casa/aips.h>
30// # The following is to get abs(int) and (is)finite.
31#include <casacore/casa/math.h>
32#include <casacore/casa/stdlib.h>
33
34// On some systems the following is needed to get the finite function
35#if defined(AIPS_SOLARIS) || defined(AIPS_IRIX)
36#include <ieeefp.h>
37#endif
38
39namespace casacore { // # NAMESPACE CASACORE - BEGIN
40
41// <summary>
42// Casacore interface to math.h and other scalar math functions
43// </summary>
44// <reviewed reviewer="UNKNOWN" date="before2004/08/25" tests="" demos="">
45// </reviewed>
46
47// <synopsis>
48
49// Casacore interface to <src><math.h></src>. You should include this file
50// rather than <src><math.h></src> directly. It will be used to cover up any
51// deficiencies in the system <src><math.h></src>.
52
53// This file does not include things like element-by-element
54// array operations. See the
55// <linkto group="ArrayMath.h#Array mathematical operations">ArrayMath</linkto>
56// functions for these functions.
57
58// This file includes the standard math library. Hence besides the functions
59// defined here the following functions are also available.
60// <srcblock>
61// Double sin(Double x) Sine function
62// Double cos(Double x) Cosine function
63// Double tan(Double x) Tangent function
64// Double asin(Double x) Inverse sine function
65// Double acos(Double x) Inverse cosine function
66// Double atan(Double x) Inverse tangent function
67// Double atan2(Double y, Double x) Four quandrant inverse tangent function
68// Double hypot(Double y, Double x) Euclidean distance sqrt(x*x+y*y)
69
70// Double sinh(Double x) Hyperbolic sine
71// Double cosh(Double x) Hyperbolic cosine
72// Double tanh(Double x) Hyperbolic tangent
73// Double acosh(Double x) Inverse hyperbolic sine
74// Double asinh(Double x) Inverse hyperbolic cosine
75// Double atanh(Double x) Inverse hyperbolic tangent
76
77// Double sqrt(Double x) Square root
78// Double cbrt(Double x) Cube root
79
80// Double pow(Double x, Double y) x raised to the power of y
81// Double exp(Double x) Exponental function
82// Double expm1(Double x) exp(x)-1. Use when x is small.
83// Double log(Double x) Natural logarithm
84// Double log10(Double x) Base ten logarithm
85// Double log1p(Double x) log(x+1). Use when x is small
86
87// Double j0(Double x) Bessel function of the first kind, zeroth order
88// Double j1(Double x) Bessel function of the first kind, first order
89// Double jn(Int n, Double x) Bessel function of the first kind nth order
90// Double y0(Double x) Bessel function of the second kind, zeroth order
91// Double y1(Double x) Bessel function of the second kind, first order
92// Double yn(Int n, Double x) Bessel function of the second kind, nth order
93//
94// Double lgamma(Double x) Natural Log of the absolute value of the gamma
95// function
96// Double lgamma_r(Double x, Int* sign) Same as lgamma. The sign of the gamma
97// function is returned in the second argument.
98
99// Double erf(Double x) Error function
100// Double erfc(Double x) Complementary error function (1 - erf(x)).
101// Use for large x.
102
103// Double ceil(Double x) Returns the least integral value greater than or
104// equal to x
105// Double floor(Double x) Returns the least integral value than than or
106// equal to x
107// Double rint(Double x) Round to an integer using the current direction.
108
109// Double fabs(Double x) Absolute value of x
110// Double remainder(Double x, Double y) the remainder. x - y*Int(x/y)
111// Double fmod(Double x, Double y) As above. May differ by +/- y
112// Int isNaN(Double x) Returns 1 if x is a NaN, zero otherwise
113// Int ilogb(Double x) Unbiased exponent of x
114// Double logb(Double x) As above but returns floating point result
115// Double scalbn(Double x, Int n) x*2**n. Uses exponent manipulation.
116// Double scalb(Double x, Double n) x*2**n. As above but n is a Double
117// Double significand(Double x) Returns the fractional part of x
118// (between 1 and 2)
119// Double copysign(Double x, Double y) returns a value with the magnitude of
120// x and the sign bit of y.
121// Double nextafter(Double x, Double y) Returns the next machine representable
122// number after x in the direction specified by y
123// </srcblock>
124//
125
126// This file also includes the standard C library (stdlib.h). This is to obtain
127// a definition of the following functions.
128// <srcblock>
129// Int abs(Int x) absolute value function
130// </srcblock>
131// </synopsis>
132
133// <group name="Math interface for casacore">
135// Returns f1**f2. The Double precision version is defined in the standard
136// library. But many compilers are not good enough to automatically do the type
137// promotion. Hence these functions are explicitly defined.
138// <group>
139inline Float pow(Float f1, Double f2) { return Float(std::pow(Double(f1), f2)); }
140inline Float pow(Double f1, Float f2) { return Float(std::pow(f1, Double(f2))); }
141inline Int pow(Int f1, Int f2) { return Int(std::pow(Double(f1), Double(f2))); }
142// </group>
143
144// Return the integer "less than" point (i.e. the one further from zero if
145// "point" is negative.
146// <group>
147inline Int ifloor(Float point) {
148 if (point >= 0.0)
149 return Int(point);
150 else
151 return Int(point - 1.0);
152}
153inline Int ifloor(Double point) {
154 if (point >= 0.0)
155 return Int(point);
156 else
157 return Int(point - 1.0);
158}
159// </group>
160
161// Functions to get the max or min of two numbers.
162// <group>
163inline Int max(Int a, Int b) {
164 if (a > b)
165 return a;
166 else
167 return b;
168}
169inline Int min(Int a, Int b) {
170 if (a > b)
171 return b;
172 else
173 return a;
174}
175
176inline uInt max(uInt a, uInt b) {
177 if (a > b)
178 return a;
179 else
180 return b;
181}
182inline uInt min(uInt a, uInt b) {
183 if (a > b)
184 return b;
185 else
186 return a;
187}
188
189inline uInt64 max(uInt64 a, uInt64 b) {
190 if (a > b)
191 return a;
192 else
193 return b;
194}
195inline uInt64 min(uInt64 a, uInt64 b) {
196 if (a > b)
197 return b;
198 else
199 return a;
200}
201
202inline Double max(Double a, Double b) {
203 if (a > b)
204 return a;
205 else
206 return b;
207}
208inline Double min(Double a, Double b) {
209 if (a > b)
210 return b;
211 else
212 return a;
213}
214inline Double max(Double a, Float b) {
215 if (a > b)
216 return a;
217 else
218 return b;
219}
220inline Double min(Double a, Float b) {
221 if (a > b)
222 return b;
223 else
224 return a;
225}
226inline Double max(Float a, Double b) {
227 if (a > b)
228 return a;
229 else
230 return b;
231}
232inline Double min(Float a, Double b) {
233 if (a > b)
234 return b;
235 else
236 return a;
237}
238
239inline Float max(Float a, Float b) {
240 if (a > b)
241 return a;
242 else
243 return b;
244}
245inline Float min(Float a, Float b) {
246 if (a > b)
247 return b;
248 else
249 return a;
250}
251// </group>
252
253// Return the square of a value.
254// <group>
255inline Int square(Int val) { return val * val; }
256inline Int64 square(Int64 val) { return val * val; }
257inline Float square(Float val) { return val * val; }
258inline Double square(Double val) { return val * val; }
259// </group>
260
261// Return the cube of a value.
262// <group>
263inline Int cube(Int val) { return val * val * val; }
264inline Int64 cube(Int64 val) { return val * val * val; }
265inline Float cube(Float val) { return val * val * val; }
266inline Double cube(Double val) { return val * val * val; }
267// </group>
268
269// Return the sign of a value.
270// <group>
271inline Int sign(Int val) { return val < 0 ? -1 : (val > 0 ? 1 : 0); }
272inline Int64 sign(Int64 val) { return val < 0 ? -1 : (val > 0 ? 1 : 0); }
273inline Float sign(Float val) { return val < 0 ? -1 : (val > 0 ? 1 : 0); }
274inline Double sign(Double val) { return val < 0 ? -1 : (val > 0 ? 1 : 0); }
275// </group>
276
277// Return the floor modulo as used by Python (unlike C); divisor sign is used.
278// Note that function fmod can be used for C behaviour; dividend sign is used.
279// In Python: 5%3=2 -5%3=1 5%-3=-1 -5%-3=-2
280// In C: 5%3=2 -5%3=-2 5%-3=2 -5%-3=-2
281// <group>
282inline Int floormod(Int x, Int y) {
283 Int r = x % y;
284 if (r != 0 && (x < 0) != (y < 0)) r += y;
285 return r;
286}
287inline Int64 floormod(Int64 x, Int64 y) {
288 Int64 r = x % y;
289 if (r != 0 && (x < 0) != (y < 0)) r += y;
290 return r;
291}
292inline Float floormod(Float x, Float y) {
293 Float r = fmod(x, y);
294 if (r != 0 && (x < 0) != (y < 0)) r += y;
295 return r;
296}
298 Double r = fmod(x, y);
299 if (r != 0 && (x < 0) != (y < 0)) r += y;
300 return r;
301}
302// </group>
303
304// Functions to return whether a value is "relatively" near another. Returns
305// <src> tol > abs(val2 - val1)/max(abs(val1),(val2))</src>.
306// If tol <= 0, returns val1 == val2. If either val is 0.0, take care of area
307// around the minimum number that can be represented.
308// <group>
309Bool near(uInt val1, uInt val2, Double tol = 1.0e-5);
310Bool near(Int val1, Int val2, Double tol = 1.0e-5);
311Bool near(Float val1, Float val2, Double tol = 1.0e-5);
312Bool near(Float val1, Double val2, Double tol = 1.0e-5);
313Bool near(Double val1, Float val2, Double tol = 1.0e-5);
314Bool near(Double val1, Double val2, Double tol = 1.0e-13);
315// </group>
316
317// The "allNear" versions are aliases for the normal "near" versions. They
318// exist to make template functions that work for both arrays and scalars
319// easier to write. These functions should be moved to ArrayMath.h
320// <group>
321inline Bool allNear(uInt val1, uInt val2, Double tol = 1.0e-5) { return near(val1, val2, tol); }
322inline Bool allNear(Int val1, Int val2, Double tol = 1.0e-5) { return near(val1, val2, tol); }
323inline Bool allNear(Float val1, Double val2, Double tol = 1.0e-5) { return near(val1, val2, tol); }
324inline Bool allNear(Double val1, Float val2, Double tol = 1.0e-5) { return near(val1, val2, tol); }
325inline Bool allNear(Float val1, Float val2, Double tol = 1.0e-5) { return near(val1, val2, tol); }
326inline Bool allNear(Double val1, Double val2, Double tol = 1.0e-13) {
327 return near(val1, val2, tol);
328}
329// </group>
330
331// Functions to return whether a value is "absolutely" near another. Returns
332// <src> tol > abs(val2 - val1)</src>
333// <group>
334Bool nearAbs(uInt val1, uInt val2, Double tol = 1.0e-5);
335Bool nearAbs(Int val1, Int val2, Double tol = 1.0e-5);
336Bool nearAbs(Float val1, Float val2, Double tol = 1.0e-5);
337Bool nearAbs(Float val1, Double val2, Double tol = 1.0e-5);
338Bool nearAbs(Double val1, Float val2, Double tol = 1.0e-5);
339Bool nearAbs(Double val1, Double val2, Double tol = 1.0e-13);
340// </group>
341
342// The "allNearAbs" versions are aliases for the normal "nearAbs"
343// versions. They exist to make template functions that work for both arrays
344// and scalars easier to write. These functions should be in ArrayMath.h
345// <group>
346inline Bool allNearAbs(uInt val1, uInt val2, uInt tol = 1) { return nearAbs(val1, val2, tol); }
347inline Bool allNearAbs(Int val1, Int val2, Int tol = 1) { return nearAbs(val1, val2, tol); }
348inline Bool allNearAbs(Float val1, Float val2, Double tol = 1.0e-5) {
349 return nearAbs(val1, val2, tol);
350}
351inline Bool allNearAbs(Float val1, Double val2, Double tol = 1.0e-5) {
352 return nearAbs(val1, val2, tol);
353}
354inline Bool allNearAbs(Double val1, Float val2, Double tol = 1.0e-5) {
355 return nearAbs(val1, val2, tol);
356}
357inline Bool allNearAbs(Double val1, Double val2, Double tol = 1.0e-13) {
358 return nearAbs(val1, val2, tol);
359}
360// </group>
361
362// Functions to test if a floating point number is finite.
363// It is if it is NaN nor infinity.
364// <group>
365inline Bool isFinite(const Float& val) {
366#if defined(AIPS_DARWIN)
367 return std::isfinite(val);
368#else
369 return finite(val);
370#endif
371}
372inline Bool isFinite(const Double& val) {
373#if defined(AIPS_DARWIN)
374 return std::isfinite(val);
375#else
376 return finite(val);
377#endif
378}
379// </group>
380
381// Functions to test for IEEE NaN's. The Float variant uses an in-line
382// Macro examining the bit pattern (for portability and efficiency). The
383// Double version invokes the IEEE function isnan found in ieeefp.h or math.h
384// <group>
385inline Bool isNaN(const Float& val) {
386 return (((*(Int*)&(val) & 0x7f800000) == 0x7f800000) &&
387 ((*(Int*)&(val) & 0x007fffff) != 0x00000000));
388}
389inline Bool isNaN(Double val) { return (std::isnan(val)); }
390// </group>
391
392// Round a number to <src>ndigit</src> significant digits, usually used
393// for formatting for printing.
394// <br>A non-integer <src>ndigit=N+F<src>, with integer N and fraction F,
395// is interpreted as follows.
396// For <src>x = A*10^B</src>, where B is an integer, A is rounded to N digits
397// if <src>A > 10^F</src>, otherwise N+1 digits.
398// <br>For the default 2.5, a value of 32157 is rounded to 32000,
399// while 22157 is rounded to 22200.
400Double roundDouble(Double val, Double ndigit = 2.5);
401
402// Functions that return IEEE NaN's. The specific NaN returned has all bits
403// set. This is 'quiet' NaN, and because the sign bit is set it may be
404// considered a negative number (but NaN's are not numbers!).
405// <group>
408void setNaN(Float& val);
409void setNaN(Double& val);
410// </group>
411
412// Functions to test for IEEE Infinity's. Should work for positive or negative
413// infinity.
414// <group>
417// </group>
418
419// Functions that return an IEEE Infinity, (positive infinity).
420// <group>
423void setInf(Float& val);
424void setInf(Double& val);
425// </group>
426// </group>
427
428} // namespace casacore
429
430#endif
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
LatticeExprNode fmod(const LatticeExprNode &left, const LatticeExprNode &right)
TableExprNode nearAbs(const TableExprNode &left, const TableExprNode &right)
Definition ExprNode.h:1148
unsigned int uInt
Definition aipstype.h:49
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
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
double Double
Definition aipstype.h:53
Bool near(const GaussianBeam &left, const GaussianBeam &other, const Double relWidthTol, const Quantity &absPaTol)
unsigned long long uInt64
Definition aipsxtype.h:37
Bool nearAbs(Float val1, Double val2, Double tol=1.0e-5)
Bool nearAbs(uInt val1, uInt val2, Double tol=1.0e-5)
Functions to return whether a value is "absolutely" near another.
Bool nearAbs(Int val1, Int val2, Double tol=1.0e-5)
Bool nearAbs(Float val1, Float val2, Double tol=1.0e-5)
Bool nearAbs(Double val1, Double val2, Double tol=1.0e-13)
Float floatNaN()
Functions that return IEEE NaN's.
Bool allNear(Float val1, Float val2, Double tol=1.0e-5)
Definition Math.h:325
Bool allNearAbs(Double val1, Float val2, Double tol=1.0e-5)
Definition Math.h:354
Bool near(Double val1, Double val2, Double tol=1.0e-13)
Int cube(Int val)
Return the cube of a value.
Definition Math.h:263
Int max(Int a, Int b)
Functions to get the max or min of two numbers.
Definition Math.h:163
Bool isFinite(const Float &val)
Functions to test if a floating point number is finite.
Definition Math.h:365
Bool allNear(uInt val1, uInt val2, Double tol=1.0e-5)
The "allNear" versions are aliases for the normal "near" versions.
Definition Math.h:321
Int ifloor(Float point)
Return the integer "less than" point (i.e.
Definition Math.h:147
Bool near(Int val1, Int val2, Double tol=1.0e-5)
Bool allNear(Double val1, Float val2, Double tol=1.0e-5)
Definition Math.h:324
Bool near(Float val1, Double val2, Double tol=1.0e-5)
Bool near(Float val1, Float val2, Double tol=1.0e-5)
Bool allNearAbs(uInt val1, uInt val2, uInt tol=1)
The "allNearAbs" versions are aliases for the normal "nearAbs" versions.
Definition Math.h:346
Bool allNear(Int val1, Int val2, Double tol=1.0e-5)
Definition Math.h:322
Bool allNearAbs(Float val1, Float val2, Double tol=1.0e-5)
Definition Math.h:348
Bool allNearAbs(Double val1, Double val2, Double tol=1.0e-13)
Definition Math.h:357
Float floatInf()
Functions that return an IEEE Infinity, (positive infinity).
Bool near(Double val1, Float val2, Double tol=1.0e-5)
Bool isNaN(const Float &val)
Functions to test for IEEE NaN's.
Definition Math.h:385
Bool allNear(Float val1, Double val2, Double tol=1.0e-5)
Definition Math.h:323
Bool near(uInt val1, uInt val2, Double tol=1.0e-5)
Functions to return whether a value is "relatively" near another.
Int floormod(Int x, Int y)
Return the floor modulo as used by Python (unlike C); divisor sign is used.
Definition Math.h:282
Float pow(Float f1, Double f2)
Returns f1**f2.
Definition Math.h:139
Bool nearAbs(Double val1, Float val2, Double tol=1.0e-5)
Int square(Int val)
Return the square of a value.
Definition Math.h:255
Bool allNearAbs(Int val1, Int val2, Int tol=1)
Definition Math.h:347
Bool allNear(Double val1, Double val2, Double tol=1.0e-13)
Definition Math.h:326
Bool isInf(Float val)
Functions to test for IEEE Infinity's.
Bool allNearAbs(Float val1, Double val2, Double tol=1.0e-5)
Definition Math.h:351
Int sign(Int val)
Return the sign of a value.
Definition Math.h:271
Double roundDouble(Double val, Double ndigit=2.5)
Round a number to ndigit significant digits, usually used for formatting for printing.