xref: /petsc/include/petscmath.h (revision 4a60b67276c5ac83cc346a976d6de65582915772)
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)
30b7940d39SSatish Balay #if defined(PETSC_CLANGUAGE_CXX)
310bfd3fbfSBarry Smith /*
32b7940d39SSatish Balay    C++ support of complex numbers: Original support
330bfd3fbfSBarry Smith */
34df9b3741SSatish Balay #include <complex>
35adc17e78SSatish Balay 
36329f5518SBarry Smith #define PetscRealPart(a)        (a).real()
37329f5518SBarry Smith #define PetscImaginaryPart(a)   (a).imag()
383f6de6efSSatish Balay #define PetscAbsScalar(a)   std::abs(a)
393f6de6efSSatish Balay #define PetscConj(a)        std::conj(a)
4018a7d68fSSatish Balay #define PetscSqrtScalar(a)  std::sqrt(a)
41184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b)
42184914b5SBarry Smith #define PetscExpScalar(a)   std::exp(a)
43184914b5SBarry Smith #define PetscSinScalar(a)   std::sin(a)
44184914b5SBarry Smith #define PetscCosScalar(a)   std::cos(a)
450bfd3fbfSBarry Smith 
46*4a60b672SMatthew Knepley #if defined(PETSC_USE_SINGLE)
47*4a60b672SMatthew Knepley typedef std::complex<float> PetscScalar;
48*4a60b672SMatthew Knepley #elif defined(PETSC_USE_LONG_DOUBLE)
49*4a60b672SMatthew Knepley typedef std::complex<long double> PetscScalar;
50*4a60b672SMatthew Knepley #elif defined(PETSC_USE_INT)
51*4a60b672SMatthew Knepley typedef std::complex<int> PetscScalar;
52*4a60b672SMatthew Knepley #else
53ea709b57SSatish Balay typedef std::complex<double> PetscScalar;
54*4a60b672SMatthew Knepley #endif
55b7940d39SSatish Balay #else
56b7940d39SSatish Balay #include <complex.h>
57b7940d39SSatish Balay 
58b7940d39SSatish Balay /*
59b7940d39SSatish Balay    C support of complex numbers: Warning it needs a
60b7940d39SSatish Balay    C90 compliant compiler to work...
61b7940d39SSatish Balay  */
62b7940d39SSatish Balay 
63b7940d39SSatish Balay #define PetscRealPart(a)        creal(a)
64b7940d39SSatish Balay #define PetscImaginaryPart(a)   cimag(a)
65b7940d39SSatish Balay #define PetscAbsScalar(a)   cabs(a)
66b7940d39SSatish Balay #define PetscConj(a)        conj(a)
67b7940d39SSatish Balay #define PetscSqrtScalar(a)  csqrt(a)
68b7940d39SSatish Balay #define PetscPowScalar(a,b) cpow(a,b)
69b7940d39SSatish Balay #define PetscExpScalar(a)   cexp(a)
70b7940d39SSatish Balay #define PetscSinScalar(a)   csin(a)
71b7940d39SSatish Balay #define PetscCosScalar(a)   ccos(a)
72b7940d39SSatish Balay 
73*4a60b672SMatthew Knepley #if defined(PETSC_USE_SINGLE)
74*4a60b672SMatthew Knepley typedef float complex PetscScalar;
75*4a60b672SMatthew Knepley #elif defined(PETSC_USE_LONG_DOUBLE)
76*4a60b672SMatthew Knepley typedef long double complex PetscScalar;
77*4a60b672SMatthew Knepley #else
78b7940d39SSatish Balay typedef double complex PetscScalar;
79b7940d39SSatish Balay #endif
80*4a60b672SMatthew Knepley #endif
81e489efc1SBarry Smith 
82762437b8SSatish Balay extern  MPI_Datatype PETSC_DLLEXPORT MPIU_COMPLEX;
83762437b8SSatish Balay #define MPIU_SCALAR         MPIU_COMPLEX
84762437b8SSatish Balay #if defined(PETSC_USE_MAT_SINGLE)
85762437b8SSatish Balay #define MPIU_MATSCALAR        ??Notdone
86762437b8SSatish Balay #else
87762437b8SSatish Balay #define MPIU_MATSCALAR      MPIU_COMPLEX
88762437b8SSatish Balay #endif
89762437b8SSatish Balay 
90e489efc1SBarry Smith /* Compiling for real numbers only */
91e489efc1SBarry Smith #else
9287828ca2SBarry Smith #  if defined(PETSC_USE_SINGLE)
9387828ca2SBarry Smith #    define MPIU_SCALAR           MPI_FLOAT
94f68b968cSBarry Smith #  elif defined(PETSC_USE_LONG_DOUBLE)
95f68b968cSBarry Smith #    define MPIU_SCALAR           MPI_LONG_DOUBLE
9603c60df9SBarry Smith #  elif defined(PETSC_INT)
9703c60df9SBarry Smith #    define MPIU_INT              MPI_INT
9887828ca2SBarry Smith #  else
99e489efc1SBarry Smith #    define MPIU_SCALAR           MPI_DOUBLE
10087828ca2SBarry Smith #  endif
10187828ca2SBarry Smith #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
1023eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_FLOAT
103f68b968cSBarry Smith #  elif defined(PETSC_USE_LONG_DOUBLE)
104f68b968cSBarry Smith #    define MPIU_MATSCALAR        MPI_LONG_DOUBLE
10503c60df9SBarry Smith #  elif defined(PETSC_USE_INT)
10603c60df9SBarry Smith #    define MPIU_MATSCALAR        MPI_INT
1073eda8832SBarry Smith #  else
1083eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_DOUBLE
1093eda8832SBarry Smith #  endif
110329f5518SBarry Smith #  define PetscRealPart(a)      (a)
1119b0def1dSBarry Smith #  define PetscImaginaryPart(a) (0)
112e489efc1SBarry Smith #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
113e489efc1SBarry Smith #  define PetscConj(a)          (a)
11418a7d68fSSatish Balay #  define PetscSqrtScalar(a)    sqrt(a)
115184914b5SBarry Smith #  define PetscPowScalar(a,b)   pow(a,b)
116184914b5SBarry Smith #  define PetscExpScalar(a)     exp(a)
117184914b5SBarry Smith #  define PetscSinScalar(a)     sin(a)
118184914b5SBarry Smith #  define PetscCosScalar(a)     cos(a)
119b0a32e0cSBarry Smith 
120b0a32e0cSBarry Smith #  if defined(PETSC_USE_SINGLE)
121ea709b57SSatish Balay   typedef float PetscScalar;
122f68b968cSBarry Smith #  elif defined(PETSC_USE_LONG_DOUBLE)
123f68b968cSBarry Smith   typedef long double PetscScalar;
12403c60df9SBarry Smith #  elif defined(PETSC_USE_INT)
12503c60df9SBarry Smith   typedef int PetscScalar;
126b0a32e0cSBarry Smith #  else
127ea709b57SSatish Balay   typedef double PetscScalar;
128b0a32e0cSBarry Smith #  endif
129e489efc1SBarry Smith #endif
130e489efc1SBarry Smith 
131d7d1e502SBarry Smith #if defined(PETSC_USE_SINGLE)
132d7d1e502SBarry Smith #  define MPIU_REAL   MPI_FLOAT
133f68b968cSBarry Smith #elif defined(PETSC_USE_LONG_DOUBLE)
134f68b968cSBarry Smith #  define MPIU_REAL   MPI_LONG_DOUBLE
13503c60df9SBarry Smith #elif defined(PETSC_USE_INT)
13603c60df9SBarry Smith #  define MPIU_REAL   MPI_INT
137d7d1e502SBarry Smith #else
138d7d1e502SBarry Smith #  define MPIU_REAL   MPI_DOUBLE
139d7d1e502SBarry Smith #endif
140d7d1e502SBarry Smith 
141da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
14226aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
1433f1db9ecSBarry Smith /*
1443f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
1453f1db9ecSBarry Smith    single precision but all other objects still use double
1463f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
1473f1db9ecSBarry Smith    it remains double
1483f1db9ecSBarry Smith 
1493f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
1503f1db9ecSBarry Smith */
1513f1db9ecSBarry Smith 
15211380375SSatish Balay #if defined(PETSC_USE_MAT_SINGLE)
153b400db4cSSatish Balay typedef float MatScalar;
1543f1db9ecSBarry Smith #else
155ea709b57SSatish Balay typedef PetscScalar MatScalar;
15611380375SSatish Balay #endif
1573f1db9ecSBarry Smith 
158329f5518SBarry Smith #if defined(PETSC_USE_SINGLE)
159b400db4cSSatish Balay   typedef float PetscReal;
160f68b968cSBarry Smith #elif defined(PETSC_USE_LONG_DOUBLE)
161f68b968cSBarry Smith   typedef long double PetscReal;
16203c60df9SBarry Smith #elif defined(PETSC_USE_INT)
16303c60df9SBarry Smith   typedef int PetscReal;
164329f5518SBarry Smith #else
165b400db4cSSatish Balay   typedef double PetscReal;
166329f5518SBarry Smith #endif
1673f1db9ecSBarry Smith 
168f68b968cSBarry Smith #if defined(PETSC_USE_COMPLEX)
169f68b968cSBarry Smith typedef PetscReal MatReal;
170f68b968cSBarry Smith #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
171f68b968cSBarry Smith typedef float MatReal;
172f68b968cSBarry Smith #else
173f68b968cSBarry Smith typedef PetscReal MatReal;
174f68b968cSBarry Smith #endif
175f68b968cSBarry Smith 
176f68b968cSBarry Smith 
177314da920SBarry Smith /* --------------------------------------------------------------------------*/
178314da920SBarry Smith 
179e489efc1SBarry Smith /*
180e489efc1SBarry Smith    Certain objects may be created using either single
181e489efc1SBarry Smith   or double precision.
182e489efc1SBarry Smith */
183f68b968cSBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
184e489efc1SBarry Smith 
185e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
186ff73aad6SKris Buschelman extern  PetscScalar PETSC_DLLEXPORT PETSC_i;
187e489efc1SBarry Smith 
188b6a5bde7SBarry Smith /*MC
189b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
190b6a5bde7SBarry Smith 
191b6a5bde7SBarry Smith    Input Parameter:
192b6a5bde7SBarry Smith +  v1 - first value to find minimum of
193b6a5bde7SBarry Smith -  v2 - second value to find minimum of
194b6a5bde7SBarry Smith 
195b6a5bde7SBarry Smith    Synopsis:
196b6a5bde7SBarry Smith    type PetscMin(type v1,type v2)
197b6a5bde7SBarry Smith 
198b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
199b6a5bde7SBarry Smith 
200b6a5bde7SBarry Smith    Level: beginner
201b6a5bde7SBarry Smith 
202b6a5bde7SBarry Smith 
203b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
204b6a5bde7SBarry Smith 
205b6a5bde7SBarry Smith M*/
206e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
207b6a5bde7SBarry Smith 
208b6a5bde7SBarry Smith /*MC
209b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
210b6a5bde7SBarry Smith 
211b6a5bde7SBarry Smith    Input Parameter:
212b6a5bde7SBarry Smith +  v1 - first value to find maximum of
213b6a5bde7SBarry Smith -  v2 - second value to find maximum of
214b6a5bde7SBarry Smith 
215b6a5bde7SBarry Smith    Synopsis:
216b6a5bde7SBarry Smith    type max PetscMax(type v1,type v2)
217b6a5bde7SBarry Smith 
218b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
219b6a5bde7SBarry Smith 
220b6a5bde7SBarry Smith    Level: beginner
221b6a5bde7SBarry Smith 
222b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
223b6a5bde7SBarry Smith 
224b6a5bde7SBarry Smith M*/
225e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
226b6a5bde7SBarry Smith 
227b6a5bde7SBarry Smith /*MC
228b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
229b6a5bde7SBarry Smith 
230b6a5bde7SBarry Smith    Input Parameter:
231b6a5bde7SBarry Smith .   v1 - the integer
232b6a5bde7SBarry Smith 
233b6a5bde7SBarry Smith    Synopsis:
234b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
235b6a5bde7SBarry Smith 
236b6a5bde7SBarry Smith 
237b6a5bde7SBarry Smith    Level: beginner
238b6a5bde7SBarry Smith 
239b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
240b6a5bde7SBarry Smith 
241b6a5bde7SBarry Smith M*/
242e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
243b6a5bde7SBarry Smith 
244b6a5bde7SBarry Smith /*MC
245b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
246b6a5bde7SBarry Smith 
247b6a5bde7SBarry Smith    Input Parameter:
248b6a5bde7SBarry Smith .   v1 - the double
249b6a5bde7SBarry Smith 
250b6a5bde7SBarry Smith    Synopsis:
251b6a5bde7SBarry Smith    int abs PetscAbsReal(PetscReal v1)
252b6a5bde7SBarry Smith 
253b6a5bde7SBarry Smith 
254b6a5bde7SBarry Smith    Level: beginner
255b6a5bde7SBarry Smith 
256b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
257b6a5bde7SBarry Smith 
258b6a5bde7SBarry Smith M*/
259f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
260b6a5bde7SBarry Smith 
261b6a5bde7SBarry Smith /*MC
262b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
263b6a5bde7SBarry Smith 
264b6a5bde7SBarry Smith    Input Parameter:
265b6a5bde7SBarry Smith .   v1 - the value
266b6a5bde7SBarry Smith 
267b6a5bde7SBarry Smith    Synopsis:
268b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
269b6a5bde7SBarry Smith 
270b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
271b6a5bde7SBarry Smith 
272b6a5bde7SBarry Smith    Level: beginner
273b6a5bde7SBarry Smith 
274b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
275b6a5bde7SBarry Smith 
276b6a5bde7SBarry Smith M*/
2774ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
278e489efc1SBarry Smith 
279314da920SBarry Smith /* ----------------------------------------------------------------------------*/
280314da920SBarry Smith /*
28103c60df9SBarry Smith      Basic constants - These should be done much better
282314da920SBarry Smith */
283314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
284314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
285f10639e6SSatish Balay #define PETSC_MAX_INT            1000000000
286f10639e6SSatish Balay #define PETSC_MIN_INT            -1000000000
287e489efc1SBarry Smith 
28882adfdadSBarry Smith #if defined(PETSC_USE_SINGLE)
2897e032f8bSBarry Smith #  define PETSC_MAX                     1.e30
2907e032f8bSBarry Smith #  define PETSC_MIN                    -1.e30
291f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-7
292f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
293cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
29403c60df9SBarry Smith #elif defined(PETSC_USE_INT)
29503c60df9SBarry Smith #  define PETSC_MAX                     PETSC_MAX_INT
29603c60df9SBarry Smith #  define PETSC_MIN                     PETSC_MIN_INT
29703c60df9SBarry Smith #  define PETSC_MACHINE_EPSILON         1
29803c60df9SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1
29903c60df9SBarry Smith #  define PETSC_SMALL                   0
30082adfdadSBarry Smith #else
3017e032f8bSBarry Smith #  define PETSC_MAX                     1.e300
3027e032f8bSBarry Smith #  define PETSC_MIN                    -1.e300
303f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-14
304f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
305cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
30682adfdadSBarry Smith #endif
30782adfdadSBarry Smith 
308ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
309ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
310ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);
3113e523bebSBarry Smith 
3123e523bebSBarry Smith 
313314da920SBarry Smith /* ----------------------------------------------------------------------------*/
314e489efc1SBarry Smith /*
315b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
316e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
317e489efc1SBarry Smith   timing etc.
318e489efc1SBarry Smith */
319b0a32e0cSBarry Smith typedef double PetscLogDouble;
320b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
321e489efc1SBarry Smith 
32287828ca2SBarry Smith #define PassiveReal   PetscReal
323ea709b57SSatish Balay #define PassiveScalar PetscScalar
324d3ecb3a7SBarry Smith 
325e9fa29b7SSatish Balay 
326e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
327e489efc1SBarry Smith #endif
328