xref: /petsc/include/petscmath.h (revision 8f1a2a5e609bf3d0ec1c7e7bf09507165b68d274) !
1e489efc1SBarry Smith /*
2314da920SBarry Smith 
3314da920SBarry Smith       PETSc mathematics include file. Defines certain basic mathematical
4314da920SBarry Smith     constants and functions for working with single and double precision
5314da920SBarry Smith     floating point numbers as well as complex and integers.
6314da920SBarry Smith 
7d382aafbSBarry Smith     This file is included by petscsys.h and should not be used directly.
8e7029fe1SSatish Balay 
9e489efc1SBarry Smith */
10e489efc1SBarry Smith 
11488ecbafSBarry Smith #if !defined(__PETSCMATH_H)
12488ecbafSBarry Smith #define __PETSCMATH_H
130a5f7794SBarry Smith #include <math.h>
14e9fa29b7SSatish Balay PETSC_EXTERN_CXX_BEGIN
150a5f7794SBarry Smith 
167087cfbeSBarry Smith extern  MPI_Datatype  MPIU_2SCALAR;
177087cfbeSBarry Smith extern  MPI_Datatype  MPIU_2INT;
18c90a1750SBarry Smith 
19314da920SBarry Smith /*
20f4ccad53SBarry Smith 
21f4ccad53SBarry Smith      Defines operations that are different for complex and real numbers;
22f4ccad53SBarry Smith    note that one cannot really mix the use of complex and real in the same
23f4ccad53SBarry Smith    PETSc program. All PETSc objects in one program are built around the object
2498725619SBarry Smith    PetscScalar which is either always a real or a complex.
25f4ccad53SBarry Smith 
26e489efc1SBarry Smith */
27b36a9721SBarry Smith 
2859cb5930SBarry Smith #define PetscExpPassiveScalar(a) PetscExpScalar()
29c1d390e3SJed Brown #if defined(PETSC_USE_REAL_SINGLE)
30c1d390e3SJed Brown #define MPIU_REAL   MPI_FLOAT
31c1d390e3SJed Brown typedef float PetscReal;
32*8f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
33c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
34c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
35c1d390e3SJed Brown typedef double PetscReal;
36*8f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
37c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_LONG_DOUBLE)
38c1d390e3SJed Brown #define MPIU_REAL   MPI_LONG_DOUBLE
39c1d390e3SJed Brown typedef long double PetscReal;
40c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
41c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
42c1d390e3SJed Brown typedef __float128 PetscReal;
43*8f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
44c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
4559cb5930SBarry Smith 
461093a601SBarry Smith /*
471093a601SBarry Smith     Complex number definitions
481093a601SBarry Smith  */
49aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
50b7940d39SSatish Balay #if defined(PETSC_CLANGUAGE_CXX)
511093a601SBarry Smith /* C++ support of complex number */
52df9b3741SSatish Balay #include <complex>
53adc17e78SSatish Balay 
54329f5518SBarry Smith #define PetscRealPart(a)      (a).real()
55329f5518SBarry Smith #define PetscImaginaryPart(a) (a).imag()
563f6de6efSSatish Balay #define PetscAbsScalar(a)     std::abs(a)
573f6de6efSSatish Balay #define PetscConj(a)          std::conj(a)
5818a7d68fSSatish Balay #define PetscSqrtScalar(a)    std::sqrt(a)
59184914b5SBarry Smith #define PetscPowScalar(a,b)   std::pow(a,b)
60184914b5SBarry Smith #define PetscExpScalar(a)     std::exp(a)
6106c1185fSBarry Smith #define PetscLogScalar(a)     std::log(a)
62184914b5SBarry Smith #define PetscSinScalar(a)     std::sin(a)
63184914b5SBarry Smith #define PetscCosScalar(a)     std::cos(a)
640bfd3fbfSBarry Smith 
65ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
664a60b672SMatthew Knepley typedef std::complex<float> PetscScalar;
67ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
681093a601SBarry Smith typedef std::complex<double> PetscScalar;
69ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_LONG_DOUBLE)
704a60b672SMatthew Knepley typedef std::complex<long double> PetscScalar;
71ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
72b7940d39SSatish Balay 
731b65fc54SMatthew G Knepley #else /* PETSC_CLANGUAGE_CXX */
741093a601SBarry Smith /*  C support of complex numbers: Requires C99 compliant compiler*/
751093a601SBarry Smith #include <complex.h>
76b7940d39SSatish Balay 
77ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
7885b47369SMatthew Knepley typedef float complex PetscScalar;
7985b47369SMatthew Knepley 
8085b47369SMatthew Knepley #define PetscRealPart(a)      crealf(a)
8185b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagf(a)
8285b47369SMatthew Knepley #define PetscAbsScalar(a)     cabsf(a)
8385b47369SMatthew Knepley #define PetscConj(a)          conjf(a)
8485b47369SMatthew Knepley #define PetscSqrtScalar(a)    csqrtf(a)
8585b47369SMatthew Knepley #define PetscPowScalar(a,b)   cpowf(a,b)
8685b47369SMatthew Knepley #define PetscExpScalar(a)     cexpf(a)
8706c1185fSBarry Smith #define PetscLogScalar(a)     clogf(a)
8885b47369SMatthew Knepley #define PetscSinScalar(a)     csinf(a)
8985b47369SMatthew Knepley #define PetscCosScalar(a)     ccosf(a)
901093a601SBarry Smith 
91ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
921093a601SBarry Smith typedef double complex PetscScalar;
931093a601SBarry Smith 
941093a601SBarry Smith #define PetscRealPart(a)      creal(a)
951093a601SBarry Smith #define PetscImaginaryPart(a) cimag(a)
961093a601SBarry Smith #define PetscAbsScalar(a)     cabs(a)
971093a601SBarry Smith #define PetscConj(a)          conj(a)
981093a601SBarry Smith #define PetscSqrtScalar(a)    csqrt(a)
991093a601SBarry Smith #define PetscPowScalar(a,b)   cpow(a,b)
1001093a601SBarry Smith #define PetscExpScalar(a)     cexp(a)
1011093a601SBarry Smith #define PetscLogScalar(a)     clog(a)
1021093a601SBarry Smith #define PetscSinScalar(a)     csin(a)
1031093a601SBarry Smith #define PetscCosScalar(a)     ccos(a)
1041093a601SBarry Smith 
105ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_LONG_DOUBLE)
10685b47369SMatthew Knepley typedef long double complex PetscScalar;
10785b47369SMatthew Knepley 
10885b47369SMatthew Knepley #define PetscRealPart(a)      creall(a)
10985b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagl(a)
11085b47369SMatthew Knepley #define PetscAbsScalar(a)     cabsl(a)
11185b47369SMatthew Knepley #define PetscConj(a)          conjl(a)
11285b47369SMatthew Knepley #define PetscSqrtScalar(a)    csqrtl(a)
11385b47369SMatthew Knepley #define PetscPowScalar(a,b)   cpowl(a,b)
11485b47369SMatthew Knepley #define PetscExpScalar(a)     cexpl(a)
11506c1185fSBarry Smith #define PetscLogScalar(a)     clogl(a)
11685b47369SMatthew Knepley #define PetscSinScalar(a)     csinl(a)
11785b47369SMatthew Knepley #define PetscCosScalar(a)     ccosl(a)
11885b47369SMatthew Knepley 
119ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
1201b65fc54SMatthew G Knepley #endif /* PETSC_CLANGUAGE_CXX */
121e489efc1SBarry Smith 
1222c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
1237087cfbeSBarry Smith extern  MPI_Datatype  MPI_C_DOUBLE_COMPLEX;
1247087cfbeSBarry Smith extern  MPI_Datatype  MPI_C_COMPLEX;
1251b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
1262c876bd9SBarry Smith 
127ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
128a83b8d76SBarry Smith #define MPIU_SCALAR MPI_C_COMPLEX
129ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
1302c876bd9SBarry Smith #define MPIU_SCALAR MPI_C_DOUBLE_COMPLEX
131ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_LONG_DOUBLE)
1321093a601SBarry Smith #define MPIU_SCALAR error
133ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
13475567043SBarry Smith 
1351093a601SBarry Smith /*
1361093a601SBarry Smith     real number definitions
1371093a601SBarry Smith  */
1381b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
139ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
14087828ca2SBarry Smith #define MPIU_SCALAR           MPI_FLOAT
1411093a601SBarry Smith typedef float PetscScalar;
142ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
1431093a601SBarry Smith #define MPIU_SCALAR           MPI_DOUBLE
1441093a601SBarry Smith typedef double PetscScalar;
145ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_LONG_DOUBLE)
146f68b968cSBarry Smith #define MPIU_SCALAR           MPI_LONG_DOUBLE
1471093a601SBarry Smith typedef long double PetscScalar;
148ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
149c90a1750SBarry Smith extern MPI_Datatype MPIU___FLOAT128;
150c90a1750SBarry Smith #define MPIU_SCALAR MPIU___FLOAT128
1510d0cc1b5SBarry Smith typedef __float128 PetscScalar;
152ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
153329f5518SBarry Smith #define PetscRealPart(a)      (a)
154c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
155c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
156e489efc1SBarry Smith #define PetscConj(a)          (a)
157ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
15818a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
159184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
160184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
16106c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
162184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
163184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
164ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
1650d0cc1b5SBarry Smith #include <quadmath.h>
1660d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
1670d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
1680d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
1690d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
1700d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
1710d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
172ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
173b0a32e0cSBarry Smith 
1741b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
175e489efc1SBarry Smith 
176da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
17726aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
1783f1db9ecSBarry Smith 
179314da920SBarry Smith /* --------------------------------------------------------------------------*/
180314da920SBarry Smith 
181e489efc1SBarry Smith /*
182f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
183f22f69f0SBarry Smith    This is currently not used.
184e489efc1SBarry Smith */
185557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
186e489efc1SBarry Smith 
187e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
1887087cfbeSBarry Smith extern  PetscScalar  PETSC_i;
189e489efc1SBarry Smith 
190b6a5bde7SBarry Smith /*MC
191b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
192b6a5bde7SBarry Smith 
193eca87e8dSBarry Smith    Synopsis:
194eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
195eca87e8dSBarry Smith 
196eca87e8dSBarry Smith    Not Collective
197eca87e8dSBarry Smith 
198b6a5bde7SBarry Smith    Input Parameter:
199b6a5bde7SBarry Smith +  v1 - first value to find minimum of
200b6a5bde7SBarry Smith -  v2 - second value to find minimum of
201b6a5bde7SBarry Smith 
202b6a5bde7SBarry Smith 
203b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
204b6a5bde7SBarry Smith 
205b6a5bde7SBarry Smith    Level: beginner
206b6a5bde7SBarry Smith 
207b6a5bde7SBarry Smith 
208b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
209b6a5bde7SBarry Smith 
210b6a5bde7SBarry Smith M*/
211e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
212b6a5bde7SBarry Smith 
213b6a5bde7SBarry Smith /*MC
214b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
215b6a5bde7SBarry Smith 
216eca87e8dSBarry Smith    Synopsis:
217eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
218eca87e8dSBarry Smith 
219eca87e8dSBarry Smith    Not Collective
220eca87e8dSBarry Smith 
221b6a5bde7SBarry Smith    Input Parameter:
222b6a5bde7SBarry Smith +  v1 - first value to find maximum of
223b6a5bde7SBarry Smith -  v2 - second value to find maximum of
224b6a5bde7SBarry Smith 
225b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
226b6a5bde7SBarry Smith 
227b6a5bde7SBarry Smith    Level: beginner
228b6a5bde7SBarry Smith 
229b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
230b6a5bde7SBarry Smith 
231b6a5bde7SBarry Smith M*/
232e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
233b6a5bde7SBarry Smith 
234b6a5bde7SBarry Smith /*MC
235b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
236b6a5bde7SBarry Smith 
237b6a5bde7SBarry Smith    Synopsis:
238b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
239b6a5bde7SBarry Smith 
240eca87e8dSBarry Smith    Not Collective
241eca87e8dSBarry Smith 
242eca87e8dSBarry Smith    Input Parameter:
243eca87e8dSBarry Smith .   v1 - the integer
244b6a5bde7SBarry Smith 
245b6a5bde7SBarry Smith    Level: beginner
246b6a5bde7SBarry Smith 
247b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
248b6a5bde7SBarry Smith 
249b6a5bde7SBarry Smith M*/
250e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
251b6a5bde7SBarry Smith 
252b6a5bde7SBarry Smith /*MC
253b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
254b6a5bde7SBarry Smith 
255eca87e8dSBarry Smith    Synopsis:
256eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
257eca87e8dSBarry Smith 
258eca87e8dSBarry Smith    Not Collective
259eca87e8dSBarry Smith 
260b6a5bde7SBarry Smith    Input Parameter:
261b6a5bde7SBarry Smith .   v1 - the double
262b6a5bde7SBarry Smith 
263b6a5bde7SBarry Smith 
264b6a5bde7SBarry Smith    Level: beginner
265b6a5bde7SBarry Smith 
266b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
267b6a5bde7SBarry Smith 
268b6a5bde7SBarry Smith M*/
269f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
270b6a5bde7SBarry Smith 
271b6a5bde7SBarry Smith /*MC
272b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
273b6a5bde7SBarry Smith 
274b6a5bde7SBarry Smith    Synopsis:
275b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
276b6a5bde7SBarry Smith 
277eca87e8dSBarry Smith    Not Collective
278eca87e8dSBarry Smith 
279eca87e8dSBarry Smith    Input Parameter:
280eca87e8dSBarry Smith .   v1 - the value
281eca87e8dSBarry Smith 
282b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
283b6a5bde7SBarry Smith 
284b6a5bde7SBarry Smith    Level: beginner
285b6a5bde7SBarry Smith 
286b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
287b6a5bde7SBarry Smith 
288b6a5bde7SBarry Smith M*/
2894ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
290e489efc1SBarry Smith 
291314da920SBarry Smith /* ----------------------------------------------------------------------------*/
292314da920SBarry Smith /*
293d34fcf5fSBarry Smith      Basic constants
294314da920SBarry Smith */
295ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
296d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
297d34fcf5fSBarry Smith #elif defined(M_PI)
298d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
299d34fcf5fSBarry Smith #else
300314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
301d34fcf5fSBarry Smith #endif
302d34fcf5fSBarry Smith 
303d34fcf5fSBarry Smith 
30471fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
30571fd2e92SBarry Smith #define PETSC_MIN_INT            -2147483647
306e489efc1SBarry Smith 
307ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
308d34fcf5fSBarry Smith #if defined(MAXFLOAT)
309ea345e14SBarry Smith #  define PETSC_MAX_REAL                 MAXFLOAT
310d34fcf5fSBarry Smith #else
311ea345e14SBarry Smith #  define PETSC_MAX_REAL                1.e30
312d34fcf5fSBarry Smith #endif
313ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
314f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-7
315f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
316cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
317ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
318ea345e14SBarry Smith #  define PETSC_MAX_REAL                1.e300
319ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
320f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-14
321f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
322cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
323513dbe71SLisandro Dalcin #elif defined(PETSC_USE_REAL_LONG_DOUBLE)
324513dbe71SLisandro Dalcin #  define PETSC_MAX_REAL                1.e4900L
325513dbe71SLisandro Dalcin #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
326513dbe71SLisandro Dalcin #  define PETSC_MACHINE_EPSILON         1.e-18
327513dbe71SLisandro Dalcin #  define PETSC_SQRT_MACHINE_EPSILON    1.e-9
328513dbe71SLisandro Dalcin #  define PETSC_SMALL                   1.e-13
329ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
330ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
331ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
332d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
333d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
334d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
33582adfdadSBarry Smith #endif
33682adfdadSBarry Smith 
3379cf09972SJed Brown #if defined PETSC_HAVE_ADIC
3389cf09972SJed Brown /* Use MPI_Allreduce when ADIC is not available. */
3397087cfbeSBarry Smith extern PetscErrorCode  PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*);
3407087cfbeSBarry Smith extern PetscErrorCode  PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*);
3417087cfbeSBarry Smith extern PetscErrorCode  PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*);
3429cf09972SJed Brown #endif
3433e523bebSBarry Smith 
3440763cb5fSBarry Smith /*MC
3450763cb5fSBarry Smith       PetscIsInfOrNan - Returns 1 if the input double has an infinity for Not-a-number (Nan) value, otherwise 0.
3463e523bebSBarry Smith 
3470763cb5fSBarry Smith     Input Parameter:
3480763cb5fSBarry Smith .     a - the double
3490763cb5fSBarry Smith 
3500763cb5fSBarry Smith 
3510763cb5fSBarry Smith      Notes: uses the C99 standard isinf() and isnan() on systems where they exist.
35283886165SBarry Smith       Otherwises uses ( (a - a) != 0.0), note that some optimizing compiles compile
3530763cb5fSBarry Smith       out this form, thus removing the check.
3540763cb5fSBarry Smith 
35583672c4dSSatish Balay      Level: beginner
35683672c4dSSatish Balay 
35783672c4dSSatish Balay M*/
358ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
359a06653b4SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) {
360a06653b4SBarry Smith   return isinfq(PetscAbsScalar(a)) || isnanq(PetscAbsScalar(a));
361a06653b4SBarry Smith }
362a06653b4SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) {
363a06653b4SBarry Smith   return isinfq(a) || isnanq(a);
364a06653b4SBarry Smith }
365380a9c49SSatish Balay #elif defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN) && !defined(_GLIBCXX_CMATH)
36662cbcd01SMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) {
36762cbcd01SMatthew G Knepley   return isinf(PetscAbsScalar(a)) || isnan(PetscAbsScalar(a));
36862cbcd01SMatthew G Knepley }
36962cbcd01SMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) {
37062cbcd01SMatthew G Knepley   return isinf(a) || isnan(a);
37162cbcd01SMatthew G Knepley }
37262b4c0b3SBarry Smith #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN)
373270b8587SSatish Balay #if defined(PETSC_HAVE_FLOAT_H)
37498725619SBarry Smith #include "float.h"  /* Microsoft Windows defines _finite() in float.h */
375270b8587SSatish Balay #endif
376961faeafSBarry Smith #if defined(PETSC_HAVE_IEEEFP_H)
377961faeafSBarry Smith #include "ieeefp.h"  /* Solaris prototypes these here */
378961faeafSBarry Smith #endif
37998725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) {
38098725619SBarry Smith   return !_finite(PetscAbsScalar(a)) || _isnan(PetscAbsScalar(a));
38198725619SBarry Smith }
38298725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) {
38398725619SBarry Smith   return !_finite(a) || _isnan(a);
38498725619SBarry Smith }
3859a25a3ccSBarry Smith #else
38698725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) {
387d4a378daSJed Brown   return  ((a - a) != (PetscScalar)0);
38898725619SBarry Smith }
38998725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) {
390d4a378daSJed Brown   return ((a - a) != 0);
39198725619SBarry Smith }
3929a25a3ccSBarry Smith #endif
3939a25a3ccSBarry Smith 
3949a25a3ccSBarry Smith 
395314da920SBarry Smith /* ----------------------------------------------------------------------------*/
396e489efc1SBarry Smith /*
397b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
398e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
399e489efc1SBarry Smith   timing etc.
400e489efc1SBarry Smith */
401b0a32e0cSBarry Smith typedef double PetscLogDouble;
402b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
403e489efc1SBarry Smith 
40487828ca2SBarry Smith #define PassiveReal   PetscReal
405ea709b57SSatish Balay #define PassiveScalar PetscScalar
406d3ecb3a7SBarry Smith 
40798725619SBarry Smith /*
40898725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
40998725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
41098725619SBarry Smith  */
41198725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
41298725619SBarry Smith typedef PetscScalar MatScalar;
41398725619SBarry Smith typedef PetscReal MatReal;
41498725619SBarry Smith 
415e9fa29b7SSatish Balay 
416e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
417e489efc1SBarry Smith #endif
418