xref: /petsc/include/petscmath.h (revision 292f8084fb157dadf9a2ae26c5bd14368ed7ffcb)
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.
6 
7     This file is included by petsc.h and should not be used directly.
8 
9 */
10 
11 #if !defined(__PETSCMATH_H)
12 #define __PETSCMATH_H
13 #include <math.h>
14 PETSC_EXTERN_CXX_BEGIN
15 
16 extern  MPI_Datatype        MPIU_2SCALAR;
17 /*
18 
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.
23 
24 */
25 
26 #define PetscExpPassiveScalar(a) PetscExpScalar()
27 
28 #if defined(PETSC_USE_COMPLEX)
29 
30 /*
31    PETSc now only supports std::complex
32 */
33 #include <complex>
34 
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
42 
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)
52 
53 typedef std::complex<double> PetscScalar;
54 
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)
76 
77 #  if defined(PETSC_USE_SINGLE)
78   typedef float PetscScalar;
79 #  else
80   typedef double PetscScalar;
81 #  endif
82 #endif
83 
84 #if defined(PETSC_USE_SINGLE)
85 #  define MPIU_REAL   MPI_FLOAT
86 #else
87 #  define MPIU_REAL   MPI_DOUBLE
88 #endif
89 
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
97 
98           EXPERIMENTAL! NOT YET COMPLETELY WORKING
99 */
100 
101 #if defined(PETSC_USE_MAT_SINGLE)
102 typedef float MatScalar;
103 #else
104 typedef PetscScalar MatScalar;
105 #endif
106 
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
114 
115 #if defined(PETSC_USE_SINGLE)
116   typedef float PetscReal;
117 #else
118   typedef double PetscReal;
119 #endif
120 
121 /* --------------------------------------------------------------------------*/
122 
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;
128 
129 /* PETSC_i is the imaginary number, i */
130 extern  PetscScalar       PETSC_i;
131 
132 /*MC
133    PetscMin - Returns minimum of two numbers
134 
135    Input Parameter:
136 +  v1 - first value to find minimum of
137 -  v2 - second value to find minimum of
138 
139    Synopsis:
140    type PetscMin(type v1,type v2)
141 
142    Notes: type can be integer or floating point value
143 
144    Level: beginner
145 
146 
147 .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
148 
149 M*/
150 #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
151 
152 /*MC
153    PetscMax - Returns maxium of two numbers
154 
155    Input Parameter:
156 +  v1 - first value to find maximum of
157 -  v2 - second value to find maximum of
158 
159    Synopsis:
160    type max PetscMax(type v1,type v2)
161 
162    Notes: type can be integer or floating point value
163 
164    Level: beginner
165 
166 .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
167 
168 M*/
169 #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
170 
171 /*MC
172    PetscAbsInt - Returns the absolute value of an integer
173 
174    Input Parameter:
175 .   v1 - the integer
176 
177    Synopsis:
178    int abs PetscAbsInt(int v1)
179 
180 
181    Level: beginner
182 
183 .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
184 
185 M*/
186 #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
187 
188 /*MC
189    PetscAbsReal - Returns the absolute value of an real number
190 
191    Input Parameter:
192 .   v1 - the double
193 
194    Synopsis:
195    int abs PetscAbsReal(PetscReal v1)
196 
197 
198    Level: beginner
199 
200 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
201 
202 M*/
203 #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
204 
205 /*MC
206    PetscSqr - Returns the square of a number
207 
208    Input Parameter:
209 .   v1 - the value
210 
211    Synopsis:
212    type sqr PetscSqr(type v1)
213 
214    Notes: type can be integer or floating point value
215 
216    Level: beginner
217 
218 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
219 
220 M*/
221 #define PetscSqr(a)     ((a)*(a))
222 
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
231 
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
245 
246 EXTERN PetscErrorCode PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
247 EXTERN PetscErrorCode PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
248 EXTERN PetscErrorCode PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);
249 
250 
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
269 
270 #define PassiveReal   PetscReal
271 #define PassiveScalar PetscScalar
272 
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 
277 PETSC_EXTERN_CXX_END
278 #endif
279