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