xref: /petsc/include/petscmath.h (revision 58c0e5077dcf40d6a880c19c87f1075ae1d22c8e)
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 #include <petscsystypes.h>
15 
16 /*
17 
18    Defines operations that are different for complex and real numbers.
19    All PETSc objects in one program are built around the object
20    PetscScalar which is either always a real or a complex.
21 
22 */
23 
24 /*
25     Real number definitions
26  */
27 #if defined(PETSC_USE_REAL_SINGLE)
28 #define PetscSqrtReal(a)    sqrtf(a)
29 #define PetscCbrtReal(a)    cbrtf(a)
30 #define PetscHypotReal(a,b) hypotf(a,b)
31 #define PetscAtan2Real(a,b) atan2f(a,b)
32 #define PetscPowReal(a,b)   powf(a,b)
33 #define PetscExpReal(a)     expf(a)
34 #define PetscLogReal(a)     logf(a)
35 #define PetscLog10Real(a)   log10f(a)
36 #define PetscLog2Real(a)    log2f(a)
37 #define PetscSinReal(a)     sinf(a)
38 #define PetscCosReal(a)     cosf(a)
39 #define PetscTanReal(a)     tanf(a)
40 #define PetscAsinReal(a)    asinf(a)
41 #define PetscAcosReal(a)    acosf(a)
42 #define PetscAtanReal(a)    atanf(a)
43 #define PetscSinhReal(a)    sinhf(a)
44 #define PetscCoshReal(a)    coshf(a)
45 #define PetscTanhReal(a)    tanhf(a)
46 #define PetscAsinhReal(a)   asinhf(a)
47 #define PetscAcoshReal(a)   acoshf(a)
48 #define PetscAtanhReal(a)   atanhf(a)
49 #define PetscRoundReal(a)   roundf(a)
50 #define PetscCeilReal(a)    ceilf(a)
51 #define PetscFloorReal(a)   floorf(a)
52 #define PetscFmodReal(a,b)  fmodf(a,b)
53 #define PetscTGamma(a)      tgammaf(a)
54 
55 #elif defined(PETSC_USE_REAL_DOUBLE)
56 #define PetscSqrtReal(a)    sqrt(a)
57 #define PetscCbrtReal(a)    cbrt(a)
58 #define PetscHypotReal(a,b) hypot(a,b)
59 #define PetscAtan2Real(a,b) atan2(a,b)
60 #define PetscPowReal(a,b)   pow(a,b)
61 #define PetscExpReal(a)     exp(a)
62 #define PetscLogReal(a)     log(a)
63 #define PetscLog10Real(a)   log10(a)
64 #define PetscLog2Real(a)    log2(a)
65 #define PetscSinReal(a)     sin(a)
66 #define PetscCosReal(a)     cos(a)
67 #define PetscTanReal(a)     tan(a)
68 #define PetscAsinReal(a)    asin(a)
69 #define PetscAcosReal(a)    acos(a)
70 #define PetscAtanReal(a)    atan(a)
71 #define PetscSinhReal(a)    sinh(a)
72 #define PetscCoshReal(a)    cosh(a)
73 #define PetscTanhReal(a)    tanh(a)
74 #define PetscAsinhReal(a)   asinh(a)
75 #define PetscAcoshReal(a)   acosh(a)
76 #define PetscAtanhReal(a)   atanh(a)
77 #define PetscRoundReal(a)   round(a)
78 #define PetscCeilReal(a)    ceil(a)
79 #define PetscFloorReal(a)   floor(a)
80 #define PetscFmodReal(a,b)  fmod(a,b)
81 #define PetscTGamma(a)      tgamma(a)
82 
83 #elif defined(PETSC_USE_REAL___FLOAT128)
84 #define PetscSqrtReal(a)    sqrtq(a)
85 #define PetscCbrtReal(a)    cbrtq(a)
86 #define PetscHypotReal(a,b) hypotq(a,b)
87 #define PetscAtan2Real(a,b) atan2q(a,b)
88 #define PetscPowReal(a,b)   powq(a,b)
89 #define PetscExpReal(a)     expq(a)
90 #define PetscLogReal(a)     logq(a)
91 #define PetscLog10Real(a)   log10q(a)
92 #define PetscLog2Real(a)    log2q(a)
93 #define PetscSinReal(a)     sinq(a)
94 #define PetscCosReal(a)     cosq(a)
95 #define PetscTanReal(a)     tanq(a)
96 #define PetscAsinReal(a)    asinq(a)
97 #define PetscAcosReal(a)    acosq(a)
98 #define PetscAtanReal(a)    atanq(a)
99 #define PetscSinhReal(a)    sinhq(a)
100 #define PetscCoshReal(a)    coshq(a)
101 #define PetscTanhReal(a)    tanhq(a)
102 #define PetscAsinhReal(a)   asinhq(a)
103 #define PetscAcoshReal(a)   acoshq(a)
104 #define PetscAtanhReal(a)   atanhq(a)
105 #define PetscRoundReal(a)   roundq(a)
106 #define PetscCeilReal(a)    ceilq(a)
107 #define PetscFloorReal(a)   floorq(a)
108 #define PetscFmodReal(a,b)  fmodq(a,b)
109 #define PetscTGamma(a)      tgammaq(a)
110 
111 #elif defined(PETSC_USE_REAL___FP16)
112 #define PetscSqrtReal(a)    sqrtf(a)
113 #define PetscCbrtReal(a)    cbrtf(a)
114 #define PetscHypotReal(a,b) hypotf(a,b)
115 #define PetscAtan2Real(a,b) atan2f(a,b)
116 #define PetscPowReal(a,b)   powf(a,b)
117 #define PetscExpReal(a)     expf(a)
118 #define PetscLogReal(a)     logf(a)
119 #define PetscLog10Real(a)   log10f(a)
120 #define PetscLog2Real(a)    log2f(a)
121 #define PetscSinReal(a)     sinf(a)
122 #define PetscCosReal(a)     cosf(a)
123 #define PetscTanReal(a)     tanf(a)
124 #define PetscAsinReal(a)    asinf(a)
125 #define PetscAcosReal(a)    acosf(a)
126 #define PetscAtanReal(a)    atanf(a)
127 #define PetscSinhReal(a)    sinhf(a)
128 #define PetscCoshReal(a)    coshf(a)
129 #define PetscTanhReal(a)    tanhf(a)
130 #define PetscAsinhReal(a)   asinhf(a)
131 #define PetscAcoshReal(a)   acoshf(a)
132 #define PetscAtanhReal(a)   atanhf(a)
133 #define PetscRoundReal(a)   roundf(a)
134 #define PetscCeilReal(a)    ceilf(a)
135 #define PetscFloorReal(a)   floorf(a)
136 #define PetscFmodReal(a,b)  fmodf(a,b)
137 #define PetscTGamma(a)      tgammaf(a)
138 
139 #endif /* PETSC_USE_REAL_* */
140 
141 PETSC_STATIC_INLINE PetscReal PetscSignReal(PetscReal a)
142 {
143   return (PetscReal)((a < (PetscReal)0) ? -1 : ((a > (PetscReal)0) ? 1 : 0));
144 }
145 
146 #if !defined(PETSC_HAVE_LOG2)
147 #undef PetscLog2Real
148 PETSC_STATIC_INLINE PetscReal PetscLog2Real(PetscReal a)
149 {
150   return PetscLogReal(a)/PetscLogReal((PetscReal)2);
151 }
152 #endif
153 
154 #if defined(PETSC_USE_REAL___FLOAT128)
155 PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
156 #endif
157 #if defined(PETSC_USE_REAL___FP16)
158 PETSC_EXTERN MPI_Datatype MPIU___FP16 PetscAttrMPITypeTag(__fp16);
159 #endif
160 
161 /*MC
162    MPIU_REAL - MPI datatype corresponding to PetscReal
163 
164    Notes:
165    In MPI calls that require an MPI datatype that matches a PetscReal or array of PetscReal values, pass this value.
166 
167    Level: beginner
168 
169 .seealso: PetscReal, PetscScalar, PetscComplex, PetscInt, MPIU_SCALAR, MPIU_COMPLEX, MPIU_INT
170 M*/
171 #if defined(PETSC_USE_REAL_SINGLE)
172 #  define MPIU_REAL MPI_FLOAT
173 #elif defined(PETSC_USE_REAL_DOUBLE)
174 #  define MPIU_REAL MPI_DOUBLE
175 #elif defined(PETSC_USE_REAL___FLOAT128)
176 #  define MPIU_REAL MPIU___FLOAT128
177 #elif defined(PETSC_USE_REAL___FP16)
178 #  define MPIU_REAL MPIU___FP16
179 #endif /* PETSC_USE_REAL_* */
180 
181 /*
182     Complex number definitions
183  */
184 #if defined(PETSC_HAVE_COMPLEX)
185 #if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
186 /* C++ support of complex number */
187 
188 #define PetscRealPartComplex(a)      (a).real()
189 #define PetscImaginaryPartComplex(a) (a).imag()
190 #define PetscAbsComplex(a)           petsccomplexlib::abs(a)
191 #define PetscArgComplex(a)           petsccomplexlib::arg(a)
192 #define PetscConjComplex(a)          petsccomplexlib::conj(a)
193 #define PetscSqrtComplex(a)          petsccomplexlib::sqrt(a)
194 #define PetscPowComplex(a,b)         petsccomplexlib::pow(a,b)
195 #define PetscExpComplex(a)           petsccomplexlib::exp(a)
196 #define PetscLogComplex(a)           petsccomplexlib::log(a)
197 #define PetscSinComplex(a)           petsccomplexlib::sin(a)
198 #define PetscCosComplex(a)           petsccomplexlib::cos(a)
199 #define PetscTanComplex(a)           petsccomplexlib::tan(a)
200 #define PetscAsinComplex(a)          petsccomplexlib::asin(a)
201 #define PetscAcosComplex(a)          petsccomplexlib::acos(a)
202 #define PetscAtanComplex(a)          petsccomplexlib::atan(a)
203 #define PetscSinhComplex(a)          petsccomplexlib::sinh(a)
204 #define PetscCoshComplex(a)          petsccomplexlib::cosh(a)
205 #define PetscTanhComplex(a)          petsccomplexlib::tanh(a)
206 #define PetscAsinhComplex(a)         petsccomplexlib::asinh(a)
207 #define PetscAcoshComplex(a)         petsccomplexlib::acosh(a)
208 #define PetscAtanhComplex(a)         petsccomplexlib::atanh(a)
209 
210 /* TODO: Add configure tests
211 
212 #if !defined(PETSC_HAVE_CXX_TAN_COMPLEX)
213 #undef PetscTanComplex
214 PETSC_STATIC_INLINE PetscComplex PetscTanComplex(PetscComplex z)
215 {
216   return PetscSinComplex(z)/PetscCosComplex(z);
217 }
218 #endif
219 
220 #if !defined(PETSC_HAVE_CXX_TANH_COMPLEX)
221 #undef PetscTanhComplex
222 PETSC_STATIC_INLINE PetscComplex PetscTanhComplex(PetscComplex z)
223 {
224   return PetscSinhComplex(z)/PetscCoshComplex(z);
225 }
226 #endif
227 
228 #if !defined(PETSC_HAVE_CXX_ASIN_COMPLEX)
229 #undef PetscAsinComplex
230 PETSC_STATIC_INLINE PetscComplex PetscAsinComplex(PetscComplex z)
231 {
232   const PetscComplex j(0,1);
233   return -j*PetscLogComplex(j*z+PetscSqrtComplex(1.0f-z*z));
234 }
235 #endif
236 
237 #if !defined(PETSC_HAVE_CXX_ACOS_COMPLEX)
238 #undef PetscAcosComplex
239 PETSC_STATIC_INLINE PetscComplex PetscAcosComplex(PetscComplex z)
240 {
241   const PetscComplex j(0,1);
242   return j*PetscLogComplex(z-j*PetscSqrtComplex(1.0f-z*z));
243 }
244 #endif
245 
246 #if !defined(PETSC_HAVE_CXX_ATAN_COMPLEX)
247 #undef PetscAtanComplex
248 PETSC_STATIC_INLINE PetscComplex PetscAtanComplex(PetscComplex z)
249 {
250   const PetscComplex j(0,1);
251   return 0.5f*j*PetscLogComplex((1.0f-j*z)/(1.0f+j*z));
252 }
253 #endif
254 
255 #if !defined(PETSC_HAVE_CXX_ASINH_COMPLEX)
256 #undef PetscAsinhComplex
257 PETSC_STATIC_INLINE PetscComplex PetscAsinhComplex(PetscComplex z)
258 {
259   return PetscLogComplex(z+PetscSqrtComplex(z*z+1.0f));
260 }
261 #endif
262 
263 #if !defined(PETSC_HAVE_CXX_ACOSH_COMPLEX)
264 #undef PetscAcoshComplex
265 PETSC_STATIC_INLINE PetscComplex PetscAcoshComplex(PetscComplex z)
266 {
267   return PetscLogComplex(z+PetscSqrtComplex(z*z-1.0f));
268 }
269 #endif
270 
271 #if !defined(PETSC_HAVE_CXX_ATANH_COMPLEX)
272 #undef PetscAtanhComplex
273 PETSC_STATIC_INLINE PetscComplex PetscAtanhComplex(PetscComplex z)
274 {
275   return 0.5f*PetscLogComplex((1.0f+z)/(1.0f-z));
276 }
277 #endif
278 
279 */
280 
281 #elif defined(PETSC_HAVE_C99_COMPLEX) && !defined(PETSC_USE_REAL___FP16)
282 /* C99 support of complex number */
283 
284 #if defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL___FP16)
285 #define PetscRealPartComplex(a)      crealf(a)
286 #define PetscImaginaryPartComplex(a) cimagf(a)
287 #define PetscAbsComplex(a)           cabsf(a)
288 #define PetscArgComplex(a)           cargf(a)
289 #define PetscConjComplex(a)          conjf(a)
290 #define PetscSqrtComplex(a)          csqrtf(a)
291 #define PetscPowComplex(a,b)         cpowf(a,b)
292 #define PetscExpComplex(a)           cexpf(a)
293 #define PetscLogComplex(a)           clogf(a)
294 #define PetscSinComplex(a)           csinf(a)
295 #define PetscCosComplex(a)           ccosf(a)
296 #define PetscTanComplex(a)           ctanf(a)
297 #define PetscAsinComplex(a)          casinf(a)
298 #define PetscAcosComplex(a)          cacosf(a)
299 #define PetscAtanComplex(a)          catanf(a)
300 #define PetscSinhComplex(a)          csinhf(a)
301 #define PetscCoshComplex(a)          ccoshf(a)
302 #define PetscTanhComplex(a)          ctanhf(a)
303 #define PetscAsinhComplex(a)         casinhf(a)
304 #define PetscAcoshComplex(a)         cacoshf(a)
305 #define PetscAtanhComplex(a)         catanhf(a)
306 
307 #elif defined(PETSC_USE_REAL_DOUBLE)
308 #define PetscRealPartComplex(a)      creal(a)
309 #define PetscImaginaryPartComplex(a) cimag(a)
310 #define PetscAbsComplex(a)           cabs(a)
311 #define PetscArgComplex(a)           carg(a)
312 #define PetscConjComplex(a)          conj(a)
313 #define PetscSqrtComplex(a)          csqrt(a)
314 #define PetscPowComplex(a,b)         cpow(a,b)
315 #define PetscExpComplex(a)           cexp(a)
316 #define PetscLogComplex(a)           clog(a)
317 #define PetscSinComplex(a)           csin(a)
318 #define PetscCosComplex(a)           ccos(a)
319 #define PetscTanComplex(a)           ctan(a)
320 #define PetscAsinComplex(a)          casin(a)
321 #define PetscAcosComplex(a)          cacos(a)
322 #define PetscAtanComplex(a)          catan(a)
323 #define PetscSinhComplex(a)          csinh(a)
324 #define PetscCoshComplex(a)          ccosh(a)
325 #define PetscTanhComplex(a)          ctanh(a)
326 #define PetscAsinhComplex(a)         casinh(a)
327 #define PetscAcoshComplex(a)         cacosh(a)
328 #define PetscAtanhComplex(a)         catanh(a)
329 
330 #elif defined(PETSC_USE_REAL___FLOAT128)
331 #define PetscRealPartComplex(a)      crealq(a)
332 #define PetscImaginaryPartComplex(a) cimagq(a)
333 #define PetscAbsComplex(a)           cabsq(a)
334 #define PetscArgComplex(a)           cargq(a)
335 #define PetscConjComplex(a)          conjq(a)
336 #define PetscSqrtComplex(a)          csqrtq(a)
337 #define PetscPowComplex(a,b)         cpowq(a,b)
338 #define PetscExpComplex(a)           cexpq(a)
339 #define PetscLogComplex(a)           clogq(a)
340 #define PetscSinComplex(a)           csinq(a)
341 #define PetscCosComplex(a)           ccosq(a)
342 #define PetscTanComplex(a)           ctanq(a)
343 #define PetscAsinComplex(a)          casinq(a)
344 #define PetscAcosComplex(a)          cacosq(a)
345 #define PetscAtanComplex(a)          catanq(a)
346 #define PetscSinhComplex(a)          csinhq(a)
347 #define PetscCoshComplex(a)          ccoshq(a)
348 #define PetscTanhComplex(a)          ctanhq(a)
349 #define PetscAsinhComplex(a)         casinhq(a)
350 #define PetscAcoshComplex(a)         cacoshq(a)
351 #define PetscAtanhComplex(a)         catanhq(a)
352 
353 #endif /* PETSC_USE_REAL_* */
354 #endif /* (__cplusplus && PETSC_HAVE_CXX_COMPLEX) else-if (!__cplusplus && PETSC_HAVE_C99_COMPLEX) */
355 
356 /*
357    PETSC_i is the imaginary number, i
358 */
359 PETSC_EXTERN PetscComplex PETSC_i;
360 
361 /*
362    Try to do the right thing for complex number construction: see
363    http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1464.htm
364    for details
365 */
366 PETSC_STATIC_INLINE PetscComplex PetscCMPLX(PetscReal x, PetscReal y)
367 {
368 #if   defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
369   return PetscComplex(x,y);
370 #elif defined(_Imaginary_I)
371   return x + y * _Imaginary_I;
372 #else
373   { /* In both C99 and C11 (ISO/IEC 9899, Section 6.2.5),
374 
375        "For each floating type there is a corresponding real type, which is always a real floating
376        type. For real floating types, it is the same type. For complex types, it is the type given
377        by deleting the keyword _Complex from the type name."
378 
379        So type punning should be portable. */
380     union { PetscComplex z; PetscReal f[2]; } uz;
381 
382     uz.f[0] = x;
383     uz.f[1] = y;
384     return uz.z;
385   }
386 #endif
387 }
388 
389 #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
390 #define MPIU_C_COMPLEX MPI_C_COMPLEX
391 #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
392 #else
393 # if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
394   typedef petsccomplexlib::complex<double> petsc_mpiu_c_double_complex;
395   typedef petsccomplexlib::complex<float> petsc_mpiu_c_complex;
396 # elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
397   typedef double _Complex petsc_mpiu_c_double_complex;
398   typedef float _Complex petsc_mpiu_c_complex;
399 # else
400   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
401   typedef struct {float real,imag;} petsc_mpiu_c_complex;
402 # endif
403 PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
404 PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
405 #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
406 #if defined(PETSC_USE_REAL___FLOAT128)
407 PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
408 #endif /* PETSC_USE_REAL___FLOAT128 */
409 
410 /*MC
411    MPIU_COMPLEX - MPI datatype corresponding to PetscComplex
412 
413    Notes:
414    In MPI calls that require an MPI datatype that matches a PetscComplex or array of PetscComplex values, pass this value.
415 
416    Level: beginner
417 
418 .seealso: PetscReal, PetscScalar, PetscComplex, PetscInt, MPIU_REAL, MPIU_SCALAR, MPIU_COMPLEX, MPIU_INT, PETSC_i
419 M*/
420 #if defined(PETSC_USE_REAL_SINGLE)
421 #  define MPIU_COMPLEX MPIU_C_COMPLEX
422 #elif defined(PETSC_USE_REAL_DOUBLE)
423 #  define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
424 #elif defined(PETSC_USE_REAL___FLOAT128)
425 #  define MPIU_COMPLEX MPIU___COMPLEX128
426 #elif defined(PETSC_USE_REAL___FP16)
427 #  define MPIU_COMPLEX MPIU_C_COMPLEX
428 #endif /* PETSC_USE_REAL_* */
429 
430 #endif /* PETSC_HAVE_COMPLEX */
431 
432 /*
433     Scalar number definitions
434  */
435 #if defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX)
436 /*MC
437    MPIU_SCALAR - MPI datatype corresponding to PetscScalar
438 
439    Notes:
440    In MPI calls that require an MPI datatype that matches a PetscScalar or array of PetscScalar values, pass this value.
441 
442    Level: beginner
443 
444 .seealso: PetscReal, PetscScalar, PetscComplex, PetscInt, MPIU_REAL, MPIU_COMPLEX, MPIU_INT
445 M*/
446 #define MPIU_SCALAR MPIU_COMPLEX
447 
448 /*MC
449    PetscRealPart - Returns the real part of a PetscScalar
450 
451    Synopsis:
452    #include <petscmath.h>
453    PetscReal PetscRealPart(PetscScalar v)
454 
455    Not Collective
456 
457    Input Parameter:
458 .  v - value to find the real part of
459 
460    Level: beginner
461 
462 .seealso: PetscScalar, PetscImaginaryPart(), PetscMax(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
463 
464 M*/
465 #define PetscRealPart(a)      PetscRealPartComplex(a)
466 
467 /*MC
468    PetscImaginaryPart - Returns the imaginary part of a PetscScalar
469 
470    Synopsis:
471    #include <petscmath.h>
472    PetscReal PetscImaginaryPart(PetscScalar v)
473 
474    Not Collective
475 
476    Input Parameter:
477 .  v - value to find the imaginary part of
478 
479    Level: beginner
480 
481    Notes:
482        If PETSc was configured for real numbers then this always returns the value 0
483 
484 .seealso: PetscScalar, PetscRealPart(), PetscMax(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
485 
486 M*/
487 #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
488 
489 #define PetscAbsScalar(a)     PetscAbsComplex(a)
490 #define PetscArgScalar(a)     PetscArgComplex(a)
491 #define PetscConj(a)          PetscConjComplex(a)
492 #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
493 #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
494 #define PetscExpScalar(a)     PetscExpComplex(a)
495 #define PetscLogScalar(a)     PetscLogComplex(a)
496 #define PetscSinScalar(a)     PetscSinComplex(a)
497 #define PetscCosScalar(a)     PetscCosComplex(a)
498 #define PetscTanScalar(a)     PetscTanComplex(a)
499 #define PetscAsinScalar(a)    PetscAsinComplex(a)
500 #define PetscAcosScalar(a)    PetscAcosComplex(a)
501 #define PetscAtanScalar(a)    PetscAtanComplex(a)
502 #define PetscSinhScalar(a)    PetscSinhComplex(a)
503 #define PetscCoshScalar(a)    PetscCoshComplex(a)
504 #define PetscTanhScalar(a)    PetscTanhComplex(a)
505 #define PetscAsinhScalar(a)   PetscAsinhComplex(a)
506 #define PetscAcoshScalar(a)   PetscAcoshComplex(a)
507 #define PetscAtanhScalar(a)   PetscAtanhComplex(a)
508 
509 #else /* PETSC_USE_COMPLEX */
510 #define MPIU_SCALAR MPIU_REAL
511 #define PetscRealPart(a)      (a)
512 #define PetscImaginaryPart(a) ((PetscReal)0)
513 #define PetscAbsScalar(a)     PetscAbsReal(a)
514 #define PetscArgScalar(a)     (((a) < (PetscReal)0) ? PETSC_PI : (PetscReal)0)
515 #define PetscConj(a)          (a)
516 #define PetscSqrtScalar(a)    PetscSqrtReal(a)
517 #define PetscPowScalar(a,b)   PetscPowReal(a,b)
518 #define PetscExpScalar(a)     PetscExpReal(a)
519 #define PetscLogScalar(a)     PetscLogReal(a)
520 #define PetscSinScalar(a)     PetscSinReal(a)
521 #define PetscCosScalar(a)     PetscCosReal(a)
522 #define PetscTanScalar(a)     PetscTanReal(a)
523 #define PetscAsinScalar(a)    PetscAsinReal(a)
524 #define PetscAcosScalar(a)    PetscAcosReal(a)
525 #define PetscAtanScalar(a)    PetscAtanReal(a)
526 #define PetscSinhScalar(a)    PetscSinhReal(a)
527 #define PetscCoshScalar(a)    PetscCoshReal(a)
528 #define PetscTanhScalar(a)    PetscTanhReal(a)
529 #define PetscAsinhScalar(a)   PetscAsinhReal(a)
530 #define PetscAcoshScalar(a)   PetscAcoshReal(a)
531 #define PetscAtanhScalar(a)   PetscAtanhReal(a)
532 
533 #endif /* PETSC_USE_COMPLEX */
534 
535 /*
536    Certain objects may be created using either single or double precision.
537    This is currently not used.
538 */
539 typedef enum { PETSC_SCALAR_DOUBLE, PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE, PETSC_SCALAR_HALF } PetscScalarPrecision;
540 
541 /* --------------------------------------------------------------------------*/
542 
543 /*MC
544    PetscAbs - Returns the absolute value of a number
545 
546    Synopsis:
547    #include <petscmath.h>
548    type PetscAbs(type v)
549 
550    Not Collective
551 
552    Input Parameter:
553 .  v - the number
554 
555    Notes:
556     type can be integer or real floating point value
557 
558    Level: beginner
559 
560 .seealso: PetscAbsInt(), PetscAbsReal(), PetscAbsScalar()
561 
562 M*/
563 #define PetscAbs(a)  (((a) >= 0) ? (a) : (-(a)))
564 
565 /*MC
566    PetscSign - Returns the sign of a number as an integer
567 
568    Synopsis:
569    #include <petscmath.h>
570    int PetscSign(type v)
571 
572    Not Collective
573 
574    Input Parameter:
575 .  v - the number
576 
577    Notes:
578     type can be integer or real floating point value
579 
580    Level: beginner
581 
582 M*/
583 #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
584 
585 /*MC
586    PetscMin - Returns minimum of two numbers
587 
588    Synopsis:
589    #include <petscmath.h>
590    type PetscMin(type v1,type v2)
591 
592    Not Collective
593 
594    Input Parameter:
595 +  v1 - first value to find minimum of
596 -  v2 - second value to find minimum of
597 
598    Notes:
599     type can be integer or floating point value
600 
601    Level: beginner
602 
603 .seealso: PetscMax(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
604 
605 M*/
606 #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
607 
608 /*MC
609    PetscMax - Returns maxium of two numbers
610 
611    Synopsis:
612    #include <petscmath.h>
613    type max PetscMax(type v1,type v2)
614 
615    Not Collective
616 
617    Input Parameter:
618 +  v1 - first value to find maximum of
619 -  v2 - second value to find maximum of
620 
621    Notes:
622     type can be integer or floating point value
623 
624    Level: beginner
625 
626 .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
627 
628 M*/
629 #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
630 
631 /*MC
632    PetscClipInterval - Returns a number clipped to be within an interval
633 
634    Synopsis:
635    #include <petscmath.h>
636    type clip PetscClipInterval(type x,type a,type b)
637 
638    Not Collective
639 
640    Input Parameter:
641 +  x - value to use if within interval (a,b)
642 .  a - lower end of interval
643 -  b - upper end of interval
644 
645    Notes:
646     type can be integer or floating point value
647 
648    Level: beginner
649 
650 .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
651 
652 M*/
653 #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
654 
655 /*MC
656    PetscAbsInt - Returns the absolute value of an integer
657 
658    Synopsis:
659    #include <petscmath.h>
660    int abs PetscAbsInt(int v1)
661 
662    Not Collective
663 
664    Input Parameter:
665 .   v1 - the integer
666 
667    Level: beginner
668 
669 .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
670 
671 M*/
672 #define PetscAbsInt(a)  (((a)<0)   ? (-(a)) : (a))
673 
674 /*MC
675    PetscAbsReal - Returns the absolute value of an real number
676 
677    Synopsis:
678    #include <petscmath.h>
679    Real abs PetscAbsReal(PetscReal v1)
680 
681    Not Collective
682 
683    Input Parameter:
684 .   v1 - the double
685 
686 
687    Level: beginner
688 
689 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
690 
691 M*/
692 #if defined(PETSC_USE_REAL_SINGLE)
693 #define PetscAbsReal(a) fabsf(a)
694 #elif defined(PETSC_USE_REAL_DOUBLE)
695 #define PetscAbsReal(a) fabs(a)
696 #elif defined(PETSC_USE_REAL___FLOAT128)
697 #define PetscAbsReal(a) fabsq(a)
698 #elif defined(PETSC_USE_REAL___FP16)
699 #define PetscAbsReal(a) fabsf(a)
700 #endif
701 
702 /*MC
703    PetscSqr - Returns the square of a number
704 
705    Synopsis:
706    #include <petscmath.h>
707    type sqr PetscSqr(type v1)
708 
709    Not Collective
710 
711    Input Parameter:
712 .   v1 - the value
713 
714    Notes:
715     type can be integer or floating point value
716 
717    Level: beginner
718 
719 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
720 
721 M*/
722 #define PetscSqr(a)     ((a)*(a))
723 
724 /* ----------------------------------------------------------------------------*/
725 
726 #if defined(PETSC_USE_REAL_SINGLE)
727 #define PetscRealConstant(constant) constant##F
728 #elif defined(PETSC_USE_REAL_DOUBLE)
729 #define PetscRealConstant(constant) constant
730 #elif defined(PETSC_USE_REAL___FLOAT128)
731 #define PetscRealConstant(constant) constant##Q
732 #elif defined(PETSC_USE_REAL___FP16)
733 #define PetscRealConstant(constant) constant##F
734 #endif
735 
736 /*
737      Basic constants
738 */
739 #define PETSC_PI    PetscRealConstant(3.1415926535897932384626433832795029)
740 #define PETSC_PHI   PetscRealConstant(1.6180339887498948482045868343656381)
741 #define PETSC_SQRT2 PetscRealConstant(1.4142135623730950488016887242096981)
742 
743 #if !defined(PETSC_USE_64BIT_INDICES)
744 #define PETSC_MAX_INT            2147483647
745 #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
746 #else
747 #define PETSC_MAX_INT            9223372036854775807L
748 #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
749 #endif
750 
751 #if defined(PETSC_USE_REAL_SINGLE)
752 #  define PETSC_MAX_REAL                3.40282346638528860e+38F
753 #  define PETSC_MIN_REAL                (-PETSC_MAX_REAL)
754 #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
755 #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
756 #  define PETSC_SMALL                   1.e-5F
757 #elif defined(PETSC_USE_REAL_DOUBLE)
758 #  define PETSC_MAX_REAL                1.7976931348623157e+308
759 #  define PETSC_MIN_REAL                (-PETSC_MAX_REAL)
760 #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
761 #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
762 #  define PETSC_SMALL                   1.e-10
763 #elif defined(PETSC_USE_REAL___FLOAT128)
764 #  define PETSC_MAX_REAL                FLT128_MAX
765 #  define PETSC_MIN_REAL                (-FLT128_MAX)
766 #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
767 #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078144567552953958511352539e-17Q
768 #  define PETSC_SMALL                   1.e-20Q
769 #elif defined(PETSC_USE_REAL___FP16)
770 #  define PETSC_MAX_REAL                65504.0F
771 #  define PETSC_MIN_REAL                (-PETSC_MAX_REAL)
772 #  define PETSC_MACHINE_EPSILON         .0009765625F
773 #  define PETSC_SQRT_MACHINE_EPSILON    .03125F
774 #  define PETSC_SMALL                   5.e-3F
775 #endif
776 
777 #define PETSC_INFINITY               (PETSC_MAX_REAL/4)
778 #define PETSC_NINFINITY              (-PETSC_INFINITY)
779 
780 PETSC_EXTERN PetscBool PetscIsInfReal(PetscReal);
781 PETSC_EXTERN PetscBool PetscIsNanReal(PetscReal);
782 PETSC_EXTERN PetscBool PetscIsNormalReal(PetscReal);
783 PETSC_STATIC_INLINE PetscBool PetscIsInfOrNanReal(PetscReal v) {return PetscIsInfReal(v) || PetscIsNanReal(v) ? PETSC_TRUE : PETSC_FALSE;}
784 PETSC_STATIC_INLINE PetscBool PetscIsInfScalar(PetscScalar v) {return PetscIsInfReal(PetscAbsScalar(v));}
785 PETSC_STATIC_INLINE PetscBool PetscIsNanScalar(PetscScalar v) {return PetscIsNanReal(PetscAbsScalar(v));}
786 PETSC_STATIC_INLINE PetscBool PetscIsInfOrNanScalar(PetscScalar v) {return PetscIsInfOrNanReal(PetscAbsScalar(v));}
787 PETSC_STATIC_INLINE PetscBool PetscIsNormalScalar(PetscScalar v) {return PetscIsNormalReal(PetscAbsScalar(v));}
788 
789 PETSC_EXTERN PetscBool PetscIsCloseAtTol(PetscReal,PetscReal,PetscReal,PetscReal);
790 PETSC_EXTERN PetscBool PetscEqualReal(PetscReal,PetscReal);
791 PETSC_EXTERN PetscBool PetscEqualScalar(PetscScalar,PetscScalar);
792 
793 /*
794     These macros are currently hardwired to match the regular data types, so there is no support for a different
795     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
796  */
797 #define MPIU_MATSCALAR MPIU_SCALAR
798 typedef PetscScalar MatScalar;
799 typedef PetscReal MatReal;
800 
801 struct petsc_mpiu_2scalar {PetscScalar a,b;};
802 PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
803 
804 #if defined(PETSC_USE_64BIT_INDICES)
805 struct petsc_mpiu_2int {PetscInt a,b;};
806 PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
807 #else
808 #define MPIU_2INT MPI_2INT
809 #endif
810 
811 PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power)
812 {
813   PetscInt result = 1;
814   while (power) {
815     if (power & 1) result *= base;
816     power >>= 1;
817     base *= base;
818   }
819   return result;
820 }
821 
822 PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power)
823 {
824   PetscReal result = 1;
825   if (power < 0) {
826     power = -power;
827     base  = ((PetscReal)1)/base;
828   }
829   while (power) {
830     if (power & 1) result *= base;
831     power >>= 1;
832     base *= base;
833   }
834   return result;
835 }
836 
837 PETSC_STATIC_INLINE PetscScalar PetscPowScalarInt(PetscScalar base,PetscInt power)
838 {
839   PetscScalar result = (PetscReal)1;
840   if (power < 0) {
841     power = -power;
842     base  = ((PetscReal)1)/base;
843   }
844   while (power) {
845     if (power & 1) result *= base;
846     power >>= 1;
847     base *= base;
848   }
849   return result;
850 }
851 
852 PETSC_STATIC_INLINE PetscScalar PetscPowScalarReal(PetscScalar base,PetscReal power)
853 {
854   PetscScalar cpower = power;
855   return PetscPowScalar(base,cpower);
856 }
857 
858 PETSC_EXTERN PetscErrorCode PetscLinearRegression(PetscInt,const PetscReal[],const PetscReal[],PetscReal*,PetscReal*);
859 #endif
860