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