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