xref: /petsc/include/petscmath.h (revision ef66eb6987ddfdf4e414d6b820cbc8d8d7d17bc2)
1 /* $Id: petscmath.h,v 1.31 2001/08/10 18:59:16 balay Exp bsmith $ */
2 /*
3 
4       PETSc mathematics include file. Defines certain basic mathematical
5     constants and functions for working with single and double precision
6     floating point numbers as well as complex and integers.
7 
8     This file is included by petsc.h and should not be used directly.
9 
10 */
11 
12 #if !defined(__PETSCMATH_H)
13 #define __PETSCMATH_H
14 #include <math.h>
15 
16 /*
17 
18      Defines operations that are different for complex and real numbers;
19    note that one cannot really mix the use of complex and real in the same
20    PETSc program. All PETSc objects in one program are built around the object
21    PetscScalar which is either always a double or a complex.
22 
23 */
24 #if defined(PETSC_USE_COMPLEX)
25 
26 #if defined (PETSC_HAVE_STD_COMPLEX)
27 #include <complex>
28 #elif defined(PETSC_HAVE_NONSTANDARD_COMPLEX_H)
29 #include PETSC_HAVE_NONSTANDARD_COMPLEX_H
30 #else
31 #include <complex.h>
32 #endif
33 
34 extern  MPI_Datatype        MPIU_COMPLEX;
35 #define MPIU_SCALAR         MPIU_COMPLEX
36 #if defined(PETSC_USE_MAT_SINGLE)
37 #define MPIU_MATSCALAR        ??Notdone
38 #else
39 #define MPIU_MATSCALAR      MPIU_COMPLEX
40 #endif
41 
42 #if defined (PETSC_HAVE_STD_COMPLEX)
43 #define PetscRealPart(a)        (a).real()
44 #define PetscImaginaryPart(a)   (a).imag()
45 #define PetscAbsScalar(a)   std::abs(a)
46 #define PetscConj(a)        std::conj(a)
47 #define PetscSqrtScalar(a)  std::sqrt(a)
48 #define PetscPowScalar(a,b) std::pow(a,b)
49 #define PetscExpScalar(a)   std::exp(a)
50 #define PetscSinScalar(a)   std::sin(a)
51 #define PetscCosScalar(a)   std::cos(a)
52 #else
53 #define PetscRealPart(a)        real(a)
54 #define PetscImaginaryPart(a)   imag(a)
55 #define PetscAbsScalar(a)   abs(a)
56 #define PetscConj(a)        conj(a)
57 #define PetscSqrtScalar(a)  sqrt(a)
58 #define PetscPowScalar(a,b) pow(a,b)
59 #define PetscExpScalar(a)   exp(a)
60 #define PetscSinScalar(a)   sin(a)
61 #define PetscCosScalar(a)   cos(a)
62 #endif
63 /*
64   The new complex class for GNU C++ is based on templates and is not backward
65   compatible with all previous complex class libraries.
66 */
67 #if defined(PETSC_HAVE_STD_COMPLEX)
68   typedef std::complex<double> PetscScalar;
69 #elif defined(PETSC_HAVE_TEMPLATED_COMPLEX)
70   typedef complex<double> PetscScalar;
71 #else
72   typedef complex PetscScalar;
73 #endif
74 
75 /* Compiling for real numbers only */
76 #else
77 #  if defined(PETSC_USE_SINGLE)
78 #    define MPIU_SCALAR           MPI_FLOAT
79 #  else
80 #    define MPIU_SCALAR           MPI_DOUBLE
81 #  endif
82 #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
83 #    define MPIU_MATSCALAR        MPI_FLOAT
84 #  else
85 #    define MPIU_MATSCALAR        MPI_DOUBLE
86 #  endif
87 #  define PetscRealPart(a)      (a)
88 #  define PetscImaginaryPart(a) (a)
89 #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
90 #  define PetscConj(a)          (a)
91 #  define PetscSqrtScalar(a)    sqrt(a)
92 #  define PetscPowScalar(a,b)   pow(a,b)
93 #  define PetscExpScalar(a)     exp(a)
94 #  define PetscSinScalar(a)     sin(a)
95 #  define PetscCosScalar(a)     cos(a)
96 
97 #  if defined(PETSC_USE_SINGLE)
98   typedef float PetscScalar;
99 #  else
100   typedef double PetscScalar;
101 #  endif
102 #endif
103 
104 #if defined(PETSC_USE_SINGLE)
105 #  define MPIU_REAL   MPI_FLOAT
106 #else
107 #  define MPIU_REAL   MPI_DOUBLE
108 #endif
109 
110 /*
111        Allows compiling PETSc so that matrix values are stored in
112    single precision but all other objects still use double
113    precision. This does not work for complex numbers in that case
114    it remains double
115 
116           EXPERIMENTAL! NOT YET COMPLETELY WORKING
117 */
118 #if defined(PETSC_USE_COMPLEX)
119 
120 typedef PetscScalar MatScalar;
121 typedef double MatReal;
122 
123 #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
124 
125 typedef float MatScalar;
126 typedef float MatReal;
127 
128 #else
129 
130 typedef PetscScalar MatScalar;
131 typedef double MatReal;
132 
133 #endif
134 
135 #if defined(PETSC_USE_SINGLE)
136   typedef float PetscReal;
137 #else
138   typedef double PetscReal;
139 #endif
140 
141 /* --------------------------------------------------------------------------*/
142 
143 /*
144    Certain objects may be created using either single
145   or double precision.
146 */
147 typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE } PetscScalarPrecision;
148 
149 /* PETSC_i is the imaginary number, i */
150 extern  PetscScalar       PETSC_i;
151 
152 #define PetscMin(a,b)      (((a)<(b)) ? (a) : (b))
153 #define PetscMax(a,b)      (((a)<(b)) ? (b) : (a))
154 #define PetscAbsInt(a)     (((a)<0)   ? -(a) : (a))
155 #define PetscAbsReal(a)  (((a)<0)   ? -(a) : (a))
156 
157 /* ----------------------------------------------------------------------------*/
158 /*
159      Basic constants
160 */
161 #define PETSC_PI                 3.14159265358979323846264
162 #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
163 #define PETSC_MAX_INT            1000000000
164 #define PETSC_MIN_INT            -1000000000
165 
166 #if defined(PETSC_USE_SINGLE)
167 #  define PETSC_MAX                1.e30
168 #  define PETSC_MIN                -1.e30
169 #  define PETSC_MACHINE_EPSILON         1.e-7
170 #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
171 #  define PETSC_SMALL                   1.e-5
172 #else
173 #  define PETSC_MAX                1.e300
174 #  define PETSC_MIN                -1.e300
175 #  define PETSC_MACHINE_EPSILON         1.e-14
176 #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
177 #  define PETSC_SMALL                   1.e-10
178 #endif
179 
180 /* ----------------------------------------------------------------------------*/
181 /*
182     PetscLogDouble variables are used to contain double precision numbers
183   that are not used in the numerical computations, but rather in logging,
184   timing etc.
185 */
186 typedef double PetscLogDouble;
187 /*
188       Once PETSc is compiling with a ADIC enhanced version of MPI
189    we will create a new MPI_Datatype for the inactive double variables.
190 */
191 #if defined(AD_DERIV_H)
192 /* extern  MPI_Datatype  MPIU_PETSCLOGDOUBLE; */
193 #else
194 #if !defined(USING_MPIUNI)
195 #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
196 #endif
197 #endif
198 
199 #define PassiveReal PetscReal
200 #define PassiveScalar PetscScalar
201 
202 #define PETSCMAP1_a(a,b)  a ## _ ## b
203 #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
204 #define PETSCMAP1(a)      PETSCMAP1_b(a,PetscScalar)
205 #endif
206