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) 30*b7940d39SSatish Balay #if defined(PETSC_CLANGUAGE_CXX) 310bfd3fbfSBarry Smith /* 32*b7940d39SSatish Balay C++ support of complex numbers: Original support 330bfd3fbfSBarry Smith */ 34df9b3741SSatish Balay #include <complex> 35adc17e78SSatish Balay 36ff73aad6SKris Buschelman extern MPI_Datatype PETSC_DLLEXPORT MPIU_COMPLEX; 37adc17e78SSatish Balay #define MPIU_SCALAR MPIU_COMPLEX 383eda8832SBarry Smith #if defined(PETSC_USE_MAT_SINGLE) 393eda8832SBarry Smith #define MPIU_MATSCALAR ??Notdone 403eda8832SBarry Smith #else 413eda8832SBarry Smith #define MPIU_MATSCALAR MPIU_COMPLEX 423eda8832SBarry Smith #endif 433eda8832SBarry Smith 44329f5518SBarry Smith #define PetscRealPart(a) (a).real() 45329f5518SBarry Smith #define PetscImaginaryPart(a) (a).imag() 463f6de6efSSatish Balay #define PetscAbsScalar(a) std::abs(a) 473f6de6efSSatish Balay #define PetscConj(a) std::conj(a) 4818a7d68fSSatish Balay #define PetscSqrtScalar(a) std::sqrt(a) 49184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b) 50184914b5SBarry Smith #define PetscExpScalar(a) std::exp(a) 51184914b5SBarry Smith #define PetscSinScalar(a) std::sin(a) 52184914b5SBarry Smith #define PetscCosScalar(a) std::cos(a) 530bfd3fbfSBarry Smith 54ea709b57SSatish Balay typedef std::complex<double> PetscScalar; 55*b7940d39SSatish Balay #else 56*b7940d39SSatish Balay #include <complex.h> 57*b7940d39SSatish Balay 58*b7940d39SSatish Balay /* 59*b7940d39SSatish Balay C support of complex numbers: Warning it needs a 60*b7940d39SSatish Balay C90 compliant compiler to work... 61*b7940d39SSatish Balay */ 62*b7940d39SSatish Balay 63*b7940d39SSatish Balay extern MPI_Datatype PETSC_DLLEXPORT MPIU_COMPLEX; 64*b7940d39SSatish Balay #define MPIU_SCALAR MPIU_COMPLEX 65*b7940d39SSatish Balay #if defined(PETSC_USE_MAT_SINGLE) 66*b7940d39SSatish Balay #define MPIU_MATSCALAR ??Notdone 67*b7940d39SSatish Balay #else 68*b7940d39SSatish Balay #define MPIU_MATSCALAR MPIU_COMPLEX 69*b7940d39SSatish Balay #endif 70*b7940d39SSatish Balay 71*b7940d39SSatish Balay #define PetscRealPart(a) creal(a) 72*b7940d39SSatish Balay #define PetscImaginaryPart(a) cimag(a) 73*b7940d39SSatish Balay #define PetscAbsScalar(a) cabs(a) 74*b7940d39SSatish Balay #define PetscConj(a) conj(a) 75*b7940d39SSatish Balay #define PetscSqrtScalar(a) csqrt(a) 76*b7940d39SSatish Balay #define PetscPowScalar(a,b) cpow(a,b) 77*b7940d39SSatish Balay #define PetscExpScalar(a) cexp(a) 78*b7940d39SSatish Balay #define PetscSinScalar(a) csin(a) 79*b7940d39SSatish Balay #define PetscCosScalar(a) ccos(a) 80*b7940d39SSatish Balay 81*b7940d39SSatish Balay typedef double complex PetscScalar; 82*b7940d39SSatish Balay #endif 83e489efc1SBarry Smith 84e489efc1SBarry Smith /* Compiling for real numbers only */ 85e489efc1SBarry Smith #else 8687828ca2SBarry Smith # if defined(PETSC_USE_SINGLE) 8787828ca2SBarry Smith # define MPIU_SCALAR MPI_FLOAT 88f68b968cSBarry Smith # elif defined(PETSC_USE_LONG_DOUBLE) 89f68b968cSBarry Smith # define MPIU_SCALAR MPI_LONG_DOUBLE 9003c60df9SBarry Smith # elif defined(PETSC_INT) 9103c60df9SBarry Smith # define MPIU_INT MPI_INT 9287828ca2SBarry Smith # else 93e489efc1SBarry Smith # define MPIU_SCALAR MPI_DOUBLE 9487828ca2SBarry Smith # endif 9587828ca2SBarry Smith # if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE) 963eda8832SBarry Smith # define MPIU_MATSCALAR MPI_FLOAT 97f68b968cSBarry Smith # elif defined(PETSC_USE_LONG_DOUBLE) 98f68b968cSBarry Smith # define MPIU_MATSCALAR MPI_LONG_DOUBLE 9903c60df9SBarry Smith # elif defined(PETSC_USE_INT) 10003c60df9SBarry Smith # define MPIU_MATSCALAR MPI_INT 1013eda8832SBarry Smith # else 1023eda8832SBarry Smith # define MPIU_MATSCALAR MPI_DOUBLE 1033eda8832SBarry Smith # endif 104329f5518SBarry Smith # define PetscRealPart(a) (a) 1059b0def1dSBarry Smith # define PetscImaginaryPart(a) (0) 106e489efc1SBarry Smith # define PetscAbsScalar(a) (((a)<0.0) ? -(a) : (a)) 107e489efc1SBarry Smith # define PetscConj(a) (a) 10818a7d68fSSatish Balay # define PetscSqrtScalar(a) sqrt(a) 109184914b5SBarry Smith # define PetscPowScalar(a,b) pow(a,b) 110184914b5SBarry Smith # define PetscExpScalar(a) exp(a) 111184914b5SBarry Smith # define PetscSinScalar(a) sin(a) 112184914b5SBarry Smith # define PetscCosScalar(a) cos(a) 113b0a32e0cSBarry Smith 114b0a32e0cSBarry Smith # if defined(PETSC_USE_SINGLE) 115ea709b57SSatish Balay typedef float PetscScalar; 116f68b968cSBarry Smith # elif defined(PETSC_USE_LONG_DOUBLE) 117f68b968cSBarry Smith typedef long double PetscScalar; 11803c60df9SBarry Smith # elif defined(PETSC_USE_INT) 11903c60df9SBarry Smith typedef int PetscScalar; 120b0a32e0cSBarry Smith # else 121ea709b57SSatish Balay typedef double PetscScalar; 122b0a32e0cSBarry Smith # endif 123e489efc1SBarry Smith #endif 124e489efc1SBarry Smith 125d7d1e502SBarry Smith #if defined(PETSC_USE_SINGLE) 126d7d1e502SBarry Smith # define MPIU_REAL MPI_FLOAT 127f68b968cSBarry Smith #elif defined(PETSC_USE_LONG_DOUBLE) 128f68b968cSBarry Smith # define MPIU_REAL MPI_LONG_DOUBLE 12903c60df9SBarry Smith #elif defined(PETSC_USE_INT) 13003c60df9SBarry Smith # define MPIU_REAL MPI_INT 131d7d1e502SBarry Smith #else 132d7d1e502SBarry Smith # define MPIU_REAL MPI_DOUBLE 133d7d1e502SBarry Smith #endif 134d7d1e502SBarry Smith 135da9b6338SBarry Smith #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1) 13626aa1773SMatthew Knepley #define PetscAbs(a) (((a) >= 0) ? (a) : -(a)) 1373f1db9ecSBarry Smith /* 1383f1db9ecSBarry Smith Allows compiling PETSc so that matrix values are stored in 1393f1db9ecSBarry Smith single precision but all other objects still use double 1403f1db9ecSBarry Smith precision. This does not work for complex numbers in that case 1413f1db9ecSBarry Smith it remains double 1423f1db9ecSBarry Smith 1433f1db9ecSBarry Smith EXPERIMENTAL! NOT YET COMPLETELY WORKING 1443f1db9ecSBarry Smith */ 1453f1db9ecSBarry Smith 14611380375SSatish Balay #if defined(PETSC_USE_MAT_SINGLE) 147b400db4cSSatish Balay typedef float MatScalar; 1483f1db9ecSBarry Smith #else 149ea709b57SSatish Balay typedef PetscScalar MatScalar; 15011380375SSatish Balay #endif 1513f1db9ecSBarry Smith 152329f5518SBarry Smith #if defined(PETSC_USE_SINGLE) 153b400db4cSSatish Balay typedef float PetscReal; 154f68b968cSBarry Smith #elif defined(PETSC_USE_LONG_DOUBLE) 155f68b968cSBarry Smith typedef long double PetscReal; 15603c60df9SBarry Smith #elif defined(PETSC_USE_INT) 15703c60df9SBarry Smith typedef int PetscReal; 158329f5518SBarry Smith #else 159b400db4cSSatish Balay typedef double PetscReal; 160329f5518SBarry Smith #endif 1613f1db9ecSBarry Smith 162f68b968cSBarry Smith #if defined(PETSC_USE_COMPLEX) 163f68b968cSBarry Smith typedef PetscReal MatReal; 164f68b968cSBarry Smith #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE) 165f68b968cSBarry Smith typedef float MatReal; 166f68b968cSBarry Smith #else 167f68b968cSBarry Smith typedef PetscReal MatReal; 168f68b968cSBarry Smith #endif 169f68b968cSBarry Smith 170f68b968cSBarry Smith 171314da920SBarry Smith /* --------------------------------------------------------------------------*/ 172314da920SBarry Smith 173e489efc1SBarry Smith /* 174e489efc1SBarry Smith Certain objects may be created using either single 175e489efc1SBarry Smith or double precision. 176e489efc1SBarry Smith */ 177f68b968cSBarry Smith typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision; 178e489efc1SBarry Smith 179e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */ 180ff73aad6SKris Buschelman extern PetscScalar PETSC_DLLEXPORT PETSC_i; 181e489efc1SBarry Smith 182b6a5bde7SBarry Smith /*MC 183b6a5bde7SBarry Smith PetscMin - Returns minimum of two numbers 184b6a5bde7SBarry Smith 185b6a5bde7SBarry Smith Input Parameter: 186b6a5bde7SBarry Smith + v1 - first value to find minimum of 187b6a5bde7SBarry Smith - v2 - second value to find minimum of 188b6a5bde7SBarry Smith 189b6a5bde7SBarry Smith Synopsis: 190b6a5bde7SBarry Smith type PetscMin(type v1,type v2) 191b6a5bde7SBarry Smith 192b6a5bde7SBarry Smith Notes: type can be integer or floating point value 193b6a5bde7SBarry Smith 194b6a5bde7SBarry Smith Level: beginner 195b6a5bde7SBarry Smith 196b6a5bde7SBarry Smith 197b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 198b6a5bde7SBarry Smith 199b6a5bde7SBarry Smith M*/ 200e489efc1SBarry Smith #define PetscMin(a,b) (((a)<(b)) ? (a) : (b)) 201b6a5bde7SBarry Smith 202b6a5bde7SBarry Smith /*MC 203b6a5bde7SBarry Smith PetscMax - Returns maxium of two numbers 204b6a5bde7SBarry 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 Synopsis: 210b6a5bde7SBarry Smith type max PetscMax(type v1,type v2) 211b6a5bde7SBarry Smith 212b6a5bde7SBarry Smith Notes: type can be integer or floating point value 213b6a5bde7SBarry Smith 214b6a5bde7SBarry Smith Level: beginner 215b6a5bde7SBarry Smith 216b6a5bde7SBarry Smith .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 217b6a5bde7SBarry Smith 218b6a5bde7SBarry Smith M*/ 219e489efc1SBarry Smith #define PetscMax(a,b) (((a)<(b)) ? (b) : (a)) 220b6a5bde7SBarry Smith 221b6a5bde7SBarry Smith /*MC 222b6a5bde7SBarry Smith PetscAbsInt - Returns the absolute value of an integer 223b6a5bde7SBarry Smith 224b6a5bde7SBarry Smith Input Parameter: 225b6a5bde7SBarry Smith . v1 - the integer 226b6a5bde7SBarry Smith 227b6a5bde7SBarry Smith Synopsis: 228b6a5bde7SBarry Smith int abs PetscAbsInt(int v1) 229b6a5bde7SBarry Smith 230b6a5bde7SBarry Smith 231b6a5bde7SBarry Smith Level: beginner 232b6a5bde7SBarry Smith 233b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr() 234b6a5bde7SBarry Smith 235b6a5bde7SBarry Smith M*/ 236e489efc1SBarry Smith #define PetscAbsInt(a) (((a)<0) ? -(a) : (a)) 237b6a5bde7SBarry Smith 238b6a5bde7SBarry Smith /*MC 239b6a5bde7SBarry Smith PetscAbsReal - Returns the absolute value of an real number 240b6a5bde7SBarry Smith 241b6a5bde7SBarry Smith Input Parameter: 242b6a5bde7SBarry Smith . v1 - the double 243b6a5bde7SBarry Smith 244b6a5bde7SBarry Smith Synopsis: 245b6a5bde7SBarry Smith int abs PetscAbsReal(PetscReal v1) 246b6a5bde7SBarry Smith 247b6a5bde7SBarry Smith 248b6a5bde7SBarry Smith Level: beginner 249b6a5bde7SBarry Smith 250b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr() 251b6a5bde7SBarry Smith 252b6a5bde7SBarry Smith M*/ 253f6275e2eSBarry Smith #define PetscAbsReal(a) (((a)<0) ? -(a) : (a)) 254b6a5bde7SBarry Smith 255b6a5bde7SBarry Smith /*MC 256b6a5bde7SBarry Smith PetscSqr - Returns the square of a number 257b6a5bde7SBarry Smith 258b6a5bde7SBarry Smith Input Parameter: 259b6a5bde7SBarry Smith . v1 - the value 260b6a5bde7SBarry Smith 261b6a5bde7SBarry Smith Synopsis: 262b6a5bde7SBarry Smith type sqr PetscSqr(type v1) 263b6a5bde7SBarry Smith 264b6a5bde7SBarry Smith Notes: type can be integer or floating point value 265b6a5bde7SBarry Smith 266b6a5bde7SBarry Smith Level: beginner 267b6a5bde7SBarry Smith 268b6a5bde7SBarry Smith .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal() 269b6a5bde7SBarry Smith 270b6a5bde7SBarry Smith M*/ 2714ebda54eSMatthew Knepley #define PetscSqr(a) ((a)*(a)) 272e489efc1SBarry Smith 273314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 274314da920SBarry Smith /* 27503c60df9SBarry Smith Basic constants - These should be done much better 276314da920SBarry Smith */ 277314da920SBarry Smith #define PETSC_PI 3.14159265358979323846264 278314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994 279f10639e6SSatish Balay #define PETSC_MAX_INT 1000000000 280f10639e6SSatish Balay #define PETSC_MIN_INT -1000000000 281e489efc1SBarry Smith 28282adfdadSBarry Smith #if defined(PETSC_USE_SINGLE) 2837e032f8bSBarry Smith # define PETSC_MAX 1.e30 2847e032f8bSBarry Smith # define PETSC_MIN -1.e30 285f10639e6SSatish Balay # define PETSC_MACHINE_EPSILON 1.e-7 286f10639e6SSatish Balay # define PETSC_SQRT_MACHINE_EPSILON 3.e-4 287cf6e855fSSatish Balay # define PETSC_SMALL 1.e-5 28803c60df9SBarry Smith #elif defined(PETSC_USE_INT) 28903c60df9SBarry Smith # define PETSC_MAX PETSC_MAX_INT 29003c60df9SBarry Smith # define PETSC_MIN PETSC_MIN_INT 29103c60df9SBarry Smith # define PETSC_MACHINE_EPSILON 1 29203c60df9SBarry Smith # define PETSC_SQRT_MACHINE_EPSILON 1 29303c60df9SBarry Smith # define PETSC_SMALL 0 29482adfdadSBarry Smith #else 2957e032f8bSBarry Smith # define PETSC_MAX 1.e300 2967e032f8bSBarry Smith # define PETSC_MIN -1.e300 297f10639e6SSatish Balay # define PETSC_MACHINE_EPSILON 1.e-14 298f10639e6SSatish Balay # define PETSC_SQRT_MACHINE_EPSILON 1.e-7 299cf6e855fSSatish Balay # define PETSC_SMALL 1.e-10 30082adfdadSBarry Smith #endif 30182adfdadSBarry Smith 302ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm); 303ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm); 304ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm); 3053e523bebSBarry Smith 3063e523bebSBarry Smith 307314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 308e489efc1SBarry Smith /* 309b0a32e0cSBarry Smith PetscLogDouble variables are used to contain double precision numbers 310e489efc1SBarry Smith that are not used in the numerical computations, but rather in logging, 311e489efc1SBarry Smith timing etc. 312e489efc1SBarry Smith */ 313b0a32e0cSBarry Smith typedef double PetscLogDouble; 314b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE 315e489efc1SBarry Smith 31687828ca2SBarry Smith #define PassiveReal PetscReal 317ea709b57SSatish Balay #define PassiveScalar PetscScalar 318d3ecb3a7SBarry Smith 319e9fa29b7SSatish Balay 320e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END 321e489efc1SBarry Smith #endif 322