Jpp  18.3.0-rc.1
the software that should make you happy
 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;
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 data.");
116  }
117 
118  return V;
119 
120  } else {
121  THROW(JEmptyCollection, "Method makeCDF(): not sufficient input 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  * The value is set to (the equivalent of) zero, see method JMATH::getZero.
215  *
216  * \param value value
217  */
218  template<class T>
219  inline void reset(T& value)
220  {
221  value = JMATH::getZero<T>();
222  }
223 
224 
225  /**
226  * Recursive reset of collection.
227  *
228  * \param collection collection
229  */
230  template<class JElement_t, class JDistance_t>
232  {
233  typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
234 
235  for (iterator i = collection.begin(); i != collection.end(); ++i) {
236  reset(i->getY());
237  }
238  }
239 
240 
241  /**
242  * Copy of input to output.
243  *
244  * The output value is set to the input value.
245  *
246  * \param input input
247  * \param output output
248  */
249  template<class T>
250  inline void copy(const T& input, T& output)
251  {
252  output = input;
253  }
254 
255 
256  /**
257  * Recursive copy of input collection to output collection.
258  *
259  * \param input input
260  * \param output output
261  */
262  template<class JElement_t, class JDistance_t, class JKey_t, class JValue_t>
264  {
265  typedef typename JCollection<JElement_t, JDistance_t>::const_iterator const_iterator;
266 
267  output.clear();
268 
269  for (const_iterator i = input.begin(); i != input.end(); ++i) {
270  copy(i->getY(), output.get(i->getX()));
271  }
272  }
273 
274 
275  /**
276  * Configuration of value.
277  *
278  * This is a fall-back method; it does nothing.
279  *
280  * \param value value
281  * \param bounds bounds
282  * \param option false
283  */
284  template<class T, class JAbscissa_t>
285  inline void configure(const T& value, const JAbstractCollection<JAbscissa_t>& bounds, JBool<false> option)
286  {}
287 
288 
289  /**
290  * Recursive configuration of collection.
291  *
292  * \param collection collection
293  * \param bounds bounds
294  * \param option true
295  */
296  template<class JElement_t, class JDistance_t>
299  JBool<true> option = JBool<true>())
300  {
301  typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
302  typedef typename JCollection<JElement_t, JDistance_t>::ordinate_type ordinate_type;
303 
304  collection.configure(bounds);
305 
306  for (iterator i = collection.begin(); i != collection.end(); ++i) {
308  }
309  }
310 
311 
312  /**
313  * Accumulation of value.
314  *
315  * This is a fall-back method; it does nothing.
316  *
317  * \param value value
318  * \param option false
319  */
320  template<class T>
321  inline void accumulate(T& value, JBool<false> option)
322  {}
323 
324 
325  /**
326  * Recursive accumulation of collection.
327  *
328  * \param collection collection
329  * \param option true
330  */
331  template<class JElement_t, class JDistance_t>
333  {
334  typedef typename JCollection<JElement_t, JDistance_t>::iterator iterator;
335  typedef typename JCollection<JElement_t, JDistance_t>::ordinate_type ordinate_type;
336 
337  for (iterator i = collection.begin(); i != collection.end(); ++i) {
339  }
340 
341  if (collection.getSize() > 1) {
342 
343  for (iterator j = collection.begin(), i = j++; j != collection.end(); ++i, ++j) {
344  j->getY() += i->getY();
345  }
346 
347  reset(collection.begin()->getY());
348  }
349  }
350 }
351 
352 #endif
const double xmax
Definition: JQuadrature.cc:24
void compile()
Compilation.
General exception.
Definition: JException.hh:24
Exceptions.
General purpose class for collection of elements, see: &lt;a href=&quot;JTools.PDF&quot;;&gt;Collection of elements...
Definition: JCollection.hh:73
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 const mapped_type & get(typename JClass< key_type >::argument_type key) const =0
Get mapped value.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Template interface definition for associative collection of elements.
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
Auxiliary class to define corresponding one-dimensional function interpolator function_type.
V(JDAQEvent-JTriggerReprocessor)*1.0/(JDAQEvent+1.0e-10)
Definition of zero value for any class.
std::vector< JHitW0 > buffer_type
hits
Definition: JPerth.cc:67
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:93
do set_variable OUTPUT_DIRECTORY $WORKDIR T
void configure(const JAbstractCollection< abscissa_type > &bounds)
Configure collection.
Definition: JCollection.hh:332
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:244
void reset(T &value)
Reset value.
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:83
Exception for division by zero.
Definition: JException.hh:286
virtual void clear()=0
Clear.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:162
Exception for an empty collection.
Definition: JException.hh:160
2D Element.
Definition: JElement.hh:46
container_type::const_iterator const_iterator
Definition: JCollection.hh:91
int j
Definition: JPolint.hh:792
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:48
JContainer_t::ordinate_type getIntegral(const JContainer_t &input)
Get integral of input data points.
Multidimensional map.
Definition: JMultiMap.hh:52
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:812
virtual int getSize() const override
Get number of elements.
Definition: JCollection.hh:197