Jpp 21.0.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
JToolsToolkit.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JTOOLSTOOLKIT__
2#define __JTOOLS__JTOOLSTOOLKIT__
3
4#include <type_traits>
5
6
7#include "JLang/JException.hh"
8#include "JMath/JZero.hh"
10#include "JTools/JElement.hh"
11#include "JTools/JCollection.hh"
13#include "JTools/JRange.hh"
17
18
19/**
20 * \file
21 *
22 * This include file contains various recursive methods to operate on multi-dimensional collections.
23 * \author mdejong
24 */
25namespace JTOOLS {}
26namespace JPP { using namespace JTOOLS; }
27
28/**
29 * Auxiliary classes and methods for multi-dimensional interpolations and histograms.
30 */
31namespace JTOOLS {
32
36
37
38 /**
39 * Conversion of data points to cumulative probability distribition (CDF).
40 *
41 * Note that the data type of the input container should be preserved
42 * so that the corresponding method <tt>integrate()</tt> is used.
43 *
44 * \param input collection
45 * \param output mappable collection
46 * \param eps minimal step size
47 * \return integral value
48 */
49 template<class JContainer_t,
50 class JKey_t,
51 class JValue_t>
52 inline typename JContainer_t::ordinate_type
53 makeCDF(const JContainer_t& input,
55 const typename JContainer_t::ordinate_type eps = JMATH::zero)
56 {
57 typedef typename JContainer_t::ordinate_type ordinate_type;
58 typedef typename JContainer_t::abscissa_type abscissa_type;
60 typedef JCollection<element_type> buffer_type;
61
62
63 if (input.getSize() > 1) {
64
65 buffer_type buffer;
66
67 const ordinate_type V = integrate(input, buffer);
68
69 if (V == ordinate_type(JMATH::zero)) {
70 THROW(JDivisionByZero, "Method makeCDF(): integral equals zero.");
71 }
72
73 output.clear();
74
75 typename buffer_type::const_iterator i = buffer.begin();
76
77 for ( ; i != buffer.end() && i->getY() <= 0.5 * eps * V; ++i) {}
78
79 if (i != buffer.end()) {
80
81 // force first point at (0, ymin)
82
83 JKey_t x = 0.0;
84 JValue_t y = i->getX();
85
86 output.put(x, y);
87
88 JKey_t xmax = x;
89 JValue_t ymax = y;
90
91 for (++i; i != buffer.end(); ++i) {
92
93 x = i->getY() / V;
94 y = i->getX();
95
96 if (x > xmax) {
97
98 ymax = y;
99
100 if (x > xmax + eps) {
101
102 output.put(x, y);
103
104 xmax = x;
105 }
106 }
107 }
108
109 // force last point at (1, ymax)
110
111 x = 1.0;
112 y = ymax;
113
114 output.put(x,y);
115
116 } else {
117 THROW(JDivisionByZero, "Method makeCDF(): no remaining data.");
118 }
119
120 return V;
121
122 } else {
123 THROW(JEmptyCollection, "Method makeCDF(): not sufficient input data.");
124 }
125 }
126
127
128 /**
129 * Get integral of input data points.
130 *
131 * \param input input data
132 * \return integral value
133 */
134 template<class JContainer_t>
135 inline typename JContainer_t::ordinate_type getIntegral(const JContainer_t& input)
136 {
138
139 return integrate(input, garbage);
140 }
141
142
143 /**
144 * Get integral of input data points.
145 *
146 * \param input input data
147 * \param range range
148 * \return integral value
149 */
150 template<class JContainer_t>
151 inline typename JContainer_t::ordinate_type getIntegral(const JContainer_t& input,
153 {
154 JContainer_t output;
155
156 integrate(input, output);
157
158 if (!output.empty())
159 return ((output.in_range(range.getUpperLimit()) ? get_value(output(range.getUpperLimit())) : output.rbegin()->getY()) -
160 (output.in_range(range.getLowerLimit()) ? get_value(output(range.getLowerLimit())) : output. begin()->getY()));
161 else
162 return 0.0;
163 }
164
165
166 /**
167 * Auxiliary method to get integral of input data points.
168 *
169 * \param input input data
170 * \return integral value
171 */
172 template<class JFunction_t,
173 template<class, class, class> class JMap_t,
174 class JTail_t>
175 inline typename JFunction_t::ordinate_type
176 getIntegral(const JMultiMap<typename JFunction_t::abscissa_type, JFunction_t, JMapList<JMap_t, JTail_t>, typename JFunction_t::distance_type>& input)
177 {
178 typedef typename JFunction_t::abscissa_type abscissa_type;
179 typedef typename JFunction_t::ordinate_type ordinate_type;
180 typedef typename JFunction_t::distance_type distance_type;
181 typedef typename JFunctionalMap<JMap_t>::template function_type<abscissa_type,
182 ordinate_type,
183 ordinate_type,
184 distance_type> buffer_type;
185 typedef JMultiMap<abscissa_type, JFunction_t, JMapList<JMap_t, JTail_t>, distance_type> multimap_type;
186
187 static buffer_type buffer;
188
189 buffer.configure(input);
190
191 typename buffer_type::iterator out = buffer.begin();
192
193 for (typename multimap_type::const_iterator in = input.begin(); in != input.end(); ++in, ++out) {
194 out->getY() = getIntegral(in->getY());
195 }
196
197 buffer.compile();
198
199 return getIntegral(buffer);
200 }
201
202
203 /**
204 * Get integral of input data points.
205 *
206 * \param input input data
207 * \return integral value
208 */
209 template<class JFunction_t,
210 template<class, class, class> class JMap_t,
211 class JTail_t>
212 inline typename JFunction_t::ordinate_type
213 getIntegral(const JMultiFunction<JFunction_t, JMapList<JMap_t, JTail_t>, typename JFunction_t::distance_type>& input)
214 {
215 typedef typename JFunction_t::abscissa_type abscissa_type;
216 typedef typename JFunction_t::ordinate_type ordinate_type;
217 typedef typename JFunction_t::distance_type distance_type;
218 typedef typename JFunctionalMap<JMap_t>::template function_type<abscissa_type,
219 ordinate_type,
220 ordinate_type,
221 distance_type> buffer_type;
222 typedef JMultiFunction<JFunction_t, JMapList<JMap_t, JTail_t>, distance_type> multifunction_type;
223
224 buffer_type buffer;
225
226 for (typename multifunction_type::const_iterator i = input.begin(); i != input.end(); ++i) {
227 buffer.put(i->getX(), getIntegral(i->getY()));
228 }
229
230 buffer.compile();
231
232 return getIntegral(buffer);
233 }
234
235
236 /**
237 * Reset value.
238 *
239 * The value is set to (the equivalent of) zero, see method JMATH::getZero.
240 *
241 * \param value value
242 */
243 template<class T>
244 inline void reset(T& value)
245 {
246 value = JMATH::getZero<T>();
247 }
248
249
250 /**
251 * Recursive reset of collection.
252 *
253 * \param collection collection
254 */
255 template<class JElement_t, class JDistance_t>
257 {
258 typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
259
260 for (iterator i = collection.begin(); i != collection.end(); ++i) {
261 reset(i->getY());
262 }
263 }
264
265
266 /**
267 * Copy of input to output.
268 *
269 * The output value is set to the input value.
270 *
271 * \param input input
272 * \param output output
273 */
274 template<class T>
275 inline void copy(const T& input, T& output)
276 {
277 output = input;
278 }
279
280
281 /**
282 * Recursive copy of input collection to output collection.
283 *
284 * \param input input
285 * \param output output
286 */
287 template<class JElement_t, class JDistance_t, class JKey_t, class JValue_t>
289 {
290 typedef typename JCollection<JElement_t, JDistance_t>::const_iterator const_iterator;
291
292 output.clear();
293
294 for (const_iterator i = input.begin(); i != input.end(); ++i) {
295 copy(i->getY(), output.get(i->getX()));
296 }
297 }
298
299
300 /**
301 * Configuration of value.
302 *
303 * This is a fall-back method; it does nothing.
304 *
305 * \param value value
306 * \param bounds bounds
307 * \param option false
308 */
309 template<class T, class JAbscissa_t>
310 inline void configure(const T& value, const JAbstractCollection<JAbscissa_t>& bounds, std::false_type option)
311 {}
312
313
314 /**
315 * Recursive configuration of collection.
316 *
317 * \param collection collection
318 * \param bounds bounds
319 * \param option true
320 */
321 template<class JElement_t, class JDistance_t>
324 std::true_type option = std::true_type())
325 {
326 typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
327 typedef typename JCollection<JElement_t, JDistance_t>::ordinate_type ordinate_type;
328
329 collection.configure(bounds);
330
331 for (iterator i = collection.begin(); i != collection.end(); ++i) {
332 configure(i->getY(), bounds, std::bool_constant<JAssembler<ordinate_type>::is_collection>());
333 }
334 }
335
336
337 /**
338 * Accumulation of value.
339 *
340 * This is a fall-back method; it does nothing.
341 *
342 * \param value value
343 * \param option false
344 */
345 template<class T>
346 inline void accumulate(T& value, std::false_type option)
347 {}
348
349
350 /**
351 * Recursive accumulation of collection.
352 *
353 * \param collection collection
354 * \param option true
355 */
356 template<class JElement_t, class JDistance_t>
357 inline void accumulate(JCollection<JElement_t, JDistance_t>& collection, std::true_type option = std::true_type())
358 {
359 typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
360 typedef typename JCollection<JElement_t, JDistance_t>::ordinate_type ordinate_type;
361
362 for (iterator i = collection.begin(); i != collection.end(); ++i) {
363 accumulate(i->getY(), std::bool_constant<JAssembler<ordinate_type>::is_collection>());
364 }
365
366 if (collection.getSize() > 1) {
367
368 for (iterator j = collection.begin(), i = j++; j != collection.end(); ++i, ++j) {
369 j->getY() += i->getY();
370 }
371
372 reset(collection.begin()->getY());
373 }
374 }
375}
376
377#endif
General purpose class for a collection of sorted elements.
The elements in a collection are sorted according to their abscissa values and a given distance opera...
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Auxiliary class to define a range between two values.
Definition of zero value for any class.
Exception for division by zero.
Exception for an empty collection.
General exception.
Definition JException.hh:25
General purpose class for collection of elements, see: <a href="JTools.PDF";>Collection of elements....
Definition JSet.hh:22
void configure(const JAbstractCollection< abscissa_type > &bounds)
Configure collection.
JElement_t::ordinate_type ordinate_type
virtual int getSize() const override
Get number of elements.
container_type::iterator iterator
const ordinate_type & getY(int index) const
Get ordinate value.
container_type::const_iterator const_iterator
Multidimensional interpolation method.
Multidimensional map.
Definition JMultiMap.hh:52
Range of values.
Definition JRange.hh:42
T getLowerLimit() const
Get lower limit.
Definition JRange.hh:202
T getUpperLimit() const
Get upper limit.
Definition JRange.hh:213
T getZero()
Get zero value for a given data type.
Definition JZero.hh:26
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).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, std::false_type option)
Configuration of value.
void copy(const T &input, T &output)
Copy of input to output.
JContainer_t::ordinate_type makeCDF(const JContainer_t &input, JMappableCollection< JKey_t, JValue_t > &output, const typename JContainer_t::ordinate_type eps=JMATH::zero)
Conversion of data points to cumulative probability distribition (CDF).
JElement_t::ordinate_type integrate(const JCollection< JElement_t, JDistance_t > &input, typename JMappable< JElement_t >::map_type &output)
Conversion of data points to integral values.
JContainer_t::ordinate_type getIntegral(const JContainer_t &input)
Get integral of input data points.
int j
Definition JPolint.hh:801
void accumulate(T &value, std::false_type option)
Accumulation of value.
void reset(T &value)
Reset value.
Abstract interface for abscissa values of a collection of elements.
Auxiliary class to check whether given template is a collection, i.e. has a defined type collection_t...
Definition JAssembler.hh:24
2D Element.
Definition JPolint.hh:1131
Auxiliary class to define corresponding one-dimensional function interpolator function_type.
Map list.
Definition JMapList.hh:25
Template interface definition for associative collection of elements.
void put(typename JClass< key_type > ::argument_type key, typename JClass< mapped_type >::argument_type value)
Put pair-wise element (key,value) into collection.
virtual void clear()=0
Clear.
virtual const mapped_type & get(typename JClass< key_type >::argument_type key) const =0
Get mapped value.