xref: /petsc/include/petscmath.h (revision d4161b4ae070c1d342339263d4b0f5afb9b9e637)
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)
319a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
329a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
33255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
34255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
35369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
360646a658SBarry Smith #define PetscTGamma(a)      tgammaf(a)
37c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
38c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
39c1d390e3SJed Brown typedef double PetscReal;
408f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
419a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
429a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
439a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
449a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
45255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
46255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
47369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
480646a658SBarry Smith #define PetscTGamma(a)      tgamma(a)
49c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
50574fde7bSSatish Balay #if defined(__cplusplus)
51574fde7bSSatish Balay extern "C" {
52574fde7bSSatish Balay #endif
53574fde7bSSatish Balay #include <quadmath.h>
54574fde7bSSatish Balay #if defined(__cplusplus)
55574fde7bSSatish Balay }
56574fde7bSSatish Balay #endif
578ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
58c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
59c1d390e3SJed Brown typedef __float128 PetscReal;
608f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
619a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
629a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
639a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
649a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
65255453a1SBarry Smith #define PetscAsinReal(a)    asinq(a)
66255453a1SBarry Smith #define PetscAcosReal(a)    acosq(a)
67369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
680646a658SBarry Smith #define PetscTGamma(a)      tgammaq(a)
69c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
7059cb5930SBarry Smith 
711093a601SBarry Smith /*
721093a601SBarry Smith     Complex number definitions
731093a601SBarry Smith  */
7403df5147SBarry Smith #if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
75*d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX)) || defined(PETSC_DESIRE_COMPLEX)
7650f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
771093a601SBarry Smith /* C++ support of complex number */
78debe9ee2SPaul Mullowney #if defined(PETSC_HAVE_CUSP)
79debe9ee2SPaul Mullowney #define complexlib cusp
809ae82921SPaul Mullowney #include <cusp/complex.h>
81debe9ee2SPaul Mullowney #else
82debe9ee2SPaul Mullowney #define complexlib std
83debe9ee2SPaul Mullowney #include <complex>
849ae82921SPaul Mullowney #endif
85b7940d39SSatish Balay 
8650f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
8750f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
8850f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
8950f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
9050f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
9150f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
9250f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
9350f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
9450f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
9550f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
96255453a1SBarry Smith #define PetscAsinComplex(a)          complexlib::asin(a)
97255453a1SBarry Smith #define PetscAcosComplex(a)          complexlib::acos(a)
98debe9ee2SPaul Mullowney 
99debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
10050f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
101debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
10250f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
1038c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
10450f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
105debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
10603df5147SBarry Smith #endif  /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
107debe9ee2SPaul Mullowney 
10850f81f78SJed Brown #elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
10950f81f78SJed Brown /* Use C99 _Complex for the type. Do not include complex.h by default to define "complex" because of symbol conflicts in Hypre. */
11050f81f78SJed Brown /* Compilation units that can safely use complex should define PETSC_DESIRE_COMPLEX before including any headers */
111*d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX)) || defined(PETSC_DESIRE_COMPLEX)
1129f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
113519e2a1fSPaul Mullowney #include <complex.h>
114519e2a1fSPaul Mullowney 
115ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
11650f81f78SJed Brown typedef float _Complex PetscComplex;
11785b47369SMatthew Knepley 
11850f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
11950f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
12050f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
12150f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
12250f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
12350f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
12450f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
12550f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
12650f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
12750f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
128255453a1SBarry Smith #define PetscAsinComplex(a)          casinf(a)
129255453a1SBarry Smith #define PetscAcosComplex(a)          cacosf(a)
1301093a601SBarry Smith 
131ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
13250f81f78SJed Brown typedef double _Complex PetscComplex;
1331093a601SBarry Smith 
13450f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
13550f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
13650f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
13750f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
13850f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
13950f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
14050f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
14150f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
14250f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
14350f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
144255453a1SBarry Smith #define PetscAsinComplex(a)          casin(a)
145255453a1SBarry Smith #define PetscAcosComplex(a)          cacos(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)
161255453a1SBarry Smith #define PetscAsinComplex(a)          casinq(a)
162255453a1SBarry Smith #define PetscAcosComplex(a)          cacosq(a)
163ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
164*d4161b4aSBarry Smith #elif (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
16550f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
1669f20b660SSatish Balay #endif /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
1679f20b660SSatish Balay #endif /* (PETSC_CLANGUAGE_CXX && PETSC_HAVE_CXX_COMPLEX) else-if (PETSC_CLANGUAGE_C && PETSC_HAVE_C99_COMPLEX) */
168e489efc1SBarry Smith 
1698dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
17070da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
171500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
172500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
17370da9c3bSJed Brown #else
1748ad47952SJed Brown # if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX)
1758ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
1768ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
1778ad47952SJed Brown # elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
1788ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
1798ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
1808ad47952SJed Brown # else
1818ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
1828ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
1838ad47952SJed Brown # endif
1848ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
1858ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
1861b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
1878dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
1882c876bd9SBarry Smith 
1897c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
1907c2de775SJed Brown #  if defined(PETSC_USE_REAL_SINGLE)
1917c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
1927c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
1937c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
1947c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
1957c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
1967c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
1977c2de775SJed Brown #endif
1987c2de775SJed Brown 
199*d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
20050f81f78SJed Brown typedef PetscComplex PetscScalar;
20150f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
20250f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
20350f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
20450f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
20550f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
20650f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
20750f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
20850f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
20950f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
21050f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
211255453a1SBarry Smith #define PetscAsinScalar(a)    PetscAsinComplex(a)
212255453a1SBarry Smith #define PetscAcosScalar(a)    PetscAcosComplex(a)
21350f81f78SJed Brown 
2147c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
21575567043SBarry Smith 
2161093a601SBarry Smith /*
2171093a601SBarry Smith     real number definitions
2181093a601SBarry Smith  */
2191b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
2207c2de775SJed Brown typedef PetscReal PetscScalar;
2217c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
2227c2de775SJed Brown 
223329f5518SBarry Smith #define PetscRealPart(a)      (a)
224c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
225c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
226e489efc1SBarry Smith #define PetscConj(a)          (a)
227ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
22818a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
229184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
230184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
23106c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
232184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
233184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
234255453a1SBarry Smith #define PetscAsinScalar(a)    asin(a)
235255453a1SBarry Smith #define PetscAcosScalar(a)    acos(a)
236ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
2370d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
2380d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
2390d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
2400d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
2410d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
2420d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
243255453a1SBarry Smith #define PetscAsinScalar(a)    asinq(a)
244255453a1SBarry Smith #define PetscAcosScalar(a)    acosq(a)
245ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
246b0a32e0cSBarry Smith 
2471b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
248e489efc1SBarry Smith 
249da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
25026aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
2513f1db9ecSBarry Smith 
252314da920SBarry Smith /* --------------------------------------------------------------------------*/
253314da920SBarry Smith 
254e489efc1SBarry Smith /*
255f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
256f22f69f0SBarry Smith    This is currently not used.
257e489efc1SBarry Smith */
258557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
259e489efc1SBarry Smith 
26050f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
261e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
26250f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
26350f81f78SJed Brown #endif
264e489efc1SBarry Smith 
265b6a5bde7SBarry Smith /*MC
266b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
267b6a5bde7SBarry Smith 
268eca87e8dSBarry Smith    Synopsis:
269aaa7dc30SBarry Smith    #include <petscmath.h>
270eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
271eca87e8dSBarry Smith 
272eca87e8dSBarry Smith    Not Collective
273eca87e8dSBarry Smith 
274b6a5bde7SBarry Smith    Input Parameter:
275b6a5bde7SBarry Smith +  v1 - first value to find minimum of
276b6a5bde7SBarry Smith -  v2 - second value to find minimum of
277b6a5bde7SBarry Smith 
278b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
279b6a5bde7SBarry Smith 
280b6a5bde7SBarry Smith    Level: beginner
281b6a5bde7SBarry Smith 
282d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
283b6a5bde7SBarry Smith 
284b6a5bde7SBarry Smith M*/
285e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
286b6a5bde7SBarry Smith 
287b6a5bde7SBarry Smith /*MC
288b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
289b6a5bde7SBarry Smith 
290eca87e8dSBarry Smith    Synopsis:
291aaa7dc30SBarry Smith    #include <petscmath.h>
292eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
293eca87e8dSBarry Smith 
294eca87e8dSBarry Smith    Not Collective
295eca87e8dSBarry Smith 
296b6a5bde7SBarry Smith    Input Parameter:
297b6a5bde7SBarry Smith +  v1 - first value to find maximum of
298b6a5bde7SBarry Smith -  v2 - second value to find maximum of
299b6a5bde7SBarry Smith 
300b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
301b6a5bde7SBarry Smith 
302b6a5bde7SBarry Smith    Level: beginner
303b6a5bde7SBarry Smith 
304d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
305b6a5bde7SBarry Smith 
306b6a5bde7SBarry Smith M*/
307e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
308b6a5bde7SBarry Smith 
309b6a5bde7SBarry Smith /*MC
310d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
311d9a4bb16SJed Brown 
312d9a4bb16SJed Brown    Synopsis:
313aaa7dc30SBarry Smith    #include <petscmath.h>
314d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
315d9a4bb16SJed Brown 
316d9a4bb16SJed Brown    Not Collective
317d9a4bb16SJed Brown 
318d9a4bb16SJed Brown    Input Parameter:
319d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
320d9a4bb16SJed Brown .  a - lower end of interval
321d9a4bb16SJed Brown -  b - upper end of interval
322d9a4bb16SJed Brown 
323d9a4bb16SJed Brown    Notes: type can be integer or floating point value
324d9a4bb16SJed Brown 
325d9a4bb16SJed Brown    Level: beginner
326d9a4bb16SJed Brown 
327d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
328d9a4bb16SJed Brown 
329d9a4bb16SJed Brown M*/
330d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
331d9a4bb16SJed Brown 
332d9a4bb16SJed Brown /*MC
333b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
334b6a5bde7SBarry Smith 
335b6a5bde7SBarry Smith    Synopsis:
336aaa7dc30SBarry Smith    #include <petscmath.h>
337b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
338b6a5bde7SBarry Smith 
339eca87e8dSBarry Smith    Not Collective
340eca87e8dSBarry Smith 
341eca87e8dSBarry Smith    Input Parameter:
342eca87e8dSBarry Smith .   v1 - the integer
343b6a5bde7SBarry Smith 
344b6a5bde7SBarry Smith    Level: beginner
345b6a5bde7SBarry Smith 
346b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
347b6a5bde7SBarry Smith 
348b6a5bde7SBarry Smith M*/
349e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
350b6a5bde7SBarry Smith 
351b6a5bde7SBarry Smith /*MC
352b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
353b6a5bde7SBarry Smith 
354eca87e8dSBarry Smith    Synopsis:
355aaa7dc30SBarry Smith    #include <petscmath.h>
356eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
357eca87e8dSBarry Smith 
358eca87e8dSBarry Smith    Not Collective
359eca87e8dSBarry Smith 
360b6a5bde7SBarry Smith    Input Parameter:
361b6a5bde7SBarry Smith .   v1 - the double
362b6a5bde7SBarry Smith 
363b6a5bde7SBarry Smith 
364b6a5bde7SBarry Smith    Level: beginner
365b6a5bde7SBarry Smith 
366b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
367b6a5bde7SBarry Smith 
368b6a5bde7SBarry Smith M*/
369f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
370b6a5bde7SBarry Smith 
371b6a5bde7SBarry Smith /*MC
372b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
373b6a5bde7SBarry Smith 
374b6a5bde7SBarry Smith    Synopsis:
375aaa7dc30SBarry Smith    #include <petscmath.h>
376b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
377b6a5bde7SBarry Smith 
378eca87e8dSBarry Smith    Not Collective
379eca87e8dSBarry Smith 
380eca87e8dSBarry Smith    Input Parameter:
381eca87e8dSBarry Smith .   v1 - the value
382eca87e8dSBarry Smith 
383b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
384b6a5bde7SBarry Smith 
385b6a5bde7SBarry Smith    Level: beginner
386b6a5bde7SBarry Smith 
387b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
388b6a5bde7SBarry Smith 
389b6a5bde7SBarry Smith M*/
3904ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
391e489efc1SBarry Smith 
392314da920SBarry Smith /* ----------------------------------------------------------------------------*/
393314da920SBarry Smith /*
394d34fcf5fSBarry Smith      Basic constants
395314da920SBarry Smith */
396ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
397d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
398d34fcf5fSBarry Smith #elif defined(M_PI)
399d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
400d34fcf5fSBarry Smith #else
401faa6e9b0SMatthew G Knepley #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
402d34fcf5fSBarry Smith #endif
403d34fcf5fSBarry Smith 
404ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
40571fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
406ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
407ab824b78SBarry Smith #else
408ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
409ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
410ab824b78SBarry Smith #endif
411e489efc1SBarry Smith 
412ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
413ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
414ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
41582a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
41682a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
417cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
418ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
419ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
420ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
42182a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
42282a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
423cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
424ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
425ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
426ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
427d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
428d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
429d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
4309cf09972SJed Brown #endif
4313e523bebSBarry Smith 
432e270355aSBarry Smith #define PETSC_INFINITY                PETSC_MAX_REAL/4.0
433e270355aSBarry Smith #define PETSC_NINFINITY              -PETSC_INFINITY
434e270355aSBarry Smith 
435014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanScalar(PetscScalar);
436014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
4378b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalScalar(PetscScalar);
4388b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalReal(PetscReal);
4399a25a3ccSBarry Smith 
440314da920SBarry Smith /* ----------------------------------------------------------------------------*/
44187828ca2SBarry Smith #define PassiveReal   PetscReal
442ea709b57SSatish Balay #define PassiveScalar PetscScalar
443d3ecb3a7SBarry Smith 
44498725619SBarry Smith /*
44598725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
44698725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
44798725619SBarry Smith  */
44898725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
44998725619SBarry Smith typedef PetscScalar MatScalar;
45098725619SBarry Smith typedef PetscReal MatReal;
45198725619SBarry Smith 
4528ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
4538ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
4548ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
4558ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
4568ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
4578ad47952SJed Brown #else
4588ad47952SJed Brown #define MPIU_2INT MPI_2INT
4598ad47952SJed Brown #endif
460e9fa29b7SSatish Balay 
461fa711258SJed Brown PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power) {
462fa711258SJed Brown   PetscInt result = 1;
463fa711258SJed Brown   while (power) {
464fa711258SJed Brown     if (power & 1) result *= base;
465fa711258SJed Brown     power >>= 1;
466fa711258SJed Brown     base *= base;
467fa711258SJed Brown   }
468fa711258SJed Brown   return result;
469fa711258SJed Brown }
470fa711258SJed Brown PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power) {
471fa711258SJed Brown   PetscReal result = 1;
472d98d5da7SBarry Smith   if (power < 0) {
473d98d5da7SBarry Smith     power = -power;
474d98d5da7SBarry Smith     if (base != 0.0) base  = 1./base;
475d98d5da7SBarry Smith   }
476fa711258SJed Brown   while (power) {
477fa711258SJed Brown     if (power & 1) result *= base;
478fa711258SJed Brown     power >>= 1;
479fa711258SJed Brown     base *= base;
480fa711258SJed Brown   }
481fa711258SJed Brown   return result;
482fa711258SJed Brown }
483fa711258SJed Brown 
4848b49ba18SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarInt(PetscScalar base,PetscInt power) {
4858b49ba18SBarry Smith   PetscScalar result = 1;
4868b49ba18SBarry Smith   if (power < 0) {
4878b49ba18SBarry Smith     power = -power;
4888b49ba18SBarry Smith     if (base != 0.0) base  = 1./base;
4898b49ba18SBarry Smith   }
4908b49ba18SBarry Smith   while (power) {
4918b49ba18SBarry Smith     if (power & 1) result *= base;
4928b49ba18SBarry Smith     power >>= 1;
4938b49ba18SBarry Smith     base *= base;
4948b49ba18SBarry Smith   }
4958b49ba18SBarry Smith   return result;
4968b49ba18SBarry Smith }
4978b49ba18SBarry Smith 
498e489efc1SBarry Smith #endif
499