xref: /petsc/include/petscmath.h (revision 5441df8ea7fc15821ed1fa0916d70c7c3774aeba)
1 /* $Id: petscmath.h,v 1.32 2001/08/30 20:37:06 bsmith Exp $ */
2 /*
3 
4       PETSc mathematics include file. Defines certain basic mathematical
5     constants and functions for working with single and double precision
6     floating point numbers as well as complex and integers.
7 
8     This file is included by petsc.h and should not be used directly.
9 
10 */
11 
12 #if !defined(__PETSCMATH_H)
13 #define __PETSCMATH_H
14 #include <math.h>
15 
16 extern  MPI_Datatype        MPIU_2SCALAR;
17 /*
18 
19      Defines operations that are different for complex and real numbers;
20    note that one cannot really mix the use of complex and real in the same
21    PETSc program. All PETSc objects in one program are built around the object
22    PetscScalar which is either always a double or a complex.
23 
24 */
25 
26 #if defined(PETSC_USE_COMPLEX)
27 
28 /*
29    PETSc now only supports std::complex
30 */
31 #include <complex>
32 
33 extern  MPI_Datatype        MPIU_COMPLEX;
34 #define MPIU_SCALAR         MPIU_COMPLEX
35 #if defined(PETSC_USE_MAT_SINGLE)
36 #define MPIU_MATSCALAR        ??Notdone
37 #else
38 #define MPIU_MATSCALAR      MPIU_COMPLEX
39 #endif
40 
41 #define PetscRealPart(a)        (a).real()
42 #define PetscImaginaryPart(a)   (a).imag()
43 #define PetscAbsScalar(a)   std::abs(a)
44 #define PetscConj(a)        std::conj(a)
45 #define PetscSqrtScalar(a)  std::sqrt(a)
46 #define PetscPowScalar(a,b) std::pow(a,b)
47 #define PetscExpScalar(a)   std::exp(a)
48 #define PetscSinScalar(a)   std::sin(a)
49 #define PetscCosScalar(a)   std::cos(a)
50 
51 typedef std::complex<double> PetscScalar;
52 
53 /* Compiling for real numbers only */
54 #else
55 #  if defined(PETSC_USE_SINGLE)
56 #    define MPIU_SCALAR           MPI_FLOAT
57 #  else
58 #    define MPIU_SCALAR           MPI_DOUBLE
59 #  endif
60 #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
61 #    define MPIU_MATSCALAR        MPI_FLOAT
62 #  else
63 #    define MPIU_MATSCALAR        MPI_DOUBLE
64 #  endif
65 #  define PetscRealPart(a)      (a)
66 #  define PetscImaginaryPart(a) (a)
67 #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
68 #  define PetscConj(a)          (a)
69 #  define PetscSqrtScalar(a)    sqrt(a)
70 #  define PetscPowScalar(a,b)   pow(a,b)
71 #  define PetscExpScalar(a)     exp(a)
72 #  define PetscSinScalar(a)     sin(a)
73 #  define PetscCosScalar(a)     cos(a)
74 
75 #  if defined(PETSC_USE_SINGLE)
76   typedef float PetscScalar;
77 #  else
78   typedef double PetscScalar;
79 #  endif
80 #endif
81 
82 #if defined(PETSC_USE_SINGLE)
83 #  define MPIU_REAL   MPI_FLOAT
84 #else
85 #  define MPIU_REAL   MPI_DOUBLE
86 #endif
87 
88 #define PetscSign(a) (((a) >= 0) ? ((a) == 0 ? 0 : 1) : -1)
89 #define PetscAbs(a)  (((a) >= 0) ? a : -a)
90 /*
91        Allows compiling PETSc so that matrix values are stored in
92    single precision but all other objects still use double
93    precision. This does not work for complex numbers in that case
94    it remains double
95 
96           EXPERIMENTAL! NOT YET COMPLETELY WORKING
97 */
98 
99 #if defined(PETSC_USE_MAT_SINGLE)
100 typedef float MatScalar;
101 #else
102 typedef PetscScalar MatScalar;
103 #endif
104 
105 #if defined(PETSC_USE_COMPLEX)
106 typedef double MatReal;
107 #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
108 typedef float MatReal;
109 #else
110 typedef double MatReal;
111 #endif
112 
113 #if defined(PETSC_USE_SINGLE)
114   typedef float PetscReal;
115 #else
116   typedef double PetscReal;
117 #endif
118 
119 /* --------------------------------------------------------------------------*/
120 
121 /*
122    Certain objects may be created using either single
123   or double precision.
124 */
125 typedef enum { PETSC_SCALAR_DOUBLE,PETSC_SCALAR_SINGLE } PetscScalarPrecision;
126 
127 /* PETSC_i is the imaginary number, i */
128 extern  PetscScalar       PETSC_i;
129 
130 /*MC
131    PetscMin - Returns minimum of two numbers
132 
133    Input Parameter:
134 +  v1 - first value to find minimum of
135 -  v2 - second value to find minimum of
136 
137    Synopsis:
138    type PetscMin(type v1,type v2)
139 
140    Notes: type can be integer or floating point value
141 
142    Level: beginner
143 
144 
145 .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
146 
147 M*/
148 #define PetscMin(a,b)   (((a)<(b)) ?  (a) : (b))
149 
150 /*MC
151    PetscMax - Returns maxium of two numbers
152 
153    Input Parameter:
154 +  v1 - first value to find maximum of
155 -  v2 - second value to find maximum of
156 
157    Synopsis:
158    type max PetscMax(type v1,type v2)
159 
160    Notes: type can be integer or floating point value
161 
162    Level: beginner
163 
164 .seealso: PetscMin(), PetscAbsInt(), PetscAbsReal(), PetscSqr()
165 
166 M*/
167 #define PetscMax(a,b)   (((a)<(b)) ?  (b) : (a))
168 
169 /*MC
170    PetscAbsInt - Returns the absolute value of an integer
171 
172    Input Parameter:
173 .   v1 - the integer
174 
175    Synopsis:
176    int abs PetscAbsInt(int v1)
177 
178 
179    Level: beginner
180 
181 .seealso: PetscMax(), PetscMin(), PetscAbsReal(), PetscSqr()
182 
183 M*/
184 #define PetscAbsInt(a)  (((a)<0)   ? -(a) : (a))
185 
186 /*MC
187    PetscAbsReal - Returns the absolute value of an real number
188 
189    Input Parameter:
190 .   v1 - the double
191 
192    Synopsis:
193    int abs PetscAbsReal(PetscReal v1)
194 
195 
196    Level: beginner
197 
198 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscSqr()
199 
200 M*/
201 #define PetscAbsReal(a) (((a)<0)   ? -(a) : (a))
202 
203 /*MC
204    PetscSqr - Returns the square of a number
205 
206    Input Parameter:
207 .   v1 - the value
208 
209    Synopsis:
210    type sqr PetscSqr(type v1)
211 
212    Notes: type can be integer or floating point value
213 
214    Level: beginner
215 
216 .seealso: PetscMax(), PetscMin(), PetscAbsInt(), PetscAbsReal()
217 
218 M*/
219 #define PetscSqr(a)     ((a)*(a))
220 
221 /* ----------------------------------------------------------------------------*/
222 /*
223      Basic constants
224 */
225 #define PETSC_PI                 3.14159265358979323846264
226 #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
227 #define PETSC_MAX_INT            1000000000
228 #define PETSC_MIN_INT            -1000000000
229 
230 #if defined(PETSC_USE_SINGLE)
231 #  define PETSC_MAX                1.e30
232 #  define PETSC_MIN                -1.e30
233 #  define PETSC_MACHINE_EPSILON         1.e-7
234 #  define PETSC_SQRT_MACHINE_EPSILON    3.e-4
235 #  define PETSC_SMALL                   1.e-5
236 #else
237 #  define PETSC_MAX                1.e300
238 #  define PETSC_MIN                -1.e300
239 #  define PETSC_MACHINE_EPSILON         1.e-14
240 #  define PETSC_SQRT_MACHINE_EPSILON    1.e-7
241 #  define PETSC_SMALL                   1.e-10
242 #endif
243 
244 extern int PetscGlobalMax(PetscReal*,PetscReal*,MPI_Comm);
245 extern int PetscGlobalMin(PetscReal*,PetscReal*,MPI_Comm);
246 extern int PetscGlobalSum(PetscScalar*,PetscScalar*,MPI_Comm);
247 
248 
249 /* ----------------------------------------------------------------------------*/
250 /*
251     PetscLogDouble variables are used to contain double precision numbers
252   that are not used in the numerical computations, but rather in logging,
253   timing etc.
254 */
255 typedef double PetscLogDouble;
256 /*
257       Once PETSc is compiling with a ADIC enhanced version of MPI
258    we will create a new MPI_Datatype for the inactive double variables.
259 */
260 #if defined(AD_DERIV_H)
261 /* extern  MPI_Datatype  MPIU_PETSCLOGDOUBLE; */
262 #else
263 #if !defined(_petsc_mpi_uni)
264 #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
265 #endif
266 #endif
267 
268 #define PassiveReal   PetscReal
269 #define PassiveScalar PetscScalar
270 
271 #define PETSCMAP1_a(a,b)  a ## _ ## b
272 #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
273 #define PETSCMAP1(a)      PETSCMAP1_b(a,PetscScalar)
274 #endif
275