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: extern  MPI_Datatype        MPIU_2SCALAR;
 17: /*

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

 24: */

 26: #define PetscExpPassiveScalar(a) PetscExpScalar()

 28: #if defined(PETSC_USE_COMPLEX)

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

 35: extern  MPI_Datatype        MPIU_COMPLEX;
 36: #define MPIU_SCALAR         MPIU_COMPLEX
 37: #if defined(PETSC_USE_MAT_SINGLE)
 38: #define MPIU_MATSCALAR        ??Notdone
 39: #else
 40: #define MPIU_MATSCALAR      MPIU_COMPLEX
 41: #endif

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

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

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

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

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

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

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

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

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

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

121: /* --------------------------------------------------------------------------*/

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

129: /* PETSC_i is the imaginary number, i */
130: extern  PetscScalar       PETSC_i;

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

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

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

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

144:    Level: beginner


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

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

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

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

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

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

164:    Level: beginner

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

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

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

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

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


181:    Level: beginner

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

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

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

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

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


198:    Level: beginner

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

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

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

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

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

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

216:    Level: beginner

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

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

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

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

246: extern int PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
247: extern int PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
248: extern int PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);


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

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

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