1*184914b5SBarry Smith /* $Id: petscmath.h,v 1.16 1999/05/27 19:41:20 balay Exp bsmith $ */ 2e489efc1SBarry Smith /* 3314da920SBarry Smith 4314da920SBarry Smith PETSc mathematics include file. Defines certain basic mathematical 5314da920SBarry Smith constants and functions for working with single and double precision 6314da920SBarry Smith floating point numbers as well as complex and integers. 7314da920SBarry Smith 8e7029fe1SSatish Balay This file is included by petsc.h and should not be used directly. 9e7029fe1SSatish Balay 10e489efc1SBarry Smith */ 11e489efc1SBarry Smith 12488ecbafSBarry Smith #if !defined(__PETSCMATH_H) 13488ecbafSBarry Smith #define __PETSCMATH_H 140a5f7794SBarry Smith #include <math.h> 150a5f7794SBarry Smith 16314da920SBarry Smith /* 17f4ccad53SBarry Smith 18f4ccad53SBarry Smith Defines operations that are different for complex and real numbers; 19f4ccad53SBarry Smith note that one cannot really mix the use of complex and real in the same 20f4ccad53SBarry Smith PETSc program. All PETSc objects in one program are built around the object 21f4ccad53SBarry Smith Scalar which is either always a double or a complex. 22f4ccad53SBarry Smith 23e489efc1SBarry Smith */ 24aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 25adc17e78SSatish Balay 26c80bc32dSSatish Balay #if defined (PETSC_HAVE_STD_COMPLEX) 27df9b3741SSatish Balay #include <complex> 28aa482453SBarry Smith #elif defined(PETSC_HAVE_NONSTANDARD_COMPLEX_H) 29aa482453SBarry Smith #include PETSC_HAVE_NONSTANDARD_COMPLEX_H 30adc17e78SSatish Balay #else 31adc17e78SSatish Balay #include <complex.h> 32adc17e78SSatish Balay #endif 33adc17e78SSatish Balay 34adc17e78SSatish Balay extern MPI_Datatype MPIU_COMPLEX; 35adc17e78SSatish Balay #define MPIU_SCALAR MPIU_COMPLEX 36c80bc32dSSatish Balay #if defined (PETSC_HAVE_STD_COMPLEX) 37df9b3741SSatish Balay #define PetscReal(a) (a).real() 38df9b3741SSatish Balay #define PetscImaginary(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) 42*184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b) 43*184914b5SBarry Smith #define PetscExpScalar(a) std::exp(a) 44*184914b5SBarry Smith #define PetscSinScalar(a) std::sin(a) 45*184914b5SBarry Smith #define PetscCosScalar(a) std::cos(a) 46e489efc1SBarry Smith #else 47e489efc1SBarry Smith #define PetscReal(a) real(a) 48e489efc1SBarry Smith #define PetscImaginary(a) imag(a) 49e489efc1SBarry Smith #define PetscAbsScalar(a) abs(a) 50e489efc1SBarry Smith #define PetscConj(a) conj(a) 5118a7d68fSSatish Balay #define PetscSqrtScalar(a) sqrt(a) 52*184914b5SBarry Smith #define PetscPowScalar(a,b) pow(a,b) 53*184914b5SBarry Smith #define PetscExpScalar(a) exp(a) 54*184914b5SBarry Smith #define PetscSinScalar(a) sin(a) 55*184914b5SBarry Smith #define PetscCosScalar(a) cos(a) 56adc17e78SSatish Balay #endif 57e489efc1SBarry Smith /* 58e489efc1SBarry Smith The new complex class for GNU C++ is based on templates and is not backward 59e489efc1SBarry Smith compatible with all previous complex class libraries. 60e489efc1SBarry Smith */ 61c80bc32dSSatish Balay #if defined(PETSC_HAVE_STD_COMPLEX) 62df9b3741SSatish Balay #define Scalar std::complex<double> 63aa482453SBarry Smith #elif defined(PETSC_HAVE_TEMPLATED_COMPLEX) 64e489efc1SBarry Smith #define Scalar complex<double> 65e489efc1SBarry Smith #else 66e489efc1SBarry Smith #define Scalar complex 67e489efc1SBarry Smith #endif 68e489efc1SBarry Smith 69e489efc1SBarry Smith /* Compiling for real numbers only */ 70e489efc1SBarry Smith #else 71e489efc1SBarry Smith #define MPIU_SCALAR MPI_DOUBLE 72e489efc1SBarry Smith #define PetscReal(a) (a) 73e489efc1SBarry Smith #define PetscImaginary(a) (a) 74e489efc1SBarry Smith #define PetscAbsScalar(a) ( ((a)<0.0) ? -(a) : (a) ) 75e489efc1SBarry Smith #define Scalar double 76e489efc1SBarry Smith #define PetscConj(a) (a) 7718a7d68fSSatish Balay #define PetscSqrtScalar(a) sqrt(a) 78*184914b5SBarry Smith #define PetscPowScalar(a,b) pow(a,b) 79*184914b5SBarry Smith #define PetscExpScalar(a) exp(a) 80*184914b5SBarry Smith #define PetscSinScalar(a) sin(a) 81*184914b5SBarry Smith #define PetscCosScalar(a) cos(a) 82e489efc1SBarry Smith #endif 83e489efc1SBarry Smith 843f1db9ecSBarry Smith /* 853f1db9ecSBarry Smith Allows compiling PETSc so that matrix values are stored in 863f1db9ecSBarry Smith single precision but all other objects still use double 873f1db9ecSBarry Smith precision. This does not work for complex numbers in that case 883f1db9ecSBarry Smith it remains double 893f1db9ecSBarry Smith 903f1db9ecSBarry Smith EXPERIMENTAL! NOT YET COMPLETELY WORKING 913f1db9ecSBarry Smith */ 92aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX) 933f1db9ecSBarry Smith 943f1db9ecSBarry Smith #define MatScalar Scalar 953f1db9ecSBarry Smith #define MatFloat double 963f1db9ecSBarry Smith 97aa482453SBarry Smith #elif defined(PETSC_USE_MAT_SINGLE) 983f1db9ecSBarry Smith 993f1db9ecSBarry Smith #define MatScalar float 1003f1db9ecSBarry Smith #define MatFloat float 1013f1db9ecSBarry Smith 1023f1db9ecSBarry Smith #else 1033f1db9ecSBarry Smith 1043f1db9ecSBarry Smith #define MatScalar Scalar 1053f1db9ecSBarry Smith #define MatFloat double 1063f1db9ecSBarry Smith 1073f1db9ecSBarry Smith #endif 1083f1db9ecSBarry Smith 1093f1db9ecSBarry Smith 110314da920SBarry Smith /* --------------------------------------------------------------------------*/ 111314da920SBarry Smith 112e489efc1SBarry Smith /* 113e489efc1SBarry Smith Certain objects may be created using either single 114e489efc1SBarry Smith or double precision. 115e489efc1SBarry Smith */ 116e489efc1SBarry Smith typedef enum { SCALAR_DOUBLE, SCALAR_SINGLE } ScalarPrecision; 117e489efc1SBarry Smith 118e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */ 119e489efc1SBarry Smith extern Scalar PETSC_i; 120e489efc1SBarry Smith 121e489efc1SBarry Smith #define PetscMin(a,b) ( ((a)<(b)) ? (a) : (b) ) 122e489efc1SBarry Smith #define PetscMax(a,b) ( ((a)<(b)) ? (b) : (a) ) 123e489efc1SBarry Smith #define PetscAbsInt(a) ( ((a)<0) ? -(a) : (a) ) 124e489efc1SBarry Smith #define PetscAbsDouble(a) ( ((a)<0) ? -(a) : (a) ) 125e489efc1SBarry Smith 126314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 127314da920SBarry Smith /* 128314da920SBarry Smith Basic constants 129314da920SBarry Smith */ 130314da920SBarry Smith #define PETSC_PI 3.14159265358979323846264 131314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994 132e489efc1SBarry Smith #define PETSC_MAX 1.e300 133e489efc1SBarry Smith #define PETSC_MIN -1.e300 1340a5f7794SBarry Smith #define PETSC_MAX_INT 1000000000; 1350a5f7794SBarry Smith #define PETSC_MIN_INT -1000000000; 136e489efc1SBarry Smith 137314da920SBarry Smith /* ----------------------------------------------------------------------------*/ 138e489efc1SBarry Smith /* 139e489efc1SBarry Smith PLogDouble variables are used to contain double precision numbers 140e489efc1SBarry Smith that are not used in the numerical computations, but rather in logging, 141e489efc1SBarry Smith timing etc. 142e489efc1SBarry Smith */ 143e489efc1SBarry Smith typedef double PLogDouble; 144e489efc1SBarry Smith /* 145e489efc1SBarry Smith Once PETSc is compiling with a ADIC enhanced version of MPI 146e489efc1SBarry Smith we will create a new MPI_Datatype for the inactive double variables. 147e489efc1SBarry Smith */ 148e489efc1SBarry Smith #if defined(AD_DERIV_H) 149e489efc1SBarry Smith /* extern MPI_Datatype MPIU_PLOGDOUBLE; */ 150e489efc1SBarry Smith #else 151488ecbafSBarry Smith #if !defined(USING_MPIUNI) 152e489efc1SBarry Smith #define MPIU_PLOGDOUBLE MPI_DOUBLE 153e489efc1SBarry Smith #endif 154e489efc1SBarry Smith #endif 155e489efc1SBarry Smith 156e489efc1SBarry Smith 157e489efc1SBarry Smith #endif 158