1e489efc1SBarry Smith /* 2314da920SBarry Smith 3314da920SBarry Smith PETSc mathematics include file. Defines certain basic mathematical 4314da920SBarry Smith constants and functions for working with single and double precision 5314da920SBarry Smith floating point numbers as well as complex and integers. 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> 14e9fa29b7SSatish Balay PETSC_EXTERN_CXX_BEGIN 150a5f7794SBarry Smith 167087cfbeSBarry Smith extern MPI_Datatype MPIU_2SCALAR; 177087cfbeSBarry Smith extern MPI_Datatype MPIU_2INT; 18314da920SBarry Smith /* 19f4ccad53SBarry Smith 20f4ccad53SBarry Smith Defines operations that are different for complex and real numbers; 21f4ccad53SBarry Smith note that one cannot really mix the use of complex and real in the same 22f4ccad53SBarry Smith PETSc program. All PETSc objects in one program are built around the object 23*98725619SBarry Smith PetscScalar which is either always a real or a complex. 24f4ccad53SBarry Smith 25e489efc1SBarry Smith */ 26b36a9721SBarry Smith 2759cb5930SBarry Smith #define PetscExpPassiveScalar(a) PetscExpScalar() 2859cb5930SBarry Smith 29aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 30b7940d39SSatish Balay #if defined(PETSC_CLANGUAGE_CXX) 310bfd3fbfSBarry Smith /* 32b7940d39SSatish Balay C++ support of complex numbers: Original support 330bfd3fbfSBarry Smith */ 34df9b3741SSatish Balay #include <complex> 35adc17e78SSatish Balay 36329f5518SBarry Smith #define PetscRealPart(a) (a).real() 37329f5518SBarry Smith #define PetscImaginaryPart(a) (a).imag() 383f6de6efSSatish Balay #define PetscAbsScalar(a) std::abs(a) 393f6de6efSSatish Balay #define PetscConj(a) std::conj(a) 4018a7d68fSSatish Balay #define PetscSqrtScalar(a) std::sqrt(a) 41184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b) 42184914b5SBarry Smith #define PetscExpScalar(a) std::exp(a) 4306c1185fSBarry Smith #define PetscLogScalar(a) std::log(a) 44184914b5SBarry Smith #define PetscSinScalar(a) std::sin(a) 45184914b5SBarry Smith #define PetscCosScalar(a) std::cos(a) 460bfd3fbfSBarry Smith 4765460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 484a60b672SMatthew Knepley typedef std::complex<float> PetscScalar; 4965460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 504a60b672SMatthew Knepley typedef std::complex<long double> PetscScalar; 514a60b672SMatthew Knepley #else 52ea709b57SSatish Balay typedef std::complex<double> PetscScalar; 534a60b672SMatthew Knepley #endif 54b7940d39SSatish Balay #else 55b7940d39SSatish Balay #include <complex.h> 56b7940d39SSatish Balay 57b7940d39SSatish Balay /* 58*98725619SBarry Smith C support of complex numbers: Requires C99 compliant compiler 59b7940d39SSatish Balay */ 60b7940d39SSatish Balay 6165460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 6285b47369SMatthew Knepley typedef float complex PetscScalar; 6385b47369SMatthew Knepley 6485b47369SMatthew Knepley #define PetscRealPart(a) crealf(a) 6585b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagf(a) 6685b47369SMatthew Knepley #define PetscAbsScalar(a) cabsf(a) 6785b47369SMatthew Knepley #define PetscConj(a) conjf(a) 6885b47369SMatthew Knepley #define PetscSqrtScalar(a) csqrtf(a) 6985b47369SMatthew Knepley #define PetscPowScalar(a,b) cpowf(a,b) 7085b47369SMatthew Knepley #define PetscExpScalar(a) cexpf(a) 7106c1185fSBarry Smith #define PetscLogScalar(a) clogf(a) 7285b47369SMatthew Knepley #define PetscSinScalar(a) csinf(a) 7385b47369SMatthew Knepley #define PetscCosScalar(a) ccosf(a) 7465460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 7585b47369SMatthew Knepley typedef long double complex PetscScalar; 7685b47369SMatthew Knepley 7785b47369SMatthew Knepley #define PetscRealPart(a) creall(a) 7885b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagl(a) 7985b47369SMatthew Knepley #define PetscAbsScalar(a) cabsl(a) 8085b47369SMatthew Knepley #define PetscConj(a) conjl(a) 8185b47369SMatthew Knepley #define PetscSqrtScalar(a) csqrtl(a) 8285b47369SMatthew Knepley #define PetscPowScalar(a,b) cpowl(a,b) 8385b47369SMatthew Knepley #define PetscExpScalar(a) cexpl(a) 8406c1185fSBarry Smith #define PetscLogScalar(a) clogl(a) 8585b47369SMatthew Knepley #define PetscSinScalar(a) csinl(a) 8685b47369SMatthew Knepley #define PetscCosScalar(a) ccosl(a) 8785b47369SMatthew Knepley 8885b47369SMatthew Knepley #else 8985b47369SMatthew Knepley typedef double complex PetscScalar; 9085b47369SMatthew Knepley 91b7940d39SSatish Balay #define PetscRealPart(a) creal(a) 92b7940d39SSatish Balay #define PetscImaginaryPart(a) cimag(a) 93b7940d39SSatish Balay #define PetscAbsScalar(a) cabs(a) 94b7940d39SSatish Balay #define PetscConj(a) conj(a) 95b7940d39SSatish Balay #define PetscSqrtScalar(a) csqrt(a) 96b7940d39SSatish Balay #define PetscPowScalar(a,b) cpow(a,b) 97b7940d39SSatish Balay #define PetscExpScalar(a) cexp(a) 9806c1185fSBarry Smith #define PetscLogScalar(a) clog(a) 99b7940d39SSatish Balay #define PetscSinScalar(a) csin(a) 100b7940d39SSatish Balay #define PetscCosScalar(a) ccos(a) 101b7940d39SSatish Balay #endif 1024a60b672SMatthew Knepley #endif 103e489efc1SBarry Smith 1042c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX) 1057087cfbeSBarry Smith extern MPI_Datatype MPI_C_DOUBLE_COMPLEX; 1067087cfbeSBarry Smith extern MPI_Datatype MPI_C_COMPLEX; 1072c876bd9SBarry Smith #endif 1082c876bd9SBarry Smith 109a83b8d76SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 110a83b8d76SBarry Smith #define MPIU_SCALAR MPI_C_COMPLEX 111a83b8d76SBarry Smith #else 1122c876bd9SBarry Smith #define MPIU_SCALAR MPI_C_DOUBLE_COMPLEX 113a83b8d76SBarry Smith #endif 11475567043SBarry Smith 115e489efc1SBarry Smith /* Compiling for real numbers only */ 116e489efc1SBarry Smith #else 11765460251SBarry Smith # if defined(PETSC_USE_SCALAR_SINGLE) 11887828ca2SBarry Smith # define MPIU_SCALAR MPI_FLOAT 11965460251SBarry Smith # elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 120f68b968cSBarry Smith # define MPIU_SCALAR MPI_LONG_DOUBLE 12187828ca2SBarry Smith # else 122e489efc1SBarry Smith # define MPIU_SCALAR MPI_DOUBLE 12387828ca2SBarry Smith # endif 124329f5518SBarry Smith # define PetscRealPart(a) (a) 12575567043SBarry Smith # define PetscImaginaryPart(a) (0.) 126e489efc1SBarry Smith # define PetscAbsScalar(a) (((a)<0.0) ? -(a) : (a)) 127e489efc1SBarry Smith # define PetscConj(a) (a) 12818a7d68fSSatish Balay # define PetscSqrtScalar(a) sqrt(a) 129184914b5SBarry Smith # define PetscPowScalar(a,b) pow(a,b) 130184914b5SBarry Smith # define PetscExpScalar(a) exp(a) 13106c1185fSBarry Smith # define PetscLogScalar(a) log(a) 132184914b5SBarry Smith # define PetscSinScalar(a) sin(a) 133184914b5SBarry Smith # define PetscCosScalar(a) cos(a) 134b0a32e0cSBarry Smith 13565460251SBarry Smith # if defined(PETSC_USE_SCALAR_SINGLE) 136ea709b57SSatish Balay typedef float PetscScalar; 13765460251SBarry Smith # elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 138f68b968cSBarry Smith typedef long double PetscScalar; 139b0a32e0cSBarry Smith # else 140ea709b57SSatish Balay typedef double PetscScalar; 141b0a32e0cSBarry Smith # endif 142e489efc1SBarry Smith #endif 143e489efc1SBarry Smith 14465460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 145d7d1e502SBarry Smith # define MPIU_REAL MPI_FLOAT 14665460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 147f68b968cSBarry Smith # define MPIU_REAL MPI_LONG_DOUBLE 148d7d1e502SBarry Smith #else 149d7d1e502SBarry Smith # define MPIU_REAL MPI_DOUBLE 150d7d1e502SBarry Smith #endif 151d7d1e502SBarry Smith 152da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1) 15326aa1773SMatthew Knepley #define PetscAbs(a) (((a) >= 0) ? (a) : -(a)) 1543f1db9ecSBarry Smith 15565460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 156b400db4cSSatish Balay typedef float PetscReal; 15765460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 158f68b968cSBarry Smith typedef long double PetscReal; 159329f5518SBarry Smith #else 160b400db4cSSatish Balay typedef double PetscReal; 161329f5518SBarry Smith #endif 1623f1db9ecSBarry Smith 163314da920SBarry Smith /* --------------------------------------------------------------------------*/ 164314da920SBarry Smith 165e489efc1SBarry Smith /* 166f22f69f0SBarry Smith Certain objects may be created using either single or double precision. 167f22f69f0SBarry Smith This is currently not used. 168e489efc1SBarry Smith */ 169557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision; 170e489efc1SBarry Smith 171e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */ 1727087cfbeSBarry Smith extern PetscScalar PETSC_i; 173e489efc1SBarry Smith 174b6a5bde7SBarry Smith /*MC 175b6a5bde7SBarry Smith PetscMin - Returns minimum of two numbers 176b6a5bde7SBarry Smith 177eca87e8dSBarry Smith Synopsis: 178eca87e8dSBarry Smith type PetscMin(type v1,type v2) 179eca87e8dSBarry Smith 180eca87e8dSBarry Smith Not Collective 181eca87e8dSBarry Smith 182b6a5bde7SBarry Smith Input Parameter: 183b6a5bde7SBarry Smith + v1 - first value to find minimum of 184b6a5bde7SBarry Smith - v2 - second value to find minimum of 185b6a5bde7SBarry Smith 186b6a5bde7SBarry Smith 187b6a5bde7SBarry Smith Notes: type can be integer or floating point value 188b6a5bde7SBarry Smith 189b6a5bde7SBarry Smith Level: beginner 190b6a5bde7SBarry Smith 191b6a5bde7SBarry Smith 192b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 193b6a5bde7SBarry Smith 194b6a5bde7SBarry Smith M*/ 195e489efc1SBarry Smith #define PetscMin(a,b) (((a)<(b)) ? (a) : (b)) 196b6a5bde7SBarry Smith 197b6a5bde7SBarry Smith /*MC 198b6a5bde7SBarry Smith PetscMax - Returns maxium of two numbers 199b6a5bde7SBarry Smith 200eca87e8dSBarry Smith Synopsis: 201eca87e8dSBarry Smith type max PetscMax(type v1,type v2) 202eca87e8dSBarry Smith 203eca87e8dSBarry Smith Not Collective 204eca87e8dSBarry Smith 205b6a5bde7SBarry Smith Input Parameter: 206b6a5bde7SBarry Smith + v1 - first value to find maximum of 207b6a5bde7SBarry Smith - v2 - second value to find maximum of 208b6a5bde7SBarry Smith 209b6a5bde7SBarry Smith Notes: type can be integer or floating point value 210b6a5bde7SBarry Smith 211b6a5bde7SBarry Smith Level: beginner 212b6a5bde7SBarry Smith 213b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 214b6a5bde7SBarry Smith 215b6a5bde7SBarry Smith M*/ 216e489efc1SBarry Smith #define PetscMax(a,b) (((a)<(b)) ? (b) : (a)) 217b6a5bde7SBarry Smith 218b6a5bde7SBarry Smith /*MC 219b6a5bde7SBarry Smith PetscAbsInt - Returns the absolute value of an integer 220b6a5bde7SBarry Smith 221b6a5bde7SBarry Smith Synopsis: 222b6a5bde7SBarry Smith int abs PetscAbsInt(int v1) 223b6a5bde7SBarry Smith 224eca87e8dSBarry Smith Not Collective 225eca87e8dSBarry Smith 226eca87e8dSBarry Smith Input Parameter: 227eca87e8dSBarry Smith . v1 - the integer 228b6a5bde7SBarry Smith 229b6a5bde7SBarry Smith Level: beginner 230b6a5bde7SBarry Smith 231b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr() 232b6a5bde7SBarry Smith 233b6a5bde7SBarry Smith M*/ 234e489efc1SBarry Smith #define PetscAbsInt(a) (((a)<0) ? -(a) : (a)) 235b6a5bde7SBarry Smith 236b6a5bde7SBarry Smith /*MC 237b6a5bde7SBarry Smith PetscAbsReal - Returns the absolute value of an real number 238b6a5bde7SBarry Smith 239eca87e8dSBarry Smith Synopsis: 240eca87e8dSBarry Smith Real abs PetscAbsReal(PetscReal v1) 241eca87e8dSBarry Smith 242eca87e8dSBarry Smith Not Collective 243eca87e8dSBarry Smith 244b6a5bde7SBarry Smith Input Parameter: 245b6a5bde7SBarry Smith . v1 - the double 246b6a5bde7SBarry Smith 247b6a5bde7SBarry Smith 248b6a5bde7SBarry Smith Level: beginner 249b6a5bde7SBarry Smith 250b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr() 251b6a5bde7SBarry Smith 252b6a5bde7SBarry Smith M*/ 253f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0) ? -(a) : (a)) 254b6a5bde7SBarry Smith 255b6a5bde7SBarry Smith /*MC 256b6a5bde7SBarry Smith PetscSqr - Returns the square of a number 257b6a5bde7SBarry Smith 258b6a5bde7SBarry Smith Synopsis: 259b6a5bde7SBarry Smith type sqr PetscSqr(type v1) 260b6a5bde7SBarry Smith 261eca87e8dSBarry Smith Not Collective 262eca87e8dSBarry Smith 263eca87e8dSBarry Smith Input Parameter: 264eca87e8dSBarry Smith . v1 - the value 265eca87e8dSBarry Smith 266b6a5bde7SBarry Smith Notes: type can be integer or floating point value 267b6a5bde7SBarry Smith 268b6a5bde7SBarry Smith Level: beginner 269b6a5bde7SBarry Smith 270b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal() 271b6a5bde7SBarry Smith 272b6a5bde7SBarry Smith M*/ 2734ebda54eSMatthew Knepley #define PetscSqr(a) ((a)*(a)) 274e489efc1SBarry Smith 275314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 276314da920SBarry Smith /* 27703c60df9SBarry Smith Basic constants - These should be done much better 278314da920SBarry Smith */ 279314da920SBarry Smith #define PETSC_PI 3.14159265358979323846264 280314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994 28171fd2e92SBarry Smith #define PETSC_MAX_INT 2147483647 28271fd2e92SBarry Smith #define PETSC_MIN_INT -2147483647 283e489efc1SBarry Smith 28465460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 2857e032f8bSBarry Smith # define PETSC_MAX 1.e30 2867e032f8bSBarry Smith # define PETSC_MIN -1.e30 287f10639e6SSatish Balay # define PETSC_MACHINE_EPSILON 1.e-7 288f10639e6SSatish Balay # define PETSC_SQRT_MACHINE_EPSILON 3.e-4 289cf6e855fSSatish Balay # define PETSC_SMALL 1.e-5 29082adfdadSBarry Smith #else 2917e032f8bSBarry Smith # define PETSC_MAX 1.e300 2927e032f8bSBarry Smith # define PETSC_MIN -1.e300 293f10639e6SSatish Balay # define PETSC_MACHINE_EPSILON 1.e-14 294f10639e6SSatish Balay # define PETSC_SQRT_MACHINE_EPSILON 1.e-7 295cf6e855fSSatish Balay # define PETSC_SMALL 1.e-10 29682adfdadSBarry Smith #endif 29782adfdadSBarry Smith 2989cf09972SJed Brown #if defined PETSC_HAVE_ADIC 2999cf09972SJed Brown /* Use MPI_Allreduce when ADIC is not available. */ 3007087cfbeSBarry Smith extern PetscErrorCode PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*); 3017087cfbeSBarry Smith extern PetscErrorCode PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*); 3027087cfbeSBarry Smith extern PetscErrorCode PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*); 3039cf09972SJed Brown #endif 3043e523bebSBarry Smith 3050763cb5fSBarry Smith /*MC 3060763cb5fSBarry Smith PetscIsInfOrNan - Returns 1 if the input double has an infinity for Not-a-number (Nan) value, otherwise 0. 3073e523bebSBarry Smith 3080763cb5fSBarry Smith Input Parameter: 3090763cb5fSBarry Smith . a - the double 3100763cb5fSBarry Smith 3110763cb5fSBarry Smith 3120763cb5fSBarry Smith Notes: uses the C99 standard isinf() and isnan() on systems where they exist. 31383886165SBarry Smith Otherwises uses ( (a - a) != 0.0), note that some optimizing compiles compile 3140763cb5fSBarry Smith out this form, thus removing the check. 3150763cb5fSBarry Smith 31683672c4dSSatish Balay Level: beginner 31783672c4dSSatish Balay 31883672c4dSSatish Balay M*/ 3199a25a3ccSBarry Smith #if defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN) 32062cbcd01SMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) { 32162cbcd01SMatthew G Knepley return isinf(PetscAbsScalar(a)) || isnan(PetscAbsScalar(a)); 32262cbcd01SMatthew G Knepley } 32362cbcd01SMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) { 32462cbcd01SMatthew G Knepley return isinf(a) || isnan(a); 32562cbcd01SMatthew G Knepley } 32662b4c0b3SBarry Smith #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN) 327270b8587SSatish Balay #if defined(PETSC_HAVE_FLOAT_H) 328*98725619SBarry Smith #include "float.h" /* Microsoft Windows defines _finite() in float.h */ 329270b8587SSatish Balay #endif 330961faeafSBarry Smith #if defined(PETSC_HAVE_IEEEFP_H) 331961faeafSBarry Smith #include "ieeefp.h" /* Solaris prototypes these here */ 332961faeafSBarry Smith #endif 333*98725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) { 334*98725619SBarry Smith return !_finite(PetscAbsScalar(a)) || _isnan(PetscAbsScalar(a)); 335*98725619SBarry Smith } 336*98725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) { 337*98725619SBarry Smith return !_finite(a) || _isnan(a); 338*98725619SBarry Smith } 3399a25a3ccSBarry Smith #else 340*98725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) { 341*98725619SBarry Smith return ((a - a) != 0.0); 342*98725619SBarry Smith } 343*98725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) { 344*98725619SBarry Smith return ((a - a) != 0.0); 345*98725619SBarry Smith } 3469a25a3ccSBarry Smith #endif 3479a25a3ccSBarry Smith 3489a25a3ccSBarry Smith 349314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 350e489efc1SBarry Smith /* 351b0a32e0cSBarry Smith PetscLogDouble variables are used to contain double precision numbers 352e489efc1SBarry Smith that are not used in the numerical computations, but rather in logging, 353e489efc1SBarry Smith timing etc. 354e489efc1SBarry Smith */ 355b0a32e0cSBarry Smith typedef double PetscLogDouble; 356b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE 357e489efc1SBarry Smith 35887828ca2SBarry Smith #define PassiveReal PetscReal 359ea709b57SSatish Balay #define PassiveScalar PetscScalar 360d3ecb3a7SBarry Smith 361*98725619SBarry Smith /* 362*98725619SBarry Smith These macros are currently hardwired to match the regular data types, so there is no support for a different 363*98725619SBarry Smith MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again. 364*98725619SBarry Smith */ 365*98725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR 366*98725619SBarry Smith typedef PetscScalar MatScalar; 367*98725619SBarry Smith typedef PetscReal MatReal; 368*98725619SBarry Smith 369e9fa29b7SSatish Balay 370e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END 371e489efc1SBarry Smith #endif 372