Jpp  15.0.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAcoustics/JModel.hh
Go to the documentation of this file.
1 #ifndef __JACOUSTICS__JMODEL__
2 #define __JACOUSTICS__JMODEL__
3 
4 #include <istream>
5 #include <ostream>
6 #include <iomanip>
7 #include <limits>
8 #include <cmath>
9 
10 #include "JMath/JMath.hh"
11 #include "JMath/JZero.hh"
12 #include "JLang/JEquals.hh"
13 #include "JLang/JException.hh"
14 #include "JLang/JManip.hh"
15 #include "JTools/JHashMap.hh"
17 
18 #include "JAcoustics/JEKey.hh"
19 
20 
21 /**
22  * \file
23  *
24  * Model for fit to acoutsics data.
25  * \author mdejong
26  */
27 namespace JACOUSTICS {}
28 namespace JPP { using namespace JACOUSTICS; }
29 
30 namespace JACOUSTICS {
31 
32  using JMATH::JMath;
33  using JLANG::JEquals;
36  using JTOOLS::JHashMap;
37 
38 
39  /**
40  * Auxiliary namespace to encapsulate different model parameters.\n
41  */
42  namespace JMODEL {
43 
44  /**
45  * String parameters.
46  */
47  struct JString :
48  public JMath <JString>,
49  public JEquals<JString>
50  {
51  /**
52  * Default constructor.
53  */
54  JString() :
55  tx(0.0),
56  ty(0.0)
57  {}
58 
59 
60  /**
61  * Constructor.
62  *
63  * \param tx slope dx/dz
64  * \param ty slope dy/dz
65  */
66  JString(const double tx,
67  const double ty) :
68  tx(tx),
69  ty(ty)
70  {}
71 
72 
73  /**
74  * Get number of fit parameters.
75  *
76  * \return number of parameters
77  */
78  static inline size_t getN()
79  {
80  return 2;
81  }
82 
83 
84  /**
85  * Negate string.
86  *
87  * \return this string
88  */
90  {
91  tx = -tx;
92  ty = -ty;
93 
94  return *this;
95  }
96 
97 
98  /**
99  * Add string.
100  *
101  * \param string string
102  * \return this string
103  */
104  JString& add(const JString& string)
105  {
106  tx += string.tx;
107  ty += string.ty;
108 
109  return *this;
110  }
111 
112 
113  /**
114  * Subtract string.
115  *
116  * \param string string
117  * \return this string
118  */
119  JString& sub(const JString& string)
120  {
121  tx -= string.tx;
122  ty -= string.ty;
123 
124  return *this;
125  }
126 
127 
128  /**
129  * Scale string.
130  *
131  * \param factor multiplication factor
132  * \return this string
133  */
134  JString& mul(const double factor)
135  {
136  tx *= factor;
137  ty *= factor;
138 
139  return *this;
140  }
141 
142 
143  /**
144  * Scale string.
145  *
146  * \param factor division factor
147  * \return this string
148  */
149  JString& div(const double factor)
150  {
151  tx /= factor;
152  ty /= factor;
153 
154  return *this;
155  }
156 
157 
158  /**
159  * Check equality.
160  *
161  * \param string string
162  * \param precision precision
163  * \return true if strings are equal; else false
164  */
165  bool equals(const JString& string,
166  const double precision = std::numeric_limits<double>::min()) const
167  {
168  return (fabs(tx - string.tx) <= precision &&
169  fabs(ty - string.ty) <= precision);
170  }
171 
172 
173  /**
174  * Get length squared.
175  *
176  * \return square of length
177  */
178  double getLengthSquared() const
179  {
180  return tx*tx + ty*ty;
181  }
182 
183 
184  /**
185  * Get length.
186  *
187  * \return length
188  */
189  double getLength() const
190  {
191  return sqrt(getLengthSquared());
192  }
193 
194 
195  /**
196  * Get angle.
197  *
198  * \return angle [rad]
199  */
200  double getAngle() const
201  {
202  return atan2(ty, tx);
203  }
204 
205 
206  /**
207  * Get dot product.
208  *
209  * \param string string
210  * \return dot product
211  */
212  double getDot(const JString& string) const
213  {
214  return (tx * string.tx +
215  ty * string.ty);
216  }
217 
218 
219  /**
220  * Read string parameters from input stream.
221  *
222  * \param in input stream
223  * \param string string
224  * \return input stream
225  */
226  friend inline std::istream& operator>>(std::istream& in, JString& string)
227  {
228  return in >> string.tx >> string.ty;
229  }
230 
231 
232  /**
233  * Write string parameters to output stream.
234  *
235  * \param out output stream
236  * \param string string
237  * \return output stream
238  */
239  friend inline std::ostream& operator<<(std::ostream& out, const JString& string)
240  {
241  using namespace std;
242  using namespace JPP;
243 
244  return out << FIXED(10,7) << string.tx << ' '
245  << FIXED(10,7) << string.ty;
246  }
247 
248 
249  double tx;
250  double ty;
251  };
252 
253 
254  /**
255  * Emitter parameters.
256  */
257  struct JEmitter :
258  public JMath <JEmitter>,
259  public JEquals<JEmitter>
260  {
261  /**
262  * Default constructor.
263  */
265  t1(0.0)
266  {}
267 
268 
269  /**
270  * Constructor.
271  *
272  * \param t1 time-of-emission [s]
273  */
274  JEmitter(const double t1) :
275  t1(t1)
276  {}
277 
278 
279  /**
280  * Get number of fit parameters.
281  *
282  * \return number of parameters
283  */
284  static inline size_t getN()
285  {
286  return 1;
287  }
288 
289 
290  /**
291  * Negate emitter.
292  *
293  * \return this emitter
294  */
296  {
297  t1 = -t1;
298 
299  return *this;
300  }
301 
302 
303  /**
304  * Add emitter.
305  *
306  * \param emitter emitter
307  * \return this emitter
308  */
309  JEmitter& add(const JEmitter& emitter)
310  {
311  t1 += emitter.t1;
312 
313  return *this;
314  }
315 
316 
317  /**
318  * Subtract emitter.
319  *
320  * \param emitter emitter
321  * \return this emitter
322  */
323  JEmitter& sub(const JEmitter& emitter)
324  {
325  t1 -= emitter.t1;
326 
327  return *this;
328  }
329 
330 
331  /**
332  * Scale emitter.
333  *
334  * \param factor multiplication factor
335  * \return this emitter
336  */
337  JEmitter& mul(const double factor)
338  {
339  t1 *= factor;
340 
341  return *this;
342  }
343 
344 
345  /**
346  * Scale emitter.
347  *
348  * \param factor division factor
349  * \return this emitter
350  */
351  JEmitter& div(const double factor)
352  {
353  t1 /= factor;
354 
355  return *this;
356  }
357 
358 
359  /**
360  * Check equality.
361  *
362  * \param emitter emitter
363  * \param precision precision
364  * \return true if emitters are equal; else false
365  */
366  bool equals(const JEmitter& emitter,
367  const double precision = std::numeric_limits<double>::min()) const
368  {
369  return (fabs(t1 - emitter.t1) <= precision);
370  }
371 
372 
373  /**
374  * Write emitter parameters to output stream.
375  *
376  * \param out output stream
377  * \param emitter emitter
378  * \return output stream
379  */
380  friend inline std::ostream& operator<<(std::ostream& out, const JEmitter& emitter)
381  {
382  using namespace std;
383  using namespace JPP;
384 
385  return out << FIXED(20,6) << emitter.t1;
386  }
387 
388 
389  double t1;
390  };
391  }
392 
393 
394  /**
395  * Model for fit to acoustics data.
396  *
397  * The model consists of string parameters and emitter parameters.\n
398  * These parameters relate to the string identifer and emitter key, respectively.
399  */
400  struct JModel :
401  public JMath <JModel>,
402  public JEquals<JModel>,
404  {
408 
411 
412 
413  /**
414  * Type definition of fit parameter.
415  */
416  typedef size_t parameter_type;
417 
418 
419  /**
420  * Default constructor.
421  */
423  {}
424 
425 
426  /**
427  * Constructor.
428  *
429  * This constructor can be used to set up a default model (i.e. all values at zero) for the given set of hits.\n
430  * The data type corresponding to the hits should provide for the following policy methods.
431  * <pre>
432  * int getString(); // get string identifier
433  * JEkey getEKey(); // get emitter key
434  * </pre>
435  *
436  * \param __begin begin of hits
437  * \param __end end of hits
438  */
439  template<class T>
440  JModel(T __begin, T __end)
441  {
442  for (T hit = __begin; hit != __end; ++hit) {
443 
444  if (!this->string.has(hit->getString())) {
445  this->string[hit->getString()] = JString();
446  }
447 
448  if (!this->emitter.has(hit->getEKey())) {
449  this->emitter[hit->getEKey()] = JEmitter();
450  }
451  }
452  }
453 
454 
455  /**
456  * Reset parameters.
457  *
458  * \param zero zero
459  * \return this model
460  */
462  {
463  this->reset();
464 
465  return *this;
466  }
467 
468 
469  /**
470  * Clear parameters.
471  */
472  void clear()
473  {
474  string .clear();
475  emitter.clear();
476  }
477 
478 
479  /**
480  * Reset parameters.
481  */
482  void reset()
483  {
484  reset(string);
485  reset(emitter);
486  }
487 
488 
489  /**
490  * Negate model.
491  *
492  * \return this model
493  */
495  {
496  evaluate(this->string, &JString ::negate);
497  evaluate(this->emitter, &JEmitter::negate);
498 
499  return *this;
500  }
501 
502 
503  /**
504  * Add model.
505  *
506  * \param model model
507  * \return this model
508  */
509  JModel& add(const JModel& model)
510  {
511  evaluate(this->string, model.string, &JString ::add);
512  evaluate(this->emitter, model.emitter, &JEmitter::add);
513 
514  return *this;
515  }
516 
517 
518  /**
519  * Subtract model.
520  *
521  * \param model model
522  * \return this model
523  */
524  JModel& sub(const JModel& model)
525  {
526  evaluate(this->string, model.string, &JString ::sub);
527  evaluate(this->emitter, model.emitter, &JEmitter::sub);
528 
529  return *this;
530  }
531 
532 
533  /**
534  * Scale model.
535  *
536  * \param factor multiplication factor
537  * \return this model
538  */
539  JModel& mul(const double factor)
540  {
541  evaluate(this->string, &JString ::mul, factor);
542  evaluate(this->emitter, &JEmitter::mul, factor);
543 
544  return *this;
545  }
546 
547 
548  /**
549  * Scale model.
550  *
551  * \param factor division factor
552  * \return this model
553  */
554  JModel& div(const double factor)
555  {
556  evaluate(this->string, &JString ::div, factor);
557  evaluate(this->emitter, &JEmitter::div, factor);
558 
559  return *this;
560  }
561 
562 
563  /**
564  * Check equality.
565  *
566  * \param model model
567  * \param precision precision
568  * \return true if models are equal; else false
569  */
570  bool equals(const JModel& model,
571  const double precision = std::numeric_limits<double>::min()) const
572  {
573  return (equals(this->string, model.string, precision) &&
574  equals(this->emitter, model.emitter, precision));
575  }
576 
577 
578  /**
579  * Write model parameters to output stream.
580  *
581  * \param out output stream
582  * \param model model
583  * \return output stream
584  */
585  friend inline std::ostream& operator<<(std::ostream& out, const JModel& model)
586  {
587  using namespace std;
588  using namespace JPP;
589 
590  for (JHashMap<int, JString>::const_iterator i = model.string.begin(); i != model.string.end(); ++i) {
591  out << "string: " << setw(4) << i->first << ' ' << i->second << endl;
592  }
593 
594  for (JHashMap<JEKey, JEmitter>::const_iterator i = model.emitter.begin(); i != model.emitter.end(); ++i) {
595  out << "emitter: " << setw(2) << i->first << ' ' << i->second << endl;
596  }
597 
598  return out;
599  }
600 
601 
602  /**
603  * Get number of fit parameters.
604  *
605  * \return number of parameters
606  */
607  size_t getN() const
608  {
609  return emitter.getN() + string.getN();
610  }
611 
612 
613  /**
614  * Get index of fit parameter for given string.
615  *
616  * \param id string identifier
617  * \param p pointer to data member
618  * \return parameter
619  */
620  size_t getIndex(int id, double JString::*p) const
621  {
622  if (string.has(id))
623  return emitter.getN() + string.getIndex(id) * JString::getN() + getIndex(p);
624  else
625  THROW(JValueOutOfRange, "Invalid identifier " << id);
626  }
627 
628 
629  /**
630  * Get index of fit parameter for given emitter.
631  *
632  * \param id emitter key
633  * \param p pointer to data member
634  * \return parameter
635  */
636  size_t getIndex(const JEKey& id, double JEmitter::*p) const
637  {
638  if (emitter.has(id))
639  return emitter.getIndex(id) * JEmitter::getN() + getIndex(p);
640  else
641  THROW(JValueOutOfRange, "Invalid identifier " << id);
642  }
643 
644 
645  /**
646  * Read access to fit parameter value by index.
647  *
648  * \param index index
649  * \return value
650  */
651  inline double operator[](const size_t index) const
652  {
653  size_t i = index;
654 
655  if (i < emitter.getN()) { return getParameter(emitter, i); }
656 
657  i -= emitter.getN();
658 
659  if (i < string .getN()) { return getParameter(string, i); }
660 
661  THROW(JIndexOutOfRange, "Invalid index " << index << " >= " << getN());
662  }
663 
664 
665  /**
666  * Read/write access to fit parameter value by index.
667  *
668  * \param index index
669  * \return value
670  */
671  inline double& operator[](const size_t index)
672  {
673  size_t i = index;
674 
675  if (i < emitter.getN()) { return getParameter(emitter, i); }
676 
677  i -= emitter.getN();
678 
679  if (i < string .getN()) { return getParameter(string, i); }
680 
681  THROW(JIndexOutOfRange, "Invalid index " << index << " >= " << getN());
682  }
683 
684 
685  /**
686  * Map emitter key to model parameters of emitter.
687  */
688  struct emitter_type :
689  public JHashMap<JEKey, JEmitter>
690  {
691  private:
692  /**
693  * Auxiliary class for multiple associative map operators.
694  */
695  struct JHashMapHelper {
696  /**
697  * Constructor.
698  *
699  * \param map map
700  * \param id emitter identifier
701  */
703  map(map),
704  id (id)
705  {}
706 
707 
708  /**
709  * Get value corresponding to event counter (i.e.\ second part of JEKey).
710  *
711  * \param counter event counter
712  * \return emitter
713  */
714  JEmitter& operator[](const int counter)
715  {
716  return map[JEKey(id, counter)];
717  }
718 
719  private:
721  const int id;
722  };
723 
724  public:
725 
727 
728  /**
729  * Get number of fit parameters.
730  *
731  * \return number of parameters
732  */
733  size_t getN() const
734  {
735  return this->size() * JEmitter::getN();
736  }
737 
738 
739  /**
740  * Get helper corresponding to emitter identifier (i.e.\ first part of JEKey).
741  *
742  * \param id emitter identifier
743  * \return helper
744  */
746  {
747  return JHashMapHelper(*this, id);
748  }
749 
750  } emitter;
751 
752 
753  /**
754  * Map string identifier to model parameters of string.
755  */
756  struct string_type :
757  public JHashMap<int, JString>
758  {
759  /**
760  * Get number of fit parameters.
761  *
762  * \return number of parameters
763  */
764  size_t getN() const
765  {
766  return this->size() * JString::getN();
767  }
768  } string;
769 
770 
771  private:
772  /**
773  * Get index of fit parameter in given data structure.
774  *
775  * \param p pointer to data member
776  * \return index
777  */
778  template<class T>
779  static inline size_t getIndex(double T::*p)
780  {
781  T* __p__ = NULL;
782 
783  return ((double*) &(__p__->*p) - (double*) __p__);
784  }
785 
786 
787  /**
788  * Get read access to fit parameter value at given index in buffer.
789  *
790  * \param buffer buffer
791  * \param index index
792  * \return value
793  */
794  template<class JKey_t, class JValue_t, class JEvaluator_t>
795  static double getParameter(const JHashMap<JKey_t, JValue_t, JEvaluator_t>& buffer, const size_t index)
796  {
797  const size_t pos = index / JValue_t::getN(); // position of element in buffer
798  const size_t offset = index % JValue_t::getN(); // offset of parameter in element
799 
800  const JValue_t& value = buffer.data()[pos].second; // value of element at given position
801 
802  return ((const double*) &value)[offset]; // parameter at given offset
803  }
804 
805 
806  /**
807  * Get read/write access to fit parameter value at given index in buffer.
808  *
809  * \param buffer buffer
810  * \param index index
811  * \return value
812  */
813  template<class JKey_t, class JValue_t, class JEvaluator_t>
814  static double& getParameter(JHashMap<JKey_t, JValue_t, JEvaluator_t>& buffer, const size_t index)
815  {
816  const size_t pos = index / JValue_t::getN(); // position of element in buffer
817  const size_t offset = index % JValue_t::getN(); // offset of parameter in element
818 
819  JValue_t& value = buffer.data()[pos].second; // value of element at given position
820 
821  return ((double*) &value)[offset]; // parameter at given offset
822  }
823  };
824 }
825 
826 #endif
size_t getN() const
Get number of fit parameters.
JModel & negate()
Negate model.
Exceptions.
double getN(const JRange< T > &range, const double R)
Get expected number of occurrences due to given rate within specified interval.
Definition: JRange.hh:714
int getParameter(const std::string &text)
Get parameter number from text string.
JString & mul(const double factor)
Scale string.
JEmitter & sub(const JEmitter &emitter)
Subtract emitter.
Auxiliary base class for aritmetic operations of derived class types.
Definition: JMath.hh:110
void clear()
Clear parameters.
double getAngle() const
Get angle.
General purpose class for hash map of unique keys.
Definition: JHashMap.hh:72
JModel & mul(const double factor)
Scale model.
JEmitter(const double t1)
Constructor.
bool equals(const JEmitter &emitter, const double precision=std::numeric_limits< double >::min()) const
Check equality.
General purpose class for hash map of unique elements.
bool equals(const JModel &model, const double precision=std::numeric_limits< double >::min()) const
Check equality.
JModel(T __begin, T __end)
Constructor.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
JEmitter & mul(const double factor)
Scale emitter.
JEmitter & div(const double factor)
Scale emitter.
static const JZero zero
Function object to assign zero value.
Definition: JZero.hh:105
JMODEL::JEmitter JEmitter
friend std::istream & operator>>(std::istream &in, JString &string)
Read string parameters from input stream.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
friend std::ostream & operator<<(std::ostream &out, const JString &string)
Write string parameters to output stream.
Definition of zero value for any class.
JHashMapHelper operator[](int id)
Get helper corresponding to emitter identifier (i.e. first part of JEKey).
double operator[](const size_t index) const
Read access to fit parameter value by index.
Auxiliary class for multiple associative map operators.
static void reset(JHashMap< JKey_t, JValue_t, JEvaluator_t > &buffer)
Reset buffer.
static size_t getIndex(double T::*p)
Get index of fit parameter in given data structure.
friend std::ostream & operator<<(std::ostream &out, const JEmitter &emitter)
Write emitter parameters to output stream.
static size_t getN()
Get number of fit parameters.
friend std::ostream & operator<<(std::ostream &out, const JModel &model)
Write model parameters to output stream.
Model for fit to acoustics data.
size_t parameter_type
Type definition of fit parameter.
bool equals(const JString &string, const double precision=std::numeric_limits< double >::min()) const
Check equality.
static double & getParameter(JHashMap< JKey_t, JValue_t, JEvaluator_t > &buffer, const size_t index)
Get read/write access to fit parameter value at given index in buffer.
JString & sub(const JString &string)
Subtract string.
Auxiliary class to assign zero value.
Definition: JZero.hh:81
static double getParameter(const JHashMap< JKey_t, JValue_t, JEvaluator_t > &buffer, const size_t index)
Get read access to fit parameter value at given index in buffer.
JEmitter & add(const JEmitter &emitter)
Add emitter.
do set_variable OUTPUT_DIRECTORY $WORKDIR T
JString & add(const JString &string)
Add string.
JMODEL::JString JString
Template definition of auxiliary base class for comparison of data structures.
Definition: JEquals.hh:24
JString & negate()
Negate string.
static size_t getN()
Get number of fit parameters.
Arithmetic toolkit for hash maps.
double getLengthSquared() const
Get length squared.
size_t getIndex(const JEKey &id, double JEmitter::*p) const
Get index of fit parameter for given emitter.
int getIndex()
Get index for user I/O manipulation.
Definition: JManip.hh:26
JEmitter()
Default constructor.
static void set(JHashMap< JKey_t, JValue_t, JEvaluator_t > &target, const JHashMap< JKey_t, JValue_t, JEvaluator_t > &source, const JValue_t &value)
Set values in target corresponding to keys in source.
I/O manipulators.
size_t getIndex(int id, double JString::*p) const
Get index of fit parameter for given string.
JModel & sub(const JModel &model)
Subtract model.
Emitter hash key.
JACOUSTICS::JModel::string_type string
JString & div(const double factor)
Scale string.
void reset(T &value)
Reset value.
double & operator[](const size_t index)
Read/write access to fit parameter value by index.
JACOUSTICS::JModel::emitter_type emitter
JModel & div(const double factor)
Scale model.
Map emitter key to model parameters of emitter.
size_t getN() const
Get number of fit parameters.
JModel()
Default constructor.
JString(const double tx, const double ty)
Constructor.
JModel & add(const JModel &model)
Add model.
static bool equals(const JHashMap< JKey_t, JValue_t, JEvaluator_t > &first, const JHashMap< JKey_t, JValue_t, JEvaluator_t > &second)
Check equality of buffers.
Emitter key.
Definition: JEKey.hh:32
size_t getN() const
Get number of fit parameters.
Toolkit for JHashMap.
Base class for data structures with artithmetic capabilities.
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
Exception for accessing an index in a collection that is outside of its range.
Definition: JException.hh:90
JEmitter & negate()
Negate emitter.
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 source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:42
void reset()
Reset parameters.
JString()
Default constructor.
double getDot(const JString &string) const
Get dot product.
JEmitter & operator[](const int counter)
Get value corresponding to event counter (i.e. second part of JEKey).
double getLength() const
Get length.
JHashMapHelper(JHashMap< JEKey, JEmitter > &map, int id)
Constructor.
Map string identifier to model parameters of string.
JModel & operator=(const JMATH::JZero &zero)
Reset parameters.