Jpp master_rocky-44-g75b7c4f75
the software that should make you happy
Loading...
Searching...
No Matches
JMultiFunction.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JMULTIFUNCTION__
2#define __JTOOLS__JMULTIFUNCTION__
3
4#include "JTools/JMultiMap.hh"
7#include "JTools/JArray.hh"
11
12
13/**
14 * \author mdejong
15 */
16
17namespace JTOOLS {}
18namespace JPP { using namespace JTOOLS; }
19
20namespace JTOOLS {
21
22 /**
23 * Multidimensional interpolation method.
24 *
25 * The template parameters respectively refer to:
26 * -# function object used for the lowest dimension(s);
27 * -# list of functional maps used for the higher dimension(s); and
28 * -# distance operator.
29 *
30 * The number of dimensions of this function object is equal to the length of
31 * the given map list plus the extra dimensions of the first function object.
32 * When converting a multidimensional histogram to a multidimensional function for
33 * subsequent interpolations, the function object used for the lowest dimension(s)
34 * is treated as a probability density function.
35 */
36 template<class JFunction_t,
37 class JMaplist_t,
40 public JMultiMap<typename JFunction_t::argument_type, JFunction_t, JMaplist_t, JDistance_t>
41 {
42 public:
43
44 typedef JMultiMap<typename JFunction_t::argument_type,
45 JFunction_t,
46 JMaplist_t,
47 JDistance_t> multimap_type;
48
49 enum { NUMBER_OF_DIMENSIONS = JMapLength<JMaplist_t>::value + JFunction_t::NUMBER_OF_DIMENSIONS };
51 typedef JFunction_t function_type;
52
53 typedef typename function_type::value_type value_type;
54 typedef typename function_type::argument_type argument_type;
55
56 typedef typename multimap_type::abscissa_type abscissa_type;
57 typedef typename multimap_type::ordinate_type ordinate_type;
58 typedef typename multimap_type::result_type result_type;
59
60 typedef typename multimap_type::const_iterator const_iterator;
61 typedef typename multimap_type::const_reverse_iterator const_reverse_iterator;
62 typedef typename multimap_type::iterator iterator;
63 typedef typename multimap_type::reverse_iterator reverse_iterator;
64
65 typedef typename multimap_type::super_iterator super_iterator;
66 typedef typename multimap_type::super_const_iterator super_const_iterator;
67
69 using multimap_type::insert;
70
71
72 /**
73 * Default constructor.
74 */
77
78
79 /**
80 * Constructor.
81 *
82 * \param input multidimensional input
83 */
84 template<class T>
85 JMultiFunction(const T& input)
86 {
87 insert(input);
88
89 this->compile();
90 }
91
92
93 /**
94 * Get multidimensional function.
95 *
96 * \return this multidimensional function
97 */
99 {
100 return static_cast<const JMultiFunction&>(*this);
101 }
102
103
104 /**
105 * Get multidimensional function.
106 *
107 * \return this multidimensional function
108 */
110 {
111 return static_cast<JMultiFunction&>(*this);
112 }
113
114
115 /**
116 * Insert multidimensional input.
117 *
118 * \param input multidimensional function
119 */
120 template<class __JFunction_t, class __JMaplist_t, class __JDistance_t>
122 {
123 copy(input, *this);
124 }
125
126
127 /**
128 * Insert multidimensional input.
129 *
130 * \param input multidimensional histogram
131 */
132 template<class JHistogram_t, class __JMaplist_t, class __JDistance_t>
137
138
139 /**
140 * Compilation.
141 */
142 void compile()
143 {
144 this->for_each(compiler);
145
146 for (super_iterator i = this->super_begin(); i != this->super_end(); ++i) {
147 (*i).getValue().compile();
148 }
149 }
150
151
152 /**
153 * Set the supervisor for handling of exceptions.
154 *
155 * \param supervisor supervisor
156 */
157 void setExceptionHandler(const typename function_type::supervisor_type& supervisor)
158 {
159 this->for_each(supervisor);
160
161 for (super_iterator i = this->super_begin(); i != this->super_end(); ++i) {
162 (*i).getValue().setExceptionHandler(supervisor);
163 }
164 }
165
166
167 /**
168 * Multi-dimensional interpolation method call.
169 *
170 * \param args comma seperated list of abscissa values
171 * \return function value
172 */
173 template<class ...Args>
174 result_type operator()(const Args& ...args) const
175 {
176 buffer.set(args...);
177
178 return this->evaluate(buffer.data());
179 }
180
181
182 protected:
184
185 /**
186 * Insert multidimensional histogram at multidimensional key.
187 *
188 * \param key multidimensional key
189 * \param input multidimensional histogram
190 */
191 template<unsigned int N,
192 class __JAbscissa_t,
193 class __JContents_t,
194 template<class, class, class> class __JMap_t,
195 class __JDistance_t>
198 {
199 if (input.size() > 1) {
200
201 for (auto j = input.begin(), i = j++; j != input.end(); ++i, ++j) {
202
203 const argument_type x = 0.5 * (i->getX() + j->getX());
204
205 insert(JMultiKey<N+1, argument_type>(key, x), i->getY());
206 }
207 }
208 }
209
210
211 /**
212 * Convert one-dimensional histogram to PDF and insert result at given multidimensional key.
213 *
214 * \param key multidimensional key
215 * \param input histogram
216 */
217 template<class __JElement_t, template<class, class> class __JContainer_t, class __JDistance_t>
220
221 {
222 JFunction_t buffer;
223
224 makePDF(input, buffer);
225
226 multimap_type::insert(key, buffer);
227 }
228
229
230 /**
231 * Convert multidimensional histogram to PDF and insert result at given multidimensional key.
232 *
233 * \param key multidimensional key
234 * \param input multidimensional histogram
235 */
236 template<class JHistogram_t, class __JMaplist_t, class __JDistance_t>
239 {
240 JFunction_t buffer;
241
242 makePDF(input, buffer);
243
244 multimap_type::insert(key, buffer);
245 }
246 };
247
248
249 /**
250 * Template specialisation of JMultiFunction for a JConstantFunction.
251 * The primary 2D function is reduced to a 1D function.
252 */
253 template<class JArgument_t,
254 class JResult_t,
255 template<class, class, class> class JMap_t,
256 class JDistance_t>
257 class JMultiFunction<JConstantFunction1D<JArgument_t, JResult_t>, JMapList<JMap_t>, JDistance_t> :
258 public JMap_t<JArgument_t, JResult_t, JDistance_t>,
259 public virtual JFunction1D<JArgument_t, typename JMap_t<JArgument_t, JResult_t, JDistance_t>::result_type>
260 {
261 public:
262
263 typedef JMap_t<JArgument_t, JResult_t, JDistance_t> multimap_type;
264
265 typedef typename multimap_type::abscissa_type abscissa_type;
266 typedef typename multimap_type::ordinate_type ordinate_type;
267
268 typedef typename multimap_type::argument_type argument_type;
269 typedef typename multimap_type::result_type result_type;
270
271 typedef typename multimap_type::const_iterator const_iterator;
272 typedef typename multimap_type::const_reverse_iterator const_reverse_iterator;
273 typedef typename multimap_type::iterator iterator;
274 typedef typename multimap_type::reverse_iterator reverse_iterator;
275
277 typedef JMap_t<JArgument_t, JResult_t, JDistance_t> map_type;
278
280
281 using multimap_type::insert;
282
283
284 /**
285 * Insert element.
286 *
287 * \param key multidimensional key
288 * \param function function
289 */
290 void insert(const JMultiKey<1, abscissa_type>& key, const function_type& function)
291 {
292 this->insert(key.first, function.getY());
293 }
294 };
295
296
297 /**
298 * Template specialisation of JMultiFunction for a JConstantFunction.
299 * The number of dimensions of the primary function is reduced by one.
300 */
301 template<class JArgument_t,
302 class JResult_t,
303 class JMaplist_t,
304 class JDistance_t>
305 class JMultiFunction<JConstantFunction1D<JArgument_t, JResult_t>, JMaplist_t, JDistance_t> :
306 public JMultiFunction<JMultiFunction<JConstantFunction1D<JArgument_t, JResult_t>,
307 typename JMaplist_t::tail_type,
308 JDistance_t>,
309 typename JMaplist_t::head_list,
310 JDistance_t>
311 {
312 public:
313
315 typename JMaplist_t::tail_type,
316 JDistance_t>,
317 typename JMaplist_t::head_list,
318 JDistance_t> multifunction_type;
319
321
322 typedef typename multimap_type::abscissa_type abscissa_type;
323 typedef typename multimap_type::ordinate_type ordinate_type;
324
325 typedef typename multimap_type::argument_type argument_type;
326 typedef typename multimap_type::result_type result_type;
327
328 typedef typename multimap_type::const_iterator const_iterator;
329 typedef typename multimap_type::const_reverse_iterator const_reverse_iterator;
330 typedef typename multimap_type::iterator iterator;
331 typedef typename multimap_type::reverse_iterator reverse_iterator;
332
333 typedef typename multimap_type::super_iterator super_iterator;
334 typedef typename multimap_type::super_const_iterator super_const_iterator;
335
338 typename JMaplist_t::tail_type,
339 JDistance_t> map_type;
340
342
343 using multimap_type::insert;
344
345
346 /**
347 * Insert element.
348 *
349 * \param key multidimensional key
350 * \param function function
351 */
353 {
354 this->get(key.front()).put(key.back(), function.getY());
355 }
356 };
357
358
359 /**
360 * Conversion of multidimensional histogram to multidimensional function.
361 *
362 * \param input multidimensional histogram
363 * \param output multidimensional function
364 */
365 template<class JHistogram_t,
366 class JHistogramMaplist_t,
367 class JHistogramDistance_t,
368 class JFunction_t,
369 class JFunctionMaplist_t,
370 class JFunctionDistance_t>
376}
377
378#endif
General purpose multidimensional map based on a type list of maps.
One dimensional array of template objects with fixed length.
Definition JArray.hh:43
const_pointer data() const
Get pointer to data.
Definition JArray.hh:284
JArray & set(const Args &...args)
Set array.
Definition JArray.hh:199
Template implementation of function object in one dimension returning a constant value.
result_type getY() const
Function value.
Template definition of function object interface.
Histogram in 1D.
void insert(const JMultiKey< NUMBER_OF_DIMENSIONS, abscissa_type > &key, const function_type &function)
Insert element.
JMultiFunction< JConstantFunction1D< JArgument_t, JResult_t >, typename JMaplist_t::tail_type, JDistance_t > map_type
JMultiFunction< JMultiFunction< JConstantFunction1D< JArgument_t, JResult_t >, typename JMaplist_t::tail_type, JDistance_t >, typename JMaplist_t::head_list, JDistance_t > multifunction_type
void insert(const JMultiKey< 1, abscissa_type > &key, const function_type &function)
Insert element.
Multidimensional interpolation method.
multimap_type::super_iterator super_iterator
multimap_type::super_const_iterator super_const_iterator
void insert(const JMultiKey< JMapLength< JMaplist_t >::value, argument_type > &key, const JMultiHistogram< JHistogram_t, __JMaplist_t, __JDistance_t > &input)
Convert multidimensional histogram to PDF and insert result at given multidimensional key.
multimap_type::result_type result_type
result_type operator()(const Args &...args) const
Multi-dimensional interpolation method call.
void setExceptionHandler(const typename function_type::supervisor_type &supervisor)
Set the supervisor for handling of exceptions.
multimap_type::ordinate_type ordinate_type
JMultiFunction()
Default constructor.
void compile()
Compilation.
multimap_type::const_iterator const_iterator
multimap_type::iterator iterator
void insert(const JMultiFunction< __JFunction_t, __JMaplist_t, __JDistance_t > &input)
Insert multidimensional input.
multimap_type::reverse_iterator reverse_iterator
multimap_type::const_reverse_iterator const_reverse_iterator
JMultiFunction & getMultiFunction()
Get multidimensional function.
const JMultiFunction & getMultiFunction() const
Get multidimensional function.
void insert(const JMultiKey< N, argument_type > &key, const JHistogramMap< __JAbscissa_t, __JContents_t, __JMap_t, __JDistance_t > &input)
Insert multidimensional histogram at multidimensional key.
JMultiFunction(const T &input)
Constructor.
void insert(const JMultiKey< JMapLength< JMaplist_t >::value, argument_type > &key, const JHistogram1D< __JElement_t, __JContainer_t, __JDistance_t > &input)
Convert one-dimensional histogram to PDF and insert result at given multidimensional key.
JArray< NUMBER_OF_DIMENSIONS, argument_type > buffer
JMultiMap< typename JFunction_t::argument_type, JFunction_t, JMaplist_t, JDistance_t > multimap_type
function_type::value_type value_type
void insert(const JMultiHistogram< JHistogram_t, __JMaplist_t, __JDistance_t > &input)
Insert multidimensional input.
function_type::argument_type argument_type
multimap_type::abscissa_type abscissa_type
Multidimensional histogram.
Multidimensional key.
Definition JMultiKey.hh:69
key_type back() const
Get backend key.
Definition JMultiKey.hh:174
JMultiKey< N-1, JKey_t > front() const
Get frontend key.
Definition JMultiKey.hh:163
Multidimensional map.
Definition JMultiMap.hh:52
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
void copy(const T &input, T &output)
Copy of input to output.
static const JCompiler compiler
Function object for functional object compilation.
void makePDF(const JHistogram1D< JElement_t, JContainer_t, JDistance_t > &input, typename JMappable< JElement_t >::map_type &output)
Conversion of histogram to probability density function (PDF).
JObject_t & for_each(JObject_t &object, JType< JTypeList< JHead_t, JTail_t > > typelist, const JTuple< T > &tuple)
For each data type method.
Definition JTuple.hh:666
int j
Definition JPolint.hh:792
Template class for distance evaluation.
Definition JDistance.hh:24
Template definition of function object interface in one dimension.
Length of map list.
Definition JMapList.hh:45
Map list.
Definition JMapList.hh:25