Jpp
JPolynome3G.cc
Go to the documentation of this file.
1 
2 
3 #include <string>
4 #include <iostream>
5 #include <iomanip>
6 #include <cmath>
7 #include <map>
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/JToolsToolkit.hh"
15 #include "JTools/JFunctionalMap.hh"
16 
17 #include "Jeep/JPrint.hh"
18 #include "Jeep/JParser.hh"
19 #include "Jeep/JMessage.hh"
20 
21 
22 namespace {
23 
24  using namespace JPP;
25 
26 
27  /**
28  * 3D polynomial function.
29  */
30  struct JFunction3D {
31 
32  /**
33  * Result.
34  */
35  struct result_type {
36  /**
37  * Constructor.
38  *
39  * \param __f function value f
40  * \param __fx derivative df/dx
41  * \param __fy derivative df/dy
42  * \param __fz derivative df/dz
43  */
44  result_type(const double __f,
45  const double __fx,
46  const double __fy,
47  const double __fz) :
48  f (__f),
49  fx(__fx),
50  fy(__fy),
51  fz(__fz)
52  {}
53 
54 
55  /**
56  * Type conversion operator.
57  *
58  * \return function value
59  */
60  operator double() const
61  {
62  return f;
63  }
64 
65 
66  const double f;
67  const double fx;
68  const double fy;
69  const double fz;
70  };
71 
72 
73  /**
74  * Constructor.
75  *
76  * In each dimension, the function value and the derivative are given by:
77  * <pre>
78  * f (x) = f1.getValue(x);
79  * f'(x) = f1.getDerivative(x);
80  * </pre>
81  *
82  * \param f1 polynome
83  */
84  JFunction3D(const JPolynome& f1) :
85  __f1(f1)
86  {}
87 
88 
89  /**
90  * Evaluation of function value and derivatives.
91  *
92  * \param x x-value
93  * \param y y-value
94  * \param z z-value
95  * \return function and derivatives
96  */
97  inline result_type operator()(const double x,
98  const double y,
99  const double z) const
100  {
101  return result_type(f (x) * f (y) * f (z),
102  fp(x) * f (y) * f (z),
103  f (x) * fp(y) * f (z),
104  f (x) * f (y) * fp(z));
105  }
106 
107 
108  /**
109  * Evaluation of integral.
110  *
111  * \param xmin minimal x-value
112  * \param xmax maximal x-value
113  * \param ymin minimal y-value
114  * \param ymax maximal y-value
115  * \param zmin minimal z-value
116  * \param zmax maximal z-value
117  * \return integral
118  */
119  inline double operator()(const double xmin, const double xmax,
120  const double ymin, const double ymax,
121  const double zmin, const double zmax) const
122  {
123  return ((__f1.getIntegral(xmax) - __f1.getIntegral(xmin)) *
124  (__f1.getIntegral(ymax) - __f1.getIntegral(ymin)) *
125  (__f1.getIntegral(zmax) - __f1.getIntegral(zmin)));
126  }
127 
128 
129  protected:
130  /**
131  * Evaluation of function value.
132  *
133  * \param x x-value
134  * \return function value
135  */
136  inline double f(const double x) const
137  {
138  return __f1.getValue(x);
139  }
140 
141 
142  /**
143  * Evaluation of derivative.
144  *
145  * \param x x-value
146  * \return derivative
147  */
148  inline double fp(const double x) const
149  {
150  return __f1.getDerivative(x);
151  }
152 
153  JPolynome __f1;
154  };
155 
156 
157  /**
158  * Test method for multi-dimensional interpolation with derivatives.
159  */
160  template<class JMultiFunction_t>
161  int do_main(int argc, char **argv, const char* title)
162  {
163  using namespace std;
164 
165  int numberOfBins;
166  JPolynome f1;
167  map<string, double> precision;
168  int debug;
169 
170  precision["spline"] = 1.0e-3;
171  precision["polint"] = 1.0e-10;
172 
173  try {
174 
175  JParser<> zap("Example program to test multi-dimensional integration.");
176 
177  zap['N'] = make_field(numberOfBins) = 11;
178  zap['P'] = make_field(f1) = JPolynome(3, 1.0, 1.0, 1.0);
179  zap['e'] = make_field(precision) = JPARSER::initialised();
180  zap['d'] = make_field(debug) = 3;
181 
182  zap(argc, argv);
183  }
184  catch(const exception &error) {
185  FATAL(error.what() << endl);
186  }
187 
188 
189  if (numberOfBins <= 2) {
190  FATAL("Number of bins " << numberOfBins << endl);
191  }
192 
193  using namespace JPP;
194 
195 
196  const JFunction3D f3(f1);
197 
198  const double xmin = -1.0;
199  const double xmax = +1.0;
200  const double dx = (xmax - xmin) / (numberOfBins - 1);
201 
202 
203  JMultiFunction_t g3;
204 
205  for (double x = xmin; x < xmax + 0.5*dx; x += dx) {
206  for (double y = xmin; y < xmax + 0.5*dx; y += dx) {
207  for (double z = xmin; z < xmax + 0.5*dx; z += dx) {
208  g3[x][y][z] = f3(x,y,z);
209  }
210  }
211  }
212 
213  g3.compile();
214  //g3.setExceptionHandler(new JFunction1D_t::JDefaultResult(JMATH::zero));
215 
216  if (debug >= debug_t) {
217  cout << endl
218  << setw(8) << left << title << ' '
219  << setw(12) << right << "real" << ' '
220  << setw(12) << right << "calculated" << endl;
221  cout << setw(8) << left << "integral";
222  cout << ' ' << SCIENTIFIC(12,4) << f3(xmin, xmax, xmin, xmax, xmin, xmax);
223  cout << ' ' << SCIENTIFIC(12,4) << getIntegral(g3);
224  cout << endl;
225  }
226 
227  ASSERT(fabs(f3(xmin, xmax, xmin, xmax, xmin, xmax) - getIntegral(g3)) <= precision[title] * f3(xmin, xmax, xmin, xmax, xmin, xmax));
228 
229  return 0;
230  }
231 }
232 
233 
234 /**
235  * \file
236  *
237  * Example program to test multi-dimensional integration.
238  * \author mdejong
239  */
240 int main(int argc, char **argv)
241 {
242  using namespace std;
243  using namespace JPP;
244 
245  {
246  typedef JGridPolint3Function1D_t JFunction1D_t;
248  JPolint3FunctionalGridMap>::maplist JMaplist_t;
249  typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
250 
251  if (do_main<JMultiFunction_t>(argc, argv, "polint") != 0) return 1;
252  }
253 
254  {
255  typedef JGridSplineFunction1D_t JFunction1D_t;
257  JSplineFunctionalGridMap>::maplist JMaplist_t;
258  typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
259 
260  if (do_main<JMultiFunction_t>(argc, argv, "spline") != 0) return 1;
261  }
262 }
JMessage.hh
ASSERT
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
JPrint.hh
JPARSER::initialised
Empty structure for specification of parser element that is initialised (i.e.
Definition: JParser.hh:63
JMATH::JPolynome
Polynome function object.
Definition: JPolynome.hh:26
JTOOLS::JSplineFunctionalGridMap
Type definition of a spline interpolation based on a JGridMap implementation.
Definition: JFunctionalMap_t.hh:37
JTOOLS::JGridSplineFunction1D_t
Type definition of a spline interpolation based on a JGridCollection with result type double.
Definition: JFunction1D_t.hh:59
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
f3
double f3(const double x, const double y, const double z)
3D function.
Definition: JPolynome3D.cc:23
JFunctionalMap.hh
JTOOLS::JMAPLIST
Auxiliary class for recursive map list generation.
Definition: JMapList.hh:108
JTOOLS::getIntegral
JContainer_t::ordinate_type getIntegral(const JContainer_t &input)
Get integral of input data points.
Definition: JToolsToolkit.hh:133
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JFunction1D_t.hh
debug
int debug
debug level
Definition: JSirene.cc:59
JToolsToolkit.hh
JFunctionalMap_t.hh
main
int main(int argc, char **argv)
Definition: JPolynome3G.cc:240
SCIENTIFIC
Auxiliary data structure for floating point format specification.
Definition: JPrint.hh:518
std::map
Definition: JSTDTypes.hh:16
JParser.hh
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
JMultiFunction.hh
JTOOLS::JGridPolint3Function1D_t
Type definition of a 3rd degree polynomial interpolation based on a JGridCollection with result type ...
Definition: JFunction1D_t.hh:308
std
Definition: jaanetDictionary.h:36
JPolynome.hh
JMapList.hh
JTOOLS::JMultiFunction
Multidimensional interpolation method.
Definition: JMultiFunction.hh:40
FATAL
#define FATAL(A)
Definition: JMessage.hh:67
JEEP::debug_t
debug
Definition: JMessage.hh:29
JTOOLS::JPolint3FunctionalGridMap
Type definition of a 3rd degree polynomial interpolation based on a JGridMap implementation.
Definition: JFunctionalMap_t.hh:109