Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JPolynome3H.cc
Go to the documentation of this file.
1
2
3#include <string>
4#include <iostream>
5#include <iomanip>
6
7#include "TRandom3.h"
8
9#include "JMath/JPolynome.hh"
10#include "JTools/JMapList.hh"
14#include "JTools/JQuantile.hh"
15
16#include "Jeep/JParser.hh"
17#include "Jeep/JMessage.hh"
18
19
20namespace {
21
22 using namespace JPP;
23
24
25 /**
26 * 3D polynomial function.
27 */
28 struct JFunction3D {
29
30 /**
31 * Result.
32 */
33 struct result_type {
34 /**
35 * Constructor.
36 *
37 * \param __f function value f
38 * \param __fx derivative df/dx
39 * \param __fy derivative df/dy
40 * \param __fz derivative df/dz
41 */
42 result_type(const double __f,
43 const double __fx,
44 const double __fy,
45 const double __fz) :
46 f (__f),
47 fx(__fx),
48 fy(__fy),
49 fz(__fz)
50 {}
51
52
53 /**
54 * Type conversion operator.
55 *
56 * \return function value
57 */
58 operator double() const
59 {
60 return f;
61 }
62
63
64 const double f;
65 const double fx;
66 const double fy;
67 const double fz;
68 };
69
70
71 /**
72 * Constructor.
73 *
74 * In each dimension, the function value and the derivative are given by:
75 * <pre>
76 * f (x) = f1.getValue(x);
77 * f'(x) = f1.getDerivative(x);
78 * </pre>
79 *
80 * \param f1 polynome
81 */
82 JFunction3D(const JPolynome& f1) :
83 __f1(f1)
84 {}
85
86
87 /**
88 * Evaluation of function value and derivatives.
89 *
90 * \param x x-value
91 * \param y y-value
92 * \param z z-value
93 * \return function and derivatives
94 */
95 inline result_type operator()(const double x,
96 const double y,
97 const double z) const
98 {
99 return result_type(f (x) * f (y) * f (z),
100 fp(x) * f (y) * f (z),
101 f (x) * fp(y) * f (z),
102 f (x) * f (y) * fp(z));
103 }
104
105
106 protected:
107 /**
108 * Evaluation of function value.
109 *
110 * \param x x-value
111 * \return function value
112 */
113 inline double f(const double x) const
114 {
115 return __f1.getValue(x);
116 }
117
118
119 /**
120 * Evaluation of derivative.
121 *
122 * \param x x-value
123 * \return derivative
124 */
125 inline double fp(const double x) const
126 {
127 return __f1.getDerivative(x);
128 }
129
130 JPolynome __f1;
131 };
132
133
134 /**
135 * Test method for multi-dimensional interpolation with derivatives.
136 *
137 * \param argc number of command line arguments
138 * \param argv list of command line arguments
139 * \param title title of this template process
140 * \return status
141 */
142 template<class JMultiFunction_t>
143 int do_main(int argc, char **argv, const char* title)
144 {
145 using namespace std;
146
147 int numberOfEvents;
148 int numberOfBins;
150 int debug;
151
152 try {
153
154 JParser<> zap("Example program to test multi-dimensional interpolation with derivatives.");
155
156 zap['n'] = make_field(numberOfEvents) = 1000;
157 zap['N'] = make_field(numberOfBins) = 11;
158 zap['P'] = make_field(f1) = JPolynome(1.0, 1.0, 1.0);
159 zap['d'] = make_field(debug) = 3;
160
161 zap(argc, argv);
162 }
163 catch(const exception &error) {
164 FATAL(error.what() << endl);
165 }
166
167
168 if (numberOfEvents <= 0) { FATAL("Number of events " << numberOfEvents << endl); }
169 if (numberOfBins <= 2) { FATAL("Number of bins " << numberOfBins << endl); }
170
171 using namespace JPP;
172
173
174 const JFunction3D f3(f1);
175
176 const double xmin = -1.0;
177 const double xmax = +1.0;
178 const double dx = (xmax - xmin) / (numberOfBins - 1);
179
180
181 JMultiFunction_t g3;
182
183 for (double x = xmin; x < xmax + 0.5*dx; x += dx) {
184 for (double y = xmin; y < xmax + 0.5*dx; y += dx) {
185 for (double z = xmin; z < xmax + 0.5*dx; z += dx) {
186 g3[x][y][z] = f3(x,y,z);
187 }
188 }
189 }
190
191 g3.compile();
192 //g3.setExceptionHandler(new JFunction1D_t::JDefaultResult(JMATH::zero));
193
194
195 JQuantile f ("f ");
196 JQuantile fx("df/dx");
197 JQuantile fy("df/dy");
198 JQuantile fz("df/dz");
199
200 for (int i = 0; i != numberOfEvents; ++i) {
201
202 const double x = gRandom->Uniform(xmin, xmax);
203 const double y = gRandom->Uniform(xmin, xmax);
204 const double z = gRandom->Uniform(xmin, xmax);
205
206 const JFunction3D ::result_type u = f3(x,y,z);
207 const typename JMultiFunction_t::result_type v = g3(x,y,z);
208
209 f .put(u.f - v.f .f .f);
210 fx.put(u.fx - v.fp.f .f);
211 fy.put(u.fy - v.f .fp.f);
212 fz.put(u.fz - v.f .f .fp);
213 }
214
215 cout << endl << title << ":" << endl;
216
217 for (const JQuantile* p : { &f, &fx, &fy, &fz }) {
218 p->print(cout, p == &f);
219 }
220
221 return 0;
222 }
223}
224
225
226/**
227 * \file
228 * Example program to test multi-dimensional interpolation with derivatives.
229 * \author mdejong
230 */
231int main(int argc, char **argv)
232{
233 using namespace std;
234 using namespace JPP;
235
236 {
237 typedef JGridPolint3Function1H_t JFunction1D_t;
239 JPolint3FunctionalGridMapH>::maplist JMaplist_t;
240 typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
241
242 if (do_main<JMultiFunction_t>(argc, argv, "Polint") != 0) return 1;
243 }
244
245 {
246 typedef JGridSplineFunction1H_t JFunction1D_t;
248 JSplineFunctionalGridMapH>::maplist JMaplist_t;
249 typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
250
251 if (do_main<JMultiFunction_t>(argc, argv, "Spline") != 0) return 1;
252 }
253}
Various implementations of functional maps.
General purpose messaging.
#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
double f3(const double x, const double y, const double z)
3D function.
int main(int argc, char **argv)
int numberOfBins
number of bins for average CDF integral of optical module
Definition JSirene.cc:73
Utility class to parse command line options.
Definition JParser.hh:1698
Multidimensional interpolation method.
const JPolynome f1(1.0, 2.0, 3.0)
Function.
const double xmax
const double xmin
const parameter_list< JPolynome< ID_t, 0 > > JPolynome< ID_t, 0 >::parameters & JPolynome
Set parameters.
Definition JMathlib.hh:1566
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Recursive template class for polynomial function.
Definition JPolynome.hh:165
Type definition of a 3rd degree polynomial interpolation with result type JResultDerivative.
Type definition of a spline interpolation based on a JGridCollection with JResultDerivative result ty...
Auxiliary class for recursive map list generation.
Definition JMapList.hh:109
Type definition of a 3rd degree polynomial interpolation based on a JGridMap implementation.
Auxiliary data structure for running average, standard deviation and quantiles.
Definition JQuantile.hh:46
Type definition of a spline interpolation based on a JGridMap implementation.