xref: /petsc/include/petscmath.h (revision 2cfcea2917e9827eeaf4fa9c2e8dd75b054ced7b)
1 /*
2 
3       PETSc mathematics include file. Defines certain basic mathematical
4     constants and functions for working with single and double precision
5     floating point numbers as well as complex and integers.
6 
7     This file is included by petsc.h and should not be used directly.
8 
9 */
10 
11 #if !defined(__PETSCMATH_H)
12 #define __PETSCMATH_H
13 #include <math.h>
14 PETSC_EXTERN_CXX_BEGIN
15 
16 extern  MPI_Datatype PETSC_DLLEXPORT MPIU_2SCALAR;
17 extern  MPI_Datatype PETSC_DLLEXPORT MPIU_2INT;
18 /*
19 
20      Defines operations that are different for complex and real numbers;
21    note that one cannot really mix the use of complex and real in the same
22    PETSc program. All PETSc objects in one program are built around the object
23    PetscScalar which is either always a double or a complex.
24 
25 */
26 
27 #define PetscExpPassiveScalar(a) PetscExpScalar()
28 
29 #if defined(PETSC_USE_COMPLEX)
30 
31 /*
32    PETSc now only supports std::complex
33 */
34 #include <complex>
35 
36 extern  MPI_Datatype PETSC_DLLEXPORT MPIU_COMPLEX;
37 #define MPIU_SCALAR         MPIU_COMPLEX
38 #if defined(PETSC_USE_MAT_SINGLE)
39 #define MPIU_MATSCALAR        ??Notdone
40 #else
41 #define MPIU_MATSCALAR      MPIU_COMPLEX
42 #endif
43 
44 #define PetscRealPart(a)        (a).real()
45 #define PetscImaginaryPart(a)   (a).imag()
46 #define PetscAbsScalar(a)   std::abs(a)
47 #define PetscConj(a)        std::conj(a)
48 #define PetscSqrtScalar(a)  std::sqrt(a)
49 #define PetscPowScalar(a,b) std::pow(a,b)
50 #define PetscExpScalar(a)   std::exp(a)
51 #define PetscSinScalar(a)   std::sin(a)
52 #define PetscCosScalar(a)   std::cos(a)
53 
54 typedef std::complex<double> PetscScalar;
55 
56 /* Compiling for real numbers only */
57 #else
58 #  if defined(PETSC_USE_SINGLE)
59 #    define MPIU_SCALAR           MPI_FLOAT
60 #  elif defined(PETSC_USE_LONG_DOUBLE)
61 #    define MPIU_SCALAR           MPI_LONG_DOUBLE
62 #  elif defined(PETSC_INT)
63 #    define MPIU_INT              MPI_INT
64 #  else
65 #    define MPIU_SCALAR           MPI_DOUBLE
66 #  endif
67 #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
68 #    define MPIU_MATSCALAR        MPI_FLOAT
69 #  elif defined(PETSC_USE_LONG_DOUBLE)
70 #    define MPIU_MATSCALAR        MPI_LONG_DOUBLE
71 #  elif defined(PETSC_USE_INT)
72 #    define MPIU_MATSCALAR        MPI_INT
73 #  else
74 #    define MPIU_MATSCALAR        MPI_DOUBLE
75 #  endif
76 #  define PetscRealPart(a)      (a)
77 #  define PetscImaginaryPart(a) (0)
78 #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
79 #  define PetscConj(a)          (a)
80 #  define PetscSqrtScalar(a)    sqrt(a)
81 #  define PetscPowScalar(a,b)   pow(a,b)
82 #  define PetscExpScalar(a)     exp(a)
83 #  define PetscSinScalar(a)     sin(a)
84 #  define PetscCosScalar(a)     cos(a)
85 
86 #  if defined(PETSC_USE_SINGLE)
87   typedef float PetscScalar;
88 #  elif defined(PETSC_USE_LONG_DOUBLE)
89   typedef long double PetscScalar;
90 #  elif defined(PETSC_USE_INT)
91   typedef int PetscScalar;
92 #  else
93   typedef double PetscScalar;
94 #  endif
95 #endif
96 
97 #if defined(PETSC_USE_SINGLE)
98 #  define MPIU_REAL   MPI_FLOAT
99 #elif defined(PETSC_USE_LONG_DOUBLE)
100 #  define MPIU_REAL   MPI_LONG_DOUBLE
101 #elif defined(PETSC_USE_INT)
102 #  define MPIU_REAL   MPI_INT
103 #else
104 #  define MPIU_REAL   MPI_DOUBLE
105 #endif
106 
107 #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
108 #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
109 /*
110        Allows compiling PETSc so that matrix values are stored in
111    single precision but all other objects still use double
112    precision. This does not work for complex numbers in that case
113    it remains double
114 
115           EXPERIMENTAL! NOT YET COMPLETELY WORKING
116 */
117 
118 #if defined(PETSC_USE_MAT_SINGLE)
119 typedef float MatScalar;
120 #else
121 typedef PetscScalar MatScalar;
122 #endif
123 
124 #if defined(PETSC_USE_SINGLE)
125   typedef float PetscReal;
126 #elif defined(PETSC_USE_LONG_DOUBLE)
127   typedef long double PetscReal;
128 #elif defined(PETSC_USE_INT)
129   typedef int PetscReal;
130 #else
131   typedef double PetscReal;
132 #endif
133 
134 #if defined(PETSC_USE_COMPLEX)
135 typedef PetscReal MatReal;
136 #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
137 typedef float MatReal;
138 #else
139 typedef PetscReal MatReal;
140 #endif
141 
142 
143 /* --------------------------------------------------------------------------*/
144 
145 /*
146    Certain objects may be created using either single
147   or double precision.
148 */
149 typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
150 
151 /* PETSC_i is the imaginary number, i */
152 extern  PetscScalar PETSC_DLLEXPORT PETSC_i;
153 
154 /*MC
155    PetscMin - Returns minimum of two numbers
156 
157    Input Parameter:
158 +  v1 - first value to find minimum of
159 -  v2 - second value to find minimum of
160 
161    Synopsis:
162    type PetscMin(type v1,type v2)
163 
164    Notes: type can be integer or floating point value
165 
166    Level: beginner
167 
168 
169 .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
170 
171 M*/
172 #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
173 
174 /*MC
175    PetscMax - Returns maxium of two numbers
176 
177    Input Parameter:
178 +  v1 - first value to find maximum of
179 -  v2 - second value to find maximum of
180 
181    Synopsis:
182    type max PetscMax(type v1,type v2)
183 
184    Notes: type can be integer or floating point value
185 
186    Level: beginner
187 
188 .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
189 
190 M*/
191 #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
192 
193 /*MC
194    PetscAbsInt - Returns the absolute value of an integer
195 
196    Input Parameter:
197 .   v1 - the integer
198 
199    Synopsis:
200    int abs PetscAbsInt(int v1)
201 
202 
203    Level: beginner
204 
205 .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
206 
207 M*/
208 #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
209 
210 /*MC
211    PetscAbsReal - Returns the absolute value of an real number
212 
213    Input Parameter:
214 .   v1 - the double
215 
216    Synopsis:
217    int abs PetscAbsReal(PetscReal v1)
218 
219 
220    Level: beginner
221 
222 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
223 
224 M*/
225 #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
226 
227 /*MC
228    PetscSqr - Returns the square of a number
229 
230    Input Parameter:
231 .   v1 - the value
232 
233    Synopsis:
234    type sqr PetscSqr(type v1)
235 
236    Notes: type can be integer or floating point value
237 
238    Level: beginner
239 
240 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
241 
242 M*/
243 #define PetscSqr(a)     ((a)*(a))
244 
245 /* ----------------------------------------------------------------------------*/
246 /*
247      Basic constants - These should be done much better
248 */
249 #define PETSC_PI                 3.14159265358979323846264
250 #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
251 #define PETSC_MAX_INT            1000000000
252 #define PETSC_MIN_INT            -1000000000
253 
254 #if defined(PETSC_USE_SINGLE)
255 #  define PETSC_MAX                     1.e30
256 #  define PETSC_MIN                    -1.e30
257 #  define PETSC_MACHINE_EPSILON         1.e-7
258 #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
259 #  define PETSC_SMALL                   1.e-5
260 #elif defined(PETSC_USE_INT)
261 #  define PETSC_MAX                     PETSC_MAX_INT
262 #  define PETSC_MIN                     PETSC_MIN_INT
263 #  define PETSC_MACHINE_EPSILON         1
264 #  define PETSC_SQRT_MACHINE_EPSILON    1
265 #  define PETSC_SMALL                   0
266 #else
267 #  define PETSC_MAX                     1.e300
268 #  define PETSC_MIN                    -1.e300
269 #  define PETSC_MACHINE_EPSILON         1.e-14
270 #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
271 #  define PETSC_SMALL                   1.e-10
272 #endif
273 
274 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
275 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
276 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);
277 
278 
279 /* ----------------------------------------------------------------------------*/
280 /*
281     PetscLogDouble variables are used to contain double precision numbers
282   that are not used in the numerical computations, but rather in logging,
283   timing etc.
284 */
285 typedef double PetscLogDouble;
286 #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
287 
288 #define PassiveReal   PetscReal
289 #define PassiveScalar PetscScalar
290 
291 
292 PETSC_EXTERN_CXX_END
293 #endif
294