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