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