xref: /petsc/include/petscmath.h (revision da9b633845058fbb0d803e5bd383961f2538530b)
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 
16314da920SBarry Smith /*
17f4ccad53SBarry Smith 
18f4ccad53SBarry Smith      Defines operations that are different for complex and real numbers;
19f4ccad53SBarry Smith    note that one cannot really mix the use of complex and real in the same
20f4ccad53SBarry Smith    PETSc program. All PETSc objects in one program are built around the object
21ea709b57SSatish Balay    PetscScalar which is either always a double or a complex.
22f4ccad53SBarry Smith 
23e489efc1SBarry Smith */
24aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
25adc17e78SSatish Balay 
260bfd3fbfSBarry Smith /*
270bfd3fbfSBarry Smith    PETSc now only supports std::complex
280bfd3fbfSBarry Smith */
29df9b3741SSatish Balay #include <complex>
30adc17e78SSatish Balay 
31adc17e78SSatish Balay extern  MPI_Datatype        MPIU_COMPLEX;
32adc17e78SSatish Balay #define MPIU_SCALAR         MPIU_COMPLEX
333eda8832SBarry Smith #if defined(PETSC_USE_MAT_SINGLE)
343eda8832SBarry Smith #define MPIU_MATSCALAR        ??Notdone
353eda8832SBarry Smith #else
363eda8832SBarry Smith #define MPIU_MATSCALAR      MPIU_COMPLEX
373eda8832SBarry Smith #endif
383eda8832SBarry Smith 
39329f5518SBarry Smith #define PetscRealPart(a)        (a).real()
40329f5518SBarry Smith #define PetscImaginaryPart(a)   (a).imag()
413f6de6efSSatish Balay #define PetscAbsScalar(a)   std::abs(a)
423f6de6efSSatish Balay #define PetscConj(a)        std::conj(a)
4318a7d68fSSatish Balay #define PetscSqrtScalar(a)  std::sqrt(a)
44184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b)
45184914b5SBarry Smith #define PetscExpScalar(a)   std::exp(a)
46184914b5SBarry Smith #define PetscSinScalar(a)   std::sin(a)
47184914b5SBarry Smith #define PetscCosScalar(a)   std::cos(a)
480bfd3fbfSBarry Smith 
49ea709b57SSatish Balay typedef std::complex<double> PetscScalar;
50e489efc1SBarry Smith 
51e489efc1SBarry Smith /* Compiling for real numbers only */
52e489efc1SBarry Smith #else
5387828ca2SBarry Smith #  if defined(PETSC_USE_SINGLE)
5487828ca2SBarry Smith #    define MPIU_SCALAR           MPI_FLOAT
5587828ca2SBarry Smith #  else
56e489efc1SBarry Smith #    define MPIU_SCALAR           MPI_DOUBLE
5787828ca2SBarry Smith #  endif
5887828ca2SBarry Smith #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
593eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_FLOAT
603eda8832SBarry Smith #  else
613eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_DOUBLE
623eda8832SBarry Smith #  endif
63329f5518SBarry Smith #  define PetscRealPart(a)      (a)
64329f5518SBarry Smith #  define PetscImaginaryPart(a) (a)
65e489efc1SBarry Smith #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
66e489efc1SBarry Smith #  define PetscConj(a)          (a)
6718a7d68fSSatish Balay #  define PetscSqrtScalar(a)    sqrt(a)
68184914b5SBarry Smith #  define PetscPowScalar(a,b)   pow(a,b)
69184914b5SBarry Smith #  define PetscExpScalar(a)     exp(a)
70184914b5SBarry Smith #  define PetscSinScalar(a)     sin(a)
71184914b5SBarry Smith #  define PetscCosScalar(a)     cos(a)
72b0a32e0cSBarry Smith 
73b0a32e0cSBarry Smith #  if defined(PETSC_USE_SINGLE)
74ea709b57SSatish Balay   typedef float PetscScalar;
75b0a32e0cSBarry Smith #  else
76ea709b57SSatish Balay   typedef double PetscScalar;
77b0a32e0cSBarry Smith #  endif
78e489efc1SBarry Smith #endif
79e489efc1SBarry Smith 
80d7d1e502SBarry Smith #if defined(PETSC_USE_SINGLE)
81d7d1e502SBarry Smith #  define MPIU_REAL   MPI_FLOAT
82d7d1e502SBarry Smith #else
83d7d1e502SBarry Smith #  define MPIU_REAL   MPI_DOUBLE
84d7d1e502SBarry Smith #endif
85d7d1e502SBarry Smith 
86*da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
87*da9b6338SBarry Smith #define PetscAbs(a)  (((a) >= 0) ? a : -a)
883f1db9ecSBarry Smith /*
893f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
903f1db9ecSBarry Smith    single precision but all other objects still use double
913f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
923f1db9ecSBarry Smith    it remains double
933f1db9ecSBarry Smith 
943f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
953f1db9ecSBarry Smith */
963f1db9ecSBarry Smith 
9711380375SSatish Balay #if defined(PETSC_USE_MAT_SINGLE)
98b400db4cSSatish Balay typedef float MatScalar;
993f1db9ecSBarry Smith #else
100ea709b57SSatish Balay typedef PetscScalar MatScalar;
10111380375SSatish Balay #endif
1023f1db9ecSBarry Smith 
10311380375SSatish Balay #if defined(PETSC_USE_COMPLEX)
10411380375SSatish Balay typedef double MatReal;
10511380375SSatish Balay #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
10611380375SSatish Balay typedef float MatReal;
10711380375SSatish Balay #else
10811380375SSatish Balay typedef double MatReal;
1093f1db9ecSBarry Smith #endif
1103f1db9ecSBarry Smith 
111329f5518SBarry Smith #if defined(PETSC_USE_SINGLE)
112b400db4cSSatish Balay   typedef float PetscReal;
113329f5518SBarry Smith #else
114b400db4cSSatish Balay   typedef double PetscReal;
115329f5518SBarry Smith #endif
1163f1db9ecSBarry Smith 
117314da920SBarry Smith /* --------------------------------------------------------------------------*/
118314da920SBarry Smith 
119e489efc1SBarry Smith /*
120e489efc1SBarry Smith    Certain objects may be created using either single
121e489efc1SBarry Smith   or double precision.
122e489efc1SBarry Smith */
123ea709b57SSatish Balay typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE } PetscScalarPrecision;
124e489efc1SBarry Smith 
125e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
126ea709b57SSatish Balay extern  PetscScalar       PETSC_i;
127e489efc1SBarry Smith 
128e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
129e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
130e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
131f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
1324ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
133e489efc1SBarry Smith 
134314da920SBarry Smith /* ----------------------------------------------------------------------------*/
135314da920SBarry Smith /*
136314da920SBarry Smith      Basic constants
137314da920SBarry Smith */
138314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
139314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
140f10639e6SSatish Balay #define PETSC_MAX_INT            1000000000
141f10639e6SSatish Balay #define PETSC_MIN_INT            -1000000000
142e489efc1SBarry Smith 
14382adfdadSBarry Smith #if defined(PETSC_USE_SINGLE)
1447e032f8bSBarry Smith #  define PETSC_MAX                1.e30
1457e032f8bSBarry Smith #  define PETSC_MIN                -1.e30
146f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-7
147f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
148cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
14982adfdadSBarry Smith #else
1507e032f8bSBarry Smith #  define PETSC_MAX                1.e300
1517e032f8bSBarry Smith #  define PETSC_MIN                -1.e300
152f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-14
153f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
154cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
15582adfdadSBarry Smith #endif
15682adfdadSBarry Smith 
1572740c1caSMatthew Knepley extern int PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
1582740c1caSMatthew Knepley extern int PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
1593e523bebSBarry Smith extern int PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);
1603e523bebSBarry Smith 
1613e523bebSBarry Smith 
162314da920SBarry Smith /* ----------------------------------------------------------------------------*/
163e489efc1SBarry Smith /*
164b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
165e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
166e489efc1SBarry Smith   timing etc.
167e489efc1SBarry Smith */
168b0a32e0cSBarry Smith typedef double PetscLogDouble;
169e489efc1SBarry Smith /*
170e489efc1SBarry Smith       Once PETSc is compiling with a ADIC enhanced version of MPI
171e489efc1SBarry Smith    we will create a new MPI_Datatype for the inactive double variables.
172e489efc1SBarry Smith */
173e489efc1SBarry Smith #if defined(AD_DERIV_H)
174b9617806SBarry Smith /* extern  MPI_Datatype  MPIU_PETSCLOGDOUBLE; */
175e489efc1SBarry Smith #else
176f35a08a5SSatish Balay #if !defined(_petsc_mpi_uni)
177b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
178e489efc1SBarry Smith #endif
179e489efc1SBarry Smith #endif
180e489efc1SBarry Smith 
18187828ca2SBarry Smith #define PassiveReal PetscReal
182ea709b57SSatish Balay #define PassiveScalar PetscScalar
183d3ecb3a7SBarry Smith 
184b0a32e0cSBarry Smith #define PETSCMAP1_a(a,b)  a ## _ ## b
185b0a32e0cSBarry Smith #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
18687828ca2SBarry Smith #define PETSCMAP1(a)      PETSCMAP1_b(a,PetscScalar)
187e489efc1SBarry Smith #endif
188