xref: /petsc/include/petscmath.h (revision b7940d39e72cbe7ff256b0ca6984470bcc79098c)
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 PETSC_DLLEXPORT MPIU_2SCALAR;
17 extern  MPI_Datatype PETSC_DLLEXPORT MPIU_2INT;
18 /*
19 
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.
24 
25 */
26 
27 #define PetscExpPassiveScalar(a) PetscExpScalar()
28 
29 #if defined(PETSC_USE_COMPLEX)
30 #if defined(PETSC_CLANGUAGE_CXX)
31 /*
32    C++ support of complex numbers: Original support
33 */
34 #include <complex>
35 
36 extern  MPI_Datatype PETSC_DLLEXPORT MPIU_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
43 
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)
53 
54 typedef std::complex<double> PetscScalar;
55 #else
56 #include <complex.h>
57 
58 /*
59    C support of complex numbers: Warning it needs a
60    C90 compliant compiler to work...
61  */
62 
63 extern  MPI_Datatype PETSC_DLLEXPORT MPIU_COMPLEX;
64 #define MPIU_SCALAR         MPIU_COMPLEX
65 #if defined(PETSC_USE_MAT_SINGLE)
66 #define MPIU_MATSCALAR        ??Notdone
67 #else
68 #define MPIU_MATSCALAR      MPIU_COMPLEX
69 #endif
70 
71 #define PetscRealPart(a)        creal(a)
72 #define PetscImaginaryPart(a)   cimag(a)
73 #define PetscAbsScalar(a)   cabs(a)
74 #define PetscConj(a)        conj(a)
75 #define PetscSqrtScalar(a)  csqrt(a)
76 #define PetscPowScalar(a,b) cpow(a,b)
77 #define PetscExpScalar(a)   cexp(a)
78 #define PetscSinScalar(a)   csin(a)
79 #define PetscCosScalar(a)   ccos(a)
80 
81 typedef double complex PetscScalar;
82 #endif
83 
84 /* Compiling for real numbers only */
85 #else
86 #  if defined(PETSC_USE_SINGLE)
87 #    define MPIU_SCALAR           MPI_FLOAT
88 #  elif defined(PETSC_USE_LONG_DOUBLE)
89 #    define MPIU_SCALAR           MPI_LONG_DOUBLE
90 #  elif defined(PETSC_INT)
91 #    define MPIU_INT              MPI_INT
92 #  else
93 #    define MPIU_SCALAR           MPI_DOUBLE
94 #  endif
95 #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
96 #    define MPIU_MATSCALAR        MPI_FLOAT
97 #  elif defined(PETSC_USE_LONG_DOUBLE)
98 #    define MPIU_MATSCALAR        MPI_LONG_DOUBLE
99 #  elif defined(PETSC_USE_INT)
100 #    define MPIU_MATSCALAR        MPI_INT
101 #  else
102 #    define MPIU_MATSCALAR        MPI_DOUBLE
103 #  endif
104 #  define PetscRealPart(a)      (a)
105 #  define PetscImaginaryPart(a) (0)
106 #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
107 #  define PetscConj(a)          (a)
108 #  define PetscSqrtScalar(a)    sqrt(a)
109 #  define PetscPowScalar(a,b)   pow(a,b)
110 #  define PetscExpScalar(a)     exp(a)
111 #  define PetscSinScalar(a)     sin(a)
112 #  define PetscCosScalar(a)     cos(a)
113 
114 #  if defined(PETSC_USE_SINGLE)
115   typedef float PetscScalar;
116 #  elif defined(PETSC_USE_LONG_DOUBLE)
117   typedef long double PetscScalar;
118 #  elif defined(PETSC_USE_INT)
119   typedef int PetscScalar;
120 #  else
121   typedef double PetscScalar;
122 #  endif
123 #endif
124 
125 #if defined(PETSC_USE_SINGLE)
126 #  define MPIU_REAL   MPI_FLOAT
127 #elif defined(PETSC_USE_LONG_DOUBLE)
128 #  define MPIU_REAL   MPI_LONG_DOUBLE
129 #elif defined(PETSC_USE_INT)
130 #  define MPIU_REAL   MPI_INT
131 #else
132 #  define MPIU_REAL   MPI_DOUBLE
133 #endif
134 
135 #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
136 #define PetscAbs(a)  (((a) >= 0) ? (a) : -(a))
137 /*
138        Allows compiling PETSc so that matrix values are stored in
139    single precision but all other objects still use double
140    precision. This does not work for complex numbers in that case
141    it remains double
142 
143           EXPERIMENTAL! NOT YET COMPLETELY WORKING
144 */
145 
146 #if defined(PETSC_USE_MAT_SINGLE)
147 typedef float MatScalar;
148 #else
149 typedef PetscScalar MatScalar;
150 #endif
151 
152 #if defined(PETSC_USE_SINGLE)
153   typedef float PetscReal;
154 #elif defined(PETSC_USE_LONG_DOUBLE)
155   typedef long double PetscReal;
156 #elif defined(PETSC_USE_INT)
157   typedef int PetscReal;
158 #else
159   typedef double PetscReal;
160 #endif
161 
162 #if defined(PETSC_USE_COMPLEX)
163 typedef PetscReal MatReal;
164 #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
165 typedef float MatReal;
166 #else
167 typedef PetscReal MatReal;
168 #endif
169 
170 
171 /* --------------------------------------------------------------------------*/
172 
173 /*
174    Certain objects may be created using either single
175   or double precision.
176 */
177 typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE, PETSC_SCALAR_LONG_DOUBLE } PetscScalarPrecision;
178 
179 /* PETSC_i is the imaginary number, i */
180 extern  PetscScalar PETSC_DLLEXPORT PETSC_i;
181 
182 /*MC
183    PetscMin - Returns minimum of two numbers
184 
185    Input Parameter:
186 +  v1 - first value to find minimum of
187 -  v2 - second value to find minimum of
188 
189    Synopsis:
190    type PetscMin(type v1,type v2)
191 
192    Notes: type can be integer or floating point value
193 
194    Level: beginner
195 
196 
197 .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
198 
199 M*/
200 #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
201 
202 /*MC
203    PetscMax - Returns maxium of two numbers
204 
205    Input Parameter:
206 +  v1 - first value to find maximum of
207 -  v2 - second value to find maximum of
208 
209    Synopsis:
210    type max PetscMax(type v1,type v2)
211 
212    Notes: type can be integer or floating point value
213 
214    Level: beginner
215 
216 .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
217 
218 M*/
219 #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
220 
221 /*MC
222    PetscAbsInt - Returns the absolute value of an integer
223 
224    Input Parameter:
225 .   v1 - the integer
226 
227    Synopsis:
228    int abs PetscAbsInt(int v1)
229 
230 
231    Level: beginner
232 
233 .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
234 
235 M*/
236 #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
237 
238 /*MC
239    PetscAbsReal - Returns the absolute value of an real number
240 
241    Input Parameter:
242 .   v1 - the double
243 
244    Synopsis:
245    int abs PetscAbsReal(PetscReal v1)
246 
247 
248    Level: beginner
249 
250 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
251 
252 M*/
253 #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
254 
255 /*MC
256    PetscSqr - Returns the square of a number
257 
258    Input Parameter:
259 .   v1 - the value
260 
261    Synopsis:
262    type sqr PetscSqr(type v1)
263 
264    Notes: type can be integer or floating point value
265 
266    Level: beginner
267 
268 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
269 
270 M*/
271 #define PetscSqr(a)     ((a)*(a))
272 
273 /* ----------------------------------------------------------------------------*/
274 /*
275      Basic constants - These should be done much better
276 */
277 #define PETSC_PI                 3.14159265358979323846264
278 #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
279 #define PETSC_MAX_INT            1000000000
280 #define PETSC_MIN_INT            -1000000000
281 
282 #if defined(PETSC_USE_SINGLE)
283 #  define PETSC_MAX                     1.e30
284 #  define PETSC_MIN                    -1.e30
285 #  define PETSC_MACHINE_EPSILON         1.e-7
286 #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
287 #  define PETSC_SMALL                   1.e-5
288 #elif defined(PETSC_USE_INT)
289 #  define PETSC_MAX                     PETSC_MAX_INT
290 #  define PETSC_MIN                     PETSC_MIN_INT
291 #  define PETSC_MACHINE_EPSILON         1
292 #  define PETSC_SQRT_MACHINE_EPSILON    1
293 #  define PETSC_SMALL                   0
294 #else
295 #  define PETSC_MAX                     1.e300
296 #  define PETSC_MIN                    -1.e300
297 #  define PETSC_MACHINE_EPSILON         1.e-14
298 #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
299 #  define PETSC_SMALL                   1.e-10
300 #endif
301 
302 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
303 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
304 EXTERN PetscErrorCode PETSC_DLLEXPORT PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);
305 
306 
307 /* ----------------------------------------------------------------------------*/
308 /*
309     PetscLogDouble variables are used to contain double precision numbers
310   that are not used in the numerical computations, but rather in logging,
311   timing etc.
312 */
313 typedef double PetscLogDouble;
314 #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
315 
316 #define PassiveReal   PetscReal
317 #define PassiveScalar PetscScalar
318 
319 
320 PETSC_EXTERN_CXX_END
321 #endif
322