xref: /petsc/include/petscmath.h (revision 36850ab741a2e501816e93c966d4900bc8acc6d1)
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;
279cf33046SSatish Balay #define PetscSqrtReal(a)    sqrt(a)
289a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
299a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
3077b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
3178a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
3278a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2(a)
3378a59e97SMatthew G. Knepley #endif
349a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
359a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
36a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
37255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
38255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
39a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
401d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2(a,b)
41a4bea5a6SPeter Brune #define PetscSinhReal(a)    sinh(a)
42a4bea5a6SPeter Brune #define PetscCoshReal(a)    cosh(a)
43a4bea5a6SPeter Brune #define PetscTanhReal(a)    tanh(a)
44369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
4577b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
4677b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
47a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
4843f0d3baSJed Brown #define PetscTGamma(a)      tgammaf(a)
49c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
50c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
51c1d390e3SJed Brown typedef double PetscReal;
528f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
539a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
549a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
5577b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
5678a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
5778a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2(a)
5878a59e97SMatthew G. Knepley #endif
599a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
609a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
61a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
62255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
63255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
64a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
651d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2(a,b)
668d1b9be0SJed Brown #define PetscSinhReal(a)    sinh(a)
678d1b9be0SJed Brown #define PetscCoshReal(a)    cosh(a)
688d1b9be0SJed Brown #define PetscTanhReal(a)    tanh(a)
69369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
7077b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
7177b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
72a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
730646a658SBarry Smith #define PetscTGamma(a)      tgamma(a)
74c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
75574fde7bSSatish Balay #if defined(__cplusplus)
76574fde7bSSatish Balay extern "C" {
77574fde7bSSatish Balay #endif
78574fde7bSSatish Balay #include <quadmath.h>
79574fde7bSSatish Balay #if defined(__cplusplus)
80574fde7bSSatish Balay }
81574fde7bSSatish Balay #endif
828ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
83c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
84c1d390e3SJed Brown typedef __float128 PetscReal;
858f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
869a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
879a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
8877b4d14cSPeter Brune #define PetscLog10Real(a)   log10q(a)
8978a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
9078a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2q(a)
9178a59e97SMatthew G. Knepley #endif
929a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
939a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
94a4bea5a6SPeter Brune #define PetscTanReal(a)     tanq(a)
95255453a1SBarry Smith #define PetscAsinReal(a)    asinq(a)
96255453a1SBarry Smith #define PetscAcosReal(a)    acosq(a)
97a4bea5a6SPeter Brune #define PetscAtanReal(a)    atanq(a)
981d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2q(a,b)
998d1b9be0SJed Brown #define PetscSinhReal(a)    sinhq(a)
1008d1b9be0SJed Brown #define PetscCoshReal(a)    coshq(a)
1018d1b9be0SJed Brown #define PetscTanhReal(a)    tanhq(a)
102369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
10377b4d14cSPeter Brune #define PetscCeilReal(a)    ceilq(a)
10477b4d14cSPeter Brune #define PetscFloorReal(a)   floorq(a)
105a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmodq(a,b)
1060646a658SBarry Smith #define PetscTGamma(a)      tgammaq(a)
107c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
10859cb5930SBarry Smith 
1091093a601SBarry Smith /*
1101093a601SBarry Smith     Complex number definitions
1111093a601SBarry Smith  */
1122f217381SBarry Smith #if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
1138cd53115SBarry Smith #if !defined(PETSC_SKIP_COMPLEX)
11450f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
1151093a601SBarry Smith /* C++ support of complex number */
116debe9ee2SPaul Mullowney #if defined(PETSC_HAVE_CUSP)
117debe9ee2SPaul Mullowney #define complexlib cusp
1189ae82921SPaul Mullowney #include <cusp/complex.h>
119fda01a78SKarl Rupp #elif defined(PETSC_HAVE_VECCUDA) && __CUDACC_VER_MAJOR__ > 6
120fda01a78SKarl Rupp /* complex headers in thrust only available in CUDA 7.0 and above */
121ec42abe4SAlejandro Lamas Daviña #define complexlib thrust
122ec42abe4SAlejandro Lamas Daviña #include <thrust/complex.h>
123debe9ee2SPaul Mullowney #else
124debe9ee2SPaul Mullowney #define complexlib std
125debe9ee2SPaul Mullowney #include <complex>
1269ae82921SPaul Mullowney #endif
127b7940d39SSatish Balay 
12850f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
12950f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
13050f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
13150f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
13250f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
13350f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
13450f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
13550f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
13650f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
13750f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
138255453a1SBarry Smith #define PetscAsinComplex(a)          complexlib::asin(a)
139255453a1SBarry Smith #define PetscAcosComplex(a)          complexlib::acos(a)
140027d9794SBarry Smith #if defined(PETSC_HAVE_TANCOMPLEX)
141a4bea5a6SPeter Brune #define PetscTanComplex(a)           complexlib::tan(a)
142027d9794SBarry Smith #else
143027d9794SBarry Smith #define PetscTanComplex(a)           PetscSinComplex(a)/PetscCosComplex(a)
144027d9794SBarry Smith #endif
145a4bea5a6SPeter Brune #define PetscSinhComplex(a)          complexlib::sinh(a)
146a4bea5a6SPeter Brune #define PetscCoshComplex(a)          complexlib::cosh(a)
147027d9794SBarry Smith #if defined(PETSC_HAVE_TANHCOMPLEX)
148a4bea5a6SPeter Brune #define PetscTanhComplex(a)          complexlib::tanh(a)
149027d9794SBarry Smith #else
150027d9794SBarry Smith #define PetscTanhComplex(a)          PetscSinhComplex(a)/PetscCoshComplex(a)
151027d9794SBarry Smith #endif
152debe9ee2SPaul Mullowney 
153debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
15450f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
1553be776deSLisandro Dalcin #if defined(PETSC_USE_CXX_COMPLEX_FLOAT_WORKAROUND)
1563be776deSLisandro Dalcin static inline PetscComplex operator+(const PetscComplex& lhs, const double& rhs) { return lhs + float(rhs); }
1573be776deSLisandro Dalcin static inline PetscComplex operator+(const double& lhs, const PetscComplex& rhs) { return float(lhs) + rhs; }
1583be776deSLisandro Dalcin static inline PetscComplex operator-(const PetscComplex& lhs, const double& rhs) { return lhs - float(rhs); }
1593be776deSLisandro Dalcin static inline PetscComplex operator-(const double& lhs, const PetscComplex& rhs) { return float(lhs) - rhs; }
1603be776deSLisandro Dalcin static inline PetscComplex operator*(const PetscComplex& lhs, const double& rhs) { return lhs * float(rhs); }
1613be776deSLisandro Dalcin static inline PetscComplex operator*(const double& lhs, const PetscComplex& rhs) { return float(lhs) * rhs; }
1623be776deSLisandro Dalcin static inline PetscComplex operator/(const PetscComplex& lhs, const double& rhs) { return lhs / float(rhs); }
1633be776deSLisandro Dalcin static inline PetscComplex operator/(const double& lhs, const PetscComplex& rhs) { return float(lhs) / rhs; }
1643be776deSLisandro Dalcin static inline bool operator==(const PetscComplex& lhs, const double& rhs) { return lhs.imag() == float(0) && lhs.real() == float(rhs); }
1653be776deSLisandro Dalcin static inline bool operator==(const double& lhs, const PetscComplex& rhs) { return rhs.imag() == float(0) && rhs.real() == float(lhs); }
1663be776deSLisandro Dalcin static inline bool operator!=(const PetscComplex& lhs, const double& rhs) { return lhs.imag() != float(0) || lhs.real() != float(rhs); }
1673be776deSLisandro Dalcin static inline bool operator!=(const double& lhs, const PetscComplex& rhs) { return rhs.imag() != float(0) || rhs.real() != float(lhs); }
1683be776deSLisandro Dalcin #endif  /* PETSC_USE_CXX_COMPLEX_FLOAT_WORKAROUND */
169debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
17050f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
1718c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
17250f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
17322b3908eSBarry Smith PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128;
174debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
1758cd53115SBarry Smith #endif  /* ! PETSC_SKIP_COMPLEX */
176debe9ee2SPaul Mullowney 
1772f217381SBarry Smith #elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
1788cd53115SBarry Smith #if !defined(PETSC_SKIP_COMPLEX)
1799f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
180519e2a1fSPaul Mullowney #include <complex.h>
181519e2a1fSPaul Mullowney 
182ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
18350f81f78SJed Brown typedef float _Complex PetscComplex;
18485b47369SMatthew Knepley 
18550f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
18650f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
18750f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
18850f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
18950f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
19050f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
19150f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
19250f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
19350f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
19450f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
195255453a1SBarry Smith #define PetscAsinComplex(a)          casinf(a)
196255453a1SBarry Smith #define PetscAcosComplex(a)          cacosf(a)
197a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanf(a)
198a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhf(a)
199a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshf(a)
200a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhf(a)
2011093a601SBarry Smith 
202ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
20350f81f78SJed Brown typedef double _Complex PetscComplex;
2041093a601SBarry Smith 
20550f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
20650f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
20750f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
20850f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
20950f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
21050f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
21150f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
21250f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
21350f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
21450f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
215255453a1SBarry Smith #define PetscAsinComplex(a)          casin(a)
216255453a1SBarry Smith #define PetscAcosComplex(a)          cacos(a)
217a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctan(a)
218a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinh(a)
219a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccosh(a)
220a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanh(a)
2211093a601SBarry Smith 
2228c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
22350f81f78SJed Brown typedef __complex128 PetscComplex;
2248ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
2258c764dc5SJose Roman 
22650f81f78SJed Brown #define PetscRealPartComplex(a)      crealq(a)
22750f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagq(a)
22850f81f78SJed Brown #define PetscAbsComplex(a)           cabsq(a)
22950f81f78SJed Brown #define PetscConjComplex(a)          conjq(a)
23050f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtq(a)
23150f81f78SJed Brown #define PetscPowComplex(a,b)         cpowq(a,b)
23250f81f78SJed Brown #define PetscExpComplex(a)           cexpq(a)
23350f81f78SJed Brown #define PetscLogComplex(a)           clogq(a)
23450f81f78SJed Brown #define PetscSinComplex(a)           csinq(a)
23550f81f78SJed Brown #define PetscCosComplex(a)           ccosq(a)
236255453a1SBarry Smith #define PetscAsinComplex(a)          casinq(a)
237255453a1SBarry Smith #define PetscAcosComplex(a)          cacosq(a)
238a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanq(a)
239a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhq(a)
240a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshq(a)
241a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhq(a)
242a4bea5a6SPeter Brune 
243ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
244d4161b4aSBarry Smith #elif (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
24550f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
2468cd53115SBarry Smith #endif /* !PETSC_SKIP_COMPLEX */
2472f217381SBarry Smith #endif /* (__cplusplus && PETSC_HAVE_CXX_COMPLEX) else-if (!__cplusplus && PETSC_HAVE_C99_COMPLEX) */
248e489efc1SBarry Smith 
2498dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
25070da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
251500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
252500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
25370da9c3bSJed Brown #else
2542f217381SBarry Smith # if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX)
2558ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
2568ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
2572f217381SBarry Smith # elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
2588ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
2598ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
2608ad47952SJed Brown # else
2618ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
2628ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
2638ad47952SJed Brown # endif
2648ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
2658ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
2661b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
2678dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
2682c876bd9SBarry Smith 
2697c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
2707c2de775SJed Brown #  if defined(PETSC_USE_REAL_SINGLE)
2717c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
2727c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
2737c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
2747c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
2757c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
2767c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
2777c2de775SJed Brown #endif
2787c2de775SJed Brown 
279d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
28050f81f78SJed Brown typedef PetscComplex PetscScalar;
28150f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
28250f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
28350f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
28450f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
28550f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
28650f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
28750f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
28850f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
28950f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
29050f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
291255453a1SBarry Smith #define PetscAsinScalar(a)    PetscAsinComplex(a)
292255453a1SBarry Smith #define PetscAcosScalar(a)    PetscAcosComplex(a)
293a4bea5a6SPeter Brune #define PetscTanScalar(a)     PetscTanComplex(a)
294a4bea5a6SPeter Brune #define PetscSinhScalar(a)    PetscSinhComplex(a)
295a4bea5a6SPeter Brune #define PetscCoshScalar(a)    PetscCoshComplex(a)
296a4bea5a6SPeter Brune #define PetscTanhScalar(a)    PetscTanhComplex(a)
2977c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
29875567043SBarry Smith 
2991093a601SBarry Smith /*
3001093a601SBarry Smith     real number definitions
3011093a601SBarry Smith  */
3021b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
3037c2de775SJed Brown typedef PetscReal PetscScalar;
3047c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
3057c2de775SJed Brown 
306329f5518SBarry Smith #define PetscRealPart(a)      (a)
307c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
308c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
309e489efc1SBarry Smith #define PetscConj(a)          (a)
310ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
31118a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
312184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
313184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
31406c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
315184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
316184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
317255453a1SBarry Smith #define PetscAsinScalar(a)    asin(a)
318255453a1SBarry Smith #define PetscAcosScalar(a)    acos(a)
319a4bea5a6SPeter Brune #define PetscTanScalar(a)     tan(a)
320a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinh(a)
321a4bea5a6SPeter Brune #define PetscCoshScalar(a)    cosh(a)
322a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanh(a)
323ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
3240d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
3250d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
3260d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
3270d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
3280d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
3290d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
330255453a1SBarry Smith #define PetscAsinScalar(a)    asinq(a)
331255453a1SBarry Smith #define PetscAcosScalar(a)    acosq(a)
332a4bea5a6SPeter Brune #define PetscTanScalar(a)     tanq(a)
333a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinhq(a)
334a4bea5a6SPeter Brune #define PetscCoshScalar(a)    coshq(a)
335a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanhq(a)
336ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
337b0a32e0cSBarry Smith 
3381b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
339e489efc1SBarry Smith 
340da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
341*36850ab7SBarry Smith #define PetscSignReal(a) (((a) >= 0.0) ? ((a) == 0.0 ? 0.0 : 1.0) : -1.0)
34226aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
3433f1db9ecSBarry Smith 
344314da920SBarry Smith /* --------------------------------------------------------------------------*/
345314da920SBarry Smith 
346e489efc1SBarry Smith /*
347f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
348f22f69f0SBarry Smith    This is currently not used.
349e489efc1SBarry Smith */
350557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
351e489efc1SBarry Smith 
35250f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
353e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
35450f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
35550f81f78SJed Brown #endif
356e489efc1SBarry Smith 
357b6a5bde7SBarry Smith /*MC
358b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
359b6a5bde7SBarry Smith 
360eca87e8dSBarry Smith    Synopsis:
361aaa7dc30SBarry Smith    #include <petscmath.h>
362eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
363eca87e8dSBarry Smith 
364eca87e8dSBarry Smith    Not Collective
365eca87e8dSBarry Smith 
366b6a5bde7SBarry Smith    Input Parameter:
367b6a5bde7SBarry Smith +  v1 - first value to find minimum of
368b6a5bde7SBarry Smith -  v2 - second value to find minimum of
369b6a5bde7SBarry Smith 
370b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
371b6a5bde7SBarry Smith 
372b6a5bde7SBarry Smith    Level: beginner
373b6a5bde7SBarry Smith 
374d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
375b6a5bde7SBarry Smith 
376b6a5bde7SBarry Smith M*/
377e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
378b6a5bde7SBarry Smith 
379b6a5bde7SBarry Smith /*MC
380b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
381b6a5bde7SBarry Smith 
382eca87e8dSBarry Smith    Synopsis:
383aaa7dc30SBarry Smith    #include <petscmath.h>
384eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
385eca87e8dSBarry Smith 
386eca87e8dSBarry Smith    Not Collective
387eca87e8dSBarry Smith 
388b6a5bde7SBarry Smith    Input Parameter:
389b6a5bde7SBarry Smith +  v1 - first value to find maximum of
390b6a5bde7SBarry Smith -  v2 - second value to find maximum of
391b6a5bde7SBarry Smith 
392b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
393b6a5bde7SBarry Smith 
394b6a5bde7SBarry Smith    Level: beginner
395b6a5bde7SBarry Smith 
396d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
397b6a5bde7SBarry Smith 
398b6a5bde7SBarry Smith M*/
399e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
400b6a5bde7SBarry Smith 
401b6a5bde7SBarry Smith /*MC
402d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
403d9a4bb16SJed Brown 
404d9a4bb16SJed Brown    Synopsis:
405aaa7dc30SBarry Smith    #include <petscmath.h>
406d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
407d9a4bb16SJed Brown 
408d9a4bb16SJed Brown    Not Collective
409d9a4bb16SJed Brown 
410d9a4bb16SJed Brown    Input Parameter:
411d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
412d9a4bb16SJed Brown .  a - lower end of interval
413d9a4bb16SJed Brown -  b - upper end of interval
414d9a4bb16SJed Brown 
415d9a4bb16SJed Brown    Notes: type can be integer or floating point value
416d9a4bb16SJed Brown 
417d9a4bb16SJed Brown    Level: beginner
418d9a4bb16SJed Brown 
419d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
420d9a4bb16SJed Brown 
421d9a4bb16SJed Brown M*/
422d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
423d9a4bb16SJed Brown 
424d9a4bb16SJed Brown /*MC
425b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
426b6a5bde7SBarry Smith 
427b6a5bde7SBarry Smith    Synopsis:
428aaa7dc30SBarry Smith    #include <petscmath.h>
429b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
430b6a5bde7SBarry Smith 
431eca87e8dSBarry Smith    Not Collective
432eca87e8dSBarry Smith 
433eca87e8dSBarry Smith    Input Parameter:
434eca87e8dSBarry Smith .   v1 - the integer
435b6a5bde7SBarry Smith 
436b6a5bde7SBarry Smith    Level: beginner
437b6a5bde7SBarry Smith 
438b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
439b6a5bde7SBarry Smith 
440b6a5bde7SBarry Smith M*/
441e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
442b6a5bde7SBarry Smith 
443b6a5bde7SBarry Smith /*MC
444b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
445b6a5bde7SBarry Smith 
446eca87e8dSBarry Smith    Synopsis:
447aaa7dc30SBarry Smith    #include <petscmath.h>
448eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
449eca87e8dSBarry Smith 
450eca87e8dSBarry Smith    Not Collective
451eca87e8dSBarry Smith 
452b6a5bde7SBarry Smith    Input Parameter:
453b6a5bde7SBarry Smith .   v1 - the double
454b6a5bde7SBarry Smith 
455b6a5bde7SBarry Smith 
456b6a5bde7SBarry Smith    Level: beginner
457b6a5bde7SBarry Smith 
458b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
459b6a5bde7SBarry Smith 
460b6a5bde7SBarry Smith M*/
461f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
462b6a5bde7SBarry Smith 
463b6a5bde7SBarry Smith /*MC
464b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
465b6a5bde7SBarry Smith 
466b6a5bde7SBarry Smith    Synopsis:
467aaa7dc30SBarry Smith    #include <petscmath.h>
468b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
469b6a5bde7SBarry Smith 
470eca87e8dSBarry Smith    Not Collective
471eca87e8dSBarry Smith 
472eca87e8dSBarry Smith    Input Parameter:
473eca87e8dSBarry Smith .   v1 - the value
474eca87e8dSBarry Smith 
475b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
476b6a5bde7SBarry Smith 
477b6a5bde7SBarry Smith    Level: beginner
478b6a5bde7SBarry Smith 
479b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
480b6a5bde7SBarry Smith 
481b6a5bde7SBarry Smith M*/
4824ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
483e489efc1SBarry Smith 
484314da920SBarry Smith /* ----------------------------------------------------------------------------*/
485314da920SBarry Smith /*
486d34fcf5fSBarry Smith      Basic constants
487314da920SBarry Smith */
488ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
489d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
490d34fcf5fSBarry Smith #elif defined(M_PI)
491d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
492d34fcf5fSBarry Smith #else
493faa6e9b0SMatthew G Knepley #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
494d34fcf5fSBarry Smith #endif
495d34fcf5fSBarry Smith 
496ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
49771fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
498ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
499ab824b78SBarry Smith #else
500ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
501ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
502ab824b78SBarry Smith #endif
503e489efc1SBarry Smith 
504ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
505ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
506ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
50782a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
50882a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
509cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
510ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
511ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
512ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
51382a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
51482a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
515cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
516ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
517ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
518ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
519d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
52046d881e9SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17q
52146d881e9SBarry Smith #  define PETSC_SMALL                   1.e-20q
5229cf09972SJed Brown #endif
5233e523bebSBarry Smith 
524e270355aSBarry Smith #define PETSC_INFINITY                PETSC_MAX_REAL/4.0
525e270355aSBarry Smith #define PETSC_NINFINITY              -PETSC_INFINITY
526e270355aSBarry Smith 
527014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
528bae46576SBarry Smith PETSC_EXTERN PetscErrorCode PetscIsNanReal(PetscReal);
5298b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalReal(PetscReal);
530834b1445SSatish Balay PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar v) {return PetscIsInfOrNanReal(PetscAbsScalar(v));}
531bae46576SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsNanScalar(PetscScalar v) {return PetscIsNanReal(PetscAbsScalar(v));}
532834b1445SSatish Balay PETSC_STATIC_INLINE PetscErrorCode PetscIsNormalScalar(PetscScalar v) {return PetscIsNormalReal(PetscAbsScalar(v));}
5339a25a3ccSBarry Smith 
53498725619SBarry Smith /*
53598725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
53698725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
53798725619SBarry Smith  */
53898725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
53998725619SBarry Smith typedef PetscScalar MatScalar;
54098725619SBarry Smith typedef PetscReal MatReal;
54198725619SBarry Smith 
5428ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
5438ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
5448ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
5458ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
5468ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
5478ad47952SJed Brown #else
5488ad47952SJed Brown #define MPIU_2INT MPI_2INT
5498ad47952SJed Brown #endif
550e9fa29b7SSatish Balay 
551b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power)
552b2fb0278SBarry Smith {
553fa711258SJed Brown   PetscInt result = 1;
554fa711258SJed Brown   while (power) {
555fa711258SJed Brown     if (power & 1) result *= base;
556fa711258SJed Brown     power >>= 1;
557fa711258SJed Brown     base *= base;
558fa711258SJed Brown   }
559fa711258SJed Brown   return result;
560fa711258SJed Brown }
561b2fb0278SBarry Smith 
562b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power)
563b2fb0278SBarry Smith {
564fa711258SJed Brown   PetscReal result = 1;
565d98d5da7SBarry Smith   if (power < 0) {
566d98d5da7SBarry Smith     power = -power;
56710d40e53SLisandro Dalcin     base  = ((PetscReal)1)/base;
568d98d5da7SBarry Smith   }
569fa711258SJed Brown   while (power) {
570fa711258SJed Brown     if (power & 1) result *= base;
571fa711258SJed Brown     power >>= 1;
572fa711258SJed Brown     base *= base;
573fa711258SJed Brown   }
574fa711258SJed Brown   return result;
575fa711258SJed Brown }
576fa711258SJed Brown 
577b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarInt(PetscScalar base,PetscInt power)
578b2fb0278SBarry Smith {
5798b49ba18SBarry Smith   PetscScalar result = 1;
5808b49ba18SBarry Smith   if (power < 0) {
5818b49ba18SBarry Smith     power = -power;
58210d40e53SLisandro Dalcin     base  = ((PetscReal)1)/base;
5838b49ba18SBarry Smith   }
5848b49ba18SBarry Smith   while (power) {
5858b49ba18SBarry Smith     if (power & 1) result *= base;
5868b49ba18SBarry Smith     power >>= 1;
5878b49ba18SBarry Smith     base *= base;
5888b49ba18SBarry Smith   }
5898b49ba18SBarry Smith   return result;
5908b49ba18SBarry Smith }
5918b49ba18SBarry Smith 
592b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarReal(PetscScalar base,PetscReal power)
593b2fb0278SBarry Smith {
594b2fb0278SBarry Smith   PetscScalar cpower = power;
595b2fb0278SBarry Smith   return PetscPowScalar(base,cpower);
596b2fb0278SBarry Smith }
59778a59e97SMatthew G. Knepley 
59878a59e97SMatthew G. Knepley #ifndef PETSC_HAVE_LOG2
59978a59e97SMatthew G. Knepley PETSC_STATIC_INLINE PetscReal PetscLog2Real(PetscReal n)
60078a59e97SMatthew G. Knepley {
60191954be4SBarry Smith   return PetscLogReal(n)/PetscLogReal(2.0);
60278a59e97SMatthew G. Knepley }
60378a59e97SMatthew G. Knepley #endif
604e489efc1SBarry Smith #endif
605