xref: /petsc/include/petscmath.h (revision 78a59e972cf0b0dbed53b3c3c91aaec52e229b07)
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)
32*78a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
33*78a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2(a)
34*78a59e97SMatthew 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)
41a4bea5a6SPeter Brune #define PetscSinhReal(a)    sinh(a)
42a4bea5a6SPeter Brune #define PetscCoshReal(a)    cosh(a)
43a4bea5a6SPeter Brune #define PetscTanhReal(a)    tanh(a)
44369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
4577b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
4677b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
47a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
4843f0d3baSJed Brown #define PetscTGamma(a)      tgammaf(a)
49c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE)
50c1d390e3SJed Brown #define MPIU_REAL   MPI_DOUBLE
51c1d390e3SJed Brown typedef double PetscReal;
528f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrt(a)
539a07f4dfSJed Brown #define PetscExpReal(a)     exp(a)
549a07f4dfSJed Brown #define PetscLogReal(a)     log(a)
5577b4d14cSPeter Brune #define PetscLog10Real(a)   log10(a)
56*78a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
57*78a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2(a)
58*78a59e97SMatthew G. Knepley #endif
599a07f4dfSJed Brown #define PetscSinReal(a)     sin(a)
609a07f4dfSJed Brown #define PetscCosReal(a)     cos(a)
61a4bea5a6SPeter Brune #define PetscTanReal(a)     tan(a)
62255453a1SBarry Smith #define PetscAsinReal(a)    asin(a)
63255453a1SBarry Smith #define PetscAcosReal(a)    acos(a)
64a4bea5a6SPeter Brune #define PetscAtanReal(a)    atan(a)
658d1b9be0SJed Brown #define PetscSinhReal(a)    sinh(a)
668d1b9be0SJed Brown #define PetscCoshReal(a)    cosh(a)
678d1b9be0SJed Brown #define PetscTanhReal(a)    tanh(a)
68369cc0aeSBarry Smith #define PetscPowReal(a,b)   pow(a,b)
6977b4d14cSPeter Brune #define PetscCeilReal(a)    ceil(a)
7077b4d14cSPeter Brune #define PetscFloorReal(a)   floor(a)
71a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmod(a,b)
720646a658SBarry Smith #define PetscTGamma(a)      tgamma(a)
73c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128)
74574fde7bSSatish Balay #if defined(__cplusplus)
75574fde7bSSatish Balay extern "C" {
76574fde7bSSatish Balay #endif
77574fde7bSSatish Balay #include <quadmath.h>
78574fde7bSSatish Balay #if defined(__cplusplus)
79574fde7bSSatish Balay }
80574fde7bSSatish Balay #endif
818ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128 PetscAttrMPITypeTag(__float128);
82c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128
83c1d390e3SJed Brown typedef __float128 PetscReal;
848f1a2a5eSBarry Smith #define PetscSqrtReal(a)    sqrtq(a)
859a07f4dfSJed Brown #define PetscExpReal(a)     expq(a)
869a07f4dfSJed Brown #define PetscLogReal(a)     logq(a)
8777b4d14cSPeter Brune #define PetscLog10Real(a)   log10q(a)
88*78a59e97SMatthew G. Knepley #ifdef PETSC_HAVE_LOG2
89*78a59e97SMatthew G. Knepley #define PetscLog2Real(a)    log2q(a)
90*78a59e97SMatthew G. Knepley #endif
919a07f4dfSJed Brown #define PetscSinReal(a)     sinq(a)
929a07f4dfSJed Brown #define PetscCosReal(a)     cosq(a)
93a4bea5a6SPeter Brune #define PetscTanReal(a)     tanq(a)
94255453a1SBarry Smith #define PetscAsinReal(a)    asinq(a)
95255453a1SBarry Smith #define PetscAcosReal(a)    acosq(a)
96a4bea5a6SPeter Brune #define PetscAtanReal(a)    atanq(a)
97a4bea5a6SPeter Brune #define PetscAtan2Real(a)   atan2q(a)
988d1b9be0SJed Brown #define PetscSinhReal(a)    sinhq(a)
998d1b9be0SJed Brown #define PetscCoshReal(a)    coshq(a)
1008d1b9be0SJed Brown #define PetscTanhReal(a)    tanhq(a)
101369cc0aeSBarry Smith #define PetscPowReal(a,b)   powq(a,b)
10277b4d14cSPeter Brune #define PetscCeilReal(a)    ceilq(a)
10377b4d14cSPeter Brune #define PetscFloorReal(a)   floorq(a)
104a4bea5a6SPeter Brune #define PetscFmodReal(a,b)  fmodq(a,b)
1050646a658SBarry Smith #define PetscTGamma(a)      tgammaq(a)
106c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */
10759cb5930SBarry Smith 
1081093a601SBarry Smith /*
1091093a601SBarry Smith     Complex number definitions
1101093a601SBarry Smith  */
1112f217381SBarry Smith #if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX) && !defined(PETSC_USE_REAL___FLOAT128)
1128cd53115SBarry Smith #if !defined(PETSC_SKIP_COMPLEX)
11350f81f78SJed Brown #define PETSC_HAVE_COMPLEX 1
1141093a601SBarry Smith /* C++ support of complex number */
115debe9ee2SPaul Mullowney #if defined(PETSC_HAVE_CUSP)
116debe9ee2SPaul Mullowney #define complexlib cusp
1179ae82921SPaul Mullowney #include <cusp/complex.h>
118debe9ee2SPaul Mullowney #else
119debe9ee2SPaul Mullowney #define complexlib std
120debe9ee2SPaul Mullowney #include <complex>
1219ae82921SPaul Mullowney #endif
122b7940d39SSatish Balay 
12350f81f78SJed Brown #define PetscRealPartComplex(a)      (a).real()
12450f81f78SJed Brown #define PetscImaginaryPartComplex(a) (a).imag()
12550f81f78SJed Brown #define PetscAbsComplex(a)           complexlib::abs(a)
12650f81f78SJed Brown #define PetscConjComplex(a)          complexlib::conj(a)
12750f81f78SJed Brown #define PetscSqrtComplex(a)          complexlib::sqrt(a)
12850f81f78SJed Brown #define PetscPowComplex(a,b)         complexlib::pow(a,b)
12950f81f78SJed Brown #define PetscExpComplex(a)           complexlib::exp(a)
13050f81f78SJed Brown #define PetscLogComplex(a)           complexlib::log(a)
13150f81f78SJed Brown #define PetscSinComplex(a)           complexlib::sin(a)
13250f81f78SJed Brown #define PetscCosComplex(a)           complexlib::cos(a)
133255453a1SBarry Smith #define PetscAsinComplex(a)          complexlib::asin(a)
134255453a1SBarry Smith #define PetscAcosComplex(a)          complexlib::acos(a)
135a4bea5a6SPeter Brune #define PetscTanComplex(a)           complexlib::tan(a)
136a4bea5a6SPeter Brune #define PetscSinhComplex(a)          complexlib::sinh(a)
137a4bea5a6SPeter Brune #define PetscCoshComplex(a)          complexlib::cosh(a)
138a4bea5a6SPeter Brune #define PetscTanhComplex(a)          complexlib::tanh(a)
139debe9ee2SPaul Mullowney 
140debe9ee2SPaul Mullowney #if defined(PETSC_USE_REAL_SINGLE)
14150f81f78SJed Brown typedef complexlib::complex<float> PetscComplex;
142debe9ee2SPaul Mullowney #elif defined(PETSC_USE_REAL_DOUBLE)
14350f81f78SJed Brown typedef complexlib::complex<double> PetscComplex;
1448c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
14550f81f78SJed Brown typedef complexlib::complex<__float128> PetscComplex; /* Notstandard and not expected to work, use __complex128 */
14622b3908eSBarry Smith PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128;
147debe9ee2SPaul Mullowney #endif  /* PETSC_USE_REAL_ */
1488cd53115SBarry Smith #endif  /* ! PETSC_SKIP_COMPLEX */
149debe9ee2SPaul Mullowney 
1502f217381SBarry Smith #elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
1518cd53115SBarry Smith #if !defined(PETSC_SKIP_COMPLEX)
1529f20b660SSatish Balay #define PETSC_HAVE_COMPLEX 1
153519e2a1fSPaul Mullowney #include <complex.h>
154519e2a1fSPaul Mullowney 
155ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
15650f81f78SJed Brown typedef float _Complex PetscComplex;
15785b47369SMatthew Knepley 
15850f81f78SJed Brown #define PetscRealPartComplex(a)      crealf(a)
15950f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagf(a)
16050f81f78SJed Brown #define PetscAbsComplex(a)           cabsf(a)
16150f81f78SJed Brown #define PetscConjComplex(a)          conjf(a)
16250f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtf(a)
16350f81f78SJed Brown #define PetscPowComplex(a,b)         cpowf(a,b)
16450f81f78SJed Brown #define PetscExpComplex(a)           cexpf(a)
16550f81f78SJed Brown #define PetscLogComplex(a)           clogf(a)
16650f81f78SJed Brown #define PetscSinComplex(a)           csinf(a)
16750f81f78SJed Brown #define PetscCosComplex(a)           ccosf(a)
168255453a1SBarry Smith #define PetscAsinComplex(a)          casinf(a)
169255453a1SBarry Smith #define PetscAcosComplex(a)          cacosf(a)
170a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanf(a)
171a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhf(a)
172a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshf(a)
173a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhf(a)
1741093a601SBarry Smith 
175ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
17650f81f78SJed Brown typedef double _Complex PetscComplex;
1771093a601SBarry Smith 
17850f81f78SJed Brown #define PetscRealPartComplex(a)      creal(a)
17950f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimag(a)
18050f81f78SJed Brown #define PetscAbsComplex(a)           cabs(a)
18150f81f78SJed Brown #define PetscConjComplex(a)          conj(a)
18250f81f78SJed Brown #define PetscSqrtComplex(a)          csqrt(a)
18350f81f78SJed Brown #define PetscPowComplex(a,b)         cpow(a,b)
18450f81f78SJed Brown #define PetscExpComplex(a)           cexp(a)
18550f81f78SJed Brown #define PetscLogComplex(a)           clog(a)
18650f81f78SJed Brown #define PetscSinComplex(a)           csin(a)
18750f81f78SJed Brown #define PetscCosComplex(a)           ccos(a)
188255453a1SBarry Smith #define PetscAsinComplex(a)          casin(a)
189255453a1SBarry Smith #define PetscAcosComplex(a)          cacos(a)
190a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctan(a)
191a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinh(a)
192a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccosh(a)
193a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanh(a)
1941093a601SBarry Smith 
1958c764dc5SJose Roman #elif defined(PETSC_USE_REAL___FLOAT128)
19650f81f78SJed Brown typedef __complex128 PetscComplex;
1978ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU___COMPLEX128 PetscAttrMPITypeTag(__complex128);
1988c764dc5SJose Roman 
19950f81f78SJed Brown #define PetscRealPartComplex(a)      crealq(a)
20050f81f78SJed Brown #define PetscImaginaryPartComplex(a) cimagq(a)
20150f81f78SJed Brown #define PetscAbsComplex(a)           cabsq(a)
20250f81f78SJed Brown #define PetscConjComplex(a)          conjq(a)
20350f81f78SJed Brown #define PetscSqrtComplex(a)          csqrtq(a)
20450f81f78SJed Brown #define PetscPowComplex(a,b)         cpowq(a,b)
20550f81f78SJed Brown #define PetscExpComplex(a)           cexpq(a)
20650f81f78SJed Brown #define PetscLogComplex(a)           clogq(a)
20750f81f78SJed Brown #define PetscSinComplex(a)           csinq(a)
20850f81f78SJed Brown #define PetscCosComplex(a)           ccosq(a)
209255453a1SBarry Smith #define PetscAsinComplex(a)          casinq(a)
210255453a1SBarry Smith #define PetscAcosComplex(a)          cacosq(a)
211a4bea5a6SPeter Brune #define PetscTanComplex(a)           ctanq(a)
212a4bea5a6SPeter Brune #define PetscSinhComplex(a)          csinhq(a)
213a4bea5a6SPeter Brune #define PetscCoshComplex(a)          ccoshq(a)
214a4bea5a6SPeter Brune #define PetscTanhComplex(a)          ctanhq(a)
215a4bea5a6SPeter Brune 
216ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */
217d4161b4aSBarry Smith #elif (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
21850f81f78SJed Brown #error "PETSc was configured --with-scalar-type=complex, but a language-appropriate complex library is not available"
2198cd53115SBarry Smith #endif /* !PETSC_SKIP_COMPLEX */
2202f217381SBarry Smith #endif /* (__cplusplus && PETSC_HAVE_CXX_COMPLEX) else-if (!__cplusplus && PETSC_HAVE_C99_COMPLEX) */
221e489efc1SBarry Smith 
2228dc6f2c2SJed Brown #if defined(PETSC_HAVE_COMPLEX)
22370da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX)
224500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX
225500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX
22670da9c3bSJed Brown #else
2272f217381SBarry Smith # if defined(__cplusplus) && defined(PETSC_HAVE_CXX_COMPLEX)
2288ad47952SJed Brown   typedef complexlib::complex<double> petsc_mpiu_c_double_complex;
2298ad47952SJed Brown   typedef complexlib::complex<float> petsc_mpiu_c_complex;
2302f217381SBarry Smith # elif !defined(__cplusplus) && defined(PETSC_HAVE_C99_COMPLEX)
2318ad47952SJed Brown   typedef double _Complex petsc_mpiu_c_double_complex;
2328ad47952SJed Brown   typedef float _Complex petsc_mpiu_c_complex;
2338ad47952SJed Brown # else
2348ad47952SJed Brown   typedef struct {double real,imag;} petsc_mpiu_c_double_complex;
2358ad47952SJed Brown   typedef struct {float real,imag;} petsc_mpiu_c_complex;
2368ad47952SJed Brown # endif
2378ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_double_complex);
2388ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX PetscAttrMPITypeTagLayoutCompatible(petsc_mpiu_c_complex);
2391b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */
2408dc6f2c2SJed Brown #endif /* PETSC_HAVE_COMPLEX */
2412c876bd9SBarry Smith 
2427c2de775SJed Brown #if defined(PETSC_HAVE_COMPLEX)
2437c2de775SJed Brown #  if defined(PETSC_USE_REAL_SINGLE)
2447c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_COMPLEX
2457c2de775SJed Brown #  elif defined(PETSC_USE_REAL_DOUBLE)
2467c2de775SJed Brown #    define MPIU_COMPLEX MPIU_C_DOUBLE_COMPLEX
2477c2de775SJed Brown #  elif defined(PETSC_USE_REAL___FLOAT128)
2487c2de775SJed Brown #    define MPIU_COMPLEX MPIU___COMPLEX128
2497c2de775SJed Brown #  endif /* PETSC_USE_REAL_* */
2507c2de775SJed Brown #endif
2517c2de775SJed Brown 
252d4161b4aSBarry Smith #if (defined(PETSC_USE_COMPLEX) && !defined(PETSC_SKIP_COMPLEX))
25350f81f78SJed Brown typedef PetscComplex PetscScalar;
25450f81f78SJed Brown #define PetscRealPart(a)      PetscRealPartComplex(a)
25550f81f78SJed Brown #define PetscImaginaryPart(a) PetscImaginaryPartComplex(a)
25650f81f78SJed Brown #define PetscAbsScalar(a)     PetscAbsComplex(a)
25750f81f78SJed Brown #define PetscConj(a)          PetscConjComplex(a)
25850f81f78SJed Brown #define PetscSqrtScalar(a)    PetscSqrtComplex(a)
25950f81f78SJed Brown #define PetscPowScalar(a,b)   PetscPowComplex(a,b)
26050f81f78SJed Brown #define PetscExpScalar(a)     PetscExpComplex(a)
26150f81f78SJed Brown #define PetscLogScalar(a)     PetscLogComplex(a)
26250f81f78SJed Brown #define PetscSinScalar(a)     PetscSinComplex(a)
26350f81f78SJed Brown #define PetscCosScalar(a)     PetscCosComplex(a)
264255453a1SBarry Smith #define PetscAsinScalar(a)    PetscAsinComplex(a)
265255453a1SBarry Smith #define PetscAcosScalar(a)    PetscAcosComplex(a)
266a4bea5a6SPeter Brune #define PetscTanScalar(a)     PetscTanComplex(a)
267a4bea5a6SPeter Brune #define PetscSinhScalar(a)    PetscSinhComplex(a)
268a4bea5a6SPeter Brune #define PetscCoshScalar(a)    PetscCoshComplex(a)
269a4bea5a6SPeter Brune #define PetscTanhScalar(a)    PetscTanhComplex(a)
2707c2de775SJed Brown #define MPIU_SCALAR MPIU_COMPLEX
27175567043SBarry Smith 
2721093a601SBarry Smith /*
2731093a601SBarry Smith     real number definitions
2741093a601SBarry Smith  */
2751b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */
2767c2de775SJed Brown typedef PetscReal PetscScalar;
2777c2de775SJed Brown #define MPIU_SCALAR MPIU_REAL
2787c2de775SJed Brown 
279329f5518SBarry Smith #define PetscRealPart(a)      (a)
280c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.)
281c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;}
282e489efc1SBarry Smith #define PetscConj(a)          (a)
283ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128)
28418a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
285184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
286184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
28706c1185fSBarry Smith #define PetscLogScalar(a)     log(a)
288184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
289184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
290255453a1SBarry Smith #define PetscAsinScalar(a)    asin(a)
291255453a1SBarry Smith #define PetscAcosScalar(a)    acos(a)
292a4bea5a6SPeter Brune #define PetscTanScalar(a)     tan(a)
293a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinh(a)
294a4bea5a6SPeter Brune #define PetscCoshScalar(a)    cosh(a)
295a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanh(a)
296ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */
2970d0cc1b5SBarry Smith #define PetscSqrtScalar(a)    sqrtq(a)
2980d0cc1b5SBarry Smith #define PetscPowScalar(a,b)   powq(a,b)
2990d0cc1b5SBarry Smith #define PetscExpScalar(a)     expq(a)
3000d0cc1b5SBarry Smith #define PetscLogScalar(a)     logq(a)
3010d0cc1b5SBarry Smith #define PetscSinScalar(a)     sinq(a)
3020d0cc1b5SBarry Smith #define PetscCosScalar(a)     cosq(a)
303255453a1SBarry Smith #define PetscAsinScalar(a)    asinq(a)
304255453a1SBarry Smith #define PetscAcosScalar(a)    acosq(a)
305a4bea5a6SPeter Brune #define PetscTanScalar(a)     tanq(a)
306a4bea5a6SPeter Brune #define PetscSinhScalar(a)    sinhq(a)
307a4bea5a6SPeter Brune #define PetscCoshScalar(a)    coshq(a)
308a4bea5a6SPeter Brune #define PetscTanhScalar(a)    tanhq(a)
309ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */
310b0a32e0cSBarry Smith 
3111b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */
312e489efc1SBarry Smith 
313da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
31426aa1773SMatthew Knepley #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
3153f1db9ecSBarry Smith 
316314da920SBarry Smith /* --------------------------------------------------------------------------*/
317314da920SBarry Smith 
318e489efc1SBarry Smith /*
319f22f69f0SBarry Smith    Certain objects may be created using either single or double precision.
320f22f69f0SBarry Smith    This is currently not used.
321e489efc1SBarry Smith */
322557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
323e489efc1SBarry Smith 
32450f81f78SJed Brown #if defined(PETSC_HAVE_COMPLEX)
325e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
32650f81f78SJed Brown PETSC_EXTERN PetscComplex PETSC_i;
32750f81f78SJed Brown #endif
328e489efc1SBarry Smith 
329b6a5bde7SBarry Smith /*MC
330b6a5bde7SBarry Smith    PetscMin - Returns minimum of two numbers
331b6a5bde7SBarry Smith 
332eca87e8dSBarry Smith    Synopsis:
333aaa7dc30SBarry Smith    #include <petscmath.h>
334eca87e8dSBarry Smith    type PetscMin(type v1,type v2)
335eca87e8dSBarry Smith 
336eca87e8dSBarry Smith    Not Collective
337eca87e8dSBarry Smith 
338b6a5bde7SBarry Smith    Input Parameter:
339b6a5bde7SBarry Smith +  v1 - first value to find minimum of
340b6a5bde7SBarry Smith -  v2 - second value to find minimum of
341b6a5bde7SBarry Smith 
342b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
343b6a5bde7SBarry Smith 
344b6a5bde7SBarry Smith    Level: beginner
345b6a5bde7SBarry Smith 
346d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
347b6a5bde7SBarry Smith 
348b6a5bde7SBarry Smith M*/
349e489efc1SBarry Smith #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
350b6a5bde7SBarry Smith 
351b6a5bde7SBarry Smith /*MC
352b6a5bde7SBarry Smith    PetscMax - Returns maxium of two numbers
353b6a5bde7SBarry Smith 
354eca87e8dSBarry Smith    Synopsis:
355aaa7dc30SBarry Smith    #include <petscmath.h>
356eca87e8dSBarry Smith    type max PetscMax(type v1,type v2)
357eca87e8dSBarry Smith 
358eca87e8dSBarry Smith    Not Collective
359eca87e8dSBarry Smith 
360b6a5bde7SBarry Smith    Input Parameter:
361b6a5bde7SBarry Smith +  v1 - first value to find maximum of
362b6a5bde7SBarry Smith -  v2 - second value to find maximum of
363b6a5bde7SBarry Smith 
364b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
365b6a5bde7SBarry Smith 
366b6a5bde7SBarry Smith    Level: beginner
367b6a5bde7SBarry Smith 
368d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
369b6a5bde7SBarry Smith 
370b6a5bde7SBarry Smith M*/
371e489efc1SBarry Smith #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
372b6a5bde7SBarry Smith 
373b6a5bde7SBarry Smith /*MC
374d9a4bb16SJed Brown    PetscClipInterval - Returns a number clipped to be within an interval
375d9a4bb16SJed Brown 
376d9a4bb16SJed Brown    Synopsis:
377aaa7dc30SBarry Smith    #include <petscmath.h>
378d9a4bb16SJed Brown    type clip PetscClipInterval(type x,type a,type b)
379d9a4bb16SJed Brown 
380d9a4bb16SJed Brown    Not Collective
381d9a4bb16SJed Brown 
382d9a4bb16SJed Brown    Input Parameter:
383d9a4bb16SJed Brown +  x - value to use if within interval (a,b)
384d9a4bb16SJed Brown .  a - lower end of interval
385d9a4bb16SJed Brown -  b - upper end of interval
386d9a4bb16SJed Brown 
387d9a4bb16SJed Brown    Notes: type can be integer or floating point value
388d9a4bb16SJed Brown 
389d9a4bb16SJed Brown    Level: beginner
390d9a4bb16SJed Brown 
391d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
392d9a4bb16SJed Brown 
393d9a4bb16SJed Brown M*/
394d9a4bb16SJed Brown #define PetscClipInterval(x,a,b)   (PetscMax((a),PetscMin((x),(b))))
395d9a4bb16SJed Brown 
396d9a4bb16SJed Brown /*MC
397b6a5bde7SBarry Smith    PetscAbsInt - Returns the absolute value of an integer
398b6a5bde7SBarry Smith 
399b6a5bde7SBarry Smith    Synopsis:
400aaa7dc30SBarry Smith    #include <petscmath.h>
401b6a5bde7SBarry Smith    int abs PetscAbsInt(int v1)
402b6a5bde7SBarry Smith 
403eca87e8dSBarry Smith    Not Collective
404eca87e8dSBarry Smith 
405eca87e8dSBarry Smith    Input Parameter:
406eca87e8dSBarry Smith .   v1 - the integer
407b6a5bde7SBarry Smith 
408b6a5bde7SBarry Smith    Level: beginner
409b6a5bde7SBarry Smith 
410b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
411b6a5bde7SBarry Smith 
412b6a5bde7SBarry Smith M*/
413e489efc1SBarry Smith #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
414b6a5bde7SBarry Smith 
415b6a5bde7SBarry Smith /*MC
416b6a5bde7SBarry Smith    PetscAbsReal - Returns the absolute value of an real number
417b6a5bde7SBarry Smith 
418eca87e8dSBarry Smith    Synopsis:
419aaa7dc30SBarry Smith    #include <petscmath.h>
420eca87e8dSBarry Smith    Real abs PetscAbsReal(PetscReal v1)
421eca87e8dSBarry Smith 
422eca87e8dSBarry Smith    Not Collective
423eca87e8dSBarry Smith 
424b6a5bde7SBarry Smith    Input Parameter:
425b6a5bde7SBarry Smith .   v1 - the double
426b6a5bde7SBarry Smith 
427b6a5bde7SBarry Smith 
428b6a5bde7SBarry Smith    Level: beginner
429b6a5bde7SBarry Smith 
430b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
431b6a5bde7SBarry Smith 
432b6a5bde7SBarry Smith M*/
433f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
434b6a5bde7SBarry Smith 
435b6a5bde7SBarry Smith /*MC
436b6a5bde7SBarry Smith    PetscSqr - Returns the square of a number
437b6a5bde7SBarry Smith 
438b6a5bde7SBarry Smith    Synopsis:
439aaa7dc30SBarry Smith    #include <petscmath.h>
440b6a5bde7SBarry Smith    type sqr PetscSqr(type v1)
441b6a5bde7SBarry Smith 
442eca87e8dSBarry Smith    Not Collective
443eca87e8dSBarry Smith 
444eca87e8dSBarry Smith    Input Parameter:
445eca87e8dSBarry Smith .   v1 - the value
446eca87e8dSBarry Smith 
447b6a5bde7SBarry Smith    Notes: type can be integer or floating point value
448b6a5bde7SBarry Smith 
449b6a5bde7SBarry Smith    Level: beginner
450b6a5bde7SBarry Smith 
451b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
452b6a5bde7SBarry Smith 
453b6a5bde7SBarry Smith M*/
4544ebda54eSMatthew Knepley #define PetscSqr(a)     ((a)*(a))
455e489efc1SBarry Smith 
456314da920SBarry Smith /* ----------------------------------------------------------------------------*/
457314da920SBarry Smith /*
458d34fcf5fSBarry Smith      Basic constants
459314da920SBarry Smith */
460ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128)
461d34fcf5fSBarry Smith #define PETSC_PI                 M_PIq
462d34fcf5fSBarry Smith #elif defined(M_PI)
463d34fcf5fSBarry Smith #define PETSC_PI                 M_PI
464d34fcf5fSBarry Smith #else
465faa6e9b0SMatthew G Knepley #define PETSC_PI                 3.14159265358979323846264338327950288419716939937510582
466d34fcf5fSBarry Smith #endif
467d34fcf5fSBarry Smith 
468ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES)
46971fd2e92SBarry Smith #define PETSC_MAX_INT            2147483647
470ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
471ab824b78SBarry Smith #else
472ab824b78SBarry Smith #define PETSC_MAX_INT            9223372036854775807L
473ab824b78SBarry Smith #define PETSC_MIN_INT            (-PETSC_MAX_INT - 1)
474ab824b78SBarry Smith #endif
475e489efc1SBarry Smith 
476ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE)
477ab824b78SBarry Smith #  define PETSC_MAX_REAL                3.40282346638528860e+38F
478ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
47982a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         1.19209290e-07F
48082a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    3.45266983e-04F
481cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-5
482ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE)
483ab824b78SBarry Smith #  define PETSC_MAX_REAL                1.7976931348623157e+308
484ea345e14SBarry Smith #  define PETSC_MIN_REAL                -PETSC_MAX_REAL
48582a7e548SBarry Smith #  define PETSC_MACHINE_EPSILON         2.2204460492503131e-16
48682a7e548SBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.490116119384766e-08
487cf6e855fSSatish Balay #  define PETSC_SMALL                   1.e-10
488ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128)
489ea345e14SBarry Smith #  define PETSC_MAX_REAL                FLT128_MAX
490ce63c4c1SBarry Smith #  define PETSC_MIN_REAL                -FLT128_MAX
491d34fcf5fSBarry Smith #  define PETSC_MACHINE_EPSILON         FLT128_EPSILON
492d34fcf5fSBarry Smith #  define PETSC_SQRT_MACHINE_EPSILON    1.38777878078e-17
493d34fcf5fSBarry Smith #  define PETSC_SMALL                   1.e-20
4949cf09972SJed Brown #endif
4953e523bebSBarry Smith 
496e270355aSBarry Smith #define PETSC_INFINITY                PETSC_MAX_REAL/4.0
497e270355aSBarry Smith #define PETSC_NINFINITY              -PETSC_INFINITY
498e270355aSBarry Smith 
499014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal);
5008b49ba18SBarry Smith PETSC_EXTERN PetscBool PetscIsNormalReal(PetscReal);
501834b1445SSatish Balay PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar v) {return PetscIsInfOrNanReal(PetscAbsScalar(v));}
502834b1445SSatish Balay PETSC_STATIC_INLINE PetscErrorCode PetscIsNormalScalar(PetscScalar v) {return PetscIsNormalReal(PetscAbsScalar(v));}
5039a25a3ccSBarry Smith 
504314da920SBarry Smith /* ----------------------------------------------------------------------------*/
50587828ca2SBarry Smith #define PassiveReal   PetscReal
506ea709b57SSatish Balay #define PassiveScalar PetscScalar
507d3ecb3a7SBarry Smith 
50898725619SBarry Smith /*
50998725619SBarry Smith     These macros are currently hardwired to match the regular data types, so there is no support for a different
51098725619SBarry Smith     MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again.
51198725619SBarry Smith  */
51298725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR
51398725619SBarry Smith typedef PetscScalar MatScalar;
51498725619SBarry Smith typedef PetscReal MatReal;
51598725619SBarry Smith 
5168ad47952SJed Brown struct petsc_mpiu_2scalar {PetscScalar a,b;};
5178ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2scalar);
5188ad47952SJed Brown #if defined(PETSC_USE_64BIT_INDICES) || !defined(MPI_2INT)
5198ad47952SJed Brown struct petsc_mpiu_2int {PetscInt a,b;};
5208ad47952SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT PetscAttrMPITypeTagLayoutCompatible(struct petsc_mpiu_2int);
5218ad47952SJed Brown #else
5228ad47952SJed Brown #define MPIU_2INT MPI_2INT
5238ad47952SJed Brown #endif
524e9fa29b7SSatish Balay 
525b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscInt PetscPowInt(PetscInt base,PetscInt power)
526b2fb0278SBarry Smith {
527fa711258SJed Brown   PetscInt result = 1;
528fa711258SJed Brown   while (power) {
529fa711258SJed Brown     if (power & 1) result *= base;
530fa711258SJed Brown     power >>= 1;
531fa711258SJed Brown     base *= base;
532fa711258SJed Brown   }
533fa711258SJed Brown   return result;
534fa711258SJed Brown }
535b2fb0278SBarry Smith 
536b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscReal PetscPowRealInt(PetscReal base,PetscInt power)
537b2fb0278SBarry Smith {
538fa711258SJed Brown   PetscReal result = 1;
539d98d5da7SBarry Smith   if (power < 0) {
540d98d5da7SBarry Smith     power = -power;
541d98d5da7SBarry Smith     if (base != 0.0) base  = 1./base;
542d98d5da7SBarry Smith   }
543fa711258SJed Brown   while (power) {
544fa711258SJed Brown     if (power & 1) result *= base;
545fa711258SJed Brown     power >>= 1;
546fa711258SJed Brown     base *= base;
547fa711258SJed Brown   }
548fa711258SJed Brown   return result;
549fa711258SJed Brown }
550fa711258SJed Brown 
551b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarInt(PetscScalar base,PetscInt power)
552b2fb0278SBarry Smith {
5538b49ba18SBarry Smith   PetscScalar result = 1;
5548b49ba18SBarry Smith   if (power < 0) {
5558b49ba18SBarry Smith     power = -power;
5568b49ba18SBarry Smith     if (base != 0.0) base  = 1./base;
5578b49ba18SBarry Smith   }
5588b49ba18SBarry Smith   while (power) {
5598b49ba18SBarry Smith     if (power & 1) result *= base;
5608b49ba18SBarry Smith     power >>= 1;
5618b49ba18SBarry Smith     base *= base;
5628b49ba18SBarry Smith   }
5638b49ba18SBarry Smith   return result;
5648b49ba18SBarry Smith }
5658b49ba18SBarry Smith 
566b2fb0278SBarry Smith PETSC_STATIC_INLINE PetscScalar PetscPowScalarReal(PetscScalar base,PetscReal power)
567b2fb0278SBarry Smith {
568b2fb0278SBarry Smith   PetscScalar cpower = power;
569b2fb0278SBarry Smith   return PetscPowScalar(base,cpower);
570b2fb0278SBarry Smith }
571*78a59e97SMatthew G. Knepley 
572*78a59e97SMatthew G. Knepley #ifndef PETSC_HAVE_LOG2
573*78a59e97SMatthew G. Knepley PETSC_STATIC_INLINE PetscReal PetscLog2Real(PetscReal n)
574*78a59e97SMatthew G. Knepley {
575*78a59e97SMatthew G. Knepley   return PetscLogReal(n)/PetscLogReal(2);
576*78a59e97SMatthew G. Knepley }
577*78a59e97SMatthew G. Knepley #endif
578e489efc1SBarry Smith #endif
579