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 23ea709b57SSatish Balay PetscScalar which is either always a double 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 36a83b8d76SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 37a83b8d76SBarry Smith /* 38a83b8d76SBarry Smith For d double and c single complex defines the following operations 39a83b8d76SBarry Smith d == c 40a83b8d76SBarry Smith c == d 41a83b8d76SBarry Smith d != c 42a83b8d76SBarry Smith c != d 43a83b8d76SBarry Smith d / c 44a83b8d76SBarry Smith c /d 45a83b8d76SBarry Smith d * c 46a83b8d76SBarry Smith c * d 47a83b8d76SBarry Smith d - c 48a83b8d76SBarry Smith c - d 49a83b8d76SBarry Smith d + c 50a83b8d76SBarry Smith c + d 51a83b8d76SBarry Smith */ 52a83b8d76SBarry Smith namespace std 53a83b8d76SBarry Smith { 54a83b8d76SBarry Smith template<typename _Tp> 55a83b8d76SBarry Smith inline bool 56a83b8d76SBarry Smith operator==(const double& __x, const complex<_Tp>& __y) 57a83b8d76SBarry Smith { return __x == __y.real() && _Tp() == __y.imag(); } 58a83b8d76SBarry Smith template<typename _Tp> 59a83b8d76SBarry Smith inline bool 60a83b8d76SBarry Smith operator==(const complex<_Tp>& __x, const double& __y) 61a83b8d76SBarry Smith { return __x.real() == __y && __x.imag() == _Tp(); } 62a83b8d76SBarry Smith template<typename _Tp> 63a83b8d76SBarry Smith inline bool 64a83b8d76SBarry Smith operator!=(const complex<_Tp>& __x, const double& __y) 65a83b8d76SBarry Smith { return __x.real() != __y || __x.imag() != _Tp(); } 66a83b8d76SBarry Smith template<typename _Tp> 67a83b8d76SBarry Smith inline bool 68a83b8d76SBarry Smith operator!=(const double& __x, const complex<_Tp>& __y) 69a83b8d76SBarry Smith { return __x != __y.real() || _Tp() != __y.imag(); } 70a83b8d76SBarry Smith template<typename _Tp> 71a83b8d76SBarry Smith inline complex<_Tp> 72a83b8d76SBarry Smith operator/(const complex<_Tp>& __x, const double& __y) 73a83b8d76SBarry Smith { 74a83b8d76SBarry Smith complex<_Tp> __r = __x; 75a83b8d76SBarry Smith __r /= ((float)__y); 76a83b8d76SBarry Smith return __r; 77a83b8d76SBarry Smith } 78a83b8d76SBarry Smith template<typename _Tp> 79a83b8d76SBarry Smith inline complex<_Tp> 80a83b8d76SBarry Smith operator/(const double& __x, const complex<_Tp>& __y) 81a83b8d76SBarry Smith { 82a83b8d76SBarry Smith complex<_Tp> __r = (float)__x; 83a83b8d76SBarry Smith __r /= __y; 84a83b8d76SBarry Smith return __r; 85a83b8d76SBarry Smith } 86a83b8d76SBarry Smith template<typename _Tp> 87a83b8d76SBarry Smith inline complex<_Tp> 88a83b8d76SBarry Smith operator*(const complex<_Tp>& __x, const double& __y) 89a83b8d76SBarry Smith { 90a83b8d76SBarry Smith complex<_Tp> __r = __x; 91a83b8d76SBarry Smith __r *= ((float)__y); 92a83b8d76SBarry Smith return __r; 93a83b8d76SBarry Smith } 94a83b8d76SBarry Smith template<typename _Tp> 95a83b8d76SBarry Smith inline complex<_Tp> 96a83b8d76SBarry Smith operator*(const double& __x, const complex<_Tp>& __y) 97a83b8d76SBarry Smith { 98a83b8d76SBarry Smith complex<_Tp> __r = (float)__x; 99a83b8d76SBarry Smith __r *= __y; 100a83b8d76SBarry Smith return __r; 101a83b8d76SBarry Smith } 102a83b8d76SBarry Smith template<typename _Tp> 103a83b8d76SBarry Smith inline complex<_Tp> 104a83b8d76SBarry Smith operator-(const complex<_Tp>& __x, const double& __y) 105a83b8d76SBarry Smith { 106a83b8d76SBarry Smith complex<_Tp> __r = __x; 107a83b8d76SBarry Smith __r -= ((float)__y); 108a83b8d76SBarry Smith return __r; 109a83b8d76SBarry Smith } 110a83b8d76SBarry Smith template<typename _Tp> 111a83b8d76SBarry Smith inline complex<_Tp> 112a83b8d76SBarry Smith operator-(const double& __x, const complex<_Tp>& __y) 113a83b8d76SBarry Smith { 114a83b8d76SBarry Smith complex<_Tp> __r = (float)__x; 115a83b8d76SBarry Smith __r -= __y; 116a83b8d76SBarry Smith return __r; 117a83b8d76SBarry Smith } 118a83b8d76SBarry Smith template<typename _Tp> 119a83b8d76SBarry Smith inline complex<_Tp> 120a83b8d76SBarry Smith operator+(const complex<_Tp>& __x, const double& __y) 121a83b8d76SBarry Smith { 122a83b8d76SBarry Smith complex<_Tp> __r = __x; 123a83b8d76SBarry Smith __r += ((float)__y); 124a83b8d76SBarry Smith return __r; 125a83b8d76SBarry Smith } 126a83b8d76SBarry Smith template<typename _Tp> 127a83b8d76SBarry Smith inline complex<_Tp> 128a83b8d76SBarry Smith operator+(const double& __x, const complex<_Tp>& __y) 129a83b8d76SBarry Smith { 130a83b8d76SBarry Smith complex<_Tp> __r = (float)__x; 131a83b8d76SBarry Smith __r += __y; 132a83b8d76SBarry Smith return __r; 133a83b8d76SBarry Smith } 134a83b8d76SBarry Smith } 135a83b8d76SBarry Smith #endif 136a83b8d76SBarry Smith 137a83b8d76SBarry Smith 138a83b8d76SBarry Smith 139329f5518SBarry Smith #define PetscRealPart(a) (a).real() 140329f5518SBarry Smith #define PetscImaginaryPart(a) (a).imag() 1413f6de6efSSatish Balay #define PetscAbsScalar(a) std::abs(a) 1423f6de6efSSatish Balay #define PetscConj(a) std::conj(a) 14318a7d68fSSatish Balay #define PetscSqrtScalar(a) std::sqrt(a) 144184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b) 145184914b5SBarry Smith #define PetscExpScalar(a) std::exp(a) 14606c1185fSBarry Smith #define PetscLogScalar(a) std::log(a) 147184914b5SBarry Smith #define PetscSinScalar(a) std::sin(a) 148184914b5SBarry Smith #define PetscCosScalar(a) std::cos(a) 1490bfd3fbfSBarry Smith 15065460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 1514a60b672SMatthew Knepley typedef std::complex<float> PetscScalar; 15265460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 1534a60b672SMatthew Knepley typedef std::complex<long double> PetscScalar; 15465460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT) 1554a60b672SMatthew Knepley typedef std::complex<int> PetscScalar; 1564a60b672SMatthew Knepley #else 157ea709b57SSatish Balay typedef std::complex<double> PetscScalar; 1584a60b672SMatthew Knepley #endif 159b7940d39SSatish Balay #else 160b7940d39SSatish Balay #include <complex.h> 161b7940d39SSatish Balay 162b7940d39SSatish Balay /* 163b7940d39SSatish Balay C support of complex numbers: Warning it needs a 164b7940d39SSatish Balay C90 compliant compiler to work... 165b7940d39SSatish Balay */ 166b7940d39SSatish Balay 16765460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 16885b47369SMatthew Knepley typedef float complex PetscScalar; 16985b47369SMatthew Knepley 17085b47369SMatthew Knepley #define PetscRealPart(a) crealf(a) 17185b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagf(a) 17285b47369SMatthew Knepley #define PetscAbsScalar(a) cabsf(a) 17385b47369SMatthew Knepley #define PetscConj(a) conjf(a) 17485b47369SMatthew Knepley #define PetscSqrtScalar(a) csqrtf(a) 17585b47369SMatthew Knepley #define PetscPowScalar(a,b) cpowf(a,b) 17685b47369SMatthew Knepley #define PetscExpScalar(a) cexpf(a) 17706c1185fSBarry Smith #define PetscLogScalar(a) clogf(a) 17885b47369SMatthew Knepley #define PetscSinScalar(a) csinf(a) 17985b47369SMatthew Knepley #define PetscCosScalar(a) ccosf(a) 18065460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 18185b47369SMatthew Knepley typedef long double complex PetscScalar; 18285b47369SMatthew Knepley 18385b47369SMatthew Knepley #define PetscRealPart(a) creall(a) 18485b47369SMatthew Knepley #define PetscImaginaryPart(a) cimagl(a) 18585b47369SMatthew Knepley #define PetscAbsScalar(a) cabsl(a) 18685b47369SMatthew Knepley #define PetscConj(a) conjl(a) 18785b47369SMatthew Knepley #define PetscSqrtScalar(a) csqrtl(a) 18885b47369SMatthew Knepley #define PetscPowScalar(a,b) cpowl(a,b) 18985b47369SMatthew Knepley #define PetscExpScalar(a) cexpl(a) 19006c1185fSBarry Smith #define PetscLogScalar(a) clogl(a) 19185b47369SMatthew Knepley #define PetscSinScalar(a) csinl(a) 19285b47369SMatthew Knepley #define PetscCosScalar(a) ccosl(a) 19385b47369SMatthew Knepley 19485b47369SMatthew Knepley #else 19585b47369SMatthew Knepley typedef double complex PetscScalar; 19685b47369SMatthew Knepley 197b7940d39SSatish Balay #define PetscRealPart(a) creal(a) 198b7940d39SSatish Balay #define PetscImaginaryPart(a) cimag(a) 199b7940d39SSatish Balay #define PetscAbsScalar(a) cabs(a) 200b7940d39SSatish Balay #define PetscConj(a) conj(a) 201b7940d39SSatish Balay #define PetscSqrtScalar(a) csqrt(a) 202b7940d39SSatish Balay #define PetscPowScalar(a,b) cpow(a,b) 203b7940d39SSatish Balay #define PetscExpScalar(a) cexp(a) 20406c1185fSBarry Smith #define PetscLogScalar(a) clog(a) 205b7940d39SSatish Balay #define PetscSinScalar(a) csin(a) 206b7940d39SSatish Balay #define PetscCosScalar(a) ccos(a) 207b7940d39SSatish Balay #endif 2084a60b672SMatthew Knepley #endif 209e489efc1SBarry Smith 2102c876bd9SBarry Smith #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX) 2117087cfbeSBarry Smith extern MPI_Datatype MPI_C_DOUBLE_COMPLEX; 2127087cfbeSBarry Smith extern MPI_Datatype MPI_C_COMPLEX; 2132c876bd9SBarry Smith #endif 2142c876bd9SBarry Smith 215a83b8d76SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 216a83b8d76SBarry Smith #define MPIU_SCALAR MPI_C_COMPLEX 217a83b8d76SBarry Smith #else 2182c876bd9SBarry Smith #define MPIU_SCALAR MPI_C_DOUBLE_COMPLEX 219a83b8d76SBarry Smith #endif 22065460251SBarry Smith #if defined(PETSC_USE_SCALAR_MAT_SINGLE) 221762437b8SSatish Balay #define MPIU_MATSCALAR ??Notdone 222762437b8SSatish Balay #else 2232c876bd9SBarry Smith #define MPIU_MATSCALAR MPI_C_DOUBLE_COMPLEX 224762437b8SSatish Balay #endif 225762437b8SSatish Balay 22675567043SBarry Smith 227e489efc1SBarry Smith /* Compiling for real numbers only */ 228e489efc1SBarry Smith #else 22965460251SBarry Smith # if defined(PETSC_USE_SCALAR_SINGLE) 23087828ca2SBarry Smith # define MPIU_SCALAR MPI_FLOAT 23165460251SBarry Smith # elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 232f68b968cSBarry Smith # define MPIU_SCALAR MPI_LONG_DOUBLE 23375567043SBarry Smith # elif defined(PETSC_USE_SCALAR_INT) 23475567043SBarry Smith # define MPIU_SCALAR MPI_INT 23587828ca2SBarry Smith # else 236e489efc1SBarry Smith # define MPIU_SCALAR MPI_DOUBLE 23787828ca2SBarry Smith # endif 23865460251SBarry Smith # if defined(PETSC_USE_SCALAR_MAT_SINGLE) || defined(PETSC_USE_SCALAR_SINGLE) 2393eda8832SBarry Smith # define MPIU_MATSCALAR MPI_FLOAT 24065460251SBarry Smith # elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 241f68b968cSBarry Smith # define MPIU_MATSCALAR MPI_LONG_DOUBLE 24265460251SBarry Smith # elif defined(PETSC_USE_SCALAR_INT) 24303c60df9SBarry Smith # define MPIU_MATSCALAR MPI_INT 2443eda8832SBarry Smith # else 2453eda8832SBarry Smith # define MPIU_MATSCALAR MPI_DOUBLE 2463eda8832SBarry Smith # endif 247329f5518SBarry Smith # define PetscRealPart(a) (a) 24875567043SBarry Smith # define PetscImaginaryPart(a) (0.) 249e489efc1SBarry Smith # define PetscAbsScalar(a) (((a)<0.0) ? -(a) : (a)) 250e489efc1SBarry Smith # define PetscConj(a) (a) 25118a7d68fSSatish Balay # define PetscSqrtScalar(a) sqrt(a) 252184914b5SBarry Smith # define PetscPowScalar(a,b) pow(a,b) 253184914b5SBarry Smith # define PetscExpScalar(a) exp(a) 25406c1185fSBarry Smith # define PetscLogScalar(a) log(a) 255184914b5SBarry Smith # define PetscSinScalar(a) sin(a) 256184914b5SBarry Smith # define PetscCosScalar(a) cos(a) 257b0a32e0cSBarry Smith 25865460251SBarry Smith # if defined(PETSC_USE_SCALAR_SINGLE) 259ea709b57SSatish Balay typedef float PetscScalar; 26065460251SBarry Smith # elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 261f68b968cSBarry Smith typedef long double PetscScalar; 26265460251SBarry Smith # elif defined(PETSC_USE_SCALAR_INT) 26303c60df9SBarry Smith typedef int PetscScalar; 264b0a32e0cSBarry Smith # else 265ea709b57SSatish Balay typedef double PetscScalar; 266b0a32e0cSBarry Smith # endif 267e489efc1SBarry Smith #endif 268e489efc1SBarry Smith 26965460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 270d7d1e502SBarry Smith # define MPIU_REAL MPI_FLOAT 27165460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 272f68b968cSBarry Smith # define MPIU_REAL MPI_LONG_DOUBLE 27365460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT) 27403c60df9SBarry Smith # define MPIU_REAL MPI_INT 275d7d1e502SBarry Smith #else 276d7d1e502SBarry Smith # define MPIU_REAL MPI_DOUBLE 277d7d1e502SBarry Smith #endif 278d7d1e502SBarry Smith 279da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1) 28026aa1773SMatthew Knepley #define PetscAbs(a) (((a) >= 0) ? (a) : -(a)) 2813f1db9ecSBarry Smith /* 2823f1db9ecSBarry Smith Allows compiling PETSc so that matrix values are stored in 2833f1db9ecSBarry Smith single precision but all other objects still use double 2843f1db9ecSBarry Smith precision. This does not work for complex numbers in that case 2853f1db9ecSBarry Smith it remains double 2863f1db9ecSBarry Smith 2873f1db9ecSBarry Smith EXPERIMENTAL! NOT YET COMPLETELY WORKING 2883f1db9ecSBarry Smith */ 2893f1db9ecSBarry Smith 29065460251SBarry Smith #if defined(PETSC_USE_SCALAR_MAT_SINGLE) 291b400db4cSSatish Balay typedef float MatScalar; 2923f1db9ecSBarry Smith #else 293ea709b57SSatish Balay typedef PetscScalar MatScalar; 29411380375SSatish Balay #endif 2953f1db9ecSBarry Smith 29665460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 297b400db4cSSatish Balay typedef float PetscReal; 29865460251SBarry Smith #elif defined(PETSC_USE_SCALAR_LONG_DOUBLE) 299f68b968cSBarry Smith typedef long double PetscReal; 30065460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT) 30103c60df9SBarry Smith typedef int PetscReal; 302329f5518SBarry Smith #else 303b400db4cSSatish Balay typedef double PetscReal; 304329f5518SBarry Smith #endif 3053f1db9ecSBarry Smith 306f68b968cSBarry Smith #if defined(PETSC_USE_COMPLEX) 307f68b968cSBarry Smith typedef PetscReal MatReal; 30865460251SBarry Smith #elif defined(PETSC_USE_SCALAR_MAT_SINGLE) || defined(PETSC_USE_SCALAR_SINGLE) 309f68b968cSBarry Smith typedef float MatReal; 310f68b968cSBarry Smith #else 311f68b968cSBarry Smith typedef PetscReal MatReal; 312f68b968cSBarry Smith #endif 313f68b968cSBarry Smith 314f68b968cSBarry Smith 315314da920SBarry Smith /* --------------------------------------------------------------------------*/ 316314da920SBarry Smith 317e489efc1SBarry Smith /* 318f22f69f0SBarry Smith Certain objects may be created using either single or double precision. 319f22f69f0SBarry Smith This is currently not used. 320e489efc1SBarry Smith */ 321*557d4da8SBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision; 322e489efc1SBarry Smith 323e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */ 3247087cfbeSBarry Smith extern PetscScalar PETSC_i; 325e489efc1SBarry Smith 326b6a5bde7SBarry Smith /*MC 327b6a5bde7SBarry Smith PetscMin - Returns minimum of two numbers 328b6a5bde7SBarry Smith 329eca87e8dSBarry Smith Synopsis: 330eca87e8dSBarry Smith type PetscMin(type v1,type v2) 331eca87e8dSBarry Smith 332eca87e8dSBarry Smith Not Collective 333eca87e8dSBarry Smith 334b6a5bde7SBarry Smith Input Parameter: 335b6a5bde7SBarry Smith + v1 - first value to find minimum of 336b6a5bde7SBarry Smith - v2 - second value to find minimum of 337b6a5bde7SBarry Smith 338b6a5bde7SBarry Smith 339b6a5bde7SBarry Smith Notes: type can be integer or floating point value 340b6a5bde7SBarry Smith 341b6a5bde7SBarry Smith Level: beginner 342b6a5bde7SBarry Smith 343b6a5bde7SBarry Smith 344b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 345b6a5bde7SBarry Smith 346b6a5bde7SBarry Smith M*/ 347e489efc1SBarry Smith #define PetscMin(a,b) (((a)<(b)) ? (a) : (b)) 348b6a5bde7SBarry Smith 349b6a5bde7SBarry Smith /*MC 350b6a5bde7SBarry Smith PetscMax - Returns maxium of two numbers 351b6a5bde7SBarry Smith 352eca87e8dSBarry Smith Synopsis: 353eca87e8dSBarry Smith type max PetscMax(type v1,type v2) 354eca87e8dSBarry Smith 355eca87e8dSBarry Smith Not Collective 356eca87e8dSBarry Smith 357b6a5bde7SBarry Smith Input Parameter: 358b6a5bde7SBarry Smith + v1 - first value to find maximum of 359b6a5bde7SBarry Smith - v2 - second value to find maximum of 360b6a5bde7SBarry Smith 361b6a5bde7SBarry Smith Notes: type can be integer or floating point value 362b6a5bde7SBarry Smith 363b6a5bde7SBarry Smith Level: beginner 364b6a5bde7SBarry Smith 365b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 366b6a5bde7SBarry Smith 367b6a5bde7SBarry Smith M*/ 368e489efc1SBarry Smith #define PetscMax(a,b) (((a)<(b)) ? (b) : (a)) 369b6a5bde7SBarry Smith 370b6a5bde7SBarry Smith /*MC 371b6a5bde7SBarry Smith PetscAbsInt - Returns the absolute value of an integer 372b6a5bde7SBarry Smith 373b6a5bde7SBarry Smith Synopsis: 374b6a5bde7SBarry Smith int abs PetscAbsInt(int v1) 375b6a5bde7SBarry Smith 376eca87e8dSBarry Smith Not Collective 377eca87e8dSBarry Smith 378eca87e8dSBarry Smith Input Parameter: 379eca87e8dSBarry Smith . v1 - the integer 380b6a5bde7SBarry Smith 381b6a5bde7SBarry Smith Level: beginner 382b6a5bde7SBarry Smith 383b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr() 384b6a5bde7SBarry Smith 385b6a5bde7SBarry Smith M*/ 386e489efc1SBarry Smith #define PetscAbsInt(a) (((a)<0) ? -(a) : (a)) 387b6a5bde7SBarry Smith 388b6a5bde7SBarry Smith /*MC 389b6a5bde7SBarry Smith PetscAbsReal - Returns the absolute value of an real number 390b6a5bde7SBarry Smith 391eca87e8dSBarry Smith Synopsis: 392eca87e8dSBarry Smith Real abs PetscAbsReal(PetscReal v1) 393eca87e8dSBarry Smith 394eca87e8dSBarry Smith Not Collective 395eca87e8dSBarry Smith 396b6a5bde7SBarry Smith Input Parameter: 397b6a5bde7SBarry Smith . v1 - the double 398b6a5bde7SBarry Smith 399b6a5bde7SBarry Smith 400b6a5bde7SBarry Smith Level: beginner 401b6a5bde7SBarry Smith 402b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr() 403b6a5bde7SBarry Smith 404b6a5bde7SBarry Smith M*/ 405f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0) ? -(a) : (a)) 406b6a5bde7SBarry Smith 407b6a5bde7SBarry Smith /*MC 408b6a5bde7SBarry Smith PetscSqr - Returns the square of a number 409b6a5bde7SBarry Smith 410b6a5bde7SBarry Smith Synopsis: 411b6a5bde7SBarry Smith type sqr PetscSqr(type v1) 412b6a5bde7SBarry Smith 413eca87e8dSBarry Smith Not Collective 414eca87e8dSBarry Smith 415eca87e8dSBarry Smith Input Parameter: 416eca87e8dSBarry Smith . v1 - the value 417eca87e8dSBarry Smith 418b6a5bde7SBarry Smith Notes: type can be integer or floating point value 419b6a5bde7SBarry Smith 420b6a5bde7SBarry Smith Level: beginner 421b6a5bde7SBarry Smith 422b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal() 423b6a5bde7SBarry Smith 424b6a5bde7SBarry Smith M*/ 4254ebda54eSMatthew Knepley #define PetscSqr(a) ((a)*(a)) 426e489efc1SBarry Smith 427314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 428314da920SBarry Smith /* 42903c60df9SBarry Smith Basic constants - These should be done much better 430314da920SBarry Smith */ 431314da920SBarry Smith #define PETSC_PI 3.14159265358979323846264 432314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994 43371fd2e92SBarry Smith #define PETSC_MAX_INT 2147483647 43471fd2e92SBarry Smith #define PETSC_MIN_INT -2147483647 435e489efc1SBarry Smith 43665460251SBarry Smith #if defined(PETSC_USE_SCALAR_SINGLE) 4377e032f8bSBarry Smith # define PETSC_MAX 1.e30 4387e032f8bSBarry Smith # define PETSC_MIN -1.e30 439f10639e6SSatish Balay # define PETSC_MACHINE_EPSILON 1.e-7 440f10639e6SSatish Balay # define PETSC_SQRT_MACHINE_EPSILON 3.e-4 441cf6e855fSSatish Balay # define PETSC_SMALL 1.e-5 44265460251SBarry Smith #elif defined(PETSC_USE_SCALAR_INT) 44303c60df9SBarry Smith # define PETSC_MAX PETSC_MAX_INT 44403c60df9SBarry Smith # define PETSC_MIN PETSC_MIN_INT 44503c60df9SBarry Smith # define PETSC_MACHINE_EPSILON 1 44603c60df9SBarry Smith # define PETSC_SQRT_MACHINE_EPSILON 1 44703c60df9SBarry Smith # define PETSC_SMALL 0 44882adfdadSBarry Smith #else 4497e032f8bSBarry Smith # define PETSC_MAX 1.e300 4507e032f8bSBarry Smith # define PETSC_MIN -1.e300 451f10639e6SSatish Balay # define PETSC_MACHINE_EPSILON 1.e-14 452f10639e6SSatish Balay # define PETSC_SQRT_MACHINE_EPSILON 1.e-7 453cf6e855fSSatish Balay # define PETSC_SMALL 1.e-10 45482adfdadSBarry Smith #endif 45582adfdadSBarry Smith 4569cf09972SJed Brown #if defined PETSC_HAVE_ADIC 4579cf09972SJed Brown /* Use MPI_Allreduce when ADIC is not available. */ 4587087cfbeSBarry Smith extern PetscErrorCode PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*); 4597087cfbeSBarry Smith extern PetscErrorCode PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*); 4607087cfbeSBarry Smith extern PetscErrorCode PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*); 4619cf09972SJed Brown #endif 4623e523bebSBarry Smith 4630763cb5fSBarry Smith /*MC 4640763cb5fSBarry Smith PetscIsInfOrNan - Returns 1 if the input double has an infinity for Not-a-number (Nan) value, otherwise 0. 4653e523bebSBarry Smith 4660763cb5fSBarry Smith Input Parameter: 4670763cb5fSBarry Smith . a - the double 4680763cb5fSBarry Smith 4690763cb5fSBarry Smith 4700763cb5fSBarry Smith Notes: uses the C99 standard isinf() and isnan() on systems where they exist. 47183886165SBarry Smith Otherwises uses ( (a - a) != 0.0), note that some optimizing compiles compile 4720763cb5fSBarry Smith out this form, thus removing the check. 4730763cb5fSBarry Smith 47483672c4dSSatish Balay Level: beginner 47583672c4dSSatish Balay 47683672c4dSSatish Balay M*/ 4779a25a3ccSBarry Smith #if defined(PETSC_HAVE_ISINF) && defined(PETSC_HAVE_ISNAN) 478f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) (isinf(PetscAbsScalar(a)) || isnan(PetscAbsScalar(a))) 479f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) (isinf(a) || isnan(a)) 48062b4c0b3SBarry Smith #elif defined(PETSC_HAVE__FINITE) && defined(PETSC_HAVE__ISNAN) 481270b8587SSatish Balay #if defined(PETSC_HAVE_FLOAT_H) 482270b8587SSatish Balay #include "float.h" /* windows defines _finite() in float.h */ 483270b8587SSatish Balay #endif 484961faeafSBarry Smith #if defined(PETSC_HAVE_IEEEFP_H) 485961faeafSBarry Smith #include "ieeefp.h" /* Solaris prototypes these here */ 486961faeafSBarry Smith #endif 487f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) (!_finite(PetscAbsScalar(a)) || _isnan(PetscAbsScalar(a))) 488f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) (!_finite(a) || _isnan(a)) 4899a25a3ccSBarry Smith #else 490f66fdb6dSSatish Balay #define PetscIsInfOrNanScalar(a) ((a - a) != 0.0) 491f66fdb6dSSatish Balay #define PetscIsInfOrNanReal(a) ((a - a) != 0.0) 4929a25a3ccSBarry Smith #endif 4939a25a3ccSBarry Smith 4949a25a3ccSBarry Smith 495314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 496e489efc1SBarry Smith /* 497b0a32e0cSBarry Smith PetscLogDouble variables are used to contain double precision numbers 498e489efc1SBarry Smith that are not used in the numerical computations, but rather in logging, 499e489efc1SBarry Smith timing etc. 500e489efc1SBarry Smith */ 501b0a32e0cSBarry Smith typedef double PetscLogDouble; 502b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE 503e489efc1SBarry Smith 50487828ca2SBarry Smith #define PassiveReal PetscReal 505ea709b57SSatish Balay #define PassiveScalar PetscScalar 506d3ecb3a7SBarry Smith 507e9fa29b7SSatish Balay 508e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END 509e489efc1SBarry Smith #endif 510