Actual source code: petscmath.h

  1: /* $Id: petscmath.h,v 1.32 2001/08/30 20:37:06 bsmith Exp $ */
  2: /*
  3:    
  4:       PETSc mathematics include file. Defines certain basic mathematical 
  5:     constants and functions for working with single and double precision
  6:     floating point numbers as well as complex and integers.

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

 10: */

 14: #include <math.h>

 16: /*

 18:      Defines operations that are different for complex and real numbers;
 19:    note that one cannot really mix the use of complex and real in the same 
 20:    PETSc program. All PETSc objects in one program are built around the object
 21:    PetscScalar which is either always a double or a complex.

 23: */
 24: #if defined(PETSC_USE_COMPLEX)

 26: /*
 27:    PETSc now only supports std::complex
 28: */
 29: #include <complex>

 31: extern  MPI_Datatype        MPIU_COMPLEX;
 32: #define MPIU_SCALAR         MPIU_COMPLEX
 33: #if defined(PETSC_USE_MAT_SINGLE)
 34: #define MPIU_MATSCALAR        ??Notdone
 35: #else
 36: #define MPIU_MATSCALAR      MPIU_COMPLEX
 37: #endif

 39: #define PetscRealPart(a)        (a).real()
 40: #define PetscImaginaryPart(a)   (a).imag()
 41: #define PetscAbsScalar(a)   std::abs(a)
 42: #define PetscConj(a)        std::conj(a)
 43: #define PetscSqrtScalar(a)  std::sqrt(a)
 44: #define PetscPowScalar(a,b) std::pow(a,b)
 45: #define PetscExpScalar(a)   std::exp(a)
 46: #define PetscSinScalar(a)   std::sin(a)
 47: #define PetscCosScalar(a)   std::cos(a)

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

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

 73: #  if defined(PETSC_USE_SINGLE)
 74:   typedef float PetscScalar;
 75: #  else
 76:   typedef double PetscScalar;
 77: #  endif
 78: #endif

 80: #if defined(PETSC_USE_SINGLE)
 81: #  define MPIU_REAL   MPI_FLOAT
 82: #else
 83: #  define MPIU_REAL   MPI_DOUBLE
 84: #endif

 86: /*
 87:        Allows compiling PETSc so that matrix values are stored in 
 88:    single precision but all other objects still use double
 89:    precision. This does not work for complex numbers in that case
 90:    it remains double

 92:           EXPERIMENTAL! NOT YET COMPLETELY WORKING
 93: */

 95: #if defined(PETSC_USE_MAT_SINGLE)
 96: typedef float MatScalar;
 97: #else
 98: typedef PetscScalar MatScalar;
 99: #endif

101: #if defined(PETSC_USE_COMPLEX)
102: typedef double MatReal;
103: #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
104: typedef float MatReal;
105: #else
106: typedef double MatReal;
107: #endif

109: #if defined(PETSC_USE_SINGLE)
110:   typedef float PetscReal;
111: #else 
112:   typedef double PetscReal;
113: #endif

115: /* --------------------------------------------------------------------------*/

117: /*
118:    Certain objects may be created using either single
119:   or double precision.
120: */
121: typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE } PetscScalarPrecision;

123: /* PETSC_i is the imaginary number, i */
124: extern  PetscScalar       PETSC_i;

126: #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
127: #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
128: #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
129: #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
130: #define PetscSqr(a)     ((a)*(a))

132: /* ----------------------------------------------------------------------------*/
133: /*
134:      Basic constants
135: */
136: #define PETSC_PI                 3.14159265358979323846264
137: #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
138: #define PETSC_MAX_INT            1000000000
139: #define PETSC_MIN_INT            -1000000000

141: #if defined(PETSC_USE_SINGLE)
142: #  define PETSC_MAX                1.e30
143: #  define PETSC_MIN                -1.e30
144: #  define PETSC_MACHINE_EPSILON         1.e-7
145: #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
146: #  define PETSC_SMALL                   1.e-5
147: #else
148: #  define PETSC_MAX                1.e300
149: #  define PETSC_MIN                -1.e300
150: #  define PETSC_MACHINE_EPSILON         1.e-14
151: #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
152: #  define PETSC_SMALL                   1.e-10
153: #endif

155: extern int PetscGlobalMax(double*,double*,MPI_Comm);
156: extern int PetscGlobalMin(double*,double*,MPI_Comm);
157: extern int PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);


160: /* ----------------------------------------------------------------------------*/
161: /*
162:     PetscLogDouble variables are used to contain double precision numbers
163:   that are not used in the numerical computations, but rather in logging,
164:   timing etc.
165: */
166: typedef double PetscLogDouble;
167: /*
168:       Once PETSc is compiling with a ADIC enhanced version of MPI
169:    we will create a new MPI_Datatype for the inactive double variables.
170: */
171: #if defined(AD_DERIV_H)
172: /* extern  MPI_Datatype  MPIU_PETSCLOGDOUBLE; */
173: #else
174: #if !defined(_petsc_mpi_uni)
175: #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
176: #endif
177: #endif

179: #define PassiveReal PetscReal
180: #define PassiveScalar PetscScalar

182: #define PETSCMAP1_a(a,b)  a ## _ ## b
183: #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
184: #define PETSCMAP1(a)      PETSCMAP1_b(a,PetscScalar)
185: #endif