1 /* $Id: petscmath.h,v 1.32 2001/08/30 20:37:06 bsmith Exp $ */ 2 /* 3 4 PETSc mathematics include file. Defines certain basic mathematical 5 constants and functions for working with single and double precision 6 floating point numbers as well as complex and integers. 7 8 This file is included by petsc.h and should not be used directly. 9 10 */ 11 12 #if !defined(__PETSCMATH_H) 13 #define __PETSCMATH_H 14 #include <math.h> 15 16 /* 17 18 Defines operations that are different for complex and real numbers; 19 note that one cannot really mix the use of complex and real in the same 20 PETSc program. All PETSc objects in one program are built around the object 21 PetscScalar which is either always a double or a complex. 22 23 */ 24 #if defined(PETSC_USE_COMPLEX) 25 26 #if defined (PETSC_HAVE_STD_COMPLEX) 27 #include <complex> 28 #else 29 #include <complex.h> 30 #endif 31 32 extern MPI_Datatype MPIU_COMPLEX; 33 #define MPIU_SCALAR MPIU_COMPLEX 34 #if defined(PETSC_USE_MAT_SINGLE) 35 #define MPIU_MATSCALAR ??Notdone 36 #else 37 #define MPIU_MATSCALAR MPIU_COMPLEX 38 #endif 39 40 #if defined (PETSC_HAVE_STD_COMPLEX) 41 #define PetscRealPart(a) (a).real() 42 #define PetscImaginaryPart(a) (a).imag() 43 #define PetscAbsScalar(a) std::abs(a) 44 #define PetscConj(a) std::conj(a) 45 #define PetscSqrtScalar(a) std::sqrt(a) 46 #define PetscPowScalar(a,b) std::pow(a,b) 47 #define PetscExpScalar(a) std::exp(a) 48 #define PetscSinScalar(a) std::sin(a) 49 #define PetscCosScalar(a) std::cos(a) 50 #else 51 #define PetscRealPart(a) real(a) 52 #define PetscImaginaryPart(a) imag(a) 53 #define PetscAbsScalar(a) abs(a) 54 #define PetscConj(a) conj(a) 55 #define PetscSqrtScalar(a) sqrt(a) 56 #define PetscPowScalar(a,b) pow(a,b) 57 #define PetscExpScalar(a) exp(a) 58 #define PetscSinScalar(a) sin(a) 59 #define PetscCosScalar(a) cos(a) 60 #endif 61 /* 62 The new complex class for GNU C++ is based on templates and is not backward 63 compatible with all previous complex class libraries. 64 */ 65 #if defined(PETSC_HAVE_STD_COMPLEX) 66 typedef std::complex<double> PetscScalar; 67 #elif defined(PETSC_HAVE_TEMPLATED_COMPLEX) 68 typedef complex<double> PetscScalar; 69 #else 70 typedef complex PetscScalar; 71 #endif 72 73 /* Compiling for real numbers only */ 74 #else 75 # if defined(PETSC_USE_SINGLE) 76 # define MPIU_SCALAR MPI_FLOAT 77 # else 78 # define MPIU_SCALAR MPI_DOUBLE 79 # endif 80 # if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE) 81 # define MPIU_MATSCALAR MPI_FLOAT 82 # else 83 # define MPIU_MATSCALAR MPI_DOUBLE 84 # endif 85 # define PetscRealPart(a) (a) 86 # define PetscImaginaryPart(a) (a) 87 # define PetscAbsScalar(a) (((a)<0.0) ? -(a) : (a)) 88 # define PetscConj(a) (a) 89 # define PetscSqrtScalar(a) sqrt(a) 90 # define PetscPowScalar(a,b) pow(a,b) 91 # define PetscExpScalar(a) exp(a) 92 # define PetscSinScalar(a) sin(a) 93 # define PetscCosScalar(a) cos(a) 94 95 # if defined(PETSC_USE_SINGLE) 96 typedef float PetscScalar; 97 # else 98 typedef double PetscScalar; 99 # endif 100 #endif 101 102 #if defined(PETSC_USE_SINGLE) 103 # define MPIU_REAL MPI_FLOAT 104 #else 105 # define MPIU_REAL MPI_DOUBLE 106 #endif 107 108 /* 109 Allows compiling PETSc so that matrix values are stored in 110 single precision but all other objects still use double 111 precision. This does not work for complex numbers in that case 112 it remains double 113 114 EXPERIMENTAL! NOT YET COMPLETELY WORKING 115 */ 116 #if defined(PETSC_USE_COMPLEX) 117 118 typedef PetscScalar MatScalar; 119 typedef double MatReal; 120 121 #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE) 122 123 typedef float MatScalar; 124 typedef float MatReal; 125 126 #else 127 128 typedef PetscScalar MatScalar; 129 typedef double MatReal; 130 131 #endif 132 133 #if defined(PETSC_USE_SINGLE) 134 typedef float PetscReal; 135 #else 136 typedef double PetscReal; 137 #endif 138 139 /* --------------------------------------------------------------------------*/ 140 141 /* 142 Certain objects may be created using either single 143 or double precision. 144 */ 145 typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE } PetscScalarPrecision; 146 147 /* PETSC_i is the imaginary number, i */ 148 extern PetscScalar PETSC_i; 149 150 #define PetscMin(a,b) (((a)<(b)) ? (a) : (b)) 151 #define PetscMax(a,b) (((a)<(b)) ? (b) : (a)) 152 #define PetscAbsInt(a) (((a)<0) ? -(a) : (a)) 153 #define PetscAbsReal(a) (((a)<0) ? -(a) : (a)) 154 #define PetscSqr(a) ((a)*(a)) 155 156 /* ----------------------------------------------------------------------------*/ 157 /* 158 Basic constants 159 */ 160 #define PETSC_PI 3.14159265358979323846264 161 #define PETSC_DEGREES_TO_RADIANS 0.01745329251994 162 #define PETSC_MAX_INT 1000000000 163 #define PETSC_MIN_INT -1000000000 164 165 #if defined(PETSC_USE_SINGLE) 166 # define PETSC_MAX 1.e30 167 # define PETSC_MIN -1.e30 168 # define PETSC_MACHINE_EPSILON 1.e-7 169 # define PETSC_SQRT_MACHINE_EPSILON 3.e-4 170 # define PETSC_SMALL 1.e-5 171 #else 172 # define PETSC_MAX 1.e300 173 # define PETSC_MIN -1.e300 174 # define PETSC_MACHINE_EPSILON 1.e-14 175 # define PETSC_SQRT_MACHINE_EPSILON 1.e-7 176 # define PETSC_SMALL 1.e-10 177 #endif 178 179 /* ----------------------------------------------------------------------------*/ 180 /* 181 PetscLogDouble variables are used to contain double precision numbers 182 that are not used in the numerical computations, but rather in logging, 183 timing etc. 184 */ 185 typedef double PetscLogDouble; 186 /* 187 Once PETSc is compiling with a ADIC enhanced version of MPI 188 we will create a new MPI_Datatype for the inactive double variables. 189 */ 190 #if defined(AD_DERIV_H) 191 /* extern MPI_Datatype MPIU_PETSCLOGDOUBLE; */ 192 #else 193 #if !defined(USING_MPIUNI) 194 #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE 195 #endif 196 #endif 197 198 #define PassiveReal PetscReal 199 #define PassiveScalar PetscScalar 200 201 #define PETSCMAP1_a(a,b) a ## _ ## b 202 #define PETSCMAP1_b(a,b) PETSCMAP1_a(a,b) 203 #define PETSCMAP1(a) PETSCMAP1_b(a,PetscScalar) 204 #endif 205