xref: /petsc/include/petscmath.h (revision debe9ee2755b2e6ee26f815bab511dcc308e711d)
1 /*
2 
3       PETSc mathematics include file. Defines certain basic mathematical
4     constants and functions for working with single, double, and quad precision
5     floating point numbers as well as complex single and double.
6 
7     This file is included by petscsys.h and should not be used directly.
8 
9 */
10 
11 #if !defined(__PETSCMATH_H)
12 #define __PETSCMATH_H
13 #include <math.h>
14 
15 PETSC_EXTERN MPI_Datatype MPIU_2SCALAR;
16 PETSC_EXTERN MPI_Datatype MPIU_2INT;
17 
18 /*
19 
20      Defines operations that are different for complex and real numbers;
21    note that one cannot 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 real or a complex.
24 
25 */
26 
27 #define PetscExpPassiveScalar(a) PetscExpScalar()
28 #if defined(PETSC_USE_REAL_SINGLE)
29 #define MPIU_REAL   MPI_FLOAT
30 typedef float PetscReal;
31 #define PetscSqrtReal(a)    sqrt(a)
32 #elif defined(PETSC_USE_REAL_DOUBLE)
33 #define MPIU_REAL   MPI_DOUBLE
34 typedef double PetscReal;
35 #define PetscSqrtReal(a)    sqrt(a)
36 #elif defined(PETSC_USE_REAL___FLOAT128)
37 #if defined(__cplusplus)
38 extern "C" {
39 #endif
40 #include <quadmath.h>
41 #if defined(__cplusplus)
42 }
43 #endif
44 #define MPIU_REAL MPIU___FLOAT128
45 typedef __float128 PetscReal;
46 #define PetscSqrtReal(a)    sqrtq(a)
47 #endif /* PETSC_USE_REAL_* */
48 
49 /*
50     Complex number definitions
51  */
52 #if defined(PETSC_USE_COMPLEX)
53 #if defined(PETSC_CLANGUAGE_CXX)
54 /* C++ support of complex number */
55 #if defined(PETSC_HAVE_CUSP)
56 #define complexlib cusp
57 #include <cusp/complex.h>
58 #else
59 #define complexlib std
60 #include <complex>
61 #endif
62 
63 #define PetscRealPart(a)      (a).real()
64 #define PetscImaginaryPart(a) (a).imag()
65 #define PetscAbsScalar(a)     complexlib::abs(a)
66 #define PetscConj(a)          complexlib::conj(a)
67 #define PetscSqrtScalar(a)    complexlib::sqrt(a)
68 #define PetscPowScalar(a,b)   complexlib::pow(a,b)
69 #define PetscExpScalar(a)     complexlib::exp(a)
70 #define PetscLogScalar(a)     complexlib::log(a)
71 #define PetscSinScalar(a)     complexlib::sin(a)
72 #define PetscCosScalar(a)     complexlib::cos(a)
73 
74 #if defined(PETSC_USE_REAL_SINGLE)
75 typedef complexlib::complex<float> PetscScalar;
76 #elif defined(PETSC_USE_REAL_DOUBLE)
77 typedef complexlib::complex<double> PetscScalar;
78 #endif  /* PETSC_USE_REAL_ */
79 
80 #else /* PETSC_CLANGUAGE_CXX */
81 
82 #if defined(PETSC_USE_REAL_SINGLE)
83 typedef float complex PetscScalar;
84 
85 #define PetscRealPart(a)      crealf(a)
86 #define PetscImaginaryPart(a) cimagf(a)
87 #define PetscAbsScalar(a)     cabsf(a)
88 #define PetscConj(a)          conjf(a)
89 #define PetscSqrtScalar(a)    csqrtf(a)
90 #define PetscPowScalar(a,b)   cpowf(a,b)
91 #define PetscExpScalar(a)     cexpf(a)
92 #define PetscLogScalar(a)     clogf(a)
93 #define PetscSinScalar(a)     csinf(a)
94 #define PetscCosScalar(a)     ccosf(a)
95 
96 #elif defined(PETSC_USE_REAL_DOUBLE)
97 typedef double complex PetscScalar;
98 
99 #define PetscRealPart(a)      creal(a)
100 #define PetscImaginaryPart(a) cimag(a)
101 #define PetscAbsScalar(a)     cabs(a)
102 #define PetscConj(a)          conj(a)
103 #define PetscSqrtScalar(a)    csqrt(a)
104 #define PetscPowScalar(a,b)   cpow(a,b)
105 #define PetscExpScalar(a)     cexp(a)
106 #define PetscLogScalar(a)     clog(a)
107 #define PetscSinScalar(a)     csin(a)
108 #define PetscCosScalar(a)     ccos(a)
109 
110 #endif /* PETSC_USE_REAL_* */
111 #endif /* PETSC_CLANGUAGE_CXX */
112 
113 #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
114 #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
115 #define MPIU_C_COMPLEX MPI_C_COMPLEX
116 #else
117 PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX;
118 PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX;
119 #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
120 
121 #if defined(PETSC_USE_REAL_SINGLE)
122 #define MPIU_SCALAR MPIU_C_COMPLEX
123 #elif defined(PETSC_USE_REAL_DOUBLE)
124 #define MPIU_SCALAR MPIU_C_DOUBLE_COMPLEX
125 #endif /* PETSC_USE_REAL_* */
126 
127 /*
128     real number definitions
129  */
130 #else /* PETSC_USE_COMPLEX */
131 #if defined(PETSC_USE_REAL_SINGLE)
132 #define MPIU_SCALAR           MPI_FLOAT
133 typedef float PetscScalar;
134 #elif defined(PETSC_USE_REAL_DOUBLE)
135 #define MPIU_SCALAR           MPI_DOUBLE
136 typedef double PetscScalar;
137 #elif defined(PETSC_USE_REAL___FLOAT128)
138 PETSC_EXTERN MPI_Datatype MPIU___FLOAT128;
139 #define MPIU_SCALAR MPIU___FLOAT128
140 typedef __float128 PetscScalar;
141 #endif /* PETSC_USE_REAL_* */
142 #define PetscRealPart(a)      (a)
143 #define PetscImaginaryPart(a) ((PetscReal)0.)
144 PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
145 #define PetscConj(a)          (a)
146 #if !defined(PETSC_USE_REAL___FLOAT128)
147 #define PetscSqrtScalar(a)    sqrt(a)
148 #define PetscPowScalar(a,b)   pow(a,b)
149 #define PetscExpScalar(a)     exp(a)
150 #define PetscLogScalar(a)     log(a)
151 #define PetscSinScalar(a)     sin(a)
152 #define PetscCosScalar(a)     cos(a)
153 #else /* PETSC_USE_REAL___FLOAT128 */
154 #define PetscSqrtScalar(a)    sqrtq(a)
155 #define PetscPowScalar(a,b)   powq(a,b)
156 #define PetscExpScalar(a)     expq(a)
157 #define PetscLogScalar(a)     logq(a)
158 #define PetscSinScalar(a)     sinq(a)
159 #define PetscCosScalar(a)     cosq(a)
160 #endif /* PETSC_USE_REAL___FLOAT128 */
161 
162 #endif /* PETSC_USE_COMPLEX */
163 
164 #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
165 #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
166 
167 /* --------------------------------------------------------------------------*/
168 
169 /*
170    Certain objects may be created using either single or double precision.
171    This is currently not used.
172 */
173 typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
174 
175 /* PETSC_i is the imaginary number, i */
176 PETSC_EXTERN PetscScalar   PETSC_i;
177 
178 /*MC
179    PetscMin - Returns minimum of two numbers
180 
181    Synopsis:
182    type PetscMin(type v1,type v2)
183 
184    Not Collective
185 
186    Input Parameter:
187 +  v1 - first value to find minimum of
188 -  v2 - second value to find minimum of
189 
190 
191    Notes: type can be integer or floating point value
192 
193    Level: beginner
194 
195 
196 .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
197 
198 M*/
199 #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
200 
201 /*MC
202    PetscMax - Returns maxium of two numbers
203 
204    Synopsis:
205    type max PetscMax(type v1,type v2)
206 
207    Not Collective
208 
209    Input Parameter:
210 +  v1 - first value to find maximum of
211 -  v2 - second value to find maximum of
212 
213    Notes: type can be integer or floating point value
214 
215    Level: beginner
216 
217 .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
218 
219 M*/
220 #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
221 
222 /*MC
223    PetscClipInterval - Returns a number clipped to be within an interval
224 
225    Synopsis:
226    type clip PetscClipInterval(type x,type a,type b)
227 
228    Not Collective
229 
230    Input Parameter:
231 +  x - value to use if within interval (a,b)
232 .  a - lower end of interval
233 -  b - upper end of interval
234 
235    Notes: type can be integer or floating point value
236 
237    Level: beginner
238 
239 .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
240 
241 M*/
242 #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
243 
244 /*MC
245    PetscAbsInt - Returns the absolute value of an integer
246 
247    Synopsis:
248    int abs PetscAbsInt(int v1)
249 
250    Not Collective
251 
252    Input Parameter:
253 .   v1 - the integer
254 
255    Level: beginner
256 
257 .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
258 
259 M*/
260 #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
261 
262 /*MC
263    PetscAbsReal - Returns the absolute value of an real number
264 
265    Synopsis:
266    Real abs PetscAbsReal(PetscReal v1)
267 
268    Not Collective
269 
270    Input Parameter:
271 .   v1 - the double
272 
273 
274    Level: beginner
275 
276 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
277 
278 M*/
279 #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
280 
281 /*MC
282    PetscSqr - Returns the square of a number
283 
284    Synopsis:
285    type sqr PetscSqr(type v1)
286 
287    Not Collective
288 
289    Input Parameter:
290 .   v1 - the value
291 
292    Notes: type can be integer or floating point value
293 
294    Level: beginner
295 
296 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
297 
298 M*/
299 #define PetscSqr(a)     ((a)*(a))
300 
301 /* ----------------------------------------------------------------------------*/
302 /*
303      Basic constants
304 */
305 #if defined(PETSC_USE_REAL___FLOAT128)
306 #define PETSC_PI                 M_PIq
307 #elif defined(M_PI)
308 #define PETSC_PI                 M_PI
309 #else
310 #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
311 #endif
312 
313 #if !defined(PETSC_USE_64BIT_INDICES)
314 #define PETSC_MAX_INT            2147483647
315 #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
316 #else
317 #define PETSC_MAX_INT            9223372036854775807L
318 #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
319 #endif
320 
321 #if defined(PETSC_USE_REAL_SINGLE)
322 #  define PETSC_MAX_REAL                3.40282346638528860e+38F
323 #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
324 #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
325 #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
326 #  define PETSC_SMALL                   1.e-5
327 #elif defined(PETSC_USE_REAL_DOUBLE)
328 #  define PETSC_MAX_REAL                1.7976931348623157e+308
329 #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
330 #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
331 #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
332 #  define PETSC_SMALL                   1.e-10
333 #elif defined(PETSC_USE_REAL___FLOAT128)
334 #  define PETSC_MAX_REAL                FLT128_MAX
335 #  define PETSC_MIN_REAL                -FLT128_MAX
336 #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
337 #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
338 #  define PETSC_SMALL                   1.e-20
339 #endif
340 
341 #if defined PETSC_HAVE_ADIC
342 /* Use MPI_Allreduce when ADIC is not available. */
343 PETSC_EXTERN PetscErrorCode PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*);
344 PETSC_EXTERN PetscErrorCode PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*);
345 PETSC_EXTERN PetscErrorCode PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*);
346 #endif
347 
348 PETSC_EXTERN PetscErrorCode PetscIsInfOrNanScalar(PetscScalar);
349 PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
350 
351 /* ----------------------------------------------------------------------------*/
352 #define PassiveReal   PetscReal
353 #define PassiveScalar PetscScalar
354 
355 /*
356     These macros are currently hardwired to match the regular data types, so there is no support for a different
357     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
358  */
359 #define MPIU_MATSCALAR MPIU_SCALAR
360 typedef PetscScalar MatScalar;
361 typedef PetscReal MatReal;
362 
363 
364 #endif
365