Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JPolynome2Dx1D.cc
Go to the documentation of this file.
1
2#include <iostream>
3#include <iomanip>
4#include <cmath>
5
6#include "TRandom3.h"
7
8#include "JTools/JTable1D.hh"
9#include "JTools/JGrid.hh"
10#include "JTools/JElement.hh"
11#include "JTools/JPolint.hh"
13#include "JTools/JMapList.hh"
17#include "JTools/JStats.hh"
18
19#include "Jeep/JPrint.hh"
20#include "Jeep/JTimer.hh"
21#include "Jeep/JParser.hh"
22#include "Jeep/JMessage.hh"
23
24
25namespace {
26
27 /**
28 * 3D function.
29 * smooth in x and y, arbitrary in z
30 *
31 * \param x x value
32 * \param y y value
33 * \param z z value
34 * \return function value
35 */
36 inline double f3(const double x,
37 const double y,
38 const double z)
39 {
40 return sin(z/2)*exp(-fabs(z)) * (x*x - x*y - y*y + x -2*y + 3) / 25;
41 }
42}
43
44
45/**
46 * \file
47 *
48 * Example program to test interpolation on 2D grid between 1D tables.
49 * \author hbaetsen
50 */
51int main(int argc, char **argv)
52{
53 using namespace std;
54 using namespace JPP;
55
56 int numberOfEvents;
57 int debug;
58 double precision;
59
60 try {
61
62 JParser<> zap("Example program to test interpolation between 1D tables on 2D grid.");
63
64 zap['n'] = make_field(numberOfEvents);
65 zap['d'] = make_field(debug) = 2;
66 zap['p'] = make_field(precision) = 1e-6;
67
68 zap(argc, argv);
69 }
70 catch(const exception &error) {
71 FATAL(error.what() << endl);
72 }
73
74
75 const int NX = 19; // 11
76 const int NY = 35; // 11
77 const int NZ = 16384; // 21
78
79 const JGrid<double> X(NX, -5.0, +5.0);
80 const JGrid<double> Y(NY, -5.0, +5.0);
81 const JGrid<double> Z(NZ, -10.0, +10.0);
82
83
84 // Define 2D 2nd order interpolants
85 typedef JTable1D<NZ> JTable1D_t;
88 typedef JMapList<JPolint2FunctionalGridMap> JMaplist_t;
89 typedef JMultiFunction<JFunction1D_t, JMaplist_t> JMultiFunction_t;
90
91 JMultiFunction_t g3;
92
93 for (int ix = 0; ix != X.getSize(); ++ix) {
94 for (int iy = 0; iy != Y.getSize(); ++iy) {
95
96 DEBUG("table[" << ix << ", " << iy << "]" << endl);
97
98 const double x = X.getX(ix);
99 const double y = Y.getX(iy);
100
101 JTable1D_t& table = g3[x][y];
102
103 for (int iz = 0; iz != Z.getSize(); ++iz) {
104
105 const double z = Z.getX(iz);
106 table[iz] = f3(x,y,z);
107
108 DEBUG(' ' << FIXED(8,3) << table[iz]);
109
110 }
111 DEBUG(endl);
112 }
113 }
114
115 g3.compile();
116
117 // Random events
118 JTimer t1("JTable1D interpolant");
119
120 for (int i = 0; i != numberOfEvents; ++i) {
121
122 const double x = gRandom->Uniform(X.getXmin(), X.getXmax());
123 const double y = gRandom->Uniform(Y.getXmin(), Y.getXmax());
124 DEBUG("x = " << x << " y = " << y << endl);
125
126 t1.start();
127 JTable1D_t res = g3(x,y);
128 t1.stop();
129
130 int valid = 1;
131
132 DEBUG(FIXED(9,3) << "f(x,y,z)" << FIXED(9,3) << "interp" << endl);
133
134 for (int iz = 0; iz != Z.getSize(); ++iz) {
135 const double z = Z.getX(iz);
136 double val_f3 = f3(x,y,z);
137
138 DEBUG(FIXED(9,3) << val_f3 << FIXED(9,3) << res[iz] << endl);
139 if (fabs((val_f3 - res[iz]) / res[iz]) > precision) {valid = 0;}
140
141 }
142
143 ASSERT(valid, FIXED(5,0) << i << ": [x, y] = " << FIXED(9,3) << x << FIXED(9,3) << y);
144
145 }
146
147 for (JTimer buffer[] = { t1, JTimer() }, *i = buffer; i->getTitle() != ""; ++i) {
148 i->print(cout, true, micro_t);
149 }
150
151}
The elements in a collection are sorted according to their abscissa values and a given distance opera...
Various implementations of functional maps.
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ASSERT(A,...)
Assert macro.
Definition JMessage.hh:90
#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)
double f3(const double x, const double y, const double z)
3D function.
I/O formatting auxiliaries.
Auxiliary class for CPU timing and usage.
Definition JTimer.hh:33
void stop()
Stop timer.
Definition JTimer.hh:127
void start()
Start timer.
Definition JTimer.hh:106
const std::string & getTitle() const
Get title.
Definition JTitle.hh:55
Utility class to parse command line options.
Definition JParser.hh:1698
Multidimensional interpolation method.
void compile()
Compilation.
Template class for polynomial interpolation in 1D.
Definition JPolint.hh:1095
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
2D Element.
Definition JPolint.hh:1131
Simple data structure for an abstract collection of equidistant abscissa values.
Definition JGrid.hh:40
virtual int getSize() const override
Get number of elements.
Definition JGrid.hh:75
virtual abscissa_type getXmin() const override
Get minimal abscissa value.
Definition JGrid.hh:98
virtual abscissa_type getXmax() const override
Get maximal abscissa value.
Definition JGrid.hh:109
virtual abscissa_type getX(int index) const override
Get abscissa value.
Definition JGrid.hh:87
Map list.
Definition JMapList.hh:25
1D table with arithmetic capabilities.
Definition JTable1D.hh:29