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