Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JToolsToolkit.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JTOOLSTOOLKIT__
2 #define __JTOOLS__JTOOLSTOOLKIT__
3 
4 #include "JLang/JException.hh"
5 #include "JLang/JBool.hh"
6 #include "JMath/JZero.hh"
7 #include "JTools/JAssembler.hh"
8 #include "JTools/JElement.hh"
9 #include "JTools/JCollection.hh"
11 #include "JTools/JMultiFunction.hh"
13 #include "JTools/JFunctionalMap.hh"
14 
15 
16 /**
17  * \file
18  *
19  * This include file contains various recursive methods to operate on multi-dimensional collections.
20  * \author mdejong
21  */
22 namespace JTOOLS {}
23 namespace JPP { using namespace JTOOLS; }
24 
25 /**
26  * Auxiliary classes and methods for multi-dimensional interpolations and histograms.
27  */
28 namespace JTOOLS {
29 
30  using JLANG::JException;
33  using JLANG::JBool;
34 
35 
36  /**
37  * Conversion of data points to cumulative probability distribition (CDF).
38  *
39  * Note that the data type of the input container should be preserved
40  * so that the corresponding method <tt>integrate()</tt> is used.
41  *
42  * \param input collection
43  * \param output mappable collection
44  * \param eps minimal step size
45  * \return integral value
46  */
47  template<class JContainer_t,
48  class JKey_t,
49  class JValue_t>
50  inline typename JContainer_t::ordinate_type
51  makeCDF(const JContainer_t& input,
53  const typename JContainer_t::ordinate_type eps = JMATH::zero)
54  {
55  typedef typename JContainer_t::ordinate_type ordinate_type;
56  typedef typename JContainer_t::abscissa_type abscissa_type;
57  typedef JElement2D<abscissa_type, ordinate_type> element_type;
58  typedef JCollection<element_type> buffer_type;
59 
60 
61  if (input.getSize() > 1) {
62 
63  buffer_type buffer;
64 
65  const ordinate_type V = integrate(input, buffer);
66 
67  if (V == ordinate_type(JMATH::zero)) {
68  throw JDivisionByZero("Method makeCDF(): integral equals zero.");
69  }
70 
71  output.clear();
72 
73  typename buffer_type::const_iterator i = buffer.begin();
74 
75  for ( ; i != buffer.end() && i->getY() <= 0.5 * eps * V; ++i) {}
76 
77  if (i != buffer.end()) {
78 
79  // force first point at (0, ymin)
80 
81  JKey_t x = 0.0;
82  JValue_t y = i->getX();
83 
84  output.put(x, y);
85 
86  JKey_t xmax = x;
87  JValue_t ymax = y;
88 
89  for (++i; i != buffer.end(); ++i) {
90 
91  x = i->getY() / V;
92  y = i->getX();
93 
94  if (x > xmax) {
95 
96  ymax = y;
97 
98  if (x > xmax + eps) {
99 
100  output.put(x, y);
101 
102  xmax = x;
103  }
104  }
105  }
106 
107  // force last point at (1, ymax)
108 
109  x = 1.0;
110  y = ymax;
111 
112  output.put(x,y);
113 
114  } else {
115  throw JDivisionByZero("Method makeCDF() no remaining elements.");
116  }
117 
118  return V;
119 
120  } else {
121  throw JEmptyCollection("Method makeCDF(): no sufficient data.");
122  }
123  }
124 
125 
126  /**
127  * Get integral of input data points.
128  *
129  * \param input input data
130  * \return integral value
131  */
132  template<class JContainer_t>
133  inline typename JContainer_t::ordinate_type getIntegral(const JContainer_t& input)
134  {
136 
137  return integrate(input, garbage);
138  }
139 
140 
141  /**
142  * Auxiliary method to get integral of input data points.
143  *
144  * \param input input data
145  * \return integral value
146  */
147  template<class JFunction_t,
148  template<class, class, class> class JMap_t,
149  class JTail_t>
150  inline typename JFunction_t::ordinate_type
151  getIntegral(const JMultiMap<typename JFunction_t::abscissa_type, JFunction_t, JMapList<JMap_t, JTail_t>, typename JFunction_t::distance_type>& input)
152  {
153  typedef typename JFunction_t::abscissa_type abscissa_type;
154  typedef typename JFunction_t::ordinate_type ordinate_type;
155  typedef typename JFunction_t::distance_type distance_type;
156  typedef typename JFunctionalMap<JMap_t>::template function_type<abscissa_type,
157  ordinate_type,
158  ordinate_type,
159  distance_type> buffer_type;
160  typedef JMultiMap<abscissa_type, JFunction_t, JMapList<JMap_t, JTail_t>, distance_type> multimap_type;
161 
162  static buffer_type buffer;
163 
164  buffer.configure(input);
165 
166  typename buffer_type::iterator out = buffer.begin();
167 
168  for (typename multimap_type::const_iterator in = input.begin(); in != input.end(); ++in, ++out) {
169  out->getY() = getIntegral(in->getY());
170  }
171 
172  buffer.compile();
173 
174  return getIntegral(buffer);
175  }
176 
177 
178  /**
179  * Get integral of input data points.
180  *
181  * \param input input data
182  * \return integral value
183  */
184  template<class JFunction_t,
185  template<class, class, class> class JMap_t,
186  class JTail_t>
187  inline typename JFunction_t::ordinate_type
188  getIntegral(const JMultiFunction<JFunction_t, JMapList<JMap_t, JTail_t>, typename JFunction_t::distance_type>& input)
189  {
190  typedef typename JFunction_t::abscissa_type abscissa_type;
191  typedef typename JFunction_t::ordinate_type ordinate_type;
192  typedef typename JFunction_t::distance_type distance_type;
193  typedef typename JFunctionalMap<JMap_t>::template function_type<abscissa_type,
194  ordinate_type,
195  ordinate_type,
196  distance_type> buffer_type;
197  typedef JMultiFunction<JFunction_t, JMapList<JMap_t, JTail_t>, distance_type> multifunction_type;
198 
199  buffer_type buffer;
200 
201  for (typename multifunction_type::const_iterator i = input.begin(); i != input.end(); ++i) {
202  buffer.put(i->getX(), getIntegral(i->getY()));
203  }
204 
205  buffer.compile();
206 
207  return getIntegral(buffer);
208  }
209 
210 
211  /**
212  * Reset value.
213  *
214  * \param value value
215  */
216  template<class T>
217  inline void reset(T& value)
218  {
219  value = JMATH::getZero<T>();
220  }
221 
222 
223  /**
224  * Recursive reset of collection.
225  *
226  * \param collection collection
227  */
228  template<class JElement_t, class JDistance_t>
230  {
231  typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
232 
233  for (iterator i = collection.begin(); i != collection.end(); ++i) {
234  reset(i->getY());
235  }
236  }
237 
238 
239  /**
240  * Copy of input to output.
241  *
242  * \param input input
243  * \param output output
244  */
245  template<class T>
246  inline void copy(const T& input, T& output)
247  {
248  output = input;
249  }
250 
251 
252  /**
253  * Recursive copy of input collection to output collection.
254  *
255  * \param input input
256  * \param output output
257  */
258  template<class JElement_t, class JDistance_t, class JKey_t, class JValue_t>
260  {
261  typedef typename JCollection<JElement_t, JDistance_t>::const_iterator const_iterator;
262 
263  output.clear();
264 
265  for (const_iterator i = input.begin(); i != input.end(); ++i) {
266  copy(i->getY(), output.get(i->getX()));
267  }
268  }
269 
270 
271  /**
272  * Configuration of value.
273  *
274  * This method does nothing.
275  *
276  * \param value value
277  * \param bounds bounds
278  * \param option false
279  */
280  template<class T, class JAbscissa_t>
281  inline void configure(const T& value, const JAbstractCollection<JAbscissa_t>& bounds, JBool<false> option)
282  {}
283 
284 
285  /**
286  * Recursive configuration of collection.
287  *
288  * \param collection collection
289  * \param bounds bounds
290  * \param option true
291  */
292  template<class JElement_t, class JDistance_t>
295  JBool<true> option = JBool<true>())
296  {
297  typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
298  typedef typename JCollection<JElement_t, JDistance_t>::ordinate_type ordinate_type;
299 
300  collection.configure(bounds);
301 
302  for (iterator i = collection.begin(); i != collection.end(); ++i) {
304  }
305  }
306 
307 
308  /**
309  * Accumulation of value.
310  *
311  * This method does nothing.
312  *
313  * \param value value
314  * \param option false
315  */
316  template<class T>
317  inline void accumulate(T& value, JBool<false> option)
318  {}
319 
320 
321  /**
322  * Recursive accumulation of collection.
323  *
324  * \param collection collection
325  * \param option true
326  */
327  template<class JElement_t, class JDistance_t>
329  {
330  typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
331  typedef typename JCollection<JElement_t, JDistance_t>::ordinate_type ordinate_type;
332 
333  for (iterator i = collection.begin(); i != collection.end(); ++i) {
335  }
336 
337  if (collection.getSize() > 1) {
338 
339  for (iterator j = collection.begin(), i = j++; j != collection.end(); ++i, ++j) {
340  j->getY() += i->getY();
341  }
342 
343  reset(collection.begin()->getY());
344  }
345  }
346 }
347 
348 #endif
void compile()
Compilation.
General exception.
Definition: JException.hh:40
Exceptions.
2D Element.
Definition: JElement.hh:44
General purpose class for collection of elements, see: &lt;a href=&quot;JTools.PDF&quot;;&gt;Collection of elements...
Definition: JCollection.hh:71
The elements in a collection are sorted according to their abscissa values and a given distance opera...
Abstract interface for abscissa values of a collection of elements.
virtual mapped_type & get(typename JClass< key_type >::argument_type key)=0
Get mapped value.
virtual int getSize() const
Get number of elements.
Definition: JCollection.hh:177
Template interface definition for associative collection of elements.
void reset(JCLBInput &data, size_t size)
Reset CLB buffers.
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:94
Auxiliary class to define corresponding one-dimensional function interpolator function_type.
Definition of zero value for any class.
Auxiliary template class for type bool.
Definition: JBool.hh:20
Multidimensional interpolation method.
Map list.
Definition: JMapList.hh:24
container_type::iterator iterator
Definition: JCollection.hh:91
void configure(const JAbstractCollection< abscissa_type > &bounds)
Configure collection.
Definition: JCollection.hh:314
General purpose class for a collection of sorted 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.
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).
const ordinate_type & getY(int index) const
Get ordinate value.
Definition: JCollection.hh:224
void configure(const T &value, const JAbstractCollection< JAbscissa_t > &bounds, JBool< false > option)
Configuration of value.
JElement_t::ordinate_type ordinate_type
Definition: JCollection.hh:81
Exception for division by zero.
Definition: JException.hh:252
virtual void clear()=0
Clear.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:40
Exception for an empty collection.
Definition: JException.hh:126
container_type::const_iterator const_iterator
Definition: JCollection.hh:89
JContainer_t::ordinate_type getIntegral(const JContainer_t &input)
Get integral of input data points.
Multidimensional map.
Definition: JMultiMap.hh:46
void accumulate(T &value, JBool< false > option)
Accumulation of value.
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.
Definition: JCollection.hh:793