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 7e7029fe1SSatish Balay This file is included by petsc.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 16ff73aad6SKris Buschelman extern MPI_Datatype PETSC_DLLEXPORT MPIU_2SCALAR; 17ff73aad6SKris Buschelman extern MPI_Datatype PETSC_DLLEXPORT 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 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) 43184914b5SBarry Smith #define PetscSinScalar(a) std::sin(a) 44184914b5SBarry Smith #define PetscCosScalar(a) std::cos(a) 450bfd3fbfSBarry Smith 46ea709b57SSatish Balay typedef std::complex<double> PetscScalar; 47b7940d39SSatish Balay #else 48b7940d39SSatish Balay #include <complex.h> 49b7940d39SSatish Balay 50b7940d39SSatish Balay /* 51b7940d39SSatish Balay C support of complex numbers: Warning it needs a 52b7940d39SSatish Balay C90 compliant compiler to work... 53b7940d39SSatish Balay */ 54b7940d39SSatish Balay 55b7940d39SSatish Balay #define PetscRealPart(a) creal(a) 56b7940d39SSatish Balay #define PetscImaginaryPart(a) cimag(a) 57b7940d39SSatish Balay #define PetscAbsScalar(a) cabs(a) 58b7940d39SSatish Balay #define PetscConj(a) conj(a) 59b7940d39SSatish Balay #define PetscSqrtScalar(a) csqrt(a) 60b7940d39SSatish Balay #define PetscPowScalar(a,b) cpow(a,b) 61b7940d39SSatish Balay #define PetscExpScalar(a) cexp(a) 62b7940d39SSatish Balay #define PetscSinScalar(a) csin(a) 63b7940d39SSatish Balay #define PetscCosScalar(a) ccos(a) 64b7940d39SSatish Balay 65b7940d39SSatish Balay typedef double complex PetscScalar; 66b7940d39SSatish Balay #endif 67e489efc1SBarry Smith 68*762437b8SSatish Balay extern MPI_Datatype PETSC_DLLEXPORT MPIU_COMPLEX; 69*762437b8SSatish Balay #define MPIU_SCALAR MPIU_COMPLEX 70*762437b8SSatish Balay #if defined(PETSC_USE_MAT_SINGLE) 71*762437b8SSatish Balay #define MPIU_MATSCALAR ??Notdone 72*762437b8SSatish Balay #else 73*762437b8SSatish Balay #define MPIU_MATSCALAR MPIU_COMPLEX 74*762437b8SSatish Balay #endif 75*762437b8SSatish Balay 76e489efc1SBarry Smith /* Compiling for real numbers only */ 77e489efc1SBarry Smith #else 7887828ca2SBarry Smith # if defined(PETSC_USE_SINGLE) 7987828ca2SBarry Smith # define MPIU_SCALAR MPI_FLOAT 80f68b968cSBarry Smith # elif defined(PETSC_USE_LONG_DOUBLE) 81f68b968cSBarry Smith # define MPIU_SCALAR MPI_LONG_DOUBLE 8203c60df9SBarry Smith # elif defined(PETSC_INT) 8303c60df9SBarry Smith # define MPIU_INT MPI_INT 8487828ca2SBarry Smith # else 85e489efc1SBarry Smith # define MPIU_SCALAR MPI_DOUBLE 8687828ca2SBarry Smith # endif 8787828ca2SBarry Smith # if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE) 883eda8832SBarry Smith # define MPIU_MATSCALAR MPI_FLOAT 89f68b968cSBarry Smith # elif defined(PETSC_USE_LONG_DOUBLE) 90f68b968cSBarry Smith # define MPIU_MATSCALAR MPI_LONG_DOUBLE 9103c60df9SBarry Smith # elif defined(PETSC_USE_INT) 9203c60df9SBarry Smith # define MPIU_MATSCALAR MPI_INT 933eda8832SBarry Smith # else 943eda8832SBarry Smith # define MPIU_MATSCALAR MPI_DOUBLE 953eda8832SBarry Smith # endif 96329f5518SBarry Smith # define PetscRealPart(a) (a) 979b0def1dSBarry Smith # define PetscImaginaryPart(a) (0) 98e489efc1SBarry Smith # define PetscAbsScalar(a) (((a)<0.0) ? -(a) : (a)) 99e489efc1SBarry Smith # define PetscConj(a) (a) 10018a7d68fSSatish Balay # define PetscSqrtScalar(a) sqrt(a) 101184914b5SBarry Smith # define PetscPowScalar(a,b) pow(a,b) 102184914b5SBarry Smith # define PetscExpScalar(a) exp(a) 103184914b5SBarry Smith # define PetscSinScalar(a) sin(a) 104184914b5SBarry Smith # define PetscCosScalar(a) cos(a) 105b0a32e0cSBarry Smith 106b0a32e0cSBarry Smith # if defined(PETSC_USE_SINGLE) 107ea709b57SSatish Balay typedef float PetscScalar; 108f68b968cSBarry Smith # elif defined(PETSC_USE_LONG_DOUBLE) 109f68b968cSBarry Smith typedef long double PetscScalar; 11003c60df9SBarry Smith # elif defined(PETSC_USE_INT) 11103c60df9SBarry Smith typedef int PetscScalar; 112b0a32e0cSBarry Smith # else 113ea709b57SSatish Balay typedef double PetscScalar; 114b0a32e0cSBarry Smith # endif 115e489efc1SBarry Smith #endif 116e489efc1SBarry Smith 117d7d1e502SBarry Smith #if defined(PETSC_USE_SINGLE) 118d7d1e502SBarry Smith # define MPIU_REAL MPI_FLOAT 119f68b968cSBarry Smith #elif defined(PETSC_USE_LONG_DOUBLE) 120f68b968cSBarry Smith # define MPIU_REAL MPI_LONG_DOUBLE 12103c60df9SBarry Smith #elif defined(PETSC_USE_INT) 12203c60df9SBarry Smith # define MPIU_REAL MPI_INT 123d7d1e502SBarry Smith #else 124d7d1e502SBarry Smith # define MPIU_REAL MPI_DOUBLE 125d7d1e502SBarry Smith #endif 126d7d1e502SBarry Smith 127da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1) 12826aa1773SMatthew Knepley #define PetscAbs(a) (((a) >= 0) ? (a) : -(a)) 1293f1db9ecSBarry Smith /* 1303f1db9ecSBarry Smith Allows compiling PETSc so that matrix values are stored in 1313f1db9ecSBarry Smith single precision but all other objects still use double 1323f1db9ecSBarry Smith precision. This does not work for complex numbers in that case 1333f1db9ecSBarry Smith it remains double 1343f1db9ecSBarry Smith 1353f1db9ecSBarry Smith EXPERIMENTAL! NOT YET COMPLETELY WORKING 1363f1db9ecSBarry Smith */ 1373f1db9ecSBarry Smith 13811380375SSatish Balay #if defined(PETSC_USE_MAT_SINGLE) 139b400db4cSSatish Balay typedef float MatScalar; 1403f1db9ecSBarry Smith #else 141ea709b57SSatish Balay typedef PetscScalar MatScalar; 14211380375SSatish Balay #endif 1433f1db9ecSBarry Smith 144329f5518SBarry Smith #if defined(PETSC_USE_SINGLE) 145b400db4cSSatish Balay typedef float PetscReal; 146f68b968cSBarry Smith #elif defined(PETSC_USE_LONG_DOUBLE) 147f68b968cSBarry Smith typedef long double PetscReal; 14803c60df9SBarry Smith #elif defined(PETSC_USE_INT) 14903c60df9SBarry Smith typedef int PetscReal; 150329f5518SBarry Smith #else 151b400db4cSSatish Balay typedef double PetscReal; 152329f5518SBarry Smith #endif 1533f1db9ecSBarry Smith 154f68b968cSBarry Smith #if defined(PETSC_USE_COMPLEX) 155f68b968cSBarry Smith typedef PetscReal MatReal; 156f68b968cSBarry Smith #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE) 157f68b968cSBarry Smith typedef float MatReal; 158f68b968cSBarry Smith #else 159f68b968cSBarry Smith typedef PetscReal MatReal; 160f68b968cSBarry Smith #endif 161f68b968cSBarry Smith 162f68b968cSBarry Smith 163314da920SBarry Smith /* --------------------------------------------------------------------------*/ 164314da920SBarry Smith 165e489efc1SBarry Smith /* 166e489efc1SBarry Smith Certain objects may be created using either single 167e489efc1SBarry Smith or double precision. 168e489efc1SBarry Smith */ 169f68b968cSBarry 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 */ 172ff73aad6SKris Buschelman extern PetscScalar PETSC_DLLEXPORT PETSC_i; 173e489efc1SBarry Smith 174b6a5bde7SBarry Smith /*MC 175b6a5bde7SBarry Smith PetscMin - Returns minimum of two numbers 176b6a5bde7SBarry Smith 177b6a5bde7SBarry Smith Input Parameter: 178b6a5bde7SBarry Smith + v1 - first value to find minimum of 179b6a5bde7SBarry Smith - v2 - second value to find minimum of 180b6a5bde7SBarry Smith 181b6a5bde7SBarry Smith Synopsis: 182b6a5bde7SBarry Smith type PetscMin(type v1,type v2) 183b6a5bde7SBarry Smith 184b6a5bde7SBarry Smith Notes: type can be integer or floating point value 185b6a5bde7SBarry Smith 186b6a5bde7SBarry Smith Level: beginner 187b6a5bde7SBarry Smith 188b6a5bde7SBarry Smith 189b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 190b6a5bde7SBarry Smith 191b6a5bde7SBarry Smith M*/ 192e489efc1SBarry Smith #define PetscMin(a,b) (((a)<(b)) ? (a) : (b)) 193b6a5bde7SBarry Smith 194b6a5bde7SBarry Smith /*MC 195b6a5bde7SBarry Smith PetscMax - Returns maxium of two numbers 196b6a5bde7SBarry Smith 197b6a5bde7SBarry Smith Input Parameter: 198b6a5bde7SBarry Smith + v1 - first value to find maximum of 199b6a5bde7SBarry Smith - v2 - second value to find maximum of 200b6a5bde7SBarry Smith 201b6a5bde7SBarry Smith Synopsis: 202b6a5bde7SBarry Smith type max PetscMax(type v1,type v2) 203b6a5bde7SBarry Smith 204b6a5bde7SBarry Smith Notes: type can be integer or floating point value 205b6a5bde7SBarry Smith 206b6a5bde7SBarry Smith Level: beginner 207b6a5bde7SBarry Smith 208b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 209b6a5bde7SBarry Smith 210b6a5bde7SBarry Smith M*/ 211e489efc1SBarry Smith #define PetscMax(a,b) (((a)<(b)) ? (b) : (a)) 212b6a5bde7SBarry Smith 213b6a5bde7SBarry Smith /*MC 214b6a5bde7SBarry Smith PetscAbsInt - Returns the absolute value of an integer 215b6a5bde7SBarry Smith 216b6a5bde7SBarry Smith Input Parameter: 217b6a5bde7SBarry Smith . v1 - the integer 218b6a5bde7SBarry Smith 219b6a5bde7SBarry Smith Synopsis: 220b6a5bde7SBarry Smith int abs PetscAbsInt(int v1) 221b6a5bde7SBarry Smith 222b6a5bde7SBarry Smith 223b6a5bde7SBarry Smith Level: beginner 224b6a5bde7SBarry Smith 225b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr() 226b6a5bde7SBarry Smith 227b6a5bde7SBarry Smith M*/ 228e489efc1SBarry Smith #define PetscAbsInt(a) (((a)<0) ? -(a) : (a)) 229b6a5bde7SBarry Smith 230b6a5bde7SBarry Smith /*MC 231b6a5bde7SBarry Smith PetscAbsReal - Returns the absolute value of an real number 232b6a5bde7SBarry Smith 233b6a5bde7SBarry Smith Input Parameter: 234b6a5bde7SBarry Smith . v1 - the double 235b6a5bde7SBarry Smith 236b6a5bde7SBarry Smith Synopsis: 237b6a5bde7SBarry Smith int abs PetscAbsReal(PetscReal v1) 238b6a5bde7SBarry Smith 239b6a5bde7SBarry Smith 240b6a5bde7SBarry Smith Level: beginner 241b6a5bde7SBarry Smith 242b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr() 243b6a5bde7SBarry Smith 244b6a5bde7SBarry Smith M*/ 245f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0) ? -(a) : (a)) 246b6a5bde7SBarry Smith 247b6a5bde7SBarry Smith /*MC 248b6a5bde7SBarry Smith PetscSqr - Returns the square of a number 249b6a5bde7SBarry Smith 250b6a5bde7SBarry Smith Input Parameter: 251b6a5bde7SBarry Smith . v1 - the value 252b6a5bde7SBarry Smith 253b6a5bde7SBarry Smith Synopsis: 254b6a5bde7SBarry Smith type sqr PetscSqr(type v1) 255b6a5bde7SBarry Smith 256b6a5bde7SBarry Smith Notes: type can be integer or floating point value 257b6a5bde7SBarry Smith 258b6a5bde7SBarry Smith Level: beginner 259b6a5bde7SBarry Smith 260b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal() 261b6a5bde7SBarry Smith 262b6a5bde7SBarry Smith M*/ 2634ebda54eSMatthew Knepley #define PetscSqr(a) ((a)*(a)) 264e489efc1SBarry Smith 265314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 266314da920SBarry Smith /* 26703c60df9SBarry Smith Basic constants - These should be done much better 268314da920SBarry Smith */ 269314da920SBarry Smith #define PETSC_PI 3.14159265358979323846264 270314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994 271f10639e6SSatish Balay #define PETSC_MAX_INT 1000000000 272f10639e6SSatish Balay #define PETSC_MIN_INT -1000000000 273e489efc1SBarry Smith 27482adfdadSBarry Smith #if defined(PETSC_USE_SINGLE) 2757e032f8bSBarry Smith # define PETSC_MAX 1.e30 2767e032f8bSBarry Smith # define PETSC_MIN -1.e30 277f10639e6SSatish Balay # define PETSC_MACHINE_EPSILON 1.e-7 278f10639e6SSatish Balay # define PETSC_SQRT_MACHINE_EPSILON 3.e-4 279cf6e855fSSatish Balay # define PETSC_SMALL 1.e-5 28003c60df9SBarry Smith #elif defined(PETSC_USE_INT) 28103c60df9SBarry Smith # define PETSC_MAX PETSC_MAX_INT 28203c60df9SBarry Smith # define PETSC_MIN PETSC_MIN_INT 28303c60df9SBarry Smith # define PETSC_MACHINE_EPSILON 1 28403c60df9SBarry Smith # define PETSC_SQRT_MACHINE_EPSILON 1 28503c60df9SBarry Smith # define PETSC_SMALL 0 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 294ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm); 295ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm); 296ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm); 2973e523bebSBarry Smith 2983e523bebSBarry Smith 299314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 300e489efc1SBarry Smith /* 301b0a32e0cSBarry Smith PetscLogDouble variables are used to contain double precision numbers 302e489efc1SBarry Smith that are not used in the numerical computations, but rather in logging, 303e489efc1SBarry Smith timing etc. 304e489efc1SBarry Smith */ 305b0a32e0cSBarry Smith typedef double PetscLogDouble; 306b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE 307e489efc1SBarry Smith 30887828ca2SBarry Smith #define PassiveReal PetscReal 309ea709b57SSatish Balay #define PassiveScalar PetscScalar 310d3ecb3a7SBarry Smith 311e9fa29b7SSatish Balay 312e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END 313e489efc1SBarry Smith #endif 314