xref: /petsc/include/petscmath.h (revision 77b4d14c11e1fd0016d0aca9d0ab7f8afe36e9da)
1e489efc1SBarry Smith /*
2314da920SBarry Smith 
3314da920SBarry Smith       PETSc mathematics include file. Defines certain basic mathematical
4a5057860SBarry Smith     constants and functions for working with single, double, and quad precision
5a5057860SBarry Smith     floating point numbers as well as complex single and double.
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>
140a5f7794SBarry Smith 
15314da920SBarry Smith /*
16f4ccad53SBarry Smith 
17f4ccad53SBarry Smith      Defines operations that are different for complex and real numbers;
18a5057860SBarry Smith    note that one cannot mix the use of complex and real in the same
19f4ccad53SBarry Smith    PETSc program. All PETSc objects in one program are built around the object
2098725619SBarry Smith    PetscScalar which is either always a real or a complex.
21f4ccad53SBarry Smith 
22e489efc1SBarry Smith */
23b36a9721SBarry Smith 
2459cb5930SBarry Smith #define PetscExpPassiveScalar(a) PetscExpScalar()
25c1d390e3SJed Brown #if defined(PETSC_USE_REAL_SINGLE)
26c1d390e3SJed Brown #define MPIU_REAL   MPI_FLOAT
27c1d390e3SJed Brown typedef float PetscReal;
289cf33046SSatish Balay #define PetscSqrtReal(a)    sqrt(a)
299a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
309a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
31*77b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
329a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
339a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
34*77b4d14cSPeter Brune #define PetscAcosReal(a)    acos(a)
35369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
36*77b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
37*77b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
38*77b4d14cSPeter Brune #define PetscFmodReal(a)    fmod(a)
39c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
40c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
41c1d390e3SJed Brown typedef double PetscReal;
428f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
439a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
449a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
45*77b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
469a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
479a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
48*77b4d14cSPeter Brune #define PetscAcosReal(a)    acos(a)
49369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
50*77b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
51*77b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
52*77b4d14cSPeter Brune #define PetscFmodReal(a)    fmod(a)
53c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
54574fde7bSSatish Balay #if defined(__cplusplus)
55574fde7bSSatish Balay extern "C" {
56574fde7bSSatish Balay #endif
57574fde7bSSatish Balay #include <quadmath.h>
58574fde7bSSatish Balay #if defined(__cplusplus)
59574fde7bSSatish Balay }
60574fde7bSSatish Balay #endif
618ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
62c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
63c1d390e3SJed Brown typedef __float128 PetscReal;
648f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
659a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
669a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
67*77b4d14cSPeter Brune #define PetscLog10Real(a)   log10q(a)
689a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
699a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
70*77b4d14cSPeter Brune #define PetscAcosReal(a)    acosq(a)
71369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
72*77b4d14cSPeter Brune #define PetscCeilReal(a)    ceilq(a)
73*77b4d14cSPeter Brune #define PetscFloorReal(a)   floorq(a)
74*77b4d14cSPeter Brune #define PetscFmodReal(a)    fmodq(a)
75c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
7659cb5930SBarry Smith 
771093a601SBarry Smith /*
781093a601SBarry Smith     Complex number definitions
791093a601SBarry Smith  */
8050f81f78SJed Brown #if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX)
819f20b660SSatish Balay #if defined(PETSC_USE_COMPLEX) || defined(PETSC_DESIRE_COMPLEX)
8250f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
831093a601SBarry Smith /* C++ support of complex number */
84debe9ee2SPaul Mullowney #if defined(PETSC_HAVE_CUSP)
85debe9ee2SPaul Mullowney #define complexlib cusp
869ae82921SPaul Mullowney #include <cusp/complex.h>
87debe9ee2SPaul Mullowney #else
88debe9ee2SPaul Mullowney #define complexlib std
89debe9ee2SPaul Mullowney #include <complex>
909ae82921SPaul Mullowney #endif
91b7940d39SSatish Balay 
9250f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
9350f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
9450f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
9550f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
9650f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
9750f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
9850f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
9950f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
10050f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
10150f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
102debe9ee2SPaul Mullowney 
103debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
10450f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
105debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
10650f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
1078c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
10850f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
109debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
1109f20b660SSatish Balay #endif  /* PETSC_USE_COMPLEX && PETSC_DESIRE_COMPLEX */
111debe9ee2SPaul Mullowney 
11250f81f78SJed Brown #elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
11350f81f78SJed Brown /* Use C99 _Complex for the type. Do not include complex.h by default to define "complex" because of symbol conflicts in Hypre. */
11450f81f78SJed Brown /* Compilation units that can safely use complex should define PETSC_DESIRE_COMPLEX before including any headers */
11550f81f78SJed Brown #if defined(PETSC_USE_COMPLEX) || defined(PETSC_DESIRE_COMPLEX)
1169f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
117519e2a1fSPaul Mullowney #include <complex.h>
118519e2a1fSPaul Mullowney 
119ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
12050f81f78SJed Brown typedef float _Complex PetscComplex;
12185b47369SMatthew Knepley 
12250f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
12350f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
12450f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
12550f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
12650f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
12750f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
12850f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
12950f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
13050f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
13150f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
1321093a601SBarry Smith 
133ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
13450f81f78SJed Brown typedef double _Complex PetscComplex;
1351093a601SBarry Smith 
13650f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
13750f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
13850f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
13950f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
14050f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
14150f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
14250f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
14350f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
14450f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
14550f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
1461093a601SBarry Smith 
1478c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
14850f81f78SJed Brown typedef __complex128 PetscComplex;
1498ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
1508c764dc5SJose Roman 
15150f81f78SJed Brown #define PetscRealPartComplex(a)      crealq(a)
15250f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagq(a)
15350f81f78SJed Brown #define PetscAbsComplex(a)           cabsq(a)
15450f81f78SJed Brown #define PetscConjComplex(a)          conjq(a)
15550f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtq(a)
15650f81f78SJed Brown #define PetscPowComplex(a,b)         cpowq(a,b)
15750f81f78SJed Brown #define PetscExpComplex(a)           cexpq(a)
15850f81f78SJed Brown #define PetscLogComplex(a)           clogq(a)
15950f81f78SJed Brown #define PetscSinComplex(a)           csinq(a)
16050f81f78SJed Brown #define PetscCosComplex(a)           ccosq(a)
161ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
16250f81f78SJed Brown #elif defined(PETSC_USE_COMPLEX)
16350f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
1649f20b660SSatish Balay #endif /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
1659f20b660SSatish Balay #endif /* (PETSC_CLANGUAGE_CXX && PETSC_HAVE_CXX_COMPLEX) else-if (PETSC_CLANGUAGE_C && PETSC_HAVE_C99_COMPLEX) */
166e489efc1SBarry Smith 
1678dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
16870da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
169500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
170500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
17170da9c3bSJed Brown #else
1728ad47952SJed Brown # if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX)
1738ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
1748ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
1758ad47952SJed Brown # elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
1768ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
1778ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
1788ad47952SJed Brown # else
1798ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
1808ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
1818ad47952SJed Brown # endif
1828ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
1838ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
1841b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
1858dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
1862c876bd9SBarry Smith 
1877c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
1887c2de775SJed Brown #  if defined(PETSC_USE_REAL_SINGLE)
1897c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
1907c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
1917c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
1927c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
1937c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
1947c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
1957c2de775SJed Brown #endif
1967c2de775SJed Brown 
19750f81f78SJed Brown #if defined(PETSC_USE_COMPLEX)
19850f81f78SJed Brown typedef PetscComplex PetscScalar;
19950f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
20050f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
20150f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
20250f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
20350f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
20450f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
20550f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
20650f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
20750f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
20850f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
20950f81f78SJed Brown 
2107c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
21175567043SBarry Smith 
2121093a601SBarry Smith /*
2131093a601SBarry Smith     real number definitions
2141093a601SBarry Smith  */
2151b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
2167c2de775SJed Brown typedef PetscReal PetscScalar;
2177c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
2187c2de775SJed Brown 
219329f5518SBarry Smith #define PetscRealPart(a)      (a)
220c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
221c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
222e489efc1SBarry Smith #define PetscConj(a)          (a)
223ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
22418a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
225184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
226184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
22706c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
228184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
229184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
230ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
2310d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
2320d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
2330d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
2340d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
2350d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
2360d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
237ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
238b0a32e0cSBarry Smith 
2391b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
240e489efc1SBarry Smith 
241da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
24226aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
2433f1db9ecSBarry Smith 
244314da920SBarry Smith /* --------------------------------------------------------------------------*/
245314da920SBarry Smith 
246e489efc1SBarry Smith /*
247f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
248f22f69f0SBarry Smith    This is currently not used.
249e489efc1SBarry Smith */
250557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
251e489efc1SBarry Smith 
25250f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
253e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
25450f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
25550f81f78SJed Brown #endif
256e489efc1SBarry Smith 
257b6a5bde7SBarry Smith /*MC
258b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
259b6a5bde7SBarry Smith 
260eca87e8dSBarry Smith    Synopsis:
261f2ba6396SBarry Smith    #include "petscmath.h"
262eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
263eca87e8dSBarry Smith 
264eca87e8dSBarry Smith    Not Collective
265eca87e8dSBarry Smith 
266b6a5bde7SBarry Smith    Input Parameter:
267b6a5bde7SBarry Smith +  v1 - first value to find minimum of
268b6a5bde7SBarry Smith -  v2 - second value to find minimum of
269b6a5bde7SBarry Smith 
270b6a5bde7SBarry Smith 
271b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
272b6a5bde7SBarry Smith 
273b6a5bde7SBarry Smith    Level: beginner
274b6a5bde7SBarry Smith 
275b6a5bde7SBarry Smith 
276d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
277b6a5bde7SBarry Smith 
278b6a5bde7SBarry Smith M*/
279e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
280b6a5bde7SBarry Smith 
281b6a5bde7SBarry Smith /*MC
282b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
283b6a5bde7SBarry Smith 
284eca87e8dSBarry Smith    Synopsis:
285a663daf8SBarry Smith    #include "petscmath.h"
286eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
287eca87e8dSBarry Smith 
288eca87e8dSBarry Smith    Not Collective
289eca87e8dSBarry Smith 
290b6a5bde7SBarry Smith    Input Parameter:
291b6a5bde7SBarry Smith +  v1 - first value to find maximum of
292b6a5bde7SBarry Smith -  v2 - second value to find maximum of
293b6a5bde7SBarry Smith 
294b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
295b6a5bde7SBarry Smith 
296b6a5bde7SBarry Smith    Level: beginner
297b6a5bde7SBarry Smith 
298d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
299b6a5bde7SBarry Smith 
300b6a5bde7SBarry Smith M*/
301e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
302b6a5bde7SBarry Smith 
303b6a5bde7SBarry Smith /*MC
304d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
305d9a4bb16SJed Brown 
306d9a4bb16SJed Brown    Synopsis:
307f2ba6396SBarry Smith    #include "petscmath.h"
308d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
309d9a4bb16SJed Brown 
310d9a4bb16SJed Brown    Not Collective
311d9a4bb16SJed Brown 
312d9a4bb16SJed Brown    Input Parameter:
313d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
314d9a4bb16SJed Brown .  a - lower end of interval
315d9a4bb16SJed Brown -  b - upper end of interval
316d9a4bb16SJed Brown 
317d9a4bb16SJed Brown    Notes: type can be integer or floating point value
318d9a4bb16SJed Brown 
319d9a4bb16SJed Brown    Level: beginner
320d9a4bb16SJed Brown 
321d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
322d9a4bb16SJed Brown 
323d9a4bb16SJed Brown M*/
324d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
325d9a4bb16SJed Brown 
326d9a4bb16SJed Brown /*MC
327b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
328b6a5bde7SBarry Smith 
329b6a5bde7SBarry Smith    Synopsis:
330f2ba6396SBarry Smith    #include "petscmath.h"
331b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
332b6a5bde7SBarry Smith 
333eca87e8dSBarry Smith    Not Collective
334eca87e8dSBarry Smith 
335eca87e8dSBarry Smith    Input Parameter:
336eca87e8dSBarry Smith .   v1 - the integer
337b6a5bde7SBarry Smith 
338b6a5bde7SBarry Smith    Level: beginner
339b6a5bde7SBarry Smith 
340b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
341b6a5bde7SBarry Smith 
342b6a5bde7SBarry Smith M*/
343e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
344b6a5bde7SBarry Smith 
345b6a5bde7SBarry Smith /*MC
346b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
347b6a5bde7SBarry Smith 
348eca87e8dSBarry Smith    Synopsis:
349f2ba6396SBarry Smith    #include "petscmath.h"
350eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
351eca87e8dSBarry Smith 
352eca87e8dSBarry Smith    Not Collective
353eca87e8dSBarry Smith 
354b6a5bde7SBarry Smith    Input Parameter:
355b6a5bde7SBarry Smith .   v1 - the double
356b6a5bde7SBarry Smith 
357b6a5bde7SBarry Smith 
358b6a5bde7SBarry Smith    Level: beginner
359b6a5bde7SBarry Smith 
360b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
361b6a5bde7SBarry Smith 
362b6a5bde7SBarry Smith M*/
363f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
364b6a5bde7SBarry Smith 
365b6a5bde7SBarry Smith /*MC
366b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
367b6a5bde7SBarry Smith 
368b6a5bde7SBarry Smith    Synopsis:
369f2ba6396SBarry Smith    #include "petscmath.h"
370b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
371b6a5bde7SBarry Smith 
372eca87e8dSBarry Smith    Not Collective
373eca87e8dSBarry Smith 
374eca87e8dSBarry Smith    Input Parameter:
375eca87e8dSBarry Smith .   v1 - the value
376eca87e8dSBarry Smith 
377b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
378b6a5bde7SBarry Smith 
379b6a5bde7SBarry Smith    Level: beginner
380b6a5bde7SBarry Smith 
381b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
382b6a5bde7SBarry Smith 
383b6a5bde7SBarry Smith M*/
3844ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
385e489efc1SBarry Smith 
386314da920SBarry Smith /* ----------------------------------------------------------------------------*/
387314da920SBarry Smith /*
388d34fcf5fSBarry Smith      Basic constants
389314da920SBarry Smith */
390ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
391d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
392d34fcf5fSBarry Smith #elif defined(M_PI)
393d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
394d34fcf5fSBarry Smith #else
395faa6e9b0SMatthew G Knepley #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
396d34fcf5fSBarry Smith #endif
397d34fcf5fSBarry Smith 
398ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
39971fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
400ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
401ab824b78SBarry Smith #else
402ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
403ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
404ab824b78SBarry Smith #endif
405e489efc1SBarry Smith 
406ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
407ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
408ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
40982a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
41082a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
411cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
412ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
413ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
414ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
41582a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
41682a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
417cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
418ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
419ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
420ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
421d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
422d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
423d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
4249cf09972SJed Brown #endif
4253e523bebSBarry Smith 
426014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanScalar(PetscScalar);
427014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
4289a25a3ccSBarry Smith 
429314da920SBarry Smith /* ----------------------------------------------------------------------------*/
43087828ca2SBarry Smith #define PassiveReal   PetscReal
431ea709b57SSatish Balay #define PassiveScalar PetscScalar
432d3ecb3a7SBarry Smith 
43398725619SBarry Smith /*
43498725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
43598725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
43698725619SBarry Smith  */
43798725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
43898725619SBarry Smith typedef PetscScalar MatScalar;
43998725619SBarry Smith typedef PetscReal MatReal;
44098725619SBarry Smith 
4418ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
4428ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
4438ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
4448ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
4458ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
4468ad47952SJed Brown #else
4478ad47952SJed Brown #define MPIU_2INT MPI_2INT
4488ad47952SJed Brown #endif
449e9fa29b7SSatish Balay 
450fa711258SJed Brown PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power) {
451fa711258SJed Brown   PetscInt result = 1;
452fa711258SJed Brown   while (power) {
453fa711258SJed Brown     if (power & 1) result *= base;
454fa711258SJed Brown     power >>= 1;
455fa711258SJed Brown     base *= base;
456fa711258SJed Brown   }
457fa711258SJed Brown   return result;
458fa711258SJed Brown }
459fa711258SJed Brown PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power) {
460fa711258SJed Brown   PetscReal result = 1;
461fa711258SJed Brown   while (power) {
462fa711258SJed Brown     if (power & 1) result *= base;
463fa711258SJed Brown     power >>= 1;
464fa711258SJed Brown     base *= base;
465fa711258SJed Brown   }
466fa711258SJed Brown   return result;
467fa711258SJed Brown }
468fa711258SJed Brown 
469e489efc1SBarry Smith #endif
470