xref: /petsc/include/petscmath.h (revision 329f5518e9d4bb7ce96c0c5576cc53785c973973)
1 /* $Id: petscmath.h,v 1.17 1999/09/27 21:33:07 bsmith Exp bsmith $ */
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.
7 
8     This file is included by petsc.h and should not be used directly.
9 
10 */
11 
12 #if !defined(__PETSCMATH_H)
13 #define __PETSCMATH_H
14 #include <math.h>
15 
16 /*
17 
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    Scalar which is either always a double or a complex.
22 
23 */
24 #if defined(PETSC_USE_COMPLEX)
25 
26 #if defined (PETSC_HAVE_STD_COMPLEX)
27 #include <complex>
28 #elif defined(PETSC_HAVE_NONSTANDARD_COMPLEX_H)
29 #include PETSC_HAVE_NONSTANDARD_COMPLEX_H
30 #else
31 #include <complex.h>
32 #endif
33 
34 extern  MPI_Datatype        MPIU_COMPLEX;
35 #define MPIU_SCALAR         MPIU_COMPLEX
36 #if defined (PETSC_HAVE_STD_COMPLEX)
37 #define PetscRealPart(a)        (a).real()
38 #define PetscImaginaryPart(a)   (a).imag()
39 #define PetscAbsScalar(a)   std::abs(a)
40 #define PetscConj(a)        std::conj(a)
41 #define PetscSqrtScalar(a)  std::sqrt(a)
42 #define PetscPowScalar(a,b) std::pow(a,b)
43 #define PetscExpScalar(a)   std::exp(a)
44 #define PetscSinScalar(a)   std::sin(a)
45 #define PetscCosScalar(a)   std::cos(a)
46 #else
47 #define PetscRealPart(a)        real(a)
48 #define PetscImaginaryPart(a)   imag(a)
49 #define PetscAbsScalar(a)   abs(a)
50 #define PetscConj(a)        conj(a)
51 #define PetscSqrtScalar(a)  sqrt(a)
52 #define PetscPowScalar(a,b) pow(a,b)
53 #define PetscExpScalar(a)   exp(a)
54 #define PetscSinScalar(a)   sin(a)
55 #define PetscCosScalar(a)   cos(a)
56 #endif
57 /*
58   The new complex class for GNU C++ is based on templates and is not backward
59   compatible with all previous complex class libraries.
60 */
61 #if defined(PETSC_HAVE_STD_COMPLEX)
62 #define Scalar             std::complex<double>
63 #elif defined(PETSC_HAVE_TEMPLATED_COMPLEX)
64 #define Scalar             complex<double>
65 #else
66 #define Scalar             complex
67 #endif
68 
69 /* Compiling for real numbers only */
70 #else
71 #define MPIU_SCALAR           MPI_DOUBLE
72 #define PetscRealPart(a)      (a)
73 #define PetscImaginaryPart(a) (a)
74 #define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
75 #define Scalar                double
76 #define PetscConj(a)          (a)
77 #define PetscSqrtScalar(a)    sqrt(a)
78 #define PetscPowScalar(a,b)   pow(a,b)
79 #define PetscExpScalar(a)     exp(a)
80 #define PetscSinScalar(a)     sin(a)
81 #define PetscCosScalar(a)     cos(a)
82 #endif
83 
84 /*
85        Allows compiling PETSc so that matrix values are stored in
86    single precision but all other objects still use double
87    precision. This does not work for complex numbers in that case
88    it remains double
89 
90           EXPERIMENTAL! NOT YET COMPLETELY WORKING
91 */
92 #if defined(PETSC_USE_COMPLEX)
93 
94 #define MatScalar Scalar
95 #define MatReal   double
96 
97 #elif defined(PETSC_USE_MAT_SINGLE)
98 
99 #define MatScalar float
100 #define MatReal   float
101 
102 #else
103 
104 #define MatScalar Scalar
105 #define MatReal   double
106 
107 #endif
108 
109 #if defined(PETSC_USE_SINGLE)
110 #define PetscReal float
111 #else
112 #define PetscReal double
113 #endif
114 
115 /* --------------------------------------------------------------------------*/
116 
117 /*
118    Certain objects may be created using either single
119   or double precision.
120 */
121 typedef enum { SCALAR_DOUBLE,SCALAR_SINGLE } ScalarPrecision;
122 
123 /* PETSC_i is the imaginary number, i */
124 extern  Scalar            PETSC_i;
125 
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 PetscAbsDouble(a)  (((a)<0)   ? -(a) : (a))
130 
131 /* ----------------------------------------------------------------------------*/
132 /*
133      Basic constants
134 */
135 #define PETSC_PI                 3.14159265358979323846264
136 #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
137 #define PETSC_MAX                1.e300
138 #define PETSC_MIN                -1.e300
139 #define PETSC_MAX_INT            1000000000;
140 #define PETSC_MIN_INT            -1000000000;
141 
142 /* ----------------------------------------------------------------------------*/
143 /*
144     PLogDouble variables are used to contain double precision numbers
145   that are not used in the numerical computations, but rather in logging,
146   timing etc.
147 */
148 typedef double PLogDouble;
149 /*
150       Once PETSc is compiling with a ADIC enhanced version of MPI
151    we will create a new MPI_Datatype for the inactive double variables.
152 */
153 #if defined(AD_DERIV_H)
154 /* extern  MPI_Datatype  MPIU_PLOGDOUBLE; */
155 #else
156 #if !defined(USING_MPIUNI)
157 #define MPIU_PLOGDOUBLE MPI_DOUBLE
158 #endif
159 #endif
160 
161 
162 #endif
163