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 15*014dd563SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR; 16*014dd563SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2INT; 17c90a1750SBarry Smith 18314da920SBarry Smith /* 19f4ccad53SBarry Smith 20f4ccad53SBarry Smith Defines operations that are different for complex and real numbers; 21a5057860SBarry Smith note that one cannot 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 2398725619SBarry Smith PetscScalar which is either always a real or a complex. 24f4ccad53SBarry Smith 25e489efc1SBarry Smith */ 26b36a9721SBarry Smith 2759cb5930SBarry Smith #define PetscExpPassiveScalar(a) PetscExpScalar() 28c1d390e3SJed Brown #if defined(PETSC_USE_REAL_SINGLE) 29c1d390e3SJed Brown #define MPIU_REAL MPI_FLOAT 30c1d390e3SJed Brown typedef float PetscReal; 318f1a2a5eSBarry Smith #define PetscSqrtReal(a) sqrt(a) 32c1d390e3SJed Brown #elif defined(PETSC_USE_REAL_DOUBLE) 33c1d390e3SJed Brown #define MPIU_REAL MPI_DOUBLE 34c1d390e3SJed Brown typedef double PetscReal; 358f1a2a5eSBarry Smith #define PetscSqrtReal(a) sqrt(a) 36c1d390e3SJed Brown #elif defined(PETSC_USE_REAL___FLOAT128) 37c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128 38c1d390e3SJed Brown typedef __float128 PetscReal; 398f1a2a5eSBarry Smith #define PetscSqrtReal(a) sqrtq(a) 40c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */ 4159cb5930SBarry Smith 421093a601SBarry Smith /* 431093a601SBarry Smith Complex number definitions 441093a601SBarry Smith */ 45aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 46b7940d39SSatish Balay #if defined(PETSC_CLANGUAGE_CXX) 471093a601SBarry Smith /* C++ support of complex number */ 48df9b3741SSatish Balay #include <complex> 49adc17e78SSatish Balay 50329f5518SBarry Smith #define PetscRealPart(a) (a).real() 51329f5518SBarry Smith #define PetscImaginaryPart(a) (a).imag() 523f6de6efSSatish Balay #define PetscAbsScalar(a) std::abs(a) 533f6de6efSSatish Balay #define PetscConj(a) std::conj(a) 5418a7d68fSSatish Balay #define PetscSqrtScalar(a) std::sqrt(a) 55184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b) 56184914b5SBarry Smith #define PetscExpScalar(a) std::exp(a) 5706c1185fSBarry Smith #define PetscLogScalar(a) std::log(a) 58184914b5SBarry Smith #define PetscSinScalar(a) std::sin(a) 59184914b5SBarry Smith #define PetscCosScalar(a) std::cos(a) 600bfd3fbfSBarry Smith 61ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 624a60b672SMatthew Knepley typedef std::complex<float> PetscScalar; 63ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE) 641093a601SBarry Smith typedef std::complex<double> PetscScalar; 65ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */ 66b7940d39SSatish Balay 671b65fc54SMatthew G Knepley #else /* PETSC_CLANGUAGE_CXX */ 681093a601SBarry Smith /* C support of complex numbers: Requires C99 compliant compiler*/ 691093a601SBarry Smith #include <complex.h> 70b7940d39SSatish Balay 71ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 7285b47369SMatthew Knepley typedef float complex PetscScalar; 7385b47369SMatthew Knepley 7485b47369SMatthew Knepley #define PetscRealPart(a) crealf(a) 7585b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagf(a) 7685b47369SMatthew Knepley #define PetscAbsScalar(a) cabsf(a) 7785b47369SMatthew Knepley #define PetscConj(a) conjf(a) 7885b47369SMatthew Knepley #define PetscSqrtScalar(a) csqrtf(a) 7985b47369SMatthew Knepley #define PetscPowScalar(a,b) cpowf(a,b) 8085b47369SMatthew Knepley #define PetscExpScalar(a) cexpf(a) 8106c1185fSBarry Smith #define PetscLogScalar(a) clogf(a) 8285b47369SMatthew Knepley #define PetscSinScalar(a) csinf(a) 8385b47369SMatthew Knepley #define PetscCosScalar(a) ccosf(a) 841093a601SBarry Smith 85ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE) 861093a601SBarry Smith typedef double complex PetscScalar; 871093a601SBarry Smith 881093a601SBarry Smith #define PetscRealPart(a) creal(a) 891093a601SBarry Smith #define PetscImaginaryPart(a) cimag(a) 901093a601SBarry Smith #define PetscAbsScalar(a) cabs(a) 911093a601SBarry Smith #define PetscConj(a) conj(a) 921093a601SBarry Smith #define PetscSqrtScalar(a) csqrt(a) 931093a601SBarry Smith #define PetscPowScalar(a,b) cpow(a,b) 941093a601SBarry Smith #define PetscExpScalar(a) cexp(a) 951093a601SBarry Smith #define PetscLogScalar(a) clog(a) 961093a601SBarry Smith #define PetscSinScalar(a) csin(a) 971093a601SBarry Smith #define PetscCosScalar(a) ccos(a) 981093a601SBarry Smith 99ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */ 1001b65fc54SMatthew G Knepley #endif /* PETSC_CLANGUAGE_CXX */ 101e489efc1SBarry Smith 10270da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX) 103500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX 104500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX 10570da9c3bSJed Brown #else 106*014dd563SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX; 107*014dd563SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX; 1081b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */ 1092c876bd9SBarry Smith 110ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 111500d8756SSatish Balay #define MPIU_SCALAR MPIU_C_COMPLEX 112ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE) 113500d8756SSatish Balay #define MPIU_SCALAR MPIU_C_DOUBLE_COMPLEX 114ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */ 11575567043SBarry Smith 1161093a601SBarry Smith /* 1171093a601SBarry Smith real number definitions 1181093a601SBarry Smith */ 1191b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */ 120ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 12187828ca2SBarry Smith #define MPIU_SCALAR MPI_FLOAT 1221093a601SBarry Smith typedef float PetscScalar; 123ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE) 1241093a601SBarry Smith #define MPIU_SCALAR MPI_DOUBLE 1251093a601SBarry Smith typedef double PetscScalar; 126ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128) 127*014dd563SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128; 128c90a1750SBarry Smith #define MPIU_SCALAR MPIU___FLOAT128 1290d0cc1b5SBarry Smith typedef __float128 PetscScalar; 130ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */ 131329f5518SBarry Smith #define PetscRealPart(a) (a) 132c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.) 133c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;} 134e489efc1SBarry Smith #define PetscConj(a) (a) 135ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128) 13618a7d68fSSatish Balay #define PetscSqrtScalar(a) sqrt(a) 137184914b5SBarry Smith #define PetscPowScalar(a,b) pow(a,b) 138184914b5SBarry Smith #define PetscExpScalar(a) exp(a) 13906c1185fSBarry Smith #define PetscLogScalar(a) log(a) 140184914b5SBarry Smith #define PetscSinScalar(a) sin(a) 141184914b5SBarry Smith #define PetscCosScalar(a) cos(a) 142ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */ 1430d0cc1b5SBarry Smith #include <quadmath.h> 1440d0cc1b5SBarry Smith #define PetscSqrtScalar(a) sqrtq(a) 1450d0cc1b5SBarry Smith #define PetscPowScalar(a,b) powq(a,b) 1460d0cc1b5SBarry Smith #define PetscExpScalar(a) expq(a) 1470d0cc1b5SBarry Smith #define PetscLogScalar(a) logq(a) 1480d0cc1b5SBarry Smith #define PetscSinScalar(a) sinq(a) 1490d0cc1b5SBarry Smith #define PetscCosScalar(a) cosq(a) 150ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */ 151b0a32e0cSBarry Smith 1521b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */ 153e489efc1SBarry Smith 154da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1) 15526aa1773SMatthew Knepley #define PetscAbs(a) (((a) >= 0) ? (a) : -(a)) 1563f1db9ecSBarry Smith 157314da920SBarry Smith /* --------------------------------------------------------------------------*/ 158314da920SBarry Smith 159e489efc1SBarry Smith /* 160f22f69f0SBarry Smith Certain objects may be created using either single or double precision. 161f22f69f0SBarry Smith This is currently not used. 162e489efc1SBarry Smith */ 163557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision; 164e489efc1SBarry Smith 165e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */ 166*014dd563SJed Brown PETSC_EXTERN PetscScalar PETSC_i; 167e489efc1SBarry Smith 168b6a5bde7SBarry Smith /*MC 169b6a5bde7SBarry Smith PetscMin - Returns minimum of two numbers 170b6a5bde7SBarry Smith 171eca87e8dSBarry Smith Synopsis: 172eca87e8dSBarry Smith type PetscMin(type v1,type v2) 173eca87e8dSBarry Smith 174eca87e8dSBarry Smith Not Collective 175eca87e8dSBarry Smith 176b6a5bde7SBarry Smith Input Parameter: 177b6a5bde7SBarry Smith + v1 - first value to find minimum of 178b6a5bde7SBarry Smith - v2 - second value to find minimum of 179b6a5bde7SBarry Smith 180b6a5bde7SBarry Smith 181b6a5bde7SBarry Smith Notes: type can be integer or floating point value 182b6a5bde7SBarry Smith 183b6a5bde7SBarry Smith Level: beginner 184b6a5bde7SBarry Smith 185b6a5bde7SBarry Smith 186d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 187b6a5bde7SBarry Smith 188b6a5bde7SBarry Smith M*/ 189e489efc1SBarry Smith #define PetscMin(a,b) (((a)<(b)) ? (a) : (b)) 190b6a5bde7SBarry Smith 191b6a5bde7SBarry Smith /*MC 192b6a5bde7SBarry Smith PetscMax - Returns maxium of two numbers 193b6a5bde7SBarry Smith 194eca87e8dSBarry Smith Synopsis: 195eca87e8dSBarry Smith type max PetscMax(type v1,type v2) 196eca87e8dSBarry Smith 197eca87e8dSBarry Smith Not Collective 198eca87e8dSBarry Smith 199b6a5bde7SBarry Smith Input Parameter: 200b6a5bde7SBarry Smith + v1 - first value to find maximum of 201b6a5bde7SBarry Smith - v2 - second value to find maximum of 202b6a5bde7SBarry Smith 203b6a5bde7SBarry Smith Notes: type can be integer or floating point value 204b6a5bde7SBarry Smith 205b6a5bde7SBarry Smith Level: beginner 206b6a5bde7SBarry Smith 207d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 208b6a5bde7SBarry Smith 209b6a5bde7SBarry Smith M*/ 210e489efc1SBarry Smith #define PetscMax(a,b) (((a)<(b)) ? (b) : (a)) 211b6a5bde7SBarry Smith 212b6a5bde7SBarry Smith /*MC 213d9a4bb16SJed Brown PetscClipInterval - Returns a number clipped to be within an interval 214d9a4bb16SJed Brown 215d9a4bb16SJed Brown Synopsis: 216d9a4bb16SJed Brown type clip PetscClipInterval(type x,type a,type b) 217d9a4bb16SJed Brown 218d9a4bb16SJed Brown Not Collective 219d9a4bb16SJed Brown 220d9a4bb16SJed Brown Input Parameter: 221d9a4bb16SJed Brown + x - value to use if within interval (a,b) 222d9a4bb16SJed Brown . a - lower end of interval 223d9a4bb16SJed Brown - b - upper end of interval 224d9a4bb16SJed Brown 225d9a4bb16SJed Brown Notes: type can be integer or floating point value 226d9a4bb16SJed Brown 227d9a4bb16SJed Brown Level: beginner 228d9a4bb16SJed Brown 229d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 230d9a4bb16SJed Brown 231d9a4bb16SJed Brown M*/ 232d9a4bb16SJed Brown #define PetscClipInterval(x,a,b) (PetscMax((a),PetscMin((x),(b)))) 233d9a4bb16SJed Brown 234d9a4bb16SJed Brown /*MC 235b6a5bde7SBarry Smith PetscAbsInt - Returns the absolute value of an integer 236b6a5bde7SBarry Smith 237b6a5bde7SBarry Smith Synopsis: 238b6a5bde7SBarry Smith int abs PetscAbsInt(int v1) 239b6a5bde7SBarry Smith 240eca87e8dSBarry Smith Not Collective 241eca87e8dSBarry Smith 242eca87e8dSBarry Smith Input Parameter: 243eca87e8dSBarry Smith . v1 - the integer 244b6a5bde7SBarry Smith 245b6a5bde7SBarry Smith Level: beginner 246b6a5bde7SBarry Smith 247b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr() 248b6a5bde7SBarry Smith 249b6a5bde7SBarry Smith M*/ 250e489efc1SBarry Smith #define PetscAbsInt(a) (((a)<0) ? -(a) : (a)) 251b6a5bde7SBarry Smith 252b6a5bde7SBarry Smith /*MC 253b6a5bde7SBarry Smith PetscAbsReal - Returns the absolute value of an real number 254b6a5bde7SBarry Smith 255eca87e8dSBarry Smith Synopsis: 256eca87e8dSBarry Smith Real abs PetscAbsReal(PetscReal v1) 257eca87e8dSBarry Smith 258eca87e8dSBarry Smith Not Collective 259eca87e8dSBarry Smith 260b6a5bde7SBarry Smith Input Parameter: 261b6a5bde7SBarry Smith . v1 - the double 262b6a5bde7SBarry Smith 263b6a5bde7SBarry Smith 264b6a5bde7SBarry Smith Level: beginner 265b6a5bde7SBarry Smith 266b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr() 267b6a5bde7SBarry Smith 268b6a5bde7SBarry Smith M*/ 269f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0) ? -(a) : (a)) 270b6a5bde7SBarry Smith 271b6a5bde7SBarry Smith /*MC 272b6a5bde7SBarry Smith PetscSqr - Returns the square of a number 273b6a5bde7SBarry Smith 274b6a5bde7SBarry Smith Synopsis: 275b6a5bde7SBarry Smith type sqr PetscSqr(type v1) 276b6a5bde7SBarry Smith 277eca87e8dSBarry Smith Not Collective 278eca87e8dSBarry Smith 279eca87e8dSBarry Smith Input Parameter: 280eca87e8dSBarry Smith . v1 - the value 281eca87e8dSBarry Smith 282b6a5bde7SBarry Smith Notes: type can be integer or floating point value 283b6a5bde7SBarry Smith 284b6a5bde7SBarry Smith Level: beginner 285b6a5bde7SBarry Smith 286b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal() 287b6a5bde7SBarry Smith 288b6a5bde7SBarry Smith M*/ 2894ebda54eSMatthew Knepley #define PetscSqr(a) ((a)*(a)) 290e489efc1SBarry Smith 291314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 292314da920SBarry Smith /* 293d34fcf5fSBarry Smith Basic constants 294314da920SBarry Smith */ 295ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) 296d34fcf5fSBarry Smith #define PETSC_PI M_PIq 297d34fcf5fSBarry Smith #elif defined(M_PI) 298d34fcf5fSBarry Smith #define PETSC_PI M_PI 299d34fcf5fSBarry Smith #else 300faa6e9b0SMatthew G Knepley #define PETSC_PI 3.14159265358979323846264338327950288419716939937510582 301d34fcf5fSBarry Smith #endif 302d34fcf5fSBarry Smith 303ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES) 30471fd2e92SBarry Smith #define PETSC_MAX_INT 2147483647 305ab824b78SBarry Smith #define PETSC_MIN_INT (-PETSC_MAX_INT - 1) 306ab824b78SBarry Smith #else 307ab824b78SBarry Smith #define PETSC_MAX_INT 9223372036854775807L 308ab824b78SBarry Smith #define PETSC_MIN_INT (-PETSC_MAX_INT - 1) 309ab824b78SBarry Smith #endif 310e489efc1SBarry Smith 311ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 312ab824b78SBarry Smith # define PETSC_MAX_REAL 3.40282346638528860e+38F 313ea345e14SBarry Smith # define PETSC_MIN_REAL -PETSC_MAX_REAL 31482a7e548SBarry Smith # define PETSC_MACHINE_EPSILON 1.19209290e-07F 31582a7e548SBarry Smith # define PETSC_SQRT_MACHINE_EPSILON 3.45266983e-04F 316cf6e855fSSatish Balay # define PETSC_SMALL 1.e-5 317ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE) 318ab824b78SBarry Smith # define PETSC_MAX_REAL 1.7976931348623157e+308 319ea345e14SBarry Smith # define PETSC_MIN_REAL -PETSC_MAX_REAL 32082a7e548SBarry Smith # define PETSC_MACHINE_EPSILON 2.2204460492503131e-16 32182a7e548SBarry Smith # define PETSC_SQRT_MACHINE_EPSILON 1.490116119384766e-08 322cf6e855fSSatish Balay # define PETSC_SMALL 1.e-10 323ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128) 324ea345e14SBarry Smith # define PETSC_MAX_REAL FLT128_MAX 325ce63c4c1SBarry Smith # define PETSC_MIN_REAL -FLT128_MAX 326d34fcf5fSBarry Smith # define PETSC_MACHINE_EPSILON FLT128_EPSILON 327d34fcf5fSBarry Smith # define PETSC_SQRT_MACHINE_EPSILON 1.38777878078e-17 328d34fcf5fSBarry Smith # define PETSC_SMALL 1.e-20 32982adfdadSBarry Smith #endif 33082adfdadSBarry Smith 3319cf09972SJed Brown #if defined PETSC_HAVE_ADIC 3329cf09972SJed Brown /* Use MPI_Allreduce when ADIC is not available. */ 333*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*); 334*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*); 335*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*); 3369cf09972SJed Brown #endif 3373e523bebSBarry Smith 338*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanScalar(PetscScalar); 339*014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal); 3409a25a3ccSBarry Smith 341314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 34287828ca2SBarry Smith #define PassiveReal PetscReal 343ea709b57SSatish Balay #define PassiveScalar PetscScalar 344d3ecb3a7SBarry Smith 34598725619SBarry Smith /* 34698725619SBarry Smith These macros are currently hardwired to match the regular data types, so there is no support for a different 34798725619SBarry Smith MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again. 34898725619SBarry Smith */ 34998725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR 35098725619SBarry Smith typedef PetscScalar MatScalar; 35198725619SBarry Smith typedef PetscReal MatReal; 35298725619SBarry Smith 353e9fa29b7SSatish Balay 354e489efc1SBarry Smith #endif 355