xref: /petsc/include/petscmath.h (revision ea709b57ddb8cc304f11df2e9a39d6a8fdb28b53)
1*ea709b57SSatish Balay /* $Id: petscmath.h,v 1.25 2001/08/06 21:19:20 bsmith Exp balay $ */
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
21*ea709b57SSatish 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>
28aa482453SBarry Smith #elif defined(PETSC_HAVE_NONSTANDARD_COMPLEX_H)
29aa482453SBarry Smith #include PETSC_HAVE_NONSTANDARD_COMPLEX_H
30adc17e78SSatish Balay #else
31adc17e78SSatish Balay #include <complex.h>
32adc17e78SSatish Balay #endif
33adc17e78SSatish Balay 
34adc17e78SSatish Balay extern  MPI_Datatype        MPIU_COMPLEX;
35adc17e78SSatish Balay #define MPIU_SCALAR         MPIU_COMPLEX
363eda8832SBarry Smith #if defined(PETSC_USE_MAT_SINGLE)
373eda8832SBarry Smith #define MPIU_MATSCALAR        ??Notdone
383eda8832SBarry Smith #else
393eda8832SBarry Smith #define MPIU_MATSCALAR      MPIU_COMPLEX
403eda8832SBarry Smith #endif
413eda8832SBarry Smith 
42c80bc32dSSatish Balay #if defined (PETSC_HAVE_STD_COMPLEX)
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)
52e489efc1SBarry Smith #else
53329f5518SBarry Smith #define PetscRealPart(a)        real(a)
54329f5518SBarry Smith #define PetscImaginaryPart(a)   imag(a)
55e489efc1SBarry Smith #define PetscAbsScalar(a)   abs(a)
56e489efc1SBarry Smith #define PetscConj(a)        conj(a)
5718a7d68fSSatish Balay #define PetscSqrtScalar(a)  sqrt(a)
58184914b5SBarry Smith #define PetscPowScalar(a,b) pow(a,b)
59184914b5SBarry Smith #define PetscExpScalar(a)   exp(a)
60184914b5SBarry Smith #define PetscSinScalar(a)   sin(a)
61184914b5SBarry Smith #define PetscCosScalar(a)   cos(a)
62adc17e78SSatish Balay #endif
63e489efc1SBarry Smith /*
64e489efc1SBarry Smith   The new complex class for GNU C++ is based on templates and is not backward
65e489efc1SBarry Smith   compatible with all previous complex class libraries.
66e489efc1SBarry Smith */
67c80bc32dSSatish Balay #if defined(PETSC_HAVE_STD_COMPLEX)
68*ea709b57SSatish Balay   typedef std::complex<double> PetscScalar;
69aa482453SBarry Smith #elif defined(PETSC_HAVE_TEMPLATED_COMPLEX)
70*ea709b57SSatish Balay   typedef complex<double> PetscScalar;
71e489efc1SBarry Smith #else
72*ea709b57SSatish Balay   typedef complex PetscScalar;
73e489efc1SBarry Smith #endif
74e489efc1SBarry Smith 
75e489efc1SBarry Smith /* Compiling for real numbers only */
76e489efc1SBarry Smith #else
7787828ca2SBarry Smith #  if defined(PETSC_USE_SINGLE)
7887828ca2SBarry Smith #    define MPIU_SCALAR           MPI_FLOAT
7987828ca2SBarry Smith #  else
80e489efc1SBarry Smith #    define MPIU_SCALAR           MPI_DOUBLE
8187828ca2SBarry Smith #  endif
8287828ca2SBarry Smith #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
833eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_FLOAT
843eda8832SBarry Smith #  else
853eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_DOUBLE
863eda8832SBarry Smith #  endif
87329f5518SBarry Smith #  define PetscRealPart(a)      (a)
88329f5518SBarry Smith #  define PetscImaginaryPart(a) (a)
89e489efc1SBarry Smith #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
90e489efc1SBarry Smith #  define PetscConj(a)          (a)
9118a7d68fSSatish Balay #  define PetscSqrtScalar(a)    sqrt(a)
92184914b5SBarry Smith #  define PetscPowScalar(a,b)   pow(a,b)
93184914b5SBarry Smith #  define PetscExpScalar(a)     exp(a)
94184914b5SBarry Smith #  define PetscSinScalar(a)     sin(a)
95184914b5SBarry Smith #  define PetscCosScalar(a)     cos(a)
96b0a32e0cSBarry Smith 
97b0a32e0cSBarry Smith #  if defined(PETSC_USE_SINGLE)
98*ea709b57SSatish Balay   typedef float PetscScalar;
99b0a32e0cSBarry Smith #  else
100*ea709b57SSatish Balay   typedef double PetscScalar;
101b0a32e0cSBarry Smith #  endif
102e489efc1SBarry Smith #endif
103e489efc1SBarry Smith 
1043f1db9ecSBarry Smith /*
1053f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
1063f1db9ecSBarry Smith    single precision but all other objects still use double
1073f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
1083f1db9ecSBarry Smith    it remains double
1093f1db9ecSBarry Smith 
1103f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
1113f1db9ecSBarry Smith */
112aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1133f1db9ecSBarry Smith 
114*ea709b57SSatish Balay typedef PetscScalar MatScalar;
115b400db4cSSatish Balay typedef double MatReal;
1163f1db9ecSBarry Smith 
11787828ca2SBarry Smith #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
1183f1db9ecSBarry Smith 
119b400db4cSSatish Balay typedef float MatScalar;
120b400db4cSSatish Balay typedef float MatReal;
1213f1db9ecSBarry Smith 
1223f1db9ecSBarry Smith #else
1233f1db9ecSBarry Smith 
124*ea709b57SSatish Balay typedef PetscScalar MatScalar;
125b400db4cSSatish Balay typedef double MatReal;
1263f1db9ecSBarry Smith 
1273f1db9ecSBarry Smith #endif
1283f1db9ecSBarry Smith 
129329f5518SBarry Smith #if defined(PETSC_USE_SINGLE)
130b400db4cSSatish Balay   typedef float PetscReal;
131329f5518SBarry Smith #else
132b400db4cSSatish Balay   typedef double PetscReal;
133329f5518SBarry Smith #endif
1343f1db9ecSBarry Smith 
135314da920SBarry Smith /* --------------------------------------------------------------------------*/
136314da920SBarry Smith 
137e489efc1SBarry Smith /*
138e489efc1SBarry Smith    Certain objects may be created using either single
139e489efc1SBarry Smith   or double precision.
140e489efc1SBarry Smith */
141*ea709b57SSatish Balay typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE } PetscScalarPrecision;
142e489efc1SBarry Smith 
143e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
144*ea709b57SSatish Balay extern  PetscScalar       PETSC_i;
145e489efc1SBarry Smith 
146e489efc1SBarry Smith #define PetscMin(a,b)      (((a)<(b)) ? (a) : (b))
147e489efc1SBarry Smith #define PetscMax(a,b)      (((a)<(b)) ? (b) : (a))
148e489efc1SBarry Smith #define PetscAbsInt(a)     (((a)<0)   ? -(a) : (a))
149e489efc1SBarry Smith #define PetscAbsDouble(a)  (((a)<0)   ? -(a) : (a))
150e489efc1SBarry Smith 
151314da920SBarry Smith /* ----------------------------------------------------------------------------*/
152314da920SBarry Smith /*
153314da920SBarry Smith      Basic constants
154314da920SBarry Smith */
155314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
156314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
157e489efc1SBarry Smith #define PETSC_MAX                1.e300
158e489efc1SBarry Smith #define PETSC_MIN                -1.e300
1590a5f7794SBarry Smith #define PETSC_MAX_INT            1000000000;
1600a5f7794SBarry Smith #define PETSC_MIN_INT            -1000000000;
161e489efc1SBarry 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
176488ecbafSBarry Smith #if !defined(USING_MPIUNI)
177b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
178e489efc1SBarry Smith #endif
179e489efc1SBarry Smith #endif
180e489efc1SBarry Smith 
18187828ca2SBarry Smith #define PassiveReal PetscReal
182*ea709b57SSatish 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