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 #define MPIU_C_DOUBLE_COMPLEX MPI_C_DOUBLE_COMPLEX 105 #define MPIU_C_COMPLEX MPI_C_COMPLEX 106 #else 107 extern MPI_Datatype MPIU_C_DOUBLE_COMPLEX; 108 extern MPI_Datatype MPIU_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(), PetscClipInterval(), 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(), PetscClipInterval(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 209 210 M*/ 211 #define PetscMax(a,b) (((a)<(b)) ? (b) : (a)) 212 213 /*MC 214 PetscClipInterval - Returns a number clipped to be within an interval 215 216 Synopsis: 217 type clip PetscClipInterval(type x,type a,type b) 218 219 Not Collective 220 221 Input Parameter: 222 + x - value to use if within interval (a,b) 223 . a - lower end of interval 224 - b - upper end of interval 225 226 Notes: type can be integer or floating point value 227 228 Level: beginner 229 230 .seealso: PetscMin(), PetscMax(), PetscAbsInt(), PetscAbsReal(), PetscSqr() 231 232 M*/ 233 #define PetscClipInterval(x,a,b) (PetscMax((a),PetscMin((x),(b)))) 234 235 /*MC 236 PetscAbsInt - Returns the absolute value of an integer 237 238 Synopsis: 239 int abs PetscAbsInt(int v1) 240 241 Not Collective 242 243 Input Parameter: 244 . v1 - the integer 245 246 Level: beginner 247 248 .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr() 249 250 M*/ 251 #define PetscAbsInt(a) (((a)<0) ? -(a) : (a)) 252 253 /*MC 254 PetscAbsReal - Returns the absolute value of an real number 255 256 Synopsis: 257 Real abs PetscAbsReal(PetscReal v1) 258 259 Not Collective 260 261 Input Parameter: 262 . v1 - the double 263 264 265 Level: beginner 266 267 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr() 268 269 M*/ 270 #define PetscAbsReal(a) (((a)<0) ? -(a) : (a)) 271 272 /*MC 273 PetscSqr - Returns the square of a number 274 275 Synopsis: 276 type sqr PetscSqr(type v1) 277 278 Not Collective 279 280 Input Parameter: 281 . v1 - the value 282 283 Notes: type can be integer or floating point value 284 285 Level: beginner 286 287 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal() 288 289 M*/ 290 #define PetscSqr(a) ((a)*(a)) 291 292 /* ----------------------------------------------------------------------------*/ 293 /* 294 Basic constants 295 */ 296 #if defined(PETSC_USE_REAL___FLOAT128) 297 #define PETSC_PI M_PIq 298 #elif defined(M_PI) 299 #define PETSC_PI M_PI 300 #else 301 #define PETSC_PI 3.14159265358979323846264338327950288419716939937510582 302 #endif 303 304 #if !defined(PETSC_USE_64BIT_INDICES) 305 #define PETSC_MAX_INT 2147483647 306 #define PETSC_MIN_INT (-PETSC_MAX_INT - 1) 307 #else 308 #define PETSC_MAX_INT 9223372036854775807L 309 #define PETSC_MIN_INT (-PETSC_MAX_INT - 1) 310 #endif 311 312 #if defined(PETSC_USE_REAL_SINGLE) 313 # define PETSC_MAX_REAL 3.40282346638528860e+38F 314 # define PETSC_MIN_REAL -PETSC_MAX_REAL 315 # define PETSC_MACHINE_EPSILON 1.19209290e-07F 316 # define PETSC_SQRT_MACHINE_EPSILON 3.45266983e-04F 317 # define PETSC_SMALL 1.e-5 318 #elif defined(PETSC_USE_REAL_DOUBLE) 319 # define PETSC_MAX_REAL 1.7976931348623157e+308 320 # define PETSC_MIN_REAL -PETSC_MAX_REAL 321 # define PETSC_MACHINE_EPSILON 2.2204460492503131e-16 322 # define PETSC_SQRT_MACHINE_EPSILON 1.490116119384766e-08 323 # define PETSC_SMALL 1.e-10 324 #elif defined(PETSC_USE_REAL___FLOAT128) 325 # define PETSC_MAX_REAL FLT128_MAX 326 # define PETSC_MIN_REAL -FLT128_MAX 327 # define PETSC_MACHINE_EPSILON FLT128_EPSILON 328 # define PETSC_SQRT_MACHINE_EPSILON 1.38777878078e-17 329 # define PETSC_SMALL 1.e-20 330 #endif 331 332 #if defined PETSC_HAVE_ADIC 333 /* Use MPI_Allreduce when ADIC is not available. */ 334 extern PetscErrorCode PetscGlobalMax(MPI_Comm, const PetscReal*,PetscReal*); 335 extern PetscErrorCode PetscGlobalMin(MPI_Comm, const PetscReal*,PetscReal*); 336 extern PetscErrorCode PetscGlobalSum(MPI_Comm, const PetscScalar*,PetscScalar*); 337 #endif 338 339 extern PetscErrorCode PetscIsInfOrNanScalar(PetscScalar); 340 extern PetscErrorCode PetscIsInfOrNanReal(PetscReal); 341 342 /* ----------------------------------------------------------------------------*/ 343 /* 344 PetscLogDouble variables are used to contain double precision numbers 345 that are not used in the numerical computations, but rather in logging, 346 timing etc. 347 */ 348 typedef double PetscLogDouble; 349 #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE 350 351 #define PassiveReal PetscReal 352 #define PassiveScalar PetscScalar 353 354 /* 355 These macros are currently hardwired to match the regular data types, so there is no support for a different 356 MatScalar from PetscScalar. We left the MatScalar in the source just in case we use it again. 357 */ 358 #define MPIU_MATSCALAR MPIU_SCALAR 359 typedef PetscScalar MatScalar; 360 typedef PetscReal MatReal; 361 362 363 PETSC_EXTERN_CXX_END 364 #endif 365