Logo ROOT   6.16/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
44namespace ROOT {
45
46namespace Math {
47
48
49//==============================================================================
50// Constructors
51//==============================================================================
52template <class T, unsigned int D>
54 for(unsigned int i=0; i<D; ++i)
55 fArray[i] = 0;
56}
57
58template <class T, unsigned int D>
59template <class A>
61 operator=(rhs);
62}
63
64template <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
77template <class T, unsigned int D>
78template <class InputIterator>
79SVector<T,D>::SVector(InputIterator begin, InputIterator end) {
80 assert(begin + D == end);
81 std::copy(begin, end, fArray);
82}
83
84template <class T, unsigned int D>
85template <class InputIterator>
86SVector<T,D>::SVector(InputIterator begin, unsigned int size) {
87 assert( size <= D);
88 std::copy(begin, begin+size, fArray);
89}
90
91#else
92
93template <class T, unsigned int D>
94#ifdef NDEBUG
95SVector<T,D>::SVector(const T* a, unsigned int) {
96#else
97SVector<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
104template <class T, unsigned int D>
106 assert(ibegin + D == iend);
107 std::copy(ibegin, iend, fArray);
108}
109
110
111#endif
112
113
114template <class T, unsigned int D>
116 STATIC_CHECK( D == 1,SVector_dimension_not_right);
117 fArray[0] = a1;
118}
119
120template <class T, unsigned int D>
121SVector<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
126template <class T, unsigned int D>
127SVector<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
132template <class T, unsigned int D>
133SVector<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
138template <class T, unsigned int D>
139SVector<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
146template <class T, unsigned int D>
147SVector<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
154template <class T, unsigned int D>
155SVector<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
162template <class T, unsigned int D>
163SVector<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
170template <class T, unsigned int D>
171SVector<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
180template <class T, unsigned int D>
181SVector<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//==============================================================================
193template <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
201template <class T, unsigned int D>
202template <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//==============================================================================
225template <class T, unsigned int D>
226bool 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
234template <class T, unsigned int D>
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
243template <class T, unsigned int D>
244template <class A>
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//==============================================================================
256template <class T, unsigned int D>
257inline bool SVector<T,D>::operator!=(const T& rhs) const {
258 return !operator==(rhs);
259}
260
261template <class T, unsigned int D>
262inline bool SVector<T,D>::operator!=(const SVector<T,D>& rhs) const {
263 return !operator==(rhs);
264}
265
266template <class T, unsigned int D>
267template <class A>
268inline bool SVector<T,D>::operator!=(const VecExpr<A,T,D>& rhs) const {
269 return !operator==(rhs);
270}
271
272//==============================================================================
273// operator>
274//==============================================================================
275template <class T, unsigned int D>
276bool 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
284template <class T, unsigned int D>
285bool 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
293template <class T, unsigned int D>
294template <class A>
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//==============================================================================
306template <class T, unsigned int D>
307bool 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
315template <class T, unsigned int D>
316bool 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
324template <class T, unsigned int D>
325template <class A>
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
338template <class T, unsigned int D>
339template<class A>
341 return operator=(*this + rhs);
342}
343
344template <class T, unsigned int D>
345template<class A>
346SVector<T,D>& SVector<T,D>::operator-=(const A& rhs) {
347 // self subtraction
348 return operator=(*this - rhs);
349}
350
351template <class T, unsigned int D>
352template<class A>
353SVector<T,D>& SVector<T,D>::operator*=(const A& rhs) {
354 // self multiplication
355 return operator=(*this * rhs);
356}
357
358template <class T, unsigned int D>
359template<class A>
360SVector<T,D>& SVector<T,D>::operator/=(const A& rhs) {
361 // self division
362 return operator=(*this / rhs);
363}
364#endif
365
366template <class T, unsigned int D>
368 for(unsigned int i=0; i<D; ++i) {
369 fArray[i] += rhs;
370 }
371 return *this;
372}
373
374template <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
383template <class T, unsigned int D>
384template <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//==============================================================================
395template <class T, unsigned int D>
397 for(unsigned int i=0; i<D; ++i) {
398 fArray[i] -= rhs;
399 }
400 return *this;
401}
402
403template <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
411template <class T, unsigned int D>
412template <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//==============================================================================
423template <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
431template <class T, unsigned int D>
432template <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//==============================================================================
443template <class T, unsigned int D>
444SVector<T,D>& SVector<T,D>::operator/=(const SVector<T,D>& rhs) {
445 for(unsigned int i=0; i<D; ++i) {
446 fArray[i] /= rhs.apply(i);
447 }
448 return *this;
449}
450
451template <class T, unsigned int D>
452template <class A>
453SVector<T,D>& SVector<T,D>::operator/=(const VecExpr<A,T,D>& rhs) {
454 for(unsigned int i=0; i<D; ++i) {
455 fArray[i] /= rhs.apply(i);
456 }
457 return *this;
458}
459#endif
460template <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//==============================================================================
472template <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//==============================================================================
484template <class T, unsigned int D>
485template <unsigned int D2>
486SVector<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//==============================================================================
501template <class T, unsigned int D>
502template <class A, unsigned int D2>
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//==============================================================================
516template <class T, unsigned int D>
517std::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//==============================================================================
532template <class T, unsigned int D>
533inline T SVector<T,D>::apply(unsigned int i) const { return fArray[i]; }
534
535template <class T, unsigned int D>
536inline const T* SVector<T,D>::Array() const { return fArray; }
537
538template <class T, unsigned int D>
539inline T* SVector<T,D>::Array() { return fArray; }
540
541
542//==============================================================================
543// STL interface
544//==============================================================================
545template <class T, unsigned int D>
546inline T* SVector<T,D>::begin() { return fArray; }
547
548template <class T, unsigned int D>
549inline const T* SVector<T,D>::begin() const { return fArray; }
550
551template <class T, unsigned int D>
552inline T* SVector<T,D>::end() { return fArray + Dim(); }
553
554template <class T, unsigned int D>
555inline const T* SVector<T,D>::end() const { return fArray + Dim(); }
556
557template <class T, unsigned int D>
558template <class InputIterator>
559void 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
565template <class T, unsigned int D>
566template <class InputIterator>
567void 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//==============================================================================
577template <class T, unsigned int D>
578inline const T& SVector<T,D>::operator[](unsigned int i) const { return fArray[i]; }
579
580template <class T, unsigned int D>
581inline const T& SVector<T,D>::operator()(unsigned int i) const { return fArray[i]; }
582
583template <class T, unsigned int D>
584inline T& SVector<T,D>::operator[](unsigned int i) { return fArray[i]; }
585
586template <class T, unsigned int D>
587inline T& SVector<T,D>::operator()(unsigned int i) { return fArray[i]; }
588//==============================================================================
589// Element access with At()
590//==============================================================================
591template <class T, unsigned int D>
592inline const T& SVector<T,D>::At(unsigned int i) const {
593 assert(i < D);
594 return fArray[i];
595}
596
597template <class T, unsigned int D>
598inline T& SVector<T,D>::At(unsigned int i) {
599 assert(i < D);
600 return fArray[i];
601}
602
603//==============================================================================
604// SubVector
605//==============================================================================
606template <class T, unsigned int D>
607template <class SubVector>
608SubVector 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
623template <class T, unsigned int D>
624bool SVector<T,D>::IsInUse( const T * p) const {
625 return p == fArray;
626}
627
628
629//==============================================================================
630// operator<<
631//==============================================================================
632template <class T, unsigned int D>
633inline 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
#define STATIC_CHECK(expr, msg)
Definition: StaticCheck.h:56
Bool_t operator==(const TDatime &d1, const TDatime &d2)
Definition: TDatime.h:102
Binding & operator=(OUT(*fun)(void))
@ kSize
Definition: TStructNode.h:26
SVector: a generic fixed size Vector class.
Definition: SVector.h:75
const T * const_iterator
STL const_iterator interface.
Definition: SVector.h:85
SVector< T, D > & operator=(const T &a1)
assignment from a scalar (only for size 1 vector)
Definition: SVector.icc:194
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
iterator begin()
STL iterator interface.
Definition: SVector.icc:546
SVector< T, D > & operator*=(const T &rhs)
self multiplication with a scalar
Definition: SVector.icc:424
SVector< T, D > & Unit()
transform vector into a vector of length 1
Definition: SVector.icc:473
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
const T * Array() const
return read-only pointer to internal array
Definition: SVector.icc:536
T apply(unsigned int i) const
access the parse tree. Index starts from zero
Definition: SVector.icc:533
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
bool operator!=(const T &rhs) const
element wise comparison
Definition: SVector.icc:257
SVector()
Default constructor: vector filled with zero values.
Definition: SVector.icc:53
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 subtraction with a scalar
Definition: SVector.icc:396
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
bool operator<(const T &rhs) const
element wise comparison
Definition: SVector.icc:307
const T & operator[](unsigned int i) const
read-only access of vector elements. Index starts from 0.
Definition: SVector.icc:578
SVector< T, D > & operator+=(const T &rhs)
self addition with a scalar
Definition: SVector.icc:367
bool operator==(const T &rhs) const
element wise comparison
Definition: SVector.icc:226
SVector< T, D > & operator/=(const T &rhs)
self division with a scalar
Definition: SVector.icc:461
T fArray[D]
SVector data.
Definition: SVector.h:333
std::ostream & Print(std::ostream &os) const
used by operator<<()
Definition: SVector.icc:517
iterator end()
STL iterator interface.
Definition: SVector.icc:552
void SetElements(InputIterator begin, InputIterator end)
set vector elements copying the values iterator size must match vector size
Definition: SVector.icc:559
Expression wrapper class for Vector objects.
Definition: Expression.h:64
T apply(unsigned int i) const
Definition: Expression.h:77
bool IsInUse(const T *p) const
function to determine if any use operand is being used (has same memory adress)
Definition: Expression.h:104
T Mag(const SVector< T, D > &rhs)
Vector magnitude (Euclidian norm) Compute : .
Definition: Functions.h:252
Namespace for new Math classes and functions.
static double A[]
double T(double x)
Definition: ChebyshevPol.h:34
std::ostream & operator<<(std::ostream &os, const AxisAngle &a)
Stream Output and Input.
Definition: AxisAngle.cxx:91
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21
auto * a
Definition: textangle.C:12