xref: /petsc/include/petscmath.h (revision f22f69f01ec1ca0bb29797ff3752b65f1e1c47db)
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 
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 
36a83b8d76SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
37a83b8d76SBarry Smith /*
38a83b8d76SBarry Smith     For d double and c single complex defines the following operations
39a83b8d76SBarry Smith        d == c
40a83b8d76SBarry Smith        c == d
41a83b8d76SBarry Smith        d != c
42a83b8d76SBarry Smith        c != d
43a83b8d76SBarry Smith        d / c
44a83b8d76SBarry Smith        c /d
45a83b8d76SBarry Smith        d * c
46a83b8d76SBarry Smith        c * d
47a83b8d76SBarry Smith        d - c
48a83b8d76SBarry Smith        c - d
49a83b8d76SBarry Smith        d + c
50a83b8d76SBarry Smith        c + d
51a83b8d76SBarry Smith */
52a83b8d76SBarry Smith namespace std
53a83b8d76SBarry Smith {
54a83b8d76SBarry Smith   template<typename _Tp>
55a83b8d76SBarry Smith     inline bool
56a83b8d76SBarry Smith     operator==(const double& __x, const complex<_Tp>& __y)
57a83b8d76SBarry Smith     { return __x == __y.real() && _Tp() == __y.imag(); }
58a83b8d76SBarry Smith   template<typename _Tp>
59a83b8d76SBarry Smith     inline bool
60a83b8d76SBarry Smith     operator==(const complex<_Tp>& __x, const double& __y)
61a83b8d76SBarry Smith     { return __x.real() == __y && __x.imag() == _Tp(); }
62a83b8d76SBarry Smith   template<typename _Tp>
63a83b8d76SBarry Smith     inline bool
64a83b8d76SBarry Smith     operator!=(const complex<_Tp>& __x, const double& __y)
65a83b8d76SBarry Smith     { return __x.real() != __y || __x.imag() != _Tp(); }
66a83b8d76SBarry Smith   template<typename _Tp>
67a83b8d76SBarry Smith     inline bool
68a83b8d76SBarry Smith     operator!=(const double& __x, const complex<_Tp>& __y)
69a83b8d76SBarry Smith     { return __x != __y.real() || _Tp() != __y.imag(); }
70a83b8d76SBarry Smith   template<typename _Tp>
71a83b8d76SBarry Smith     inline complex<_Tp>
72a83b8d76SBarry Smith     operator/(const complex<_Tp>& __x, const double& __y)
73a83b8d76SBarry Smith     {
74a83b8d76SBarry Smith       complex<_Tp> __r = __x;
75a83b8d76SBarry Smith       __r /= ((float)__y);
76a83b8d76SBarry Smith       return __r;
77a83b8d76SBarry Smith     }
78a83b8d76SBarry Smith   template<typename _Tp>
79a83b8d76SBarry Smith     inline complex<_Tp>
80a83b8d76SBarry Smith     operator/(const double& __x, const complex<_Tp>& __y)
81a83b8d76SBarry Smith     {
82a83b8d76SBarry Smith       complex<_Tp> __r = (float)__x;
83a83b8d76SBarry Smith       __r /= __y;
84a83b8d76SBarry Smith       return __r;
85a83b8d76SBarry Smith     }
86a83b8d76SBarry Smith   template<typename _Tp>
87a83b8d76SBarry Smith     inline complex<_Tp>
88a83b8d76SBarry Smith     operator*(const complex<_Tp>& __x, const double& __y)
89a83b8d76SBarry Smith     {
90a83b8d76SBarry Smith       complex<_Tp> __r = __x;
91a83b8d76SBarry Smith       __r *= ((float)__y);
92a83b8d76SBarry Smith       return __r;
93a83b8d76SBarry Smith     }
94a83b8d76SBarry Smith   template<typename _Tp>
95a83b8d76SBarry Smith     inline complex<_Tp>
96a83b8d76SBarry Smith     operator*(const double& __x, const complex<_Tp>& __y)
97a83b8d76SBarry Smith     {
98a83b8d76SBarry Smith       complex<_Tp> __r = (float)__x;
99a83b8d76SBarry Smith       __r *= __y;
100a83b8d76SBarry Smith       return __r;
101a83b8d76SBarry Smith     }
102a83b8d76SBarry Smith   template<typename _Tp>
103a83b8d76SBarry Smith     inline complex<_Tp>
104a83b8d76SBarry Smith     operator-(const complex<_Tp>& __x, const double& __y)
105a83b8d76SBarry Smith     {
106a83b8d76SBarry Smith       complex<_Tp> __r = __x;
107a83b8d76SBarry Smith       __r -= ((float)__y);
108a83b8d76SBarry Smith       return __r;
109a83b8d76SBarry Smith     }
110a83b8d76SBarry Smith   template<typename _Tp>
111a83b8d76SBarry Smith     inline complex<_Tp>
112a83b8d76SBarry Smith     operator-(const double& __x, const complex<_Tp>& __y)
113a83b8d76SBarry Smith     {
114a83b8d76SBarry Smith       complex<_Tp> __r = (float)__x;
115a83b8d76SBarry Smith       __r -= __y;
116a83b8d76SBarry Smith       return __r;
117a83b8d76SBarry Smith     }
118a83b8d76SBarry Smith   template<typename _Tp>
119a83b8d76SBarry Smith     inline complex<_Tp>
120a83b8d76SBarry Smith     operator+(const complex<_Tp>& __x, const double& __y)
121a83b8d76SBarry Smith     {
122a83b8d76SBarry Smith       complex<_Tp> __r = __x;
123a83b8d76SBarry Smith       __r += ((float)__y);
124a83b8d76SBarry Smith       return __r;
125a83b8d76SBarry Smith     }
126a83b8d76SBarry Smith   template<typename _Tp>
127a83b8d76SBarry Smith     inline complex<_Tp>
128a83b8d76SBarry Smith     operator+(const double& __x, const complex<_Tp>& __y)
129a83b8d76SBarry Smith     {
130a83b8d76SBarry Smith       complex<_Tp> __r = (float)__x;
131a83b8d76SBarry Smith       __r += __y;
132a83b8d76SBarry Smith       return __r;
133a83b8d76SBarry Smith     }
134a83b8d76SBarry Smith }
135a83b8d76SBarry Smith #endif
136a83b8d76SBarry Smith 
137a83b8d76SBarry Smith 
138a83b8d76SBarry 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;
212a83b8d76SBarry Smith extern  MPI_Datatype PETSC_DLLEXPORT MPI_C_COMPLEX;
2132c876bd9SBarry Smith #endif
2142c876bd9SBarry Smith 
215a83b8d76SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
216a83b8d76SBarry Smith #define MPIU_SCALAR         MPI_C_COMPLEX
217a83b8d76SBarry Smith #else
2182c876bd9SBarry Smith #define MPIU_SCALAR         MPI_C_DOUBLE_COMPLEX
219a83b8d76SBarry 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 /*
333*f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
334*f22f69f0SBarry Smith    This is currently not used.
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 
344eca87e8dSBarry Smith    Synopsis:
345eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
346eca87e8dSBarry Smith 
347eca87e8dSBarry Smith    Not Collective
348eca87e8dSBarry Smith 
349b6a5bde7SBarry Smith    Input Parameter:
350b6a5bde7SBarry Smith +  v1 - first value to find minimum of
351b6a5bde7SBarry Smith -  v2 - second value to find minimum of
352b6a5bde7SBarry Smith 
353b6a5bde7SBarry Smith 
354b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
355b6a5bde7SBarry Smith 
356b6a5bde7SBarry Smith    Level: beginner
357b6a5bde7SBarry Smith 
358b6a5bde7SBarry Smith 
359b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
360b6a5bde7SBarry Smith 
361b6a5bde7SBarry Smith M*/
362e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
363b6a5bde7SBarry Smith 
364b6a5bde7SBarry Smith /*MC
365b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
366b6a5bde7SBarry Smith 
367eca87e8dSBarry Smith    Synopsis:
368eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
369eca87e8dSBarry Smith 
370eca87e8dSBarry Smith    Not Collective
371eca87e8dSBarry Smith 
372b6a5bde7SBarry Smith    Input Parameter:
373b6a5bde7SBarry Smith +  v1 - first value to find maximum of
374b6a5bde7SBarry Smith -  v2 - second value to find maximum of
375b6a5bde7SBarry Smith 
376b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
377b6a5bde7SBarry Smith 
378b6a5bde7SBarry Smith    Level: beginner
379b6a5bde7SBarry Smith 
380b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
381b6a5bde7SBarry Smith 
382b6a5bde7SBarry Smith M*/
383e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
384b6a5bde7SBarry Smith 
385b6a5bde7SBarry Smith /*MC
386b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
387b6a5bde7SBarry Smith 
388b6a5bde7SBarry Smith    Synopsis:
389b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
390b6a5bde7SBarry Smith 
391eca87e8dSBarry Smith    Not Collective
392eca87e8dSBarry Smith 
393eca87e8dSBarry Smith    Input Parameter:
394eca87e8dSBarry Smith .   v1 - the integer
395b6a5bde7SBarry Smith 
396b6a5bde7SBarry Smith    Level: beginner
397b6a5bde7SBarry Smith 
398b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
399b6a5bde7SBarry Smith 
400b6a5bde7SBarry Smith M*/
401e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
402b6a5bde7SBarry Smith 
403b6a5bde7SBarry Smith /*MC
404b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
405b6a5bde7SBarry Smith 
406eca87e8dSBarry Smith    Synopsis:
407eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
408eca87e8dSBarry Smith 
409eca87e8dSBarry Smith    Not Collective
410eca87e8dSBarry Smith 
411b6a5bde7SBarry Smith    Input Parameter:
412b6a5bde7SBarry Smith .   v1 - the double
413b6a5bde7SBarry Smith 
414b6a5bde7SBarry Smith 
415b6a5bde7SBarry Smith    Level: beginner
416b6a5bde7SBarry Smith 
417b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
418b6a5bde7SBarry Smith 
419b6a5bde7SBarry Smith M*/
420f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
421b6a5bde7SBarry Smith 
422b6a5bde7SBarry Smith /*MC
423b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
424b6a5bde7SBarry Smith 
425b6a5bde7SBarry Smith    Synopsis:
426b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
427b6a5bde7SBarry Smith 
428eca87e8dSBarry Smith    Not Collective
429eca87e8dSBarry Smith 
430eca87e8dSBarry Smith    Input Parameter:
431eca87e8dSBarry Smith .   v1 - the value
432eca87e8dSBarry Smith 
433b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
434b6a5bde7SBarry Smith 
435b6a5bde7SBarry Smith    Level: beginner
436b6a5bde7SBarry Smith 
437b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
438b6a5bde7SBarry Smith 
439b6a5bde7SBarry Smith M*/
4404ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
441e489efc1SBarry Smith 
442314da920SBarry Smith /* ----------------------------------------------------------------------------*/
443314da920SBarry Smith /*
44403c60df9SBarry Smith      Basic constants - These should be done much better
445314da920SBarry Smith */
446314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
447314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
44871fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
44971fd2e92SBarry Smith #define PETSC_MIN_INT            -2147483647
450e489efc1SBarry Smith 
45165460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE)
4527e032f8bSBarry Smith #  define PETSC_MAX                     1.e30
4537e032f8bSBarry Smith #  define PETSC_MIN                    -1.e30
454f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-7
455f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
456cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
45765460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT)
45803c60df9SBarry Smith #  define PETSC_MAX                     PETSC_MAX_INT
45903c60df9SBarry Smith #  define PETSC_MIN                     PETSC_MIN_INT
46003c60df9SBarry Smith #  define PETSC_MACHINE_EPSILON         1
46103c60df9SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1
46203c60df9SBarry Smith #  define PETSC_SMALL                   0
46375567043SBarry Smith #elif defined(PETSC_USE_SCALAR_QD_DD)
46475567043SBarry Smith #  define PETSC_MAX                     1.e300
46575567043SBarry Smith #  define PETSC_MIN                    -1.e300
46675567043SBarry Smith #  define PETSC_MACHINE_EPSILON         1.e-30
46775567043SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.e-15
46875567043SBarry Smith #  define PETSC_SMALL                   1.e-25
46982adfdadSBarry Smith #else
4707e032f8bSBarry Smith #  define PETSC_MAX                     1.e300
4717e032f8bSBarry Smith #  define PETSC_MIN                    -1.e300
472f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-14
473f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
474cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
47582adfdadSBarry Smith #endif
47682adfdadSBarry Smith 
477ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
478ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
479ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);
4803e523bebSBarry Smith 
4810763cb5fSBarry Smith /*MC
4820763cb5fSBarry Smith       PetscIsInfOrNan - Returns 1 if the input double has an infinity for Not-a-number (Nan) value, otherwise 0.
4833e523bebSBarry Smith 
4840763cb5fSBarry Smith     Input Parameter:
4850763cb5fSBarry Smith .     a - the double
4860763cb5fSBarry Smith 
4870763cb5fSBarry Smith 
4880763cb5fSBarry Smith      Notes: uses the C99 standard isinf() and isnan() on systems where they exist.
48983886165SBarry Smith       Otherwises uses ( (a - a) != 0.0), note that some optimizing compiles compile
4900763cb5fSBarry Smith       out this form, thus removing the check.
4910763cb5fSBarry Smith 
49283672c4dSSatish Balay      Level: beginner
49383672c4dSSatish Balay 
49483672c4dSSatish Balay M*/
4959a25a3ccSBarry Smith #if defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN)
496f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) (isinf(PetscAbsScalar(a)) || isnan(PetscAbsScalar(a)))
497f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) (isinf(a) || isnan(a))
49862b4c0b3SBarry Smith #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN)
499270b8587SSatish Balay #if defined(PETSC_HAVE_FLOAT_H)
500270b8587SSatish Balay #include "float.h"  /* windows defines _finite() in float.h */
501270b8587SSatish Balay #endif
502961faeafSBarry Smith #if defined(PETSC_HAVE_IEEEFP_H)
503961faeafSBarry Smith #include "ieeefp.h"  /* Solaris prototypes these here */
504961faeafSBarry Smith #endif
505f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) (!_finite(PetscAbsScalar(a)) || _isnan(PetscAbsScalar(a)))
506f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) (!_finite(a) || _isnan(a))
5079a25a3ccSBarry Smith #else
508f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) ((a - a) != 0.0)
509f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) ((a - a) != 0.0)
5109a25a3ccSBarry Smith #endif
5119a25a3ccSBarry Smith 
5129a25a3ccSBarry Smith 
513314da920SBarry Smith /* ----------------------------------------------------------------------------*/
514e489efc1SBarry Smith /*
515b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
516e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
517e489efc1SBarry Smith   timing etc.
518e489efc1SBarry Smith */
519b0a32e0cSBarry Smith typedef double PetscLogDouble;
520b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
521e489efc1SBarry Smith 
52287828ca2SBarry Smith #define PassiveReal   PetscReal
523ea709b57SSatish Balay #define PassiveScalar PetscScalar
524d3ecb3a7SBarry Smith 
525e9fa29b7SSatish Balay 
526e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
527e489efc1SBarry Smith #endif
528