1 /* 2 3 PETSc mathematics include file. Defines certain basic mathematical 4 constants and functions for working with single and double precision 5 floating point numbers as well as complex and integers. 6 7 This file is included by petscsys.h and should not be used directly. 8 9 */ 10 11 #if !defined(__PETSCMATH_H) 12 #define __PETSCMATH_H 13 #include <math.h> 14 PETSC_EXTERN_CXX_BEGIN 15 16 extern MPI_Datatype MPIU_2SCALAR; 17 extern MPI_Datatype MPIU_2INT; 18 19 /* 20 21 Defines operations that are different for complex and real numbers; 22 note that one cannot really mix the use of complex and real in the same 23 PETSc program. All PETSc objects in one program are built around the object 24 PetscScalar which is either always a real or a complex. 25 26 */ 27 28 #define PetscExpPassiveScalar(a) PetscExpScalar() 29 #if defined(PETSC_USE_REAL_SINGLE) 30 #define MPIU_REAL MPI_FLOAT 31 typedef float PetscReal; 32 #define PetscSqrtReal(a) sqrt(a) 33 #elif defined(PETSC_USE_REAL_DOUBLE) 34 #define MPIU_REAL MPI_DOUBLE 35 typedef double PetscReal; 36 #define PetscSqrtReal(a) sqrt(a) 37 #elif defined(PETSC_USE_REAL___FLOAT128) 38 #define MPIU_REAL MPIU___FLOAT128 39 typedef __float128 PetscReal; 40 #define PetscSqrtReal(a) sqrtq(a) 41 #endif /* PETSC_USE_REAL_* */ 42 43 /* 44 Complex number definitions 45 */ 46 #if defined(PETSC_USE_COMPLEX) 47 #if defined(PETSC_CLANGUAGE_CXX) 48 /* C++ support of complex number */ 49 #include <complex> 50 51 #define PetscRealPart(a) (a).real() 52 #define PetscImaginaryPart(a) (a).imag() 53 #define PetscAbsScalar(a) std::abs(a) 54 #define PetscConj(a) std::conj(a) 55 #define PetscSqrtScalar(a) std::sqrt(a) 56 #define PetscPowScalar(a,b) std::pow(a,b) 57 #define PetscExpScalar(a) std::exp(a) 58 #define PetscLogScalar(a) std::log(a) 59 #define PetscSinScalar(a) std::sin(a) 60 #define PetscCosScalar(a) std::cos(a) 61 62 #if defined(PETSC_USE_REAL_SINGLE) 63 typedef std::complex<float> PetscScalar; 64 #elif defined(PETSC_USE_REAL_DOUBLE) 65 typedef std::complex<double> PetscScalar; 66 #endif /* PETSC_USE_REAL_* */ 67 68 #else /* PETSC_CLANGUAGE_CXX */ 69 /* C support of complex numbers: Requires C99 compliant compiler*/ 70 #include <complex.h> 71 72 #if defined(PETSC_USE_REAL_SINGLE) 73 typedef float complex PetscScalar; 74 75 #define PetscRealPart(a) crealf(a) 76 #define PetscImaginaryPart(a) cimagf(a) 77 #define PetscAbsScalar(a) cabsf(a) 78 #define PetscConj(a) conjf(a) 79 #define PetscSqrtScalar(a) csqrtf(a) 80 #define PetscPowScalar(a,b) cpowf(a,b) 81 #define PetscExpScalar(a) cexpf(a) 82 #define PetscLogScalar(a) clogf(a) 83 #define PetscSinScalar(a) csinf(a) 84 #define PetscCosScalar(a) ccosf(a) 85 86 #elif defined(PETSC_USE_REAL_DOUBLE) 87 typedef double complex PetscScalar; 88 89 #define PetscRealPart(a) creal(a) 90 #define PetscImaginaryPart(a) cimag(a) 91 #define PetscAbsScalar(a) cabs(a) 92 #define PetscConj(a) conj(a) 93 #define PetscSqrtScalar(a) csqrt(a) 94 #define PetscPowScalar(a,b) cpow(a,b) 95 #define PetscExpScalar(a) cexp(a) 96 #define PetscLogScalar(a) clog(a) 97 #define PetscSinScalar(a) csin(a) 98 #define PetscCosScalar(a) ccos(a) 99 100 #endif /* PETSC_USE_REAL_* */ 101 #endif /* PETSC_CLANGUAGE_CXX */ 102 103 #if !defined(PETSC_HAVE_MPI_C_DOUBLE_COMPLEX) 104 extern MPI_Datatype MPIU_C_DOUBLE_COMPLEX; 105 extern MPI_Datatype MPIU_C_COMPLEX; 106 #else 107 #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX 108 #define MPIU_C_COMPLEX MPI_C_COMPLEX 109 #endif /* PETSC_HAVE_MPI_C_DOUBLE_COMPLEX */ 110 111 #if defined(PETSC_USE_REAL_SINGLE) 112 #define MPIU_SCALAR MPIU_C_COMPLEX 113 #elif defined(PETSC_USE_REAL_DOUBLE) 114 #define MPIU_SCALAR MPIU_C_DOUBLE_COMPLEX 115 #endif /* PETSC_USE_REAL_* */ 116 117 /* 118 real number definitions 119 */ 120 #else /* PETSC_USE_COMPLEX */ 121 #if defined(PETSC_USE_REAL_SINGLE) 122 #define MPIU_SCALAR MPI_FLOAT 123 typedef float PetscScalar; 124 #elif defined(PETSC_USE_REAL_DOUBLE) 125 #define MPIU_SCALAR MPI_DOUBLE 126 typedef double PetscScalar; 127 #elif defined(PETSC_USE_REAL___FLOAT128) 128 extern MPI_Datatype MPIU___FLOAT128; 129 #define MPIU_SCALAR MPIU___FLOAT128 130 typedef __float128 PetscScalar; 131 #endif /* PETSC_USE_REAL_* */ 132 #define PetscRealPart(a) (a) 133 #define PetscImaginaryPart(a) ((PetscReal)0.) 134 PETSC_STATIC_INLINE PetscReal PetscAbsScalar(PetscScalar a) {return a < 0.0 ? -a : a;} 135 #define PetscConj(a) (a) 136 #if !defined(PETSC_USE_REAL___FLOAT128) 137 #define PetscSqrtScalar(a) sqrt(a) 138 #define PetscPowScalar(a,b) pow(a,b) 139 #define PetscExpScalar(a) exp(a) 140 #define PetscLogScalar(a) log(a) 141 #define PetscSinScalar(a) sin(a) 142 #define PetscCosScalar(a) cos(a) 143 #else /* PETSC_USE_REAL___FLOAT128 */ 144 #include <quadmath.h> 145 #define PetscSqrtScalar(a) sqrtq(a) 146 #define PetscPowScalar(a,b) powq(a,b) 147 #define PetscExpScalar(a) expq(a) 148 #define PetscLogScalar(a) logq(a) 149 #define PetscSinScalar(a) sinq(a) 150 #define PetscCosScalar(a) cosq(a) 151 #endif /* PETSC_USE_REAL___FLOAT128 */ 152 153 #endif /* PETSC_USE_COMPLEX */ 154 155 #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1) 156 #define PetscAbs(a) (((a) >= 0) ? (a) : -(a)) 157 158 /* --------------------------------------------------------------------------*/ 159 160 /* 161 Certain objects may be created using either single or double precision. 162 This is currently not used. 163 */ 164 typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision; 165 166 /* PETSC_i is the imaginary number, i */ 167 extern PetscScalar PETSC_i; 168 169 /*MC 170 PetscMin - Returns minimum of two numbers 171 172 Synopsis: 173 type PetscMin(type v1,type v2) 174 175 Not Collective 176 177 Input Parameter: 178 + v1 - first value to find minimum of 179 - v2 - second value to find minimum of 180 181 182 Notes: type can be integer or floating point value 183 184 Level: beginner 185 186 187 .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 188 189 M*/ 190 #define PetscMin(a,b) (((a)<(b)) ? (a) : (b)) 191 192 /*MC 193 PetscMax - Returns maxium of two numbers 194 195 Synopsis: 196 type max PetscMax(type v1,type v2) 197 198 Not Collective 199 200 Input Parameter: 201 + v1 - first value to find maximum of 202 - v2 - second value to find maximum of 203 204 Notes: type can be integer or floating point value 205 206 Level: beginner 207 208 .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 209 210 M*/ 211 #define PetscMax(a,b) (((a)<(b)) ? (b) : (a)) 212 213 /*MC 214 PetscAbsInt - Returns the absolute value of an integer 215 216 Synopsis: 217 int abs PetscAbsInt(int v1) 218 219 Not Collective 220 221 Input Parameter: 222 . v1 - the integer 223 224 Level: beginner 225 226 .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr() 227 228 M*/ 229 #define PetscAbsInt(a) (((a)<0) ? -(a) : (a)) 230 231 /*MC 232 PetscAbsReal - Returns the absolute value of an real number 233 234 Synopsis: 235 Real abs PetscAbsReal(PetscReal v1) 236 237 Not Collective 238 239 Input Parameter: 240 . v1 - the double 241 242 243 Level: beginner 244 245 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr() 246 247 M*/ 248 #define PetscAbsReal(a) (((a)<0) ? -(a) : (a)) 249 250 /*MC 251 PetscSqr - Returns the square of a number 252 253 Synopsis: 254 type sqr PetscSqr(type v1) 255 256 Not Collective 257 258 Input Parameter: 259 . v1 - the value 260 261 Notes: type can be integer or floating point value 262 263 Level: beginner 264 265 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal() 266 267 M*/ 268 #define PetscSqr(a) ((a)*(a)) 269 270 /* ----------------------------------------------------------------------------*/ 271 /* 272 Basic constants 273 */ 274 #if defined(PETSC_USE_REAL___FLOAT128) 275 #define PETSC_PI M_PIq 276 #elif defined(M_PI) 277 #define PETSC_PI M_PI 278 #else 279 #define PETSC_PI 3.14159265358979323846264 280 #endif 281 282 #if !defined(PETSC_USE_64BIT_INDICES) 283 #define PETSC_MAX_INT 2147483647 284 #define PETSC_MIN_INT (-PETSC_MAX_INT - 1) 285 #else 286 #define PETSC_MAX_INT 9223372036854775807L 287 #define PETSC_MIN_INT (-PETSC_MAX_INT - 1) 288 #endif 289 290 #if defined(PETSC_USE_REAL_SINGLE) 291 # define PETSC_MAX_REAL 3.40282346638528860e+38F 292 # define PETSC_MIN_REAL -PETSC_MAX_REAL 293 # define PETSC_MACHINE_EPSILON 1.19209290e-07F 294 # define PETSC_SQRT_MACHINE_EPSILON 3.45266983e-04F 295 # define PETSC_SMALL 1.e-5 296 #elif defined(PETSC_USE_REAL_DOUBLE) 297 # define PETSC_MAX_REAL 1.7976931348623157e+308 298 # define PETSC_MIN_REAL -PETSC_MAX_REAL 299 # define PETSC_MACHINE_EPSILON 2.2204460492503131e-16 300 # define PETSC_SQRT_MACHINE_EPSILON 1.490116119384766e-08 301 # define PETSC_SMALL 1.e-10 302 #elif defined(PETSC_USE_REAL___FLOAT128) 303 # define PETSC_MAX_REAL FLT128_MAX 304 # define PETSC_MIN_REAL -FLT128_MAX 305 # define PETSC_MACHINE_EPSILON FLT128_EPSILON 306 # define PETSC_SQRT_MACHINE_EPSILON 1.38777878078e-17 307 # define PETSC_SMALL 1.e-20 308 #endif 309 310 #if defined PETSC_HAVE_ADIC 311 /* Use MPI_Allreduce when ADIC is not available. */ 312 extern PetscErrorCode PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*); 313 extern PetscErrorCode PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*); 314 extern PetscErrorCode PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*); 315 #endif 316 317 extern PetscErrorCode PetscIsInfOrNanScalar(PetscScalar); 318 extern PetscErrorCode PetscIsInfOrNanReal(PetscReal); 319 320 /* ----------------------------------------------------------------------------*/ 321 /* 322 PetscLogDouble variables are used to contain double precision numbers 323 that are not used in the numerical computations, but rather in logging, 324 timing etc. 325 */ 326 typedef double PetscLogDouble; 327 #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE 328 329 #define PassiveReal PetscReal 330 #define PassiveScalar PetscScalar 331 332 /* 333 These macros are currently hardwired to match the regular data types, so there is no support for a different 334 MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again. 335 */ 336 #define MPIU_MATSCALAR MPIU_SCALAR 337 typedef PetscScalar MatScalar; 338 typedef PetscReal MatReal; 339 340 341 PETSC_EXTERN_CXX_END 342 #endif 343