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: #  elif defined(PETSC_USE_LONG_DOUBLE)
 61: #    define MPIU_SCALAR           MPI_LONG_DOUBLE
 62: #  elif defined(PETSC_INT)
 63: #    define MPIU_INT              MPI_INT
 64: #  else
 65: #    define MPIU_SCALAR           MPI_DOUBLE
 66: #  endif
 67: #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
 68: #    define MPIU_MATSCALAR        MPI_FLOAT
 69: #  elif defined(PETSC_USE_LONG_DOUBLE)
 70: #    define MPIU_MATSCALAR        MPI_LONG_DOUBLE
 71: #  elif defined(PETSC_USE_INT)
 72: #    define MPIU_MATSCALAR        MPI_INT
 73: #  else
 74: #    define MPIU_MATSCALAR        MPI_DOUBLE
 75: #  endif
 76: #  define PetscRealPart(a)      (a)
 77: #  define PetscImaginaryPart(a) (0)
 78: #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
 79: #  define PetscConj(a)          (a)
 80: #  define PetscSqrtScalar(a)    sqrt(a)
 81: #  define PetscPowScalar(a,b)   pow(a,b)
 82: #  define PetscExpScalar(a)     exp(a)
 83: #  define PetscSinScalar(a)     sin(a)
 84: #  define PetscCosScalar(a)     cos(a)

 86: #  if defined(PETSC_USE_SINGLE)
 87:   typedef float PetscScalar;
 88: #  elif defined(PETSC_USE_LONG_DOUBLE)
 89:   typedef long double PetscScalar;
 90: #  elif defined(PETSC_USE_INT)
 91:   typedef int PetscScalar;
 92: #  else
 93:   typedef double PetscScalar;
 94: #  endif
 95: #endif

 97: #if defined(PETSC_USE_SINGLE)
 98: #  define MPIU_REAL   MPI_FLOAT
 99: #elif defined(PETSC_USE_LONG_DOUBLE)
100: #  define MPIU_REAL   MPI_LONG_DOUBLE
101: #elif defined(PETSC_USE_INT)
102: #  define MPIU_REAL   MPI_INT
103: #else
104: #  define MPIU_REAL   MPI_DOUBLE
105: #endif

107: #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
108: #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
109: /*
110:        Allows compiling PETSc so that matrix values are stored in 
111:    single precision but all other objects still use double
112:    precision. This does not work for complex numbers in that case
113:    it remains double

115:           EXPERIMENTAL! NOT YET COMPLETELY WORKING
116: */

118: #if defined(PETSC_USE_MAT_SINGLE)
119: typedef float MatScalar;
120: #else
121: typedef PetscScalar MatScalar;
122: #endif

124: #if defined(PETSC_USE_SINGLE)
125:   typedef float PetscReal;
126: #elif defined(PETSC_USE_LONG_DOUBLE)
127:   typedef long double PetscReal;
128: #elif defined(PETSC_USE_INT)
129:   typedef int PetscReal;
130: #else 
131:   typedef double PetscReal;
132: #endif

134: #if defined(PETSC_USE_COMPLEX)
135: typedef PetscReal MatReal;
136: #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
137: typedef float MatReal;
138: #else
139: typedef PetscReal MatReal;
140: #endif


143: /* --------------------------------------------------------------------------*/

145: /*
146:    Certain objects may be created using either single
147:   or double precision.
148: */
149: typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;

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

154: /*MC
155:    PetscMin - Returns minimum of two numbers

157:    Input Parameter:
158: +  v1 - first value to find minimum of
159: -  v2 - second value to find minimum of

161:    Synopsis:
162:    type PetscMin(type v1,type v2)

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

166:    Level: beginner


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

171: M*/
172: #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))

174: /*MC
175:    PetscMax - Returns maxium of two numbers

177:    Input Parameter:
178: +  v1 - first value to find maximum of
179: -  v2 - second value to find maximum of

181:    Synopsis:
182:    type max PetscMax(type v1,type v2)

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

186:    Level: beginner

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

190: M*/
191: #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))

193: /*MC
194:    PetscAbsInt - Returns the absolute value of an integer

196:    Input Parameter:
197: .   v1 - the integer

199:    Synopsis:
200:    int abs PetscAbsInt(int v1)


203:    Level: beginner

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

207: M*/
208: #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))

210: /*MC
211:    PetscAbsReal - Returns the absolute value of an real number

213:    Input Parameter:
214: .   v1 - the double 

216:    Synopsis:
217:    int abs PetscAbsReal(PetscReal v1)


220:    Level: beginner

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

224: M*/
225: #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))

227: /*MC
228:    PetscSqr - Returns the square of a number

230:    Input Parameter:
231: .   v1 - the value

233:    Synopsis:
234:    type sqr PetscSqr(type v1)

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

238:    Level: beginner

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

242: M*/
243: #define PetscSqr(a)     ((a)*(a))

245: /* ----------------------------------------------------------------------------*/
246: /*
247:      Basic constants - These should be done much better
248: */
249: #define PETSC_PI                 3.14159265358979323846264
250: #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
251: #define PETSC_MAX_INT            1000000000
252: #define PETSC_MIN_INT            -1000000000

254: #if defined(PETSC_USE_SINGLE)
255: #  define PETSC_MAX                     1.e30
256: #  define PETSC_MIN                    -1.e30
257: #  define PETSC_MACHINE_EPSILON         1.e-7
258: #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
259: #  define PETSC_SMALL                   1.e-5
260: #elif defined(PETSC_USE_INT)
261: #  define PETSC_MAX                     PETSC_MAX_INT
262: #  define PETSC_MIN                     PETSC_MIN_INT
263: #  define PETSC_MACHINE_EPSILON         1
264: #  define PETSC_SQRT_MACHINE_EPSILON    1
265: #  define PETSC_SMALL                   0
266: #else
267: #  define PETSC_MAX                     1.e300
268: #  define PETSC_MIN                    -1.e300
269: #  define PETSC_MACHINE_EPSILON         1.e-14
270: #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
271: #  define PETSC_SMALL                   1.e-10
272: #endif

274: EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
275: EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
276: EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);


279: /* ----------------------------------------------------------------------------*/
280: /*
281:     PetscLogDouble variables are used to contain double precision numbers
282:   that are not used in the numerical computations, but rather in logging,
283:   timing etc.
284: */
285: typedef double PetscLogDouble;
286: #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE

288: #define PassiveReal   PetscReal
289: #define PassiveScalar PetscScalar

291: #define PETSCMAP1_a(a,b)  a ## _ ## b
292: #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
293: #define PETSCMAP1(a)      PETSCMAP1_b(a,PetscScalar)

296: #endif