Actual source code: petscmath.h

      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.

      7:     This file is included by petsc.h and should not be used directly.

      9: */

     13: #include <math.h>

     18: /*

     20:      Defines operations that are different for complex and real numbers;
     21:    note that one cannot really 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 double or a complex.

     25: */

     27: #define PetscExpPassiveScalar(a) PetscExpScalar()

     29: #if defined(PETSC_USE_COMPLEX)

     31: /*
     32:    PETSc now only supports std::complex
     33: */
     34: #include <complex>

     37: #define MPIU_SCALAR         MPIU_COMPLEX
     38: #if defined(PETSC_USE_MAT_SINGLE)
     39: #define MPIU_MATSCALAR        ??Notdone
     40: #else
     41: #define MPIU_MATSCALAR      MPIU_COMPLEX
     42: #endif

     44: #define PetscRealPart(a)        (a).real()
     45: #define PetscImaginaryPart(a)   (a).imag()
     46: #define PetscAbsScalar(a)   std::abs(a)
     47: #define PetscConj(a)        std::conj(a)
     48: #define PetscSqrtScalar(a)  std::sqrt(a)
     49: #define PetscPowScalar(a,b) std::pow(a,b)
     50: #define PetscExpScalar(a)   std::exp(a)
     51: #define PetscSinScalar(a)   std::sin(a)
     52: #define PetscCosScalar(a)   std::cos(a)

     54: typedef std::complex<double> PetscScalar;

     56: /* Compiling for real numbers only */
     57: #else
     58: #  if defined(PETSC_USE_SINGLE)
     59: #    define MPIU_SCALAR           MPI_FLOAT
     60: #  else
     61: #    define MPIU_SCALAR           MPI_DOUBLE
     62: #  endif
     63: #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
     64: #    define MPIU_MATSCALAR        MPI_FLOAT
     65: #  else
     66: #    define MPIU_MATSCALAR        MPI_DOUBLE
     67: #  endif
     68: #  define PetscRealPart(a)      (a)
     69: #  define PetscImaginaryPart(a) (a)
     70: #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
     71: #  define PetscConj(a)          (a)
     72: #  define PetscSqrtScalar(a)    sqrt(a)
     73: #  define PetscPowScalar(a,b)   pow(a,b)
     74: #  define PetscExpScalar(a)     exp(a)
     75: #  define PetscSinScalar(a)     sin(a)
     76: #  define PetscCosScalar(a)     cos(a)

     78: #  if defined(PETSC_USE_SINGLE)
     79:   typedef float PetscScalar;
     80: #  else
     81:   typedef double PetscScalar;
     82: #  endif
     83: #endif

     85: #if defined(PETSC_USE_SINGLE)
     86: #  define MPIU_REAL   MPI_FLOAT
     87: #else
     88: #  define MPIU_REAL   MPI_DOUBLE
     89: #endif

     91: #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
     92: #define PetscAbs(a)  (((a) >= 0) ? a : -a)
     93: /*
     94:        Allows compiling PETSc so that matrix values are stored in
     95:    single precision but all other objects still use double
     96:    precision. This does not work for complex numbers in that case
     97:    it remains double

     99:           EXPERIMENTAL! NOT YET COMPLETELY WORKING
    100: */

    102: #if defined(PETSC_USE_MAT_SINGLE)
    103: typedef float MatScalar;
    104: #else
    105: typedef PetscScalar MatScalar;
    106: #endif

    108: #if defined(PETSC_USE_COMPLEX)
    109: typedef double MatReal;
    110: #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
    111: typedef float MatReal;
    112: #else
    113: typedef double MatReal;
    114: #endif

    116: #if defined(PETSC_USE_SINGLE)
    117:   typedef float PetscReal;
    118: #else
    119:   typedef double PetscReal;
    120: #endif

    122: /* --------------------------------------------------------------------------*/

    124: /*
    125:    Certain objects may be created using either single
    126:   or double precision.
    127: */
    128: typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE } PetscScalarPrecision;

    130: /* PETSC_i is the imaginary number, i */

    133: /*MC
    134:    PetscMin - Returns minimum of two numbers

    136:    Input Parameter:
    137: +  v1 - first value to find minimum of
    138: -  v2 - second value to find minimum of

    140:    Synopsis:
    141:    type PetscMin(type v1,type v2)

    143:    Notes: type can be integer or floating point value

    145:    Level: beginner

    148: .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()

    150: M*/
    151: #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))

    153: /*MC
    154:    PetscMax - Returns maxium of two numbers

    156:    Input Parameter:
    157: +  v1 - first value to find maximum of
    158: -  v2 - second value to find maximum of

    160:    Synopsis:
    161:    type max PetscMax(type v1,type v2)

    163:    Notes: type can be integer or floating point value

    165:    Level: beginner

    167: .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()

    169: M*/
    170: #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))

    172: /*MC
    173:    PetscAbsInt - Returns the absolute value of an integer

    175:    Input Parameter:
    176: .   v1 - the integer

    178:    Synopsis:
    179:    int abs PetscAbsInt(int v1)

    182:    Level: beginner

    184: .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()

    186: M*/
    187: #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))

    189: /*MC
    190:    PetscAbsReal - Returns the absolute value of an real number

    192:    Input Parameter:
    193: .   v1 - the double

    195:    Synopsis:
    196:    int abs PetscAbsReal(PetscReal v1)

    199:    Level: beginner

    201: .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()

    203: M*/
    204: #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))

    206: /*MC
    207:    PetscSqr - Returns the square of a number

    209:    Input Parameter:
    210: .   v1 - the value

    212:    Synopsis:
    213:    type sqr PetscSqr(type v1)

    215:    Notes: type can be integer or floating point value

    217:    Level: beginner

    219: .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()

    221: M*/
    222: #define PetscSqr(a)     ((a)*(a))

    224: /* ----------------------------------------------------------------------------*/
    225: /*
    226:      Basic constants
    227: */
    228: #define PETSC_PI                 3.14159265358979323846264
    229: #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
    230: #define PETSC_MAX_INT            1000000000
    231: #define PETSC_MIN_INT            -1000000000

    233: #if defined(PETSC_USE_SINGLE)
    234: #  define PETSC_MAX                1.e30
    235: #  define PETSC_MIN                -1.e30
    236: #  define PETSC_MACHINE_EPSILON         1.e-7
    237: #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
    238: #  define PETSC_SMALL                   1.e-5
    239: #else
    240: #  define PETSC_MAX                1.e300
    241: #  define PETSC_MIN                -1.e300
    242: #  define PETSC_MACHINE_EPSILON         1.e-14
    243: #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
    244: #  define PETSC_SMALL                   1.e-10
    245: #endif

    247: EXTERN PetscErrorCode PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
    248: EXTERN PetscErrorCode PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
    249: EXTERN PetscErrorCode PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);

    252: /* ----------------------------------------------------------------------------*/
    253: /*
    254:     PetscLogDouble variables are used to contain double precision numbers
    255:   that are not used in the numerical computations, but rather in logging,
    256:   timing etc.
    257: */
    258: typedef double PetscLogDouble;
    259: /*
    260:       Once PETSc is compiling with a ADIC enhanced version of MPI
    261:    we will create a new MPI_Datatype for the inactive double variables.
    262: */
    263: #if defined(AD_DERIV_H)
    265: #else
    266: #if !defined(_petsc_mpi_uni)
    267: #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
    268: #endif
    269: #endif

    271: #define PassiveReal   PetscReal
    272: #define PassiveScalar PetscScalar

    274: #define PETSCMAP1_a(a,b)  a ## _ ## b
    275: #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
    276: #define PETSCMAP1(a)      PETSCMAP1_b(a,PetscScalar)

    279: #endif