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