Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JQuadrature.cc
Go to the documentation of this file.
1
2#include <string>
3#include <iostream>
4#include <iomanip>
5#include <cmath>
6
8
9#include "JMath/JPolynome.hh"
10#include "JMath/JGauss.hh"
11
13
14#include "Jeep/JParser.hh"
15#include "Jeep/JMessage.hh"
16
17
18/**
19 * \file
20 *
21 * Example program to test JTOOLS::JQuadrature.
22 * \author mdejong
23 */
24int main(int argc, char **argv)
25{
26 using namespace std;
27 using namespace JPP;
28
29 double precision;
30 int debug;
31
32 try {
33
34 JParser<> zap("Example program to test JTOOLS::JQuadrature.");
35
36 zap['e'] = make_field(precision) = 1.0e-4;
37 zap['d'] = make_field(debug) = 3;
38
39 zap(argc, argv);
40 }
41 catch(const exception &error) {
42 FATAL(error.what() << endl);
43 }
44
45 {
46 const int numberOfPoints = 10;
47 const double epsilon = 1.0e-12;
48
49 const double xmin = -5.0;
50 const double xmax = +3.0;
51
52 /**
53 * Function.
54 */
55 const JPolynome f1(1.0, 2.0, 3.0);
56
57 /**
58 * Integral.
59 */
60 const JPolynome F1 = f1.getIntegral();
61
62 double V = F1(xmax) - F1(xmin);
63
64 JGaussLegendre engine(numberOfPoints, epsilon);
65
66 const double x0 = 0.5 * (xmax + xmin);
67 const double x1 = 0.5 * (xmax - xmin);
68
69 double W = 0.0;
70
71 for (JQuadrature::const_iterator i = engine.begin(); i != engine.end(); ++i) {
72
73 const double x = x0 + i->getX() * x1;
74
75 W += f1(x) * i->getY() * x1;
76 }
77
78 DEBUG("integral analytical " << FIXED(9,5) << V << endl);
79 DEBUG("integral numerical " << FIXED(9,5) << W << endl);
80
81 ASSERT(fabs(V - W) <= precision, "Test Gauss-Legendre integration.");
82 }
83 {
84 const int numberOfPoints = 10;
85 const double a = 1.0;
86 const double epsilon = 1.0e-12;
87
88 const double xmin = 0.0;
89 const double xmax = +1.0e10; // approximation of infinity
90
91 /**
92 * Function.
93 */
94 const struct {
95 inline double operator()(const double x) const
96 {
97 return sin(x);
98 }
99 } f1;
100
101 /**
102 * Integral.
103 */
104 const struct {
105 inline double operator()(const double x) const
106 {
107 // integral of sin(x) * x^1 * exp(-x)
108
109 return -0.5 * exp(-x) * (x*sin(x) + (x + 1.0)*cos(x));
110 }
111 } F1;
112
113 const double V = F1(xmax) - F1(xmin);
114
115 JGaussLaguerre engine(numberOfPoints, a, epsilon);
116
117 double W = 0.0;
118
119 for (JQuadrature::const_iterator i = engine.begin(); i != engine.end(); ++i) {
120
121 const double x = i->getX();
122
123 W += f1(x) * i->getY();
124 }
125
126 DEBUG("integral analytical " << FIXED(9,5) << V << endl);
127 DEBUG("integral numerical " << FIXED(9,5) << W << endl);
128
129 ASSERT(fabs(V - W) <= precision, "Test Gauss-Laguerre integration.");
130 }
131 {
132 const int numberOfPoints = 10;
133 const double epsilon = 1.0e-12;
134
135 const double sigma[] = { 4.0, 0.5 };
136
137 const double xmin = -3.0 * sigma[0];
138 const double xmax = +3.0 * sigma[0];
139
140 /**
141 * Function.
142 */
143 const JGauss f1(0.0, sigma[0]);
144
145 /**
146 * Integral.
147 */
148 const JGauss F1(0.0, hypot(sigma[0], sigma[1]));
149
150 JGaussHermite engine(numberOfPoints, epsilon);
151
152 for (double x = xmin; x <= xmax; x += 0.05*(xmax - xmin)) {
153
154 const double V = F1(x);
155
156 double W = 0.0;
157
158 for (JQuadrature::const_iterator i = engine.begin(); i != engine.end(); ++i) {
159
160 const double u = i->getX() * sigma[1] * sqrt(2.0);
161 const double v = i->getY() / sqrt(PI);
162
163 W += f1(x + u) * v;
164 }
165
166 DEBUG(FIXED(7,3) << x << ' ' << FIXED(9,5) << V << ' ' << FIXED(9,5) << W << endl);
167
168 ASSERT(fabs(V - W) <= precision * V, "Test Gauss-Hermite integration.");
169 }
170 }
171 {
172 const int numberOfPoints = 10;
173
174 const double a = -1.5;
175 const double g = 0.924;
176
177 const double V = 1.0;
178
179 JHenyeyGreenstein engine(numberOfPoints, g, a);
180
181 double W = 0.0;
182
183 for (JQuadrature::const_iterator i = engine.begin(); i != engine.end(); ++i) {
184
185 const double x = i->getX();
186
187 W += henyey_greenstein(g,x) * i->getY() * 2*PI;
188 }
189
190 DEBUG("integral analytical " << FIXED(9,5) << V << endl);
191 DEBUG("integral numerical " << FIXED(9,5) << W << endl);
192
193 ASSERT(fabs(V - W) <= precision, "Test Henyey-Greenstein integration.");
194 }
195 {
196 const int numberOfPoints = 10;
197
198 const double a = 0.835;
199
200 const double V = 1.0;
201
202 JRayleigh engine(numberOfPoints, a);
203
204 double W = 0.0;
205
206 for (JQuadrature::const_iterator i = engine.begin(); i != engine.end(); ++i) {
207
208 const double x = i->getX();
209
210 W += rayleigh(a,x) * i->getY() * 2*PI;
211 }
212
213 DEBUG("integral analytical " << FIXED(9,5) << V << endl);
214 DEBUG("integral numerical " << FIXED(9,5) << W << endl);
215
216 ASSERT(fabs(V - W) <= precision, "Test Rayleigh integration.");
217 }
218
219 return 0;
220}
221
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
int main(int argc, char **argv)
Auxiliary classes for numerical integration.
int numberOfPoints
Definition JResultPDF.cc:22
Utility class to parse command line options.
Definition JParser.hh:1698
container_type::const_iterator const_iterator
Numerical integrator for .
Numerical integrator for .
Numerical integrator for .
Numerical integrator for , where .
Numerical integrator for , where .
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Recursive template class for polynomial function.
Definition JPolynome.hh:165
double getIntegral(const double x) const
Integral value.
Definition JPolynome.hh:276