xref: /petsc/include/petscmath.h (revision df4397b0113739eddce7836635e612aecb0144dc)
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>
14*df4397b0SStefano Zampini #include <petscsystypes.h>
15*df4397b0SStefano Zampini 
16*df4397b0SStefano Zampini /*MC
17*df4397b0SStefano Zampini    MPIU_REAL - MPI datatype corresponding to PetscReal
18*df4397b0SStefano Zampini 
19*df4397b0SStefano Zampini    Notes:
20*df4397b0SStefano Zampini    In MPI calls that require an MPI datatype that matches a PetscReal or array of PetscReal values, pass this value.
21*df4397b0SStefano Zampini 
22*df4397b0SStefano Zampini    Level: beginner
23*df4397b0SStefano Zampini 
24*df4397b0SStefano Zampini .seealso: PetscReal, PetscScalar, PetscComplex, PetscInt, MPIU_SCALAR, MPIU_COMPLEX, MPIU_INT
25*df4397b0SStefano Zampini M*/
260a5f7794SBarry Smith 
27314da920SBarry Smith /*
28f4ccad53SBarry Smith 
29f4ccad53SBarry Smith      Defines operations that are different for complex and real numbers;
30a5057860SBarry Smith    note that one cannot mix the use of complex and real in the same
31f4ccad53SBarry Smith    PETSc program. All PETSc objects in one program are built around the object
3298725619SBarry Smith    PetscScalar which is either always a real or a complex.
33f4ccad53SBarry Smith 
34e489efc1SBarry Smith */
35b36a9721SBarry Smith 
36c1d390e3SJed Brown #if defined(PETSC_USE_REAL_SINGLE)
37c1d390e3SJed Brown #define MPIU_REAL   MPI_FLOAT
38a97cf724SLisandro Dalcin #define PetscRoundReal(a)   roundf(a)
39a97cf724SLisandro Dalcin #define PetscSqrtReal(a)    sqrtf(a)
40a97cf724SLisandro Dalcin #define PetscExpReal(a)     expf(a)
41a97cf724SLisandro Dalcin #define PetscLogReal(a)     logf(a)
42a97cf724SLisandro Dalcin #define PetscLog10Real(a)   log10f(a)
4378a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
44a97cf724SLisandro Dalcin #define PetscLog2Real(a)    log2f(a)
4578a59e97SMatthew G. Knepley #endif
46a97cf724SLisandro Dalcin #define PetscSinReal(a)     sinf(a)
47a97cf724SLisandro Dalcin #define PetscCosReal(a)     cosf(a)
48a97cf724SLisandro Dalcin #define PetscTanReal(a)     tanf(a)
49a97cf724SLisandro Dalcin #define PetscAsinReal(a)    asinf(a)
50a97cf724SLisandro Dalcin #define PetscAcosReal(a)    acosf(a)
51a97cf724SLisandro Dalcin #define PetscAtanReal(a)    atanf(a)
52a97cf724SLisandro Dalcin #define PetscAtan2Real(a,b) atan2f(a,b)
53a97cf724SLisandro Dalcin #define PetscSinhReal(a)    sinhf(a)
54a97cf724SLisandro Dalcin #define PetscCoshReal(a)    coshf(a)
55a97cf724SLisandro Dalcin #define PetscTanhReal(a)    tanhf(a)
56a97cf724SLisandro Dalcin #define PetscPowReal(a,b)   powf(a,b)
57a97cf724SLisandro Dalcin #define PetscCeilReal(a)    ceilf(a)
58a97cf724SLisandro Dalcin #define PetscFloorReal(a)   floorf(a)
59a97cf724SLisandro Dalcin #define PetscFmodReal(a,b)  fmodf(a,b)
6043f0d3baSJed Brown #define PetscTGamma(a)      tgammaf(a)
61c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
62c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
63814a3092SSatish Balay #define PetscRoundReal(a)   round(a)
648f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
659a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
669a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
6777b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
6878a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
6978a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2(a)
7078a59e97SMatthew G. Knepley #endif
719a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
729a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
73a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
74255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
75255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
76a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
771d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2(a,b)
788d1b9be0SJed Brown #define PetscSinhReal(a)    sinh(a)
798d1b9be0SJed Brown #define PetscCoshReal(a)    cosh(a)
808d1b9be0SJed Brown #define PetscTanhReal(a)    tanh(a)
81369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
8277b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
8377b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
84a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
850646a658SBarry Smith #define PetscTGamma(a)      tgamma(a)
86c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
87574fde7bSSatish Balay #if defined(__cplusplus)
88574fde7bSSatish Balay extern "C" {
89574fde7bSSatish Balay #endif
90574fde7bSSatish Balay #include <quadmath.h>
91574fde7bSSatish Balay #if defined(__cplusplus)
92574fde7bSSatish Balay }
93574fde7bSSatish Balay #endif
948ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
95c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
96814a3092SSatish Balay #define PetscRoundReal(a)   roundq(a)
978f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
989a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
999a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
10077b4d14cSPeter Brune #define PetscLog10Real(a)   log10q(a)
10178a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
10278a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2q(a)
10378a59e97SMatthew G. Knepley #endif
1049a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
1059a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
106a4bea5a6SPeter Brune #define PetscTanReal(a)     tanq(a)
107255453a1SBarry Smith #define PetscAsinReal(a)    asinq(a)
108255453a1SBarry Smith #define PetscAcosReal(a)    acosq(a)
109a4bea5a6SPeter Brune #define PetscAtanReal(a)    atanq(a)
1101d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2q(a,b)
1118d1b9be0SJed Brown #define PetscSinhReal(a)    sinhq(a)
1128d1b9be0SJed Brown #define PetscCoshReal(a)    coshq(a)
1138d1b9be0SJed Brown #define PetscTanhReal(a)    tanhq(a)
114369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
11577b4d14cSPeter Brune #define PetscCeilReal(a)    ceilq(a)
11677b4d14cSPeter Brune #define PetscFloorReal(a)   floorq(a)
117a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmodq(a,b)
1180646a658SBarry Smith #define PetscTGamma(a)      tgammaq(a)
119570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)
120570b7f6dSBarry Smith PETSC_EXTERN MPI_Datatype MPIU___FP16 PetscAttrMPITypeTag(__fp16);
121570b7f6dSBarry Smith #define MPIU_REAL MPIU___FP16
1225a0908ecSJose E. Roman #define PetscRoundReal(a)   roundf(a)
123570b7f6dSBarry Smith #define PetscSqrtReal(a)    sqrtf(a)
124570b7f6dSBarry Smith #define PetscExpReal(a)     expf(a)
125570b7f6dSBarry Smith #define PetscLogReal(a)     logf(a)
126570b7f6dSBarry Smith #define PetscLog10Real(a)   log10f(a)
127570b7f6dSBarry Smith #ifdef PETSC_HAVE_LOG2
128570b7f6dSBarry Smith #define PetscLog2Real(a)    log2f(a)
129570b7f6dSBarry Smith #endif
130570b7f6dSBarry Smith #define PetscSinReal(a)     sinf(a)
131570b7f6dSBarry Smith #define PetscCosReal(a)     cosf(a)
132570b7f6dSBarry Smith #define PetscTanReal(a)     tanf(a)
133570b7f6dSBarry Smith #define PetscAsinReal(a)    asinf(a)
134570b7f6dSBarry Smith #define PetscAcosReal(a)    acosf(a)
135570b7f6dSBarry Smith #define PetscAtanReal(a)    atanf(a)
136570b7f6dSBarry Smith #define PetscAtan2Real(a,b) atan2f(a,b)
137570b7f6dSBarry Smith #define PetscSinhReal(a)    sinhf(a)
138570b7f6dSBarry Smith #define PetscCoshReal(a)    coshf(a)
139570b7f6dSBarry Smith #define PetscTanhReal(a)    tanhf(a)
140570b7f6dSBarry Smith #define PetscPowReal(a,b)   powf(a,b)
141570b7f6dSBarry Smith #define PetscCeilReal(a)    ceilf(a)
142570b7f6dSBarry Smith #define PetscFloorReal(a)   floorf(a)
143570b7f6dSBarry Smith #define PetscFmodReal(a,b)  fmodf(a,b)
144570b7f6dSBarry Smith #define PetscTGamma(a)      tgammaf(a)
145c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
14659cb5930SBarry Smith 
147*df4397b0SStefano Zampini /*MC
148*df4397b0SStefano Zampini    MPIU_COMPLEX - MPI datatype corresponding to PetscComplex
149*df4397b0SStefano Zampini 
150*df4397b0SStefano Zampini    Notes:
151*df4397b0SStefano Zampini    In MPI calls that require an MPI datatype that matches a PetscComplex or array of PetscComplex values, pass this value.
152*df4397b0SStefano Zampini 
153*df4397b0SStefano Zampini    Level: beginner
154*df4397b0SStefano Zampini 
155*df4397b0SStefano Zampini .seealso: PetscReal, PetscScalar, PetscComplex, PetscInt, MPIU_REAL, MPIU_SCALAR, MPIU_COMPLEX, MPIU_INT, PETSC_i
156*df4397b0SStefano Zampini M*/
157*df4397b0SStefano Zampini 
1581093a601SBarry Smith /*
1591093a601SBarry Smith     Complex number definitions
1601093a601SBarry Smith  */
1612f217381SBarry Smith #if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
162*df4397b0SStefano Zampini #if defined(PETSC_HAVE_COMPLEX)
1631093a601SBarry Smith /* C++ support of complex number */
164b7940d39SSatish Balay 
16550f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
16650f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
167*df4397b0SStefano Zampini #define PetscAbsComplex(a)           petsccomplexlib::abs(a)
168*df4397b0SStefano Zampini #define PetscConjComplex(a)          petsccomplexlib::conj(a)
169*df4397b0SStefano Zampini #define PetscSqrtComplex(a)          petsccomplexlib::sqrt(a)
170*df4397b0SStefano Zampini #define PetscPowComplex(a,b)         petsccomplexlib::pow(a,b)
171*df4397b0SStefano Zampini #define PetscExpComplex(a)           petsccomplexlib::exp(a)
172*df4397b0SStefano Zampini #define PetscLogComplex(a)           petsccomplexlib::log(a)
173*df4397b0SStefano Zampini #define PetscSinComplex(a)           petsccomplexlib::sin(a)
174*df4397b0SStefano Zampini #define PetscCosComplex(a)           petsccomplexlib::cos(a)
175*df4397b0SStefano Zampini #define PetscAsinComplex(a)          petsccomplexlib::asin(a)
176*df4397b0SStefano Zampini #define PetscAcosComplex(a)          petsccomplexlib::acos(a)
177027d9794SBarry Smith #if defined(PETSC_HAVE_TANCOMPLEX)
178*df4397b0SStefano Zampini #define PetscTanComplex(a)           petsccomplexlib::tan(a)
179027d9794SBarry Smith #else
180027d9794SBarry Smith #define PetscTanComplex(a)           PetscSinComplex(a)/PetscCosComplex(a)
181027d9794SBarry Smith #endif
182*df4397b0SStefano Zampini #define PetscSinhComplex(a)          petsccomplexlib::sinh(a)
183*df4397b0SStefano Zampini #define PetscCoshComplex(a)          petsccomplexlib::cosh(a)
184027d9794SBarry Smith #if defined(PETSC_HAVE_TANHCOMPLEX)
185*df4397b0SStefano Zampini #define PetscTanhComplex(a)          petsccomplexlib::tanh(a)
186027d9794SBarry Smith #else
187027d9794SBarry Smith #define PetscTanhComplex(a)          PetscSinhComplex(a)/PetscCoshComplex(a)
188027d9794SBarry Smith #endif
189debe9ee2SPaul Mullowney 
190debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
1913be776deSLisandro Dalcin #if defined(PETSC_USE_CXX_COMPLEX_FLOAT_WORKAROUND)
1923be776deSLisandro Dalcin static inline PetscComplex operator+(const PetscComplex& lhs, const double& rhs) { return lhs + float(rhs); }
1933be776deSLisandro Dalcin static inline PetscComplex operator+(const double& lhs, const PetscComplex& rhs) { return float(lhs) + rhs; }
1943be776deSLisandro Dalcin static inline PetscComplex operator-(const PetscComplex& lhs, const double& rhs) { return lhs - float(rhs); }
1953be776deSLisandro Dalcin static inline PetscComplex operator-(const double& lhs, const PetscComplex& rhs) { return float(lhs) - rhs; }
1963be776deSLisandro Dalcin static inline PetscComplex operator*(const PetscComplex& lhs, const double& rhs) { return lhs * float(rhs); }
1973be776deSLisandro Dalcin static inline PetscComplex operator*(const double& lhs, const PetscComplex& rhs) { return float(lhs) * rhs; }
1983be776deSLisandro Dalcin static inline PetscComplex operator/(const PetscComplex& lhs, const double& rhs) { return lhs / float(rhs); }
1993be776deSLisandro Dalcin static inline PetscComplex operator/(const double& lhs, const PetscComplex& rhs) { return float(lhs) / rhs; }
2003be776deSLisandro Dalcin static inline bool operator==(const PetscComplex& lhs, const double& rhs) { return lhs.imag() == float(0) && lhs.real() == float(rhs); }
2013be776deSLisandro Dalcin static inline bool operator==(const double& lhs, const PetscComplex& rhs) { return rhs.imag() == float(0) && rhs.real() == float(lhs); }
2023be776deSLisandro Dalcin static inline bool operator!=(const PetscComplex& lhs, const double& rhs) { return lhs.imag() != float(0) || lhs.real() != float(rhs); }
2033be776deSLisandro Dalcin static inline bool operator!=(const double& lhs, const PetscComplex& rhs) { return rhs.imag() != float(0) || rhs.real() != float(lhs); }
2043be776deSLisandro Dalcin #endif  /* PETSC_USE_CXX_COMPLEX_FLOAT_WORKAROUND */
205debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
2061a9c6b96SLisandro Dalcin #if defined(PETSC_USE_CXX_COMPLEX_FLOAT_WORKAROUND)
2071a9c6b96SLisandro Dalcin static inline PetscComplex operator+(const PetscComplex& lhs, const PetscInt& rhs) { return lhs + double(rhs); }
2081a9c6b96SLisandro Dalcin static inline PetscComplex operator+(const PetscInt& lhs, const PetscComplex& rhs) { return double(lhs) + rhs; }
2091a9c6b96SLisandro Dalcin static inline PetscComplex operator-(const PetscComplex& lhs, const PetscInt& rhs) { return lhs - double(rhs); }
2101a9c6b96SLisandro Dalcin static inline PetscComplex operator-(const PetscInt& lhs, const PetscComplex& rhs) { return double(lhs) - rhs; }
2111a9c6b96SLisandro Dalcin static inline PetscComplex operator*(const PetscComplex& lhs, const PetscInt& rhs) { return lhs * double(rhs); }
2121a9c6b96SLisandro Dalcin static inline PetscComplex operator*(const PetscInt& lhs, const PetscComplex& rhs) { return double(lhs) * rhs; }
2131a9c6b96SLisandro Dalcin static inline PetscComplex operator/(const PetscComplex& lhs, const PetscInt& rhs) { return lhs / double(rhs); }
2141a9c6b96SLisandro Dalcin static inline PetscComplex operator/(const PetscInt& lhs, const PetscComplex& rhs) { return double(lhs) / rhs; }
2151a9c6b96SLisandro Dalcin static inline bool operator==(const PetscComplex& lhs, const PetscInt& rhs) { return lhs.imag() == double(0) && lhs.real() == double(rhs); }
2161a9c6b96SLisandro Dalcin static inline bool operator==(const PetscInt& lhs, const PetscComplex& rhs) { return rhs.imag() == double(0) && rhs.real() == double(lhs); }
2171a9c6b96SLisandro Dalcin static inline bool operator!=(const PetscComplex& lhs, const PetscInt& rhs) { return lhs.imag() != double(0) || lhs.real() != double(rhs); }
2181a9c6b96SLisandro Dalcin static inline bool operator!=(const PetscInt& lhs, const PetscComplex& rhs) { return rhs.imag() != double(0) || rhs.real() != double(lhs); }
2191a9c6b96SLisandro Dalcin #endif  /* PETSC_USE_CXX_COMPLEX_FLOAT_WORKAROUND */
2208c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
22122b3908eSBarry Smith PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128;
222*df4397b0SStefano Zampini #endif
223*df4397b0SStefano Zampini #endif  /* PETSC_HAVE_COMPLEX */
224debe9ee2SPaul Mullowney 
225546cf897SSatish Balay #elif defined(PETSC_HAVE_C99_COMPLEX) && !defined(PETSC_USE_REAL___FP16)
226*df4397b0SStefano Zampini #if defined(PETSC_HAVE_COMPLEX)
227519e2a1fSPaul Mullowney 
228570b7f6dSBarry Smith #if defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL___FP16)
22950f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
23050f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
23150f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
23250f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
23350f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
23450f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
23550f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
23650f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
23750f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
23850f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
239255453a1SBarry Smith #define PetscAsinComplex(a)          casinf(a)
240255453a1SBarry Smith #define PetscAcosComplex(a)          cacosf(a)
241a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanf(a)
242a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhf(a)
243a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshf(a)
244a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhf(a)
2451093a601SBarry Smith 
246ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
24750f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
24850f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
24950f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
25050f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
25150f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
25250f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
25350f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
25450f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
25550f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
25650f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
257255453a1SBarry Smith #define PetscAsinComplex(a)          casin(a)
258255453a1SBarry Smith #define PetscAcosComplex(a)          cacos(a)
259a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctan(a)
260a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinh(a)
261a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccosh(a)
262a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanh(a)
2631093a601SBarry Smith 
2648c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
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_* */
285*df4397b0SStefano Zampini #endif /* PETSC_HAVE_COMPLEX */
2862f217381SBarry Smith #endif /* (__cplusplus && PETSC_HAVE_CXX_COMPLEX) else-if (!__cplusplus && PETSC_HAVE_C99_COMPLEX) */
287e489efc1SBarry Smith 
2888dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
28970da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
290500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
291500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
29270da9c3bSJed Brown #else
293252ecd31SSatish Balay # if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
294*df4397b0SStefano Zampini   typedef petsccomplexlib::complex<double> petsc_mpiu_c_double_complex;
295*df4397b0SStefano Zampini   typedef petsccomplexlib::complex<float> petsc_mpiu_c_complex;
2962f217381SBarry Smith # elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
2978ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
2988ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
2998ad47952SJed Brown # else
3008ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
3018ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
3028ad47952SJed Brown # endif
3038ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
3048ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
3051b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
3068dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
3072c876bd9SBarry Smith 
3087c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
309570b7f6dSBarry Smith #  if defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL___FP16)
3107c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
3117c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
3127c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
3137c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
3147c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
3157c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
3167c2de775SJed Brown #endif
3177c2de775SJed Brown 
318*df4397b0SStefano Zampini /*MC
319*df4397b0SStefano Zampini    MPIU_SCALAR - MPI datatype corresponding to PetscScalar
320*df4397b0SStefano Zampini 
321*df4397b0SStefano Zampini    Notes:
322*df4397b0SStefano Zampini    In MPI calls that require an MPI datatype that matches a PetscScalar or array of PetscScalar values, pass this value.
323*df4397b0SStefano Zampini 
324*df4397b0SStefano Zampini    Level: beginner
325*df4397b0SStefano Zampini 
326*df4397b0SStefano Zampini .seealso: PetscReal, PetscScalar, PetscComplex, PetscInt, MPIU_REAL, MPIU_COMPLEX, MPIU_INT
327*df4397b0SStefano Zampini M*/
328*df4397b0SStefano Zampini 
329d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
33059479656SBarry Smith 
33159479656SBarry Smith /*MC
33259479656SBarry Smith    PetscRealPart - Returns the real part of a PetscScalar
33359479656SBarry Smith 
33459479656SBarry Smith    Synopsis:
33559479656SBarry Smith    #include <petscmath.h>
33659479656SBarry Smith    PetscScalar PetscRealPart(PetscScalar v)
33759479656SBarry Smith 
33859479656SBarry Smith    Not Collective
33959479656SBarry Smith 
34059479656SBarry Smith    Input Parameter:
34159479656SBarry Smith .  v - value to find the real part of
34259479656SBarry Smith 
34359479656SBarry Smith    Level: beginner
34459479656SBarry Smith 
34559479656SBarry Smith .seealso: PetscScalar, PetscImaginaryPart(), PetscMax(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
34659479656SBarry Smith 
34759479656SBarry Smith M*/
34850f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
34959479656SBarry Smith 
35059479656SBarry Smith /*MC
35159479656SBarry Smith    PetscImaginaryPart - Returns the imaginary part of a PetscScalar
35259479656SBarry Smith 
35359479656SBarry Smith    Synopsis:
35459479656SBarry Smith    #include <petscmath.h>
35559479656SBarry Smith    PetscScalar PetscImaginaryPart(PetscScalar v)
35659479656SBarry Smith 
35759479656SBarry Smith    Not Collective
35859479656SBarry Smith 
35959479656SBarry Smith    Input Parameter:
36059479656SBarry Smith .  v - value to find the imaginary part of
36159479656SBarry Smith 
36259479656SBarry Smith    Level: beginner
36359479656SBarry Smith 
36459479656SBarry Smith    Notes:
36559479656SBarry Smith        If PETSc was configured for real numbers then this always returns the value 0
36659479656SBarry Smith 
36759479656SBarry Smith .seealso: PetscScalar, PetscRealPart(), PetscMax(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
36859479656SBarry Smith 
36959479656SBarry Smith M*/
37050f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
37159479656SBarry Smith 
37250f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
37350f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
37450f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
37550f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
37650f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
37750f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
37850f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
37950f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
380255453a1SBarry Smith #define PetscAsinScalar(a)    PetscAsinComplex(a)
381255453a1SBarry Smith #define PetscAcosScalar(a)    PetscAcosComplex(a)
382a4bea5a6SPeter Brune #define PetscTanScalar(a)     PetscTanComplex(a)
383a4bea5a6SPeter Brune #define PetscSinhScalar(a)    PetscSinhComplex(a)
384a4bea5a6SPeter Brune #define PetscCoshScalar(a)    PetscCoshComplex(a)
385a4bea5a6SPeter Brune #define PetscTanhScalar(a)    PetscTanhComplex(a)
3867c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
38775567043SBarry Smith 
3881093a601SBarry Smith /*
3891093a601SBarry Smith     real number definitions
3901093a601SBarry Smith  */
3911b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
3927c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
3937c2de775SJed Brown 
394329f5518SBarry Smith #define PetscRealPart(a)      (a)
395c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
396c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
397e489efc1SBarry Smith #define PetscConj(a)          (a)
398570b7f6dSBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)  && !defined(PETSC_USE_REAL___FP16)
39918a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
400184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
401184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
40206c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
403184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
404184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
405255453a1SBarry Smith #define PetscAsinScalar(a)    asin(a)
406255453a1SBarry Smith #define PetscAcosScalar(a)    acos(a)
407a4bea5a6SPeter Brune #define PetscTanScalar(a)     tan(a)
408a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinh(a)
409a4bea5a6SPeter Brune #define PetscCoshScalar(a)    cosh(a)
410a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanh(a)
411570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)
412570b7f6dSBarry Smith #define PetscSqrtScalar(a)    sqrtf(a)
413570b7f6dSBarry Smith #define PetscPowScalar(a,b)   powf(a,b)
414570b7f6dSBarry Smith #define PetscExpScalar(a)     expf(a)
415570b7f6dSBarry Smith #define PetscLogScalar(a)     logf(a)
416570b7f6dSBarry Smith #define PetscSinScalar(a)     sinf(a)
417570b7f6dSBarry Smith #define PetscCosScalar(a)     cosf(a)
418570b7f6dSBarry Smith #define PetscAsinScalar(a)    asinf(a)
419570b7f6dSBarry Smith #define PetscAcosScalar(a)    acosf(a)
420570b7f6dSBarry Smith #define PetscTanScalar(a)     tanf(a)
421570b7f6dSBarry Smith #define PetscSinhScalar(a)    sinhf(a)
422570b7f6dSBarry Smith #define PetscCoshScalar(a)    coshf(a)
423570b7f6dSBarry Smith #define PetscTanhScalar(a)    tanhf(a)
424ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
4250d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
4260d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
4270d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
4280d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
4290d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
4300d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
431255453a1SBarry Smith #define PetscAsinScalar(a)    asinq(a)
432255453a1SBarry Smith #define PetscAcosScalar(a)    acosq(a)
433a4bea5a6SPeter Brune #define PetscTanScalar(a)     tanq(a)
434a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinhq(a)
435a4bea5a6SPeter Brune #define PetscCoshScalar(a)    coshq(a)
436a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanhq(a)
437ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
438b0a32e0cSBarry Smith 
4391b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
440e489efc1SBarry Smith 
441da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
44236850ab7SBarry Smith #define PetscSignReal(a) (((a) >= 0.0) ? ((a) == 0.0 ? 0.0 : 1.0) : -1.0)
4439fa7d148SSatish Balay #define PetscAbs(a)  (((a) >= 0) ? (a) : (-(a)))
4443f1db9ecSBarry Smith 
445314da920SBarry Smith /* --------------------------------------------------------------------------*/
446314da920SBarry Smith 
447e489efc1SBarry Smith /*
448f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
449f22f69f0SBarry Smith    This is currently not used.
450e489efc1SBarry Smith */
451570b7f6dSBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE, PETSC_SCALAR_HALF } PetscScalarPrecision;
452e489efc1SBarry Smith 
45350f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
454e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
45550f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
4568a351411SToby Isaac 
4578a351411SToby Isaac /* Try to do the right thing for complex number construction: see
4588a351411SToby Isaac 
4598a351411SToby Isaac   http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1464.htm
4608a351411SToby Isaac 
4618a351411SToby Isaac   for details
4628a351411SToby Isaac */
46319e222d7SToby Isaac PETSC_STATIC_INLINE PetscComplex PetscCMPLX(PetscReal x, PetscReal y)
4648a351411SToby Isaac {
465546cf897SSatish Balay #if   defined(__cplusplus) && !defined(PETSC_USE_REAL___FLOAT128)
4668a351411SToby Isaac   return PetscComplex(x,y);
4678a351411SToby Isaac #elif defined(_Imaginary_I)
4688a351411SToby Isaac   return x + y * _Imaginary_I;
4698a351411SToby Isaac #else
470616d7c5eSToby Isaac   { /* In both C99 and C11 (ISO/IEC 9899, Section 6.2.5),
471616d7c5eSToby Isaac 
472616d7c5eSToby Isaac        "For each floating type there is a corresponding real type, which is always a real floating
473616d7c5eSToby Isaac        type. For real floating types, it is the same type. For complex types, it is the type given
474616d7c5eSToby Isaac        by deleting the keyword _Complex from the type name."
475616d7c5eSToby Isaac 
476616d7c5eSToby Isaac        So type punning should be portable. */
477616d7c5eSToby Isaac     union { PetscComplex z; PetscReal f[2]; } uz;
478616d7c5eSToby Isaac 
479616d7c5eSToby Isaac     uz.f[0] = x;
480616d7c5eSToby Isaac     uz.f[1] = y;
481616d7c5eSToby Isaac     return uz.z;
482616d7c5eSToby Isaac   }
48350f81f78SJed Brown #endif
4848a351411SToby Isaac }
4858a351411SToby Isaac #endif
4868a351411SToby Isaac 
487e489efc1SBarry Smith 
488b6a5bde7SBarry Smith /*MC
489b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
490b6a5bde7SBarry Smith 
491eca87e8dSBarry Smith    Synopsis:
492aaa7dc30SBarry Smith    #include <petscmath.h>
493eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
494eca87e8dSBarry Smith 
495eca87e8dSBarry Smith    Not Collective
496eca87e8dSBarry Smith 
497b6a5bde7SBarry Smith    Input Parameter:
498b6a5bde7SBarry Smith +  v1 - first value to find minimum of
499b6a5bde7SBarry Smith -  v2 - second value to find minimum of
500b6a5bde7SBarry Smith 
50195452b02SPatrick Sanan    Notes:
50295452b02SPatrick Sanan     type can be integer or floating point value
503b6a5bde7SBarry Smith 
504b6a5bde7SBarry Smith    Level: beginner
505b6a5bde7SBarry Smith 
5061175f9beSHong Zhang .seealso: PetscMax(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
507b6a5bde7SBarry Smith 
508b6a5bde7SBarry Smith M*/
509e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
510b6a5bde7SBarry Smith 
511b6a5bde7SBarry Smith /*MC
512b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
513b6a5bde7SBarry Smith 
514eca87e8dSBarry Smith    Synopsis:
515aaa7dc30SBarry Smith    #include <petscmath.h>
516eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
517eca87e8dSBarry Smith 
518eca87e8dSBarry Smith    Not Collective
519eca87e8dSBarry Smith 
520b6a5bde7SBarry Smith    Input Parameter:
521b6a5bde7SBarry Smith +  v1 - first value to find maximum of
522b6a5bde7SBarry Smith -  v2 - second value to find maximum of
523b6a5bde7SBarry Smith 
52495452b02SPatrick Sanan    Notes:
52595452b02SPatrick Sanan     type can be integer or floating point value
526b6a5bde7SBarry Smith 
527b6a5bde7SBarry Smith    Level: beginner
528b6a5bde7SBarry Smith 
529d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
530b6a5bde7SBarry Smith 
531b6a5bde7SBarry Smith M*/
532e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
533b6a5bde7SBarry Smith 
534b6a5bde7SBarry Smith /*MC
535d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
536d9a4bb16SJed Brown 
537d9a4bb16SJed Brown    Synopsis:
538aaa7dc30SBarry Smith    #include <petscmath.h>
539d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
540d9a4bb16SJed Brown 
541d9a4bb16SJed Brown    Not Collective
542d9a4bb16SJed Brown 
543d9a4bb16SJed Brown    Input Parameter:
544d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
545d9a4bb16SJed Brown .  a - lower end of interval
546d9a4bb16SJed Brown -  b - upper end of interval
547d9a4bb16SJed Brown 
54895452b02SPatrick Sanan    Notes:
54995452b02SPatrick Sanan     type can be integer or floating point value
550d9a4bb16SJed Brown 
551d9a4bb16SJed Brown    Level: beginner
552d9a4bb16SJed Brown 
553d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
554d9a4bb16SJed Brown 
555d9a4bb16SJed Brown M*/
556d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
557d9a4bb16SJed Brown 
558d9a4bb16SJed Brown /*MC
559b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
560b6a5bde7SBarry Smith 
561b6a5bde7SBarry Smith    Synopsis:
562aaa7dc30SBarry Smith    #include <petscmath.h>
563b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
564b6a5bde7SBarry Smith 
565eca87e8dSBarry Smith    Not Collective
566eca87e8dSBarry Smith 
567eca87e8dSBarry Smith    Input Parameter:
568eca87e8dSBarry Smith .   v1 - the integer
569b6a5bde7SBarry Smith 
570b6a5bde7SBarry Smith    Level: beginner
571b6a5bde7SBarry Smith 
572b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
573b6a5bde7SBarry Smith 
574b6a5bde7SBarry Smith M*/
5759fa7d148SSatish Balay #define PetscAbsInt(a)  (((a)<0)   ? (-(a)) : (a))
576b6a5bde7SBarry Smith 
577b6a5bde7SBarry Smith /*MC
578b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
579b6a5bde7SBarry Smith 
580eca87e8dSBarry Smith    Synopsis:
581aaa7dc30SBarry Smith    #include <petscmath.h>
582eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
583eca87e8dSBarry Smith 
584eca87e8dSBarry Smith    Not Collective
585eca87e8dSBarry Smith 
586b6a5bde7SBarry Smith    Input Parameter:
587b6a5bde7SBarry Smith .   v1 - the double
588b6a5bde7SBarry Smith 
589b6a5bde7SBarry Smith 
590b6a5bde7SBarry Smith    Level: beginner
591b6a5bde7SBarry Smith 
592b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
593b6a5bde7SBarry Smith 
594b6a5bde7SBarry Smith M*/
5951118d4bcSLisandro Dalcin #if defined(PETSC_USE_REAL_SINGLE)
5961118d4bcSLisandro Dalcin #define PetscAbsReal(a) fabsf(a)
5971118d4bcSLisandro Dalcin #elif defined(PETSC_USE_REAL_DOUBLE)
5981118d4bcSLisandro Dalcin #define PetscAbsReal(a) fabs(a)
5991118d4bcSLisandro Dalcin #elif defined(PETSC_USE_REAL___FLOAT128)
6001118d4bcSLisandro Dalcin #define PetscAbsReal(a) fabsq(a)
6011118d4bcSLisandro Dalcin #elif defined(PETSC_USE_REAL___FP16)
6021118d4bcSLisandro Dalcin #define PetscAbsReal(a) fabsf(a)
6031118d4bcSLisandro Dalcin #endif
604b6a5bde7SBarry Smith 
605b6a5bde7SBarry Smith /*MC
606b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
607b6a5bde7SBarry Smith 
608b6a5bde7SBarry Smith    Synopsis:
609aaa7dc30SBarry Smith    #include <petscmath.h>
610b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
611b6a5bde7SBarry Smith 
612eca87e8dSBarry Smith    Not Collective
613eca87e8dSBarry Smith 
614eca87e8dSBarry Smith    Input Parameter:
615eca87e8dSBarry Smith .   v1 - the value
616eca87e8dSBarry Smith 
61795452b02SPatrick Sanan    Notes:
61895452b02SPatrick Sanan     type can be integer or floating point value
619b6a5bde7SBarry Smith 
620b6a5bde7SBarry Smith    Level: beginner
621b6a5bde7SBarry Smith 
622b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
623b6a5bde7SBarry Smith 
624b6a5bde7SBarry Smith M*/
6254ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
626e489efc1SBarry Smith 
627314da920SBarry Smith /* ----------------------------------------------------------------------------*/
628ee223c85SLisandro Dalcin 
629ee223c85SLisandro Dalcin #if defined(PETSC_USE_REAL_SINGLE)
630ee223c85SLisandro Dalcin #define PetscRealConstant(constant) constant##F
631ee223c85SLisandro Dalcin #elif defined(PETSC_USE_REAL___FLOAT128)
632ee223c85SLisandro Dalcin #define PetscRealConstant(constant) constant##Q
633ee223c85SLisandro Dalcin #else
634ee223c85SLisandro Dalcin #define PetscRealConstant(constant) constant
635ee223c85SLisandro Dalcin #endif
636ee223c85SLisandro Dalcin 
637314da920SBarry Smith /*
638d34fcf5fSBarry Smith      Basic constants
639314da920SBarry Smith */
6402fab75feSLisandro Dalcin #define PETSC_PI   PetscRealConstant(3.1415926535897932384626433832795029)
6412fab75feSLisandro Dalcin #define PETSC_PHI  PetscRealConstant(1.6180339887498948482045868343656381)
642d34fcf5fSBarry Smith 
643ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
64471fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
645ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
646ab824b78SBarry Smith #else
647ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
648ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
649ab824b78SBarry Smith #endif
650e489efc1SBarry Smith 
651ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
652ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
6539fa7d148SSatish Balay #  define PETSC_MIN_REAL                (-PETSC_MAX_REAL)
65482a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
65582a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
656ee223c85SLisandro Dalcin #  define PETSC_SMALL                   1.e-5F
657ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
658ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
6599fa7d148SSatish Balay #  define PETSC_MIN_REAL                (-PETSC_MAX_REAL)
66082a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
66182a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
662cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
663ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
664ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
6659fa7d148SSatish Balay #  define PETSC_MIN_REAL                (-FLT128_MAX)
666d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
667ee223c85SLisandro Dalcin #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078144567552953958511352539e-17Q
668ee223c85SLisandro Dalcin #  define PETSC_SMALL                   1.e-20Q
669570b7f6dSBarry Smith #elif defined(PETSC_USE_REAL___FP16)  /* maybe should use single precision values for these? */
670570b7f6dSBarry Smith #  define PETSC_MAX_REAL                65504.
6719fa7d148SSatish Balay #  define PETSC_MIN_REAL                (-PETSC_MAX_REAL)
672570b7f6dSBarry Smith #  define PETSC_MACHINE_EPSILON         .00097656
673570b7f6dSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    .0312
674570b7f6dSBarry Smith #  define PETSC_SMALL                   5.e-3
6759cf09972SJed Brown #endif
6763e523bebSBarry Smith 
67725d0f998SSatish Balay #define PETSC_INFINITY               (PETSC_MAX_REAL/4)
6789fa7d148SSatish Balay #define PETSC_NINFINITY              (-PETSC_INFINITY)
679e270355aSBarry Smith 
6809f4f8022SLisandro Dalcin PETSC_EXTERN PetscBool PetscIsInfReal(PetscReal);
6813948c36eSLisandro Dalcin PETSC_EXTERN PetscBool PetscIsNanReal(PetscReal);
6828b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalReal(PetscReal);
6839f4f8022SLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscIsInfOrNanReal(PetscReal v) {return PetscIsInfReal(v) || PetscIsNanReal(v) ? PETSC_TRUE : PETSC_FALSE;}
6849f4f8022SLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscIsInfScalar(PetscScalar v) {return PetscIsInfReal(PetscAbsScalar(v));}
6853948c36eSLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscIsNanScalar(PetscScalar v) {return PetscIsNanReal(PetscAbsScalar(v));}
6869f4f8022SLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscIsInfOrNanScalar(PetscScalar v) {return PetscIsInfOrNanReal(PetscAbsScalar(v));}
6873948c36eSLisandro Dalcin PETSC_STATIC_INLINE PetscBool PetscIsNormalScalar(PetscScalar v) {return PetscIsNormalReal(PetscAbsScalar(v));}
6889a25a3ccSBarry Smith 
689ce4818fdSLisandro Dalcin PETSC_EXTERN PetscBool PetscEqualReal(PetscReal,PetscReal);
690ce4818fdSLisandro Dalcin PETSC_EXTERN PetscBool PetscEqualScalar(PetscScalar,PetscScalar);
691ce4818fdSLisandro Dalcin 
69298725619SBarry Smith /*
69398725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
69498725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
69598725619SBarry Smith  */
69698725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
69798725619SBarry Smith typedef PetscScalar MatScalar;
69898725619SBarry Smith typedef PetscReal MatReal;
69998725619SBarry Smith 
7008ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
7018ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
702*df4397b0SStefano Zampini 
7038ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
7048ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
7058ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
7068ad47952SJed Brown #else
7078ad47952SJed Brown #define MPIU_2INT MPI_2INT
7088ad47952SJed Brown #endif
709e9fa29b7SSatish Balay 
710b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power)
711b2fb0278SBarry Smith {
712fa711258SJed Brown   PetscInt result = 1;
713fa711258SJed Brown   while (power) {
714fa711258SJed Brown     if (power & 1) result *= base;
715fa711258SJed Brown     power >>= 1;
716fa711258SJed Brown     base *= base;
717fa711258SJed Brown   }
718fa711258SJed Brown   return result;
719fa711258SJed Brown }
720b2fb0278SBarry Smith 
721b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power)
722b2fb0278SBarry Smith {
723fa711258SJed Brown   PetscReal result = 1;
724d98d5da7SBarry Smith   if (power < 0) {
725d98d5da7SBarry Smith     power = -power;
72610d40e53SLisandro Dalcin     base  = ((PetscReal)1)/base;
727d98d5da7SBarry Smith   }
728fa711258SJed Brown   while (power) {
729fa711258SJed Brown     if (power & 1) result *= base;
730fa711258SJed Brown     power >>= 1;
731fa711258SJed Brown     base *= base;
732fa711258SJed Brown   }
733fa711258SJed Brown   return result;
734fa711258SJed Brown }
735fa711258SJed Brown 
736b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarInt(PetscScalar base,PetscInt power)
737b2fb0278SBarry Smith {
7388b49ba18SBarry Smith   PetscScalar result = 1;
7398b49ba18SBarry Smith   if (power < 0) {
7408b49ba18SBarry Smith     power = -power;
74110d40e53SLisandro Dalcin     base  = ((PetscReal)1)/base;
7428b49ba18SBarry Smith   }
7438b49ba18SBarry Smith   while (power) {
7448b49ba18SBarry Smith     if (power & 1) result *= base;
7458b49ba18SBarry Smith     power >>= 1;
7468b49ba18SBarry Smith     base *= base;
7478b49ba18SBarry Smith   }
7488b49ba18SBarry Smith   return result;
7498b49ba18SBarry Smith }
7508b49ba18SBarry Smith 
751b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarReal(PetscScalar base,PetscReal power)
752b2fb0278SBarry Smith {
753b2fb0278SBarry Smith   PetscScalar cpower = power;
754b2fb0278SBarry Smith   return PetscPowScalar(base,cpower);
755b2fb0278SBarry Smith }
75678a59e97SMatthew G. Knepley 
75778a59e97SMatthew G. Knepley #ifndef PETSC_HAVE_LOG2
75878a59e97SMatthew G. Knepley PETSC_STATIC_INLINE PetscReal PetscLog2Real(PetscReal n)
75978a59e97SMatthew G. Knepley {
76091954be4SBarry Smith   return PetscLogReal(n)/PetscLogReal(2.0);
76178a59e97SMatthew G. Knepley }
76278a59e97SMatthew G. Knepley #endif
763e489efc1SBarry Smith #endif
764