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