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