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 15014dd563SJed Brown PETSC_EXTERN MPI_Datatype MPIU_2SCALAR; 16014dd563SJed 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) 37*574fde7bSSatish Balay #if defined(__cplusplus) 38*574fde7bSSatish Balay extern "C" { 39*574fde7bSSatish Balay #endif 40*574fde7bSSatish Balay #include <quadmath.h> 41*574fde7bSSatish Balay #if defined(__cplusplus) 42*574fde7bSSatish Balay } 43*574fde7bSSatish Balay #endif 44c1d390e3SJed Brown #define MPIU_REAL MPIU___FLOAT128 45c1d390e3SJed Brown typedef __float128 PetscReal; 468f1a2a5eSBarry Smith #define PetscSqrtReal(a) sqrtq(a) 47c1d390e3SJed Brown #endif /* PETSC_USE_REAL_* */ 4859cb5930SBarry Smith 491093a601SBarry Smith /* 501093a601SBarry Smith Complex number definitions 511093a601SBarry Smith */ 52aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 53b7940d39SSatish Balay #if defined(PETSC_CLANGUAGE_CXX) 541093a601SBarry Smith /* C++ support of complex number */ 55df9b3741SSatish Balay #include <complex> 56adc17e78SSatish Balay 57329f5518SBarry Smith #define PetscRealPart(a) (a).real() 58329f5518SBarry Smith #define PetscImaginaryPart(a) (a).imag() 593f6de6efSSatish Balay #define PetscAbsScalar(a) std::abs(a) 603f6de6efSSatish Balay #define PetscConj(a) std::conj(a) 6118a7d68fSSatish Balay #define PetscSqrtScalar(a) std::sqrt(a) 62184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b) 63184914b5SBarry Smith #define PetscExpScalar(a) std::exp(a) 6406c1185fSBarry Smith #define PetscLogScalar(a) std::log(a) 65184914b5SBarry Smith #define PetscSinScalar(a) std::sin(a) 66184914b5SBarry Smith #define PetscCosScalar(a) std::cos(a) 670bfd3fbfSBarry Smith 68ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 694a60b672SMatthew Knepley typedef std::complex<float> PetscScalar; 70ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE) 711093a601SBarry Smith typedef std::complex<double> PetscScalar; 72ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */ 73b7940d39SSatish Balay 741b65fc54SMatthew G Knepley #else /* PETSC_CLANGUAGE_CXX */ 751093a601SBarry Smith /* C support of complex numbers: Requires C99 compliant compiler*/ 761093a601SBarry Smith #include <complex.h> 77b7940d39SSatish Balay 78ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 7985b47369SMatthew Knepley typedef float complex PetscScalar; 8085b47369SMatthew Knepley 8185b47369SMatthew Knepley #define PetscRealPart(a) crealf(a) 8285b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagf(a) 8385b47369SMatthew Knepley #define PetscAbsScalar(a) cabsf(a) 8485b47369SMatthew Knepley #define PetscConj(a) conjf(a) 8585b47369SMatthew Knepley #define PetscSqrtScalar(a) csqrtf(a) 8685b47369SMatthew Knepley #define PetscPowScalar(a,b) cpowf(a,b) 8785b47369SMatthew Knepley #define PetscExpScalar(a) cexpf(a) 8806c1185fSBarry Smith #define PetscLogScalar(a) clogf(a) 8985b47369SMatthew Knepley #define PetscSinScalar(a) csinf(a) 9085b47369SMatthew Knepley #define PetscCosScalar(a) ccosf(a) 911093a601SBarry Smith 92ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE) 931093a601SBarry Smith typedef double complex PetscScalar; 941093a601SBarry Smith 951093a601SBarry Smith #define PetscRealPart(a) creal(a) 961093a601SBarry Smith #define PetscImaginaryPart(a) cimag(a) 971093a601SBarry Smith #define PetscAbsScalar(a) cabs(a) 981093a601SBarry Smith #define PetscConj(a) conj(a) 991093a601SBarry Smith #define PetscSqrtScalar(a) csqrt(a) 1001093a601SBarry Smith #define PetscPowScalar(a,b) cpow(a,b) 1011093a601SBarry Smith #define PetscExpScalar(a) cexp(a) 1021093a601SBarry Smith #define PetscLogScalar(a) clog(a) 1031093a601SBarry Smith #define PetscSinScalar(a) csin(a) 1041093a601SBarry Smith #define PetscCosScalar(a) ccos(a) 1051093a601SBarry Smith 106ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */ 1071b65fc54SMatthew G Knepley #endif /* PETSC_CLANGUAGE_CXX */ 108e489efc1SBarry Smith 10970da9c3bSJed Brown #if defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX) 110500d8756SSatish Balay #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX 111500d8756SSatish Balay #define MPIU_C_COMPLEX MPI_C_COMPLEX 11270da9c3bSJed Brown #else 113014dd563SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_DOUBLE_COMPLEX; 114014dd563SJed Brown PETSC_EXTERN MPI_Datatype MPIU_C_COMPLEX; 1151b65fc54SMatthew G Knepley #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */ 1162c876bd9SBarry Smith 117ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 118500d8756SSatish Balay #define MPIU_SCALAR MPIU_C_COMPLEX 119ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE) 120500d8756SSatish Balay #define MPIU_SCALAR MPIU_C_DOUBLE_COMPLEX 121ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */ 12275567043SBarry Smith 1231093a601SBarry Smith /* 1241093a601SBarry Smith real number definitions 1251093a601SBarry Smith */ 1261b65fc54SMatthew G Knepley #else /* PETSC_USE_COMPLEX */ 127ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 12887828ca2SBarry Smith #define MPIU_SCALAR MPI_FLOAT 1291093a601SBarry Smith typedef float PetscScalar; 130ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE) 1311093a601SBarry Smith #define MPIU_SCALAR MPI_DOUBLE 1321093a601SBarry Smith typedef double PetscScalar; 133ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128) 134014dd563SJed Brown PETSC_EXTERN MPI_Datatype MPIU___FLOAT128; 135c90a1750SBarry Smith #define MPIU_SCALAR MPIU___FLOAT128 1360d0cc1b5SBarry Smith typedef __float128 PetscScalar; 137ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL_* */ 138329f5518SBarry Smith #define PetscRealPart(a) (a) 139c1d390e3SJed Brown #define PetscImaginaryPart(a) ((PetscReal)0.) 140c1d390e3SJed Brown PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;} 141e489efc1SBarry Smith #define PetscConj(a) (a) 142ce63c4c1SBarry Smith #if !defined(PETSC_USE_REAL___FLOAT128) 14318a7d68fSSatish Balay #define PetscSqrtScalar(a) sqrt(a) 144184914b5SBarry Smith #define PetscPowScalar(a,b) pow(a,b) 145184914b5SBarry Smith #define PetscExpScalar(a) exp(a) 14606c1185fSBarry Smith #define PetscLogScalar(a) log(a) 147184914b5SBarry Smith #define PetscSinScalar(a) sin(a) 148184914b5SBarry Smith #define PetscCosScalar(a) cos(a) 149ce63c4c1SBarry Smith #else /* PETSC_USE_REAL___FLOAT128 */ 1500d0cc1b5SBarry Smith #define PetscSqrtScalar(a) sqrtq(a) 1510d0cc1b5SBarry Smith #define PetscPowScalar(a,b) powq(a,b) 1520d0cc1b5SBarry Smith #define PetscExpScalar(a) expq(a) 1530d0cc1b5SBarry Smith #define PetscLogScalar(a) logq(a) 1540d0cc1b5SBarry Smith #define PetscSinScalar(a) sinq(a) 1550d0cc1b5SBarry Smith #define PetscCosScalar(a) cosq(a) 156ce63c4c1SBarry Smith #endif /* PETSC_USE_REAL___FLOAT128 */ 157b0a32e0cSBarry Smith 1581b65fc54SMatthew G Knepley #endif /* PETSC_USE_COMPLEX */ 159e489efc1SBarry Smith 160da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1) 16126aa1773SMatthew Knepley #define PetscAbs(a) (((a) >= 0) ? (a) : -(a)) 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 */ 172014dd563SJed Brown PETSC_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 192d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), 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 213d9a4bb16SJed Brown .seealso: PetscMin(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 214b6a5bde7SBarry Smith 215b6a5bde7SBarry Smith M*/ 216e489efc1SBarry Smith #define PetscMax(a,b) (((a)<(b)) ? (b) : (a)) 217b6a5bde7SBarry Smith 218b6a5bde7SBarry Smith /*MC 219d9a4bb16SJed Brown PetscClipInterval - Returns a number clipped to be within an interval 220d9a4bb16SJed Brown 221d9a4bb16SJed Brown Synopsis: 222d9a4bb16SJed Brown type clip PetscClipInterval(type x,type a,type b) 223d9a4bb16SJed Brown 224d9a4bb16SJed Brown Not Collective 225d9a4bb16SJed Brown 226d9a4bb16SJed Brown Input Parameter: 227d9a4bb16SJed Brown + x - value to use if within interval (a,b) 228d9a4bb16SJed Brown . a - lower end of interval 229d9a4bb16SJed Brown - b - upper end of interval 230d9a4bb16SJed Brown 231d9a4bb16SJed Brown Notes: type can be integer or floating point value 232d9a4bb16SJed Brown 233d9a4bb16SJed Brown Level: beginner 234d9a4bb16SJed Brown 235d9a4bb16SJed Brown .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 236d9a4bb16SJed Brown 237d9a4bb16SJed Brown M*/ 238d9a4bb16SJed Brown #define PetscClipInterval(x,a,b) (PetscMax((a),PetscMin((x),(b)))) 239d9a4bb16SJed Brown 240d9a4bb16SJed Brown /*MC 241b6a5bde7SBarry Smith PetscAbsInt - Returns the absolute value of an integer 242b6a5bde7SBarry Smith 243b6a5bde7SBarry Smith Synopsis: 244b6a5bde7SBarry Smith int abs PetscAbsInt(int v1) 245b6a5bde7SBarry Smith 246eca87e8dSBarry Smith Not Collective 247eca87e8dSBarry Smith 248eca87e8dSBarry Smith Input Parameter: 249eca87e8dSBarry Smith . v1 - the integer 250b6a5bde7SBarry Smith 251b6a5bde7SBarry Smith Level: beginner 252b6a5bde7SBarry Smith 253b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr() 254b6a5bde7SBarry Smith 255b6a5bde7SBarry Smith M*/ 256e489efc1SBarry Smith #define PetscAbsInt(a) (((a)<0) ? -(a) : (a)) 257b6a5bde7SBarry Smith 258b6a5bde7SBarry Smith /*MC 259b6a5bde7SBarry Smith PetscAbsReal - Returns the absolute value of an real number 260b6a5bde7SBarry Smith 261eca87e8dSBarry Smith Synopsis: 262eca87e8dSBarry Smith Real abs PetscAbsReal(PetscReal v1) 263eca87e8dSBarry Smith 264eca87e8dSBarry Smith Not Collective 265eca87e8dSBarry Smith 266b6a5bde7SBarry Smith Input Parameter: 267b6a5bde7SBarry Smith . v1 - the double 268b6a5bde7SBarry Smith 269b6a5bde7SBarry Smith 270b6a5bde7SBarry Smith Level: beginner 271b6a5bde7SBarry Smith 272b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr() 273b6a5bde7SBarry Smith 274b6a5bde7SBarry Smith M*/ 275f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0) ? -(a) : (a)) 276b6a5bde7SBarry Smith 277b6a5bde7SBarry Smith /*MC 278b6a5bde7SBarry Smith PetscSqr - Returns the square of a number 279b6a5bde7SBarry Smith 280b6a5bde7SBarry Smith Synopsis: 281b6a5bde7SBarry Smith type sqr PetscSqr(type v1) 282b6a5bde7SBarry Smith 283eca87e8dSBarry Smith Not Collective 284eca87e8dSBarry Smith 285eca87e8dSBarry Smith Input Parameter: 286eca87e8dSBarry Smith . v1 - the value 287eca87e8dSBarry Smith 288b6a5bde7SBarry Smith Notes: type can be integer or floating point value 289b6a5bde7SBarry Smith 290b6a5bde7SBarry Smith Level: beginner 291b6a5bde7SBarry Smith 292b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal() 293b6a5bde7SBarry Smith 294b6a5bde7SBarry Smith M*/ 2954ebda54eSMatthew Knepley #define PetscSqr(a) ((a)*(a)) 296e489efc1SBarry Smith 297314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 298314da920SBarry Smith /* 299d34fcf5fSBarry Smith Basic constants 300314da920SBarry Smith */ 301ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL___FLOAT128) 302d34fcf5fSBarry Smith #define PETSC_PI M_PIq 303d34fcf5fSBarry Smith #elif defined(M_PI) 304d34fcf5fSBarry Smith #define PETSC_PI M_PI 305d34fcf5fSBarry Smith #else 306faa6e9b0SMatthew G Knepley #define PETSC_PI 3.14159265358979323846264338327950288419716939937510582 307d34fcf5fSBarry Smith #endif 308d34fcf5fSBarry Smith 309ab824b78SBarry Smith #if !defined(PETSC_USE_64BIT_INDICES) 31071fd2e92SBarry Smith #define PETSC_MAX_INT 2147483647 311ab824b78SBarry Smith #define PETSC_MIN_INT (-PETSC_MAX_INT - 1) 312ab824b78SBarry Smith #else 313ab824b78SBarry Smith #define PETSC_MAX_INT 9223372036854775807L 314ab824b78SBarry Smith #define PETSC_MIN_INT (-PETSC_MAX_INT - 1) 315ab824b78SBarry Smith #endif 316e489efc1SBarry Smith 317ce63c4c1SBarry Smith #if defined(PETSC_USE_REAL_SINGLE) 318ab824b78SBarry Smith # define PETSC_MAX_REAL 3.40282346638528860e+38F 319ea345e14SBarry Smith # define PETSC_MIN_REAL -PETSC_MAX_REAL 32082a7e548SBarry Smith # define PETSC_MACHINE_EPSILON 1.19209290e-07F 32182a7e548SBarry Smith # define PETSC_SQRT_MACHINE_EPSILON 3.45266983e-04F 322cf6e855fSSatish Balay # define PETSC_SMALL 1.e-5 323ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL_DOUBLE) 324ab824b78SBarry Smith # define PETSC_MAX_REAL 1.7976931348623157e+308 325ea345e14SBarry Smith # define PETSC_MIN_REAL -PETSC_MAX_REAL 32682a7e548SBarry Smith # define PETSC_MACHINE_EPSILON 2.2204460492503131e-16 32782a7e548SBarry Smith # define PETSC_SQRT_MACHINE_EPSILON 1.490116119384766e-08 328cf6e855fSSatish Balay # define PETSC_SMALL 1.e-10 329ce63c4c1SBarry Smith #elif defined(PETSC_USE_REAL___FLOAT128) 330ea345e14SBarry Smith # define PETSC_MAX_REAL FLT128_MAX 331ce63c4c1SBarry Smith # define PETSC_MIN_REAL -FLT128_MAX 332d34fcf5fSBarry Smith # define PETSC_MACHINE_EPSILON FLT128_EPSILON 333d34fcf5fSBarry Smith # define PETSC_SQRT_MACHINE_EPSILON 1.38777878078e-17 334d34fcf5fSBarry Smith # define PETSC_SMALL 1.e-20 33582adfdadSBarry Smith #endif 33682adfdadSBarry Smith 3379cf09972SJed Brown #if defined PETSC_HAVE_ADIC 3389cf09972SJed Brown /* Use MPI_Allreduce when ADIC is not available. */ 339014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*); 340014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*); 341014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*); 3429cf09972SJed Brown #endif 3433e523bebSBarry Smith 344014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanScalar(PetscScalar); 345014dd563SJed Brown PETSC_EXTERN PetscErrorCode PetscIsInfOrNanReal(PetscReal); 3469a25a3ccSBarry Smith 347314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 34887828ca2SBarry Smith #define PassiveReal PetscReal 349ea709b57SSatish Balay #define PassiveScalar PetscScalar 350d3ecb3a7SBarry Smith 35198725619SBarry Smith /* 35298725619SBarry Smith These macros are currently hardwired to match the regular data types, so there is no support for a different 35398725619SBarry Smith MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again. 35498725619SBarry Smith */ 35598725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR 35698725619SBarry Smith typedef PetscScalar MatScalar; 35798725619SBarry Smith typedef PetscReal MatReal; 35898725619SBarry Smith 359e9fa29b7SSatish Balay 360e489efc1SBarry Smith #endif 361