xref: /petsc/include/petscmath.h (revision 329f5518e9d4bb7ce96c0c5576cc53785c973973)
1*329f5518SBarry Smith /* $Id: petscmath.h,v 1.17 1999/09/27 21:33:07 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
36c80bc32dSSatish Balay #if defined (PETSC_HAVE_STD_COMPLEX)
37*329f5518SBarry Smith #define PetscRealPart(a)        (a).real()
38*329f5518SBarry Smith #define PetscImaginaryPart(a)   (a).imag()
393f6de6efSSatish Balay #define PetscAbsScalar(a)   std::abs(a)
403f6de6efSSatish Balay #define PetscConj(a)        std::conj(a)
4118a7d68fSSatish Balay #define PetscSqrtScalar(a)  std::sqrt(a)
42184914b5SBarry Smith #define PetscPowScalar(a,b) std::pow(a,b)
43184914b5SBarry Smith #define PetscExpScalar(a)   std::exp(a)
44184914b5SBarry Smith #define PetscSinScalar(a)   std::sin(a)
45184914b5SBarry Smith #define PetscCosScalar(a)   std::cos(a)
46e489efc1SBarry Smith #else
47*329f5518SBarry Smith #define PetscRealPart(a)        real(a)
48*329f5518SBarry Smith #define PetscImaginaryPart(a)   imag(a)
49e489efc1SBarry Smith #define PetscAbsScalar(a)   abs(a)
50e489efc1SBarry Smith #define PetscConj(a)        conj(a)
5118a7d68fSSatish Balay #define PetscSqrtScalar(a)  sqrt(a)
52184914b5SBarry Smith #define PetscPowScalar(a,b) pow(a,b)
53184914b5SBarry Smith #define PetscExpScalar(a)   exp(a)
54184914b5SBarry Smith #define PetscSinScalar(a)   sin(a)
55184914b5SBarry Smith #define PetscCosScalar(a)   cos(a)
56adc17e78SSatish Balay #endif
57e489efc1SBarry Smith /*
58e489efc1SBarry Smith   The new complex class for GNU C++ is based on templates and is not backward
59e489efc1SBarry Smith   compatible with all previous complex class libraries.
60e489efc1SBarry Smith */
61c80bc32dSSatish Balay #if defined(PETSC_HAVE_STD_COMPLEX)
62df9b3741SSatish Balay #define Scalar             std::complex<double>
63aa482453SBarry Smith #elif defined(PETSC_HAVE_TEMPLATED_COMPLEX)
64e489efc1SBarry Smith #define Scalar             complex<double>
65e489efc1SBarry Smith #else
66e489efc1SBarry Smith #define Scalar             complex
67e489efc1SBarry Smith #endif
68e489efc1SBarry Smith 
69e489efc1SBarry Smith /* Compiling for real numbers only */
70e489efc1SBarry Smith #else
71e489efc1SBarry Smith #define MPIU_SCALAR           MPI_DOUBLE
72*329f5518SBarry Smith #define PetscRealPart(a)      (a)
73*329f5518SBarry Smith #define PetscImaginaryPart(a) (a)
74e489efc1SBarry Smith #define PetscAbsScalar(a)     (((a)<0.0)   ? -(a) : (a))
75e489efc1SBarry Smith #define Scalar                double
76e489efc1SBarry Smith #define PetscConj(a)          (a)
7718a7d68fSSatish Balay #define PetscSqrtScalar(a)    sqrt(a)
78184914b5SBarry Smith #define PetscPowScalar(a,b)   pow(a,b)
79184914b5SBarry Smith #define PetscExpScalar(a)     exp(a)
80184914b5SBarry Smith #define PetscSinScalar(a)     sin(a)
81184914b5SBarry Smith #define PetscCosScalar(a)     cos(a)
82e489efc1SBarry Smith #endif
83e489efc1SBarry Smith 
843f1db9ecSBarry Smith /*
853f1db9ecSBarry Smith        Allows compiling PETSc so that matrix values are stored in
863f1db9ecSBarry Smith    single precision but all other objects still use double
873f1db9ecSBarry Smith    precision. This does not work for complex numbers in that case
883f1db9ecSBarry Smith    it remains double
893f1db9ecSBarry Smith 
903f1db9ecSBarry Smith           EXPERIMENTAL! NOT YET COMPLETELY WORKING
913f1db9ecSBarry Smith */
92aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
933f1db9ecSBarry Smith 
943f1db9ecSBarry Smith #define MatScalar Scalar
95*329f5518SBarry Smith #define MatReal   double
963f1db9ecSBarry Smith 
97aa482453SBarry Smith #elif defined(PETSC_USE_MAT_SINGLE)
983f1db9ecSBarry Smith 
993f1db9ecSBarry Smith #define MatScalar float
100*329f5518SBarry Smith #define MatReal   float
1013f1db9ecSBarry Smith 
1023f1db9ecSBarry Smith #else
1033f1db9ecSBarry Smith 
1043f1db9ecSBarry Smith #define MatScalar Scalar
105*329f5518SBarry Smith #define MatReal   double
1063f1db9ecSBarry Smith 
1073f1db9ecSBarry Smith #endif
1083f1db9ecSBarry Smith 
109*329f5518SBarry Smith #if defined(PETSC_USE_SINGLE)
110*329f5518SBarry Smith #define PetscReal float
111*329f5518SBarry Smith #else
112*329f5518SBarry Smith #define PetscReal double
113*329f5518SBarry Smith #endif
1143f1db9ecSBarry Smith 
115314da920SBarry Smith /* --------------------------------------------------------------------------*/
116314da920SBarry Smith 
117e489efc1SBarry Smith /*
118e489efc1SBarry Smith    Certain objects may be created using either single
119e489efc1SBarry Smith   or double precision.
120e489efc1SBarry Smith */
121e489efc1SBarry Smith typedef enum { SCALAR_DOUBLE,SCALAR_SINGLE } ScalarPrecision;
122e489efc1SBarry Smith 
123e489efc1SBarry Smith /* PETSC_i is the imaginary number, i */
124e489efc1SBarry Smith extern  Scalar            PETSC_i;
125e489efc1SBarry Smith 
126e489efc1SBarry Smith #define PetscMin(a,b)      (((a)<(b)) ? (a) : (b))
127e489efc1SBarry Smith #define PetscMax(a,b)      (((a)<(b)) ? (b) : (a))
128e489efc1SBarry Smith #define PetscAbsInt(a)     (((a)<0)   ? -(a) : (a))
129e489efc1SBarry Smith #define PetscAbsDouble(a)  (((a)<0)   ? -(a) : (a))
130e489efc1SBarry Smith 
131314da920SBarry Smith /* ----------------------------------------------------------------------------*/
132314da920SBarry Smith /*
133314da920SBarry Smith      Basic constants
134314da920SBarry Smith */
135314da920SBarry Smith #define PETSC_PI                 3.14159265358979323846264
136314da920SBarry Smith #define PETSC_DEGREES_TO_RADIANS 0.01745329251994
137e489efc1SBarry Smith #define PETSC_MAX                1.e300
138e489efc1SBarry Smith #define PETSC_MIN                -1.e300
1390a5f7794SBarry Smith #define PETSC_MAX_INT            1000000000;
1400a5f7794SBarry Smith #define PETSC_MIN_INT            -1000000000;
141e489efc1SBarry Smith 
142314da920SBarry Smith /* ----------------------------------------------------------------------------*/
143e489efc1SBarry Smith /*
144e489efc1SBarry Smith     PLogDouble variables are used to contain double precision numbers
145e489efc1SBarry Smith   that are not used in the numerical computations, but rather in logging,
146e489efc1SBarry Smith   timing etc.
147e489efc1SBarry Smith */
148e489efc1SBarry Smith typedef double PLogDouble;
149e489efc1SBarry Smith /*
150e489efc1SBarry Smith       Once PETSc is compiling with a ADIC enhanced version of MPI
151e489efc1SBarry Smith    we will create a new MPI_Datatype for the inactive double variables.
152e489efc1SBarry Smith */
153e489efc1SBarry Smith #if defined(AD_DERIV_H)
154e489efc1SBarry Smith /* extern  MPI_Datatype  MPIU_PLOGDOUBLE; */
155e489efc1SBarry Smith #else
156488ecbafSBarry Smith #if !defined(USING_MPIUNI)
157e489efc1SBarry Smith #define MPIU_PLOGDOUBLE MPI_DOUBLE
158e489efc1SBarry Smith #endif
159e489efc1SBarry Smith #endif
160e489efc1SBarry Smith 
161e489efc1SBarry Smith 
162e489efc1SBarry Smith #endif
163