xref: /petsc/include/petscmath.h (revision 03c60df9f85d61e692827a30bb417e0ec54001a2)
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 
7e7029fe1SSatish Balay     This file is included by petsc.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 
16ff73aad6SKris Buschelman extern  MPI_Datatype PETSC_DLLEXPORT MPIU_2SCALAR;
17ff73aad6SKris Buschelman extern  MPI_Datatype PETSC_DLLEXPORT MPIU_2INT;
18314da920SBarry Smith /*
19f4ccad53SBarry Smith 
20f4ccad53SBarry Smith      Defines operations that are different for complex and real numbers;
21f4ccad53SBarry Smith    note that one cannot really mix the use of complex and real in the same
22f4ccad53SBarry Smith    PETSc program. All PETSc objects in one program are built around the object
23ea709b57SSatish Balay    PetscScalar which is either always a double or a complex.
24f4ccad53SBarry Smith 
25e489efc1SBarry Smith */
26b36a9721SBarry Smith 
2759cb5930SBarry Smith #define PetscExpPassiveScalar(a) PetscExpScalar()
2859cb5930SBarry Smith 
29aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
30adc17e78SSatish Balay 
310bfd3fbfSBarry Smith /*
320bfd3fbfSBarry Smith    PETSc now only supports std::complex
330bfd3fbfSBarry Smith */
34df9b3741SSatish Balay #include <complex>
35adc17e78SSatish Balay 
36ff73aad6SKris Buschelman extern  MPI_Datatype PETSC_DLLEXPORT MPIU_COMPLEX;
37adc17e78SSatish Balay #define MPIU_SCALAR         MPIU_COMPLEX
383eda8832SBarry Smith #if defined(PETSC_USE_MAT_SINGLE)
393eda8832SBarry Smith #define MPIU_MATSCALAR        ??Notdone
403eda8832SBarry Smith #else
413eda8832SBarry Smith #define MPIU_MATSCALAR      MPIU_COMPLEX
423eda8832SBarry Smith #endif
433eda8832SBarry Smith 
44329f5518SBarry Smith #define PetscRealPart(a)        (a).real()
45329f5518SBarry Smith #define PetscImaginaryPart(a)   (a).imag()
463f6de6efSSatish Balay #define PetscAbsScalar(a)   std::abs(a)
473f6de6efSSatish Balay #define PetscConj(a)        std::conj(a)
4818a7d68fSSatish Balay #define PetscSqrtScalar(a)  std::sqrt(a)
49184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b)
50184914b5SBarry Smith #define PetscExpScalar(a)   std::exp(a)
51184914b5SBarry Smith #define PetscSinScalar(a)   std::sin(a)
52184914b5SBarry Smith #define PetscCosScalar(a)   std::cos(a)
530bfd3fbfSBarry Smith 
54ea709b57SSatish Balay typedef std::complex<double> PetscScalar;
55e489efc1SBarry Smith 
56e489efc1SBarry Smith /* Compiling for real numbers only */
57e489efc1SBarry Smith #else
5887828ca2SBarry Smith #  if defined(PETSC_USE_SINGLE)
5987828ca2SBarry Smith #    define MPIU_SCALAR           MPI_FLOAT
60f68b968cSBarry Smith #  elif defined(PETSC_USE_LONG_DOUBLE)
61f68b968cSBarry Smith #    define MPIU_SCALAR           MPI_LONG_DOUBLE
62*03c60df9SBarry Smith #  elif defined(PETSC_INT)
63*03c60df9SBarry Smith #    define MPIU_INT              MPI_INT
6487828ca2SBarry Smith #  else
65e489efc1SBarry Smith #    define MPIU_SCALAR           MPI_DOUBLE
6687828ca2SBarry Smith #  endif
6787828ca2SBarry Smith #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
683eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_FLOAT
69f68b968cSBarry Smith #  elif defined(PETSC_USE_LONG_DOUBLE)
70f68b968cSBarry Smith #    define MPIU_MATSCALAR        MPI_LONG_DOUBLE
71*03c60df9SBarry Smith #  elif defined(PETSC_USE_INT)
72*03c60df9SBarry Smith #    define MPIU_MATSCALAR        MPI_INT
733eda8832SBarry Smith #  else
743eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_DOUBLE
753eda8832SBarry Smith #  endif
76329f5518SBarry Smith #  define PetscRealPart(a)      (a)
779b0def1dSBarry Smith #  define PetscImaginaryPart(a) (0)
78e489efc1SBarry Smith #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
79e489efc1SBarry Smith #  define PetscConj(a)          (a)
8018a7d68fSSatish Balay #  define PetscSqrtScalar(a)    sqrt(a)
81184914b5SBarry Smith #  define PetscPowScalar(a,b)   pow(a,b)
82184914b5SBarry Smith #  define PetscExpScalar(a)     exp(a)
83184914b5SBarry Smith #  define PetscSinScalar(a)     sin(a)
84184914b5SBarry Smith #  define PetscCosScalar(a)     cos(a)
85b0a32e0cSBarry Smith 
86b0a32e0cSBarry Smith #  if defined(PETSC_USE_SINGLE)
87ea709b57SSatish Balay   typedef float PetscScalar;
88f68b968cSBarry Smith #  elif defined(PETSC_USE_LONG_DOUBLE)
89f68b968cSBarry Smith   typedef long double PetscScalar;
90*03c60df9SBarry Smith #  elif defined(PETSC_USE_INT)
91*03c60df9SBarry Smith   typedef int PetscScalar;
92b0a32e0cSBarry Smith #  else
93ea709b57SSatish Balay   typedef double PetscScalar;
94b0a32e0cSBarry Smith #  endif
95e489efc1SBarry Smith #endif
96e489efc1SBarry Smith 
97d7d1e502SBarry Smith #if defined(PETSC_USE_SINGLE)
98d7d1e502SBarry Smith #  define MPIU_REAL   MPI_FLOAT
99f68b968cSBarry Smith #elif defined(PETSC_USE_LONG_DOUBLE)
100f68b968cSBarry Smith #  define MPIU_REAL   MPI_LONG_DOUBLE
101*03c60df9SBarry Smith #elif defined(PETSC_USE_INT)
102*03c60df9SBarry Smith #  define MPIU_REAL   MPI_INT
103d7d1e502SBarry Smith #else
104d7d1e502SBarry Smith #  define MPIU_REAL   MPI_DOUBLE
105d7d1e502SBarry Smith #endif
106d7d1e502SBarry Smith 
107da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
10826aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
1093f1db9ecSBarry Smith /*
1103f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
1113f1db9ecSBarry Smith    single precision but all other objects still use double
1123f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
1133f1db9ecSBarry Smith    it remains double
1143f1db9ecSBarry Smith 
1153f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
1163f1db9ecSBarry Smith */
1173f1db9ecSBarry Smith 
11811380375SSatish Balay #if defined(PETSC_USE_MAT_SINGLE)
119b400db4cSSatish Balay typedef float MatScalar;
1203f1db9ecSBarry Smith #else
121ea709b57SSatish Balay typedef PetscScalar MatScalar;
12211380375SSatish Balay #endif
1233f1db9ecSBarry Smith 
124329f5518SBarry Smith #if defined(PETSC_USE_SINGLE)
125b400db4cSSatish Balay   typedef float PetscReal;
126f68b968cSBarry Smith #elif defined(PETSC_USE_LONG_DOUBLE)
127f68b968cSBarry Smith   typedef long double PetscReal;
128*03c60df9SBarry Smith #elif defined(PETSC_USE_INT)
129*03c60df9SBarry Smith   typedef int PetscReal;
130329f5518SBarry Smith #else
131b400db4cSSatish Balay   typedef double PetscReal;
132329f5518SBarry Smith #endif
1333f1db9ecSBarry Smith 
134f68b968cSBarry Smith #if defined(PETSC_USE_COMPLEX)
135f68b968cSBarry Smith typedef PetscReal MatReal;
136f68b968cSBarry Smith #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
137f68b968cSBarry Smith typedef float MatReal;
138f68b968cSBarry Smith #else
139f68b968cSBarry Smith typedef PetscReal MatReal;
140f68b968cSBarry Smith #endif
141f68b968cSBarry Smith 
142f68b968cSBarry Smith 
143314da920SBarry Smith /* --------------------------------------------------------------------------*/
144314da920SBarry Smith 
145e489efc1SBarry Smith /*
146e489efc1SBarry Smith    Certain objects may be created using either single
147e489efc1SBarry Smith   or double precision.
148e489efc1SBarry Smith */
149f68b968cSBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
150e489efc1SBarry Smith 
151e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
152ff73aad6SKris Buschelman extern  PetscScalar PETSC_DLLEXPORT PETSC_i;
153e489efc1SBarry Smith 
154b6a5bde7SBarry Smith /*MC
155b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
156b6a5bde7SBarry Smith 
157b6a5bde7SBarry Smith    Input Parameter:
158b6a5bde7SBarry Smith +  v1 - first value to find minimum of
159b6a5bde7SBarry Smith -  v2 - second value to find minimum of
160b6a5bde7SBarry Smith 
161b6a5bde7SBarry Smith    Synopsis:
162b6a5bde7SBarry Smith    type PetscMin(type v1,type v2)
163b6a5bde7SBarry Smith 
164b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
165b6a5bde7SBarry Smith 
166b6a5bde7SBarry Smith    Level: beginner
167b6a5bde7SBarry Smith 
168b6a5bde7SBarry Smith 
169b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
170b6a5bde7SBarry Smith 
171b6a5bde7SBarry Smith M*/
172e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
173b6a5bde7SBarry Smith 
174b6a5bde7SBarry Smith /*MC
175b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
176b6a5bde7SBarry Smith 
177b6a5bde7SBarry Smith    Input Parameter:
178b6a5bde7SBarry Smith +  v1 - first value to find maximum of
179b6a5bde7SBarry Smith -  v2 - second value to find maximum of
180b6a5bde7SBarry Smith 
181b6a5bde7SBarry Smith    Synopsis:
182b6a5bde7SBarry Smith    type max PetscMax(type v1,type v2)
183b6a5bde7SBarry Smith 
184b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
185b6a5bde7SBarry Smith 
186b6a5bde7SBarry Smith    Level: beginner
187b6a5bde7SBarry Smith 
188b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
189b6a5bde7SBarry Smith 
190b6a5bde7SBarry Smith M*/
191e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
192b6a5bde7SBarry Smith 
193b6a5bde7SBarry Smith /*MC
194b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
195b6a5bde7SBarry Smith 
196b6a5bde7SBarry Smith    Input Parameter:
197b6a5bde7SBarry Smith .   v1 - the integer
198b6a5bde7SBarry Smith 
199b6a5bde7SBarry Smith    Synopsis:
200b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
201b6a5bde7SBarry Smith 
202b6a5bde7SBarry Smith 
203b6a5bde7SBarry Smith    Level: beginner
204b6a5bde7SBarry Smith 
205b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
206b6a5bde7SBarry Smith 
207b6a5bde7SBarry Smith M*/
208e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
209b6a5bde7SBarry Smith 
210b6a5bde7SBarry Smith /*MC
211b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
212b6a5bde7SBarry Smith 
213b6a5bde7SBarry Smith    Input Parameter:
214b6a5bde7SBarry Smith .   v1 - the double
215b6a5bde7SBarry Smith 
216b6a5bde7SBarry Smith    Synopsis:
217b6a5bde7SBarry Smith    int abs PetscAbsReal(PetscReal v1)
218b6a5bde7SBarry Smith 
219b6a5bde7SBarry Smith 
220b6a5bde7SBarry Smith    Level: beginner
221b6a5bde7SBarry Smith 
222b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
223b6a5bde7SBarry Smith 
224b6a5bde7SBarry Smith M*/
225f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
226b6a5bde7SBarry Smith 
227b6a5bde7SBarry Smith /*MC
228b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
229b6a5bde7SBarry Smith 
230b6a5bde7SBarry Smith    Input Parameter:
231b6a5bde7SBarry Smith .   v1 - the value
232b6a5bde7SBarry Smith 
233b6a5bde7SBarry Smith    Synopsis:
234b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
235b6a5bde7SBarry Smith 
236b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
237b6a5bde7SBarry Smith 
238b6a5bde7SBarry Smith    Level: beginner
239b6a5bde7SBarry Smith 
240b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
241b6a5bde7SBarry Smith 
242b6a5bde7SBarry Smith M*/
2434ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
244e489efc1SBarry Smith 
245314da920SBarry Smith /* ----------------------------------------------------------------------------*/
246314da920SBarry Smith /*
247*03c60df9SBarry Smith      Basic constants - These should be done much better
248314da920SBarry Smith */
249314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
250314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
251f10639e6SSatish Balay #define PETSC_MAX_INT            1000000000
252f10639e6SSatish Balay #define PETSC_MIN_INT            -1000000000
253e489efc1SBarry Smith 
25482adfdadSBarry Smith #if defined(PETSC_USE_SINGLE)
2557e032f8bSBarry Smith #  define PETSC_MAX                     1.e30
2567e032f8bSBarry Smith #  define PETSC_MIN                    -1.e30
257f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-7
258f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
259cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
260*03c60df9SBarry Smith #elif defined(PETSC_USE_INT)
261*03c60df9SBarry Smith #  define PETSC_MAX                     PETSC_MAX_INT
262*03c60df9SBarry Smith #  define PETSC_MIN                     PETSC_MIN_INT
263*03c60df9SBarry Smith #  define PETSC_MACHINE_EPSILON         1
264*03c60df9SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1
265*03c60df9SBarry Smith #  define PETSC_SMALL                   0
26682adfdadSBarry Smith #else
2677e032f8bSBarry Smith #  define PETSC_MAX                     1.e300
2687e032f8bSBarry Smith #  define PETSC_MIN                    -1.e300
269f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-14
270f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
271cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
27282adfdadSBarry Smith #endif
27382adfdadSBarry Smith 
274ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
275ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
276ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);
2773e523bebSBarry Smith 
2783e523bebSBarry Smith 
279314da920SBarry Smith /* ----------------------------------------------------------------------------*/
280e489efc1SBarry Smith /*
281b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
282e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
283e489efc1SBarry Smith   timing etc.
284e489efc1SBarry Smith */
285b0a32e0cSBarry Smith typedef double PetscLogDouble;
286e489efc1SBarry Smith /*
287e489efc1SBarry Smith       Once PETSc is compiling with a ADIC enhanced version of MPI
288e489efc1SBarry Smith    we will create a new MPI_Datatype for the inactive double variables.
289e489efc1SBarry Smith */
290e489efc1SBarry Smith #if defined(AD_DERIV_H)
291b9617806SBarry Smith /* extern  MPI_Datatype  MPIU_PETSCLOGDOUBLE; */
292e489efc1SBarry Smith #else
293f35a08a5SSatish Balay #if !defined(_petsc_mpi_uni)
294b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
295e489efc1SBarry Smith #endif
296e489efc1SBarry Smith #endif
297e489efc1SBarry Smith 
29887828ca2SBarry Smith #define PassiveReal   PetscReal
299ea709b57SSatish Balay #define PassiveScalar PetscScalar
300d3ecb3a7SBarry Smith 
301b0a32e0cSBarry Smith #define PETSCMAP1_a(a,b)  a ## _ ## b
302b0a32e0cSBarry Smith #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
30387828ca2SBarry Smith #define PETSCMAP1(a)      PETSCMAP1_b(a,PetscScalar)
304e9fa29b7SSatish Balay 
305e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
306e489efc1SBarry Smith #endif
307