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 2398725619SBarry 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 29*1093a601SBarry Smith /* 30*1093a601SBarry Smith Complex number definitions 31*1093a601SBarry Smith */ 32aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 33b7940d39SSatish Balay #if defined(PETSC_CLANGUAGE_CXX) 34*1093a601SBarry Smith /* C++ support of complex number */ 35df9b3741SSatish Balay #include <complex> 36adc17e78SSatish Balay 37329f5518SBarry Smith #define PetscRealPart(a) (a).real() 38329f5518SBarry Smith #define PetscImaginaryPart(a) (a).imag() 393f6de6efSSatish Balay #define PetscAbsScalar(a) std::abs(a) 403f6de6efSSatish Balay #define PetscConj(a) std::conj(a) 4118a7d68fSSatish Balay #define PetscSqrtScalar(a) std::sqrt(a) 42184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b) 43184914b5SBarry Smith #define PetscExpScalar(a) std::exp(a) 4406c1185fSBarry Smith #define PetscLogScalar(a) std::log(a) 45184914b5SBarry Smith #define PetscSinScalar(a) std::sin(a) 46184914b5SBarry Smith #define PetscCosScalar(a) std::cos(a) 470bfd3fbfSBarry Smith 4865460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 494a60b672SMatthew Knepley typedef std::complex<float> PetscScalar; 50*1093a601SBarry Smith #elif defined(PETSC_USE_SCALAR_DOUBLE) 51*1093a601SBarry Smith typedef std::complex<double> PetscScalar; 5265460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 534a60b672SMatthew Knepley typedef std::complex<long double> PetscScalar; 544a60b672SMatthew Knepley #endif 55b7940d39SSatish Balay 56*1093a601SBarry Smith #else 57*1093a601SBarry Smith /* C support of complex numbers: Requires C99 compliant compiler*/ 58*1093a601SBarry Smith #include <complex.h> 59b7940d39SSatish Balay 6065460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 6185b47369SMatthew Knepley typedef float complex PetscScalar; 6285b47369SMatthew Knepley 6385b47369SMatthew Knepley #define PetscRealPart(a) crealf(a) 6485b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagf(a) 6585b47369SMatthew Knepley #define PetscAbsScalar(a) cabsf(a) 6685b47369SMatthew Knepley #define PetscConj(a) conjf(a) 6785b47369SMatthew Knepley #define PetscSqrtScalar(a) csqrtf(a) 6885b47369SMatthew Knepley #define PetscPowScalar(a,b) cpowf(a,b) 6985b47369SMatthew Knepley #define PetscExpScalar(a) cexpf(a) 7006c1185fSBarry Smith #define PetscLogScalar(a) clogf(a) 7185b47369SMatthew Knepley #define PetscSinScalar(a) csinf(a) 7285b47369SMatthew Knepley #define PetscCosScalar(a) ccosf(a) 73*1093a601SBarry Smith 74*1093a601SBarry Smith #elif defined(PETSC_USE_SCALAR_DOUBLE) 75*1093a601SBarry Smith typedef double complex PetscScalar; 76*1093a601SBarry Smith 77*1093a601SBarry Smith #define PetscRealPart(a) creal(a) 78*1093a601SBarry Smith #define PetscImaginaryPart(a) cimag(a) 79*1093a601SBarry Smith #define PetscAbsScalar(a) cabs(a) 80*1093a601SBarry Smith #define PetscConj(a) conj(a) 81*1093a601SBarry Smith #define PetscSqrtScalar(a) csqrt(a) 82*1093a601SBarry Smith #define PetscPowScalar(a,b) cpow(a,b) 83*1093a601SBarry Smith #define PetscExpScalar(a) cexp(a) 84*1093a601SBarry Smith #define PetscLogScalar(a) clog(a) 85*1093a601SBarry Smith #define PetscSinScalar(a) csin(a) 86*1093a601SBarry Smith #define PetscCosScalar(a) ccos(a) 87*1093a601SBarry Smith 8865460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 8985b47369SMatthew Knepley typedef long double complex PetscScalar; 9085b47369SMatthew Knepley 9185b47369SMatthew Knepley #define PetscRealPart(a) creall(a) 9285b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagl(a) 9385b47369SMatthew Knepley #define PetscAbsScalar(a) cabsl(a) 9485b47369SMatthew Knepley #define PetscConj(a) conjl(a) 9585b47369SMatthew Knepley #define PetscSqrtScalar(a) csqrtl(a) 9685b47369SMatthew Knepley #define PetscPowScalar(a,b) cpowl(a,b) 9785b47369SMatthew Knepley #define PetscExpScalar(a) cexpl(a) 9806c1185fSBarry Smith #define PetscLogScalar(a) clogl(a) 9985b47369SMatthew Knepley #define PetscSinScalar(a) csinl(a) 10085b47369SMatthew Knepley #define PetscCosScalar(a) ccosl(a) 10185b47369SMatthew Knepley 102b7940d39SSatish Balay #endif 1034a60b672SMatthew Knepley #endif 104e489efc1SBarry Smith 1052c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX) 1067087cfbeSBarry Smith extern MPI_Datatype MPI_C_DOUBLE_COMPLEX; 1077087cfbeSBarry Smith extern MPI_Datatype MPI_C_COMPLEX; 1082c876bd9SBarry Smith #endif 1092c876bd9SBarry Smith 110a83b8d76SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 111a83b8d76SBarry Smith #define MPIU_SCALAR MPI_C_COMPLEX 112*1093a601SBarry Smith #elif defined(PETSC_USE_SCALAR_DOUBLE) 1132c876bd9SBarry Smith #define MPIU_SCALAR MPI_C_DOUBLE_COMPLEX 114*1093a601SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 115*1093a601SBarry Smith #define MPIU_SCALAR error 116a83b8d76SBarry Smith #endif 11775567043SBarry Smith 118*1093a601SBarry Smith /* 119*1093a601SBarry Smith real number definitions 120*1093a601SBarry Smith */ 121e489efc1SBarry Smith #else 12265460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 12387828ca2SBarry Smith #define MPIU_SCALAR MPI_FLOAT 124*1093a601SBarry Smith typedef float PetscScalar; 125*1093a601SBarry Smith #elif defined(PETSC_USE_SCALAR_DOUBLE) 126*1093a601SBarry Smith #define MPIU_SCALAR MPI_DOUBLE 127*1093a601SBarry Smith typedef double PetscScalar; 12865460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 129f68b968cSBarry Smith #define MPIU_SCALAR MPI_LONG_DOUBLE 130*1093a601SBarry Smith typedef long double PetscScalar; 13187828ca2SBarry Smith #endif 132329f5518SBarry Smith #define PetscRealPart(a) (a) 13375567043SBarry Smith #define PetscImaginaryPart(a) (0.) 134e489efc1SBarry Smith #define PetscAbsScalar(a) (((a)<0.0) ? -(a) : (a)) 135e489efc1SBarry Smith #define PetscConj(a) (a) 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) 142b0a32e0cSBarry Smith 143e489efc1SBarry Smith #endif 144e489efc1SBarry Smith 14565460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 146d7d1e502SBarry Smith #define MPIU_REAL MPI_FLOAT 147*1093a601SBarry Smith typedef float PetscReal; 148*1093a601SBarry Smith #elif defined(PETSC_USE_SCALAR_DOUBLE) 149*1093a601SBarry Smith #define MPIU_REAL MPI_DOUBLE 150*1093a601SBarry Smith typedef double PetscReal; 15165460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 152f68b968cSBarry Smith #define MPIU_REAL MPI_LONG_DOUBLE 153*1093a601SBarry Smith typedef long double PetscReal; 154d7d1e502SBarry Smith #endif 155d7d1e502SBarry Smith 156da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1) 15726aa1773SMatthew Knepley #define PetscAbs(a) (((a) >= 0) ? (a) : -(a)) 1583f1db9ecSBarry Smith 159314da920SBarry Smith /* --------------------------------------------------------------------------*/ 160314da920SBarry Smith 161e489efc1SBarry Smith /* 162f22f69f0SBarry Smith Certain objects may be created using either single or double precision. 163f22f69f0SBarry Smith This is currently not used. 164e489efc1SBarry Smith */ 165557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision; 166e489efc1SBarry Smith 167e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */ 1687087cfbeSBarry Smith extern PetscScalar PETSC_i; 169e489efc1SBarry Smith 170b6a5bde7SBarry Smith /*MC 171b6a5bde7SBarry Smith PetscMin - Returns minimum of two numbers 172b6a5bde7SBarry Smith 173eca87e8dSBarry Smith Synopsis: 174eca87e8dSBarry Smith type PetscMin(type v1,type v2) 175eca87e8dSBarry Smith 176eca87e8dSBarry Smith Not Collective 177eca87e8dSBarry Smith 178b6a5bde7SBarry Smith Input Parameter: 179b6a5bde7SBarry Smith + v1 - first value to find minimum of 180b6a5bde7SBarry Smith - v2 - second value to find minimum of 181b6a5bde7SBarry Smith 182b6a5bde7SBarry Smith 183b6a5bde7SBarry Smith Notes: type can be integer or floating point value 184b6a5bde7SBarry Smith 185b6a5bde7SBarry Smith Level: beginner 186b6a5bde7SBarry Smith 187b6a5bde7SBarry Smith 188b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 189b6a5bde7SBarry Smith 190b6a5bde7SBarry Smith M*/ 191e489efc1SBarry Smith #define PetscMin(a,b) (((a)<(b)) ? (a) : (b)) 192b6a5bde7SBarry Smith 193b6a5bde7SBarry Smith /*MC 194b6a5bde7SBarry Smith PetscMax - Returns maxium of two numbers 195b6a5bde7SBarry Smith 196eca87e8dSBarry Smith Synopsis: 197eca87e8dSBarry Smith type max PetscMax(type v1,type v2) 198eca87e8dSBarry Smith 199eca87e8dSBarry Smith Not Collective 200eca87e8dSBarry Smith 201b6a5bde7SBarry Smith Input Parameter: 202b6a5bde7SBarry Smith + v1 - first value to find maximum of 203b6a5bde7SBarry Smith - v2 - second value to find maximum of 204b6a5bde7SBarry Smith 205b6a5bde7SBarry Smith Notes: type can be integer or floating point value 206b6a5bde7SBarry Smith 207b6a5bde7SBarry Smith Level: beginner 208b6a5bde7SBarry Smith 209b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 210b6a5bde7SBarry Smith 211b6a5bde7SBarry Smith M*/ 212e489efc1SBarry Smith #define PetscMax(a,b) (((a)<(b)) ? (b) : (a)) 213b6a5bde7SBarry Smith 214b6a5bde7SBarry Smith /*MC 215b6a5bde7SBarry Smith PetscAbsInt - Returns the absolute value of an integer 216b6a5bde7SBarry Smith 217b6a5bde7SBarry Smith Synopsis: 218b6a5bde7SBarry Smith int abs PetscAbsInt(int v1) 219b6a5bde7SBarry Smith 220eca87e8dSBarry Smith Not Collective 221eca87e8dSBarry Smith 222eca87e8dSBarry Smith Input Parameter: 223eca87e8dSBarry Smith . v1 - the integer 224b6a5bde7SBarry Smith 225b6a5bde7SBarry Smith Level: beginner 226b6a5bde7SBarry Smith 227b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr() 228b6a5bde7SBarry Smith 229b6a5bde7SBarry Smith M*/ 230e489efc1SBarry Smith #define PetscAbsInt(a) (((a)<0) ? -(a) : (a)) 231b6a5bde7SBarry Smith 232b6a5bde7SBarry Smith /*MC 233b6a5bde7SBarry Smith PetscAbsReal - Returns the absolute value of an real number 234b6a5bde7SBarry Smith 235eca87e8dSBarry Smith Synopsis: 236eca87e8dSBarry Smith Real abs PetscAbsReal(PetscReal v1) 237eca87e8dSBarry Smith 238eca87e8dSBarry Smith Not Collective 239eca87e8dSBarry Smith 240b6a5bde7SBarry Smith Input Parameter: 241b6a5bde7SBarry Smith . v1 - the double 242b6a5bde7SBarry Smith 243b6a5bde7SBarry Smith 244b6a5bde7SBarry Smith Level: beginner 245b6a5bde7SBarry Smith 246b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr() 247b6a5bde7SBarry Smith 248b6a5bde7SBarry Smith M*/ 249f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0) ? -(a) : (a)) 250b6a5bde7SBarry Smith 251b6a5bde7SBarry Smith /*MC 252b6a5bde7SBarry Smith PetscSqr - Returns the square of a number 253b6a5bde7SBarry Smith 254b6a5bde7SBarry Smith Synopsis: 255b6a5bde7SBarry Smith type sqr PetscSqr(type v1) 256b6a5bde7SBarry Smith 257eca87e8dSBarry Smith Not Collective 258eca87e8dSBarry Smith 259eca87e8dSBarry Smith Input Parameter: 260eca87e8dSBarry Smith . v1 - the value 261eca87e8dSBarry Smith 262b6a5bde7SBarry Smith Notes: type can be integer or floating point value 263b6a5bde7SBarry Smith 264b6a5bde7SBarry Smith Level: beginner 265b6a5bde7SBarry Smith 266b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal() 267b6a5bde7SBarry Smith 268b6a5bde7SBarry Smith M*/ 2694ebda54eSMatthew Knepley #define PetscSqr(a) ((a)*(a)) 270e489efc1SBarry Smith 271314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 272314da920SBarry Smith /* 27303c60df9SBarry Smith Basic constants - These should be done much better 274314da920SBarry Smith */ 275314da920SBarry Smith #define PETSC_PI 3.14159265358979323846264 276314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994 27771fd2e92SBarry Smith #define PETSC_MAX_INT 2147483647 27871fd2e92SBarry Smith #define PETSC_MIN_INT -2147483647 279e489efc1SBarry Smith 28065460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 2817e032f8bSBarry Smith # define PETSC_MAX 1.e30 2827e032f8bSBarry Smith # define PETSC_MIN -1.e30 283f10639e6SSatish Balay # define PETSC_MACHINE_EPSILON 1.e-7 284f10639e6SSatish Balay # define PETSC_SQRT_MACHINE_EPSILON 3.e-4 285cf6e855fSSatish Balay # define PETSC_SMALL 1.e-5 28682adfdadSBarry Smith #else 2877e032f8bSBarry Smith # define PETSC_MAX 1.e300 2887e032f8bSBarry Smith # define PETSC_MIN -1.e300 289f10639e6SSatish Balay # define PETSC_MACHINE_EPSILON 1.e-14 290f10639e6SSatish Balay # define PETSC_SQRT_MACHINE_EPSILON 1.e-7 291cf6e855fSSatish Balay # define PETSC_SMALL 1.e-10 29282adfdadSBarry Smith #endif 29382adfdadSBarry Smith 2949cf09972SJed Brown #if defined PETSC_HAVE_ADIC 2959cf09972SJed Brown /* Use MPI_Allreduce when ADIC is not available. */ 2967087cfbeSBarry Smith extern PetscErrorCode PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*); 2977087cfbeSBarry Smith extern PetscErrorCode PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*); 2987087cfbeSBarry Smith extern PetscErrorCode PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*); 2999cf09972SJed Brown #endif 3003e523bebSBarry Smith 3010763cb5fSBarry Smith /*MC 3020763cb5fSBarry Smith PetscIsInfOrNan - Returns 1 if the input double has an infinity for Not-a-number (Nan) value, otherwise 0. 3033e523bebSBarry Smith 3040763cb5fSBarry Smith Input Parameter: 3050763cb5fSBarry Smith . a - the double 3060763cb5fSBarry Smith 3070763cb5fSBarry Smith 3080763cb5fSBarry Smith Notes: uses the C99 standard isinf() and isnan() on systems where they exist. 30983886165SBarry Smith Otherwises uses ( (a - a) != 0.0), note that some optimizing compiles compile 3100763cb5fSBarry Smith out this form, thus removing the check. 3110763cb5fSBarry Smith 31283672c4dSSatish Balay Level: beginner 31383672c4dSSatish Balay 31483672c4dSSatish Balay M*/ 3159a25a3ccSBarry Smith #if defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN) 31662cbcd01SMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) { 31762cbcd01SMatthew G Knepley return isinf(PetscAbsScalar(a)) || isnan(PetscAbsScalar(a)); 31862cbcd01SMatthew G Knepley } 31962cbcd01SMatthew G Knepley PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) { 32062cbcd01SMatthew G Knepley return isinf(a) || isnan(a); 32162cbcd01SMatthew G Knepley } 32262b4c0b3SBarry Smith #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN) 323270b8587SSatish Balay #if defined(PETSC_HAVE_FLOAT_H) 32498725619SBarry Smith #include "float.h" /* Microsoft Windows defines _finite() in float.h */ 325270b8587SSatish Balay #endif 326961faeafSBarry Smith #if defined(PETSC_HAVE_IEEEFP_H) 327961faeafSBarry Smith #include "ieeefp.h" /* Solaris prototypes these here */ 328961faeafSBarry Smith #endif 32998725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) { 33098725619SBarry Smith return !_finite(PetscAbsScalar(a)) || _isnan(PetscAbsScalar(a)); 33198725619SBarry Smith } 33298725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) { 33398725619SBarry Smith return !_finite(a) || _isnan(a); 33498725619SBarry Smith } 3359a25a3ccSBarry Smith #else 33698725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanScalar(PetscScalar a) { 33798725619SBarry Smith return ((a - a) != 0.0); 33898725619SBarry Smith } 33998725619SBarry Smith PETSC_STATIC_INLINE PetscErrorCode PetscIsInfOrNanReal(PetscReal a) { 34098725619SBarry Smith return ((a - a) != 0.0); 34198725619SBarry Smith } 3429a25a3ccSBarry Smith #endif 3439a25a3ccSBarry Smith 3449a25a3ccSBarry Smith 345314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 346e489efc1SBarry Smith /* 347b0a32e0cSBarry Smith PetscLogDouble variables are used to contain double precision numbers 348e489efc1SBarry Smith that are not used in the numerical computations, but rather in logging, 349e489efc1SBarry Smith timing etc. 350e489efc1SBarry Smith */ 351b0a32e0cSBarry Smith typedef double PetscLogDouble; 352b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE 353e489efc1SBarry Smith 35487828ca2SBarry Smith #define PassiveReal PetscReal 355ea709b57SSatish Balay #define PassiveScalar PetscScalar 356d3ecb3a7SBarry Smith 35798725619SBarry Smith /* 35898725619SBarry Smith These macros are currently hardwired to match the regular data types, so there is no support for a different 35998725619SBarry Smith MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again. 36098725619SBarry Smith */ 36198725619SBarry Smith #define MPIU_MATSCALAR MPIU_SCALAR 36298725619SBarry Smith typedef PetscScalar MatScalar; 36398725619SBarry Smith typedef PetscReal MatReal; 36498725619SBarry Smith 365e9fa29b7SSatish Balay 366e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END 367e489efc1SBarry Smith #endif 368