xref: /petsc/include/petscmath.h (revision 9cf33046db122d52a68e63df0602df9c4ae0d4ea)
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;
28*9cf33046SSatish 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)
33c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
34c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
35c1d390e3SJed Brown typedef double PetscReal;
368f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
379a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
389a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
399a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
409a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
41c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
42574fde7bSSatish Balay #if defined(__cplusplus)
43574fde7bSSatish Balay extern "C" {
44574fde7bSSatish Balay #endif
45574fde7bSSatish Balay #include <quadmath.h>
46574fde7bSSatish Balay #if defined(__cplusplus)
47574fde7bSSatish Balay }
48574fde7bSSatish Balay #endif
498ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
50c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
51c1d390e3SJed Brown typedef __float128 PetscReal;
528f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
539a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
549a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
559a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
569a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
57c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
5859cb5930SBarry Smith 
591093a601SBarry Smith /*
601093a601SBarry Smith     Complex number definitions
611093a601SBarry Smith  */
6250f81f78SJed Brown #if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX)
639f20b660SSatish Balay #if defined(PETSC_USE_COMPLEX) || defined(PETSC_DESIRE_COMPLEX)
6450f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
651093a601SBarry Smith /* C++ support of complex number */
66debe9ee2SPaul Mullowney #if defined(PETSC_HAVE_CUSP)
67debe9ee2SPaul Mullowney #define complexlib cusp
689ae82921SPaul Mullowney #include <cusp/complex.h>
69debe9ee2SPaul Mullowney #else
70debe9ee2SPaul Mullowney #define complexlib std
71debe9ee2SPaul Mullowney #include <complex>
729ae82921SPaul Mullowney #endif
73b7940d39SSatish Balay 
7450f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
7550f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
7650f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
7750f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
7850f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
7950f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
8050f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
8150f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
8250f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
8350f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
84debe9ee2SPaul Mullowney 
85debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
8650f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
87debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
8850f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
898c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
9050f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
91debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
929f20b660SSatish Balay #endif  /* PETSC_USE_COMPLEX && PETSC_DESIRE_COMPLEX */
93debe9ee2SPaul Mullowney 
9450f81f78SJed Brown #elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
9550f81f78SJed Brown /* Use C99 _Complex for the type. Do not include complex.h by default to define "complex" because of symbol conflicts in Hypre. */
9650f81f78SJed Brown /* Compilation units that can safely use complex should define PETSC_DESIRE_COMPLEX before including any headers */
9750f81f78SJed Brown #if defined(PETSC_USE_COMPLEX) || defined(PETSC_DESIRE_COMPLEX)
989f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
99519e2a1fSPaul Mullowney #include <complex.h>
100519e2a1fSPaul Mullowney 
101ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
10250f81f78SJed Brown typedef float _Complex PetscComplex;
10385b47369SMatthew Knepley 
10450f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
10550f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
10650f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
10750f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
10850f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
10950f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
11050f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
11150f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
11250f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
11350f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
1141093a601SBarry Smith 
115ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
11650f81f78SJed Brown typedef double _Complex PetscComplex;
1171093a601SBarry Smith 
11850f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
11950f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
12050f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
12150f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
12250f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
12350f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
12450f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
12550f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
12650f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
12750f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
1281093a601SBarry Smith 
1298c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
13050f81f78SJed Brown typedef __complex128 PetscComplex;
1318ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
1328c764dc5SJose Roman 
13350f81f78SJed Brown #define PetscRealPartComplex(a)      crealq(a)
13450f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagq(a)
13550f81f78SJed Brown #define PetscAbsComplex(a)           cabsq(a)
13650f81f78SJed Brown #define PetscConjComplex(a)          conjq(a)
13750f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtq(a)
13850f81f78SJed Brown #define PetscPowComplex(a,b)         cpowq(a,b)
13950f81f78SJed Brown #define PetscExpComplex(a)           cexpq(a)
14050f81f78SJed Brown #define PetscLogComplex(a)           clogq(a)
14150f81f78SJed Brown #define PetscSinComplex(a)           csinq(a)
14250f81f78SJed Brown #define PetscCosComplex(a)           ccosq(a)
143ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
14450f81f78SJed Brown #elif defined(PETSC_USE_COMPLEX)
14550f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
1469f20b660SSatish Balay #endif /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
1479f20b660SSatish Balay #endif /* (PETSC_CLANGUAGE_CXX && PETSC_HAVE_CXX_COMPLEX) else-if (PETSC_CLANGUAGE_C && PETSC_HAVE_C99_COMPLEX) */
148e489efc1SBarry Smith 
1498dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
15070da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
151500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
152500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
15370da9c3bSJed Brown #else
1548ad47952SJed Brown # if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX)
1558ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
1568ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
1578ad47952SJed Brown # elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
1588ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
1598ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
1608ad47952SJed Brown # else
1618ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
1628ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
1638ad47952SJed Brown # endif
1648ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
1658ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
1661b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
1678dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
1682c876bd9SBarry Smith 
1697c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
1707c2de775SJed Brown #  if defined(PETSC_USE_REAL_SINGLE)
1717c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
1727c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
1737c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
1747c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
1757c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
1767c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
1777c2de775SJed Brown #endif
1787c2de775SJed Brown 
17950f81f78SJed Brown #if defined(PETSC_USE_COMPLEX)
18050f81f78SJed Brown typedef PetscComplex PetscScalar;
18150f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
18250f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
18350f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
18450f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
18550f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
18650f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
18750f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
18850f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
18950f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
19050f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
19150f81f78SJed Brown 
1927c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
19375567043SBarry Smith 
1941093a601SBarry Smith /*
1951093a601SBarry Smith     real number definitions
1961093a601SBarry Smith  */
1971b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
1987c2de775SJed Brown typedef PetscReal PetscScalar;
1997c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
2007c2de775SJed Brown 
201329f5518SBarry Smith #define PetscRealPart(a)      (a)
202c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
203c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
204e489efc1SBarry Smith #define PetscConj(a)          (a)
205ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
20618a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
207184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
208184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
20906c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
210184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
211184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
212ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
2130d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
2140d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
2150d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
2160d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
2170d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
2180d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
219ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
220b0a32e0cSBarry Smith 
2211b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
222e489efc1SBarry Smith 
223da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
22426aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
2253f1db9ecSBarry Smith 
226314da920SBarry Smith /* --------------------------------------------------------------------------*/
227314da920SBarry Smith 
228e489efc1SBarry Smith /*
229f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
230f22f69f0SBarry Smith    This is currently not used.
231e489efc1SBarry Smith */
232557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
233e489efc1SBarry Smith 
23450f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
235e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
23650f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
23750f81f78SJed Brown #endif
238e489efc1SBarry Smith 
239b6a5bde7SBarry Smith /*MC
240b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
241b6a5bde7SBarry Smith 
242eca87e8dSBarry Smith    Synopsis:
243eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
244eca87e8dSBarry Smith 
245eca87e8dSBarry Smith    Not Collective
246eca87e8dSBarry Smith 
247b6a5bde7SBarry Smith    Input Parameter:
248b6a5bde7SBarry Smith +  v1 - first value to find minimum of
249b6a5bde7SBarry Smith -  v2 - second value to find minimum of
250b6a5bde7SBarry Smith 
251b6a5bde7SBarry Smith 
252b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
253b6a5bde7SBarry Smith 
254b6a5bde7SBarry Smith    Level: beginner
255b6a5bde7SBarry Smith 
256b6a5bde7SBarry Smith 
257d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
258b6a5bde7SBarry Smith 
259b6a5bde7SBarry Smith M*/
260e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
261b6a5bde7SBarry Smith 
262b6a5bde7SBarry Smith /*MC
263b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
264b6a5bde7SBarry Smith 
265eca87e8dSBarry Smith    Synopsis:
266eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
267eca87e8dSBarry Smith 
268eca87e8dSBarry Smith    Not Collective
269eca87e8dSBarry Smith 
270b6a5bde7SBarry Smith    Input Parameter:
271b6a5bde7SBarry Smith +  v1 - first value to find maximum of
272b6a5bde7SBarry Smith -  v2 - second value to find maximum of
273b6a5bde7SBarry Smith 
274b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
275b6a5bde7SBarry Smith 
276b6a5bde7SBarry Smith    Level: beginner
277b6a5bde7SBarry Smith 
278d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
279b6a5bde7SBarry Smith 
280b6a5bde7SBarry Smith M*/
281e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
282b6a5bde7SBarry Smith 
283b6a5bde7SBarry Smith /*MC
284d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
285d9a4bb16SJed Brown 
286d9a4bb16SJed Brown    Synopsis:
287d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
288d9a4bb16SJed Brown 
289d9a4bb16SJed Brown    Not Collective
290d9a4bb16SJed Brown 
291d9a4bb16SJed Brown    Input Parameter:
292d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
293d9a4bb16SJed Brown .  a - lower end of interval
294d9a4bb16SJed Brown -  b - upper end of interval
295d9a4bb16SJed Brown 
296d9a4bb16SJed Brown    Notes: type can be integer or floating point value
297d9a4bb16SJed Brown 
298d9a4bb16SJed Brown    Level: beginner
299d9a4bb16SJed Brown 
300d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
301d9a4bb16SJed Brown 
302d9a4bb16SJed Brown M*/
303d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
304d9a4bb16SJed Brown 
305d9a4bb16SJed Brown /*MC
306b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
307b6a5bde7SBarry Smith 
308b6a5bde7SBarry Smith    Synopsis:
309b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
310b6a5bde7SBarry Smith 
311eca87e8dSBarry Smith    Not Collective
312eca87e8dSBarry Smith 
313eca87e8dSBarry Smith    Input Parameter:
314eca87e8dSBarry Smith .   v1 - the integer
315b6a5bde7SBarry Smith 
316b6a5bde7SBarry Smith    Level: beginner
317b6a5bde7SBarry Smith 
318b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
319b6a5bde7SBarry Smith 
320b6a5bde7SBarry Smith M*/
321e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
322b6a5bde7SBarry Smith 
323b6a5bde7SBarry Smith /*MC
324b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
325b6a5bde7SBarry Smith 
326eca87e8dSBarry Smith    Synopsis:
327eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
328eca87e8dSBarry Smith 
329eca87e8dSBarry Smith    Not Collective
330eca87e8dSBarry Smith 
331b6a5bde7SBarry Smith    Input Parameter:
332b6a5bde7SBarry Smith .   v1 - the double
333b6a5bde7SBarry Smith 
334b6a5bde7SBarry Smith 
335b6a5bde7SBarry Smith    Level: beginner
336b6a5bde7SBarry Smith 
337b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
338b6a5bde7SBarry Smith 
339b6a5bde7SBarry Smith M*/
340f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
341b6a5bde7SBarry Smith 
342b6a5bde7SBarry Smith /*MC
343b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
344b6a5bde7SBarry Smith 
345b6a5bde7SBarry Smith    Synopsis:
346b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
347b6a5bde7SBarry Smith 
348eca87e8dSBarry Smith    Not Collective
349eca87e8dSBarry Smith 
350eca87e8dSBarry Smith    Input Parameter:
351eca87e8dSBarry Smith .   v1 - the value
352eca87e8dSBarry Smith 
353b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
354b6a5bde7SBarry Smith 
355b6a5bde7SBarry Smith    Level: beginner
356b6a5bde7SBarry Smith 
357b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
358b6a5bde7SBarry Smith 
359b6a5bde7SBarry Smith M*/
3604ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
361e489efc1SBarry Smith 
362314da920SBarry Smith /* ----------------------------------------------------------------------------*/
363314da920SBarry Smith /*
364d34fcf5fSBarry Smith      Basic constants
365314da920SBarry Smith */
366ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
367d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
368d34fcf5fSBarry Smith #elif defined(M_PI)
369d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
370d34fcf5fSBarry Smith #else
371faa6e9b0SMatthew G Knepley #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
372d34fcf5fSBarry Smith #endif
373d34fcf5fSBarry Smith 
374ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
37571fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
376ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
377ab824b78SBarry Smith #else
378ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
379ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
380ab824b78SBarry Smith #endif
381e489efc1SBarry Smith 
382ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
383ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
384ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
38582a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
38682a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
387cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
388ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
389ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
390ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
39182a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
39282a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
393cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
394ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
395ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
396ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
397d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
398d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
399d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
4009cf09972SJed Brown #endif
4013e523bebSBarry Smith 
402014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanScalar(PetscScalar);
403014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
4049a25a3ccSBarry Smith 
405314da920SBarry Smith /* ----------------------------------------------------------------------------*/
40687828ca2SBarry Smith #define PassiveReal   PetscReal
407ea709b57SSatish Balay #define PassiveScalar PetscScalar
408d3ecb3a7SBarry Smith 
40998725619SBarry Smith /*
41098725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
41198725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
41298725619SBarry Smith  */
41398725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
41498725619SBarry Smith typedef PetscScalar MatScalar;
41598725619SBarry Smith typedef PetscReal MatReal;
41698725619SBarry Smith 
4178ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
4188ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
4198ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
4208ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
4218ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
4228ad47952SJed Brown #else
4238ad47952SJed Brown #define MPIU_2INT MPI_2INT
4248ad47952SJed Brown #endif
425e9fa29b7SSatish Balay 
426e489efc1SBarry Smith #endif
427