xref: /petsc/include/petscmath.h (revision a4bea5a61c92f07ad6b2bc99d7b0771c63ed8737)
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)
3177b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
329a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
339a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
34*a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
35*a4bea5a6SPeter Brune #define PetscAsinReal(a)    asin(a)
3677b4d14cSPeter Brune #define PetscAcosReal(a)    acos(a)
37*a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
38*a4bea5a6SPeter Brune #define PetscSinhReal(a)    sinh(a)
39*a4bea5a6SPeter Brune #define PetscCoshReal(a)    cosh(a)
40*a4bea5a6SPeter Brune #define PetscTanhReal(a)    tanh(a)
41369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
4277b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
4377b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
44*a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
45c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
46c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
47c1d390e3SJed Brown typedef double PetscReal;
488f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
499a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
509a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
5177b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
529a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
539a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
54*a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
55*a4bea5a6SPeter Brune #define PetscSinhReal(a)    sinh(a)
56*a4bea5a6SPeter Brune #define PetscCoshReal(a)    cosh(a)
57*a4bea5a6SPeter Brune #define PetscTanhReal(a)    tanh(a)
58*a4bea5a6SPeter Brune #define PetscAsinReal(a)    asin(a)
5977b4d14cSPeter Brune #define PetscAcosReal(a)    acos(a)
60*a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
61369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
6277b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
6377b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
64*a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
65c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
66574fde7bSSatish Balay #if defined(__cplusplus)
67574fde7bSSatish Balay extern "C" {
68574fde7bSSatish Balay #endif
69574fde7bSSatish Balay #include <quadmath.h>
70574fde7bSSatish Balay #if defined(__cplusplus)
71574fde7bSSatish Balay }
72574fde7bSSatish Balay #endif
738ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
74c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
75c1d390e3SJed Brown typedef __float128 PetscReal;
768f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
779a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
789a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
7977b4d14cSPeter Brune #define PetscLog10Real(a)   log10q(a)
809a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
819a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
82*a4bea5a6SPeter Brune #define PetscTanReal(a)     tanq(a)
83*a4bea5a6SPeter Brune #define PetscSinhReal(a)    sinhq(a)
84*a4bea5a6SPeter Brune #define PetscCoshReal(a)    coshq(a)
85*a4bea5a6SPeter Brune #define PetscTanhReal(a)    tanhq(a)
86*a4bea5a6SPeter Brune #define PetscAsinReal(a)    asinq(a)
8777b4d14cSPeter Brune #define PetscAcosReal(a)    acosq(a)
88*a4bea5a6SPeter Brune #define PetscAtanReal(a)    atanq(a)
89*a4bea5a6SPeter Brune #define PetscAtan2Real(a)   atan2q(a)
90369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
9177b4d14cSPeter Brune #define PetscCeilReal(a)    ceilq(a)
9277b4d14cSPeter Brune #define PetscFloorReal(a)   floorq(a)
93*a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmodq(a,b)
94c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
9559cb5930SBarry Smith 
961093a601SBarry Smith /*
971093a601SBarry Smith     Complex number definitions
981093a601SBarry Smith  */
9950f81f78SJed Brown #if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX)
1009f20b660SSatish Balay #if defined(PETSC_USE_COMPLEX) || defined(PETSC_DESIRE_COMPLEX)
10150f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
1021093a601SBarry Smith /* C++ support of complex number */
103debe9ee2SPaul Mullowney #if defined(PETSC_HAVE_CUSP)
104debe9ee2SPaul Mullowney #define complexlib cusp
1059ae82921SPaul Mullowney #include <cusp/complex.h>
106debe9ee2SPaul Mullowney #else
107debe9ee2SPaul Mullowney #define complexlib std
108debe9ee2SPaul Mullowney #include <complex>
1099ae82921SPaul Mullowney #endif
110b7940d39SSatish Balay 
11150f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
11250f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
11350f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
11450f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
11550f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
11650f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
11750f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
11850f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
11950f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
12050f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
121*a4bea5a6SPeter Brune #define PetscTanComplex(a)           complexlib::tan(a)
122*a4bea5a6SPeter Brune #define PetscSinhComplex(a)          complexlib::sinh(a)
123*a4bea5a6SPeter Brune #define PetscCoshComplex(a)          complexlib::cosh(a)
124*a4bea5a6SPeter Brune #define PetscTanhComplex(a)          complexlib::tanh(a)
125debe9ee2SPaul Mullowney 
126debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
12750f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
128debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
12950f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
1308c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
13150f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
132debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
1339f20b660SSatish Balay #endif  /* PETSC_USE_COMPLEX && PETSC_DESIRE_COMPLEX */
134debe9ee2SPaul Mullowney 
13550f81f78SJed Brown #elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
13650f81f78SJed Brown /* Use C99 _Complex for the type. Do not include complex.h by default to define "complex" because of symbol conflicts in Hypre. */
13750f81f78SJed Brown /* Compilation units that can safely use complex should define PETSC_DESIRE_COMPLEX before including any headers */
13850f81f78SJed Brown #if defined(PETSC_USE_COMPLEX) || defined(PETSC_DESIRE_COMPLEX)
1399f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
140519e2a1fSPaul Mullowney #include <complex.h>
141519e2a1fSPaul Mullowney 
142ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
14350f81f78SJed Brown typedef float _Complex PetscComplex;
14485b47369SMatthew Knepley 
14550f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
14650f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
14750f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
14850f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
14950f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
15050f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
15150f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
15250f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
15350f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
154*a4bea5a6SPeter Brune #define PetscCosComplex(a)           ccosf(a
155*a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanf(a)
156*a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhf(a)
157*a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshf(a)
158*a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhf(a)
1591093a601SBarry Smith 
160ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
16150f81f78SJed Brown typedef double _Complex PetscComplex;
1621093a601SBarry Smith 
16350f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
16450f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
16550f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
16650f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
16750f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
16850f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
16950f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
17050f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
17150f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
17250f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
173*a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctan(a)
174*a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinh(a)
175*a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccosh(a)
176*a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanh(a)
1771093a601SBarry Smith 
1788c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
17950f81f78SJed Brown typedef __complex128 PetscComplex;
1808ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
1818c764dc5SJose Roman 
18250f81f78SJed Brown #define PetscRealPartComplex(a)      crealq(a)
18350f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagq(a)
18450f81f78SJed Brown #define PetscAbsComplex(a)           cabsq(a)
18550f81f78SJed Brown #define PetscConjComplex(a)          conjq(a)
18650f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtq(a)
18750f81f78SJed Brown #define PetscPowComplex(a,b)         cpowq(a,b)
18850f81f78SJed Brown #define PetscExpComplex(a)           cexpq(a)
18950f81f78SJed Brown #define PetscLogComplex(a)           clogq(a)
19050f81f78SJed Brown #define PetscSinComplex(a)           csinq(a)
19150f81f78SJed Brown #define PetscCosComplex(a)           ccosq(a)
192*a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanq(a)
193*a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhq(a)
194*a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshq(a)
195*a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhq(a)
196*a4bea5a6SPeter Brune 
197ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
19850f81f78SJed Brown #elif defined(PETSC_USE_COMPLEX)
19950f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
2009f20b660SSatish Balay #endif /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
2019f20b660SSatish Balay #endif /* (PETSC_CLANGUAGE_CXX && PETSC_HAVE_CXX_COMPLEX) else-if (PETSC_CLANGUAGE_C && PETSC_HAVE_C99_COMPLEX) */
202e489efc1SBarry Smith 
2038dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
20470da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
205500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
206500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
20770da9c3bSJed Brown #else
2088ad47952SJed Brown # if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX)
2098ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
2108ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
2118ad47952SJed Brown # elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
2128ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
2138ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
2148ad47952SJed Brown # else
2158ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
2168ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
2178ad47952SJed Brown # endif
2188ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
2198ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
2201b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
2218dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
2222c876bd9SBarry Smith 
2237c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
2247c2de775SJed Brown #  if defined(PETSC_USE_REAL_SINGLE)
2257c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
2267c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
2277c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
2287c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
2297c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
2307c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
2317c2de775SJed Brown #endif
2327c2de775SJed Brown 
23350f81f78SJed Brown #if defined(PETSC_USE_COMPLEX)
23450f81f78SJed Brown typedef PetscComplex PetscScalar;
23550f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
23650f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
23750f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
23850f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
23950f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
24050f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
24150f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
24250f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
24350f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
24450f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
245*a4bea5a6SPeter Brune #define PetscTanScalar(a)     PetscTanComplex(a)
246*a4bea5a6SPeter Brune #define PetscSinhScalar(a)    PetscSinhComplex(a)
247*a4bea5a6SPeter Brune #define PetscCoshScalar(a)    PetscCoshComplex(a)
248*a4bea5a6SPeter Brune #define PetscTanhScalar(a)    PetscTanhComplex(a)
2497c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
25075567043SBarry Smith 
2511093a601SBarry Smith /*
2521093a601SBarry Smith     real number definitions
2531093a601SBarry Smith  */
2541b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
2557c2de775SJed Brown typedef PetscReal PetscScalar;
2567c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
2577c2de775SJed Brown 
258329f5518SBarry Smith #define PetscRealPart(a)      (a)
259c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
260c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
261e489efc1SBarry Smith #define PetscConj(a)          (a)
262ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
26318a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
264184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
265184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
26606c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
267184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
268184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
269*a4bea5a6SPeter Brune #define PetscTanScalar(a)     tan(a)
270*a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinh(a)
271*a4bea5a6SPeter Brune #define PetscCoshScalar(a)    cosh(a)
272*a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanh(a)
273ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
2740d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
2750d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
2760d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
2770d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
2780d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
2790d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
280*a4bea5a6SPeter Brune #define PetscTanScalar(a)     tanq(a)
281*a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinhq(a)
282*a4bea5a6SPeter Brune #define PetscCoshScalar(a)    coshq(a)
283*a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanhq(a)
284ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
285b0a32e0cSBarry Smith 
2861b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
287e489efc1SBarry Smith 
288da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
28926aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
2903f1db9ecSBarry Smith 
291314da920SBarry Smith /* --------------------------------------------------------------------------*/
292314da920SBarry Smith 
293e489efc1SBarry Smith /*
294f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
295f22f69f0SBarry Smith    This is currently not used.
296e489efc1SBarry Smith */
297557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
298e489efc1SBarry Smith 
29950f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
300e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
30150f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
30250f81f78SJed Brown #endif
303e489efc1SBarry Smith 
304b6a5bde7SBarry Smith /*MC
305b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
306b6a5bde7SBarry Smith 
307eca87e8dSBarry Smith    Synopsis:
308f2ba6396SBarry Smith    #include "petscmath.h"
309eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
310eca87e8dSBarry Smith 
311eca87e8dSBarry Smith    Not Collective
312eca87e8dSBarry Smith 
313b6a5bde7SBarry Smith    Input Parameter:
314b6a5bde7SBarry Smith +  v1 - first value to find minimum of
315b6a5bde7SBarry Smith -  v2 - second value to find minimum of
316b6a5bde7SBarry Smith 
317b6a5bde7SBarry Smith 
318b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
319b6a5bde7SBarry Smith 
320b6a5bde7SBarry Smith    Level: beginner
321b6a5bde7SBarry Smith 
322b6a5bde7SBarry Smith 
323d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
324b6a5bde7SBarry Smith 
325b6a5bde7SBarry Smith M*/
326e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
327b6a5bde7SBarry Smith 
328b6a5bde7SBarry Smith /*MC
329b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
330b6a5bde7SBarry Smith 
331eca87e8dSBarry Smith    Synopsis:
332a663daf8SBarry Smith    #include "petscmath.h"
333eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
334eca87e8dSBarry Smith 
335eca87e8dSBarry Smith    Not Collective
336eca87e8dSBarry Smith 
337b6a5bde7SBarry Smith    Input Parameter:
338b6a5bde7SBarry Smith +  v1 - first value to find maximum of
339b6a5bde7SBarry Smith -  v2 - second value to find maximum of
340b6a5bde7SBarry Smith 
341b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
342b6a5bde7SBarry Smith 
343b6a5bde7SBarry Smith    Level: beginner
344b6a5bde7SBarry Smith 
345d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
346b6a5bde7SBarry Smith 
347b6a5bde7SBarry Smith M*/
348e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
349b6a5bde7SBarry Smith 
350b6a5bde7SBarry Smith /*MC
351d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
352d9a4bb16SJed Brown 
353d9a4bb16SJed Brown    Synopsis:
354f2ba6396SBarry Smith    #include "petscmath.h"
355d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
356d9a4bb16SJed Brown 
357d9a4bb16SJed Brown    Not Collective
358d9a4bb16SJed Brown 
359d9a4bb16SJed Brown    Input Parameter:
360d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
361d9a4bb16SJed Brown .  a - lower end of interval
362d9a4bb16SJed Brown -  b - upper end of interval
363d9a4bb16SJed Brown 
364d9a4bb16SJed Brown    Notes: type can be integer or floating point value
365d9a4bb16SJed Brown 
366d9a4bb16SJed Brown    Level: beginner
367d9a4bb16SJed Brown 
368d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
369d9a4bb16SJed Brown 
370d9a4bb16SJed Brown M*/
371d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
372d9a4bb16SJed Brown 
373d9a4bb16SJed Brown /*MC
374b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
375b6a5bde7SBarry Smith 
376b6a5bde7SBarry Smith    Synopsis:
377f2ba6396SBarry Smith    #include "petscmath.h"
378b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
379b6a5bde7SBarry Smith 
380eca87e8dSBarry Smith    Not Collective
381eca87e8dSBarry Smith 
382eca87e8dSBarry Smith    Input Parameter:
383eca87e8dSBarry Smith .   v1 - the integer
384b6a5bde7SBarry Smith 
385b6a5bde7SBarry Smith    Level: beginner
386b6a5bde7SBarry Smith 
387b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
388b6a5bde7SBarry Smith 
389b6a5bde7SBarry Smith M*/
390e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
391b6a5bde7SBarry Smith 
392b6a5bde7SBarry Smith /*MC
393b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
394b6a5bde7SBarry Smith 
395eca87e8dSBarry Smith    Synopsis:
396f2ba6396SBarry Smith    #include "petscmath.h"
397eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
398eca87e8dSBarry Smith 
399eca87e8dSBarry Smith    Not Collective
400eca87e8dSBarry Smith 
401b6a5bde7SBarry Smith    Input Parameter:
402b6a5bde7SBarry Smith .   v1 - the double
403b6a5bde7SBarry Smith 
404b6a5bde7SBarry Smith 
405b6a5bde7SBarry Smith    Level: beginner
406b6a5bde7SBarry Smith 
407b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
408b6a5bde7SBarry Smith 
409b6a5bde7SBarry Smith M*/
410f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
411b6a5bde7SBarry Smith 
412b6a5bde7SBarry Smith /*MC
413b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
414b6a5bde7SBarry Smith 
415b6a5bde7SBarry Smith    Synopsis:
416f2ba6396SBarry Smith    #include "petscmath.h"
417b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
418b6a5bde7SBarry Smith 
419eca87e8dSBarry Smith    Not Collective
420eca87e8dSBarry Smith 
421eca87e8dSBarry Smith    Input Parameter:
422eca87e8dSBarry Smith .   v1 - the value
423eca87e8dSBarry Smith 
424b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
425b6a5bde7SBarry Smith 
426b6a5bde7SBarry Smith    Level: beginner
427b6a5bde7SBarry Smith 
428b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
429b6a5bde7SBarry Smith 
430b6a5bde7SBarry Smith M*/
4314ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
432e489efc1SBarry Smith 
433314da920SBarry Smith /* ----------------------------------------------------------------------------*/
434314da920SBarry Smith /*
435d34fcf5fSBarry Smith      Basic constants
436314da920SBarry Smith */
437ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
438d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
439d34fcf5fSBarry Smith #elif defined(M_PI)
440d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
441d34fcf5fSBarry Smith #else
442faa6e9b0SMatthew G Knepley #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
443d34fcf5fSBarry Smith #endif
444d34fcf5fSBarry Smith 
445ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
44671fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
447ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
448ab824b78SBarry Smith #else
449ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
450ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
451ab824b78SBarry Smith #endif
452e489efc1SBarry Smith 
453ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
454ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
455ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
45682a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
45782a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
458cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
459ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
460ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
461ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
46282a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
46382a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
464cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
465ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
466ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
467ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
468d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
469d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
470d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
4719cf09972SJed Brown #endif
4723e523bebSBarry Smith 
473014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanScalar(PetscScalar);
474014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
4759a25a3ccSBarry Smith 
476314da920SBarry Smith /* ----------------------------------------------------------------------------*/
47787828ca2SBarry Smith #define PassiveReal   PetscReal
478ea709b57SSatish Balay #define PassiveScalar PetscScalar
479d3ecb3a7SBarry Smith 
48098725619SBarry Smith /*
48198725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
48298725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
48398725619SBarry Smith  */
48498725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
48598725619SBarry Smith typedef PetscScalar MatScalar;
48698725619SBarry Smith typedef PetscReal MatReal;
48798725619SBarry Smith 
4888ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
4898ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
4908ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
4918ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
4928ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
4938ad47952SJed Brown #else
4948ad47952SJed Brown #define MPIU_2INT MPI_2INT
4958ad47952SJed Brown #endif
496e9fa29b7SSatish Balay 
497fa711258SJed Brown PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power) {
498fa711258SJed Brown   PetscInt result = 1;
499fa711258SJed Brown   while (power) {
500fa711258SJed Brown     if (power & 1) result *= base;
501fa711258SJed Brown     power >>= 1;
502fa711258SJed Brown     base *= base;
503fa711258SJed Brown   }
504fa711258SJed Brown   return result;
505fa711258SJed Brown }
506fa711258SJed Brown PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power) {
507fa711258SJed Brown   PetscReal result = 1;
508fa711258SJed Brown   while (power) {
509fa711258SJed Brown     if (power & 1) result *= base;
510fa711258SJed Brown     power >>= 1;
511fa711258SJed Brown     base *= base;
512fa711258SJed Brown   }
513fa711258SJed Brown   return result;
514fa711258SJed Brown }
515fa711258SJed Brown 
516e489efc1SBarry Smith #endif
517