casacore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 uInt64 max(uInt64 a, uInt64 b){ if (a>b) return a; else return b; }
165 inline uInt64 min(uInt64 a, uInt64 b){ if (a>b) return b; else return a; }
166 
167 inline Double max(Double a, Double b) { if (a > b) return a; else return b; }
168 inline Double min(Double a, Double b) { if (a > b) return b; else return a; }
169 inline Double max(Double a, Float b) { if (a > b) return a; else return b; }
170 inline Double min(Double a, Float b) { if (a > b) return b; else return a; }
171 inline Double max(Float a, Double b) { if (a > b) return a; else return b; }
172 inline Double min(Float a, Double b) { if (a > b) return b; else return a; }
173 
174 inline Float max(Float a, Float b) { if (a > b) return a; else return b; }
175 inline Float min(Float a, Float b) { if (a > b) return b; else return a; }
176 // </group>
177 
178 // Return the square of a value.
179 // <group>
180 inline Int square(Int val) {return val*val;}
181 inline Int64 square(Int64 val) {return val*val;}
182 inline Float square(Float val) {return val*val;}
183 inline Double square(Double val) {return val*val;}
184 // </group>
185 
186 // Return the cube of a value.
187 // <group>
188 inline Int cube(Int val) {return val*val*val;}
189 inline Int64 cube(Int64 val) {return val*val*val;}
190 inline Float cube(Float val) {return val*val*val;}
191 inline Double cube(Double val) {return val*val*val;}
192 // </group>
193 
194 // Return the sign of a value.
195 // <group>
196 inline Int sign(Int val) {return val<0 ? -1 : (val>0 ? 1:0);}
197 inline Int64 sign(Int64 val) {return val<0 ? -1 : (val>0 ? 1:0);}
198 inline Float sign(Float val) {return val<0 ? -1 : (val>0 ? 1:0);}
199 inline Double sign(Double val) {return val<0 ? -1 : (val>0 ? 1:0);}
200 // </group>
201 
202 // Return the floor modulo as used by Python (unlike C); divisor sign is used.
203 // Note that function fmod can be used for C behaviour; dividend sign is used.
204 // In Python: 5%3=2 -5%3=1 5%-3=-1 -5%-3=-2
205 // In C: 5%3=2 -5%3=-2 5%-3=2 -5%-3=-2
206 // <group>
207 inline Int floormod (Int x, Int y)
208 {
209  Int r = x%y;
210  if (r != 0 && (x<0) != (y<0)) r+=y;
211  return r;
212 }
213 inline Int64 floormod (Int64 x, Int64 y)
214 {
215  Int64 r = x%y;
216  if (r != 0 && (x<0) != (y<0)) r+=y;
217  return r;
218 }
219 inline Float floormod (Float x, Float y)
220 {
221  Float r = fmod(x,y);
222  if (r != 0 && (x<0) != (y<0)) r+=y;
223  return r;
224 }
226 {
227  Double r = fmod(x,y);
228  if (r != 0 && (x<0) != (y<0)) r+=y;
229  return r;
230 }
231 // </group>
232 
233 // Functions to return whether a value is "relatively" near another. Returns
234 // <src> tol > abs(val2 - val1)/max(abs(val1),(val2))</src>.
235 // If tol <= 0, returns val1 == val2. If either val is 0.0, take care of area
236 // around the minimum number that can be represented.
237 // <group>
238 Bool near(uInt val1, uInt val2, Double tol = 1.0e-5);
239 Bool near(Int val1, Int val2, Double tol = 1.0e-5);
240 Bool near(Float val1, Float val2, Double tol = 1.0e-5);
241 Bool near(Float val1, Double val2, Double tol = 1.0e-5);
242 Bool near(Double val1, Float val2, Double tol = 1.0e-5);
243 Bool near(Double val1, Double val2, Double tol = 1.0e-13);
244 // </group>
245 
246 // The "allNear" versions are aliases for the normal "near" versions. They
247 // exist to make template functions that work for both arrays and scalars
248 // easier to write. These functions should be moved to ArrayMath.h
249 // <group>
250 inline Bool allNear(uInt val1, uInt val2, Double tol = 1.0e-5)
251  { return near(val1, val2, tol); }
252 inline Bool allNear(Int val1, Int val2, Double tol = 1.0e-5)
253  { return near(val1, val2, tol); }
254 inline Bool allNear(Float val1, Double val2, Double tol = 1.0e-5)
255  { return near(val1, val2, tol); }
256 inline Bool allNear(Double val1, Float val2, Double tol = 1.0e-5)
257  { return near(val1, val2, tol); }
258 inline Bool allNear(Float val1, Float val2, Double tol = 1.0e-5)
259  { return near(val1, val2, tol); }
260 inline Bool allNear(Double val1, Double val2, Double tol = 1.0e-13)
261  { return near(val1, val2, tol); }
262 // </group>
263 
264 // Functions to return whether a value is "absolutely" near another. Returns
265 // <src> tol > abs(val2 - val1)</src>
266 // <group>
267 Bool nearAbs(uInt val1, uInt val2, Double tol = 1.0e-5);
268 Bool nearAbs(Int val1, Int val2, Double tol = 1.0e-5);
269 Bool nearAbs(Float val1, Float val2, Double tol = 1.0e-5);
270 Bool nearAbs(Float val1, Double val2, Double tol = 1.0e-5);
271 Bool nearAbs(Double val1, Float val2, Double tol = 1.0e-5);
272 Bool nearAbs(Double val1, Double val2, Double tol = 1.0e-13);
273 // </group>
274 
275 // The "allNearAbs" versions are aliases for the normal "nearAbs"
276 // versions. They exist to make template functions that work for both arrays
277 // and scalars easier to write. These functions should be in ArrayMath.h
278 // <group>
279 inline Bool allNearAbs(uInt val1, uInt val2, uInt tol = 1)
280  { return nearAbs(val1, val2, tol); }
281 inline Bool allNearAbs(Int val1, Int val2, Int tol = 1)
282  { return nearAbs(val1, val2, tol); }
283 inline Bool allNearAbs(Float val1, Float val2, Double tol = 1.0e-5)
284  { return nearAbs(val1, val2, tol); }
285 inline Bool allNearAbs(Float val1, Double val2, Double tol = 1.0e-5)
286  { return nearAbs(val1, val2, tol); }
287 inline Bool allNearAbs(Double val1, Float val2, Double tol = 1.0e-5)
288  { return nearAbs(val1, val2, tol); }
289 inline Bool allNearAbs(Double val1, Double val2, Double tol = 1.0e-13)
290  { return nearAbs(val1, val2, tol); }
291 // </group>
292 
293 
294 // Functions to test if a floating point number is finite.
295 // It is if it is NaN nor infinity.
296 // <group>
297 inline Bool isFinite (const Float& val)
298 {
299 #if defined(AIPS_DARWIN)
300  return std::isfinite(val);
301 #else
302  return finite(val);
303 #endif
304 }
305 inline Bool isFinite (const Double& val)
306 {
307 #if defined(AIPS_DARWIN)
308  return std::isfinite(val);
309 #else
310  return finite(val);
311 #endif
312 }
313 // </group>
314 
315 // Functions to test for IEEE NaN's. The Float variant uses an in-line
316 // Macro examining the bit pattern (for portability and efficiency). The
317 // Double version invokes the IEEE function isnan found in ieeefp.h or math.h
318 // <group>
319 inline Bool isNaN (const Float& val)
320 {
321  return (((*(Int *)&(val) & 0x7f800000) == 0x7f800000) &&
322  ((*(Int *)&(val) & 0x007fffff) != 0x00000000));
323 }
324 inline Bool isNaN(Double val)
325 {
326  return ( std::isnan(val) );
327 }
328 // </group>
329 
330 // Round a number to <src>ndigit</src> significant digits, usually used
331 // for formatting for printing.
332 // <br>A non-integer <src>ndigit=N+F<src>, with integer N and fraction F,
333 // is interpreted as follows.
334 // For <src>x = A*10^B</src>, where B is an integer, A is rounded to N digits
335 // if <src>A > 10^F</src>, otherwise N+1 digits.
336 // <br>For the default 2.5, a value of 32157 is rounded to 32000,
337 // while 22157 is rounded to 22200.
338 Double roundDouble(Double val, Double ndigit=2.5);
339 
340 // Functions that return IEEE NaN's. The specific NaN returned has all bits
341 // set. This is 'quiet' NaN, and because the sign bit is set it may be
342 // considered a negative number (but NaN's are not numbers!).
343 // <group>
344 Float floatNaN();
345 Double doubleNaN();
346 void setNaN(Float& val);
347 void setNaN(Double& val);
348 // </group>
349 
350 // Functions to test for IEEE Infinity's. Should work for positive or negative
351 // infinity.
352 // <group>
353 Bool isInf(Float val);
354 Bool isInf(Double val);
355 // </group>
356 
357 // Functions that return an IEEE Infinity, (positive infinity).
358 // <group>
359 Float floatInf();
360 Double doubleInf();
361 void setInf(Float& val);
362 void setInf(Double& val);
363 // </group>
364 // </group>
365 
366 
367 } //# NAMESPACE CASACORE - END
368 
369 #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:50
Bool allNear(uInt val1, uInt val2, Double tol=1.0e-5)
The &quot;allNear&quot; versions are aliases for the normal &quot;near&quot; versions.
Definition: Math.h:250
unsigned long long uInt64
Definition: aipsxtype.h:39
Int sign(Int val)
Return the sign of a value.
Definition: Math.h:196
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:258
Bool allNear(Int val1, Int val2, Double tol=1.0e-5)
Definition: Math.h:252
Bool isFinite(const Float &val)
Functions to test if a floating point number is finite.
Definition: Math.h:297
Bool allNear(Float val1, Double val2, Double tol=1.0e-5)
Definition: Math.h:254
Float pow(Float f1, Double f2)
Returns f1**f2.
Definition: Math.h:142
bool isnan(const std::complex< T > &val)
bool isfinite(const std::complex< T > &val)
Bool allNearAbs(Double val1, Float val2, Double tol=1.0e-5)
Definition: Math.h:287
Int cube(Int val)
Return the cube of a value.
Definition: Math.h:188
TableExprNode isInf(const TableExprNode &node)
Definition: ExprNode.h:1626
Bool allNear(Double val1, Double val2, Double tol=1.0e-13)
Definition: Math.h:260
double Double
Definition: aipstype.h:55
Int floormod(Int x, Int y)
Return the floor modulo as used by Python (unlike C); divisor sign is used.
Definition: Math.h:207
Bool allNearAbs(Int val1, Int val2, Int tol=1)
Definition: Math.h:281
Bool allNear(Double val1, Float val2, Double tol=1.0e-5)
Definition: Math.h:256
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:42
Bool allNearAbs(uInt val1, uInt val2, uInt tol=1)
The &quot;allNearAbs&quot; versions are aliases for the normal &quot;nearAbs&quot; versions.
Definition: Math.h:279
Int square(Int val)
Return the square of a value.
Definition: Math.h:180
float Float
Definition: aipstype.h:54
Bool isNaN(const Float &val)
Functions to test for IEEE NaN&#39;s.
Definition: Math.h:319
LatticeExprNode fmod(const LatticeExprNode &left, const LatticeExprNode &right)
Bool allNearAbs(Double val1, Double val2, Double tol=1.0e-13)
Definition: Math.h:289
const Double e
e and functions thereof:
Bool allNearAbs(Float val1, Float val2, Double tol=1.0e-5)
Definition: Math.h:283
TableExprNode nearAbs(const TableExprNode &left, const TableExprNode &right)
Definition: ExprNode.h:1250
LatticeExprNode pow(const LatticeExprNode &left, const LatticeExprNode &right)
Int ifloor(Float point)
Return the integer &quot;less than&quot; point (i.e.
Definition: Math.h:150
Bool allNearAbs(Float val1, Double val2, Double tol=1.0e-5)
Definition: Math.h:285
unsigned int uInt
Definition: aipstype.h:51