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