xref: /petsc/include/petscmath.h (revision 22b3908e658954e4fcf20b1fde40d4ae28a81228)
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 
2459cb5930SBarry Smith #define PetscExpPassiveScalar(a) PetscExpScalar()
25c1d390e3SJed Brown #if defined(PETSC_USE_REAL_SINGLE)
26c1d390e3SJed Brown #define MPIU_REAL   MPI_FLOAT
27c1d390e3SJed Brown typedef float PetscReal;
289cf33046SSatish Balay #define PetscSqrtReal(a)    sqrt(a)
299a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
309a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
3177b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
329a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
339a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
34a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
35255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
36255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
37a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
38a4bea5a6SPeter Brune #define PetscSinhReal(a)    sinh(a)
39a4bea5a6SPeter Brune #define PetscCoshReal(a)    cosh(a)
40a4bea5a6SPeter Brune #define PetscTanhReal(a)    tanh(a)
41369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
4277b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
4377b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
44a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
4543f0d3baSJed Brown #define PetscTGamma(a)      tgammaf(a)
46c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
47c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
48c1d390e3SJed Brown typedef double PetscReal;
498f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
509a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
519a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
5277b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
539a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
549a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
55a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
56255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
57255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
58a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
598d1b9be0SJed Brown #define PetscSinhReal(a)    sinh(a)
608d1b9be0SJed Brown #define PetscCoshReal(a)    cosh(a)
618d1b9be0SJed Brown #define PetscTanhReal(a)    tanh(a)
62369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
6377b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
6477b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
65a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
660646a658SBarry Smith #define PetscTGamma(a)      tgamma(a)
67c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
68574fde7bSSatish Balay #if defined(__cplusplus)
69574fde7bSSatish Balay extern "C" {
70574fde7bSSatish Balay #endif
71574fde7bSSatish Balay #include <quadmath.h>
72574fde7bSSatish Balay #if defined(__cplusplus)
73574fde7bSSatish Balay }
74574fde7bSSatish Balay #endif
758ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
76c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
77c1d390e3SJed Brown typedef __float128 PetscReal;
788f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
799a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
809a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
8177b4d14cSPeter Brune #define PetscLog10Real(a)   log10q(a)
829a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
839a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
84a4bea5a6SPeter Brune #define PetscTanReal(a)     tanq(a)
85255453a1SBarry Smith #define PetscAsinReal(a)    asinq(a)
86255453a1SBarry Smith #define PetscAcosReal(a)    acosq(a)
87a4bea5a6SPeter Brune #define PetscAtanReal(a)    atanq(a)
88a4bea5a6SPeter Brune #define PetscAtan2Real(a)   atan2q(a)
898d1b9be0SJed Brown #define PetscSinhReal(a)    sinhq(a)
908d1b9be0SJed Brown #define PetscCoshReal(a)    coshq(a)
918d1b9be0SJed Brown #define PetscTanhReal(a)    tanhq(a)
92369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
9377b4d14cSPeter Brune #define PetscCeilReal(a)    ceilq(a)
9477b4d14cSPeter Brune #define PetscFloorReal(a)   floorq(a)
95a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmodq(a,b)
960646a658SBarry Smith #define PetscTGamma(a)      tgammaq(a)
97c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
9859cb5930SBarry Smith 
991093a601SBarry Smith /*
1001093a601SBarry Smith     Complex number definitions
1011093a601SBarry Smith  */
10203df5147SBarry Smith #if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
103d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX)) || defined(PETSC_DESIRE_COMPLEX)
10450f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
1051093a601SBarry Smith /* C++ support of complex number */
106debe9ee2SPaul Mullowney #if defined(PETSC_HAVE_CUSP)
107debe9ee2SPaul Mullowney #define complexlib cusp
1089ae82921SPaul Mullowney #include <cusp/complex.h>
109debe9ee2SPaul Mullowney #else
110debe9ee2SPaul Mullowney #define complexlib std
111debe9ee2SPaul Mullowney #include <complex>
1129ae82921SPaul Mullowney #endif
113b7940d39SSatish Balay 
11450f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
11550f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
11650f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
11750f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
11850f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
11950f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
12050f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
12150f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
12250f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
12350f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
124255453a1SBarry Smith #define PetscAsinComplex(a)          complexlib::asin(a)
125255453a1SBarry Smith #define PetscAcosComplex(a)          complexlib::acos(a)
126a4bea5a6SPeter Brune #define PetscTanComplex(a)           complexlib::tan(a)
127a4bea5a6SPeter Brune #define PetscSinhComplex(a)          complexlib::sinh(a)
128a4bea5a6SPeter Brune #define PetscCoshComplex(a)          complexlib::cosh(a)
129a4bea5a6SPeter Brune #define PetscTanhComplex(a)          complexlib::tanh(a)
130debe9ee2SPaul Mullowney 
131debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
13250f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
133debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
13450f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
1358c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
13650f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
137*22b3908eSBarry Smith PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128;
138debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
13903df5147SBarry Smith #endif  /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
140debe9ee2SPaul Mullowney 
14150f81f78SJed Brown #elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
14250f81f78SJed Brown /* Use C99 _Complex for the type. Do not include complex.h by default to define "complex" because of symbol conflicts in Hypre. */
14350f81f78SJed Brown /* Compilation units that can safely use complex should define PETSC_DESIRE_COMPLEX before including any headers */
144d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX)) || defined(PETSC_DESIRE_COMPLEX)
1459f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
146519e2a1fSPaul Mullowney #include <complex.h>
147519e2a1fSPaul Mullowney 
148ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
14950f81f78SJed Brown typedef float _Complex PetscComplex;
15085b47369SMatthew Knepley 
15150f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
15250f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
15350f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
15450f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
15550f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
15650f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
15750f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
15850f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
15950f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
16050f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
161255453a1SBarry Smith #define PetscAsinComplex(a)          casinf(a)
162255453a1SBarry Smith #define PetscAcosComplex(a)          cacosf(a)
163a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanf(a)
164a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhf(a)
165a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshf(a)
166a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhf(a)
1671093a601SBarry Smith 
168ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
16950f81f78SJed Brown typedef double _Complex PetscComplex;
1701093a601SBarry Smith 
17150f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
17250f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
17350f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
17450f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
17550f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
17650f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
17750f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
17850f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
17950f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
18050f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
181255453a1SBarry Smith #define PetscAsinComplex(a)          casin(a)
182255453a1SBarry Smith #define PetscAcosComplex(a)          cacos(a)
183a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctan(a)
184a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinh(a)
185a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccosh(a)
186a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanh(a)
1871093a601SBarry Smith 
1888c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
18950f81f78SJed Brown typedef __complex128 PetscComplex;
1908ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
1918c764dc5SJose Roman 
19250f81f78SJed Brown #define PetscRealPartComplex(a)      crealq(a)
19350f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagq(a)
19450f81f78SJed Brown #define PetscAbsComplex(a)           cabsq(a)
19550f81f78SJed Brown #define PetscConjComplex(a)          conjq(a)
19650f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtq(a)
19750f81f78SJed Brown #define PetscPowComplex(a,b)         cpowq(a,b)
19850f81f78SJed Brown #define PetscExpComplex(a)           cexpq(a)
19950f81f78SJed Brown #define PetscLogComplex(a)           clogq(a)
20050f81f78SJed Brown #define PetscSinComplex(a)           csinq(a)
20150f81f78SJed Brown #define PetscCosComplex(a)           ccosq(a)
202255453a1SBarry Smith #define PetscAsinComplex(a)          casinq(a)
203255453a1SBarry Smith #define PetscAcosComplex(a)          cacosq(a)
204a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanq(a)
205a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhq(a)
206a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshq(a)
207a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhq(a)
208a4bea5a6SPeter Brune 
209ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
210d4161b4aSBarry Smith #elif (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
21150f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
2129f20b660SSatish Balay #endif /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
2139f20b660SSatish Balay #endif /* (PETSC_CLANGUAGE_CXX && PETSC_HAVE_CXX_COMPLEX) else-if (PETSC_CLANGUAGE_C && PETSC_HAVE_C99_COMPLEX) */
214e489efc1SBarry Smith 
2158dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
21670da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
217500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
218500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
21970da9c3bSJed Brown #else
2208ad47952SJed Brown # if defined(PETSC_CLANGUAGE_CXX) && defined(PETSC_HAVE_CXX_COMPLEX)
2218ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
2228ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
2238ad47952SJed Brown # elif defined(PETSC_CLANGUAGE_C) && defined(PETSC_HAVE_C99_COMPLEX)
2248ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
2258ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
2268ad47952SJed Brown # else
2278ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
2288ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
2298ad47952SJed Brown # endif
2308ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
2318ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
2321b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
2338dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
2342c876bd9SBarry Smith 
2357c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
2367c2de775SJed Brown #  if defined(PETSC_USE_REAL_SINGLE)
2377c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
2387c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
2397c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
2407c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
2417c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
2427c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
2437c2de775SJed Brown #endif
2447c2de775SJed Brown 
245d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
24650f81f78SJed Brown typedef PetscComplex PetscScalar;
24750f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
24850f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
24950f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
25050f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
25150f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
25250f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
25350f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
25450f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
25550f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
25650f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
257255453a1SBarry Smith #define PetscAsinScalar(a)    PetscAsinComplex(a)
258255453a1SBarry Smith #define PetscAcosScalar(a)    PetscAcosComplex(a)
259a4bea5a6SPeter Brune #define PetscTanScalar(a)     PetscTanComplex(a)
260a4bea5a6SPeter Brune #define PetscSinhScalar(a)    PetscSinhComplex(a)
261a4bea5a6SPeter Brune #define PetscCoshScalar(a)    PetscCoshComplex(a)
262a4bea5a6SPeter Brune #define PetscTanhScalar(a)    PetscTanhComplex(a)
2637c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
26475567043SBarry Smith 
2651093a601SBarry Smith /*
2661093a601SBarry Smith     real number definitions
2671093a601SBarry Smith  */
2681b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
2697c2de775SJed Brown typedef PetscReal PetscScalar;
2707c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
2717c2de775SJed Brown 
272329f5518SBarry Smith #define PetscRealPart(a)      (a)
273c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
274c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
275e489efc1SBarry Smith #define PetscConj(a)          (a)
276ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
27718a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
278184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
279184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
28006c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
281184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
282184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
283255453a1SBarry Smith #define PetscAsinScalar(a)    asin(a)
284255453a1SBarry Smith #define PetscAcosScalar(a)    acos(a)
285a4bea5a6SPeter Brune #define PetscTanScalar(a)     tan(a)
286a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinh(a)
287a4bea5a6SPeter Brune #define PetscCoshScalar(a)    cosh(a)
288a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanh(a)
289ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
2900d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
2910d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
2920d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
2930d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
2940d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
2950d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
296255453a1SBarry Smith #define PetscAsinScalar(a)    asinq(a)
297255453a1SBarry Smith #define PetscAcosScalar(a)    acosq(a)
298a4bea5a6SPeter Brune #define PetscTanScalar(a)     tanq(a)
299a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinhq(a)
300a4bea5a6SPeter Brune #define PetscCoshScalar(a)    coshq(a)
301a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanhq(a)
302ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
303b0a32e0cSBarry Smith 
3041b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
305e489efc1SBarry Smith 
306da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
30726aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
3083f1db9ecSBarry Smith 
309314da920SBarry Smith /* --------------------------------------------------------------------------*/
310314da920SBarry Smith 
311e489efc1SBarry Smith /*
312f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
313f22f69f0SBarry Smith    This is currently not used.
314e489efc1SBarry Smith */
315557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
316e489efc1SBarry Smith 
31750f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
318e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
31950f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
32050f81f78SJed Brown #endif
321e489efc1SBarry Smith 
322b6a5bde7SBarry Smith /*MC
323b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
324b6a5bde7SBarry Smith 
325eca87e8dSBarry Smith    Synopsis:
326aaa7dc30SBarry Smith    #include <petscmath.h>
327eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
328eca87e8dSBarry Smith 
329eca87e8dSBarry Smith    Not Collective
330eca87e8dSBarry Smith 
331b6a5bde7SBarry Smith    Input Parameter:
332b6a5bde7SBarry Smith +  v1 - first value to find minimum of
333b6a5bde7SBarry Smith -  v2 - second value to find minimum of
334b6a5bde7SBarry Smith 
335b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
336b6a5bde7SBarry Smith 
337b6a5bde7SBarry Smith    Level: beginner
338b6a5bde7SBarry Smith 
339d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
340b6a5bde7SBarry Smith 
341b6a5bde7SBarry Smith M*/
342e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
343b6a5bde7SBarry Smith 
344b6a5bde7SBarry Smith /*MC
345b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
346b6a5bde7SBarry Smith 
347eca87e8dSBarry Smith    Synopsis:
348aaa7dc30SBarry Smith    #include <petscmath.h>
349eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
350eca87e8dSBarry Smith 
351eca87e8dSBarry Smith    Not Collective
352eca87e8dSBarry Smith 
353b6a5bde7SBarry Smith    Input Parameter:
354b6a5bde7SBarry Smith +  v1 - first value to find maximum of
355b6a5bde7SBarry Smith -  v2 - second value to find maximum of
356b6a5bde7SBarry Smith 
357b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
358b6a5bde7SBarry Smith 
359b6a5bde7SBarry Smith    Level: beginner
360b6a5bde7SBarry Smith 
361d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
362b6a5bde7SBarry Smith 
363b6a5bde7SBarry Smith M*/
364e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
365b6a5bde7SBarry Smith 
366b6a5bde7SBarry Smith /*MC
367d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
368d9a4bb16SJed Brown 
369d9a4bb16SJed Brown    Synopsis:
370aaa7dc30SBarry Smith    #include <petscmath.h>
371d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
372d9a4bb16SJed Brown 
373d9a4bb16SJed Brown    Not Collective
374d9a4bb16SJed Brown 
375d9a4bb16SJed Brown    Input Parameter:
376d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
377d9a4bb16SJed Brown .  a - lower end of interval
378d9a4bb16SJed Brown -  b - upper end of interval
379d9a4bb16SJed Brown 
380d9a4bb16SJed Brown    Notes: type can be integer or floating point value
381d9a4bb16SJed Brown 
382d9a4bb16SJed Brown    Level: beginner
383d9a4bb16SJed Brown 
384d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
385d9a4bb16SJed Brown 
386d9a4bb16SJed Brown M*/
387d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
388d9a4bb16SJed Brown 
389d9a4bb16SJed Brown /*MC
390b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
391b6a5bde7SBarry Smith 
392b6a5bde7SBarry Smith    Synopsis:
393aaa7dc30SBarry Smith    #include <petscmath.h>
394b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
395b6a5bde7SBarry Smith 
396eca87e8dSBarry Smith    Not Collective
397eca87e8dSBarry Smith 
398eca87e8dSBarry Smith    Input Parameter:
399eca87e8dSBarry Smith .   v1 - the integer
400b6a5bde7SBarry Smith 
401b6a5bde7SBarry Smith    Level: beginner
402b6a5bde7SBarry Smith 
403b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
404b6a5bde7SBarry Smith 
405b6a5bde7SBarry Smith M*/
406e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
407b6a5bde7SBarry Smith 
408b6a5bde7SBarry Smith /*MC
409b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
410b6a5bde7SBarry Smith 
411eca87e8dSBarry Smith    Synopsis:
412aaa7dc30SBarry Smith    #include <petscmath.h>
413eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
414eca87e8dSBarry Smith 
415eca87e8dSBarry Smith    Not Collective
416eca87e8dSBarry Smith 
417b6a5bde7SBarry Smith    Input Parameter:
418b6a5bde7SBarry Smith .   v1 - the double
419b6a5bde7SBarry Smith 
420b6a5bde7SBarry Smith 
421b6a5bde7SBarry Smith    Level: beginner
422b6a5bde7SBarry Smith 
423b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
424b6a5bde7SBarry Smith 
425b6a5bde7SBarry Smith M*/
426f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
427b6a5bde7SBarry Smith 
428b6a5bde7SBarry Smith /*MC
429b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
430b6a5bde7SBarry Smith 
431b6a5bde7SBarry Smith    Synopsis:
432aaa7dc30SBarry Smith    #include <petscmath.h>
433b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
434b6a5bde7SBarry Smith 
435eca87e8dSBarry Smith    Not Collective
436eca87e8dSBarry Smith 
437eca87e8dSBarry Smith    Input Parameter:
438eca87e8dSBarry Smith .   v1 - the value
439eca87e8dSBarry Smith 
440b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
441b6a5bde7SBarry Smith 
442b6a5bde7SBarry Smith    Level: beginner
443b6a5bde7SBarry Smith 
444b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
445b6a5bde7SBarry Smith 
446b6a5bde7SBarry Smith M*/
4474ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
448e489efc1SBarry Smith 
449314da920SBarry Smith /* ----------------------------------------------------------------------------*/
450314da920SBarry Smith /*
451d34fcf5fSBarry Smith      Basic constants
452314da920SBarry Smith */
453ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
454d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
455d34fcf5fSBarry Smith #elif defined(M_PI)
456d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
457d34fcf5fSBarry Smith #else
458faa6e9b0SMatthew G Knepley #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
459d34fcf5fSBarry Smith #endif
460d34fcf5fSBarry Smith 
461ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
46271fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
463ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
464ab824b78SBarry Smith #else
465ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
466ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
467ab824b78SBarry Smith #endif
468e489efc1SBarry Smith 
469ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
470ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
471ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
47282a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
47382a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
474cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
475ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
476ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
477ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
47882a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
47982a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
480cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
481ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
482ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
483ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
484d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
485d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
486d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
4879cf09972SJed Brown #endif
4883e523bebSBarry Smith 
489e270355aSBarry Smith #define PETSC_INFINITY                PETSC_MAX_REAL/4.0
490e270355aSBarry Smith #define PETSC_NINFINITY              -PETSC_INFINITY
491e270355aSBarry Smith 
492014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanScalar(PetscScalar);
493014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
4948b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalScalar(PetscScalar);
4958b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalReal(PetscReal);
4969a25a3ccSBarry Smith 
497314da920SBarry Smith /* ----------------------------------------------------------------------------*/
49887828ca2SBarry Smith #define PassiveReal   PetscReal
499ea709b57SSatish Balay #define PassiveScalar PetscScalar
500d3ecb3a7SBarry Smith 
50198725619SBarry Smith /*
50298725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
50398725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
50498725619SBarry Smith  */
50598725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
50698725619SBarry Smith typedef PetscScalar MatScalar;
50798725619SBarry Smith typedef PetscReal MatReal;
50898725619SBarry Smith 
5098ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
5108ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
5118ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
5128ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
5138ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
5148ad47952SJed Brown #else
5158ad47952SJed Brown #define MPIU_2INT MPI_2INT
5168ad47952SJed Brown #endif
517e9fa29b7SSatish Balay 
518b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power)
519b2fb0278SBarry Smith {
520fa711258SJed Brown   PetscInt result = 1;
521fa711258SJed Brown   while (power) {
522fa711258SJed Brown     if (power & 1) result *= base;
523fa711258SJed Brown     power >>= 1;
524fa711258SJed Brown     base *= base;
525fa711258SJed Brown   }
526fa711258SJed Brown   return result;
527fa711258SJed Brown }
528b2fb0278SBarry Smith 
529b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power)
530b2fb0278SBarry Smith {
531fa711258SJed Brown   PetscReal result = 1;
532d98d5da7SBarry Smith   if (power < 0) {
533d98d5da7SBarry Smith     power = -power;
534d98d5da7SBarry Smith     if (base != 0.0) base  = 1./base;
535d98d5da7SBarry Smith   }
536fa711258SJed Brown   while (power) {
537fa711258SJed Brown     if (power & 1) result *= base;
538fa711258SJed Brown     power >>= 1;
539fa711258SJed Brown     base *= base;
540fa711258SJed Brown   }
541fa711258SJed Brown   return result;
542fa711258SJed Brown }
543fa711258SJed Brown 
544b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarInt(PetscScalar base,PetscInt power)
545b2fb0278SBarry Smith {
5468b49ba18SBarry Smith   PetscScalar result = 1;
5478b49ba18SBarry Smith   if (power < 0) {
5488b49ba18SBarry Smith     power = -power;
5498b49ba18SBarry Smith     if (base != 0.0) base  = 1./base;
5508b49ba18SBarry Smith   }
5518b49ba18SBarry Smith   while (power) {
5528b49ba18SBarry Smith     if (power & 1) result *= base;
5538b49ba18SBarry Smith     power >>= 1;
5548b49ba18SBarry Smith     base *= base;
5558b49ba18SBarry Smith   }
5568b49ba18SBarry Smith   return result;
5578b49ba18SBarry Smith }
5588b49ba18SBarry Smith 
559b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarReal(PetscScalar base,PetscReal power)
560b2fb0278SBarry Smith {
561b2fb0278SBarry Smith   PetscScalar cpower = power;
562b2fb0278SBarry Smith   return PetscPowScalar(base,cpower);
563b2fb0278SBarry Smith }
564e489efc1SBarry Smith #endif
565