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