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