xref: /petsc/include/petscmath.h (revision 1d5abddaee111641aa011ffbb235e6894d7c6ebd)
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)
38*1d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2(a,b)
39a4bea5a6SPeter Brune #define PetscSinhReal(a)    sinh(a)
40a4bea5a6SPeter Brune #define PetscCoshReal(a)    cosh(a)
41a4bea5a6SPeter Brune #define PetscTanhReal(a)    tanh(a)
42369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
4377b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
4477b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
45a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
4643f0d3baSJed Brown #define PetscTGamma(a)      tgammaf(a)
47c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
48c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
49c1d390e3SJed Brown typedef double PetscReal;
508f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
519a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
529a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
5377b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
549a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
559a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
56a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
57255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
58255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
59a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
60*1d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2(a,b)
618d1b9be0SJed Brown #define PetscSinhReal(a)    sinh(a)
628d1b9be0SJed Brown #define PetscCoshReal(a)    cosh(a)
638d1b9be0SJed Brown #define PetscTanhReal(a)    tanh(a)
64369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
6577b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
6677b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
67a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
680646a658SBarry Smith #define PetscTGamma(a)      tgamma(a)
69c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
70574fde7bSSatish Balay #if defined(__cplusplus)
71574fde7bSSatish Balay extern "C" {
72574fde7bSSatish Balay #endif
73574fde7bSSatish Balay #include <quadmath.h>
74574fde7bSSatish Balay #if defined(__cplusplus)
75574fde7bSSatish Balay }
76574fde7bSSatish Balay #endif
778ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
78c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
79c1d390e3SJed Brown typedef __float128 PetscReal;
808f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
819a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
829a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
8377b4d14cSPeter Brune #define PetscLog10Real(a)   log10q(a)
849a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
859a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
86a4bea5a6SPeter Brune #define PetscTanReal(a)     tanq(a)
87255453a1SBarry Smith #define PetscAsinReal(a)    asinq(a)
88255453a1SBarry Smith #define PetscAcosReal(a)    acosq(a)
89a4bea5a6SPeter Brune #define PetscAtanReal(a)    atanq(a)
90*1d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2q(a,b)
918d1b9be0SJed Brown #define PetscSinhReal(a)    sinhq(a)
928d1b9be0SJed Brown #define PetscCoshReal(a)    coshq(a)
938d1b9be0SJed Brown #define PetscTanhReal(a)    tanhq(a)
94369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
9577b4d14cSPeter Brune #define PetscCeilReal(a)    ceilq(a)
9677b4d14cSPeter Brune #define PetscFloorReal(a)   floorq(a)
97a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmodq(a,b)
980646a658SBarry Smith #define PetscTGamma(a)      tgammaq(a)
99c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
10059cb5930SBarry Smith 
1011093a601SBarry Smith /*
1021093a601SBarry Smith     Complex number definitions
1031093a601SBarry Smith  */
1042f217381SBarry Smith #if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
105d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX)) || defined(PETSC_DESIRE_COMPLEX)
10650f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
1071093a601SBarry Smith /* C++ support of complex number */
108debe9ee2SPaul Mullowney #if defined(PETSC_HAVE_CUSP)
109debe9ee2SPaul Mullowney #define complexlib cusp
1109ae82921SPaul Mullowney #include <cusp/complex.h>
111debe9ee2SPaul Mullowney #else
112debe9ee2SPaul Mullowney #define complexlib std
113debe9ee2SPaul Mullowney #include <complex>
1149ae82921SPaul Mullowney #endif
115b7940d39SSatish Balay 
11650f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
11750f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
11850f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
11950f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
12050f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
12150f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
12250f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
12350f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
12450f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
12550f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
126255453a1SBarry Smith #define PetscAsinComplex(a)          complexlib::asin(a)
127255453a1SBarry Smith #define PetscAcosComplex(a)          complexlib::acos(a)
128a4bea5a6SPeter Brune #define PetscTanComplex(a)           complexlib::tan(a)
129a4bea5a6SPeter Brune #define PetscSinhComplex(a)          complexlib::sinh(a)
130a4bea5a6SPeter Brune #define PetscCoshComplex(a)          complexlib::cosh(a)
131a4bea5a6SPeter Brune #define PetscTanhComplex(a)          complexlib::tanh(a)
132debe9ee2SPaul Mullowney 
133debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
13450f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
135debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
13650f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
1378c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
13850f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
13922b3908eSBarry Smith PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128;
140debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
14103df5147SBarry Smith #endif  /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
142debe9ee2SPaul Mullowney 
1432f217381SBarry Smith #elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
14450f81f78SJed Brown /* Use C99 _Complex for the type. Do not include complex.h by default to define "complex" because of symbol conflicts in Hypre. */
14550f81f78SJed Brown /* Compilation units that can safely use complex should define PETSC_DESIRE_COMPLEX before including any headers */
146d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX)) || defined(PETSC_DESIRE_COMPLEX)
1479f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
148519e2a1fSPaul Mullowney #include <complex.h>
149519e2a1fSPaul Mullowney 
150ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
15150f81f78SJed Brown typedef float _Complex PetscComplex;
15285b47369SMatthew Knepley 
15350f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
15450f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
15550f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
15650f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
15750f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
15850f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
15950f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
16050f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
16150f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
16250f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
163255453a1SBarry Smith #define PetscAsinComplex(a)          casinf(a)
164255453a1SBarry Smith #define PetscAcosComplex(a)          cacosf(a)
165a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanf(a)
166a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhf(a)
167a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshf(a)
168a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhf(a)
1691093a601SBarry Smith 
170ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
17150f81f78SJed Brown typedef double _Complex PetscComplex;
1721093a601SBarry Smith 
17350f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
17450f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
17550f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
17650f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
17750f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
17850f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
17950f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
18050f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
18150f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
18250f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
183255453a1SBarry Smith #define PetscAsinComplex(a)          casin(a)
184255453a1SBarry Smith #define PetscAcosComplex(a)          cacos(a)
185a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctan(a)
186a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinh(a)
187a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccosh(a)
188a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanh(a)
1891093a601SBarry Smith 
1908c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
19150f81f78SJed Brown typedef __complex128 PetscComplex;
1928ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
1938c764dc5SJose Roman 
19450f81f78SJed Brown #define PetscRealPartComplex(a)      crealq(a)
19550f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagq(a)
19650f81f78SJed Brown #define PetscAbsComplex(a)           cabsq(a)
19750f81f78SJed Brown #define PetscConjComplex(a)          conjq(a)
19850f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtq(a)
19950f81f78SJed Brown #define PetscPowComplex(a,b)         cpowq(a,b)
20050f81f78SJed Brown #define PetscExpComplex(a)           cexpq(a)
20150f81f78SJed Brown #define PetscLogComplex(a)           clogq(a)
20250f81f78SJed Brown #define PetscSinComplex(a)           csinq(a)
20350f81f78SJed Brown #define PetscCosComplex(a)           ccosq(a)
204255453a1SBarry Smith #define PetscAsinComplex(a)          casinq(a)
205255453a1SBarry Smith #define PetscAcosComplex(a)          cacosq(a)
206a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanq(a)
207a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhq(a)
208a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshq(a)
209a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhq(a)
210a4bea5a6SPeter Brune 
211ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
212d4161b4aSBarry Smith #elif (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
21350f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
2149f20b660SSatish Balay #endif /* PETSC_USE_COMPLEX || PETSC_DESIRE_COMPLEX */
2152f217381SBarry Smith #endif /* (__cplusplus && PETSC_HAVE_CXX_COMPLEX) else-if (!__cplusplus && PETSC_HAVE_C99_COMPLEX) */
216e489efc1SBarry Smith 
2178dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
21870da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
219500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
220500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
22170da9c3bSJed Brown #else
2222f217381SBarry Smith # if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX)
2238ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
2248ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
2252f217381SBarry Smith # elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
2268ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
2278ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
2288ad47952SJed Brown # else
2298ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
2308ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
2318ad47952SJed Brown # endif
2328ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
2338ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
2341b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
2358dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
2362c876bd9SBarry Smith 
2377c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
2387c2de775SJed Brown #  if defined(PETSC_USE_REAL_SINGLE)
2397c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
2407c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
2417c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
2427c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
2437c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
2447c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
2457c2de775SJed Brown #endif
2467c2de775SJed Brown 
247d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
24850f81f78SJed Brown typedef PetscComplex PetscScalar;
24950f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
25050f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
25150f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
25250f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
25350f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
25450f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
25550f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
25650f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
25750f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
25850f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
259255453a1SBarry Smith #define PetscAsinScalar(a)    PetscAsinComplex(a)
260255453a1SBarry Smith #define PetscAcosScalar(a)    PetscAcosComplex(a)
261a4bea5a6SPeter Brune #define PetscTanScalar(a)     PetscTanComplex(a)
262a4bea5a6SPeter Brune #define PetscSinhScalar(a)    PetscSinhComplex(a)
263a4bea5a6SPeter Brune #define PetscCoshScalar(a)    PetscCoshComplex(a)
264a4bea5a6SPeter Brune #define PetscTanhScalar(a)    PetscTanhComplex(a)
2657c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
26675567043SBarry Smith 
2671093a601SBarry Smith /*
2681093a601SBarry Smith     real number definitions
2691093a601SBarry Smith  */
2701b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
2717c2de775SJed Brown typedef PetscReal PetscScalar;
2727c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
2737c2de775SJed Brown 
274329f5518SBarry Smith #define PetscRealPart(a)      (a)
275c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
276c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
277e489efc1SBarry Smith #define PetscConj(a)          (a)
278ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
27918a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
280184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
281184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
28206c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
283184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
284184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
285255453a1SBarry Smith #define PetscAsinScalar(a)    asin(a)
286255453a1SBarry Smith #define PetscAcosScalar(a)    acos(a)
287a4bea5a6SPeter Brune #define PetscTanScalar(a)     tan(a)
288a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinh(a)
289a4bea5a6SPeter Brune #define PetscCoshScalar(a)    cosh(a)
290a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanh(a)
291ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
2920d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
2930d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
2940d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
2950d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
2960d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
2970d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
298255453a1SBarry Smith #define PetscAsinScalar(a)    asinq(a)
299255453a1SBarry Smith #define PetscAcosScalar(a)    acosq(a)
300a4bea5a6SPeter Brune #define PetscTanScalar(a)     tanq(a)
301a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinhq(a)
302a4bea5a6SPeter Brune #define PetscCoshScalar(a)    coshq(a)
303a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanhq(a)
304ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
305b0a32e0cSBarry Smith 
3061b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
307e489efc1SBarry Smith 
308da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
30926aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
3103f1db9ecSBarry Smith 
311314da920SBarry Smith /* --------------------------------------------------------------------------*/
312314da920SBarry Smith 
313e489efc1SBarry Smith /*
314f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
315f22f69f0SBarry Smith    This is currently not used.
316e489efc1SBarry Smith */
317557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
318e489efc1SBarry Smith 
31950f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
320e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
32150f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
32250f81f78SJed Brown #endif
323e489efc1SBarry Smith 
324b6a5bde7SBarry Smith /*MC
325b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
326b6a5bde7SBarry Smith 
327eca87e8dSBarry Smith    Synopsis:
328aaa7dc30SBarry Smith    #include <petscmath.h>
329eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
330eca87e8dSBarry Smith 
331eca87e8dSBarry Smith    Not Collective
332eca87e8dSBarry Smith 
333b6a5bde7SBarry Smith    Input Parameter:
334b6a5bde7SBarry Smith +  v1 - first value to find minimum of
335b6a5bde7SBarry Smith -  v2 - second value to find minimum of
336b6a5bde7SBarry Smith 
337b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
338b6a5bde7SBarry Smith 
339b6a5bde7SBarry Smith    Level: beginner
340b6a5bde7SBarry Smith 
341d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
342b6a5bde7SBarry Smith 
343b6a5bde7SBarry Smith M*/
344e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
345b6a5bde7SBarry Smith 
346b6a5bde7SBarry Smith /*MC
347b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
348b6a5bde7SBarry Smith 
349eca87e8dSBarry Smith    Synopsis:
350aaa7dc30SBarry Smith    #include <petscmath.h>
351eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
352eca87e8dSBarry Smith 
353eca87e8dSBarry Smith    Not Collective
354eca87e8dSBarry Smith 
355b6a5bde7SBarry Smith    Input Parameter:
356b6a5bde7SBarry Smith +  v1 - first value to find maximum of
357b6a5bde7SBarry Smith -  v2 - second value to find maximum of
358b6a5bde7SBarry Smith 
359b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
360b6a5bde7SBarry Smith 
361b6a5bde7SBarry Smith    Level: beginner
362b6a5bde7SBarry Smith 
363d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
364b6a5bde7SBarry Smith 
365b6a5bde7SBarry Smith M*/
366e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
367b6a5bde7SBarry Smith 
368b6a5bde7SBarry Smith /*MC
369d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
370d9a4bb16SJed Brown 
371d9a4bb16SJed Brown    Synopsis:
372aaa7dc30SBarry Smith    #include <petscmath.h>
373d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
374d9a4bb16SJed Brown 
375d9a4bb16SJed Brown    Not Collective
376d9a4bb16SJed Brown 
377d9a4bb16SJed Brown    Input Parameter:
378d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
379d9a4bb16SJed Brown .  a - lower end of interval
380d9a4bb16SJed Brown -  b - upper end of interval
381d9a4bb16SJed Brown 
382d9a4bb16SJed Brown    Notes: type can be integer or floating point value
383d9a4bb16SJed Brown 
384d9a4bb16SJed Brown    Level: beginner
385d9a4bb16SJed Brown 
386d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
387d9a4bb16SJed Brown 
388d9a4bb16SJed Brown M*/
389d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
390d9a4bb16SJed Brown 
391d9a4bb16SJed Brown /*MC
392b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
393b6a5bde7SBarry Smith 
394b6a5bde7SBarry Smith    Synopsis:
395aaa7dc30SBarry Smith    #include <petscmath.h>
396b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
397b6a5bde7SBarry Smith 
398eca87e8dSBarry Smith    Not Collective
399eca87e8dSBarry Smith 
400eca87e8dSBarry Smith    Input Parameter:
401eca87e8dSBarry Smith .   v1 - the integer
402b6a5bde7SBarry Smith 
403b6a5bde7SBarry Smith    Level: beginner
404b6a5bde7SBarry Smith 
405b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
406b6a5bde7SBarry Smith 
407b6a5bde7SBarry Smith M*/
408e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
409b6a5bde7SBarry Smith 
410b6a5bde7SBarry Smith /*MC
411b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
412b6a5bde7SBarry Smith 
413eca87e8dSBarry Smith    Synopsis:
414aaa7dc30SBarry Smith    #include <petscmath.h>
415eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
416eca87e8dSBarry Smith 
417eca87e8dSBarry Smith    Not Collective
418eca87e8dSBarry Smith 
419b6a5bde7SBarry Smith    Input Parameter:
420b6a5bde7SBarry Smith .   v1 - the double
421b6a5bde7SBarry Smith 
422b6a5bde7SBarry Smith 
423b6a5bde7SBarry Smith    Level: beginner
424b6a5bde7SBarry Smith 
425b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
426b6a5bde7SBarry Smith 
427b6a5bde7SBarry Smith M*/
428f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
429b6a5bde7SBarry Smith 
430b6a5bde7SBarry Smith /*MC
431b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
432b6a5bde7SBarry Smith 
433b6a5bde7SBarry Smith    Synopsis:
434aaa7dc30SBarry Smith    #include <petscmath.h>
435b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
436b6a5bde7SBarry Smith 
437eca87e8dSBarry Smith    Not Collective
438eca87e8dSBarry Smith 
439eca87e8dSBarry Smith    Input Parameter:
440eca87e8dSBarry Smith .   v1 - the value
441eca87e8dSBarry Smith 
442b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
443b6a5bde7SBarry Smith 
444b6a5bde7SBarry Smith    Level: beginner
445b6a5bde7SBarry Smith 
446b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
447b6a5bde7SBarry Smith 
448b6a5bde7SBarry Smith M*/
4494ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
450e489efc1SBarry Smith 
451314da920SBarry Smith /* ----------------------------------------------------------------------------*/
452314da920SBarry Smith /*
453d34fcf5fSBarry Smith      Basic constants
454314da920SBarry Smith */
455ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
456d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
457d34fcf5fSBarry Smith #elif defined(M_PI)
458d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
459d34fcf5fSBarry Smith #else
460faa6e9b0SMatthew G Knepley #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
461d34fcf5fSBarry Smith #endif
462d34fcf5fSBarry Smith 
463ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
46471fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
465ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
466ab824b78SBarry Smith #else
467ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
468ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
469ab824b78SBarry Smith #endif
470e489efc1SBarry Smith 
471ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
472ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
473ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
47482a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
47582a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
476cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
477ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
478ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
479ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
48082a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
48182a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
482cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
483ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
484ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
485ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
486d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
487d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
488d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
4899cf09972SJed Brown #endif
4903e523bebSBarry Smith 
491e270355aSBarry Smith #define PETSC_INFINITY                PETSC_MAX_REAL/4.0
492e270355aSBarry Smith #define PETSC_NINFINITY              -PETSC_INFINITY
493e270355aSBarry Smith 
494014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanScalar(PetscScalar);
495014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
4968b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalScalar(PetscScalar);
4978b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalReal(PetscReal);
4989a25a3ccSBarry Smith 
499314da920SBarry Smith /* ----------------------------------------------------------------------------*/
50087828ca2SBarry Smith #define PassiveReal   PetscReal
501ea709b57SSatish Balay #define PassiveScalar PetscScalar
502d3ecb3a7SBarry Smith 
50398725619SBarry Smith /*
50498725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
50598725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
50698725619SBarry Smith  */
50798725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
50898725619SBarry Smith typedef PetscScalar MatScalar;
50998725619SBarry Smith typedef PetscReal MatReal;
51098725619SBarry Smith 
5118ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
5128ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
5138ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
5148ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
5158ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
5168ad47952SJed Brown #else
5178ad47952SJed Brown #define MPIU_2INT MPI_2INT
5188ad47952SJed Brown #endif
519e9fa29b7SSatish Balay 
520b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power)
521b2fb0278SBarry Smith {
522fa711258SJed Brown   PetscInt result = 1;
523fa711258SJed Brown   while (power) {
524fa711258SJed Brown     if (power & 1) result *= base;
525fa711258SJed Brown     power >>= 1;
526fa711258SJed Brown     base *= base;
527fa711258SJed Brown   }
528fa711258SJed Brown   return result;
529fa711258SJed Brown }
530b2fb0278SBarry Smith 
531b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power)
532b2fb0278SBarry Smith {
533fa711258SJed Brown   PetscReal result = 1;
534d98d5da7SBarry Smith   if (power < 0) {
535d98d5da7SBarry Smith     power = -power;
536d98d5da7SBarry Smith     if (base != 0.0) base  = 1./base;
537d98d5da7SBarry Smith   }
538fa711258SJed Brown   while (power) {
539fa711258SJed Brown     if (power & 1) result *= base;
540fa711258SJed Brown     power >>= 1;
541fa711258SJed Brown     base *= base;
542fa711258SJed Brown   }
543fa711258SJed Brown   return result;
544fa711258SJed Brown }
545fa711258SJed Brown 
546b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarInt(PetscScalar base,PetscInt power)
547b2fb0278SBarry Smith {
5488b49ba18SBarry Smith   PetscScalar result = 1;
5498b49ba18SBarry Smith   if (power < 0) {
5508b49ba18SBarry Smith     power = -power;
5518b49ba18SBarry Smith     if (base != 0.0) base  = 1./base;
5528b49ba18SBarry Smith   }
5538b49ba18SBarry Smith   while (power) {
5548b49ba18SBarry Smith     if (power & 1) result *= base;
5558b49ba18SBarry Smith     power >>= 1;
5568b49ba18SBarry Smith     base *= base;
5578b49ba18SBarry Smith   }
5588b49ba18SBarry Smith   return result;
5598b49ba18SBarry Smith }
5608b49ba18SBarry Smith 
561b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarReal(PetscScalar base,PetscReal power)
562b2fb0278SBarry Smith {
563b2fb0278SBarry Smith   PetscScalar cpower = power;
564b2fb0278SBarry Smith   return PetscPowScalar(base,cpower);
565b2fb0278SBarry Smith }
566e489efc1SBarry Smith #endif
567