Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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"
11 #include "JTools/JFunction1D_t.hh"
13 #include "JTools/JMultiFunction.hh"
14 #include "JTools/JQuantile.hh"
15 
16 #include "Jeep/JParser.hh"
17 #include "Jeep/JMessage.hh"
18 
19 
20 namespace {
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  template<class JMultiFunction_t>
138  int do_main(int argc, char **argv, const char* title)
139  {
140  using namespace std;
141 
142  int numberOfEvents;
143  int numberOfBins;
144  JPolynome f1;
145  int debug;
146 
147  try {
148 
149  JParser<> zap("Example program to test multi-dimensional interpolation with derivatives.");
150 
151  zap['n'] = make_field(numberOfEvents) = 1000;
152  zap['N'] = make_field(numberOfBins) = 11;
153  zap['P'] = make_field(f1) = JPolynome(3, 1.0, 1.0, 1.0);
154  zap['d'] = make_field(debug) = 3;
155 
156  zap(argc, argv);
157  }
158  catch(const exception &error) {
159  FATAL(error.what() << endl);
160  }
161 
162 
163  if (numberOfEvents <= 0) { FATAL("Number of events " << numberOfEvents << endl); }
164  if (numberOfBins <= 2) { FATAL("Number of bins " << numberOfBins << endl); }
165 
166  using namespace JPP;
167 
168 
169  const JFunction3D f3(f1);
170 
171  const double xmin = -1.0;
172  const double xmax = +1.0;
173  const double dx = (xmax - xmin) / (numberOfBins - 1);
174 
175 
176  JMultiFunction_t g3;
177 
178  for (double x = xmin; x < xmax + 0.5*dx; x += dx) {
179  for (double y = xmin; y < xmax + 0.5*dx; y += dx) {
180  for (double z = xmin; z < xmax + 0.5*dx; z += dx) {
181  g3[x][y][z] = f3(x,y,z);
182  }
183  }
184  }
185 
186  g3.compile();
187  //g3.setExceptionHandler(new JFunction1D_t::JDefaultResult(JMATH::zero));
188 
189 
190  JQuantile f ("f ");
191  JQuantile fx("df/dx");
192  JQuantile fy("df/dy");
193  JQuantile fz("df/dz");
194 
195  for (int i = 0; i != numberOfEvents; ++i) {
196 
197  const double x = gRandom->Uniform(xmin, xmax);
198  const double y = gRandom->Uniform(xmin, xmax);
199  const double z = gRandom->Uniform(xmin, xmax);
200 
201  const JFunction3D ::result_type u = f3(x,y,z);
202  const typename JMultiFunction_t::result_type v = g3(x,y,z);
203 
204  f .put(u.f - v.f .f .f);
205  fx.put(u.fx - v.fp.f .f);
206  fy.put(u.fy - v.f .fp.f);
207  fz.put(u.fz - v.f .f .fp);
208  }
209 
210  cout << endl << title << ":" << endl;
211 
212  for (const JQuantile* buffer[] = { &f, &fx, &fy, &fz, NULL }, **p = buffer; *p != NULL; ++p) {
213  (*p)->print(cout, *p == &f);
214  }
215 
216  return 0;
217  }
218 }
219 
220 
221 /**
222  * \file
223  * Example program to test multi-dimensional interpolation with derivatives.
224  * \author mdejong
225  */
226 int main(int argc, char **argv)
227 {
228  using namespace std;
229  using namespace JPP;
230 
231  {
232  typedef JGridPolint3Function1H_t JFunction1D_t;
234  JPolint3FunctionalGridMapH>::maplist JMaplist_t;
235  typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
236 
237  if (do_main<JMultiFunction_t>(argc, argv, "Polint") != 0) return 1;
238  }
239 
240  {
241  typedef JGridSplineFunction1H_t JFunction1D_t;
243  JSplineFunctionalGridMapH>::maplist JMaplist_t;
244  typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
245 
246  if (do_main<JMultiFunction_t>(argc, argv, "Spline") != 0) return 1;
247  }
248 }
Utility class to parse command line options.
Definition: JParser.hh:1410
Type definition of a 3rd degree polynomial interpolation based on a JGridMap implementation.
Polynome function object.
Definition: JPolynome.hh:26
Quantile calculator.
Definition: JQuantile.hh:34
Type definition of a spline interpolation based on a JGridCollection with JResultHesse result type...
Various implementations of functional maps.
Type definition of a 3rd degree polynomial interpolation with result type double. ...
Multidimensional interpolation method.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
int debug
debug level
Definition: JSirene.cc:59
Auxiliary class for recursive map list generation.
Definition: JMapList.hh:108
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:65
Utility class to parse command line options.
double f3(const double x, const double y, const double z)
3D function.
Definition: JPolynome3D.cc:23
Type definition of a spline interpolation based on a JGridMap implementation.
int main(int argc, char *argv[])
Definition: Main.cpp:15