xref: /petsc/include/petscmath.h (revision b2863d3a52358f7ee22ca3a1f84f250141973e94)
1 /* $Id: petscmath.h,v 1.18 2000/01/11 21:04:04 bsmith 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 #define Scalar             std::complex<double>
69 #elif defined(PETSC_HAVE_TEMPLATED_COMPLEX)
70 #define Scalar             complex<double>
71 #else
72 #define Scalar             complex
73 #endif
74 
75 /* Compiling for real numbers only */
76 #else
77 #define MPIU_SCALAR           MPI_DOUBLE
78 #if defined(PETSC_USE_MAT_SINGLE)
79 #define MPIU_MATSCALAR        MPI_FLOAT
80 #else
81 #define MPIU_MATSCALAR        MPI_DOUBLE
82 #endif
83 #define PetscRealPart(a)      (a)
84 #define PetscImaginaryPart(a) (a)
85 #define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
86 #define Scalar                double
87 #define PetscConj(a)          (a)
88 #define PetscSqrtScalar(a)    sqrt(a)
89 #define PetscPowScalar(a,b)   pow(a,b)
90 #define PetscExpScalar(a)     exp(a)
91 #define PetscSinScalar(a)     sin(a)
92 #define PetscCosScalar(a)     cos(a)
93 #endif
94 
95 /*
96        Allows compiling PETSc so that matrix values are stored in
97    single precision but all other objects still use double
98    precision. This does not work for complex numbers in that case
99    it remains double
100 
101           EXPERIMENTAL! NOT YET COMPLETELY WORKING
102 */
103 #if defined(PETSC_USE_COMPLEX)
104 
105 #define MatScalar Scalar
106 #define MatReal   double
107 
108 #elif defined(PETSC_USE_MAT_SINGLE)
109 
110 #define MatScalar float
111 #define MatReal   float
112 
113 #else
114 
115 #define MatScalar Scalar
116 #define MatReal   double
117 
118 #endif
119 
120 #if defined(PETSC_USE_SINGLE)
121 #define PetscReal float
122 #else
123 #define PetscReal double
124 #endif
125 
126 /* --------------------------------------------------------------------------*/
127 
128 /*
129    Certain objects may be created using either single
130   or double precision.
131 */
132 typedef enum { SCALAR_DOUBLE,SCALAR_SINGLE } ScalarPrecision;
133 
134 /* PETSC_i is the imaginary number, i */
135 extern  Scalar            PETSC_i;
136 
137 #define PetscMin(a,b)      (((a)<(b)) ? (a) : (b))
138 #define PetscMax(a,b)      (((a)<(b)) ? (b) : (a))
139 #define PetscAbsInt(a)     (((a)<0)   ? -(a) : (a))
140 #define PetscAbsDouble(a)  (((a)<0)   ? -(a) : (a))
141 
142 /* ----------------------------------------------------------------------------*/
143 /*
144      Basic constants
145 */
146 #define PETSC_PI                 3.14159265358979323846264
147 #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
148 #define PETSC_MAX                1.e300
149 #define PETSC_MIN                -1.e300
150 #define PETSC_MAX_INT            1000000000;
151 #define PETSC_MIN_INT            -1000000000;
152 
153 /* ----------------------------------------------------------------------------*/
154 /*
155     PLogDouble variables are used to contain double precision numbers
156   that are not used in the numerical computations, but rather in logging,
157   timing etc.
158 */
159 typedef double PLogDouble;
160 /*
161       Once PETSc is compiling with a ADIC enhanced version of MPI
162    we will create a new MPI_Datatype for the inactive double variables.
163 */
164 #if defined(AD_DERIV_H)
165 /* extern  MPI_Datatype  MPIU_PLOGDOUBLE; */
166 #else
167 #if !defined(USING_MPIUNI)
168 #define MPIU_PLOGDOUBLE MPI_DOUBLE
169 #endif
170 #endif
171 
172 
173 #endif
174