Jpp  18.2.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTrigonometric1D.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <sstream>
4 #include <iomanip>
5 
6 #include "TRandom3.h"
7 
8 #include "JLang/JException.hh"
10 #include "JMath/JConstants.hh"
11 #include "JTools/JFunction1D_t.hh"
12 #include "JTools/JGrid.hh"
13 #include "JTools/JQuantile.hh"
14 
15 #include "Jeep/JParser.hh"
16 #include "Jeep/JMessage.hh"
17 
18 namespace {
19 
20  /**
21  * Test method for interpolation with derivatives.
22  *
23  * Accuracy of nth derivative of N degree polynome
24  * is equal to n-jth derivative of N-j degree polynome.
25  *
26  * \param argc number of command line arguments
27  * \param argv list of command line arguments
28  * \return status
29  */
30  template<int N>
31  int do_main(int argc, char **argv)
32  {
33  using namespace std;
34  using namespace JPP;
35 
36  unsigned int numberOfEvents;
37  unsigned int numberOfBins;
38  int debug;
39 
40  try {
41 
42  JParser<> zap("Example program to test 1D interpolation of a polynome.");
43 
44  zap['n'] = make_field(numberOfEvents);
45  zap['N'] = make_field(numberOfBins) = 21;
46  zap['d'] = make_field(debug) = 3;
47 
48  zap(argc, argv);
49  }
50  catch(const exception &error) {
51  FATAL(error.what() << endl);
52  }
53 
54  if (N < 1) {
55  FATAL("Number of degrees " << N << " < 1." << endl);
56  }
57 
58 
59  using namespace JPP;
60 
61  // Analytical function and its derivatives.
62 
63  vector<JTrigonometric> f1(1, JTrigonometric(sin));
64 
65  for (int i = 1; i != N + 1; ++i) {
66  f1.push_back(f1.rbegin()->getDerivative());
67  }
68 
69 
70  typedef JResultPolynome<N, double> JResult_t;
71  typedef JPolintFunction1D<N, JElement2D<double, double>, JCollection, JResult_t> JFunction1D_t;
72 
73  JFunction1D_t polint;
74 
75  const double xmin = 0.0;
76  const double xmax = PI;
77 
78  //const double dx = ((N+1)/2) * (xmax - xmin) / (double) (numberOfBins - 2*((N+1)/2));
79  const double dx = 0.0 * PI;
80 
81  const JGrid<double> grid(numberOfBins + 1, xmin - dx, xmax + dx);
82 
83  polint.configure(grid, f1[0]);
84 
85  polint.compile();
86 
87  polint.setExceptionHandler(new typename JFunction1D_t::JDefaultResult(JMATH::zero));
88 
89 
90  if (numberOfEvents != 0) {
91 
92  JQuantile Q[N+1];
93 
94  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
95 
96  ostringstream os;
97 
98  os << "f^" << i;
99 
100  Q[i].setTitle(os.str());
101  }
102 
103  for (unsigned int i = 0; i != numberOfEvents; ++i) {
104 
105  const double x = gRandom->Uniform(xmin, xmax);
106  const JResult_t result = polint(x);
107 
108  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
109  Q[i].put(f1[i](x) - result.y[i]);
110  }
111  }
112 
113  cout << "\nInterpolation with " << N << " degrees polynomial:" << endl;
114 
115  for (int i = 0; i != sizeof(Q)/sizeof(Q[0]); ++i) {
116  Q[i].print(cout, i == 0);
117  }
118  }
119 
120  return 0;
121  }
122 }
123 
124 /**
125  * \file
126  *
127  * Example program to test 1D interpolation of a trigonometric function.
128  * \author mdejong
129  */
130 int main(int argc, char **argv)
131 {
132  if (do_main<2>(argc, argv) != 0) { return 1; }
133  if (do_main<3>(argc, argv) != 0) { return 1; }
134  if (do_main<4>(argc, argv) != 0) { return 1; }
135  if (do_main<5>(argc, argv) != 0) { return 1; }
136 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1514
Exceptions.
Q(UTCMax_s-UTCMin_s)-livetime_s
int main(int argc, char *argv[])
Definition: Main.cc:15
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
then usage set_variable ACOUSTICS_WORKDIR $WORKDIR set_variable FORMULA sin([0]+2 *$PI *([1]+[2]*x)*x)" set_variable DY 1.0e-8 mkdir $WORKDIR for DETECTOR in $DETECTORS[*]
const JPolynome f1(1.0, 2.0, 3.0)
Function.
double getDerivative(const double x) const
Derivative value.
Definition: JPolynome.hh:252
Mathematical constants.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1989
static const double PI
Mathematical constants.
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
const double xmin
Definition: JQuadrature.cc:23
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
Utility class to parse command line options.
int numberOfBins
number of bins for average CDF integral of optical module
Definition: JSirene.cc:69
int debug
debug level