Logo ROOT   6.18/05
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>
203 for(unsigned int i=0; i<D; ++i)
204 fArray[i] = rhs.fArray[i];
205 return *this;
206}
207
208template <class T, unsigned int D>
209template <class A>
211 if (! rhs.IsInUse(fArray) ) {
212 for(unsigned int i=0; i<D; ++i) {
213 fArray[i] = rhs.apply(i);
214 }
215 }
216 else {
217 // otherwise we need to create a temporary object
218 T tmp[D];
219 for(unsigned int i=0; i<D; ++i) {
220 tmp[i] = rhs.apply(i);
221 }
222 for(unsigned int i=0; i<D; ++i) {
223 fArray[i] = tmp[i];
224 }
225 }
226 return *this;
227}
228
229//==============================================================================
230// operator==
231//==============================================================================
232template <class T, unsigned int D>
233bool SVector<T,D>::operator==(const T& rhs) const {
234 bool rc = true;
235 for(unsigned int i=0; i<D; ++i) {
236 rc = rc && (fArray[i] == rhs);
237 }
238 return rc;
239}
240
241template <class T, unsigned int D>
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
250template <class T, unsigned int D>
251template <class A>
253 bool rc = true;
254 for(unsigned int i=0; i<D; ++i) {
255 rc = rc && (fArray[i] == rhs.apply(i));
256 }
257 return rc;
258}
259
260//==============================================================================
261// operator!=
262//==============================================================================
263template <class T, unsigned int D>
264inline bool SVector<T,D>::operator!=(const T& rhs) const {
265 return !operator==(rhs);
266}
267
268template <class T, unsigned int D>
269inline bool SVector<T,D>::operator!=(const SVector<T,D>& rhs) const {
270 return !operator==(rhs);
271}
272
273template <class T, unsigned int D>
274template <class A>
275inline bool SVector<T,D>::operator!=(const VecExpr<A,T,D>& rhs) const {
276 return !operator==(rhs);
277}
278
279//==============================================================================
280// operator>
281//==============================================================================
282template <class T, unsigned int D>
283bool SVector<T,D>::operator>(const T& rhs) const {
284 bool rc = true;
285 for(unsigned int i=0; i<D; ++i) {
286 rc = rc && (fArray[i] > rhs);
287 }
288 return rc;
289}
290
291template <class T, unsigned int D>
292bool SVector<T,D>::operator>(const SVector<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
300template <class T, unsigned int D>
301template <class A>
303 bool rc = true;
304 for(unsigned int i=0; i<D; ++i) {
305 rc = rc && (fArray[i] > rhs.apply(i));
306 }
307 return rc;
308}
309
310//==============================================================================
311// operator<
312//==============================================================================
313template <class T, unsigned int D>
314bool SVector<T,D>::operator<(const T& rhs) const {
315 bool rc = true;
316 for(unsigned int i=0; i<D; ++i) {
317 rc = rc && (fArray[i] < rhs);
318 }
319 return rc;
320}
321
322template <class T, unsigned int D>
323bool SVector<T,D>::operator<(const SVector<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
331template <class T, unsigned int D>
332template <class A>
334 bool rc = true;
335 for(unsigned int i=0; i<D; ++i) {
336 rc = rc && (fArray[i] < rhs.apply(i));
337 }
338 return rc;
339}
340
341//==============================================================================
342// operator+=
343//==============================================================================
344#ifdef NEW_IMPL
345template <class T, unsigned int D>
346template<class A>
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 subtraction
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 multiplication
362 return operator=(*this * rhs);
363}
364
365template <class T, unsigned int D>
366template<class A>
367SVector<T,D>& SVector<T,D>::operator/=(const A& rhs) {
368 // self division
369 return operator=(*this / rhs);
370}
371#endif
372
373template <class T, unsigned int D>
375 for(unsigned int i=0; i<D; ++i) {
376 fArray[i] += rhs;
377 }
378 return *this;
379}
380
381template <class T, unsigned int D>
383 for(unsigned int i=0; i<D; ++i) {
384 fArray[i] += rhs.apply(i);
385 }
386 return *this;
387}
388
389
390template <class T, unsigned int D>
391template <class A>
393 for(unsigned int i=0; i<D; ++i) {
394 fArray[i] += rhs.apply(i);
395 }
396 return *this;
397}
398
399//==============================================================================
400// operator-=
401//==============================================================================
402template <class T, unsigned int D>
404 for(unsigned int i=0; i<D; ++i) {
405 fArray[i] -= rhs;
406 }
407 return *this;
408}
409
410template <class T, unsigned int D>
412 for(unsigned int i=0; i<D; ++i) {
413 fArray[i] -= rhs.apply(i);
414 }
415 return *this;
416}
417
418template <class T, unsigned int D>
419template <class A>
421 for(unsigned int i=0; i<D; ++i) {
422 fArray[i] -= rhs.apply(i);
423 }
424 return *this;
425}
426
427//==============================================================================
428// operator*= (only scalar values)
429//==============================================================================
430template <class T, unsigned int D>
432 for(unsigned int i=0; i<D; ++i) {
433 fArray[i] *= rhs;
434 }
435 return *this;
436}
437#ifdef OLD_IMPL
438template <class T, unsigned int D>
439template <class A>
441 for(unsigned int i=0; i<D; ++i) {
442 fArray[i] *= rhs.apply(i);
443 }
444 return *this;
445}
446
447//==============================================================================
448// operator/=
449//==============================================================================
450template <class T, unsigned int D>
451SVector<T,D>& SVector<T,D>::operator/=(const SVector<T,D>& rhs) {
452 for(unsigned int i=0; i<D; ++i) {
453 fArray[i] /= rhs.apply(i);
454 }
455 return *this;
456}
457
458template <class T, unsigned int D>
459template <class A>
460SVector<T,D>& SVector<T,D>::operator/=(const VecExpr<A,T,D>& rhs) {
461 for(unsigned int i=0; i<D; ++i) {
462 fArray[i] /= rhs.apply(i);
463 }
464 return *this;
465}
466#endif
467template <class T, unsigned int D>
469 for(unsigned int i=0; i<D; ++i) {
470 fArray[i] /= rhs;
471 }
472 return *this;
473}
474
475
476//==============================================================================
477// unit
478//==============================================================================
479template <class T, unsigned int D>
481 const T len = Mag(*this);
482 for(unsigned int i=0; i<D; ++i) {
483 fArray[i] /= len;
484 }
485 return *this;
486}
487
488//==============================================================================
489// place_at
490//==============================================================================
491template <class T, unsigned int D>
492template <unsigned int D2>
493SVector<T,D>& SVector<T,D>::Place_at(const SVector<T,D2>& rhs, unsigned int row) {
494
495 assert(row+D2 <= D);
496 // Sassert(end <= D);
497
498 for(unsigned int i=row, j=0; j<D2; ++i,++j)
499 fArray[i] = rhs.apply(j);
500
501 return *this;
502}
503
504
505//==============================================================================
506// place_at
507//==============================================================================
508template <class T, unsigned int D>
509template <class A, unsigned int D2>
511
512 assert(row+D2 <= D);
513
514 for(unsigned int i=row, j=0; j<D2; ++i,++j)
515 fArray[i] = rhs.apply(j);
516
517 return *this;
518}
519
520//==============================================================================
521// print
522//==============================================================================
523template <class T, unsigned int D>
524std::ostream& SVector<T,D>::Print(std::ostream& os) const {
525 const std::ios_base::fmtflags prevFmt = os.setf(std::ios::right,std::ios::adjustfield);
526 // os.setf(ios::fixed);
527
528 for (unsigned int i = 0; i < D; ++i ) {
529 os << fArray[i];
530 if (i != D-1) os << ", ";
531 }
532 if (prevFmt != os.flags() ) os.setf(prevFmt, std::ios::adjustfield);
533 return os;
534}
535
536//==============================================================================
537// Access functions
538//==============================================================================
539template <class T, unsigned int D>
540inline T SVector<T,D>::apply(unsigned int i) const { return fArray[i]; }
541
542template <class T, unsigned int D>
543inline const T* SVector<T,D>::Array() const { return fArray; }
544
545template <class T, unsigned int D>
546inline T* SVector<T,D>::Array() { return fArray; }
547
548
549//==============================================================================
550// STL interface
551//==============================================================================
552template <class T, unsigned int D>
553inline T* SVector<T,D>::begin() { return fArray; }
554
555template <class T, unsigned int D>
556inline const T* SVector<T,D>::begin() const { return fArray; }
557
558template <class T, unsigned int D>
559inline T* SVector<T,D>::end() { return fArray + Dim(); }
560
561template <class T, unsigned int D>
562inline const T* SVector<T,D>::end() const { return fArray + Dim(); }
563
564template <class T, unsigned int D>
565template <class InputIterator>
566void SVector<T,D>::SetElements(InputIterator ibegin, InputIterator iend) {
567 // iterator size must match vector size
568 assert( ibegin + D == iend);
569 std::copy(ibegin, iend, fArray);
570}
571
572template <class T, unsigned int D>
573template <class InputIterator>
574void SVector<T,D>::SetElements(InputIterator ibegin, unsigned int size) {
575 // size <= vector size
576 assert( size <= D);
577 std::copy(ibegin, ibegin+size, fArray);
578}
579
580
581//==============================================================================
582// Operators
583//==============================================================================
584template <class T, unsigned int D>
585inline const T& SVector<T,D>::operator[](unsigned int i) const { return fArray[i]; }
586
587template <class T, unsigned int D>
588inline const T& SVector<T,D>::operator()(unsigned int i) const { return fArray[i]; }
589
590template <class T, unsigned int D>
591inline T& SVector<T,D>::operator[](unsigned int i) { return fArray[i]; }
592
593template <class T, unsigned int D>
594inline T& SVector<T,D>::operator()(unsigned int i) { return fArray[i]; }
595//==============================================================================
596// Element access with At()
597//==============================================================================
598template <class T, unsigned int D>
599inline const T& SVector<T,D>::At(unsigned int i) const {
600 assert(i < D);
601 return fArray[i];
602}
603
604template <class T, unsigned int D>
605inline T& SVector<T,D>::At(unsigned int i) {
606 assert(i < D);
607 return fArray[i];
608}
609
610//==============================================================================
611// SubVector
612//==============================================================================
613template <class T, unsigned int D>
614template <class SubVector>
615SubVector SVector<T,D>::Sub(unsigned int row) const {
616
617 STATIC_CHECK( SubVector::kSize <= D,SVector_dimension_too_small);
618
619 assert(row + SubVector::kSize <= D);
620
621 SubVector tmp;
622 // need to use std::copy ??
623 for(unsigned int i=0; i < SubVector::kSize; ++i) {
624 tmp[i] = fArray[i+row];
625 }
626 return tmp;
627}
628
629// check if the given passed pointer is teh same contained in the vector
630template <class T, unsigned int D>
631bool SVector<T,D>::IsInUse( const T * p) const {
632 return p == fArray;
633}
634
635
636//==============================================================================
637// operator<<
638//==============================================================================
639template <class T, unsigned int D>
640inline std::ostream& operator<<(std::ostream& os, const SVector<T,D>& rhs) {
641 return rhs.Print(os);
642}
643
644
645
646
647} // namespace Math
648
649} // namespace ROOT
650
651
652#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:283
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:493
iterator begin()
STL iterator interface.
Definition: SVector.icc:553
SVector< T, D > & operator*=(const T &rhs)
self multiplication with a scalar
Definition: SVector.icc:431
SVector< T, D > & Unit()
transform vector into a vector of length 1
Definition: SVector.icc:480
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:615
const T * Array() const
return read-only pointer to internal array
Definition: SVector.icc:543
T apply(unsigned int i) const
access the parse tree. Index starts from zero
Definition: SVector.icc:540
const T & At(unsigned int i) const
read-only access of vector elements with check on index. Index starts from 0.
Definition: SVector.icc:599
bool operator!=(const T &rhs) const
element wise comparison
Definition: SVector.icc:264
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:588
SVector< T, D > & operator-=(const T &rhs)
self subtraction with a scalar
Definition: SVector.icc:403
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:631
bool operator<(const T &rhs) const
element wise comparison
Definition: SVector.icc:314
const T & operator[](unsigned int i) const
read-only access of vector elements. Index starts from 0.
Definition: SVector.icc:585
SVector< T, D > & operator+=(const T &rhs)
self addition with a scalar
Definition: SVector.icc:374
bool operator==(const T &rhs) const
element wise comparison
Definition: SVector.icc:233
SVector< T, D > & operator/=(const T &rhs)
self division with a scalar
Definition: SVector.icc:468
T fArray[D]
SVector data.
Definition: SVector.h:335
std::ostream & Print(std::ostream &os) const
used by operator<<()
Definition: SVector.icc:524
iterator end()
STL iterator interface.
Definition: SVector.icc:559
void SetElements(InputIterator begin, InputIterator end)
set vector elements copying the values iterator size must match vector size
Definition: SVector.icc:566
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