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