Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JPolynome1P.cc
Go to the documentation of this file.
1#include <string>
2#include <iostream>
3#include <sstream>
4#include <iomanip>
5#include <vector>
6
7#include "TRandom3.h"
8
9#include "JLang/JException.hh"
10#include "JMath/JPolynome.hh"
12#include "JTools/JGrid.hh"
13#include "JTools/JQuantile.hh"
14
15#include "Jeep/JParser.hh"
16#include "Jeep/JMessage.hh"
17
18
19/**
20 * \file
21 *
22 * Example program to test 1D interpolation of a polynome.
23 * \author mdejong
24 */
25int main(int argc, char **argv)
26{
27 using namespace std;
28 using namespace JPP;
29
30 unsigned int numberOfEvents;
31 unsigned int numberOfBins;
32 JPolynome f1;
33 double x0;
34 int debug;
35
36 try {
37
38 JParser<> zap("Example program to test 1D interpolation of a polynome.");
39
40 zap['n'] = make_field(numberOfEvents) = 0;
41 zap['N'] = make_field(numberOfBins) = 21;
42 zap['P'] = make_field(f1);
43 zap['x'] = make_field(x0) = 0.0;
44 zap['d'] = make_field(debug) = 1;
45
46 zap(argc, argv);
47 }
48 catch(const exception &error) {
49 FATAL(error.what() << endl);
50 }
51
52 if (f1.empty()) {
53 FATAL("Invalid polynomial.");
54 }
55
56
57 const int N = 3; // degree of polynomial interpolation
58
59 typedef JResultPolynome<N, double> JResult_t;
60 typedef JPolintFunction1D<N, JElement2D<double, double>, JCollection, JResult_t> JFunction1D_t;
61
62
63 JFunction1D_t polint;
64
65
66 const double xmin = -1.0;
67 const double xmax = +1.0;
68
69 polint.configure(JGrid<double>(numberOfBins, xmin, xmax), f1);
70
71 polint.compile();
72
73 polint.setExceptionHandler(new JFunction1D_t::JDefaultResult(JMATH::zero));
74
75
76 vector<JPolynome> fp(1, f1);
77
78 for (int i = 1; i != (int) f1.size(); ++i) {
79 fp.push_back(fp.rbegin()->getDerivative());
80 }
81
82 DEBUG("polynome: ");
83
84 for (int i = 0; i != (int) f1.size(); ++i) {
85 DEBUG(' ' << fp[i](x0));
86 }
87 DEBUG(endl);
88
89 DEBUG("result: ");
90
91 JResult_t result = polint(x0);
92
93 for (int i = 0; i != N+1; ++i) {
94 DEBUG(' ' << result.y[i]);
95 }
96 DEBUG(endl);
97
98
99 if (numberOfEvents != 0) {
100
101 JQuantile Q[N+1];
102
103 for (int j = 0; j != sizeof(Q)/sizeof(Q[0]); ++j) {
104
105 ostringstream os;
106
107 os << "f^" << j;
108
109 Q[j].setTitle(os.str());
110 }
111
112 const int M = min(fp.size(), sizeof(Q)/sizeof(Q[0]));
113
114 for (unsigned int i = 0; i != numberOfEvents; ++i) {
115
116 const double x = gRandom->Uniform(xmin, xmax);
117
118 JResult_t result = polint(x);
119
120 for (int j = 0; j != M; ++j) {
121 Q[j].put(fp[j](x) - result.y[j]);
122 }
123 }
124
125 for (int j = 0; j != M; ++j) {
126 Q[j].print(cout, j == 0);
127 }
128 }
129}
Exceptions.
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#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
int main(int argc, char **argv)
int numberOfBins
number of bins for average CDF integral of optical module
Definition JSirene.cc:73
void setTitle(const std::string &title)
Set title.
Definition JTitle.hh:66
Utility class to parse command line options.
Definition JParser.hh:1698
General purpose class for collection of elements, see: <a href="JTools.PDF";>Collection of elements....
Definition JSet.hh:22
Template class for polynomial interpolation in 1D.
Definition JPolint.hh:1095
static const JZero zero
Function object to assign zero value.
Definition JZero.hh:105
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Recursive template class for polynomial function.
Definition JPolynome.hh:165
Simple data structure for an abstract collection of equidistant abscissa values.
Definition JGrid.hh:40
Auxiliary data structure for running average, standard deviation and quantiles.
Definition JQuantile.hh:46
std::ostream & print(std::ostream &out, bool lpr=true) const
Print quantile.
Definition JQuantile.hh:382
void put(const double x, const double w=1.0)
Put value.
Definition JQuantile.hh:133
Data structure for result including value and N derivatives of function.
Definition JResult.hh:535