Logo ROOT   6.10/09
Reference Guide
SVector.h
Go to the documentation of this file.
1 // @(#)root/smatrix:$Id$
2 // Author: T. Glebe, L. Moneta, J. Palacios 2005
3 
4 #ifndef ROOT_Math_SVector
5 #define ROOT_Math_SVector
6 /********************************************************************
7 //
8 // source:
9 //
10 // type: source code
11 //
12 // created: 16. 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 // 16 Mar 2001 (TG) creation
26 // 21 Mar 2001 (TG) SVector::value_type added
27 // 21 Mar 2001 (TG) added operators +=, -=, *=, /=
28 // 26 Mar 2001 (TG) added place_at()
29 // 03 Apr 2001 (TG) Array() added
30 // 06 Apr 2001 (TG) CTORS added
31 // 07 Apr 2001 (TG) CTORS added
32 // 22 Aug 2001 (TG) CTOR(T*,len) added
33 // 04 Sep 2001 (TG) moved inlined functions to .icc file
34 // 14 Jan 2002 (TG) added operator==(), operator!=(), operator>(), operator<()
35 //
36 ********************************************************************/
37 
38 #include "Math/MConfig.h"
39 
40 #include <iosfwd>
41 
42 // expression engine
43 
44 #include "Math/Expression.h"
45 
46 
47 
48 
49 namespace ROOT {
50 
51 namespace Math {
52 
53 // template <class T, unsigned int D, unsigned int D2> class MatRepStd;
54 
55 // template <class A, class T, unsigned int D, unsigned int D2 = 1, class R = MatRepStd<T,D,D2> > class Expr;
56 
57 //____________________________________________________________________________________________________________
58 /**
59  SVector: a generic fixed size Vector class.
60  The class is template on the scalar type and on the vector size D.
61  See \ref SVectorDoc
62 
63  Original author is Thorsten Glebe
64  HERA-B Collaboration, MPI Heidelberg (Germany)
65 
66  @ingroup SMatrixSVector
67 
68  @authors T. Glebe, L. Moneta and J. Palacios
69 
70 */
71 //==============================================================================
72 // SVector
73 //==============================================================================
74 template <class T, unsigned int D>
75 class SVector {
76 public:
77  /** @name --- Typedefs --- */
78  /// contained scalar type
79  typedef T value_type;
80 
81  /** STL iterator interface. */
82  typedef T* iterator;
83 
84  /** STL const_iterator interface. */
85  typedef const T* const_iterator;
86 
87 
88  /** @name --- Constructors --- */
89  /**
90  Default constructor: vector filled with zero values
91  */
92  SVector();
93  /// contruct from a vector expression
94  template <class A>
95  SVector(const VecExpr<A,T,D>& rhs);
96  /// copy contructor
97  SVector(const SVector<T,D>& rhs);
98 
99  // new constructs using STL iterator interface
100  // skip - need to solve the ambiguities
101 #ifdef LATER
102  /**
103  Constructor with STL iterator interface. The data will be copied into the vector
104  The iterator size must be equal to the vector size
105  */
106  template<class InputIterator>
107  explicit SVector(InputIterator begin, InputIterator end);
108 
109  /**
110  Constructor with STL iterator interface. The data will be copied into the vector
111  The size must be <= vector size
112  */
113  template<class InputIterator>
114  explicit SVector(InputIterator begin, unsigned int size);
115 
116 #else
117  // if you use iterator this is not necessary
118 
119  /// fill from array with len must be equal to D!
120  SVector( const T * a, unsigned int len);
121 
122  /** fill from a SVector iterator of type T*
123  (for ambiguities iterator cannot be generic )
124  */
125  SVector(const_iterator begin, const_iterator end);
126 
127 #endif
128  /// construct a vector of size 1 from a single scalar value
129  explicit SVector(const T& a1);
130  /// construct a vector of size 2 from 2 scalar values
131  SVector(const T& a1, const T& a2);
132  /// construct a vector of size 3 from 3 scalar values
133  SVector(const T& a1, const T& a2, const T& a3);
134  /// construct a vector of size 4 from 4 scalar values
135  SVector(const T& a1, const T& a2, const T& a3, const T& a4);
136  /// construct a vector of size 5 from 5 scalar values
137  SVector(const T& a1, const T& a2, const T& a3, const T& a4,
138  const T& a5);
139  /// construct a vector of size 6 from 6 scalar values
140  SVector(const T& a1, const T& a2, const T& a3, const T& a4,
141  const T& a5, const T& a6);
142  /// construct a vector of size 7 from 7 scalar values
143  SVector(const T& a1, const T& a2, const T& a3, const T& a4,
144  const T& a5, const T& a6, const T& a7);
145  /// construct a vector of size 8 from 8 scalar values
146  SVector(const T& a1, const T& a2, const T& a3, const T& a4,
147  const T& a5, const T& a6, const T& a7, const T& a8);
148  /// construct a vector of size 9 from 9 scalar values
149  SVector(const T& a1, const T& a2, const T& a3, const T& a4,
150  const T& a5, const T& a6, const T& a7, const T& a8,
151  const T& a9);
152  /// construct a vector of size 10 from 10 scalar values
153  SVector(const T& a1, const T& a2, const T& a3, const T& a4,
154  const T& a5, const T& a6, const T& a7, const T& a8,
155  const T& a9, const T& a10);
156 
157 
158 
159  /// assignment from a scalar (only for size 1 vector)
160  SVector<T,D>& operator=(const T& a1);
161  /// assignment from Vector Expression
162  template <class A>
164 
165  /** @name --- Access functions --- */
166 
167  /**
168  Enumeration defining the Vector size
169  */
170  enum {
171  /// return vector size
172  kSize = D
173  };
174 
175 
176  /// return dimension $D$
177  inline static unsigned int Dim() { return D; }
178  /// access the parse tree. Index starts from zero
179  T apply(unsigned int i) const;
180  /// return read-only pointer to internal array
181  const T* Array() const;
182  /// return non-const pointer to internal array
183  T* Array();
184 
185  /** @name --- STL-like interface --- */
186 
187 
188  /** STL iterator interface. */
189  iterator begin();
190 
191  /** STL iterator interface. */
192  iterator end();
193 
194  /** STL const_iterator interface. */
195  const_iterator begin() const;
196 
197  /** STL const_iterator interface. */
198  const_iterator end() const;
199 
200  /// set vector elements copying the values
201  /// iterator size must match vector size
202  template<class InputIterator>
203  void SetElements(InputIterator begin, InputIterator end);
204 
205  /// set vector elements copying the values
206  /// size must be <= vector size
207  template<class InputIterator>
208  void SetElements(InputIterator begin, unsigned int size);
209 
210 
211  /** @name --- Operators --- */
212 
213  /// element wise comparison
214  bool operator==(const T& rhs) const;
215  /// element wise comparison
216  bool operator!=(const T& rhs) const;
217  /// element wise comparison
218  bool operator==(const SVector<T,D>& rhs) const;
219  /// element wise comparison
220  bool operator!=(const SVector<T,D>& rhs) const;
221  /// element wise comparison
222  template <class A>
223  bool operator==(const VecExpr<A,T,D>& rhs) const;
224  /// element wise comparison
225  template <class A>
226  bool operator!=(const VecExpr<A,T,D>& rhs) const;
227 
228  /// element wise comparison
229  bool operator>(const T& rhs) const;
230  /// element wise comparison
231  bool operator<(const T& rhs) const;
232  /// element wise comparison
233  bool operator>(const SVector<T,D>& rhs) const;
234  /// element wise comparison
235  bool operator<(const SVector<T,D>& rhs) const;
236  /// element wise comparison
237  template <class A>
238  bool operator>(const VecExpr<A,T,D>& rhs) const;
239  /// element wise comparison
240  template <class A>
241  bool operator<(const VecExpr<A,T,D>& rhs) const;
242 
243  /// read-only access of vector elements. Index starts from 0.
244  const T& operator[](unsigned int i) const;
245  /// read-only access of vector elements. Index starts from 0.
246  const T& operator()(unsigned int i) const;
247  /// read-only access of vector elements with check on index. Index starts from 0.
248  const T& At(unsigned int i) const;
249  /// read/write access of vector elements. Index starts from 0.
250  T& operator[](unsigned int i);
251  /// read/write access of vector elements. Index starts from 0.
252  T& operator()(unsigned int i);
253  /// read/write access of vector elements with check on index. Index starts from 0.
254  T& At(unsigned int i);
255 
256  /// self addition with a scalar
257  SVector<T,D>& operator+=(const T& rhs);
258  /// self subtraction with a scalar
259  SVector<T,D>& operator-=(const T& rhs);
260  /// self multiplication with a scalar
261  SVector<T,D>& operator*=(const T& rhs);
262  /// self division with a scalar
263  SVector<T,D>& operator/=(const T& rhs);
264 
265 
266  /// self addition with another vector
267  SVector<T,D>& operator+=(const SVector<T,D>& rhs);
268  /// self subtraction with another vector
269  SVector<T,D>& operator-=(const SVector<T,D>& rhs);
270  /// self addition with a vector expression
271  template <class A>
273  /// self subtraction with a vector expression
274  template <class A>
276 
277 
278 #ifdef OLD_IMPL
279 #ifndef __CINT__
280  /// self element-wise multiplication with another vector
281  SVector<T,D>& operator*=(const SVector<T,D>& rhs);
282  /// self element-wise division with another vector
283  SVector<T,D>& operator/=(const SVector<T,D>& rhs);
284 
285  /// self element-wise multiplication with a vector expression
286  template <class A>
288  /// self element-wise division with a vector expression
289  template <class A>
291 
292 #endif
293 #endif
294 
295  /** @name --- Expert functions --- */
296  /// transform vector into a vector of length 1
297  SVector<T,D>& Unit();
298  /// place a sub-vector starting from the given position
299  template <unsigned int D2>
300  SVector<T,D>& Place_at(const SVector<T,D2>& rhs, unsigned int row);
301  /// place a sub-vector expression starting from the given position
302  template <class A, unsigned int D2>
303  SVector<T,D>& Place_at(const VecExpr<A,T,D2>& rhs, unsigned int row);
304 
305  /**
306  return a subvector of size N starting at the value row
307  where N is the size of the returned vector (SubVector::kSize)
308  Condition row+N <= D
309  */
310  template <class SubVector >
311  SubVector Sub(unsigned int row) const;
312 
313 
314  /**
315  Function to check if a vector is sharing same memory location of the passed pointer
316  This function is used by the expression templates to avoid the alias problem during
317  expression evaluation. When the vector is in use, for example in operations
318  like V = M * V, where M is a mtrix, a temporary object storing the intermediate result is automatically
319  created when evaluating the expression.
320 
321  */
322  bool IsInUse(const T* p) const;
323 
324 
325  /// used by operator<<()
326  std::ostream& Print(std::ostream& os) const;
327 
328 private:
329 
330  /** @name --- Data member --- */
331 
332  /// SVector data
333  T fArray[D];
334 }; // end of class SVector
335 
336 
337 //==============================================================================
338 // operator<<
339 //==============================================================================
340 template <class T, unsigned int D>
341 std::ostream& operator<<(std::ostream& os, const ROOT::Math::SVector<T,D>& rhs);
342 
343 
344 
345 } // namespace Math
346 
347 } // namespace ROOT
348 
349 
350 
351 #ifndef __CINT__
352 
353 // include implementation file
354 #include "Math/SVector.icc"
355 
356 // include operators and functions
357 #include "Math/UnaryOperators.h"
358 #include "Math/BinaryOperators.h"
359 #include "Math/Functions.h"
360 
361 #endif // __CINT__
362 
363 
364 #endif /* ROOT_Math_SVector */
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: StringConv.hxx:21
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
static unsigned int Dim()
return dimension $D$
Definition: SVector.h:177
std::ostream & Print(std::ostream &os) const
used by operator<<()
Definition: SVector.icc:517
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
TArc * a
Definition: textangle.C:12
return vector size
Definition: SVector.h:172
T * iterator
STL iterator interface.
Definition: SVector.h:82
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
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
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
T value_type
contained scalar type
Definition: SVector.h:79
SVector()
Default constructor: vector filled with zero values.
Definition: SVector.icc:53
SVector: a generic fixed size Vector class.