xref: /petsc/include/petscmath.h (revision d3ecb3a785ef5ad0d1dbdadadf0bd2002a45dced)
1*d3ecb3a7SBarry Smith /* $Id: petscmath.h,v 1.21 2001/04/10 19:37:48 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
363eda8832SBarry Smith #if defined(PETSC_USE_MAT_SINGLE)
373eda8832SBarry Smith #define MPIU_MATSCALAR        ??Notdone
383eda8832SBarry Smith #else
393eda8832SBarry Smith #define MPIU_MATSCALAR      MPIU_COMPLEX
403eda8832SBarry Smith #endif
413eda8832SBarry 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
783eda8832SBarry Smith #  if defined(PETSC_USE_MAT_SINGLE)
793eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_FLOAT
803eda8832SBarry Smith #  else
813eda8832SBarry Smith #    define MPIU_MATSCALAR        MPI_DOUBLE
823eda8832SBarry 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 PetscConj(a)          (a)
8718a7d68fSSatish Balay #  define PetscSqrtScalar(a)    sqrt(a)
88184914b5SBarry Smith #  define PetscPowScalar(a,b)   pow(a,b)
89184914b5SBarry Smith #  define PetscExpScalar(a)     exp(a)
90184914b5SBarry Smith #  define PetscSinScalar(a)     sin(a)
91184914b5SBarry Smith #  define PetscCosScalar(a)     cos(a)
92b0a32e0cSBarry Smith 
93b0a32e0cSBarry Smith #  if defined(PETSC_USE_SINGLE)
94b0a32e0cSBarry Smith #    define Scalar            float
95b0a32e0cSBarry Smith #  else
96b0a32e0cSBarry Smith #    define Scalar            double
97b0a32e0cSBarry Smith #  endif
98e489efc1SBarry Smith #endif
99e489efc1SBarry Smith 
1003f1db9ecSBarry Smith /*
1013f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
1023f1db9ecSBarry Smith    single precision but all other objects still use double
1033f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
1043f1db9ecSBarry Smith    it remains double
1053f1db9ecSBarry Smith 
1063f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
1073f1db9ecSBarry Smith */
108aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
1093f1db9ecSBarry Smith 
1103f1db9ecSBarry Smith #define MatScalar Scalar
111329f5518SBarry Smith #define MatReal   double
1123f1db9ecSBarry Smith 
113aa482453SBarry Smith #elif defined(PETSC_USE_MAT_SINGLE)
1143f1db9ecSBarry Smith 
1153f1db9ecSBarry Smith #define MatScalar float
116329f5518SBarry Smith #define MatReal   float
1173f1db9ecSBarry Smith 
1183f1db9ecSBarry Smith #else
1193f1db9ecSBarry Smith 
1203f1db9ecSBarry Smith #define MatScalar Scalar
121329f5518SBarry Smith #define MatReal   double
1223f1db9ecSBarry Smith 
1233f1db9ecSBarry Smith #endif
1243f1db9ecSBarry Smith 
125329f5518SBarry Smith #if defined(PETSC_USE_SINGLE)
126329f5518SBarry Smith #define PetscReal float
127329f5518SBarry Smith #else
128329f5518SBarry Smith #define PetscReal double
129329f5518SBarry Smith #endif
1303f1db9ecSBarry Smith 
131314da920SBarry Smith /* --------------------------------------------------------------------------*/
132314da920SBarry Smith 
133e489efc1SBarry Smith /*
134e489efc1SBarry Smith    Certain objects may be created using either single
135e489efc1SBarry Smith   or double precision.
136e489efc1SBarry Smith */
137e489efc1SBarry Smith typedef enum { SCALAR_DOUBLE,SCALAR_SINGLE } ScalarPrecision;
138e489efc1SBarry Smith 
139e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
140e489efc1SBarry Smith extern  Scalar            PETSC_i;
141e489efc1SBarry Smith 
142e489efc1SBarry Smith #define PetscMin(a,b)      (((a)<(b)) ? (a) : (b))
143e489efc1SBarry Smith #define PetscMax(a,b)      (((a)<(b)) ? (b) : (a))
144e489efc1SBarry Smith #define PetscAbsInt(a)     (((a)<0)   ? -(a) : (a))
145e489efc1SBarry Smith #define PetscAbsDouble(a)  (((a)<0)   ? -(a) : (a))
146e489efc1SBarry Smith 
147314da920SBarry Smith /* ----------------------------------------------------------------------------*/
148314da920SBarry Smith /*
149314da920SBarry Smith      Basic constants
150314da920SBarry Smith */
151314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
152314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
153e489efc1SBarry Smith #define PETSC_MAX                1.e300
154e489efc1SBarry Smith #define PETSC_MIN                -1.e300
1550a5f7794SBarry Smith #define PETSC_MAX_INT            1000000000;
1560a5f7794SBarry Smith #define PETSC_MIN_INT            -1000000000;
157e489efc1SBarry Smith 
158314da920SBarry Smith /* ----------------------------------------------------------------------------*/
159e489efc1SBarry Smith /*
160b0a32e0cSBarry Smith     PetscLogDouble variables are used to contain double precision numbers
161e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
162e489efc1SBarry Smith   timing etc.
163e489efc1SBarry Smith */
164b0a32e0cSBarry Smith typedef double PetscLogDouble;
165e489efc1SBarry Smith /*
166e489efc1SBarry Smith       Once PETSc is compiling with a ADIC enhanced version of MPI
167e489efc1SBarry Smith    we will create a new MPI_Datatype for the inactive double variables.
168e489efc1SBarry Smith */
169e489efc1SBarry Smith #if defined(AD_DERIV_H)
170b9617806SBarry Smith /* extern  MPI_Datatype  MPIU_PETSCLOGDOUBLE; */
171e489efc1SBarry Smith #else
172488ecbafSBarry Smith #if !defined(USING_MPIUNI)
173b9617806SBarry Smith #define MPIU_PETSCLOGDOUBLE MPI_DOUBLE
174e489efc1SBarry Smith #endif
175e489efc1SBarry Smith #endif
176e489efc1SBarry Smith 
177*d3ecb3a7SBarry Smith #define InactiveDouble double
178*d3ecb3a7SBarry Smith #define InactiveScalar Scalar
179*d3ecb3a7SBarry Smith 
180b0a32e0cSBarry Smith #define PETSCMAP1_a(a,b)  a ## _ ## b
181b0a32e0cSBarry Smith #define PETSCMAP1_b(a,b)  PETSCMAP1_a(a,b)
182b0a32e0cSBarry Smith #define PETSCMAP1(a)      PETSCMAP1_b(a,Scalar)
183e489efc1SBarry Smith #endif
184