xref: /petsc/include/petscmath.h (revision 87828ca270d8140797fd4271705413c4ecfcb535)
1 /* $Id: petscmath.h,v 1.24 2001/07/17 18:30:20 balay Exp bsmith $ */
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 /*
17 
18      Defines operations that are different for complex and real numbers;
19    note that one cannot really mix the use of complex and real in the same
20    PETSc program. All PETSc objects in one program are built around the object
21    Scalar which is either always a double or a complex.
22 
23 */
24 #if defined(PETSC_USE_COMPLEX)
25 
26 #if defined (PETSC_HAVE_STD_COMPLEX)
27 #include <complex>
28 #elif defined(PETSC_HAVE_NONSTANDARD_COMPLEX_H)
29 #include PETSC_HAVE_NONSTANDARD_COMPLEX_H
30 #else
31 #include <complex.h>
32 #endif
33 
34 extern  MPI_Datatype        MPIU_COMPLEX;
35 #define MPIU_SCALAR         MPIU_COMPLEX
36 #if defined(PETSC_USE_MAT_SINGLE)
37 #define MPIU_MATSCALAR        ??Notdone
38 #else
39 #define MPIU_MATSCALAR      MPIU_COMPLEX
40 #endif
41 
42 #if defined (PETSC_HAVE_STD_COMPLEX)
43 #define PetscRealPart(a)        (a).real()
44 #define PetscImaginaryPart(a)   (a).imag()
45 #define PetscAbsScalar(a)   std::abs(a)
46 #define PetscConj(a)        std::conj(a)
47 #define PetscSqrtScalar(a)  std::sqrt(a)
48 #define PetscPowScalar(a,b) std::pow(a,b)
49 #define PetscExpScalar(a)   std::exp(a)
50 #define PetscSinScalar(a)   std::sin(a)
51 #define PetscCosScalar(a)   std::cos(a)
52 #else
53 #define PetscRealPart(a)        real(a)
54 #define PetscImaginaryPart(a)   imag(a)
55 #define PetscAbsScalar(a)   abs(a)
56 #define PetscConj(a)        conj(a)
57 #define PetscSqrtScalar(a)  sqrt(a)
58 #define PetscPowScalar(a,b) pow(a,b)
59 #define PetscExpScalar(a)   exp(a)
60 #define PetscSinScalar(a)   sin(a)
61 #define PetscCosScalar(a)   cos(a)
62 #endif
63 /*
64   The new complex class for GNU C++ is based on templates and is not backward
65   compatible with all previous complex class libraries.
66 */
67 #if defined(PETSC_HAVE_STD_COMPLEX)
68   typedef std::complex<double> Scalar;
69 #elif defined(PETSC_HAVE_TEMPLATED_COMPLEX)
70   typedef complex<double> Scalar;
71 #else
72   typedef complex Scalar;
73 #endif
74 
75 /* Compiling for real numbers only */
76 #else
77 #  if defined(PETSC_USE_SINGLE)
78 #    define MPIU_SCALAR           MPI_FLOAT
79 #  else
80 #    define MPIU_SCALAR           MPI_DOUBLE
81 #  endif
82 #  if defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
83 #    define MPIU_MATSCALAR        MPI_FLOAT
84 #  else
85 #    define MPIU_MATSCALAR        MPI_DOUBLE
86 #  endif
87 #  define PetscRealPart(a)      (a)
88 #  define PetscImaginaryPart(a) (a)
89 #  define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
90 #  define PetscConj(a)          (a)
91 #  define PetscSqrtScalar(a)    sqrt(a)
92 #  define PetscPowScalar(a,b)   pow(a,b)
93 #  define PetscExpScalar(a)     exp(a)
94 #  define PetscSinScalar(a)     sin(a)
95 #  define PetscCosScalar(a)     cos(a)
96 
97 #  if defined(PETSC_USE_SINGLE)
98   typedef float Scalar;
99 #  else
100   typedef double Scalar;
101 #  endif
102 #endif
103 
104 /*
105        Allows compiling PETSc so that matrix values are stored in
106    single precision but all other objects still use double
107    precision. This does not work for complex numbers in that case
108    it remains double
109 
110           EXPERIMENTAL! NOT YET COMPLETELY WORKING
111 */
112 #if defined(PETSC_USE_COMPLEX)
113 
114 typedef Scalar MatScalar;
115 typedef double MatReal;
116 
117 #elif defined(PETSC_USE_MAT_SINGLE) || defined(PETSC_USE_SINGLE)
118 
119 typedef float MatScalar;
120 typedef float MatReal;
121 
122 #else
123 
124 typedef Scalar MatScalar;
125 typedef double MatReal;
126 
127 #endif
128 
129 #if defined(PETSC_USE_SINGLE)
130   typedef float PetscReal;
131 #else
132   typedef double PetscReal;
133 #endif
134 
135 /* --------------------------------------------------------------------------*/
136 
137 /*
138    Certain objects may be created using either single
139   or double precision.
140 */
141 typedef enum { SCALAR_DOUBLE,SCALAR_SINGLE } ScalarPrecision;
142 
143 /* PETSC_i is the imaginary number, i */
144 extern  Scalar            PETSC_i;
145 
146 #define PetscMin(a,b)      (((a)<(b)) ? (a) : (b))
147 #define PetscMax(a,b)      (((a)<(b)) ? (b) : (a))
148 #define PetscAbsInt(a)     (((a)<0)   ? -(a) : (a))
149 #define PetscAbsDouble(a)  (((a)<0)   ? -(a) : (a))
150 
151 /* ----------------------------------------------------------------------------*/
152 /*
153      Basic constants
154 */
155 #define PETSC_PI                 3.14159265358979323846264
156 #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
157 #define PETSC_MAX                1.e300
158 #define PETSC_MIN                -1.e300
159 #define PETSC_MAX_INT            1000000000;
160 #define PETSC_MIN_INT            -1000000000;
161 
162 /* ----------------------------------------------------------------------------*/
163 /*
164     PetscLogDouble variables are used to contain double precision numbers
165   that are not used in the numerical computations, but rather in logging,
166   timing etc.
167 */
168 typedef double PetscLogDouble;
169 /*
170       Once PETSc is compiling with a ADIC enhanced version of MPI
171    we will create a new MPI_Datatype for the inactive double variables.
172 */
173 #if defined(AD_DERIV_H)
174 /* extern  MPI_Datatype  MPIU_PETSCLOGDOUBLE; */
175 #else
176 #if !defined(USING_MPIUNI)
177 #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
178 #endif
179 #endif
180 
181 #define PassiveReal PetscReal
182 #define PassiveScalar Scalar
183 
184 #define PETSCMAP1_a(a,b)  a ## _ ## b
185 #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
186 #define PETSCMAP1(a)      PETSCMAP1_b(a,PetscScalar)
187 #endif
188