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