xref: /petsc/include/petscmath.h (revision 4ebda54ed598c6ec2aeadd2270a04461e92576e4)
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 
26c80bc32dSSatish Balay #if defined (PETSC_HAVE_STD_COMPLEX)
27df9b3741SSatish Balay #include <complex>
28adc17e78SSatish Balay #else
29adc17e78SSatish Balay #include <complex.h>
30adc17e78SSatish Balay #endif
31adc17e78SSatish Balay 
32adc17e78SSatish Balay extern  MPI_Datatype        MPIU_COMPLEX;
33adc17e78SSatish Balay #define MPIU_SCALAR         MPIU_COMPLEX
343eda8832SBarry Smith #if defined(PETSC_USE_MAT_SINGLE)
353eda8832SBarry Smith #define MPIU_MATSCALAR        ??Notdone
363eda8832SBarry Smith #else
373eda8832SBarry Smith #define MPIU_MATSCALAR      MPIU_COMPLEX
383eda8832SBarry Smith #endif
393eda8832SBarry Smith 
40c80bc32dSSatish Balay #if defined (PETSC_HAVE_STD_COMPLEX)
41329f5518SBarry Smith #define PetscRealPart(a)        (a).real()
42329f5518SBarry Smith #define PetscImaginaryPart(a)   (a).imag()
433f6de6efSSatish Balay #define PetscAbsScalar(a)   std::abs(a)
443f6de6efSSatish Balay #define PetscConj(a)        std::conj(a)
4518a7d68fSSatish Balay #define PetscSqrtScalar(a)  std::sqrt(a)
46184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b)
47184914b5SBarry Smith #define PetscExpScalar(a)   std::exp(a)
48184914b5SBarry Smith #define PetscSinScalar(a)   std::sin(a)
49184914b5SBarry Smith #define PetscCosScalar(a)   std::cos(a)
50e489efc1SBarry Smith #else
51329f5518SBarry Smith #define PetscRealPart(a)        real(a)
52329f5518SBarry Smith #define PetscImaginaryPart(a)   imag(a)
53e489efc1SBarry Smith #define PetscAbsScalar(a)   abs(a)
54e489efc1SBarry Smith #define PetscConj(a)        conj(a)
5518a7d68fSSatish Balay #define PetscSqrtScalar(a)  sqrt(a)
56184914b5SBarry Smith #define PetscPowScalar(a,b) pow(a,b)
57184914b5SBarry Smith #define PetscExpScalar(a)   exp(a)
58184914b5SBarry Smith #define PetscSinScalar(a)   sin(a)
59184914b5SBarry Smith #define PetscCosScalar(a)   cos(a)
60adc17e78SSatish Balay #endif
61e489efc1SBarry Smith /*
62e489efc1SBarry Smith   The new complex class for GNU C++ is based on templates and is not backward
63e489efc1SBarry Smith   compatible with all previous complex class libraries.
64e489efc1SBarry Smith */
65c80bc32dSSatish Balay #if defined(PETSC_HAVE_STD_COMPLEX)
66ea709b57SSatish Balay   typedef std::complex<double> PetscScalar;
67aa482453SBarry Smith #elif defined(PETSC_HAVE_TEMPLATED_COMPLEX)
68ea709b57SSatish Balay   typedef complex<double> PetscScalar;
69e489efc1SBarry Smith #else
70ea709b57SSatish Balay   typedef complex PetscScalar;
71e489efc1SBarry Smith #endif
72e489efc1SBarry Smith 
73e489efc1SBarry Smith /* Compiling for real numbers only */
74e489efc1SBarry Smith #else
7587828ca2SBarry Smith #  if defined(PETSC_USE_SINGLE)
7687828ca2SBarry Smith #    define MPIU_SCALAR           MPI_FLOAT
7787828ca2SBarry Smith #  else
78e489efc1SBarry Smith #    define MPIU_SCALAR           MPI_DOUBLE
7987828ca2SBarry Smith #  endif
8087828ca2SBarry Smith #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
813eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_FLOAT
823eda8832SBarry Smith #  else
833eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_DOUBLE
843eda8832SBarry Smith #  endif
85329f5518SBarry Smith #  define PetscRealPart(a)      (a)
86329f5518SBarry Smith #  define PetscImaginaryPart(a) (a)
87e489efc1SBarry Smith #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
88e489efc1SBarry Smith #  define PetscConj(a)          (a)
8918a7d68fSSatish Balay #  define PetscSqrtScalar(a)    sqrt(a)
90184914b5SBarry Smith #  define PetscPowScalar(a,b)   pow(a,b)
91184914b5SBarry Smith #  define PetscExpScalar(a)     exp(a)
92184914b5SBarry Smith #  define PetscSinScalar(a)     sin(a)
93184914b5SBarry Smith #  define PetscCosScalar(a)     cos(a)
94b0a32e0cSBarry Smith 
95b0a32e0cSBarry Smith #  if defined(PETSC_USE_SINGLE)
96ea709b57SSatish Balay   typedef float PetscScalar;
97b0a32e0cSBarry Smith #  else
98ea709b57SSatish Balay   typedef double PetscScalar;
99b0a32e0cSBarry Smith #  endif
100e489efc1SBarry Smith #endif
101e489efc1SBarry Smith 
102d7d1e502SBarry Smith #if defined(PETSC_USE_SINGLE)
103d7d1e502SBarry Smith #  define MPIU_REAL   MPI_FLOAT
104d7d1e502SBarry Smith #else
105d7d1e502SBarry Smith #  define MPIU_REAL   MPI_DOUBLE
106d7d1e502SBarry Smith #endif
107d7d1e502SBarry Smith 
1083f1db9ecSBarry Smith /*
1093f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
1103f1db9ecSBarry Smith    single precision but all other objects still use double
1113f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
1123f1db9ecSBarry Smith    it remains double
1133f1db9ecSBarry Smith 
1143f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
1153f1db9ecSBarry Smith */
116aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1173f1db9ecSBarry Smith 
118ea709b57SSatish Balay typedef PetscScalar MatScalar;
119b400db4cSSatish Balay typedef double MatReal;
1203f1db9ecSBarry Smith 
12187828ca2SBarry Smith #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
1223f1db9ecSBarry Smith 
123b400db4cSSatish Balay typedef float MatScalar;
124b400db4cSSatish Balay typedef float MatReal;
1253f1db9ecSBarry Smith 
1263f1db9ecSBarry Smith #else
1273f1db9ecSBarry Smith 
128ea709b57SSatish Balay typedef PetscScalar MatScalar;
129b400db4cSSatish Balay typedef double MatReal;
1303f1db9ecSBarry Smith 
1313f1db9ecSBarry Smith #endif
1323f1db9ecSBarry Smith 
133329f5518SBarry Smith #if defined(PETSC_USE_SINGLE)
134b400db4cSSatish Balay   typedef float PetscReal;
135329f5518SBarry Smith #else
136b400db4cSSatish Balay   typedef double PetscReal;
137329f5518SBarry Smith #endif
1383f1db9ecSBarry Smith 
139314da920SBarry Smith /* --------------------------------------------------------------------------*/
140314da920SBarry Smith 
141e489efc1SBarry Smith /*
142e489efc1SBarry Smith    Certain objects may be created using either single
143e489efc1SBarry Smith   or double precision.
144e489efc1SBarry Smith */
145ea709b57SSatish Balay typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE } PetscScalarPrecision;
146e489efc1SBarry Smith 
147e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
148ea709b57SSatish Balay extern  PetscScalar       PETSC_i;
149e489efc1SBarry Smith 
150e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
151e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
152e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
153f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
154*4ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
155e489efc1SBarry Smith 
156314da920SBarry Smith /* ----------------------------------------------------------------------------*/
157314da920SBarry Smith /*
158314da920SBarry Smith      Basic constants
159314da920SBarry Smith */
160314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
161314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
162f10639e6SSatish Balay #define PETSC_MAX_INT            1000000000
163f10639e6SSatish Balay #define PETSC_MIN_INT            -1000000000
164e489efc1SBarry Smith 
16582adfdadSBarry Smith #if defined(PETSC_USE_SINGLE)
1667e032f8bSBarry Smith #  define PETSC_MAX                1.e30
1677e032f8bSBarry Smith #  define PETSC_MIN                -1.e30
168f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-7
169f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
170cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
17182adfdadSBarry Smith #else
1727e032f8bSBarry Smith #  define PETSC_MAX                1.e300
1737e032f8bSBarry Smith #  define PETSC_MIN                -1.e300
174f10639e6SSatish Balay #  define PETSC_MACHINE_EPSILON         1.e-14
175f10639e6SSatish Balay #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
176cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
17782adfdadSBarry Smith #endif
17882adfdadSBarry Smith 
179314da920SBarry Smith /* ----------------------------------------------------------------------------*/
180e489efc1SBarry Smith /*
181b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
182e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
183e489efc1SBarry Smith   timing etc.
184e489efc1SBarry Smith */
185b0a32e0cSBarry Smith typedef double PetscLogDouble;
186e489efc1SBarry Smith /*
187e489efc1SBarry Smith       Once PETSc is compiling with a ADIC enhanced version of MPI
188e489efc1SBarry Smith    we will create a new MPI_Datatype for the inactive double variables.
189e489efc1SBarry Smith */
190e489efc1SBarry Smith #if defined(AD_DERIV_H)
191b9617806SBarry Smith /* extern  MPI_Datatype  MPIU_PETSCLOGDOUBLE; */
192e489efc1SBarry Smith #else
193488ecbafSBarry Smith #if !defined(USING_MPIUNI)
194b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
195e489efc1SBarry Smith #endif
196e489efc1SBarry Smith #endif
197e489efc1SBarry Smith 
19887828ca2SBarry Smith #define PassiveReal PetscReal
199ea709b57SSatish Balay #define PassiveScalar PetscScalar
200d3ecb3a7SBarry Smith 
201b0a32e0cSBarry Smith #define PETSCMAP1_a(a,b)  a ## _ ## b
202b0a32e0cSBarry Smith #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
20387828ca2SBarry Smith #define PETSCMAP1(a)      PETSCMAP1_b(a,PetscScalar)
204e489efc1SBarry Smith #endif
205