xref: /petsc/include/petscmath.h (revision a83b8d76eac3dccd5dadc86ea0ccb0f1aa5a9776)
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 
7e7029fe1SSatish Balay     This file is included by petsc.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 
16ff73aad6SKris Buschelman extern  MPI_Datatype PETSC_DLLEXPORT MPIU_2SCALAR;
17ff73aad6SKris Buschelman extern  MPI_Datatype PETSC_DLLEXPORT 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 
36*a83b8d76SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
37*a83b8d76SBarry Smith /*
38*a83b8d76SBarry Smith     For d double and c single complex defines the following operations
39*a83b8d76SBarry Smith        d == c
40*a83b8d76SBarry Smith        c == d
41*a83b8d76SBarry Smith        d != c
42*a83b8d76SBarry Smith        c != d
43*a83b8d76SBarry Smith        d / c
44*a83b8d76SBarry Smith        c /d
45*a83b8d76SBarry Smith        d * c
46*a83b8d76SBarry Smith        c * d
47*a83b8d76SBarry Smith        d - c
48*a83b8d76SBarry Smith        c - d
49*a83b8d76SBarry Smith        d + c
50*a83b8d76SBarry Smith        c + d
51*a83b8d76SBarry Smith */
52*a83b8d76SBarry Smith namespace std
53*a83b8d76SBarry Smith {
54*a83b8d76SBarry Smith   template<typename _Tp>
55*a83b8d76SBarry Smith     inline bool
56*a83b8d76SBarry Smith     operator==(const double& __x, const complex<_Tp>& __y)
57*a83b8d76SBarry Smith     { return __x == __y.real() && _Tp() == __y.imag(); }
58*a83b8d76SBarry Smith   template<typename _Tp>
59*a83b8d76SBarry Smith     inline bool
60*a83b8d76SBarry Smith     operator==(const complex<_Tp>& __x, const double& __y)
61*a83b8d76SBarry Smith     { return __x.real() == __y && __x.imag() == _Tp(); }
62*a83b8d76SBarry Smith   template<typename _Tp>
63*a83b8d76SBarry Smith     inline bool
64*a83b8d76SBarry Smith     operator!=(const complex<_Tp>& __x, const double& __y)
65*a83b8d76SBarry Smith     { return __x.real() != __y || __x.imag() != _Tp(); }
66*a83b8d76SBarry Smith   template<typename _Tp>
67*a83b8d76SBarry Smith     inline bool
68*a83b8d76SBarry Smith     operator!=(const double& __x, const complex<_Tp>& __y)
69*a83b8d76SBarry Smith     { return __x != __y.real() || _Tp() != __y.imag(); }
70*a83b8d76SBarry Smith   template<typename _Tp>
71*a83b8d76SBarry Smith     inline complex<_Tp>
72*a83b8d76SBarry Smith     operator/(const complex<_Tp>& __x, const double& __y)
73*a83b8d76SBarry Smith     {
74*a83b8d76SBarry Smith       complex<_Tp> __r = __x;
75*a83b8d76SBarry Smith       __r /= ((float)__y);
76*a83b8d76SBarry Smith       return __r;
77*a83b8d76SBarry Smith     }
78*a83b8d76SBarry Smith   template<typename _Tp>
79*a83b8d76SBarry Smith     inline complex<_Tp>
80*a83b8d76SBarry Smith     operator/(const double& __x, const complex<_Tp>& __y)
81*a83b8d76SBarry Smith     {
82*a83b8d76SBarry Smith       complex<_Tp> __r = (float)__x;
83*a83b8d76SBarry Smith       __r /= __y;
84*a83b8d76SBarry Smith       return __r;
85*a83b8d76SBarry Smith     }
86*a83b8d76SBarry Smith   template<typename _Tp>
87*a83b8d76SBarry Smith     inline complex<_Tp>
88*a83b8d76SBarry Smith     operator*(const complex<_Tp>& __x, const double& __y)
89*a83b8d76SBarry Smith     {
90*a83b8d76SBarry Smith       complex<_Tp> __r = __x;
91*a83b8d76SBarry Smith       __r *= ((float)__y);
92*a83b8d76SBarry Smith       return __r;
93*a83b8d76SBarry Smith     }
94*a83b8d76SBarry Smith   template<typename _Tp>
95*a83b8d76SBarry Smith     inline complex<_Tp>
96*a83b8d76SBarry Smith     operator*(const double& __x, const complex<_Tp>& __y)
97*a83b8d76SBarry Smith     {
98*a83b8d76SBarry Smith       complex<_Tp> __r = (float)__x;
99*a83b8d76SBarry Smith       __r *= __y;
100*a83b8d76SBarry Smith       return __r;
101*a83b8d76SBarry Smith     }
102*a83b8d76SBarry Smith   template<typename _Tp>
103*a83b8d76SBarry Smith     inline complex<_Tp>
104*a83b8d76SBarry Smith     operator-(const complex<_Tp>& __x, const double& __y)
105*a83b8d76SBarry Smith     {
106*a83b8d76SBarry Smith       complex<_Tp> __r = __x;
107*a83b8d76SBarry Smith       __r -= ((float)__y);
108*a83b8d76SBarry Smith       return __r;
109*a83b8d76SBarry Smith     }
110*a83b8d76SBarry Smith   template<typename _Tp>
111*a83b8d76SBarry Smith     inline complex<_Tp>
112*a83b8d76SBarry Smith     operator-(const double& __x, const complex<_Tp>& __y)
113*a83b8d76SBarry Smith     {
114*a83b8d76SBarry Smith       complex<_Tp> __r = (float)__x;
115*a83b8d76SBarry Smith       __r -= __y;
116*a83b8d76SBarry Smith       return __r;
117*a83b8d76SBarry Smith     }
118*a83b8d76SBarry Smith   template<typename _Tp>
119*a83b8d76SBarry Smith     inline complex<_Tp>
120*a83b8d76SBarry Smith     operator+(const complex<_Tp>& __x, const double& __y)
121*a83b8d76SBarry Smith     {
122*a83b8d76SBarry Smith       complex<_Tp> __r = __x;
123*a83b8d76SBarry Smith       __r += ((float)__y);
124*a83b8d76SBarry Smith       return __r;
125*a83b8d76SBarry Smith     }
126*a83b8d76SBarry Smith   template<typename _Tp>
127*a83b8d76SBarry Smith     inline complex<_Tp>
128*a83b8d76SBarry Smith     operator+(const double& __x, const complex<_Tp>& __y)
129*a83b8d76SBarry Smith     {
130*a83b8d76SBarry Smith       complex<_Tp> __r = (float)__x;
131*a83b8d76SBarry Smith       __r += __y;
132*a83b8d76SBarry Smith       return __r;
133*a83b8d76SBarry Smith     }
134*a83b8d76SBarry Smith }
135*a83b8d76SBarry Smith #endif
136*a83b8d76SBarry Smith 
137*a83b8d76SBarry Smith 
138*a83b8d76SBarry Smith 
139329f5518SBarry Smith #define PetscRealPart(a)      (a).real()
140329f5518SBarry Smith #define PetscImaginaryPart(a) (a).imag()
1413f6de6efSSatish Balay #define PetscAbsScalar(a)     std::abs(a)
1423f6de6efSSatish Balay #define PetscConj(a)          std::conj(a)
14318a7d68fSSatish Balay #define PetscSqrtScalar(a)    std::sqrt(a)
144184914b5SBarry Smith #define PetscPowScalar(a,b)   std::pow(a,b)
145184914b5SBarry Smith #define PetscExpScalar(a)     std::exp(a)
14606c1185fSBarry Smith #define PetscLogScalar(a)     std::log(a)
147184914b5SBarry Smith #define PetscSinScalar(a)     std::sin(a)
148184914b5SBarry Smith #define PetscCosScalar(a)     std::cos(a)
1490bfd3fbfSBarry Smith 
15065460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
1514a60b672SMatthew Knepley typedef std::complex<float> PetscScalar;
15265460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
1534a60b672SMatthew Knepley typedef std::complex<long double> PetscScalar;
15465460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT)
1554a60b672SMatthew Knepley typedef std::complex<int> PetscScalar;
1564a60b672SMatthew Knepley #else
157ea709b57SSatish Balay typedef std::complex<double> PetscScalar;
1584a60b672SMatthew Knepley #endif
159b7940d39SSatish Balay #else
160b7940d39SSatish Balay #include <complex.h>
161b7940d39SSatish Balay 
162b7940d39SSatish Balay /*
163b7940d39SSatish Balay    C support of complex numbers: Warning it needs a
164b7940d39SSatish Balay    C90 compliant compiler to work...
165b7940d39SSatish Balay  */
166b7940d39SSatish Balay 
16765460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
16885b47369SMatthew Knepley typedef float complex PetscScalar;
16985b47369SMatthew Knepley 
17085b47369SMatthew Knepley #define PetscRealPart(a)      crealf(a)
17185b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagf(a)
17285b47369SMatthew Knepley #define PetscAbsScalar(a)     cabsf(a)
17385b47369SMatthew Knepley #define PetscConj(a)          conjf(a)
17485b47369SMatthew Knepley #define PetscSqrtScalar(a)    csqrtf(a)
17585b47369SMatthew Knepley #define PetscPowScalar(a,b)   cpowf(a,b)
17685b47369SMatthew Knepley #define PetscExpScalar(a)     cexpf(a)
17706c1185fSBarry Smith #define PetscLogScalar(a)     clogf(a)
17885b47369SMatthew Knepley #define PetscSinScalar(a)     csinf(a)
17985b47369SMatthew Knepley #define PetscCosScalar(a)     ccosf(a)
18065460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
18185b47369SMatthew Knepley typedef long double complex PetscScalar;
18285b47369SMatthew Knepley 
18385b47369SMatthew Knepley #define PetscRealPart(a)      creall(a)
18485b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagl(a)
18585b47369SMatthew Knepley #define PetscAbsScalar(a)     cabsl(a)
18685b47369SMatthew Knepley #define PetscConj(a)          conjl(a)
18785b47369SMatthew Knepley #define PetscSqrtScalar(a)    csqrtl(a)
18885b47369SMatthew Knepley #define PetscPowScalar(a,b)   cpowl(a,b)
18985b47369SMatthew Knepley #define PetscExpScalar(a)     cexpl(a)
19006c1185fSBarry Smith #define PetscLogScalar(a)     clogl(a)
19185b47369SMatthew Knepley #define PetscSinScalar(a)     csinl(a)
19285b47369SMatthew Knepley #define PetscCosScalar(a)     ccosl(a)
19385b47369SMatthew Knepley 
19485b47369SMatthew Knepley #else
19585b47369SMatthew Knepley typedef double complex PetscScalar;
19685b47369SMatthew Knepley 
197b7940d39SSatish Balay #define PetscRealPart(a)      creal(a)
198b7940d39SSatish Balay #define PetscImaginaryPart(a) cimag(a)
199b7940d39SSatish Balay #define PetscAbsScalar(a)     cabs(a)
200b7940d39SSatish Balay #define PetscConj(a)          conj(a)
201b7940d39SSatish Balay #define PetscSqrtScalar(a)    csqrt(a)
202b7940d39SSatish Balay #define PetscPowScalar(a,b)   cpow(a,b)
203b7940d39SSatish Balay #define PetscExpScalar(a)     cexp(a)
20406c1185fSBarry Smith #define PetscLogScalar(a)     clog(a)
205b7940d39SSatish Balay #define PetscSinScalar(a)     csin(a)
206b7940d39SSatish Balay #define PetscCosScalar(a)     ccos(a)
207b7940d39SSatish Balay #endif
2084a60b672SMatthew Knepley #endif
209e489efc1SBarry Smith 
2102c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
2115b126d37SMatthew Knepley extern  MPI_Datatype PETSC_DLLEXPORT MPI_C_DOUBLE_COMPLEX;
212*a83b8d76SBarry Smith extern  MPI_Datatype PETSC_DLLEXPORT MPI_C_COMPLEX;
2132c876bd9SBarry Smith #endif
2142c876bd9SBarry Smith 
215*a83b8d76SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
216*a83b8d76SBarry Smith #define MPIU_SCALAR         MPI_C_COMPLEX
217*a83b8d76SBarry Smith #else
2182c876bd9SBarry Smith #define MPIU_SCALAR         MPI_C_DOUBLE_COMPLEX
219*a83b8d76SBarry Smith #endif
22065460251SBarry Smith #if defined(PETSC_USE_SCALAR_MAT_SINGLE)
221762437b8SSatish Balay #define MPIU_MATSCALAR        ??Notdone
222762437b8SSatish Balay #else
2232c876bd9SBarry Smith #define MPIU_MATSCALAR      MPI_C_DOUBLE_COMPLEX
224762437b8SSatish Balay #endif
225762437b8SSatish Balay 
22675567043SBarry Smith 
227e489efc1SBarry Smith /* Compiling for real numbers only */
228e489efc1SBarry Smith #else
22965460251SBarry Smith #  if defined(PETSC_USE_SCALAR_SINGLE)
23087828ca2SBarry Smith #    define MPIU_SCALAR           MPI_FLOAT
23165460251SBarry Smith #  elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
232f68b968cSBarry Smith #    define MPIU_SCALAR           MPI_LONG_DOUBLE
23375567043SBarry Smith #  elif defined(PETSC_USE_SCALAR_INT)
23475567043SBarry Smith #    define MPIU_SCALAR           MPI_INT
23575567043SBarry Smith #  elif defined(PETSC_USE_SCALAR_QD_DD)
23675567043SBarry Smith #    define MPIU_SCALAR           MPIU_QD_DD
23787828ca2SBarry Smith #  else
238e489efc1SBarry Smith #    define MPIU_SCALAR           MPI_DOUBLE
23987828ca2SBarry Smith #  endif
24065460251SBarry Smith #  if defined(PETSC_USE_SCALAR_MAT_SINGLE) || defined(PETSC_USE_SCALAR_SINGLE)
2413eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_FLOAT
24265460251SBarry Smith #  elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
243f68b968cSBarry Smith #    define MPIU_MATSCALAR        MPI_LONG_DOUBLE
24465460251SBarry Smith #  elif defined(PETSC_USE_SCALAR_INT)
24503c60df9SBarry Smith #    define MPIU_MATSCALAR        MPI_INT
24675567043SBarry Smith #  elif defined(PETSC_USE_SCALAR_QD_DD)
24775567043SBarry Smith #    define MPIU_MATSCALAR        MPIU_QD_DD
2483eda8832SBarry Smith #  else
2493eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_DOUBLE
2503eda8832SBarry Smith #  endif
251329f5518SBarry Smith #  define PetscRealPart(a)      (a)
25275567043SBarry Smith #  define PetscImaginaryPart(a) (0.)
253e489efc1SBarry Smith #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
254e489efc1SBarry Smith #  define PetscConj(a)          (a)
25518a7d68fSSatish Balay #  define PetscSqrtScalar(a)    sqrt(a)
256184914b5SBarry Smith #  define PetscPowScalar(a,b)   pow(a,b)
257184914b5SBarry Smith #  define PetscExpScalar(a)     exp(a)
25806c1185fSBarry Smith #  define PetscLogScalar(a)     log(a)
259184914b5SBarry Smith #  define PetscSinScalar(a)     sin(a)
260184914b5SBarry Smith #  define PetscCosScalar(a)     cos(a)
261b0a32e0cSBarry Smith 
26265460251SBarry Smith #  if defined(PETSC_USE_SCALAR_SINGLE)
263ea709b57SSatish Balay   typedef float PetscScalar;
26465460251SBarry Smith #  elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
265f68b968cSBarry Smith   typedef long double PetscScalar;
26665460251SBarry Smith #  elif defined(PETSC_USE_SCALAR_INT)
26703c60df9SBarry Smith   typedef int PetscScalar;
26875567043SBarry Smith #  elif defined(PETSC_USE_SCALAR_QD_DD)
26975567043SBarry Smith #  include "qd/dd_real.h"
27075567043SBarry Smith   typedef dd_real PetscScalar;
271b0a32e0cSBarry Smith #  else
272ea709b57SSatish Balay   typedef double PetscScalar;
273b0a32e0cSBarry Smith #  endif
274e489efc1SBarry Smith #endif
275e489efc1SBarry Smith 
27665460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
277d7d1e502SBarry Smith #  define MPIU_REAL   MPI_FLOAT
27865460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
279f68b968cSBarry Smith #  define MPIU_REAL   MPI_LONG_DOUBLE
28065460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT)
28103c60df9SBarry Smith #  define MPIU_REAL   MPI_INT
28275567043SBarry Smith #elif defined(PETSC_USE_SCALAR_QD_DD)
28375567043SBarry Smith #  define MPIU_REAL   MPIU_QD_DD
284d7d1e502SBarry Smith #else
285d7d1e502SBarry Smith #  define MPIU_REAL   MPI_DOUBLE
286d7d1e502SBarry Smith #endif
287d7d1e502SBarry Smith 
28875567043SBarry Smith #if defined(PETSC_USE_SCALAR_QD_DD)
28975567043SBarry Smith extern  MPI_Datatype PETSC_DLLEXPORT MPIU_QD_DD;
29075567043SBarry Smith #endif
29175567043SBarry Smith 
292da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
29326aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
2943f1db9ecSBarry Smith /*
2953f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
2963f1db9ecSBarry Smith    single precision but all other objects still use double
2973f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
2983f1db9ecSBarry Smith    it remains double
2993f1db9ecSBarry Smith 
3003f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
3013f1db9ecSBarry Smith */
3023f1db9ecSBarry Smith 
30365460251SBarry Smith #if defined(PETSC_USE_SCALAR_MAT_SINGLE)
304b400db4cSSatish Balay typedef float MatScalar;
3053f1db9ecSBarry Smith #else
306ea709b57SSatish Balay typedef PetscScalar MatScalar;
30711380375SSatish Balay #endif
3083f1db9ecSBarry Smith 
30965460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
310b400db4cSSatish Balay   typedef float PetscReal;
31165460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE)
312f68b968cSBarry Smith   typedef long double PetscReal;
31365460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT)
31403c60df9SBarry Smith   typedef int PetscReal;
31575567043SBarry Smith #elif defined(PETSC_USE_SCALAR_QD_DD)
31675567043SBarry Smith   typedef dd_real PetscReal;
317329f5518SBarry Smith #else
318b400db4cSSatish Balay   typedef double PetscReal;
319329f5518SBarry Smith #endif
3203f1db9ecSBarry Smith 
321f68b968cSBarry Smith #if defined(PETSC_USE_COMPLEX)
322f68b968cSBarry Smith typedef PetscReal MatReal;
32365460251SBarry Smith #elif defined(PETSC_USE_SCALAR_MAT_SINGLE) || defined(PETSC_USE_SCALAR_SINGLE)
324f68b968cSBarry Smith typedef float MatReal;
325f68b968cSBarry Smith #else
326f68b968cSBarry Smith typedef PetscReal MatReal;
327f68b968cSBarry Smith #endif
328f68b968cSBarry Smith 
329f68b968cSBarry Smith 
330314da920SBarry Smith /* --------------------------------------------------------------------------*/
331314da920SBarry Smith 
332e489efc1SBarry Smith /*
333e489efc1SBarry Smith    Certain objects may be created using either single
334e489efc1SBarry Smith   or double precision.
335e489efc1SBarry Smith */
33675567043SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE, PETSC_SCALAR_QD_DD } PetscScalarPrecision;
337e489efc1SBarry Smith 
338e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
339ff73aad6SKris Buschelman extern  PetscScalar PETSC_DLLEXPORT PETSC_i;
340e489efc1SBarry Smith 
341b6a5bde7SBarry Smith /*MC
342b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
343b6a5bde7SBarry Smith 
344b6a5bde7SBarry Smith    Input Parameter:
345b6a5bde7SBarry Smith +  v1 - first value to find minimum of
346b6a5bde7SBarry Smith -  v2 - second value to find minimum of
347b6a5bde7SBarry Smith 
348b6a5bde7SBarry Smith    Synopsis:
349b6a5bde7SBarry Smith    type PetscMin(type v1,type v2)
350b6a5bde7SBarry Smith 
351b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
352b6a5bde7SBarry Smith 
353b6a5bde7SBarry Smith    Level: beginner
354b6a5bde7SBarry Smith 
355b6a5bde7SBarry Smith 
356b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
357b6a5bde7SBarry Smith 
358b6a5bde7SBarry Smith M*/
359e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
360b6a5bde7SBarry Smith 
361b6a5bde7SBarry Smith /*MC
362b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
363b6a5bde7SBarry Smith 
364b6a5bde7SBarry Smith    Input Parameter:
365b6a5bde7SBarry Smith +  v1 - first value to find maximum of
366b6a5bde7SBarry Smith -  v2 - second value to find maximum of
367b6a5bde7SBarry Smith 
368b6a5bde7SBarry Smith    Synopsis:
369b6a5bde7SBarry Smith    type max PetscMax(type v1,type v2)
370b6a5bde7SBarry Smith 
371b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
372b6a5bde7SBarry Smith 
373b6a5bde7SBarry Smith    Level: beginner
374b6a5bde7SBarry Smith 
375b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
376b6a5bde7SBarry Smith 
377b6a5bde7SBarry Smith M*/
378e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
379b6a5bde7SBarry Smith 
380b6a5bde7SBarry Smith /*MC
381b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
382b6a5bde7SBarry Smith 
383b6a5bde7SBarry Smith    Input Parameter:
384b6a5bde7SBarry Smith .   v1 - the integer
385b6a5bde7SBarry Smith 
386b6a5bde7SBarry Smith    Synopsis:
387b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
388b6a5bde7SBarry Smith 
389b6a5bde7SBarry Smith 
390b6a5bde7SBarry Smith    Level: beginner
391b6a5bde7SBarry Smith 
392b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
393b6a5bde7SBarry Smith 
394b6a5bde7SBarry Smith M*/
395e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
396b6a5bde7SBarry Smith 
397b6a5bde7SBarry Smith /*MC
398b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
399b6a5bde7SBarry Smith 
400b6a5bde7SBarry Smith    Input Parameter:
401b6a5bde7SBarry Smith .   v1 - the double
402b6a5bde7SBarry Smith 
403b6a5bde7SBarry Smith    Synopsis:
404b6a5bde7SBarry Smith    int abs PetscAbsReal(PetscReal v1)
405b6a5bde7SBarry Smith 
406b6a5bde7SBarry Smith 
407b6a5bde7SBarry Smith    Level: beginner
408b6a5bde7SBarry Smith 
409b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
410b6a5bde7SBarry Smith 
411b6a5bde7SBarry Smith M*/
412f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
413b6a5bde7SBarry Smith 
414b6a5bde7SBarry Smith /*MC
415b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
416b6a5bde7SBarry Smith 
417b6a5bde7SBarry Smith    Input Parameter:
418b6a5bde7SBarry Smith .   v1 - the value
419b6a5bde7SBarry Smith 
420b6a5bde7SBarry Smith    Synopsis:
421b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
422b6a5bde7SBarry Smith 
423b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
424b6a5bde7SBarry Smith 
425b6a5bde7SBarry Smith    Level: beginner
426b6a5bde7SBarry Smith 
427b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
428b6a5bde7SBarry Smith 
429b6a5bde7SBarry Smith M*/
4304ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
431e489efc1SBarry Smith 
432314da920SBarry Smith /* ----------------------------------------------------------------------------*/
433314da920SBarry Smith /*
43403c60df9SBarry Smith      Basic constants - These should be done much better
435314da920SBarry Smith */
436314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
437314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
43871fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
43971fd2e92SBarry Smith #define PETSC_MIN_INT            -2147483647
440e489efc1SBarry Smith 
44165460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
4427e032f8bSBarry Smith #  define PETSC_MAX                     1.e30
4437e032f8bSBarry Smith #  define PETSC_MIN                    -1.e30
444f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-7
445f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
446cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
44765460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT)
44803c60df9SBarry Smith #  define PETSC_MAX                     PETSC_MAX_INT
44903c60df9SBarry Smith #  define PETSC_MIN                     PETSC_MIN_INT
45003c60df9SBarry Smith #  define PETSC_MACHINE_EPSILON         1
45103c60df9SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1
45203c60df9SBarry Smith #  define PETSC_SMALL                   0
45375567043SBarry Smith #elif defined(PETSC_USE_SCALAR_QD_DD)
45475567043SBarry Smith #  define PETSC_MAX                     1.e300
45575567043SBarry Smith #  define PETSC_MIN                    -1.e300
45675567043SBarry Smith #  define PETSC_MACHINE_EPSILON         1.e-30
45775567043SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.e-15
45875567043SBarry Smith #  define PETSC_SMALL                   1.e-25
45982adfdadSBarry Smith #else
4607e032f8bSBarry Smith #  define PETSC_MAX                     1.e300
4617e032f8bSBarry Smith #  define PETSC_MIN                    -1.e300
462f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-14
463f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
464cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
46582adfdadSBarry Smith #endif
46682adfdadSBarry Smith 
467ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
468ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
469ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);
4703e523bebSBarry Smith 
4710763cb5fSBarry Smith /*MC
4720763cb5fSBarry Smith       PetscIsInfOrNan - Returns 1 if the input double has an infinity for Not-a-number (Nan) value, otherwise 0.
4733e523bebSBarry Smith 
4740763cb5fSBarry Smith     Input Parameter:
4750763cb5fSBarry Smith .     a - the double
4760763cb5fSBarry Smith 
4770763cb5fSBarry Smith 
4780763cb5fSBarry Smith      Notes: uses the C99 standard isinf() and isnan() on systems where they exist.
47983886165SBarry Smith       Otherwises uses ( (a - a) != 0.0), note that some optimizing compiles compile
4800763cb5fSBarry Smith       out this form, thus removing the check.
4810763cb5fSBarry Smith 
48283672c4dSSatish Balay      Level: beginner
48383672c4dSSatish Balay 
48483672c4dSSatish Balay M*/
4859a25a3ccSBarry Smith #if defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN)
486f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) (isinf(PetscAbsScalar(a)) || isnan(PetscAbsScalar(a)))
487f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) (isinf(a) || isnan(a))
48862b4c0b3SBarry Smith #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN)
489270b8587SSatish Balay #if defined(PETSC_HAVE_FLOAT_H)
490270b8587SSatish Balay #include "float.h"  /* windows defines _finite() in float.h */
491270b8587SSatish Balay #endif
492961faeafSBarry Smith #if defined(PETSC_HAVE_IEEEFP_H)
493961faeafSBarry Smith #include "ieeefp.h"  /* Solaris prototypes these here */
494961faeafSBarry Smith #endif
495f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) (!_finite(PetscAbsScalar(a)) || _isnan(PetscAbsScalar(a)))
496f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) (!_finite(a) || _isnan(a))
4979a25a3ccSBarry Smith #else
498f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) ((a - a) != 0.0)
499f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) ((a - a) != 0.0)
5009a25a3ccSBarry Smith #endif
5019a25a3ccSBarry Smith 
5029a25a3ccSBarry Smith 
503314da920SBarry Smith /* ----------------------------------------------------------------------------*/
504e489efc1SBarry Smith /*
505b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
506e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
507e489efc1SBarry Smith   timing etc.
508e489efc1SBarry Smith */
509b0a32e0cSBarry Smith typedef double PetscLogDouble;
510b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
511e489efc1SBarry Smith 
51287828ca2SBarry Smith #define PassiveReal   PetscReal
513ea709b57SSatish Balay #define PassiveScalar PetscScalar
514d3ecb3a7SBarry Smith 
515e9fa29b7SSatish Balay 
516e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
517e489efc1SBarry Smith #endif
518