xref: /petsc/include/petscmath.h (revision 59cb59303c5b736eee18743cc6ac9d39c1220d1f)
173f4d377SMatthew Knepley /* $Id: petscmath.h,v 1.32 2001/08/30 20:37:06 bsmith Exp $ */
2e489efc1SBarry Smith /*
3314da920SBarry Smith 
4314da920SBarry Smith       PETSc mathematics include file. Defines certain basic mathematical
5314da920SBarry Smith     constants and functions for working with single and double precision
6314da920SBarry Smith     floating point numbers as well as complex and integers.
7314da920SBarry Smith 
8e7029fe1SSatish Balay     This file is included by petsc.h and should not be used directly.
9e7029fe1SSatish Balay 
10e489efc1SBarry Smith */
11e489efc1SBarry Smith 
12488ecbafSBarry Smith #if !defined(__PETSCMATH_H)
13488ecbafSBarry Smith #define __PETSCMATH_H
140a5f7794SBarry Smith #include <math.h>
150a5f7794SBarry Smith 
16b36a9721SBarry Smith extern  MPI_Datatype        MPIU_2SCALAR;
17314da920SBarry Smith /*
18f4ccad53SBarry Smith 
19f4ccad53SBarry Smith      Defines operations that are different for complex and real numbers;
20f4ccad53SBarry Smith    note that one cannot really mix the use of complex and real in the same
21f4ccad53SBarry Smith    PETSc program. All PETSc objects in one program are built around the object
22ea709b57SSatish Balay    PetscScalar which is either always a double or a complex.
23f4ccad53SBarry Smith 
24e489efc1SBarry Smith */
25b36a9721SBarry Smith 
26*59cb5930SBarry Smith #define PetscExpPassiveScalar(a) PetscExpScalar()
27*59cb5930SBarry Smith 
28aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
29adc17e78SSatish Balay 
300bfd3fbfSBarry Smith /*
310bfd3fbfSBarry Smith    PETSc now only supports std::complex
320bfd3fbfSBarry Smith */
33df9b3741SSatish Balay #include <complex>
34adc17e78SSatish Balay 
35adc17e78SSatish Balay extern  MPI_Datatype        MPIU_COMPLEX;
36adc17e78SSatish Balay #define MPIU_SCALAR         MPIU_COMPLEX
373eda8832SBarry Smith #if defined(PETSC_USE_MAT_SINGLE)
383eda8832SBarry Smith #define MPIU_MATSCALAR        ??Notdone
393eda8832SBarry Smith #else
403eda8832SBarry Smith #define MPIU_MATSCALAR      MPIU_COMPLEX
413eda8832SBarry Smith #endif
423eda8832SBarry Smith 
43329f5518SBarry Smith #define PetscRealPart(a)        (a).real()
44329f5518SBarry Smith #define PetscImaginaryPart(a)   (a).imag()
453f6de6efSSatish Balay #define PetscAbsScalar(a)   std::abs(a)
463f6de6efSSatish Balay #define PetscConj(a)        std::conj(a)
4718a7d68fSSatish Balay #define PetscSqrtScalar(a)  std::sqrt(a)
48184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b)
49184914b5SBarry Smith #define PetscExpScalar(a)   std::exp(a)
50184914b5SBarry Smith #define PetscSinScalar(a)   std::sin(a)
51184914b5SBarry Smith #define PetscCosScalar(a)   std::cos(a)
520bfd3fbfSBarry Smith 
53ea709b57SSatish Balay typedef std::complex<double> PetscScalar;
54e489efc1SBarry Smith 
55e489efc1SBarry Smith /* Compiling for real numbers only */
56e489efc1SBarry Smith #else
5787828ca2SBarry Smith #  if defined(PETSC_USE_SINGLE)
5887828ca2SBarry Smith #    define MPIU_SCALAR           MPI_FLOAT
5987828ca2SBarry Smith #  else
60e489efc1SBarry Smith #    define MPIU_SCALAR           MPI_DOUBLE
6187828ca2SBarry Smith #  endif
6287828ca2SBarry Smith #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
633eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_FLOAT
643eda8832SBarry Smith #  else
653eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_DOUBLE
663eda8832SBarry Smith #  endif
67329f5518SBarry Smith #  define PetscRealPart(a)      (a)
68329f5518SBarry Smith #  define PetscImaginaryPart(a) (a)
69e489efc1SBarry Smith #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
70e489efc1SBarry Smith #  define PetscConj(a)          (a)
7118a7d68fSSatish Balay #  define PetscSqrtScalar(a)    sqrt(a)
72184914b5SBarry Smith #  define PetscPowScalar(a,b)   pow(a,b)
73184914b5SBarry Smith #  define PetscExpScalar(a)     exp(a)
74184914b5SBarry Smith #  define PetscSinScalar(a)     sin(a)
75184914b5SBarry Smith #  define PetscCosScalar(a)     cos(a)
76b0a32e0cSBarry Smith 
77b0a32e0cSBarry Smith #  if defined(PETSC_USE_SINGLE)
78ea709b57SSatish Balay   typedef float PetscScalar;
79b0a32e0cSBarry Smith #  else
80ea709b57SSatish Balay   typedef double PetscScalar;
81b0a32e0cSBarry Smith #  endif
82e489efc1SBarry Smith #endif
83e489efc1SBarry Smith 
84d7d1e502SBarry Smith #if defined(PETSC_USE_SINGLE)
85d7d1e502SBarry Smith #  define MPIU_REAL   MPI_FLOAT
86d7d1e502SBarry Smith #else
87d7d1e502SBarry Smith #  define MPIU_REAL   MPI_DOUBLE
88d7d1e502SBarry Smith #endif
89d7d1e502SBarry Smith 
90da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
91da9b6338SBarry Smith #define PetscAbs(a)  (((a) >= 0) ? a : -a)
923f1db9ecSBarry Smith /*
933f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
943f1db9ecSBarry Smith    single precision but all other objects still use double
953f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
963f1db9ecSBarry Smith    it remains double
973f1db9ecSBarry Smith 
983f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
993f1db9ecSBarry Smith */
1003f1db9ecSBarry Smith 
10111380375SSatish Balay #if defined(PETSC_USE_MAT_SINGLE)
102b400db4cSSatish Balay typedef float MatScalar;
1033f1db9ecSBarry Smith #else
104ea709b57SSatish Balay typedef PetscScalar MatScalar;
10511380375SSatish Balay #endif
1063f1db9ecSBarry Smith 
10711380375SSatish Balay #if defined(PETSC_USE_COMPLEX)
10811380375SSatish Balay typedef double MatReal;
10911380375SSatish Balay #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
11011380375SSatish Balay typedef float MatReal;
11111380375SSatish Balay #else
11211380375SSatish Balay typedef double MatReal;
1133f1db9ecSBarry Smith #endif
1143f1db9ecSBarry Smith 
115329f5518SBarry Smith #if defined(PETSC_USE_SINGLE)
116b400db4cSSatish Balay   typedef float PetscReal;
117329f5518SBarry Smith #else
118b400db4cSSatish Balay   typedef double PetscReal;
119329f5518SBarry Smith #endif
1203f1db9ecSBarry Smith 
121314da920SBarry Smith /* --------------------------------------------------------------------------*/
122314da920SBarry Smith 
123e489efc1SBarry Smith /*
124e489efc1SBarry Smith    Certain objects may be created using either single
125e489efc1SBarry Smith   or double precision.
126e489efc1SBarry Smith */
127ea709b57SSatish Balay typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE } PetscScalarPrecision;
128e489efc1SBarry Smith 
129e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
130ea709b57SSatish Balay extern  PetscScalar       PETSC_i;
131e489efc1SBarry Smith 
132b6a5bde7SBarry Smith /*MC
133b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
134b6a5bde7SBarry Smith 
135b6a5bde7SBarry Smith    Input Parameter:
136b6a5bde7SBarry Smith +  v1 - first value to find minimum of
137b6a5bde7SBarry Smith -  v2 - second value to find minimum of
138b6a5bde7SBarry Smith 
139b6a5bde7SBarry Smith    Synopsis:
140b6a5bde7SBarry Smith    type PetscMin(type v1,type v2)
141b6a5bde7SBarry Smith 
142b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
143b6a5bde7SBarry Smith 
144b6a5bde7SBarry Smith    Level: beginner
145b6a5bde7SBarry Smith 
146b6a5bde7SBarry Smith 
147b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
148b6a5bde7SBarry Smith 
149b6a5bde7SBarry Smith M*/
150e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
151b6a5bde7SBarry Smith 
152b6a5bde7SBarry Smith /*MC
153b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
154b6a5bde7SBarry Smith 
155b6a5bde7SBarry Smith    Input Parameter:
156b6a5bde7SBarry Smith +  v1 - first value to find maximum of
157b6a5bde7SBarry Smith -  v2 - second value to find maximum of
158b6a5bde7SBarry Smith 
159b6a5bde7SBarry Smith    Synopsis:
160b6a5bde7SBarry Smith    type max PetscMax(type v1,type v2)
161b6a5bde7SBarry Smith 
162b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
163b6a5bde7SBarry Smith 
164b6a5bde7SBarry Smith    Level: beginner
165b6a5bde7SBarry Smith 
166b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
167b6a5bde7SBarry Smith 
168b6a5bde7SBarry Smith M*/
169e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
170b6a5bde7SBarry Smith 
171b6a5bde7SBarry Smith /*MC
172b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
173b6a5bde7SBarry Smith 
174b6a5bde7SBarry Smith    Input Parameter:
175b6a5bde7SBarry Smith .   v1 - the integer
176b6a5bde7SBarry Smith 
177b6a5bde7SBarry Smith    Synopsis:
178b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
179b6a5bde7SBarry Smith 
180b6a5bde7SBarry Smith 
181b6a5bde7SBarry Smith    Level: beginner
182b6a5bde7SBarry Smith 
183b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
184b6a5bde7SBarry Smith 
185b6a5bde7SBarry Smith M*/
186e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
187b6a5bde7SBarry Smith 
188b6a5bde7SBarry Smith /*MC
189b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
190b6a5bde7SBarry Smith 
191b6a5bde7SBarry Smith    Input Parameter:
192b6a5bde7SBarry Smith .   v1 - the double
193b6a5bde7SBarry Smith 
194b6a5bde7SBarry Smith    Synopsis:
195b6a5bde7SBarry Smith    int abs PetscAbsReal(PetscReal v1)
196b6a5bde7SBarry Smith 
197b6a5bde7SBarry Smith 
198b6a5bde7SBarry Smith    Level: beginner
199b6a5bde7SBarry Smith 
200b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
201b6a5bde7SBarry Smith 
202b6a5bde7SBarry Smith M*/
203f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
204b6a5bde7SBarry Smith 
205b6a5bde7SBarry Smith /*MC
206b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
207b6a5bde7SBarry Smith 
208b6a5bde7SBarry Smith    Input Parameter:
209b6a5bde7SBarry Smith .   v1 - the value
210b6a5bde7SBarry Smith 
211b6a5bde7SBarry Smith    Synopsis:
212b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
213b6a5bde7SBarry Smith 
214b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
215b6a5bde7SBarry Smith 
216b6a5bde7SBarry Smith    Level: beginner
217b6a5bde7SBarry Smith 
218b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
219b6a5bde7SBarry Smith 
220b6a5bde7SBarry Smith M*/
2214ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
222e489efc1SBarry Smith 
223314da920SBarry Smith /* ----------------------------------------------------------------------------*/
224314da920SBarry Smith /*
225314da920SBarry Smith      Basic constants
226314da920SBarry Smith */
227314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
228314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
229f10639e6SSatish Balay #define PETSC_MAX_INT            1000000000
230f10639e6SSatish Balay #define PETSC_MIN_INT            -1000000000
231e489efc1SBarry Smith 
23282adfdadSBarry Smith #if defined(PETSC_USE_SINGLE)
2337e032f8bSBarry Smith #  define PETSC_MAX                1.e30
2347e032f8bSBarry Smith #  define PETSC_MIN                -1.e30
235f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-7
236f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
237cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
23882adfdadSBarry Smith #else
2397e032f8bSBarry Smith #  define PETSC_MAX                1.e300
2407e032f8bSBarry Smith #  define PETSC_MIN                -1.e300
241f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-14
242f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
243cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
24482adfdadSBarry Smith #endif
24582adfdadSBarry Smith 
2462740c1caSMatthew Knepley extern int PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
2472740c1caSMatthew Knepley extern int PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
2483e523bebSBarry Smith extern int PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);
2493e523bebSBarry Smith 
2503e523bebSBarry Smith 
251314da920SBarry Smith /* ----------------------------------------------------------------------------*/
252e489efc1SBarry Smith /*
253b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
254e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
255e489efc1SBarry Smith   timing etc.
256e489efc1SBarry Smith */
257b0a32e0cSBarry Smith typedef double PetscLogDouble;
258e489efc1SBarry Smith /*
259e489efc1SBarry Smith       Once PETSc is compiling with a ADIC enhanced version of MPI
260e489efc1SBarry Smith    we will create a new MPI_Datatype for the inactive double variables.
261e489efc1SBarry Smith */
262e489efc1SBarry Smith #if defined(AD_DERIV_H)
263b9617806SBarry Smith /* extern  MPI_Datatype  MPIU_PETSCLOGDOUBLE; */
264e489efc1SBarry Smith #else
265f35a08a5SSatish Balay #if !defined(_petsc_mpi_uni)
266b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
267e489efc1SBarry Smith #endif
268e489efc1SBarry Smith #endif
269e489efc1SBarry Smith 
27087828ca2SBarry Smith #define PassiveReal   PetscReal
271ea709b57SSatish Balay #define PassiveScalar PetscScalar
272d3ecb3a7SBarry Smith 
273b0a32e0cSBarry Smith #define PETSCMAP1_a(a,b)  a ## _ ## b
274b0a32e0cSBarry Smith #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
27587828ca2SBarry Smith #define PETSCMAP1(a)      PETSCMAP1_b(a,PetscScalar)
276e489efc1SBarry Smith #endif
277