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