Jpp 19.3.0-rc.3
the software that should make you happy
Loading...
Searching...
No Matches
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"
12#include "JTools/JGrid.hh"
13#include "JTools/JQuantile.hh"
14
15#include "Jeep/JParser.hh"
16#include "Jeep/JMessage.hh"
17
18namespace {
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
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 */
130int 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}
Exceptions.
Mathematical constants.
General purpose messaging.
#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 numberOfBins
number of bins for average CDF integral of optical module
Definition JSirene.cc:73
int main(int argc, char **argv)
void setTitle(const std::string &title)
Set title.
Definition JTitle.hh:66
Trigonometric function object for sin and cos.
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
const JPolynome f1(1.0, 2.0, 3.0)
Function.
const double xmax
const double xmin
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).
double getDerivative(const double x) const
Derivative value.
Definition JMathlib.hh:1433
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