Particle Identification and Tracking
tuple.h
Go to the documentation of this file.
1 //Copyright 2010 Thomas A Caswell
2 //tcaswell@uchicago.edu
3 //http://jfi.uchicago.edu/~tcaswell
4 //
5 //This program is free software; you can redistribute it and/or modify
6 //it under the terms of the GNU General Public License as published by
7 //the Free Software Foundation; either version 3 of the License, or (at
8 //your option) any later version.
9 //
10 //This program is distributed in the hope that it will be useful, but
11 //WITHOUT ANY WARRANTY; without even the implied warranty of
12 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 //General Public License for more details.
14 //
15 //You should have received a copy of the GNU General Public License
16 //along with this program; if not, see <http://www.gnu.org/licenses>.
17 //
18 //Additional permission under GNU GPL version 3 section 7
19 //
20 //If you modify this Program, or any covered work, by linking or
21 //combining it with MATLAB (or a modified version of that library),
22 //containing parts covered by the terms of MATLAB User License, the
23 //licensors of this Program grant you additional permission to convey
24 //the resulting work.
25 #ifndef TUPLE_T
26 #define TUPLE_T
27 
28 #include <iostream>
29 #include <sstream>
30 #include <stdexcept>
31 #include <cmath>
32 namespace utilities{
41 template<class T,int length>
42 class Tuple{
43 public:
47  bool operator==(const Tuple& b)const;
48 
52  void operator= (const Tuple& y);
56  Tuple operator- ( );
57 
61  T& operator[] (int i);
65  const T& operator[] (int i) const;
66 
70  Tuple operator+ (const Tuple& y) const;
74  Tuple operator+ (const T y)const;
78  void operator+= (const Tuple& y);
82  void operator+= (const T y);
83 
84 
85 
89  Tuple operator- ( const Tuple& y)const;
93  Tuple operator- ( const T y)const;
97  void operator-= (const Tuple& y);
101  void operator-= (const T y);
102 
103 
104 
108  Tuple operator* (const T y) const;
112  Tuple operator* (const Tuple& y) const;
116  void operator*= (const Tuple& y);
120  void operator*= (const T y);
121 
122 
123 
127  Tuple operator/ ( const Tuple& y) const;
131  void operator/= (const Tuple& y);
135  void operator/= (const T y);
136 
137 
138 
142  T magnitude_sqr () const;
143 
147  T magnitude () const;
148 
152  T dot(const Tuple & y) const;
153 
157  T prod()const;
158 
162  T sum()const;
163 
167  T max()const;
171  T min()const;
172 
173 
177  void make_unit();
178 
182  Tuple direction() const;
183 
187  void print() const;
188 
193  void clear();
194 
198  Tuple();
199 
203  Tuple(const Tuple & y);
204 
208  T dist_sqr(const Tuple& y)const;
209 
210 
214  ~Tuple();
218  Tuple(T x);
219 
223  Tuple(T x,T y);
227  Tuple(T x,T y,T z);
228 
232  Tuple(const T * );
233 
234 
239  const T * get_ptr() const;
240 
245  T * get_ptr() ;
246 
250  const static int length_ = length;
251 
255  template<class T2>
257 
258 
262  int get_len()const
263  {
264  return length;
265  }
266 
267 protected:
271  T data_[length];
272 
273 private:
274 };
275 
276 
281 template<typename _CharT, class _Traits,class T,int length>
282 std::basic_ostream<_CharT, _Traits>&
283 operator<<(std::basic_ostream<_CharT, _Traits>& __os, const Tuple<T,length>& in)
284 {
285 
286 
287  std::basic_ostringstream<_CharT, _Traits> s;
288  s.flags(__os.flags());
289  s.imbue(__os.getloc());
290  s.precision(__os.precision());
291 
292 
293  s << '(' ;
294  s<< in[0] ;
295  for(int j = 1;j<in.get_len();++j)
296  s<< ','<<in[j] ;
297  s << ')';
298 
299  return __os << s.str();
300 }
301 
302 
303 template<class T,int length>
305 {
306  for(int j = 0;j<length; ++j)
307  if(data_[j] != b.data_[j])
308  return false;
309  return true;
310 }
311 
312 template<class T,int length>
314  for(int j = 0;j<length; ++j)
315  data_[j] = y.data_[j];
316 }
317 template<class T,int length>
319  if(i<0 || i>= length)
320  throw std::runtime_error("Tuple range error");
321  return data_[i];
322 }
323 template<class T,int length>
324 const T& Tuple<T,length>::operator[] (int i) const{
325  if(i<0 || i>= length)
326  throw std::runtime_error("Tuple range error");
327  return data_[i];
328 }
329 template<class T,int length>
331  Tuple<T,length> tmp;
332  for(int j = 0;j<length; ++j)
333  tmp.data_[j] = -data_[j];
334  return tmp;
335 }
336 template<class T,int length>
337 
339  Tuple<T,length> tmp;
340  for(int j = 0;j<length; ++j)
341  tmp.data_[j] = y.data_[j] + data_[j];
342  return tmp;
343 }
344 template<class T,int length>
346  Tuple<T,length> tmp;
347  for(int j = 0;j<length; ++j)
348  tmp.data_[j] = y + data_[j];
349  return tmp;
350 }
351 template<class T,int length>
353  for(int j = 0;j<length; ++j)
354  data_[j] += y.data_[j] ;
355 
356 }
357 template<class T,int length>
359  for(int j = 0;j<length; ++j)
360  data_[j] += y ;
361 
362 }
363 
364 template<class T,int length>
366  Tuple<T,length> tmp;
367  for(int j = 0;j<length; ++j)
368  tmp.data_[j] = -y.data_[j] + data_[j];
369  return tmp;
370 }
371 template<class T,int length>
373  Tuple<T,length> tmp;
374  for(int j = 0;j<length; ++j)
375  tmp.data_[j] = -y + data_[j];
376  return tmp;
377 }
378 template<class T,int length>
380  for(int j = 0;j<length; ++j)
381  data_[j] -= y.data_[j] ;
382 
383 }
384 template<class T,int length>
386  for(int j = 0;j<length; ++j)
387  data_[j] -= y ;
388 }
389 
390 
391 template<class T,int length>
393  Tuple<T,length> tmp;
394  for(int j = 0;j<length; ++j){
395  tmp.data_[j] = data_[j]*y.data_[j] ;
396  }
397  return tmp;
398 }
399 template<class T,int length>
401  Tuple<T,length> tmp;
402  for(int j = 0;j<length; ++j){
403  tmp.data_[j] = data_[j] * y ;
404  }
405  return tmp;
406 }
407 template<class T,int length>
409  for(int j = 0;j<length; ++j)
410  data_[j] *= y.data_[j] ;
411 }
412 template<class T,int length>
414  for(int j = 0;j<length; ++j)
415  data_[j] *= y ;
416 }
417 
418 
419 
420 template<class T,int length>
422  for(int j = 0;j<length; ++j)
423  data_[j] /= y.data_[j] ;
424 
425 }
426 template<class T,int length>
428  for(int j = 0;j<length; ++j)
429  data_[j] /= y ;
430 }
431 template<class T,int length>
433  Tuple<T,length> tmp;
434  for(int j = 0;j<length; ++j)
435  tmp.data_[j] = data_[j]/y.data_[j] ;
436  return tmp;
437 }
438 
439 
440 
441 
442 template<class T,int length>
444  using std::cout;
445  using std::endl;
446  for(int j = 0;j<length; ++j)
447  cout<<data_[j] <<"\t";
448  cout<<endl;
449 }
450 
451 template<class T,int length>
453  T tmp = 0;
454  for(int j = 0;j<length; ++j)
455  tmp += data_[j]*data_[j] ;
456  return tmp;
457 }
458 template<class T,int length>
460 {
461  return sqrt(magnitude_sqr());
462 }
463 template<class T,int length>
465 {
466  T tmp = 0;
467  for(int j = 0;j<length; ++j)
468  tmp += data_[j]*y.data_[j] ;
469  return tmp;
470 }
471 
472 template<class T,int length>
474 {
475  T tmp = 1;
476  for(int j = 0;j<length; ++j)
477  tmp *= data_[j];
478  return tmp;
479 
480 }
481 template<class T,int length>
483 {
484  T tmp = 0;
485  for(int j = 0;j<length; ++j)
486  tmp += data_[j];
487  return tmp;
488 }
489 template<class T,int length>
491 {
492  T tmp = data_[0];
493  for(int j = 1;j<length; ++j)
494  if( data_[j] > tmp)
495  tmp = data_[j];
496  return tmp;
497 }
498 template<class T,int length>
500 {
501  T tmp = data_[0];
502  for(int j = 1;j<length; ++j)
503  if( data_[j] < tmp)
504  tmp = data_[j];
505  return tmp;
506 }
507 
508 template<class T,int length>
510  T tmp = magnitude();
511  for(int j = 0;j<length; ++j)
512  data_[j] /= tmp ;
513 }
514 template<class T,int length>
516  Tuple<T,length> tmp = (*this);
517  tmp.make_unit();
518  return tmp;
519 
520 }
521 
522 template<class T,int length>
524  for(int j = 0;j<length; ++j)
525  data_[j] =0 ;
526 }
527 
528 template<class T,int length>
530  // data_= (T*) new T[length];
531  for(int j = 0;j<length; ++j)
532  data_[j] = 0;
533  // std::cout<<"make"<< std::endl;
534 
535 }
536 
537 // tac 2009-07-17
538 // added
539 template<class T,int length>
541  for(int j = 0;j<length; ++j)
542  data_[j] = y.data_[j];
543 }
544 
545 template<class T,int length>
546 template<class T2>
548  for(int j = 0;j<length; ++j)
549  data_[j] = (T) y[j];
550 }
551 
552 
553 // tac 2009-10-07
554 // added because the constructors and destructors of Tuple<T,length>s are taking ~5% of run time
555 template<class T,int length>
557 {
558  T out =0;
559 
560  for(int j = 0;j<length;++j)
561  {
562  T tmp =(data_[j]-y.data_[j]);
563  out += tmp*tmp;
564  }
565  return out;
566 }
567 
568 template<class T,int length>
570  // std::cout<<"died"<< std::endl;
571  // delete[] data_;
572  // data_ = NULL;
573 }
574 
575 template<class T,int length>
577 {
578  for(int j = 0; j<length;++j)
579  data_[j] = x;
580 }
581 
582 
583 template<class T,int length>
585 {
586  if(length != 2)
587  throw std::logic_error("Tuple, using wrong number of arguments for this instantiation");
588  data_[0] = x;
589  data_[1] = y;
590 }
591 
592 
593 template<class T,int length>
595 {
596  if(length != 3)
597  throw std::logic_error("Tuple, using wrong number of arguments for this instantiation");
598 
599  data_[0] = x;
600  data_[1] = y;
601  data_[2] = z;
602 }
603 
604 template<class T,int length>
606 {
607  for( int j = 0;j<length;++j)
608  data_[j] = in[j];
609 }
610 
611 template<class T,int length>
612 const T * Tuple<T,length>::get_ptr() const
613 {
614  return data_;
615 }
616 
617 template<class T,int length>
619 {
620  return data_;
621 }
622 
623 
624 
625 }
626 
627 #endif