xref: /petsc/include/petscmath.h (revision 62cbcd01bf7444475d5b2b40f2df209719797777)
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;
18314da920SBarry Smith /*
19f4ccad53SBarry Smith 
20f4ccad53SBarry Smith      Defines operations that are different for complex and real numbers;
21f4ccad53SBarry Smith    note that one cannot really mix the use of complex and real in the same
22f4ccad53SBarry Smith    PETSc program. All PETSc objects in one program are built around the object
23ea709b57SSatish Balay    PetscScalar which is either always a double or a complex.
24f4ccad53SBarry Smith 
25e489efc1SBarry Smith */
26b36a9721SBarry Smith 
2759cb5930SBarry Smith #define PetscExpPassiveScalar(a) PetscExpScalar()
2859cb5930SBarry Smith 
29aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
30b7940d39SSatish Balay #if defined(PETSC_CLANGUAGE_CXX)
310bfd3fbfSBarry Smith /*
32b7940d39SSatish Balay    C++ support of complex numbers: Original support
330bfd3fbfSBarry Smith */
34df9b3741SSatish Balay #include <complex>
35adc17e78SSatish Balay 
36329f5518SBarry Smith #define PetscRealPart(a)      (a).real()
37329f5518SBarry Smith #define PetscImaginaryPart(a) (a).imag()
383f6de6efSSatish Balay #define PetscAbsScalar(a)     std::abs(a)
393f6de6efSSatish Balay #define PetscConj(a)          std::conj(a)
4018a7d68fSSatish Balay #define PetscSqrtScalar(a)    std::sqrt(a)
41184914b5SBarry Smith #define PetscPowScalar(a,b)   std::pow(a,b)
42184914b5SBarry Smith #define PetscExpScalar(a)     std::exp(a)
4306c1185fSBarry Smith #define PetscLogScalar(a)     std::log(a)
44184914b5SBarry Smith #define PetscSinScalar(a)     std::sin(a)
45184914b5SBarry Smith #define PetscCosScalar(a)     std::cos(a)
460bfd3fbfSBarry Smith 
4765460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
484a60b672SMatthew Knepley typedef std::complex<float> PetscScalar;
4965460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
504a60b672SMatthew Knepley typedef std::complex<long double> PetscScalar;
5165460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT)
524a60b672SMatthew Knepley typedef std::complex<int> PetscScalar;
534a60b672SMatthew Knepley #else
54ea709b57SSatish Balay typedef std::complex<double> PetscScalar;
554a60b672SMatthew Knepley #endif
56b7940d39SSatish Balay #else
57b7940d39SSatish Balay #include <complex.h>
58b7940d39SSatish Balay 
59b7940d39SSatish Balay /*
60b7940d39SSatish Balay    C support of complex numbers: Warning it needs a
6130c4ea65SBarry Smith    C99 compliant compiler to work...
62b7940d39SSatish Balay  */
63b7940d39SSatish Balay 
6465460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
6585b47369SMatthew Knepley typedef float complex PetscScalar;
6685b47369SMatthew Knepley 
6785b47369SMatthew Knepley #define PetscRealPart(a)      crealf(a)
6885b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagf(a)
6985b47369SMatthew Knepley #define PetscAbsScalar(a)     cabsf(a)
7085b47369SMatthew Knepley #define PetscConj(a)          conjf(a)
7185b47369SMatthew Knepley #define PetscSqrtScalar(a)    csqrtf(a)
7285b47369SMatthew Knepley #define PetscPowScalar(a,b)   cpowf(a,b)
7385b47369SMatthew Knepley #define PetscExpScalar(a)     cexpf(a)
7406c1185fSBarry Smith #define PetscLogScalar(a)     clogf(a)
7585b47369SMatthew Knepley #define PetscSinScalar(a)     csinf(a)
7685b47369SMatthew Knepley #define PetscCosScalar(a)     ccosf(a)
7765460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
7885b47369SMatthew Knepley typedef long double complex PetscScalar;
7985b47369SMatthew Knepley 
8085b47369SMatthew Knepley #define PetscRealPart(a)      creall(a)
8185b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagl(a)
8285b47369SMatthew Knepley #define PetscAbsScalar(a)     cabsl(a)
8385b47369SMatthew Knepley #define PetscConj(a)          conjl(a)
8485b47369SMatthew Knepley #define PetscSqrtScalar(a)    csqrtl(a)
8585b47369SMatthew Knepley #define PetscPowScalar(a,b)   cpowl(a,b)
8685b47369SMatthew Knepley #define PetscExpScalar(a)     cexpl(a)
8706c1185fSBarry Smith #define PetscLogScalar(a)     clogl(a)
8885b47369SMatthew Knepley #define PetscSinScalar(a)     csinl(a)
8985b47369SMatthew Knepley #define PetscCosScalar(a)     ccosl(a)
9085b47369SMatthew Knepley 
9185b47369SMatthew Knepley #else
9285b47369SMatthew Knepley typedef double complex PetscScalar;
9385b47369SMatthew Knepley 
94b7940d39SSatish Balay #define PetscRealPart(a)      creal(a)
95b7940d39SSatish Balay #define PetscImaginaryPart(a) cimag(a)
96b7940d39SSatish Balay #define PetscAbsScalar(a)     cabs(a)
97b7940d39SSatish Balay #define PetscConj(a)          conj(a)
98b7940d39SSatish Balay #define PetscSqrtScalar(a)    csqrt(a)
99b7940d39SSatish Balay #define PetscPowScalar(a,b)   cpow(a,b)
100b7940d39SSatish Balay #define PetscExpScalar(a)     cexp(a)
10106c1185fSBarry Smith #define PetscLogScalar(a)     clog(a)
102b7940d39SSatish Balay #define PetscSinScalar(a)     csin(a)
103b7940d39SSatish Balay #define PetscCosScalar(a)     ccos(a)
104b7940d39SSatish Balay #endif
1054a60b672SMatthew Knepley #endif
106e489efc1SBarry Smith 
1072c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
1087087cfbeSBarry Smith extern  MPI_Datatype  MPI_C_DOUBLE_COMPLEX;
1097087cfbeSBarry Smith extern  MPI_Datatype  MPI_C_COMPLEX;
1102c876bd9SBarry Smith #endif
1112c876bd9SBarry Smith 
112a83b8d76SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
113a83b8d76SBarry Smith #define MPIU_SCALAR         MPI_C_COMPLEX
114a83b8d76SBarry Smith #else
1152c876bd9SBarry Smith #define MPIU_SCALAR         MPI_C_DOUBLE_COMPLEX
116a83b8d76SBarry Smith #endif
11765460251SBarry Smith #if defined(PETSC_USE_SCALAR_MAT_SINGLE)
118762437b8SSatish Balay #define MPIU_MATSCALAR        ??Notdone
119762437b8SSatish Balay #else
1202c876bd9SBarry Smith #define MPIU_MATSCALAR      MPI_C_DOUBLE_COMPLEX
121762437b8SSatish Balay #endif
122762437b8SSatish Balay 
12375567043SBarry Smith 
124e489efc1SBarry Smith /* Compiling for real numbers only */
125e489efc1SBarry Smith #else
12665460251SBarry Smith #  if defined(PETSC_USE_SCALAR_SINGLE)
12787828ca2SBarry Smith #    define MPIU_SCALAR           MPI_FLOAT
12865460251SBarry Smith #  elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
129f68b968cSBarry Smith #    define MPIU_SCALAR           MPI_LONG_DOUBLE
13075567043SBarry Smith #  elif defined(PETSC_USE_SCALAR_INT)
13175567043SBarry Smith #    define MPIU_SCALAR           MPI_INT
13287828ca2SBarry Smith #  else
133e489efc1SBarry Smith #    define MPIU_SCALAR           MPI_DOUBLE
13487828ca2SBarry Smith #  endif
13565460251SBarry Smith #  if defined(PETSC_USE_SCALAR_MAT_SINGLE) || defined(PETSC_USE_SCALAR_SINGLE)
1363eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_FLOAT
13765460251SBarry Smith #  elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
138f68b968cSBarry Smith #    define MPIU_MATSCALAR        MPI_LONG_DOUBLE
13965460251SBarry Smith #  elif defined(PETSC_USE_SCALAR_INT)
14003c60df9SBarry Smith #    define MPIU_MATSCALAR        MPI_INT
1413eda8832SBarry Smith #  else
1423eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_DOUBLE
1433eda8832SBarry Smith #  endif
144329f5518SBarry Smith #  define PetscRealPart(a)      (a)
14575567043SBarry Smith #  define PetscImaginaryPart(a) (0.)
146e489efc1SBarry Smith #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
147e489efc1SBarry Smith #  define PetscConj(a)          (a)
14818a7d68fSSatish Balay #  define PetscSqrtScalar(a)    sqrt(a)
149184914b5SBarry Smith #  define PetscPowScalar(a,b)   pow(a,b)
150184914b5SBarry Smith #  define PetscExpScalar(a)     exp(a)
15106c1185fSBarry Smith #  define PetscLogScalar(a)     log(a)
152184914b5SBarry Smith #  define PetscSinScalar(a)     sin(a)
153184914b5SBarry Smith #  define PetscCosScalar(a)     cos(a)
154b0a32e0cSBarry Smith 
15565460251SBarry Smith #  if defined(PETSC_USE_SCALAR_SINGLE)
156ea709b57SSatish Balay   typedef float PetscScalar;
15765460251SBarry Smith #  elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
158f68b968cSBarry Smith   typedef long double PetscScalar;
15965460251SBarry Smith #  elif defined(PETSC_USE_SCALAR_INT)
16003c60df9SBarry Smith   typedef int PetscScalar;
161b0a32e0cSBarry Smith #  else
162ea709b57SSatish Balay   typedef double PetscScalar;
163b0a32e0cSBarry Smith #  endif
164e489efc1SBarry Smith #endif
165e489efc1SBarry Smith 
16665460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
167d7d1e502SBarry Smith #  define MPIU_REAL   MPI_FLOAT
16865460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
169f68b968cSBarry Smith #  define MPIU_REAL   MPI_LONG_DOUBLE
17065460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT)
17103c60df9SBarry Smith #  define MPIU_REAL   MPI_INT
172d7d1e502SBarry Smith #else
173d7d1e502SBarry Smith #  define MPIU_REAL   MPI_DOUBLE
174d7d1e502SBarry Smith #endif
175d7d1e502SBarry Smith 
176da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
17726aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
1783f1db9ecSBarry Smith /*
1793f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
1803f1db9ecSBarry Smith    single precision but all other objects still use double
1813f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
1823f1db9ecSBarry Smith    it remains double
1833f1db9ecSBarry Smith 
1843f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
1853f1db9ecSBarry Smith */
1863f1db9ecSBarry Smith 
18765460251SBarry Smith #if defined(PETSC_USE_SCALAR_MAT_SINGLE)
188b400db4cSSatish Balay typedef float MatScalar;
1893f1db9ecSBarry Smith #else
190ea709b57SSatish Balay typedef PetscScalar MatScalar;
19111380375SSatish Balay #endif
1923f1db9ecSBarry Smith 
19365460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
194b400db4cSSatish Balay   typedef float PetscReal;
19565460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
196f68b968cSBarry Smith   typedef long double PetscReal;
19765460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT)
19803c60df9SBarry Smith   typedef int PetscReal;
199329f5518SBarry Smith #else
200b400db4cSSatish Balay   typedef double PetscReal;
201329f5518SBarry Smith #endif
2023f1db9ecSBarry Smith 
203f68b968cSBarry Smith #if defined(PETSC_USE_COMPLEX)
204f68b968cSBarry Smith typedef PetscReal MatReal;
20565460251SBarry Smith #elif defined(PETSC_USE_SCALAR_MAT_SINGLE) || defined(PETSC_USE_SCALAR_SINGLE)
206f68b968cSBarry Smith typedef float MatReal;
207f68b968cSBarry Smith #else
208f68b968cSBarry Smith typedef PetscReal MatReal;
209f68b968cSBarry Smith #endif
210f68b968cSBarry Smith 
211f68b968cSBarry Smith 
212314da920SBarry Smith /* --------------------------------------------------------------------------*/
213314da920SBarry Smith 
214e489efc1SBarry Smith /*
215f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
216f22f69f0SBarry Smith    This is currently not used.
217e489efc1SBarry Smith */
218557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
219e489efc1SBarry Smith 
220e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
2217087cfbeSBarry Smith extern  PetscScalar  PETSC_i;
222e489efc1SBarry Smith 
223b6a5bde7SBarry Smith /*MC
224b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
225b6a5bde7SBarry Smith 
226eca87e8dSBarry Smith    Synopsis:
227eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
228eca87e8dSBarry Smith 
229eca87e8dSBarry Smith    Not Collective
230eca87e8dSBarry Smith 
231b6a5bde7SBarry Smith    Input Parameter:
232b6a5bde7SBarry Smith +  v1 - first value to find minimum of
233b6a5bde7SBarry Smith -  v2 - second value to find minimum of
234b6a5bde7SBarry Smith 
235b6a5bde7SBarry Smith 
236b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
237b6a5bde7SBarry Smith 
238b6a5bde7SBarry Smith    Level: beginner
239b6a5bde7SBarry Smith 
240b6a5bde7SBarry Smith 
241b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
242b6a5bde7SBarry Smith 
243b6a5bde7SBarry Smith M*/
244e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
245b6a5bde7SBarry Smith 
246b6a5bde7SBarry Smith /*MC
247b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
248b6a5bde7SBarry Smith 
249eca87e8dSBarry Smith    Synopsis:
250eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
251eca87e8dSBarry Smith 
252eca87e8dSBarry Smith    Not Collective
253eca87e8dSBarry Smith 
254b6a5bde7SBarry Smith    Input Parameter:
255b6a5bde7SBarry Smith +  v1 - first value to find maximum of
256b6a5bde7SBarry Smith -  v2 - second value to find maximum of
257b6a5bde7SBarry Smith 
258b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
259b6a5bde7SBarry Smith 
260b6a5bde7SBarry Smith    Level: beginner
261b6a5bde7SBarry Smith 
262b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
263b6a5bde7SBarry Smith 
264b6a5bde7SBarry Smith M*/
265e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
266b6a5bde7SBarry Smith 
267b6a5bde7SBarry Smith /*MC
268b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
269b6a5bde7SBarry Smith 
270b6a5bde7SBarry Smith    Synopsis:
271b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
272b6a5bde7SBarry Smith 
273eca87e8dSBarry Smith    Not Collective
274eca87e8dSBarry Smith 
275eca87e8dSBarry Smith    Input Parameter:
276eca87e8dSBarry Smith .   v1 - the integer
277b6a5bde7SBarry Smith 
278b6a5bde7SBarry Smith    Level: beginner
279b6a5bde7SBarry Smith 
280b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
281b6a5bde7SBarry Smith 
282b6a5bde7SBarry Smith M*/
283e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
284b6a5bde7SBarry Smith 
285b6a5bde7SBarry Smith /*MC
286b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
287b6a5bde7SBarry Smith 
288eca87e8dSBarry Smith    Synopsis:
289eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
290eca87e8dSBarry Smith 
291eca87e8dSBarry Smith    Not Collective
292eca87e8dSBarry Smith 
293b6a5bde7SBarry Smith    Input Parameter:
294b6a5bde7SBarry Smith .   v1 - the double
295b6a5bde7SBarry Smith 
296b6a5bde7SBarry Smith 
297b6a5bde7SBarry Smith    Level: beginner
298b6a5bde7SBarry Smith 
299b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
300b6a5bde7SBarry Smith 
301b6a5bde7SBarry Smith M*/
302f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
303b6a5bde7SBarry Smith 
304b6a5bde7SBarry Smith /*MC
305b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
306b6a5bde7SBarry Smith 
307b6a5bde7SBarry Smith    Synopsis:
308b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
309b6a5bde7SBarry Smith 
310eca87e8dSBarry Smith    Not Collective
311eca87e8dSBarry Smith 
312eca87e8dSBarry Smith    Input Parameter:
313eca87e8dSBarry Smith .   v1 - the value
314eca87e8dSBarry Smith 
315b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
316b6a5bde7SBarry Smith 
317b6a5bde7SBarry Smith    Level: beginner
318b6a5bde7SBarry Smith 
319b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
320b6a5bde7SBarry Smith 
321b6a5bde7SBarry Smith M*/
3224ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
323e489efc1SBarry Smith 
324314da920SBarry Smith /* ----------------------------------------------------------------------------*/
325314da920SBarry Smith /*
32603c60df9SBarry Smith      Basic constants - These should be done much better
327314da920SBarry Smith */
328314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
329314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
33071fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
33171fd2e92SBarry Smith #define PETSC_MIN_INT            -2147483647
332e489efc1SBarry Smith 
33365460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
3347e032f8bSBarry Smith #  define PETSC_MAX                     1.e30
3357e032f8bSBarry Smith #  define PETSC_MIN                    -1.e30
336f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-7
337f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
338cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
33965460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT)
34003c60df9SBarry Smith #  define PETSC_MAX                     PETSC_MAX_INT
34103c60df9SBarry Smith #  define PETSC_MIN                     PETSC_MIN_INT
34203c60df9SBarry Smith #  define PETSC_MACHINE_EPSILON         1
34303c60df9SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1
34403c60df9SBarry Smith #  define PETSC_SMALL                   0
34582adfdadSBarry Smith #else
3467e032f8bSBarry Smith #  define PETSC_MAX                     1.e300
3477e032f8bSBarry Smith #  define PETSC_MIN                    -1.e300
348f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-14
349f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
350cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
35182adfdadSBarry Smith #endif
35282adfdadSBarry Smith 
3539cf09972SJed Brown #if defined PETSC_HAVE_ADIC
3549cf09972SJed Brown /* Use MPI_Allreduce when ADIC is not available. */
3557087cfbeSBarry Smith extern PetscErrorCode  PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*);
3567087cfbeSBarry Smith extern PetscErrorCode  PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*);
3577087cfbeSBarry Smith extern PetscErrorCode  PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*);
3589cf09972SJed Brown #endif
3593e523bebSBarry Smith 
3600763cb5fSBarry Smith /*MC
3610763cb5fSBarry Smith       PetscIsInfOrNan - Returns 1 if the input double has an infinity for Not-a-number (Nan) value, otherwise 0.
3623e523bebSBarry Smith 
3630763cb5fSBarry Smith     Input Parameter:
3640763cb5fSBarry Smith .     a - the double
3650763cb5fSBarry Smith 
3660763cb5fSBarry Smith 
3670763cb5fSBarry Smith      Notes: uses the C99 standard isinf() and isnan() on systems where they exist.
36883886165SBarry Smith       Otherwises uses ( (a - a) != 0.0), note that some optimizing compiles compile
3690763cb5fSBarry Smith       out this form, thus removing the check.
3700763cb5fSBarry Smith 
37183672c4dSSatish Balay      Level: beginner
37283672c4dSSatish Balay 
37383672c4dSSatish Balay M*/
3749a25a3ccSBarry Smith #if defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN)
375*62cbcd01SMatthew G Knepley /* I had to introduce these inline functions because the C++ <valarray> header invalidates isinf(), making it std::isinf() */
376*62cbcd01SMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) {
377*62cbcd01SMatthew G Knepley   return isinf(PetscAbsScalar(a)) || isnan(PetscAbsScalar(a));
378*62cbcd01SMatthew G Knepley }
379*62cbcd01SMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) {
380*62cbcd01SMatthew G Knepley   return isinf(a) || isnan(a);
381*62cbcd01SMatthew G Knepley }
38262b4c0b3SBarry Smith #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN)
383270b8587SSatish Balay #if defined(PETSC_HAVE_FLOAT_H)
384270b8587SSatish Balay #include "float.h"  /* windows defines _finite() in float.h */
385270b8587SSatish Balay #endif
386961faeafSBarry Smith #if defined(PETSC_HAVE_IEEEFP_H)
387961faeafSBarry Smith #include "ieeefp.h"  /* Solaris prototypes these here */
388961faeafSBarry Smith #endif
389f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) (!_finite(PetscAbsScalar(a)) || _isnan(PetscAbsScalar(a)))
390f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) (!_finite(a) || _isnan(a))
3919a25a3ccSBarry Smith #else
392f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) ((a - a) != 0.0)
393f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) ((a - a) != 0.0)
3949a25a3ccSBarry Smith #endif
3959a25a3ccSBarry Smith 
3969a25a3ccSBarry Smith 
397314da920SBarry Smith /* ----------------------------------------------------------------------------*/
398e489efc1SBarry Smith /*
399b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
400e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
401e489efc1SBarry Smith   timing etc.
402e489efc1SBarry Smith */
403b0a32e0cSBarry Smith typedef double PetscLogDouble;
404b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
405e489efc1SBarry Smith 
40687828ca2SBarry Smith #define PassiveReal   PetscReal
407ea709b57SSatish Balay #define PassiveScalar PetscScalar
408d3ecb3a7SBarry Smith 
409e9fa29b7SSatish Balay 
410e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
411e489efc1SBarry Smith #endif
412