xref: /petsc/include/petscmath.h (revision 91954be418d857435552465feebb3daff29b99f8)
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)
3278a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
3378a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2(a)
3478a59e97SMatthew G. Knepley #endif
359a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
369a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
37a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
38255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
39255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
40a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
411d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2(a,b)
42a4bea5a6SPeter Brune #define PetscSinhReal(a)    sinh(a)
43a4bea5a6SPeter Brune #define PetscCoshReal(a)    cosh(a)
44a4bea5a6SPeter Brune #define PetscTanhReal(a)    tanh(a)
45369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
4677b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
4777b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
48a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
4943f0d3baSJed Brown #define PetscTGamma(a)      tgammaf(a)
50c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
51c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
52c1d390e3SJed Brown typedef double PetscReal;
538f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
549a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
559a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
5677b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
5778a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
5878a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2(a)
5978a59e97SMatthew G. Knepley #endif
609a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
619a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
62a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
63255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
64255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
65a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
661d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2(a,b)
678d1b9be0SJed Brown #define PetscSinhReal(a)    sinh(a)
688d1b9be0SJed Brown #define PetscCoshReal(a)    cosh(a)
698d1b9be0SJed Brown #define PetscTanhReal(a)    tanh(a)
70369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
7177b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
7277b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
73a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
740646a658SBarry Smith #define PetscTGamma(a)      tgamma(a)
75c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
76574fde7bSSatish Balay #if defined(__cplusplus)
77574fde7bSSatish Balay extern "C" {
78574fde7bSSatish Balay #endif
79574fde7bSSatish Balay #include <quadmath.h>
80574fde7bSSatish Balay #if defined(__cplusplus)
81574fde7bSSatish Balay }
82574fde7bSSatish Balay #endif
838ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
84c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
85c1d390e3SJed Brown typedef __float128 PetscReal;
868f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
879a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
889a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
8977b4d14cSPeter Brune #define PetscLog10Real(a)   log10q(a)
9078a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
9178a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2q(a)
9278a59e97SMatthew G. Knepley #endif
939a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
949a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
95a4bea5a6SPeter Brune #define PetscTanReal(a)     tanq(a)
96255453a1SBarry Smith #define PetscAsinReal(a)    asinq(a)
97255453a1SBarry Smith #define PetscAcosReal(a)    acosq(a)
98a4bea5a6SPeter Brune #define PetscAtanReal(a)    atanq(a)
991d5abddaSDmitry Karpeev #define PetscAtan2Real(a,b) atan2q(a,b)
1008d1b9be0SJed Brown #define PetscSinhReal(a)    sinhq(a)
1018d1b9be0SJed Brown #define PetscCoshReal(a)    coshq(a)
1028d1b9be0SJed Brown #define PetscTanhReal(a)    tanhq(a)
103369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
10477b4d14cSPeter Brune #define PetscCeilReal(a)    ceilq(a)
10577b4d14cSPeter Brune #define PetscFloorReal(a)   floorq(a)
106a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmodq(a,b)
1070646a658SBarry Smith #define PetscTGamma(a)      tgammaq(a)
108c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
10959cb5930SBarry Smith 
1101093a601SBarry Smith /*
1111093a601SBarry Smith     Complex number definitions
1121093a601SBarry Smith  */
1132f217381SBarry Smith #if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
1148cd53115SBarry Smith #if !defined(PETSC_SKIP_COMPLEX)
11550f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
1161093a601SBarry Smith /* C++ support of complex number */
117debe9ee2SPaul Mullowney #if defined(PETSC_HAVE_CUSP)
118debe9ee2SPaul Mullowney #define complexlib cusp
1199ae82921SPaul Mullowney #include <cusp/complex.h>
120debe9ee2SPaul Mullowney #else
121debe9ee2SPaul Mullowney #define complexlib std
122debe9ee2SPaul Mullowney #include <complex>
1239ae82921SPaul Mullowney #endif
124b7940d39SSatish Balay 
12550f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
12650f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
12750f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
12850f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
12950f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
13050f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
13150f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
13250f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
13350f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
13450f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
135255453a1SBarry Smith #define PetscAsinComplex(a)          complexlib::asin(a)
136255453a1SBarry Smith #define PetscAcosComplex(a)          complexlib::acos(a)
137a4bea5a6SPeter Brune #define PetscTanComplex(a)           complexlib::tan(a)
138a4bea5a6SPeter Brune #define PetscSinhComplex(a)          complexlib::sinh(a)
139a4bea5a6SPeter Brune #define PetscCoshComplex(a)          complexlib::cosh(a)
140a4bea5a6SPeter Brune #define PetscTanhComplex(a)          complexlib::tanh(a)
141debe9ee2SPaul Mullowney 
142debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
14350f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
144debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
14550f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
1468c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
14750f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
14822b3908eSBarry Smith PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128;
149debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
1508cd53115SBarry Smith #endif  /* ! PETSC_SKIP_COMPLEX */
151debe9ee2SPaul Mullowney 
1522f217381SBarry Smith #elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
1538cd53115SBarry Smith #if !defined(PETSC_SKIP_COMPLEX)
1549f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
155519e2a1fSPaul Mullowney #include <complex.h>
156519e2a1fSPaul Mullowney 
157ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
15850f81f78SJed Brown typedef float _Complex PetscComplex;
15985b47369SMatthew Knepley 
16050f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
16150f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
16250f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
16350f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
16450f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
16550f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
16650f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
16750f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
16850f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
16950f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
170255453a1SBarry Smith #define PetscAsinComplex(a)          casinf(a)
171255453a1SBarry Smith #define PetscAcosComplex(a)          cacosf(a)
172a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanf(a)
173a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhf(a)
174a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshf(a)
175a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhf(a)
1761093a601SBarry Smith 
177ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
17850f81f78SJed Brown typedef double _Complex PetscComplex;
1791093a601SBarry Smith 
18050f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
18150f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
18250f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
18350f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
18450f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
18550f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
18650f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
18750f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
18850f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
18950f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
190255453a1SBarry Smith #define PetscAsinComplex(a)          casin(a)
191255453a1SBarry Smith #define PetscAcosComplex(a)          cacos(a)
192a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctan(a)
193a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinh(a)
194a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccosh(a)
195a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanh(a)
1961093a601SBarry Smith 
1978c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
19850f81f78SJed Brown typedef __complex128 PetscComplex;
1998ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
2008c764dc5SJose Roman 
20150f81f78SJed Brown #define PetscRealPartComplex(a)      crealq(a)
20250f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagq(a)
20350f81f78SJed Brown #define PetscAbsComplex(a)           cabsq(a)
20450f81f78SJed Brown #define PetscConjComplex(a)          conjq(a)
20550f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtq(a)
20650f81f78SJed Brown #define PetscPowComplex(a,b)         cpowq(a,b)
20750f81f78SJed Brown #define PetscExpComplex(a)           cexpq(a)
20850f81f78SJed Brown #define PetscLogComplex(a)           clogq(a)
20950f81f78SJed Brown #define PetscSinComplex(a)           csinq(a)
21050f81f78SJed Brown #define PetscCosComplex(a)           ccosq(a)
211255453a1SBarry Smith #define PetscAsinComplex(a)          casinq(a)
212255453a1SBarry Smith #define PetscAcosComplex(a)          cacosq(a)
213a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanq(a)
214a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhq(a)
215a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshq(a)
216a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhq(a)
217a4bea5a6SPeter Brune 
218ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
219d4161b4aSBarry Smith #elif (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
22050f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
2218cd53115SBarry Smith #endif /* !PETSC_SKIP_COMPLEX */
2222f217381SBarry Smith #endif /* (__cplusplus && PETSC_HAVE_CXX_COMPLEX) else-if (!__cplusplus && PETSC_HAVE_C99_COMPLEX) */
223e489efc1SBarry Smith 
2248dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
22570da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
226500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
227500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
22870da9c3bSJed Brown #else
2292f217381SBarry Smith # if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX)
2308ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
2318ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
2322f217381SBarry Smith # elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
2338ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
2348ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
2358ad47952SJed Brown # else
2368ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
2378ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
2388ad47952SJed Brown # endif
2398ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
2408ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
2411b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
2428dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
2432c876bd9SBarry Smith 
2447c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
2457c2de775SJed Brown #  if defined(PETSC_USE_REAL_SINGLE)
2467c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
2477c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
2487c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
2497c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
2507c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
2517c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
2527c2de775SJed Brown #endif
2537c2de775SJed Brown 
254d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
25550f81f78SJed Brown typedef PetscComplex PetscScalar;
25650f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
25750f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
25850f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
25950f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
26050f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
26150f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
26250f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
26350f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
26450f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
26550f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
266255453a1SBarry Smith #define PetscAsinScalar(a)    PetscAsinComplex(a)
267255453a1SBarry Smith #define PetscAcosScalar(a)    PetscAcosComplex(a)
268a4bea5a6SPeter Brune #define PetscTanScalar(a)     PetscTanComplex(a)
269a4bea5a6SPeter Brune #define PetscSinhScalar(a)    PetscSinhComplex(a)
270a4bea5a6SPeter Brune #define PetscCoshScalar(a)    PetscCoshComplex(a)
271a4bea5a6SPeter Brune #define PetscTanhScalar(a)    PetscTanhComplex(a)
2727c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
27375567043SBarry Smith 
2741093a601SBarry Smith /*
2751093a601SBarry Smith     real number definitions
2761093a601SBarry Smith  */
2771b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
2787c2de775SJed Brown typedef PetscReal PetscScalar;
2797c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
2807c2de775SJed Brown 
281329f5518SBarry Smith #define PetscRealPart(a)      (a)
282c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
283c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
284e489efc1SBarry Smith #define PetscConj(a)          (a)
285ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
28618a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
287184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
288184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
28906c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
290184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
291184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
292255453a1SBarry Smith #define PetscAsinScalar(a)    asin(a)
293255453a1SBarry Smith #define PetscAcosScalar(a)    acos(a)
294a4bea5a6SPeter Brune #define PetscTanScalar(a)     tan(a)
295a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinh(a)
296a4bea5a6SPeter Brune #define PetscCoshScalar(a)    cosh(a)
297a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanh(a)
298ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
2990d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
3000d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
3010d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
3020d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
3030d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
3040d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
305255453a1SBarry Smith #define PetscAsinScalar(a)    asinq(a)
306255453a1SBarry Smith #define PetscAcosScalar(a)    acosq(a)
307a4bea5a6SPeter Brune #define PetscTanScalar(a)     tanq(a)
308a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinhq(a)
309a4bea5a6SPeter Brune #define PetscCoshScalar(a)    coshq(a)
310a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanhq(a)
311ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
312b0a32e0cSBarry Smith 
3131b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
314e489efc1SBarry Smith 
315da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
31626aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
3173f1db9ecSBarry Smith 
318314da920SBarry Smith /* --------------------------------------------------------------------------*/
319314da920SBarry Smith 
320e489efc1SBarry Smith /*
321f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
322f22f69f0SBarry Smith    This is currently not used.
323e489efc1SBarry Smith */
324557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
325e489efc1SBarry Smith 
32650f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
327e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
32850f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
32950f81f78SJed Brown #endif
330e489efc1SBarry Smith 
331b6a5bde7SBarry Smith /*MC
332b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
333b6a5bde7SBarry Smith 
334eca87e8dSBarry Smith    Synopsis:
335aaa7dc30SBarry Smith    #include <petscmath.h>
336eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
337eca87e8dSBarry Smith 
338eca87e8dSBarry Smith    Not Collective
339eca87e8dSBarry Smith 
340b6a5bde7SBarry Smith    Input Parameter:
341b6a5bde7SBarry Smith +  v1 - first value to find minimum of
342b6a5bde7SBarry Smith -  v2 - second value to find minimum of
343b6a5bde7SBarry Smith 
344b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
345b6a5bde7SBarry Smith 
346b6a5bde7SBarry Smith    Level: beginner
347b6a5bde7SBarry Smith 
348d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
349b6a5bde7SBarry Smith 
350b6a5bde7SBarry Smith M*/
351e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
352b6a5bde7SBarry Smith 
353b6a5bde7SBarry Smith /*MC
354b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
355b6a5bde7SBarry Smith 
356eca87e8dSBarry Smith    Synopsis:
357aaa7dc30SBarry Smith    #include <petscmath.h>
358eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
359eca87e8dSBarry Smith 
360eca87e8dSBarry Smith    Not Collective
361eca87e8dSBarry Smith 
362b6a5bde7SBarry Smith    Input Parameter:
363b6a5bde7SBarry Smith +  v1 - first value to find maximum of
364b6a5bde7SBarry Smith -  v2 - second value to find maximum of
365b6a5bde7SBarry Smith 
366b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
367b6a5bde7SBarry Smith 
368b6a5bde7SBarry Smith    Level: beginner
369b6a5bde7SBarry Smith 
370d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
371b6a5bde7SBarry Smith 
372b6a5bde7SBarry Smith M*/
373e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
374b6a5bde7SBarry Smith 
375b6a5bde7SBarry Smith /*MC
376d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
377d9a4bb16SJed Brown 
378d9a4bb16SJed Brown    Synopsis:
379aaa7dc30SBarry Smith    #include <petscmath.h>
380d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
381d9a4bb16SJed Brown 
382d9a4bb16SJed Brown    Not Collective
383d9a4bb16SJed Brown 
384d9a4bb16SJed Brown    Input Parameter:
385d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
386d9a4bb16SJed Brown .  a - lower end of interval
387d9a4bb16SJed Brown -  b - upper end of interval
388d9a4bb16SJed Brown 
389d9a4bb16SJed Brown    Notes: type can be integer or floating point value
390d9a4bb16SJed Brown 
391d9a4bb16SJed Brown    Level: beginner
392d9a4bb16SJed Brown 
393d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
394d9a4bb16SJed Brown 
395d9a4bb16SJed Brown M*/
396d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
397d9a4bb16SJed Brown 
398d9a4bb16SJed Brown /*MC
399b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
400b6a5bde7SBarry Smith 
401b6a5bde7SBarry Smith    Synopsis:
402aaa7dc30SBarry Smith    #include <petscmath.h>
403b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
404b6a5bde7SBarry Smith 
405eca87e8dSBarry Smith    Not Collective
406eca87e8dSBarry Smith 
407eca87e8dSBarry Smith    Input Parameter:
408eca87e8dSBarry Smith .   v1 - the integer
409b6a5bde7SBarry Smith 
410b6a5bde7SBarry Smith    Level: beginner
411b6a5bde7SBarry Smith 
412b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
413b6a5bde7SBarry Smith 
414b6a5bde7SBarry Smith M*/
415e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
416b6a5bde7SBarry Smith 
417b6a5bde7SBarry Smith /*MC
418b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
419b6a5bde7SBarry Smith 
420eca87e8dSBarry Smith    Synopsis:
421aaa7dc30SBarry Smith    #include <petscmath.h>
422eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
423eca87e8dSBarry Smith 
424eca87e8dSBarry Smith    Not Collective
425eca87e8dSBarry Smith 
426b6a5bde7SBarry Smith    Input Parameter:
427b6a5bde7SBarry Smith .   v1 - the double
428b6a5bde7SBarry Smith 
429b6a5bde7SBarry Smith 
430b6a5bde7SBarry Smith    Level: beginner
431b6a5bde7SBarry Smith 
432b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
433b6a5bde7SBarry Smith 
434b6a5bde7SBarry Smith M*/
435f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
436b6a5bde7SBarry Smith 
437b6a5bde7SBarry Smith /*MC
438b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
439b6a5bde7SBarry Smith 
440b6a5bde7SBarry Smith    Synopsis:
441aaa7dc30SBarry Smith    #include <petscmath.h>
442b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
443b6a5bde7SBarry Smith 
444eca87e8dSBarry Smith    Not Collective
445eca87e8dSBarry Smith 
446eca87e8dSBarry Smith    Input Parameter:
447eca87e8dSBarry Smith .   v1 - the value
448eca87e8dSBarry Smith 
449b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
450b6a5bde7SBarry Smith 
451b6a5bde7SBarry Smith    Level: beginner
452b6a5bde7SBarry Smith 
453b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
454b6a5bde7SBarry Smith 
455b6a5bde7SBarry Smith M*/
4564ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
457e489efc1SBarry Smith 
458314da920SBarry Smith /* ----------------------------------------------------------------------------*/
459314da920SBarry Smith /*
460d34fcf5fSBarry Smith      Basic constants
461314da920SBarry Smith */
462ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
463d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
464d34fcf5fSBarry Smith #elif defined(M_PI)
465d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
466d34fcf5fSBarry Smith #else
467faa6e9b0SMatthew G Knepley #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
468d34fcf5fSBarry Smith #endif
469d34fcf5fSBarry Smith 
470ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
47171fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
472ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
473ab824b78SBarry Smith #else
474ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
475ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
476ab824b78SBarry Smith #endif
477e489efc1SBarry Smith 
478ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
479ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
480ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
48182a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
48282a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
483cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
484ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
485ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
486ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
48782a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
48882a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
489cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
490ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
491ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
492ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
493d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
494d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
495d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
4969cf09972SJed Brown #endif
4973e523bebSBarry Smith 
498e270355aSBarry Smith #define PETSC_INFINITY                PETSC_MAX_REAL/4.0
499e270355aSBarry Smith #define PETSC_NINFINITY              -PETSC_INFINITY
500e270355aSBarry Smith 
501014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
5028b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalReal(PetscReal);
503834b1445SSatish Balay PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar v) {return PetscIsInfOrNanReal(PetscAbsScalar(v));}
504834b1445SSatish Balay PETSC_STATIC_INLINE PetscErrorCode PetscIsNormalScalar(PetscScalar v) {return PetscIsNormalReal(PetscAbsScalar(v));}
5059a25a3ccSBarry Smith 
506314da920SBarry Smith /* ----------------------------------------------------------------------------*/
50787828ca2SBarry Smith #define PassiveReal   PetscReal
508ea709b57SSatish Balay #define PassiveScalar PetscScalar
509d3ecb3a7SBarry Smith 
51098725619SBarry Smith /*
51198725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
51298725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
51398725619SBarry Smith  */
51498725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
51598725619SBarry Smith typedef PetscScalar MatScalar;
51698725619SBarry Smith typedef PetscReal MatReal;
51798725619SBarry Smith 
5188ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
5198ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
5208ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
5218ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
5228ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
5238ad47952SJed Brown #else
5248ad47952SJed Brown #define MPIU_2INT MPI_2INT
5258ad47952SJed Brown #endif
526e9fa29b7SSatish Balay 
527b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power)
528b2fb0278SBarry Smith {
529fa711258SJed Brown   PetscInt result = 1;
530fa711258SJed Brown   while (power) {
531fa711258SJed Brown     if (power & 1) result *= base;
532fa711258SJed Brown     power >>= 1;
533fa711258SJed Brown     base *= base;
534fa711258SJed Brown   }
535fa711258SJed Brown   return result;
536fa711258SJed Brown }
537b2fb0278SBarry Smith 
538b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power)
539b2fb0278SBarry Smith {
540fa711258SJed Brown   PetscReal result = 1;
541d98d5da7SBarry Smith   if (power < 0) {
542d98d5da7SBarry Smith     power = -power;
543d98d5da7SBarry Smith     if (base != 0.0) base  = 1./base;
544d98d5da7SBarry Smith   }
545fa711258SJed Brown   while (power) {
546fa711258SJed Brown     if (power & 1) result *= base;
547fa711258SJed Brown     power >>= 1;
548fa711258SJed Brown     base *= base;
549fa711258SJed Brown   }
550fa711258SJed Brown   return result;
551fa711258SJed Brown }
552fa711258SJed Brown 
553b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarInt(PetscScalar base,PetscInt power)
554b2fb0278SBarry Smith {
5558b49ba18SBarry Smith   PetscScalar result = 1;
5568b49ba18SBarry Smith   if (power < 0) {
5578b49ba18SBarry Smith     power = -power;
5588b49ba18SBarry Smith     if (base != 0.0) base  = 1./base;
5598b49ba18SBarry Smith   }
5608b49ba18SBarry Smith   while (power) {
5618b49ba18SBarry Smith     if (power & 1) result *= base;
5628b49ba18SBarry Smith     power >>= 1;
5638b49ba18SBarry Smith     base *= base;
5648b49ba18SBarry Smith   }
5658b49ba18SBarry Smith   return result;
5668b49ba18SBarry Smith }
5678b49ba18SBarry Smith 
568b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarReal(PetscScalar base,PetscReal power)
569b2fb0278SBarry Smith {
570b2fb0278SBarry Smith   PetscScalar cpower = power;
571b2fb0278SBarry Smith   return PetscPowScalar(base,cpower);
572b2fb0278SBarry Smith }
57378a59e97SMatthew G. Knepley 
57478a59e97SMatthew G. Knepley #ifndef PETSC_HAVE_LOG2
57578a59e97SMatthew G. Knepley PETSC_STATIC_INLINE PetscReal PetscLog2Real(PetscReal n)
57678a59e97SMatthew G. Knepley {
577*91954be4SBarry Smith   return PetscLogReal(n)/PetscLogReal(2.0);
57878a59e97SMatthew G. Knepley }
57978a59e97SMatthew G. Knepley #endif
580e489efc1SBarry Smith #endif
581