Jpp test-rotations-old-57-g407471f53
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 using namespace JPP;
147
148 int numberOfEvents;
149 int numberOfBins;
151 int debug;
152
153 try {
154
155 JParser<> zap("Example program to test multi-dimensional interpolation with derivatives.");
156
157 zap['n'] = make_field(numberOfEvents) = 1000;
158 zap['N'] = make_field(numberOfBins) = 11;
159 zap['P'] = make_field(f1) = JPolynome(1.0, 1.0, 1.0);
160 zap['d'] = make_field(debug) = 3;
161
162 zap(argc, argv);
163 }
164 catch(const exception &error) {
165 FATAL(error.what() << endl);
166 }
167
168
169 if (numberOfEvents <= 0) { FATAL("Number of events " << numberOfEvents << endl); }
170 if (numberOfBins <= 2) { FATAL("Number of bins " << numberOfBins << endl); }
171
172
173 const JFunction3D f3(f1);
174
175 const double xmin = -1.0;
176 const double xmax = +1.0;
177 const double dx = (xmax - xmin) / (numberOfBins - 1);
178
179
180 JMultiFunction_t g3;
181
182 for (double x = xmin; x < xmax + 0.5*dx; x += dx) {
183 for (double y = xmin; y < xmax + 0.5*dx; y += dx) {
184 for (double z = xmin; z < xmax + 0.5*dx; z += dx) {
185 g3[x][y][z] = f3(x,y,z);
186 }
187 }
188 }
189
190 g3.compile();
191 //g3.setExceptionHandler(new JFunction1D_t::JDefaultResult(JMATH::zero));
192
193
194 JQuantile f ("f ");
195 JQuantile fx("df/dx");
196 JQuantile fy("df/dy");
197 JQuantile fz("df/dz");
198
199 for (int i = 0; i != numberOfEvents; ++i) {
200
201 const double x = gRandom->Uniform(xmin, xmax);
202 const double y = gRandom->Uniform(xmin, xmax);
203 const double z = gRandom->Uniform(xmin, xmax);
204
205 const JFunction3D ::result_type u = f3(x,y,z);
206 const typename JMultiFunction_t::result_type v = g3(x,y,z);
207
208 f .put(u.f - v.f .f .f);
209 fx.put(u.fx - v.fp.f .f);
210 fy.put(u.fy - v.f .fp.f);
211 fz.put(u.fz - v.f .f .fp);
212 }
213
214 cout << endl << title << ":" << endl;
215
216 for (const JQuantile* p : { &f, &fx, &fy, &fz }) {
217 p->print(cout, p == &f);
218 }
219
220 return 0;
221 }
222}
223
224
225/**
226 * \file
227 * Example program to test multi-dimensional interpolation with derivatives.
228 * \author mdejong
229 */
230int main(int argc, char **argv)
231{
232 using namespace std;
233 using namespace JPP;
234
235 {
236 typedef JGridPolint3Function1H_t JFunction1D_t;
238 JPolint3FunctionalGridMapH>::maplist JMaplist_t;
239 typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
240
241 if (do_main<JMultiFunction_t>(argc, argv, "Polint") != 0) return 1;
242 }
243
244 {
245 typedef JGridSplineFunction1H_t JFunction1D_t;
247 JSplineFunctionalGridMapH>::maplist JMaplist_t;
248 typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
249
250 if (do_main<JMultiFunction_t>(argc, argv, "Spline") != 0) return 1;
251 }
252}
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.