Jpp  17.3.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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  * \param argc number of command line arguments
161  * \param argv list of command line arguments
162  * \param title title of this template process
163  * \return status
164  */
165  template<class JMultiFunction_t>
166  int do_main(int argc, char **argv, const char* title)
167  {
168  using namespace std;
169 
170  int numberOfBins;
171  JPolynome f1;
172  map<string, double> precision;
173  int debug;
174 
175  precision["spline"] = 1.0e-3;
176  precision["polint"] = 1.0e-10;
177 
178  try {
179 
180  JParser<> zap("Example program to test multi-dimensional integration.");
181 
182  zap['N'] = make_field(numberOfBins) = 11;
183  zap['P'] = make_field(f1) = JPolynome(1.0, 1.0, 1.0);
184  zap['e'] = make_field(precision) = JPARSER::initialised();
185  zap['d'] = make_field(debug) = 3;
186 
187  zap(argc, argv);
188  }
189  catch(const exception &error) {
190  FATAL(error.what() << endl);
191  }
192 
193 
194  if (numberOfBins <= 2) {
195  FATAL("Number of bins " << numberOfBins << endl);
196  }
197 
198  using namespace JPP;
199 
200 
201  const JFunction3D f3(f1);
202 
203  const double xmin = -1.0;
204  const double xmax = +1.0;
205  const double dx = (xmax - xmin) / (numberOfBins - 1);
206 
207 
208  JMultiFunction_t g3;
209 
210  for (double x = xmin; x < xmax + 0.5*dx; x += dx) {
211  for (double y = xmin; y < xmax + 0.5*dx; y += dx) {
212  for (double z = xmin; z < xmax + 0.5*dx; z += dx) {
213  g3[x][y][z] = f3(x,y,z);
214  }
215  }
216  }
217 
218  g3.compile();
219  //g3.setExceptionHandler(new JFunction1D_t::JDefaultResult(JMATH::zero));
220 
221  if (debug >= debug_t) {
222  cout << endl
223  << setw(8) << left << title << ' '
224  << setw(12) << right << "real" << ' '
225  << setw(12) << right << "calculated" << endl;
226  cout << setw(8) << left << "integral";
227  cout << ' ' << SCIENTIFIC(12,4) << f3(xmin, xmax, xmin, xmax, xmin, xmax);
228  cout << ' ' << SCIENTIFIC(12,4) << getIntegral(g3);
229  cout << endl;
230  }
231 
232  ASSERT(fabs(f3(xmin, xmax, xmin, xmax, xmin, xmax) - getIntegral(g3)) <= precision[title] * f3(xmin, xmax, xmin, xmax, xmin, xmax));
233 
234  return 0;
235  }
236 }
237 
238 
239 /**
240  * \file
241  *
242  * Example program to test multi-dimensional integration.
243  * \author mdejong
244  */
245 int main(int argc, char **argv)
246 {
247  using namespace std;
248  using namespace JPP;
249 
250  {
251  typedef JGridPolint3Function1D_t JFunction1D_t;
253  JPolint3FunctionalGridMap>::maplist JMaplist_t;
254  typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
255 
256  if (do_main<JMultiFunction_t>(argc, argv, "polint") != 0) return 1;
257  }
258 
259  {
260  typedef JGridSplineFunction1D_t JFunction1D_t;
262  JSplineFunctionalGridMap>::maplist JMaplist_t;
263  typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
264 
265  if (do_main<JMultiFunction_t>(argc, argv, "spline") != 0) return 1;
266  }
267 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1517
debug
Definition: JMessage.hh:29
int main(int argc, char *argv[])
Definition: Main.cc:15
Type definition of a 3rd degree polynomial interpolation based on a JGridMap implementation.
Type definition of a spline interpolation based on a JGridMap implementation.
o $QUALITY_ROOT d $DEBUG!CHECK_EXIT_CODE JPlot1D f
Definition: JDataQuality.sh:76
Type definition of a spline interpolation based on a JGridCollection with result type double...
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:83
Type definition of a 3rd degree polynomial interpolation based on a JGridCollection with result type ...
Various implementations of functional maps.
const JPolynome f1(1.0, 2.0, 3.0)
Function.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
I/O formatting auxiliaries.
Multidimensional interpolation method.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1993
This include file contains various recursive methods to operate on multi-dimensional collections...
Auxiliary class for recursive map list generation.
Definition: JMapList.hh:108
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
const double xmin
Definition: JQuadrature.cc:23
Utility class to parse command line options.
double f3(const double x, const double y, const double z)
3D function.
Definition: JPolynome3D.cc:23
Polynome function object.
Definition: JPolynome.hh:163
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:484
int numberOfBins
number of bins for average CDF integral of optical module
Definition: JSirene.cc:69
JContainer_t::ordinate_type getIntegral(const JContainer_t &input)
Get integral of input data points.
int debug
debug level