xref: /petsc/include/petscmath.h (revision d98d5da7d2c33ac3d23f2bba8b2d5dde24a8a47c)
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)
33369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
34c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
35c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
36c1d390e3SJed Brown typedef double PetscReal;
378f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
389a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
399a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
409a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
419a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
42369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
43c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
44574fde7bSSatish Balay #if defined(__cplusplus)
45574fde7bSSatish Balay extern "C" {
46574fde7bSSatish Balay #endif
47574fde7bSSatish Balay #include <quadmath.h>
48574fde7bSSatish Balay #if defined(__cplusplus)
49574fde7bSSatish Balay }
50574fde7bSSatish Balay #endif
518ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
52c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
53c1d390e3SJed Brown typedef __float128 PetscReal;
548f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
559a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
569a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
579a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
589a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
59369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
60c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
6159cb5930SBarry Smith 
621093a601SBarry Smith /*
631093a601SBarry Smith     Complex number definitions
641093a601SBarry Smith  */
6503df5147SBarry Smith #if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
669f20b660SSatish Balay #if defined(PETSC_USE_COMPLEX) || defined(PETSC_DESIRE_COMPLEX)
6750f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
681093a601SBarry Smith /* C++ support of complex number */
69debe9ee2SPaul Mullowney #if defined(PETSC_HAVE_CUSP)
70debe9ee2SPaul Mullowney #define complexlib cusp
719ae82921SPaul Mullowney #include <cusp/complex.h>
72debe9ee2SPaul Mullowney #else
73debe9ee2SPaul Mullowney #define complexlib std
74debe9ee2SPaul Mullowney #include <complex>
759ae82921SPaul Mullowney #endif
76b7940d39SSatish Balay 
7750f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
7850f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
7950f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
8050f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
8150f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
8250f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
8350f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
8450f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
8550f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
8650f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
87debe9ee2SPaul Mullowney 
88debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
8950f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
90debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
9150f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
928c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
9350f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
94debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
9503df5147SBarry Smith #endif  /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
96debe9ee2SPaul Mullowney 
9750f81f78SJed Brown #elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
9850f81f78SJed Brown /* Use C99 _Complex for the type. Do not include complex.h by default to define "complex" because of symbol conflicts in Hypre. */
9950f81f78SJed Brown /* Compilation units that can safely use complex should define PETSC_DESIRE_COMPLEX before including any headers */
10050f81f78SJed Brown #if defined(PETSC_USE_COMPLEX) || defined(PETSC_DESIRE_COMPLEX)
1019f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
102519e2a1fSPaul Mullowney #include <complex.h>
103519e2a1fSPaul Mullowney 
104ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
10550f81f78SJed Brown typedef float _Complex PetscComplex;
10685b47369SMatthew Knepley 
10750f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
10850f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
10950f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
11050f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
11150f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
11250f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
11350f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
11450f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
11550f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
11650f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
1171093a601SBarry Smith 
118ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
11950f81f78SJed Brown typedef double _Complex PetscComplex;
1201093a601SBarry Smith 
12150f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
12250f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
12350f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
12450f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
12550f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
12650f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
12750f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
12850f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
12950f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
13050f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
1311093a601SBarry Smith 
1328c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
13350f81f78SJed Brown typedef __complex128 PetscComplex;
1348ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
1358c764dc5SJose Roman 
13650f81f78SJed Brown #define PetscRealPartComplex(a)      crealq(a)
13750f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagq(a)
13850f81f78SJed Brown #define PetscAbsComplex(a)           cabsq(a)
13950f81f78SJed Brown #define PetscConjComplex(a)          conjq(a)
14050f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtq(a)
14150f81f78SJed Brown #define PetscPowComplex(a,b)         cpowq(a,b)
14250f81f78SJed Brown #define PetscExpComplex(a)           cexpq(a)
14350f81f78SJed Brown #define PetscLogComplex(a)           clogq(a)
14450f81f78SJed Brown #define PetscSinComplex(a)           csinq(a)
14550f81f78SJed Brown #define PetscCosComplex(a)           ccosq(a)
146ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
14750f81f78SJed Brown #elif defined(PETSC_USE_COMPLEX)
14850f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
1499f20b660SSatish Balay #endif /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
1509f20b660SSatish Balay #endif /* (PETSC_CLANGUAGE_CXX && PETSC_HAVE_CXX_COMPLEX) else-if (PETSC_CLANGUAGE_C && PETSC_HAVE_C99_COMPLEX) */
151e489efc1SBarry Smith 
1528dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
15370da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
154500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
155500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
15670da9c3bSJed Brown #else
1578ad47952SJed Brown # if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX)
1588ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
1598ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
1608ad47952SJed Brown # elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
1618ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
1628ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
1638ad47952SJed Brown # else
1648ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
1658ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
1668ad47952SJed Brown # endif
1678ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
1688ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
1691b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
1708dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
1712c876bd9SBarry Smith 
1727c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
1737c2de775SJed Brown #  if defined(PETSC_USE_REAL_SINGLE)
1747c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
1757c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
1767c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
1777c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
1787c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
1797c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
1807c2de775SJed Brown #endif
1817c2de775SJed Brown 
18250f81f78SJed Brown #if defined(PETSC_USE_COMPLEX)
18350f81f78SJed Brown typedef PetscComplex PetscScalar;
18450f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
18550f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
18650f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
18750f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
18850f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
18950f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
19050f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
19150f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
19250f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
19350f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
19450f81f78SJed Brown 
1957c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
19675567043SBarry Smith 
1971093a601SBarry Smith /*
1981093a601SBarry Smith     real number definitions
1991093a601SBarry Smith  */
2001b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
2017c2de775SJed Brown typedef PetscReal PetscScalar;
2027c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
2037c2de775SJed Brown 
204329f5518SBarry Smith #define PetscRealPart(a)      (a)
205c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
206c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
207e489efc1SBarry Smith #define PetscConj(a)          (a)
208ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
20918a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
210184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
211184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
21206c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
213184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
214184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
215ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
2160d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
2170d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
2180d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
2190d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
2200d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
2210d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
222ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
223b0a32e0cSBarry Smith 
2241b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
225e489efc1SBarry Smith 
226da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
22726aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
2283f1db9ecSBarry Smith 
229314da920SBarry Smith /* --------------------------------------------------------------------------*/
230314da920SBarry Smith 
231e489efc1SBarry Smith /*
232f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
233f22f69f0SBarry Smith    This is currently not used.
234e489efc1SBarry Smith */
235557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
236e489efc1SBarry Smith 
23750f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
238e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
23950f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
24050f81f78SJed Brown #endif
241e489efc1SBarry Smith 
242b6a5bde7SBarry Smith /*MC
243b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
244b6a5bde7SBarry Smith 
245eca87e8dSBarry Smith    Synopsis:
246f2ba6396SBarry Smith    #include "petscmath.h"
247eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
248eca87e8dSBarry Smith 
249eca87e8dSBarry Smith    Not Collective
250eca87e8dSBarry Smith 
251b6a5bde7SBarry Smith    Input Parameter:
252b6a5bde7SBarry Smith +  v1 - first value to find minimum of
253b6a5bde7SBarry Smith -  v2 - second value to find minimum of
254b6a5bde7SBarry Smith 
255b6a5bde7SBarry Smith 
256b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
257b6a5bde7SBarry Smith 
258b6a5bde7SBarry Smith    Level: beginner
259b6a5bde7SBarry Smith 
260b6a5bde7SBarry Smith 
261d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
262b6a5bde7SBarry Smith 
263b6a5bde7SBarry Smith M*/
264e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
265b6a5bde7SBarry Smith 
266b6a5bde7SBarry Smith /*MC
267b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
268b6a5bde7SBarry Smith 
269eca87e8dSBarry Smith    Synopsis:
270a663daf8SBarry Smith    #include "petscmath.h"
271eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
272eca87e8dSBarry Smith 
273eca87e8dSBarry Smith    Not Collective
274eca87e8dSBarry Smith 
275b6a5bde7SBarry Smith    Input Parameter:
276b6a5bde7SBarry Smith +  v1 - first value to find maximum of
277b6a5bde7SBarry Smith -  v2 - second value to find maximum of
278b6a5bde7SBarry Smith 
279b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
280b6a5bde7SBarry Smith 
281b6a5bde7SBarry Smith    Level: beginner
282b6a5bde7SBarry Smith 
283d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
284b6a5bde7SBarry Smith 
285b6a5bde7SBarry Smith M*/
286e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
287b6a5bde7SBarry Smith 
288b6a5bde7SBarry Smith /*MC
289d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
290d9a4bb16SJed Brown 
291d9a4bb16SJed Brown    Synopsis:
292f2ba6396SBarry Smith    #include "petscmath.h"
293d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
294d9a4bb16SJed Brown 
295d9a4bb16SJed Brown    Not Collective
296d9a4bb16SJed Brown 
297d9a4bb16SJed Brown    Input Parameter:
298d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
299d9a4bb16SJed Brown .  a - lower end of interval
300d9a4bb16SJed Brown -  b - upper end of interval
301d9a4bb16SJed Brown 
302d9a4bb16SJed Brown    Notes: type can be integer or floating point value
303d9a4bb16SJed Brown 
304d9a4bb16SJed Brown    Level: beginner
305d9a4bb16SJed Brown 
306d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
307d9a4bb16SJed Brown 
308d9a4bb16SJed Brown M*/
309d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
310d9a4bb16SJed Brown 
311d9a4bb16SJed Brown /*MC
312b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
313b6a5bde7SBarry Smith 
314b6a5bde7SBarry Smith    Synopsis:
315f2ba6396SBarry Smith    #include "petscmath.h"
316b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
317b6a5bde7SBarry Smith 
318eca87e8dSBarry Smith    Not Collective
319eca87e8dSBarry Smith 
320eca87e8dSBarry Smith    Input Parameter:
321eca87e8dSBarry Smith .   v1 - the integer
322b6a5bde7SBarry Smith 
323b6a5bde7SBarry Smith    Level: beginner
324b6a5bde7SBarry Smith 
325b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
326b6a5bde7SBarry Smith 
327b6a5bde7SBarry Smith M*/
328e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
329b6a5bde7SBarry Smith 
330b6a5bde7SBarry Smith /*MC
331b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
332b6a5bde7SBarry Smith 
333eca87e8dSBarry Smith    Synopsis:
334f2ba6396SBarry Smith    #include "petscmath.h"
335eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
336eca87e8dSBarry Smith 
337eca87e8dSBarry Smith    Not Collective
338eca87e8dSBarry Smith 
339b6a5bde7SBarry Smith    Input Parameter:
340b6a5bde7SBarry Smith .   v1 - the double
341b6a5bde7SBarry Smith 
342b6a5bde7SBarry Smith 
343b6a5bde7SBarry Smith    Level: beginner
344b6a5bde7SBarry Smith 
345b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
346b6a5bde7SBarry Smith 
347b6a5bde7SBarry Smith M*/
348f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
349b6a5bde7SBarry Smith 
350b6a5bde7SBarry Smith /*MC
351b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
352b6a5bde7SBarry Smith 
353b6a5bde7SBarry Smith    Synopsis:
354f2ba6396SBarry Smith    #include "petscmath.h"
355b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
356b6a5bde7SBarry Smith 
357eca87e8dSBarry Smith    Not Collective
358eca87e8dSBarry Smith 
359eca87e8dSBarry Smith    Input Parameter:
360eca87e8dSBarry Smith .   v1 - the value
361eca87e8dSBarry Smith 
362b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
363b6a5bde7SBarry Smith 
364b6a5bde7SBarry Smith    Level: beginner
365b6a5bde7SBarry Smith 
366b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
367b6a5bde7SBarry Smith 
368b6a5bde7SBarry Smith M*/
3694ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
370e489efc1SBarry Smith 
371314da920SBarry Smith /* ----------------------------------------------------------------------------*/
372314da920SBarry Smith /*
373d34fcf5fSBarry Smith      Basic constants
374314da920SBarry Smith */
375ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
376d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
377d34fcf5fSBarry Smith #elif defined(M_PI)
378d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
379d34fcf5fSBarry Smith #else
380faa6e9b0SMatthew G Knepley #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
381d34fcf5fSBarry Smith #endif
382d34fcf5fSBarry Smith 
383ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
38471fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
385ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
386ab824b78SBarry Smith #else
387ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
388ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
389ab824b78SBarry Smith #endif
390e489efc1SBarry Smith 
391ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
392ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
393ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
39482a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
39582a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
396cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
397ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
398ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
399ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
40082a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
40182a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
402cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
403ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
404ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
405ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
406d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
407d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
408d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
4099cf09972SJed Brown #endif
4103e523bebSBarry Smith 
411014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanScalar(PetscScalar);
412014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
4139a25a3ccSBarry Smith 
414314da920SBarry Smith /* ----------------------------------------------------------------------------*/
41587828ca2SBarry Smith #define PassiveReal   PetscReal
416ea709b57SSatish Balay #define PassiveScalar PetscScalar
417d3ecb3a7SBarry Smith 
41898725619SBarry Smith /*
41998725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
42098725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
42198725619SBarry Smith  */
42298725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
42398725619SBarry Smith typedef PetscScalar MatScalar;
42498725619SBarry Smith typedef PetscReal MatReal;
42598725619SBarry Smith 
4268ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
4278ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
4288ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
4298ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
4308ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
4318ad47952SJed Brown #else
4328ad47952SJed Brown #define MPIU_2INT MPI_2INT
4338ad47952SJed Brown #endif
434e9fa29b7SSatish Balay 
435fa711258SJed Brown PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power) {
436fa711258SJed Brown   PetscInt result = 1;
437fa711258SJed Brown   while (power) {
438fa711258SJed Brown     if (power & 1) result *= base;
439fa711258SJed Brown     power >>= 1;
440fa711258SJed Brown     base *= base;
441fa711258SJed Brown   }
442fa711258SJed Brown   return result;
443fa711258SJed Brown }
444fa711258SJed Brown PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power) {
445fa711258SJed Brown   PetscReal result = 1;
446*d98d5da7SBarry Smith   if (power < 0) {
447*d98d5da7SBarry Smith     power = -power;
448*d98d5da7SBarry Smith     if (base != 0.0) base  = 1./base;
449*d98d5da7SBarry Smith   }
450fa711258SJed Brown   while (power) {
451fa711258SJed Brown     if (power & 1) result *= base;
452fa711258SJed Brown     power >>= 1;
453fa711258SJed Brown     base *= base;
454fa711258SJed Brown   }
455fa711258SJed Brown   return result;
456fa711258SJed Brown }
457fa711258SJed Brown 
458e489efc1SBarry Smith #endif
459