xref: /petsc/include/petscmath.h (revision 0e1bea35ea154a6b1beb45cf818c0da9b32e4bb3)
1e489efc1SBarry Smith /*
2314da920SBarry Smith 
3314da920SBarry Smith       PETSc mathematics include file. Defines certain basic mathematical
4a5057860SBarry Smith     constants and functions for working with single, double, and quad precision
5a5057860SBarry Smith     floating point numbers as well as complex single and double.
6314da920SBarry Smith 
7d382aafbSBarry Smith     This file is included by petscsys.h and should not be used directly.
8e7029fe1SSatish Balay 
9e489efc1SBarry Smith */
10e489efc1SBarry Smith 
11488ecbafSBarry Smith #if !defined(__PETSCMATH_H)
12488ecbafSBarry Smith #define __PETSCMATH_H
130a5f7794SBarry Smith #include <math.h>
140a5f7794SBarry Smith 
15314da920SBarry Smith /*
16f4ccad53SBarry Smith 
17f4ccad53SBarry Smith      Defines operations that are different for complex and real numbers;
18a5057860SBarry Smith    note that one cannot mix the use of complex and real in the same
19f4ccad53SBarry Smith    PETSc program. All PETSc objects in one program are built around the object
2098725619SBarry Smith    PetscScalar which is either always a real or a complex.
21f4ccad53SBarry Smith 
22e489efc1SBarry Smith */
23b36a9721SBarry Smith 
24c1d390e3SJed Brown #if defined(PETSC_USE_REAL_SINGLE)
25c1d390e3SJed Brown #define MPIU_REAL   MPI_FLOAT
26c1d390e3SJed Brown typedef float PetscReal;
27a97cf724SLisandro Dalcin #define PetscRoundReal(a)   roundf(a)
28a97cf724SLisandro Dalcin #define PetscSqrtReal(a)    sqrtf(a)
29a97cf724SLisandro Dalcin #define PetscExpReal(a)     expf(a)
30a97cf724SLisandro Dalcin #define PetscLogReal(a)     logf(a)
31a97cf724SLisandro Dalcin #define PetscLog10Real(a)   log10f(a)
3278a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
33a97cf724SLisandro Dalcin #define PetscLog2Real(a)    log2f(a)
3478a59e97SMatthew G. Knepley #endif
35a97cf724SLisandro Dalcin #define PetscSinReal(a)     sinf(a)
36a97cf724SLisandro Dalcin #define PetscCosReal(a)     cosf(a)
37a97cf724SLisandro Dalcin #define PetscTanReal(a)     tanf(a)
38a97cf724SLisandro Dalcin #define PetscAsinReal(a)    asinf(a)
39a97cf724SLisandro Dalcin #define PetscAcosReal(a)    acosf(a)
40a97cf724SLisandro Dalcin #define PetscAtanReal(a)    atanf(a)
41a97cf724SLisandro Dalcin #define PetscAtan2Real(a,b) atan2f(a,b)
42a97cf724SLisandro Dalcin #define PetscSinhReal(a)    sinhf(a)
43a97cf724SLisandro Dalcin #define PetscCoshReal(a)    coshf(a)
44a97cf724SLisandro Dalcin #define PetscTanhReal(a)    tanhf(a)
45a97cf724SLisandro Dalcin #define PetscPowReal(a,b)   powf(a,b)
46a97cf724SLisandro Dalcin #define PetscCeilReal(a)    ceilf(a)
47a97cf724SLisandro Dalcin #define PetscFloorReal(a)   floorf(a)
48a97cf724SLisandro Dalcin #define PetscFmodReal(a,b)  fmodf(a,b)
4943f0d3baSJed Brown #define PetscTGamma(a)      tgammaf(a)
50c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
51c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
52c1d390e3SJed Brown typedef double PetscReal;
53814a3092SSatish Balay #define PetscRoundReal(a)   round(a)
548f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
559a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
569a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
5777b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
5878a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
5978a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2(a)
6078a59e97SMatthew G. Knepley #endif
619a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
629a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
63a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
64255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
65255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
66a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
671d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2(a,b)
688d1b9be0SJed Brown #define PetscSinhReal(a)    sinh(a)
698d1b9be0SJed Brown #define PetscCoshReal(a)    cosh(a)
708d1b9be0SJed Brown #define PetscTanhReal(a)    tanh(a)
71369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
7277b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
7377b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
74a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
750646a658SBarry Smith #define PetscTGamma(a)      tgamma(a)
76c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
77574fde7bSSatish Balay #if defined(__cplusplus)
78574fde7bSSatish Balay extern "C" {
79574fde7bSSatish Balay #endif
80574fde7bSSatish Balay #include <quadmath.h>
81574fde7bSSatish Balay #if defined(__cplusplus)
82574fde7bSSatish Balay }
83574fde7bSSatish Balay #endif
848ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
85c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
86c1d390e3SJed Brown typedef __float128 PetscReal;
87814a3092SSatish Balay #define PetscRoundReal(a)   roundq(a)
888f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
899a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
909a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
9177b4d14cSPeter Brune #define PetscLog10Real(a)   log10q(a)
9278a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
9378a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2q(a)
9478a59e97SMatthew G. Knepley #endif
959a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
969a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
97a4bea5a6SPeter Brune #define PetscTanReal(a)     tanq(a)
98255453a1SBarry Smith #define PetscAsinReal(a)    asinq(a)
99255453a1SBarry Smith #define PetscAcosReal(a)    acosq(a)
100a4bea5a6SPeter Brune #define PetscAtanReal(a)    atanq(a)
1011d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2q(a,b)
1028d1b9be0SJed Brown #define PetscSinhReal(a)    sinhq(a)
1038d1b9be0SJed Brown #define PetscCoshReal(a)    coshq(a)
1048d1b9be0SJed Brown #define PetscTanhReal(a)    tanhq(a)
105369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
10677b4d14cSPeter Brune #define PetscCeilReal(a)    ceilq(a)
10777b4d14cSPeter Brune #define PetscFloorReal(a)   floorq(a)
108a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmodq(a,b)
1090646a658SBarry Smith #define PetscTGamma(a)      tgammaq(a)
110570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)
111570b7f6dSBarry Smith PETSC_EXTERN MPI_Datatype MPIU___FP16 PetscAttrMPITypeTag(__fp16);
112570b7f6dSBarry Smith #define MPIU_REAL MPIU___FP16
113570b7f6dSBarry Smith typedef __fp16 PetscReal;
1145a0908ecSJose E. Roman #define PetscRoundReal(a)   roundf(a)
115570b7f6dSBarry Smith #define PetscSqrtReal(a)    sqrtf(a)
116570b7f6dSBarry Smith #define PetscExpReal(a)     expf(a)
117570b7f6dSBarry Smith #define PetscLogReal(a)     logf(a)
118570b7f6dSBarry Smith #define PetscLog10Real(a)   log10f(a)
119570b7f6dSBarry Smith #ifdef PETSC_HAVE_LOG2
120570b7f6dSBarry Smith #define PetscLog2Real(a)    log2f(a)
121570b7f6dSBarry Smith #endif
122570b7f6dSBarry Smith #define PetscSinReal(a)     sinf(a)
123570b7f6dSBarry Smith #define PetscCosReal(a)     cosf(a)
124570b7f6dSBarry Smith #define PetscTanReal(a)     tanf(a)
125570b7f6dSBarry Smith #define PetscAsinReal(a)    asinf(a)
126570b7f6dSBarry Smith #define PetscAcosReal(a)    acosf(a)
127570b7f6dSBarry Smith #define PetscAtanReal(a)    atanf(a)
128570b7f6dSBarry Smith #define PetscAtan2Real(a,b) atan2f(a,b)
129570b7f6dSBarry Smith #define PetscSinhReal(a)    sinhf(a)
130570b7f6dSBarry Smith #define PetscCoshReal(a)    coshf(a)
131570b7f6dSBarry Smith #define PetscTanhReal(a)    tanhf(a)
132570b7f6dSBarry Smith #define PetscPowReal(a,b)   powf(a,b)
133570b7f6dSBarry Smith #define PetscCeilReal(a)    ceilf(a)
134570b7f6dSBarry Smith #define PetscFloorReal(a)   floorf(a)
135570b7f6dSBarry Smith #define PetscFmodReal(a,b)  fmodf(a,b)
136570b7f6dSBarry Smith #define PetscTGamma(a)      tgammaf(a)
137c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
13859cb5930SBarry Smith 
1391093a601SBarry Smith /*
1401093a601SBarry Smith     Complex number definitions
1411093a601SBarry Smith  */
1422f217381SBarry Smith #if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
1438cd53115SBarry Smith #if !defined(PETSC_SKIP_COMPLEX)
14450f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
1451093a601SBarry Smith /* C++ support of complex number */
146e9e886b6SKarl Rupp #if defined(PETSC_HAVE_VECCUDA) && __CUDACC_VER_MAJOR__ > 6
147fda01a78SKarl Rupp /* complex headers in thrust only available in CUDA 7.0 and above */
148ec42abe4SAlejandro Lamas Daviña #define complexlib thrust
149ec42abe4SAlejandro Lamas Daviña #include <thrust/complex.h>
150debe9ee2SPaul Mullowney #else
151debe9ee2SPaul Mullowney #define complexlib std
152debe9ee2SPaul Mullowney #include <complex>
1539ae82921SPaul Mullowney #endif
154b7940d39SSatish Balay 
15550f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
15650f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
15750f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
15850f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
15950f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
16050f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
16150f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
16250f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
16350f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
16450f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
165255453a1SBarry Smith #define PetscAsinComplex(a)          complexlib::asin(a)
166255453a1SBarry Smith #define PetscAcosComplex(a)          complexlib::acos(a)
167027d9794SBarry Smith #if defined(PETSC_HAVE_TANCOMPLEX)
168a4bea5a6SPeter Brune #define PetscTanComplex(a)           complexlib::tan(a)
169027d9794SBarry Smith #else
170027d9794SBarry Smith #define PetscTanComplex(a)           PetscSinComplex(a)/PetscCosComplex(a)
171027d9794SBarry Smith #endif
172a4bea5a6SPeter Brune #define PetscSinhComplex(a)          complexlib::sinh(a)
173a4bea5a6SPeter Brune #define PetscCoshComplex(a)          complexlib::cosh(a)
174027d9794SBarry Smith #if defined(PETSC_HAVE_TANHCOMPLEX)
175a4bea5a6SPeter Brune #define PetscTanhComplex(a)          complexlib::tanh(a)
176027d9794SBarry Smith #else
177027d9794SBarry Smith #define PetscTanhComplex(a)          PetscSinhComplex(a)/PetscCoshComplex(a)
178027d9794SBarry Smith #endif
179debe9ee2SPaul Mullowney 
180debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
18150f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
1823be776deSLisandro Dalcin #if defined(PETSC_USE_CXX_COMPLEX_FLOAT_WORKAROUND)
1833be776deSLisandro Dalcin static inline PetscComplex operator+(const PetscComplex& lhs, const double& rhs) { return lhs + float(rhs); }
1843be776deSLisandro Dalcin static inline PetscComplex operator+(const double& lhs, const PetscComplex& rhs) { return float(lhs) + rhs; }
1853be776deSLisandro Dalcin static inline PetscComplex operator-(const PetscComplex& lhs, const double& rhs) { return lhs - float(rhs); }
1863be776deSLisandro Dalcin static inline PetscComplex operator-(const double& lhs, const PetscComplex& rhs) { return float(lhs) - rhs; }
1873be776deSLisandro Dalcin static inline PetscComplex operator*(const PetscComplex& lhs, const double& rhs) { return lhs * float(rhs); }
1883be776deSLisandro Dalcin static inline PetscComplex operator*(const double& lhs, const PetscComplex& rhs) { return float(lhs) * rhs; }
1893be776deSLisandro Dalcin static inline PetscComplex operator/(const PetscComplex& lhs, const double& rhs) { return lhs / float(rhs); }
1903be776deSLisandro Dalcin static inline PetscComplex operator/(const double& lhs, const PetscComplex& rhs) { return float(lhs) / rhs; }
1913be776deSLisandro Dalcin static inline bool operator==(const PetscComplex& lhs, const double& rhs) { return lhs.imag() == float(0) && lhs.real() == float(rhs); }
1923be776deSLisandro Dalcin static inline bool operator==(const double& lhs, const PetscComplex& rhs) { return rhs.imag() == float(0) && rhs.real() == float(lhs); }
1933be776deSLisandro Dalcin static inline bool operator!=(const PetscComplex& lhs, const double& rhs) { return lhs.imag() != float(0) || lhs.real() != float(rhs); }
1943be776deSLisandro Dalcin static inline bool operator!=(const double& lhs, const PetscComplex& rhs) { return rhs.imag() != float(0) || rhs.real() != float(lhs); }
1953be776deSLisandro Dalcin #endif  /* PETSC_USE_CXX_COMPLEX_FLOAT_WORKAROUND */
196debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
19750f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
1981a9c6b96SLisandro Dalcin #if defined(PETSC_USE_CXX_COMPLEX_FLOAT_WORKAROUND)
1991a9c6b96SLisandro Dalcin static inline PetscComplex operator+(const PetscComplex& lhs, const PetscInt& rhs) { return lhs + double(rhs); }
2001a9c6b96SLisandro Dalcin static inline PetscComplex operator+(const PetscInt& lhs, const PetscComplex& rhs) { return double(lhs) + rhs; }
2011a9c6b96SLisandro Dalcin static inline PetscComplex operator-(const PetscComplex& lhs, const PetscInt& rhs) { return lhs - double(rhs); }
2021a9c6b96SLisandro Dalcin static inline PetscComplex operator-(const PetscInt& lhs, const PetscComplex& rhs) { return double(lhs) - rhs; }
2031a9c6b96SLisandro Dalcin static inline PetscComplex operator*(const PetscComplex& lhs, const PetscInt& rhs) { return lhs * double(rhs); }
2041a9c6b96SLisandro Dalcin static inline PetscComplex operator*(const PetscInt& lhs, const PetscComplex& rhs) { return double(lhs) * rhs; }
2051a9c6b96SLisandro Dalcin static inline PetscComplex operator/(const PetscComplex& lhs, const PetscInt& rhs) { return lhs / double(rhs); }
2061a9c6b96SLisandro Dalcin static inline PetscComplex operator/(const PetscInt& lhs, const PetscComplex& rhs) { return double(lhs) / rhs; }
2071a9c6b96SLisandro Dalcin static inline bool operator==(const PetscComplex& lhs, const PetscInt& rhs) { return lhs.imag() == double(0) && lhs.real() == double(rhs); }
2081a9c6b96SLisandro Dalcin static inline bool operator==(const PetscInt& lhs, const PetscComplex& rhs) { return rhs.imag() == double(0) && rhs.real() == double(lhs); }
2091a9c6b96SLisandro Dalcin static inline bool operator!=(const PetscComplex& lhs, const PetscInt& rhs) { return lhs.imag() != double(0) || lhs.real() != double(rhs); }
2101a9c6b96SLisandro Dalcin static inline bool operator!=(const PetscInt& lhs, const PetscComplex& rhs) { return rhs.imag() != double(0) || rhs.real() != double(lhs); }
2111a9c6b96SLisandro Dalcin #endif  /* PETSC_USE_CXX_COMPLEX_FLOAT_WORKAROUND */
2128c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
21350f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
21422b3908eSBarry Smith PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128;
215debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
2168cd53115SBarry Smith #endif  /* ! PETSC_SKIP_COMPLEX */
217debe9ee2SPaul Mullowney 
218546cf897SSatish Balay #elif defined(PETSC_HAVE_C99_COMPLEX) && !defined(PETSC_USE_REAL___FP16)
2198cd53115SBarry Smith #if !defined(PETSC_SKIP_COMPLEX)
2209f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
221519e2a1fSPaul Mullowney #include <complex.h>
222519e2a1fSPaul Mullowney 
223570b7f6dSBarry Smith #if defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL___FP16)
22450f81f78SJed Brown typedef float _Complex PetscComplex;
22585b47369SMatthew Knepley 
22650f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
22750f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
22850f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
22950f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
23050f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
23150f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
23250f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
23350f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
23450f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
23550f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
236255453a1SBarry Smith #define PetscAsinComplex(a)          casinf(a)
237255453a1SBarry Smith #define PetscAcosComplex(a)          cacosf(a)
238a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanf(a)
239a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhf(a)
240a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshf(a)
241a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhf(a)
2421093a601SBarry Smith 
243ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
24450f81f78SJed Brown typedef double _Complex PetscComplex;
2451093a601SBarry Smith 
24650f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
24750f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
24850f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
24950f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
25050f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
25150f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
25250f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
25350f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
25450f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
25550f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
256255453a1SBarry Smith #define PetscAsinComplex(a)          casin(a)
257255453a1SBarry Smith #define PetscAcosComplex(a)          cacos(a)
258a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctan(a)
259a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinh(a)
260a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccosh(a)
261a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanh(a)
2621093a601SBarry Smith 
2638c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
26450f81f78SJed Brown typedef __complex128 PetscComplex;
2658ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
2668c764dc5SJose Roman 
26750f81f78SJed Brown #define PetscRealPartComplex(a)      crealq(a)
26850f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagq(a)
26950f81f78SJed Brown #define PetscAbsComplex(a)           cabsq(a)
27050f81f78SJed Brown #define PetscConjComplex(a)          conjq(a)
27150f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtq(a)
27250f81f78SJed Brown #define PetscPowComplex(a,b)         cpowq(a,b)
27350f81f78SJed Brown #define PetscExpComplex(a)           cexpq(a)
27450f81f78SJed Brown #define PetscLogComplex(a)           clogq(a)
27550f81f78SJed Brown #define PetscSinComplex(a)           csinq(a)
27650f81f78SJed Brown #define PetscCosComplex(a)           ccosq(a)
277255453a1SBarry Smith #define PetscAsinComplex(a)          casinq(a)
278255453a1SBarry Smith #define PetscAcosComplex(a)          cacosq(a)
279a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanq(a)
280a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhq(a)
281a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshq(a)
282a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhq(a)
283a4bea5a6SPeter Brune 
284ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
285d4161b4aSBarry Smith #elif (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
28650f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
2878cd53115SBarry Smith #endif /* !PETSC_SKIP_COMPLEX */
2882f217381SBarry Smith #endif /* (__cplusplus && PETSC_HAVE_CXX_COMPLEX) else-if (!__cplusplus && PETSC_HAVE_C99_COMPLEX) */
289e489efc1SBarry Smith 
2908dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
29170da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
292500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
293500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
29470da9c3bSJed Brown #else
295252ecd31SSatish Balay # if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
2968ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
2978ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
2982f217381SBarry Smith # elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
2998ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
3008ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
3018ad47952SJed Brown # else
3028ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
3038ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
3048ad47952SJed Brown # endif
3058ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
3068ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
3071b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
3088dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
3092c876bd9SBarry Smith 
3107c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
311570b7f6dSBarry Smith #  if defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL___FP16)
3127c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
3137c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
3147c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
3157c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
3167c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
3177c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
3187c2de775SJed Brown #endif
3197c2de775SJed Brown 
320d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
32150f81f78SJed Brown typedef PetscComplex PetscScalar;
32259479656SBarry Smith 
32359479656SBarry Smith /*MC
32459479656SBarry Smith    PetscRealPart - Returns the real part of a PetscScalar
32559479656SBarry Smith 
32659479656SBarry Smith    Synopsis:
32759479656SBarry Smith    #include <petscmath.h>
32859479656SBarry Smith    PetscScalar PetscRealPart(PetscScalar v)
32959479656SBarry Smith 
33059479656SBarry Smith    Not Collective
33159479656SBarry Smith 
33259479656SBarry Smith    Input Parameter:
33359479656SBarry Smith .  v - value to find the real part of
33459479656SBarry Smith 
33559479656SBarry Smith    Level: beginner
33659479656SBarry Smith 
33759479656SBarry Smith .seealso: PetscScalar, PetscImaginaryPart(), PetscMax(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
33859479656SBarry Smith 
33959479656SBarry Smith M*/
34050f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
34159479656SBarry Smith 
34259479656SBarry Smith /*MC
34359479656SBarry Smith    PetscImaginaryPart - Returns the imaginary part of a PetscScalar
34459479656SBarry Smith 
34559479656SBarry Smith    Synopsis:
34659479656SBarry Smith    #include <petscmath.h>
34759479656SBarry Smith    PetscScalar PetscImaginaryPart(PetscScalar v)
34859479656SBarry Smith 
34959479656SBarry Smith    Not Collective
35059479656SBarry Smith 
35159479656SBarry Smith    Input Parameter:
35259479656SBarry Smith .  v - value to find the imaginary part of
35359479656SBarry Smith 
35459479656SBarry Smith    Level: beginner
35559479656SBarry Smith 
35659479656SBarry Smith    Notes:
35759479656SBarry Smith        If PETSc was configured for real numbers then this always returns the value 0
35859479656SBarry Smith 
35959479656SBarry Smith .seealso: PetscScalar, PetscRealPart(), PetscMax(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
36059479656SBarry Smith 
36159479656SBarry Smith M*/
36250f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
36359479656SBarry Smith 
36450f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
36550f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
36650f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
36750f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
36850f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
36950f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
37050f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
37150f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
372255453a1SBarry Smith #define PetscAsinScalar(a)    PetscAsinComplex(a)
373255453a1SBarry Smith #define PetscAcosScalar(a)    PetscAcosComplex(a)
374a4bea5a6SPeter Brune #define PetscTanScalar(a)     PetscTanComplex(a)
375a4bea5a6SPeter Brune #define PetscSinhScalar(a)    PetscSinhComplex(a)
376a4bea5a6SPeter Brune #define PetscCoshScalar(a)    PetscCoshComplex(a)
377a4bea5a6SPeter Brune #define PetscTanhScalar(a)    PetscTanhComplex(a)
3787c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
37975567043SBarry Smith 
3801093a601SBarry Smith /*
3811093a601SBarry Smith     real number definitions
3821093a601SBarry Smith  */
3831b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
3847c2de775SJed Brown typedef PetscReal PetscScalar;
3857c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
3867c2de775SJed Brown 
387329f5518SBarry Smith #define PetscRealPart(a)      (a)
388c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
389c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
390e489efc1SBarry Smith #define PetscConj(a)          (a)
391570b7f6dSBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)  && !defined(PETSC_USE_REAL___FP16)
39218a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
393184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
394184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
39506c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
396184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
397184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
398255453a1SBarry Smith #define PetscAsinScalar(a)    asin(a)
399255453a1SBarry Smith #define PetscAcosScalar(a)    acos(a)
400a4bea5a6SPeter Brune #define PetscTanScalar(a)     tan(a)
401a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinh(a)
402a4bea5a6SPeter Brune #define PetscCoshScalar(a)    cosh(a)
403a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanh(a)
404570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)
405570b7f6dSBarry Smith #define PetscSqrtScalar(a)    sqrtf(a)
406570b7f6dSBarry Smith #define PetscPowScalar(a,b)   powf(a,b)
407570b7f6dSBarry Smith #define PetscExpScalar(a)     expf(a)
408570b7f6dSBarry Smith #define PetscLogScalar(a)     logf(a)
409570b7f6dSBarry Smith #define PetscSinScalar(a)     sinf(a)
410570b7f6dSBarry Smith #define PetscCosScalar(a)     cosf(a)
411570b7f6dSBarry Smith #define PetscAsinScalar(a)    asinf(a)
412570b7f6dSBarry Smith #define PetscAcosScalar(a)    acosf(a)
413570b7f6dSBarry Smith #define PetscTanScalar(a)     tanf(a)
414570b7f6dSBarry Smith #define PetscSinhScalar(a)    sinhf(a)
415570b7f6dSBarry Smith #define PetscCoshScalar(a)    coshf(a)
416570b7f6dSBarry Smith #define PetscTanhScalar(a)    tanhf(a)
417ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
4180d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
4190d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
4200d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
4210d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
4220d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
4230d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
424255453a1SBarry Smith #define PetscAsinScalar(a)    asinq(a)
425255453a1SBarry Smith #define PetscAcosScalar(a)    acosq(a)
426a4bea5a6SPeter Brune #define PetscTanScalar(a)     tanq(a)
427a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinhq(a)
428a4bea5a6SPeter Brune #define PetscCoshScalar(a)    coshq(a)
429a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanhq(a)
430ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
431b0a32e0cSBarry Smith 
4321b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
433e489efc1SBarry Smith 
434da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
43536850ab7SBarry Smith #define PetscSignReal(a) (((a) >= 0.0) ? ((a) == 0.0 ? 0.0 : 1.0) : -1.0)
4369fa7d148SSatish Balay #define PetscAbs(a)  (((a) >= 0) ? (a) : (-(a)))
4373f1db9ecSBarry Smith 
438314da920SBarry Smith /* --------------------------------------------------------------------------*/
439314da920SBarry Smith 
440e489efc1SBarry Smith /*
441f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
442f22f69f0SBarry Smith    This is currently not used.
443e489efc1SBarry Smith */
444570b7f6dSBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE, PETSC_SCALAR_HALF } PetscScalarPrecision;
445e489efc1SBarry Smith 
44650f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
447e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
44850f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
4498a351411SToby Isaac 
4508a351411SToby Isaac /* Try to do the right thing for complex number construction: see
4518a351411SToby Isaac 
4528a351411SToby Isaac   http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1464.htm
4538a351411SToby Isaac 
4548a351411SToby Isaac   for details
4558a351411SToby Isaac */
45619e222d7SToby Isaac PETSC_STATIC_INLINE PetscComplex PetscCMPLX(PetscReal x, PetscReal y)
4578a351411SToby Isaac {
458*0e1bea35STristan Konolige #if   defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
4598a351411SToby Isaac   return PetscComplex(x,y);
4608a351411SToby Isaac #elif defined(_Imaginary_I)
4618a351411SToby Isaac   return x + y * _Imaginary_I;
4628a351411SToby Isaac #else
463616d7c5eSToby Isaac   { /* In both C99 and C11 (ISO/IEC 9899, Section 6.2.5),
464616d7c5eSToby Isaac 
465616d7c5eSToby Isaac        "For each floating type there is a corresponding real type, which is always a real floating
466616d7c5eSToby Isaac        type. For real floating types, it is the same type. For complex types, it is the type given
467616d7c5eSToby Isaac        by deleting the keyword _Complex from the type name."
468616d7c5eSToby Isaac 
469616d7c5eSToby Isaac        So type punning should be portable. */
470616d7c5eSToby Isaac     union { PetscComplex z; PetscReal f[2]; } uz;
471616d7c5eSToby Isaac 
472616d7c5eSToby Isaac     uz.f[0] = x;
473616d7c5eSToby Isaac     uz.f[1] = y;
474616d7c5eSToby Isaac     return uz.z;
475616d7c5eSToby Isaac   }
47650f81f78SJed Brown #endif
4778a351411SToby Isaac }
4788a351411SToby Isaac #endif
4798a351411SToby Isaac 
480e489efc1SBarry Smith 
481b6a5bde7SBarry Smith /*MC
482b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
483b6a5bde7SBarry Smith 
484eca87e8dSBarry Smith    Synopsis:
485aaa7dc30SBarry Smith    #include <petscmath.h>
486eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
487eca87e8dSBarry Smith 
488eca87e8dSBarry Smith    Not Collective
489eca87e8dSBarry Smith 
490b6a5bde7SBarry Smith    Input Parameter:
491b6a5bde7SBarry Smith +  v1 - first value to find minimum of
492b6a5bde7SBarry Smith -  v2 - second value to find minimum of
493b6a5bde7SBarry Smith 
49495452b02SPatrick Sanan    Notes:
49595452b02SPatrick Sanan     type can be integer or floating point value
496b6a5bde7SBarry Smith 
497b6a5bde7SBarry Smith    Level: beginner
498b6a5bde7SBarry Smith 
4991175f9beSHong Zhang .seealso: PetscMax(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
500b6a5bde7SBarry Smith 
501b6a5bde7SBarry Smith M*/
502e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
503b6a5bde7SBarry Smith 
504b6a5bde7SBarry Smith /*MC
505b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
506b6a5bde7SBarry Smith 
507eca87e8dSBarry Smith    Synopsis:
508aaa7dc30SBarry Smith    #include <petscmath.h>
509eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
510eca87e8dSBarry Smith 
511eca87e8dSBarry Smith    Not Collective
512eca87e8dSBarry Smith 
513b6a5bde7SBarry Smith    Input Parameter:
514b6a5bde7SBarry Smith +  v1 - first value to find maximum of
515b6a5bde7SBarry Smith -  v2 - second value to find maximum of
516b6a5bde7SBarry Smith 
51795452b02SPatrick Sanan    Notes:
51895452b02SPatrick Sanan     type can be integer or floating point value
519b6a5bde7SBarry Smith 
520b6a5bde7SBarry Smith    Level: beginner
521b6a5bde7SBarry Smith 
522d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
523b6a5bde7SBarry Smith 
524b6a5bde7SBarry Smith M*/
525e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
526b6a5bde7SBarry Smith 
527b6a5bde7SBarry Smith /*MC
528d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
529d9a4bb16SJed Brown 
530d9a4bb16SJed Brown    Synopsis:
531aaa7dc30SBarry Smith    #include <petscmath.h>
532d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
533d9a4bb16SJed Brown 
534d9a4bb16SJed Brown    Not Collective
535d9a4bb16SJed Brown 
536d9a4bb16SJed Brown    Input Parameter:
537d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
538d9a4bb16SJed Brown .  a - lower end of interval
539d9a4bb16SJed Brown -  b - upper end of interval
540d9a4bb16SJed Brown 
54195452b02SPatrick Sanan    Notes:
54295452b02SPatrick Sanan     type can be integer or floating point value
543d9a4bb16SJed Brown 
544d9a4bb16SJed Brown    Level: beginner
545d9a4bb16SJed Brown 
546d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
547d9a4bb16SJed Brown 
548d9a4bb16SJed Brown M*/
549d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
550d9a4bb16SJed Brown 
551d9a4bb16SJed Brown /*MC
552b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
553b6a5bde7SBarry Smith 
554b6a5bde7SBarry Smith    Synopsis:
555aaa7dc30SBarry Smith    #include <petscmath.h>
556b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
557b6a5bde7SBarry Smith 
558eca87e8dSBarry Smith    Not Collective
559eca87e8dSBarry Smith 
560eca87e8dSBarry Smith    Input Parameter:
561eca87e8dSBarry Smith .   v1 - the integer
562b6a5bde7SBarry Smith 
563b6a5bde7SBarry Smith    Level: beginner
564b6a5bde7SBarry Smith 
565b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
566b6a5bde7SBarry Smith 
567b6a5bde7SBarry Smith M*/
5689fa7d148SSatish Balay #define PetscAbsInt(a)  (((a)<0)   ? (-(a)) : (a))
569b6a5bde7SBarry Smith 
570b6a5bde7SBarry Smith /*MC
571b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
572b6a5bde7SBarry Smith 
573eca87e8dSBarry Smith    Synopsis:
574aaa7dc30SBarry Smith    #include <petscmath.h>
575eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
576eca87e8dSBarry Smith 
577eca87e8dSBarry Smith    Not Collective
578eca87e8dSBarry Smith 
579b6a5bde7SBarry Smith    Input Parameter:
580b6a5bde7SBarry Smith .   v1 - the double
581b6a5bde7SBarry Smith 
582b6a5bde7SBarry Smith 
583b6a5bde7SBarry Smith    Level: beginner
584b6a5bde7SBarry Smith 
585b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
586b6a5bde7SBarry Smith 
587b6a5bde7SBarry Smith M*/
5881118d4bcSLisandro Dalcin #if defined(PETSC_USE_REAL_SINGLE)
5891118d4bcSLisandro Dalcin #define PetscAbsReal(a) fabsf(a)
5901118d4bcSLisandro Dalcin #elif defined(PETSC_USE_REAL_DOUBLE)
5911118d4bcSLisandro Dalcin #define PetscAbsReal(a) fabs(a)
5921118d4bcSLisandro Dalcin #elif defined(PETSC_USE_REAL___FLOAT128)
5931118d4bcSLisandro Dalcin #define PetscAbsReal(a) fabsq(a)
5941118d4bcSLisandro Dalcin #elif defined(PETSC_USE_REAL___FP16)
5951118d4bcSLisandro Dalcin #define PetscAbsReal(a) fabsf(a)
5961118d4bcSLisandro Dalcin #endif
597b6a5bde7SBarry Smith 
598b6a5bde7SBarry Smith /*MC
599b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
600b6a5bde7SBarry Smith 
601b6a5bde7SBarry Smith    Synopsis:
602aaa7dc30SBarry Smith    #include <petscmath.h>
603b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
604b6a5bde7SBarry Smith 
605eca87e8dSBarry Smith    Not Collective
606eca87e8dSBarry Smith 
607eca87e8dSBarry Smith    Input Parameter:
608eca87e8dSBarry Smith .   v1 - the value
609eca87e8dSBarry Smith 
61095452b02SPatrick Sanan    Notes:
61195452b02SPatrick Sanan     type can be integer or floating point value
612b6a5bde7SBarry Smith 
613b6a5bde7SBarry Smith    Level: beginner
614b6a5bde7SBarry Smith 
615b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
616b6a5bde7SBarry Smith 
617b6a5bde7SBarry Smith M*/
6184ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
619e489efc1SBarry Smith 
620314da920SBarry Smith /* ----------------------------------------------------------------------------*/
621ee223c85SLisandro Dalcin 
622ee223c85SLisandro Dalcin #if defined(PETSC_USE_REAL_SINGLE)
623ee223c85SLisandro Dalcin #define PetscRealConstant(constant) constant##F
624ee223c85SLisandro Dalcin #elif defined(PETSC_USE_REAL___FLOAT128)
625ee223c85SLisandro Dalcin #define PetscRealConstant(constant) constant##Q
626ee223c85SLisandro Dalcin #else
627ee223c85SLisandro Dalcin #define PetscRealConstant(constant) constant
628ee223c85SLisandro Dalcin #endif
629ee223c85SLisandro Dalcin 
630314da920SBarry Smith /*
631d34fcf5fSBarry Smith      Basic constants
632314da920SBarry Smith */
6332fab75feSLisandro Dalcin #define PETSC_PI   PetscRealConstant(3.1415926535897932384626433832795029)
6342fab75feSLisandro Dalcin #define PETSC_PHI  PetscRealConstant(1.6180339887498948482045868343656381)
635d34fcf5fSBarry Smith 
636ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
63771fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
638ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
639ab824b78SBarry Smith #else
640ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
641ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
642ab824b78SBarry Smith #endif
643e489efc1SBarry Smith 
644ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
645ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
6469fa7d148SSatish Balay #  define PETSC_MIN_REAL                (-PETSC_MAX_REAL)
64782a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
64882a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
649ee223c85SLisandro Dalcin #  define PETSC_SMALL                   1.e-5F
650ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
651ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
6529fa7d148SSatish Balay #  define PETSC_MIN_REAL                (-PETSC_MAX_REAL)
65382a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
65482a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
655cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
656ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
657ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
6589fa7d148SSatish Balay #  define PETSC_MIN_REAL                (-FLT128_MAX)
659d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
660ee223c85SLisandro Dalcin #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078144567552953958511352539e-17Q
661ee223c85SLisandro Dalcin #  define PETSC_SMALL                   1.e-20Q
662570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)  /* maybe should use single precision values for these? */
663570b7f6dSBarry Smith #  define PETSC_MAX_REAL                65504.
6649fa7d148SSatish Balay #  define PETSC_MIN_REAL                (-PETSC_MAX_REAL)
665570b7f6dSBarry Smith #  define PETSC_MACHINE_EPSILON         .00097656
666570b7f6dSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    .0312
667570b7f6dSBarry Smith #  define PETSC_SMALL                   5.e-3
6689cf09972SJed Brown #endif
6693e523bebSBarry Smith 
67025d0f998SSatish Balay #define PETSC_INFINITY               (PETSC_MAX_REAL/4)
6719fa7d148SSatish Balay #define PETSC_NINFINITY              (-PETSC_INFINITY)
672e270355aSBarry Smith 
6739f4f8022SLisandro Dalcin PETSC_EXTERN PetscBool PetscIsInfReal(PetscReal);
6743948c36eSLisandro Dalcin PETSC_EXTERN PetscBool PetscIsNanReal(PetscReal);
6758b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalReal(PetscReal);
6769f4f8022SLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscIsInfOrNanReal(PetscReal v) {return PetscIsInfReal(v) || PetscIsNanReal(v) ? PETSC_TRUE : PETSC_FALSE;}
6779f4f8022SLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscIsInfScalar(PetscScalar v) {return PetscIsInfReal(PetscAbsScalar(v));}
6783948c36eSLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscIsNanScalar(PetscScalar v) {return PetscIsNanReal(PetscAbsScalar(v));}
6799f4f8022SLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscIsInfOrNanScalar(PetscScalar v) {return PetscIsInfOrNanReal(PetscAbsScalar(v));}
6803948c36eSLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscIsNormalScalar(PetscScalar v) {return PetscIsNormalReal(PetscAbsScalar(v));}
6819a25a3ccSBarry Smith 
682ce4818fdSLisandro Dalcin PETSC_EXTERN PetscBool PetscEqualReal(PetscReal,PetscReal);
683ce4818fdSLisandro Dalcin PETSC_EXTERN PetscBool PetscEqualScalar(PetscScalar,PetscScalar);
684ce4818fdSLisandro Dalcin 
68598725619SBarry Smith /*
68698725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
68798725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
68898725619SBarry Smith  */
68998725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
69098725619SBarry Smith typedef PetscScalar MatScalar;
69198725619SBarry Smith typedef PetscReal MatReal;
69298725619SBarry Smith 
6938ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
6948ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
6958ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
6968ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
6978ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
6988ad47952SJed Brown #else
6998ad47952SJed Brown #define MPIU_2INT MPI_2INT
7008ad47952SJed Brown #endif
701e9fa29b7SSatish Balay 
702b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power)
703b2fb0278SBarry Smith {
704fa711258SJed Brown   PetscInt result = 1;
705fa711258SJed Brown   while (power) {
706fa711258SJed Brown     if (power & 1) result *= base;
707fa711258SJed Brown     power >>= 1;
708fa711258SJed Brown     base *= base;
709fa711258SJed Brown   }
710fa711258SJed Brown   return result;
711fa711258SJed Brown }
712b2fb0278SBarry Smith 
713b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power)
714b2fb0278SBarry Smith {
715fa711258SJed Brown   PetscReal result = 1;
716d98d5da7SBarry Smith   if (power < 0) {
717d98d5da7SBarry Smith     power = -power;
71810d40e53SLisandro Dalcin     base  = ((PetscReal)1)/base;
719d98d5da7SBarry Smith   }
720fa711258SJed Brown   while (power) {
721fa711258SJed Brown     if (power & 1) result *= base;
722fa711258SJed Brown     power >>= 1;
723fa711258SJed Brown     base *= base;
724fa711258SJed Brown   }
725fa711258SJed Brown   return result;
726fa711258SJed Brown }
727fa711258SJed Brown 
728b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarInt(PetscScalar base,PetscInt power)
729b2fb0278SBarry Smith {
7308b49ba18SBarry Smith   PetscScalar result = 1;
7318b49ba18SBarry Smith   if (power < 0) {
7328b49ba18SBarry Smith     power = -power;
73310d40e53SLisandro Dalcin     base  = ((PetscReal)1)/base;
7348b49ba18SBarry Smith   }
7358b49ba18SBarry Smith   while (power) {
7368b49ba18SBarry Smith     if (power & 1) result *= base;
7378b49ba18SBarry Smith     power >>= 1;
7388b49ba18SBarry Smith     base *= base;
7398b49ba18SBarry Smith   }
7408b49ba18SBarry Smith   return result;
7418b49ba18SBarry Smith }
7428b49ba18SBarry Smith 
743b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarReal(PetscScalar base,PetscReal power)
744b2fb0278SBarry Smith {
745b2fb0278SBarry Smith   PetscScalar cpower = power;
746b2fb0278SBarry Smith   return PetscPowScalar(base,cpower);
747b2fb0278SBarry Smith }
74878a59e97SMatthew G. Knepley 
74978a59e97SMatthew G. Knepley #ifndef PETSC_HAVE_LOG2
75078a59e97SMatthew G. Knepley PETSC_STATIC_INLINE PetscReal PetscLog2Real(PetscReal n)
75178a59e97SMatthew G. Knepley {
75291954be4SBarry Smith   return PetscLogReal(n)/PetscLogReal(2.0);
75378a59e97SMatthew G. Knepley }
75478a59e97SMatthew G. Knepley #endif
755e489efc1SBarry Smith #endif
756