Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 another vector
162  SVector<T,D>& operator=(const SVector<T,D>& rhs);
163  /// assignment from Vector Expression
164  template <class A>
165  SVector<T,D>& operator=(const VecExpr<A,T,D>& rhs);
166 
167  /** @name --- Access functions --- */
168 
169  /**
170  Enumeration defining the Vector size
171  */
172  enum {
173  /// return vector size
174  kSize = D
175  };
176 
177 
178  /// return dimension \f$D\f$
179  inline static unsigned int Dim() { return D; }
180  /// access the parse tree. Index starts from zero
181  T apply(unsigned int i) const;
182  /// return read-only pointer to internal array
183  const T* Array() const;
184  /// return non-const pointer to internal array
185  T* Array();
186 
187  /** @name --- STL-like interface --- */
188 
189 
190  /** STL iterator interface. */
191  iterator begin();
192 
193  /** STL iterator interface. */
194  iterator end();
195 
196  /** STL const_iterator interface. */
197  const_iterator begin() const;
198 
199  /** STL const_iterator interface. */
200  const_iterator end() const;
201 
202  /// set vector elements copying the values
203  /// iterator size must match vector size
204  template<class InputIterator>
205  void SetElements(InputIterator begin, InputIterator end);
206 
207  /// set vector elements copying the values
208  /// size must be <= vector size
209  template<class InputIterator>
210  void SetElements(InputIterator begin, unsigned int size);
211 
212 
213  /** @name --- Operators --- */
214 
215  /// element wise comparison
216  bool operator==(const T& rhs) const;
217  /// element wise comparison
218  bool operator!=(const T& rhs) const;
219  /// element wise comparison
220  bool operator==(const SVector<T,D>& rhs) const;
221  /// element wise comparison
222  bool operator!=(const SVector<T,D>& rhs) const;
223  /// element wise comparison
224  template <class A>
225  bool operator==(const VecExpr<A,T,D>& rhs) const;
226  /// element wise comparison
227  template <class A>
228  bool operator!=(const VecExpr<A,T,D>& rhs) const;
229 
230  /// element wise comparison
231  bool operator>(const T& rhs) const;
232  /// element wise comparison
233  bool operator<(const T& rhs) const;
234  /// element wise comparison
235  bool operator>(const SVector<T,D>& rhs) const;
236  /// element wise comparison
237  bool operator<(const SVector<T,D>& rhs) const;
238  /// element wise comparison
239  template <class A>
240  bool operator>(const VecExpr<A,T,D>& rhs) const;
241  /// element wise comparison
242  template <class A>
243  bool operator<(const VecExpr<A,T,D>& rhs) const;
244 
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. Index starts from 0.
248  const T& operator()(unsigned int i) const;
249  /// read-only access of vector elements with check on index. Index starts from 0.
250  const T& At(unsigned int i) const;
251  /// read/write access of vector elements. Index starts from 0.
252  T& operator[](unsigned int i);
253  /// read/write access of vector elements. Index starts from 0.
254  T& operator()(unsigned int i);
255  /// read/write access of vector elements with check on index. Index starts from 0.
256  T& At(unsigned int i);
257 
258  /// self addition with a scalar
259  SVector<T,D>& operator+=(const T& rhs);
260  /// self subtraction with a scalar
261  SVector<T,D>& operator-=(const T& rhs);
262  /// self multiplication with a scalar
263  SVector<T,D>& operator*=(const T& rhs);
264  /// self division with a scalar
265  SVector<T,D>& operator/=(const T& rhs);
266 
267 
268  /// self addition with another vector
269  SVector<T,D>& operator+=(const SVector<T,D>& rhs);
270  /// self subtraction with another vector
271  SVector<T,D>& operator-=(const SVector<T,D>& rhs);
272  /// self addition with a vector expression
273  template <class A>
274  SVector<T,D>& operator+=(const VecExpr<A,T,D>& rhs);
275  /// self subtraction with a vector expression
276  template <class A>
277  SVector<T,D>& operator-=(const VecExpr<A,T,D>& rhs);
278 
279 
280 #ifdef OLD_IMPL
281 #ifndef __CINT__
282  /// self element-wise multiplication with another vector
283  SVector<T,D>& operator*=(const SVector<T,D>& rhs);
284  /// self element-wise division with another vector
285  SVector<T,D>& operator/=(const SVector<T,D>& rhs);
286 
287  /// self element-wise multiplication with a vector expression
288  template <class A>
289  SVector<T,D>& operator*=(const VecExpr<A,T,D>& rhs);
290  /// self element-wise division with a vector expression
291  template <class A>
292  SVector<T,D>& operator/=(const VecExpr<A,T,D>& rhs);
293 
294 #endif
295 #endif
296 
297  /** @name --- Expert functions --- */
298  /// transform vector into a vector of length 1
299  SVector<T,D>& Unit();
300  /// place a sub-vector starting from the given position
301  template <unsigned int D2>
302  SVector<T,D>& Place_at(const SVector<T,D2>& rhs, unsigned int row);
303  /// place a sub-vector expression starting from the given position
304  template <class A, unsigned int D2>
305  SVector<T,D>& Place_at(const VecExpr<A,T,D2>& rhs, unsigned int row);
306 
307  /**
308  return a subvector of size N starting at the value row
309  where N is the size of the returned vector (SubVector::kSize)
310  Condition row+N <= D
311  */
312  template <class SubVector >
313  SubVector Sub(unsigned int row) const;
314 
315 
316  /**
317  Function to check if a vector is sharing same memory location of the passed pointer
318  This function is used by the expression templates to avoid the alias problem during
319  expression evaluation. When the vector is in use, for example in operations
320  like V = M * V, where M is a mtrix, a temporary object storing the intermediate result is automatically
321  created when evaluating the expression.
322 
323  */
324  bool IsInUse(const T* p) const;
325 
326 
327  /// used by operator<<()
328  std::ostream& Print(std::ostream& os) const;
329 
330 private:
331 
332  /** @name --- Data member --- */
333 
334  /// SVector data
335  T fArray[D];
336 }; // end of class SVector
337 
338 
339 //==============================================================================
340 // operator<<
341 //==============================================================================
342 template <class T, unsigned int D>
343 std::ostream& operator<<(std::ostream& os, const ROOT::Math::SVector<T,D>& rhs);
344 
345 
346 
347 } // namespace Math
348 
349 } // namespace ROOT
350 
351 
352 
353 #ifndef __CINT__
354 
355 // include implementation file
356 #include "Math/SVector.icc"
357 
358 // include operators and functions
359 #include "Math/UnaryOperators.h"
360 #include "Math/BinaryOperators.h"
361 #include "Math/Functions.h"
362 
363 #endif // __CINT__
364 
365 
366 #endif /* ROOT_Math_SVector */