xref: /petsc/include/petscmath.h (revision ab824b7887c7ada6d48dfd202a36a066073b50ec)
1e489efc1SBarry Smith /*
2314da920SBarry Smith 
3314da920SBarry Smith       PETSc mathematics include file. Defines certain basic mathematical
4314da920SBarry Smith     constants and functions for working with single and double precision
5314da920SBarry Smith     floating point numbers as well as complex and integers.
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>
14e9fa29b7SSatish Balay PETSC_EXTERN_CXX_BEGIN
150a5f7794SBarry Smith 
167087cfbeSBarry Smith extern  MPI_Datatype  MPIU_2SCALAR;
177087cfbeSBarry Smith extern  MPI_Datatype  MPIU_2INT;
18c90a1750SBarry Smith 
19314da920SBarry Smith /*
20f4ccad53SBarry Smith 
21f4ccad53SBarry Smith      Defines operations that are different for complex and real numbers;
22f4ccad53SBarry Smith    note that one cannot really mix the use of complex and real in the same
23f4ccad53SBarry Smith    PETSc program. All PETSc objects in one program are built around the object
2498725619SBarry Smith    PetscScalar which is either always a real or a complex.
25f4ccad53SBarry Smith 
26e489efc1SBarry Smith */
27b36a9721SBarry Smith 
2859cb5930SBarry Smith #define PetscExpPassiveScalar(a) PetscExpScalar()
29c1d390e3SJed Brown #if defined(PETSC_USE_REAL_SINGLE)
30c1d390e3SJed Brown #define MPIU_REAL   MPI_FLOAT
31c1d390e3SJed Brown typedef float PetscReal;
328f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(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)
37c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
38c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
39c1d390e3SJed Brown typedef __float128 PetscReal;
408f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
41c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
4259cb5930SBarry Smith 
431093a601SBarry Smith /*
441093a601SBarry Smith     Complex number definitions
451093a601SBarry Smith  */
46aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
47b7940d39SSatish Balay #if defined(PETSC_CLANGUAGE_CXX)
481093a601SBarry Smith /* C++ support of complex number */
49df9b3741SSatish Balay #include <complex>
50adc17e78SSatish Balay 
51329f5518SBarry Smith #define PetscRealPart(a)      (a).real()
52329f5518SBarry Smith #define PetscImaginaryPart(a) (a).imag()
533f6de6efSSatish Balay #define PetscAbsScalar(a)     std::abs(a)
543f6de6efSSatish Balay #define PetscConj(a)          std::conj(a)
5518a7d68fSSatish Balay #define PetscSqrtScalar(a)    std::sqrt(a)
56184914b5SBarry Smith #define PetscPowScalar(a,b)   std::pow(a,b)
57184914b5SBarry Smith #define PetscExpScalar(a)     std::exp(a)
5806c1185fSBarry Smith #define PetscLogScalar(a)     std::log(a)
59184914b5SBarry Smith #define PetscSinScalar(a)     std::sin(a)
60184914b5SBarry Smith #define PetscCosScalar(a)     std::cos(a)
610bfd3fbfSBarry Smith 
62ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
634a60b672SMatthew Knepley typedef std::complex<float> PetscScalar;
64ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
651093a601SBarry Smith typedef std::complex<double> PetscScalar;
66ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
67b7940d39SSatish Balay 
681b65fc54SMatthew G Knepley #else /* PETSC_CLANGUAGE_CXX */
691093a601SBarry Smith /*  C support of complex numbers: Requires C99 compliant compiler*/
701093a601SBarry Smith #include <complex.h>
71b7940d39SSatish Balay 
72ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
7385b47369SMatthew Knepley typedef float complex PetscScalar;
7485b47369SMatthew Knepley 
7585b47369SMatthew Knepley #define PetscRealPart(a)      crealf(a)
7685b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagf(a)
7785b47369SMatthew Knepley #define PetscAbsScalar(a)     cabsf(a)
7885b47369SMatthew Knepley #define PetscConj(a)          conjf(a)
7985b47369SMatthew Knepley #define PetscSqrtScalar(a)    csqrtf(a)
8085b47369SMatthew Knepley #define PetscPowScalar(a,b)   cpowf(a,b)
8185b47369SMatthew Knepley #define PetscExpScalar(a)     cexpf(a)
8206c1185fSBarry Smith #define PetscLogScalar(a)     clogf(a)
8385b47369SMatthew Knepley #define PetscSinScalar(a)     csinf(a)
8485b47369SMatthew Knepley #define PetscCosScalar(a)     ccosf(a)
851093a601SBarry Smith 
86ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
871093a601SBarry Smith typedef double complex PetscScalar;
881093a601SBarry Smith 
891093a601SBarry Smith #define PetscRealPart(a)      creal(a)
901093a601SBarry Smith #define PetscImaginaryPart(a) cimag(a)
911093a601SBarry Smith #define PetscAbsScalar(a)     cabs(a)
921093a601SBarry Smith #define PetscConj(a)          conj(a)
931093a601SBarry Smith #define PetscSqrtScalar(a)    csqrt(a)
941093a601SBarry Smith #define PetscPowScalar(a,b)   cpow(a,b)
951093a601SBarry Smith #define PetscExpScalar(a)     cexp(a)
961093a601SBarry Smith #define PetscLogScalar(a)     clog(a)
971093a601SBarry Smith #define PetscSinScalar(a)     csin(a)
981093a601SBarry Smith #define PetscCosScalar(a)     ccos(a)
991093a601SBarry Smith 
100ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
1011b65fc54SMatthew G Knepley #endif /* PETSC_CLANGUAGE_CXX */
102e489efc1SBarry Smith 
1032c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
104500d8756SSatish Balay extern  MPI_Datatype  MPIU_C_DOUBLE_COMPLEX;
105500d8756SSatish Balay extern  MPI_Datatype  MPIU_C_COMPLEX;
106500d8756SSatish Balay #else
107500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
108500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
1091b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
1102c876bd9SBarry Smith 
111ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
112500d8756SSatish Balay #define MPIU_SCALAR MPIU_C_COMPLEX
113ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
114500d8756SSatish Balay #define MPIU_SCALAR MPIU_C_DOUBLE_COMPLEX
115ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
11675567043SBarry Smith 
1171093a601SBarry Smith /*
1181093a601SBarry Smith     real number definitions
1191093a601SBarry Smith  */
1201b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
121ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
12287828ca2SBarry Smith #define MPIU_SCALAR           MPI_FLOAT
1231093a601SBarry Smith typedef float PetscScalar;
124ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
1251093a601SBarry Smith #define MPIU_SCALAR           MPI_DOUBLE
1261093a601SBarry Smith typedef double PetscScalar;
127ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
128c90a1750SBarry Smith extern MPI_Datatype MPIU___FLOAT128;
129c90a1750SBarry Smith #define MPIU_SCALAR MPIU___FLOAT128
1300d0cc1b5SBarry Smith typedef __float128 PetscScalar;
131ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
132329f5518SBarry Smith #define PetscRealPart(a)      (a)
133c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
134c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
135e489efc1SBarry Smith #define PetscConj(a)          (a)
136ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
13718a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
138184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
139184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
14006c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
141184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
142184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
143ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
1440d0cc1b5SBarry Smith #include <quadmath.h>
1450d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
1460d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
1470d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
1480d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
1490d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
1500d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
151ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
152b0a32e0cSBarry Smith 
1531b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
154e489efc1SBarry Smith 
155da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
15626aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
1573f1db9ecSBarry Smith 
158314da920SBarry Smith /* --------------------------------------------------------------------------*/
159314da920SBarry Smith 
160e489efc1SBarry Smith /*
161f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
162f22f69f0SBarry Smith    This is currently not used.
163e489efc1SBarry Smith */
164557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
165e489efc1SBarry Smith 
166e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
1677087cfbeSBarry Smith extern  PetscScalar  PETSC_i;
168e489efc1SBarry Smith 
169b6a5bde7SBarry Smith /*MC
170b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
171b6a5bde7SBarry Smith 
172eca87e8dSBarry Smith    Synopsis:
173eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
174eca87e8dSBarry Smith 
175eca87e8dSBarry Smith    Not Collective
176eca87e8dSBarry Smith 
177b6a5bde7SBarry Smith    Input Parameter:
178b6a5bde7SBarry Smith +  v1 - first value to find minimum of
179b6a5bde7SBarry Smith -  v2 - second value to find minimum of
180b6a5bde7SBarry Smith 
181b6a5bde7SBarry Smith 
182b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
183b6a5bde7SBarry Smith 
184b6a5bde7SBarry Smith    Level: beginner
185b6a5bde7SBarry Smith 
186b6a5bde7SBarry Smith 
187b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
188b6a5bde7SBarry Smith 
189b6a5bde7SBarry Smith M*/
190e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
191b6a5bde7SBarry Smith 
192b6a5bde7SBarry Smith /*MC
193b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
194b6a5bde7SBarry Smith 
195eca87e8dSBarry Smith    Synopsis:
196eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
197eca87e8dSBarry Smith 
198eca87e8dSBarry Smith    Not Collective
199eca87e8dSBarry Smith 
200b6a5bde7SBarry Smith    Input Parameter:
201b6a5bde7SBarry Smith +  v1 - first value to find maximum of
202b6a5bde7SBarry Smith -  v2 - second value to find maximum of
203b6a5bde7SBarry Smith 
204b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
205b6a5bde7SBarry Smith 
206b6a5bde7SBarry Smith    Level: beginner
207b6a5bde7SBarry Smith 
208b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
209b6a5bde7SBarry Smith 
210b6a5bde7SBarry Smith M*/
211e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
212b6a5bde7SBarry Smith 
213b6a5bde7SBarry Smith /*MC
214b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
215b6a5bde7SBarry Smith 
216b6a5bde7SBarry Smith    Synopsis:
217b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
218b6a5bde7SBarry Smith 
219eca87e8dSBarry Smith    Not Collective
220eca87e8dSBarry Smith 
221eca87e8dSBarry Smith    Input Parameter:
222eca87e8dSBarry Smith .   v1 - the integer
223b6a5bde7SBarry Smith 
224b6a5bde7SBarry Smith    Level: beginner
225b6a5bde7SBarry Smith 
226b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
227b6a5bde7SBarry Smith 
228b6a5bde7SBarry Smith M*/
229e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
230b6a5bde7SBarry Smith 
231b6a5bde7SBarry Smith /*MC
232b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
233b6a5bde7SBarry Smith 
234eca87e8dSBarry Smith    Synopsis:
235eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
236eca87e8dSBarry Smith 
237eca87e8dSBarry Smith    Not Collective
238eca87e8dSBarry Smith 
239b6a5bde7SBarry Smith    Input Parameter:
240b6a5bde7SBarry Smith .   v1 - the double
241b6a5bde7SBarry Smith 
242b6a5bde7SBarry Smith 
243b6a5bde7SBarry Smith    Level: beginner
244b6a5bde7SBarry Smith 
245b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
246b6a5bde7SBarry Smith 
247b6a5bde7SBarry Smith M*/
248f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
249b6a5bde7SBarry Smith 
250b6a5bde7SBarry Smith /*MC
251b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
252b6a5bde7SBarry Smith 
253b6a5bde7SBarry Smith    Synopsis:
254b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
255b6a5bde7SBarry Smith 
256eca87e8dSBarry Smith    Not Collective
257eca87e8dSBarry Smith 
258eca87e8dSBarry Smith    Input Parameter:
259eca87e8dSBarry Smith .   v1 - the value
260eca87e8dSBarry Smith 
261b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
262b6a5bde7SBarry Smith 
263b6a5bde7SBarry Smith    Level: beginner
264b6a5bde7SBarry Smith 
265b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
266b6a5bde7SBarry Smith 
267b6a5bde7SBarry Smith M*/
2684ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
269e489efc1SBarry Smith 
270314da920SBarry Smith /* ----------------------------------------------------------------------------*/
271314da920SBarry Smith /*
272d34fcf5fSBarry Smith      Basic constants
273314da920SBarry Smith */
274ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
275d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
276d34fcf5fSBarry Smith #elif defined(M_PI)
277d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
278d34fcf5fSBarry Smith #else
279314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
280d34fcf5fSBarry Smith #endif
281d34fcf5fSBarry Smith 
282*ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
28371fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
284*ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
285*ab824b78SBarry Smith #else
286*ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
287*ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
288*ab824b78SBarry Smith #endif
289e489efc1SBarry Smith 
290ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
291*ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
292ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
29382a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
29482a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
295cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
296ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
297*ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
298ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
29982a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
30082a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
301cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
302ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
303ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
304ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
305d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
306d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
307d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
30882adfdadSBarry Smith #endif
30982adfdadSBarry Smith 
3109cf09972SJed Brown #if defined PETSC_HAVE_ADIC
3119cf09972SJed Brown /* Use MPI_Allreduce when ADIC is not available. */
3127087cfbeSBarry Smith extern PetscErrorCode  PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*);
3137087cfbeSBarry Smith extern PetscErrorCode  PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*);
3147087cfbeSBarry Smith extern PetscErrorCode  PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*);
3159cf09972SJed Brown #endif
3163e523bebSBarry Smith 
317a3630638SSatish Balay extern PetscErrorCode PetscIsInfOrNanScalar(PetscScalar);
318a3630638SSatish Balay extern PetscErrorCode PetscIsInfOrNanReal(PetscReal);
3199a25a3ccSBarry Smith 
320314da920SBarry Smith /* ----------------------------------------------------------------------------*/
321e489efc1SBarry Smith /*
322b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
323e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
324e489efc1SBarry Smith   timing etc.
325e489efc1SBarry Smith */
326b0a32e0cSBarry Smith typedef double PetscLogDouble;
327b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
328e489efc1SBarry Smith 
32987828ca2SBarry Smith #define PassiveReal   PetscReal
330ea709b57SSatish Balay #define PassiveScalar PetscScalar
331d3ecb3a7SBarry Smith 
33298725619SBarry Smith /*
33398725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
33498725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
33598725619SBarry Smith  */
33698725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
33798725619SBarry Smith typedef PetscScalar MatScalar;
33898725619SBarry Smith typedef PetscReal MatReal;
33998725619SBarry Smith 
340e9fa29b7SSatish Balay 
341e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
342e489efc1SBarry Smith #endif
343