ROOT  6.06/09
Reference Guide
SVector.icc
Go to the documentation of this file.
1 // @(#)root/smatrix:$Id$
2 // Authors: T. Glebe, L. Moneta 2005
3 
4 #ifndef ROOT_Math_SVector_icc
5 #define ROOT_Math_SVector_icc
6 // ********************************************************************
7 //
8 // source:
9 //
10 // type: source code
11 //
12 // created: 21. Mar 2001
13 //
14 // author: Thorsten Glebe
15 // HERA-B Collaboration
16 // Max-Planck-Institut fuer Kernphysik
17 // Saupfercheckweg 1
18 // 69117 Heidelberg
19 // Germany
20 // E-mail: T.Glebe@mpi-hd.mpg.de
21 //
22 // Description: A fixed size Vector class
23 //
24 // changes:
25 // 21 Mar 2001 (TG) creation
26 // 26 Mar 2001 (TG) added place_at()
27 // 06 Apr 2001 (TG) CTORS added
28 // 07 Apr 2001 (TG) CTORS added
29 // 22 Aug 2001 (TG) CTOR(T*,len) added
30 // 14 Jan 2002 (TG) added operator==(), operator!=(), operator>(), operator<()
31 //
32 // ********************************************************************
33 #include <iostream>
34 #include <assert.h>
35 #include <algorithm>
36 
37 #ifndef ROOT_Math_StaticCheck
38 #include "Math/StaticCheck.h"
39 #endif
40 
41 namespace ROOT {
42 
43 namespace Math {
44 
45 
46 //==============================================================================
47 // Constructors
48 //==============================================================================
49 template <class T, unsigned int D>
51  for(unsigned int i=0; i<D; ++i)
52  fArray[i] = 0;
53 }
54 
55 template <class T, unsigned int D>
56 template <class A>
58  operator=(rhs);
59 }
60 
61 template <class T, unsigned int D>
63  for(unsigned int i=0; i<D; ++i)
64  fArray[i] = rhs.fArray[i];
65 }
66 
67 
68 
69 //==============================================================================
70 // New Constructors from STL interfaces
71 //==============================================================================
72 
73 #ifdef LATER
74 template <class T, unsigned int D>
75 template <class InputIterator>
76 SVector<T,D>::SVector(InputIterator begin, InputIterator end) {
77  assert(begin + D == end);
78  std::copy(begin, end, fArray);
79 }
80 
81 template <class T, unsigned int D>
82 template <class InputIterator>
83 SVector<T,D>::SVector(InputIterator begin, unsigned int size) {
84  assert( size <= D);
85  std::copy(begin, begin+size, fArray);
86 }
87 
88 #else
89 
90 template <class T, unsigned int D>
91 #ifdef NDEBUG
92 SVector<T,D>::SVector(const T* a, unsigned int) {
93 #else
94 SVector<T,D>::SVector(const T* a, unsigned int len) {
95 #endif
96  assert(len == D);
97  for(unsigned int i=0; i<D; ++i)
98  fArray[i] = a[i];
99 }
100 
101 template <class T, unsigned int D>
103  assert(ibegin + D == iend);
104  std::copy(ibegin, iend, fArray);
105 }
106 
107 
108 #endif
109 
110 
111 template <class T, unsigned int D>
113  STATIC_CHECK( D == 1,SVector_dimension_not_right);
114  fArray[0] = a1;
115 }
116 
117 template <class T, unsigned int D>
118 SVector<T,D>::SVector(const T& a1, const T& a2) {
119  STATIC_CHECK( D == 2,SVector_dimension_not_right);
120  fArray[0] = a1; fArray[1] = a2;
121 }
122 
123 template <class T, unsigned int D>
124 SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3) {
125  STATIC_CHECK( D == 3,SVector_dimension_not_right);
126  fArray[0] = a1; fArray[1] = a2; fArray[2] = a3;
127 }
128 
129 template <class T, unsigned int D>
130 SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4) {
131  STATIC_CHECK( D == 4,SVector_dimension_not_right);
132  fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4;
133 }
134 
135 template <class T, unsigned int D>
136 SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4,
137  const T& a5) {
138  STATIC_CHECK( D == 5,SVector_dimension_not_right);
139  fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4;
140  fArray[4] = a5;
141 }
142 
143 template <class T, unsigned int D>
144 SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4,
145  const T& a5, const T& a6) {
146  STATIC_CHECK( D == 6,SVector_dimension_not_right);
147  fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4;
148  fArray[4] = a5; fArray[5] = a6;
149 }
150 
151 template <class T, unsigned int D>
152 SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4,
153  const T& a5, const T& a6, const T& a7) {
154  STATIC_CHECK( D == 7,SVector_dimension_not_right);
155  fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4;
156  fArray[4] = a5; fArray[5] = a6; fArray[6] = a7;
157 }
158 
159 template <class T, unsigned int D>
160 SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4,
161  const T& a5, const T& a6, const T& a7, const T& a8) {
162  STATIC_CHECK( D == 8,SVector_dimension_not_right);
163  fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4;
164  fArray[4] = a5; fArray[5] = a6; fArray[6] = a7; fArray[7] = a8;
165 }
166 
167 template <class T, unsigned int D>
168 SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4,
169  const T& a5, const T& a6, const T& a7, const T& a8,
170  const T& a9) {
171  STATIC_CHECK( D == 9,SVector_dimension_not_right);
172  fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4;
173  fArray[4] = a5; fArray[5] = a6; fArray[6] = a7; fArray[7] = a8;
174  fArray[8] = a9;
175 }
176 
177 template <class T, unsigned int D>
178 SVector<T,D>::SVector(const T& a1, const T& a2, const T& a3, const T& a4,
179  const T& a5, const T& a6, const T& a7, const T& a8,
180  const T& a9, const T& a10) {
181  STATIC_CHECK( D == 10,SVector_dimension_not_right);
182  fArray[0] = a1; fArray[1] = a2; fArray[2] = a3; fArray[3] = a4;
183  fArray[4] = a5; fArray[5] = a6; fArray[6] = a7; fArray[7] = a8;
184  fArray[8] = a9; fArray[9] = a10;
185 }
186 
187 //==============================================================================
188 // operator=
189 //==============================================================================
190 template <class T, unsigned int D>
192  // operator = for size 1 vectors
193  STATIC_CHECK( D == 1,SVector_dimension_not_right);
194  fArray[0] = a1;
195  return *this;
196 }
197 
198 template <class T, unsigned int D>
199 template <class A>
201  if (! rhs.IsInUse(fArray) ) {
202  for(unsigned int i=0; i<D; ++i) {
203  fArray[i] = rhs.apply(i);
204  }
205  }
206  else {
207  // otherwise we need to create a temporary object
208  T tmp[D];
209  for(unsigned int i=0; i<D; ++i) {
210  tmp[i] = rhs.apply(i);
211  }
212  for(unsigned int i=0; i<D; ++i) {
213  fArray[i] = tmp[i];
214  }
215  }
216  return *this;
217 }
218 
219 //==============================================================================
220 // operator==
221 //==============================================================================
222 template <class T, unsigned int D>
223 bool SVector<T,D>::operator==(const T& rhs) const {
224  bool rc = true;
225  for(unsigned int i=0; i<D; ++i) {
226  rc = rc && (fArray[i] == rhs);
227  }
228  return rc;
229 }
230 
231 template <class T, unsigned int D>
232 bool SVector<T,D>::operator==(const SVector<T,D>& rhs) const {
233  bool rc = true;
234  for(unsigned int i=0; i<D; ++i) {
235  rc = rc && (fArray[i] == rhs.apply(i));
236  }
237  return rc;
238 }
239 
240 template <class T, unsigned int D>
241 template <class A>
242 bool SVector<T,D>::operator==(const VecExpr<A,T,D>& rhs) const {
243  bool rc = true;
244  for(unsigned int i=0; i<D; ++i) {
245  rc = rc && (fArray[i] == rhs.apply(i));
246  }
247  return rc;
248 }
249 
250 //==============================================================================
251 // operator!=
252 //==============================================================================
253 template <class T, unsigned int D>
254 inline bool SVector<T,D>::operator!=(const T& rhs) const {
255  return !operator==(rhs);
256 }
257 
258 template <class T, unsigned int D>
259 inline bool SVector<T,D>::operator!=(const SVector<T,D>& rhs) const {
260  return !operator==(rhs);
261 }
262 
263 template <class T, unsigned int D>
264 template <class A>
265 inline bool SVector<T,D>::operator!=(const VecExpr<A,T,D>& rhs) const {
266  return !operator==(rhs);
267 }
268 
269 //==============================================================================
270 // operator>
271 //==============================================================================
272 template <class T, unsigned int D>
273 bool SVector<T,D>::operator>(const T& rhs) const {
274  bool rc = true;
275  for(unsigned int i=0; i<D; ++i) {
276  rc = rc && (fArray[i] > rhs);
277  }
278  return rc;
279 }
280 
281 template <class T, unsigned int D>
282 bool SVector<T,D>::operator>(const SVector<T,D>& rhs) const {
283  bool rc = true;
284  for(unsigned int i=0; i<D; ++i) {
285  rc = rc && (fArray[i] > rhs.apply(i));
286  }
287  return rc;
288 }
289 
290 template <class T, unsigned int D>
291 template <class A>
292 bool SVector<T,D>::operator>(const VecExpr<A,T,D>& rhs) const {
293  bool rc = true;
294  for(unsigned int i=0; i<D; ++i) {
295  rc = rc && (fArray[i] > rhs.apply(i));
296  }
297  return rc;
298 }
299 
300 //==============================================================================
301 // operator<
302 //==============================================================================
303 template <class T, unsigned int D>
304 bool SVector<T,D>::operator<(const T& rhs) const {
305  bool rc = true;
306  for(unsigned int i=0; i<D; ++i) {
307  rc = rc && (fArray[i] < rhs);
308  }
309  return rc;
310 }
311 
312 template <class T, unsigned int D>
313 bool SVector<T,D>::operator<(const SVector<T,D>& rhs) const {
314  bool rc = true;
315  for(unsigned int i=0; i<D; ++i) {
316  rc = rc && (fArray[i] < rhs.apply(i));
317  }
318  return rc;
319 }
320 
321 template <class T, unsigned int D>
322 template <class A>
323 bool SVector<T,D>::operator<(const VecExpr<A,T,D>& rhs) const {
324  bool rc = true;
325  for(unsigned int i=0; i<D; ++i) {
326  rc = rc && (fArray[i] < rhs.apply(i));
327  }
328  return rc;
329 }
330 
331 //==============================================================================
332 // operator+=
333 //==============================================================================
334 #ifdef NEW_IMPL
335 template <class T, unsigned int D>
336 template<class A>
338  return operator=(*this + rhs);
339 }
340 
341 template <class T, unsigned int D>
342 template<class A>
343 SVector<T,D>& SVector<T,D>::operator-=(const A& rhs) {
344  // self subtraction
345  return operator=(*this - rhs);
346 }
347 
348 template <class T, unsigned int D>
349 template<class A>
350 SVector<T,D>& SVector<T,D>::operator*=(const A& rhs) {
351  // self multiplication
352  return operator=(*this * rhs);
353 }
354 
355 template <class T, unsigned int D>
356 template<class A>
357 SVector<T,D>& SVector<T,D>::operator/=(const A& rhs) {
358  // self division
359  return operator=(*this / rhs);
360 }
361 #endif
362 
363 template <class T, unsigned int D>
365  for(unsigned int i=0; i<D; ++i) {
366  fArray[i] += rhs;
367  }
368  return *this;
369 }
370 
371 template <class T, unsigned int D>
373  for(unsigned int i=0; i<D; ++i) {
374  fArray[i] += rhs.apply(i);
375  }
376  return *this;
377 }
378 
379 
380 template <class T, unsigned int D>
381 template <class A>
383  for(unsigned int i=0; i<D; ++i) {
384  fArray[i] += rhs.apply(i);
385  }
386  return *this;
387 }
388 
389 //==============================================================================
390 // operator-=
391 //==============================================================================
392 template <class T, unsigned int D>
394  for(unsigned int i=0; i<D; ++i) {
395  fArray[i] -= rhs;
396  }
397  return *this;
398 }
399 
400 template <class T, unsigned int D>
402  for(unsigned int i=0; i<D; ++i) {
403  fArray[i] -= rhs.apply(i);
404  }
405  return *this;
406 }
407 
408 template <class T, unsigned int D>
409 template <class A>
411  for(unsigned int i=0; i<D; ++i) {
412  fArray[i] -= rhs.apply(i);
413  }
414  return *this;
415 }
416 
417 //==============================================================================
418 // operator*= (only scalar values)
419 //==============================================================================
420 template <class T, unsigned int D>
422  for(unsigned int i=0; i<D; ++i) {
423  fArray[i] *= rhs;
424  }
425  return *this;
426 }
427 #ifdef OLD_IMPL
428 template <class T, unsigned int D>
429 template <class A>
431  for(unsigned int i=0; i<D; ++i) {
432  fArray[i] *= rhs.apply(i);
433  }
434  return *this;
435 }
436 
437 //==============================================================================
438 // operator/=
439 //==============================================================================
440 template <class T, unsigned int D>
441 SVector<T,D>& SVector<T,D>::operator/=(const SVector<T,D>& rhs) {
442  for(unsigned int i=0; i<D; ++i) {
443  fArray[i] /= rhs.apply(i);
444  }
445  return *this;
446 }
447 
448 template <class T, unsigned int D>
449 template <class A>
450 SVector<T,D>& SVector<T,D>::operator/=(const VecExpr<A,T,D>& rhs) {
451  for(unsigned int i=0; i<D; ++i) {
452  fArray[i] /= rhs.apply(i);
453  }
454  return *this;
455 }
456 #endif
457 template <class T, unsigned int D>
459  for(unsigned int i=0; i<D; ++i) {
460  fArray[i] /= rhs;
461  }
462  return *this;
463 }
464 
465 
466 //==============================================================================
467 // unit
468 //==============================================================================
469 template <class T, unsigned int D>
471  const T len = Mag(*this);
472  for(unsigned int i=0; i<D; ++i) {
473  fArray[i] /= len;
474  }
475  return *this;
476 }
477 
478 //==============================================================================
479 // place_at
480 //==============================================================================
481 template <class T, unsigned int D>
482 template <unsigned int D2>
483 SVector<T,D>& SVector<T,D>::Place_at(const SVector<T,D2>& rhs, unsigned int row) {
484 
485  assert(row+D2 <= D);
486  // Sassert(end <= D);
487 
488  for(unsigned int i=row, j=0; j<D2; ++i,++j)
489  fArray[i] = rhs.apply(j);
490 
491  return *this;
492 }
493 
494 
495 //==============================================================================
496 // place_at
497 //==============================================================================
498 template <class T, unsigned int D>
499 template <class A, unsigned int D2>
500 SVector<T,D>& SVector<T,D>::Place_at(const VecExpr<A,T,D2>& rhs, unsigned int row) {
501 
502  assert(row+D2 <= D);
503 
504  for(unsigned int i=row, j=0; j<D2; ++i,++j)
505  fArray[i] = rhs.apply(j);
506 
507  return *this;
508 }
509 
510 //==============================================================================
511 // print
512 //==============================================================================
513 template <class T, unsigned int D>
514 std::ostream& SVector<T,D>::Print(std::ostream& os) const {
515  const std::ios_base::fmtflags prevFmt = os.setf(std::ios::right,std::ios::adjustfield);
516  // os.setf(ios::fixed);
517 
518  for (unsigned int i = 0; i < D; ++i ) {
519  os << fArray[i];
520  if (i != D-1) os << ", ";
521  }
522  if (prevFmt != os.flags() ) os.setf(prevFmt, std::ios::adjustfield);
523  return os;
524 }
525 
526 //==============================================================================
527 // Access functions
528 //==============================================================================
529 template <class T, unsigned int D>
530 inline T SVector<T,D>::apply(unsigned int i) const { return fArray[i]; }
531 
532 template <class T, unsigned int D>
533 inline const T* SVector<T,D>::Array() const { return fArray; }
534 
535 template <class T, unsigned int D>
536 inline T* SVector<T,D>::Array() { return fArray; }
537 
538 
539 //==============================================================================
540 // STL interface
541 //==============================================================================
542 template <class T, unsigned int D>
543 inline T* SVector<T,D>::begin() { return fArray; }
544 
545 template <class T, unsigned int D>
546 inline const T* SVector<T,D>::begin() const { return fArray; }
547 
548 template <class T, unsigned int D>
549 inline T* SVector<T,D>::end() { return fArray + Dim(); }
550 
551 template <class T, unsigned int D>
552 inline const T* SVector<T,D>::end() const { return fArray + Dim(); }
553 
554 template <class T, unsigned int D>
555 template <class InputIterator>
556 void SVector<T,D>::SetElements(InputIterator ibegin, InputIterator iend) {
557  // iterator size must match vector size
558  assert( ibegin + D == iend);
559  std::copy(ibegin, iend, fArray);
560 }
561 
562 template <class T, unsigned int D>
563 template <class InputIterator>
564 void SVector<T,D>::SetElements(InputIterator ibegin, unsigned int size) {
565  // size <= vector size
566  assert( size <= D);
567  std::copy(ibegin, ibegin+size, fArray);
568 }
569 
570 
571 //==============================================================================
572 // Operators
573 //==============================================================================
574 template <class T, unsigned int D>
575 inline const T& SVector<T,D>::operator[](unsigned int i) const { return fArray[i]; }
576 
577 template <class T, unsigned int D>
578 inline const T& SVector<T,D>::operator()(unsigned int i) const { return fArray[i]; }
579 
580 template <class T, unsigned int D>
581 inline T& SVector<T,D>::operator[](unsigned int i) { return fArray[i]; }
582 
583 template <class T, unsigned int D>
584 inline T& SVector<T,D>::operator()(unsigned int i) { return fArray[i]; }
585 //==============================================================================
586 // Element access with At()
587 //==============================================================================
588 template <class T, unsigned int D>
589 inline const T& SVector<T,D>::At(unsigned int i) const {
590  assert(i < D);
591  return fArray[i];
592 }
593 
594 template <class T, unsigned int D>
595 inline T& SVector<T,D>::At(unsigned int i) {
596  assert(i < D);
597  return fArray[i];
598 }
599 
600 //==============================================================================
601 // SubVector
602 //==============================================================================
603 template <class T, unsigned int D>
604 template <class SubVector>
605 SubVector SVector<T,D>::Sub(unsigned int row) const {
606 
607  STATIC_CHECK( SubVector::kSize <= D,SVector_dimension_too_small);
608 
609  assert(row + SubVector::kSize <= D);
610 
611  SubVector tmp;
612  // need to use std::copy ??
613  for(unsigned int i=0; i < SubVector::kSize; ++i) {
614  tmp[i] = fArray[i+row];
615  }
616  return tmp;
617 }
618 
619 // check if the given passed pointer is teh same contained in the vector
620 template <class T, unsigned int D>
621 bool SVector<T,D>::IsInUse( const T * p) const {
622  return p == fArray;
623 }
624 
625 
626 //==============================================================================
627 // operator<<
628 //==============================================================================
629 template <class T, unsigned int D>
630 inline std::ostream& operator<<(std::ostream& os, const SVector<T,D>& rhs) {
631  return rhs.Print(os);
632 }
633 
634 
635 
636 
637 } // namespace Math
638 
639 } // namespace ROOT
640 
641 
642 #endif
bool operator==(const TKey &lhs, const TKey &rhs)
Definition: TKey.h:43
const T * Array() const
return read-only pointer to internal array
Definition: SVector.icc:533
SubVector Sub(unsigned int row) const
return a subvector of size N starting at the value row where N is the size of the returned vector (Su...
Definition: SVector.icc:605
std::ostream & Print(std::ostream &os) const
used by operator<<()
Definition: SVector.icc:514
Namespace for new ROOT classes and functions.
Definition: ROOT.py:1
bool operator!=(const T &rhs) const
element wise comparison
Definition: SVector.icc:254
double T(double x)
Definition: ChebyshevPol.h:34
SVector< T, D > & operator-=(const T &rhs)
self subtraction with a scalar
Definition: SVector.icc:393
#define assert(cond)
Definition: unittest.h:542
T Mag(const SVector< T, D > &rhs)
Vector magnitude (Euclidian norm) Compute : .
Definition: Functions.h:254
T fArray[D]
SVector data.
Definition: SVector.h:337
static double A[]
#define STATIC_CHECK(expr, msg)
Definition: StaticCheck.h:56
const T & At(unsigned int i) const
read-only access of vector elements with check on index. Index starts from 0.
Definition: SVector.icc:589
T apply(unsigned int i) const
access the parse tree. Index starts from zero
Definition: SVector.icc:530
SVector< T, D > & operator/=(const T &rhs)
self division with a scalar
Definition: SVector.icc:458
bool operator<(const T &rhs) const
element wise comparison
Definition: SVector.icc:304
iterator begin()
STL iterator interface.
Definition: SVector.icc:543
SVector< T, D > & operator=(const T &a1)
assignment from a scalar (only for size 1 vector)
Definition: SVector.icc:191
T apply(unsigned int i) const
Definition: Expression.h:77
const T * const_iterator
STL const_iterator interface.
Definition: SVector.h:89
void SetElements(InputIterator begin, InputIterator end)
set vector elements copying the values iterator size must match vector size
Definition: SVector.icc:556
const T & operator[](unsigned int i) const
read-only access of vector elements. Index starts from 0.
Definition: SVector.icc:575
bool IsInUse(const T *p) const
Function to check if a vector is sharing same memory location of the passed pointer This function is ...
Definition: SVector.icc:621
bool IsInUse(const T *p) const
function to determine if any use operand is being used (has same memory adress)
Definition: Expression.h:104
SVector< T, D > & Place_at(const SVector< T, D2 > &rhs, unsigned int row)
place a sub-vector starting from the given position
Definition: SVector.icc:483
Namespace for new Math classes and functions.
bool operator==(const T &rhs) const
element wise comparison
Definition: SVector.icc:223
Binding & operator=(OUT(*fun)(void))
bool operator>(const T &rhs) const
element wise comparison
Definition: SVector.icc:273
const T & operator()(unsigned int i) const
read-only access of vector elements. Index starts from 0.
Definition: SVector.icc:578
Expression wrapper class for Vector objects.
Definition: Expression.h:64
SVector< T, D > & Unit()
transform vector into a vector of length 1
Definition: SVector.icc:470
iterator end()
STL iterator interface.
Definition: SVector.icc:549
SVector< T, D > & operator+=(const T &rhs)
self addition with a scalar
Definition: SVector.icc:364
SVector< T, D > & operator*=(const T &rhs)
self multiplication with a scalar
Definition: SVector.icc:421
SVector()
Default constructor: vector filled with zero values.
Definition: SVector.icc:50
SVector: a generic fixed size Vector class.