xref: /petsc/include/petscmath.h (revision 3eda883290c861c5cc5a252696a01e3c923c2eb2)
1*3eda8832SBarry Smith /* $Id: petscmath.h,v 1.18 2000/01/11 21:04:04 bsmith Exp bsmith $ */
2e489efc1SBarry Smith /*
3314da920SBarry Smith 
4314da920SBarry Smith       PETSc mathematics include file. Defines certain basic mathematical
5314da920SBarry Smith     constants and functions for working with single and double precision
6314da920SBarry Smith     floating point numbers as well as complex and integers.
7314da920SBarry Smith 
8e7029fe1SSatish Balay     This file is included by petsc.h and should not be used directly.
9e7029fe1SSatish Balay 
10e489efc1SBarry Smith */
11e489efc1SBarry Smith 
12488ecbafSBarry Smith #if !defined(__PETSCMATH_H)
13488ecbafSBarry Smith #define __PETSCMATH_H
140a5f7794SBarry Smith #include <math.h>
150a5f7794SBarry Smith 
16314da920SBarry Smith /*
17f4ccad53SBarry Smith 
18f4ccad53SBarry Smith      Defines operations that are different for complex and real numbers;
19f4ccad53SBarry Smith    note that one cannot really mix the use of complex and real in the same
20f4ccad53SBarry Smith    PETSc program. All PETSc objects in one program are built around the object
21f4ccad53SBarry Smith    Scalar which is either always a double or a complex.
22f4ccad53SBarry Smith 
23e489efc1SBarry Smith */
24aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
25adc17e78SSatish Balay 
26c80bc32dSSatish Balay #if defined (PETSC_HAVE_STD_COMPLEX)
27df9b3741SSatish Balay #include <complex>
28aa482453SBarry Smith #elif defined(PETSC_HAVE_NONSTANDARD_COMPLEX_H)
29aa482453SBarry Smith #include PETSC_HAVE_NONSTANDARD_COMPLEX_H
30adc17e78SSatish Balay #else
31adc17e78SSatish Balay #include <complex.h>
32adc17e78SSatish Balay #endif
33adc17e78SSatish Balay 
34adc17e78SSatish Balay extern  MPI_Datatype        MPIU_COMPLEX;
35adc17e78SSatish Balay #define MPIU_SCALAR         MPIU_COMPLEX
36*3eda8832SBarry Smith #if defined(PETSC_USE_MAT_SINGLE)
37*3eda8832SBarry Smith #define MPIU_MATSCALAR        ??Notdone
38*3eda8832SBarry Smith #else
39*3eda8832SBarry Smith #define MPIU_MATSCALAR      MPIU_COMPLEX
40*3eda8832SBarry Smith #endif
41*3eda8832SBarry Smith 
42c80bc32dSSatish Balay #if defined (PETSC_HAVE_STD_COMPLEX)
43329f5518SBarry Smith #define PetscRealPart(a)        (a).real()
44329f5518SBarry Smith #define PetscImaginaryPart(a)   (a).imag()
453f6de6efSSatish Balay #define PetscAbsScalar(a)   std::abs(a)
463f6de6efSSatish Balay #define PetscConj(a)        std::conj(a)
4718a7d68fSSatish Balay #define PetscSqrtScalar(a)  std::sqrt(a)
48184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b)
49184914b5SBarry Smith #define PetscExpScalar(a)   std::exp(a)
50184914b5SBarry Smith #define PetscSinScalar(a)   std::sin(a)
51184914b5SBarry Smith #define PetscCosScalar(a)   std::cos(a)
52e489efc1SBarry Smith #else
53329f5518SBarry Smith #define PetscRealPart(a)        real(a)
54329f5518SBarry Smith #define PetscImaginaryPart(a)   imag(a)
55e489efc1SBarry Smith #define PetscAbsScalar(a)   abs(a)
56e489efc1SBarry Smith #define PetscConj(a)        conj(a)
5718a7d68fSSatish Balay #define PetscSqrtScalar(a)  sqrt(a)
58184914b5SBarry Smith #define PetscPowScalar(a,b) pow(a,b)
59184914b5SBarry Smith #define PetscExpScalar(a)   exp(a)
60184914b5SBarry Smith #define PetscSinScalar(a)   sin(a)
61184914b5SBarry Smith #define PetscCosScalar(a)   cos(a)
62adc17e78SSatish Balay #endif
63e489efc1SBarry Smith /*
64e489efc1SBarry Smith   The new complex class for GNU C++ is based on templates and is not backward
65e489efc1SBarry Smith   compatible with all previous complex class libraries.
66e489efc1SBarry Smith */
67c80bc32dSSatish Balay #if defined(PETSC_HAVE_STD_COMPLEX)
68df9b3741SSatish Balay #define Scalar             std::complex<double>
69aa482453SBarry Smith #elif defined(PETSC_HAVE_TEMPLATED_COMPLEX)
70e489efc1SBarry Smith #define Scalar             complex<double>
71e489efc1SBarry Smith #else
72e489efc1SBarry Smith #define Scalar             complex
73e489efc1SBarry Smith #endif
74e489efc1SBarry Smith 
75e489efc1SBarry Smith /* Compiling for real numbers only */
76e489efc1SBarry Smith #else
77e489efc1SBarry Smith #define MPIU_SCALAR           MPI_DOUBLE
78*3eda8832SBarry Smith #if defined(PETSC_USE_MAT_SINGLE)
79*3eda8832SBarry Smith #define MPIU_MATSCALAR        MPI_FLOAT
80*3eda8832SBarry Smith #else
81*3eda8832SBarry Smith #define MPIU_MATSCALAR        MPI_DOUBLE
82*3eda8832SBarry Smith #endif
83329f5518SBarry Smith #define PetscRealPart(a)      (a)
84329f5518SBarry Smith #define PetscImaginaryPart(a) (a)
85e489efc1SBarry Smith #define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
86e489efc1SBarry Smith #define Scalar                double
87e489efc1SBarry Smith #define PetscConj(a)          (a)
8818a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
89184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
90184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
91184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
92184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
93e489efc1SBarry Smith #endif
94e489efc1SBarry Smith 
953f1db9ecSBarry Smith /*
963f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
973f1db9ecSBarry Smith    single precision but all other objects still use double
983f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
993f1db9ecSBarry Smith    it remains double
1003f1db9ecSBarry Smith 
1013f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
1023f1db9ecSBarry Smith */
103aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1043f1db9ecSBarry Smith 
1053f1db9ecSBarry Smith #define MatScalar Scalar
106329f5518SBarry Smith #define MatReal   double
1073f1db9ecSBarry Smith 
108aa482453SBarry Smith #elif defined(PETSC_USE_MAT_SINGLE)
1093f1db9ecSBarry Smith 
1103f1db9ecSBarry Smith #define MatScalar float
111329f5518SBarry Smith #define MatReal   float
1123f1db9ecSBarry Smith 
1133f1db9ecSBarry Smith #else
1143f1db9ecSBarry Smith 
1153f1db9ecSBarry Smith #define MatScalar Scalar
116329f5518SBarry Smith #define MatReal   double
1173f1db9ecSBarry Smith 
1183f1db9ecSBarry Smith #endif
1193f1db9ecSBarry Smith 
120329f5518SBarry Smith #if defined(PETSC_USE_SINGLE)
121329f5518SBarry Smith #define PetscReal float
122329f5518SBarry Smith #else
123329f5518SBarry Smith #define PetscReal double
124329f5518SBarry Smith #endif
1253f1db9ecSBarry Smith 
126314da920SBarry Smith /* --------------------------------------------------------------------------*/
127314da920SBarry Smith 
128e489efc1SBarry Smith /*
129e489efc1SBarry Smith    Certain objects may be created using either single
130e489efc1SBarry Smith   or double precision.
131e489efc1SBarry Smith */
132e489efc1SBarry Smith typedef enum { SCALAR_DOUBLE,SCALAR_SINGLE } ScalarPrecision;
133e489efc1SBarry Smith 
134e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
135e489efc1SBarry Smith extern  Scalar            PETSC_i;
136e489efc1SBarry Smith 
137e489efc1SBarry Smith #define PetscMin(a,b)      (((a)<(b)) ? (a) : (b))
138e489efc1SBarry Smith #define PetscMax(a,b)      (((a)<(b)) ? (b) : (a))
139e489efc1SBarry Smith #define PetscAbsInt(a)     (((a)<0)   ? -(a) : (a))
140e489efc1SBarry Smith #define PetscAbsDouble(a)  (((a)<0)   ? -(a) : (a))
141e489efc1SBarry Smith 
142314da920SBarry Smith /* ----------------------------------------------------------------------------*/
143314da920SBarry Smith /*
144314da920SBarry Smith      Basic constants
145314da920SBarry Smith */
146314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
147314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
148e489efc1SBarry Smith #define PETSC_MAX                1.e300
149e489efc1SBarry Smith #define PETSC_MIN                -1.e300
1500a5f7794SBarry Smith #define PETSC_MAX_INT            1000000000;
1510a5f7794SBarry Smith #define PETSC_MIN_INT            -1000000000;
152e489efc1SBarry Smith 
153314da920SBarry Smith /* ----------------------------------------------------------------------------*/
154e489efc1SBarry Smith /*
155e489efc1SBarry Smith     PLogDouble variables are used to contain double precision numbers
156e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
157e489efc1SBarry Smith   timing etc.
158e489efc1SBarry Smith */
159e489efc1SBarry Smith typedef double PLogDouble;
160e489efc1SBarry Smith /*
161e489efc1SBarry Smith       Once PETSc is compiling with a ADIC enhanced version of MPI
162e489efc1SBarry Smith    we will create a new MPI_Datatype for the inactive double variables.
163e489efc1SBarry Smith */
164e489efc1SBarry Smith #if defined(AD_DERIV_H)
165e489efc1SBarry Smith /* extern  MPI_Datatype  MPIU_PLOGDOUBLE; */
166e489efc1SBarry Smith #else
167488ecbafSBarry Smith #if !defined(USING_MPIUNI)
168e489efc1SBarry Smith #define MPIU_PLOGDOUBLE MPI_DOUBLE
169e489efc1SBarry Smith #endif
170e489efc1SBarry Smith #endif
171e489efc1SBarry Smith 
172e489efc1SBarry Smith 
173e489efc1SBarry Smith #endif
174