OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Vektor.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef VEKTOR_H
12 #define VEKTOR_H
13 
14 // include files
15 #include "Utility/PAssert.h"
16 #include "Message/Message.h"
17 #include "PETE/IpplExpressions.h"
18 #include "AppTypes/TSVMeta.h"
19 
20 #include <iostream>
21 #include <iomanip>
22 
24 //
25 // Definition of class Vektor.
26 //
28 
29 template<class T, unsigned D>
30 class Vektor
31 {
32 public:
33 
34  typedef T Element_t;
35  enum { ElemDim = 1 };
36  enum { Size = D };
37 
38  // Default Constructor initializes to zero.
39  Vektor() {
40  TSV_MetaAssignScalar<Vektor<T,D>,T,OpAssign>::apply(*this,T(0));
41  }
42 
43  // Copy Constructor
44  Vektor(const Vektor<T,D> &rhs) {
45  TSV_MetaAssign< Vektor<T,D> , Vektor<T,D> ,OpAssign >::apply(*this,rhs);
46  }
47 
48  // Templated Vektor constructor.
49  template<class T1, unsigned D1>
50  Vektor(const Vektor<T1,D1> &rhs) {
51  for (unsigned d=0; d<D; ++d)
52  X[d] = (d < D1) ? rhs[d] : T1(0);
53  }
54 
55  // Constructor from a single T
56  Vektor(const T& x00) {
57  TSV_MetaAssignScalar<Vektor<T,D>,T,OpAssign>::apply(*this,x00);
58  }
59 
60  // Constructors for fixed dimension
61  Vektor(const T& x00, const T& x01) {
62  PInsist(D==2, "Number of arguments does not match Vektor dimension!!");
63  X[0] = x00;
64  X[1] = x01;
65  }
66 
67  Vektor(const T& x00, const T& x01, const T& x02) {
68  PInsist(D==3, "Number of arguments does not match Vektor dimension!!");
69  X[0] = x00;
70  X[1] = x01;
71  X[2] = x02;
72  }
73 
74  Vektor(const T& x00, const T& x01, const T& x02, const T& x03) {
75  PInsist(D==4, "Number of arguments does not match Vektor dimension!!");
76  X[0] = x00;
77  X[1] = x01;
78  X[2] = x02;
79  X[3] = x03;
80  }
81 
82  // Destructor
83  ~Vektor() { }
84 
85  // Assignment Operators
86  const Vektor<T,D>& operator=(const Vektor<T,D> &rhs)
87  {
88  TSV_MetaAssign< Vektor<T,D> , Vektor<T,D> ,OpAssign> :: apply(*this,rhs);
89  return *this;
90  }
91  template<class T1>
92  const Vektor<T,D>& operator=(const Vektor<T1,D> &rhs)
93  {
94  TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> ,OpAssign> :: apply(*this,rhs);
95  return *this;
96  }
97  const Vektor<T,D>& operator=(const T& rhs)
98  {
99  TSV_MetaAssignScalar< Vektor<T,D> , T ,OpAssign > :: apply(*this,rhs);
100  return *this;
101  }
102 
103  // Accumulation Operators
104  template<class T1>
106  {
107  TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> , OpAddAssign > :: apply(*this,rhs);
108  return *this;
109  }
110  Vektor<T,D>& operator+=(const T& rhs)
111  {
112  TSV_MetaAssignScalar< Vektor<T,D> , T , OpAddAssign > :: apply(*this,rhs);
113  return *this;
114  }
115 
116  template<class T1>
118  {
120  return *this;
121  }
122  Vektor<T,D>& operator-=(const T& rhs)
123  {
124  TSV_MetaAssignScalar< Vektor<T,D> , T , OpSubtractAssign > :: apply(*this,rhs);
125  return *this;
126  }
127 
128  template<class T1>
130  {
132  return *this;
133  }
134  Vektor<T,D>& operator*=(const T& rhs)
135  {
136  TSV_MetaAssignScalar< Vektor<T,D> , T , OpMultipplyAssign > :: apply(*this,rhs);
137  return *this;
138  }
139 
140  template<class T1>
142  {
144  apply(*this,rhs);
145  return *this;
146  }
147  Vektor<T,D>& operator/=(const T& rhs)
148  {
150  apply(*this,rhs);
151  return *this;
152  }
153 
154  // Get and Set Operations
155  Element_t& operator[](unsigned int i);
156 
157  Element_t operator[](unsigned int i) const;
158 
159  Element_t& operator()(unsigned int i);
160 
161  Element_t operator()( unsigned int i) const;
162 
163  // Comparison operators.
164  bool operator==(const Vektor<T,D>& that) const {
166  }
167  bool operator!=(const Vektor<T,D>& that) const {
168  return !(*this == that);
169  }
170 
171  //----------------------------------------------------------------------
172  // parallel communication
174  m.setCopy(true);
175  ::putMessage(m, X, X + D);
176  return m;
177  }
178 
180  ::getMessage(m, X, X + D);
181  return m;
182  }
183 
184 private:
185 
186  // Just store D elements of type T.
187  T X[D];
188 
189 };
190 
191 template<class T, unsigned D>
193 {
194  PAssert (i<D);
195  return X[i];
196 }
197 
198 template<class T, unsigned D>
199 typename Vektor<T,D>::Element_t Vektor<T,D>::operator[](unsigned int i) const
200 {
201  PAssert (i<D);
202  return X[i];
203 }
204 
205 template<class T, unsigned D>
207 {
208  PAssert (i<D);
209  return X[i];
210 }
211 
212 template<class T, unsigned D>
213 typename Vektor<T,D>::Element_t Vektor<T,D>::operator()( unsigned int i) const
214 {
215  PAssert (i<D);
216  return X[i];
217 }
218 
220 //
221 // Unary Operators
222 //
224 
225 //----------------------------------------------------------------------
226 // unary operator-
227 template<class T, unsigned D>
229 {
230  return TSV_MetaUnary< Vektor<T,D> , OpUnaryMinus > :: apply(op);
231 }
232 
233 //----------------------------------------------------------------------
234 // unary operator+
235 template<class T, unsigned D>
236 inline const Vektor<T,D> &operator+(const Vektor<T,D> &op)
237 {
238  return op;
239 }
240 
242 //
243 // Binary Operators
244 //
246 
247 //
248 // Elementwise operators.
249 //
250 
253 TSV_ELEMENTWISE_OPERATOR(Vektor,operator*,OpMultipply)
254 TSV_ELEMENTWISE_OPERATOR(Vektor,operator/,OpDivide)
257 
258 //----------------------------------------------------------------------
259 // dot product
260 //----------------------------------------------------------------------
261 
262 template < class T1, class T2, unsigned D >
263 inline typename PETEBinaryReturn<T1,T2,OpMultipply>::type
264 dot(const Vektor<T1,D> &lhs, const Vektor<T2,D> &rhs)
265 {
266  return TSV_MetaDot< Vektor<T1,D> , Vektor<T2,D> > :: apply(lhs,rhs);
267 }
268 
269 //----------------------------------------------------------------------
270 // cross product
271 //----------------------------------------------------------------------
272 
273 template < class T1, class T2, unsigned D >
275 cross(const Vektor<T1,D> &lhs, const Vektor<T2,D> &rhs)
276 {
277  return TSV_MetaCross< Vektor<T1,D> , Vektor<T2,D> > :: apply(lhs,rhs);
278 }
279 
280 //----------------------------------------------------------------------
281 // I/O
282 template<class T, unsigned D>
283 inline std::ostream& operator<<(std::ostream& out, const Vektor<T,D>& rhs)
284 {
285  std::streamsize sw = out.width();
286  out << std::setw(1);
287  if (D >= 1) {
288  out << "( ";
289  for (unsigned int i=0; i<D - 1; i++)
290  out << std::setw(sw) << rhs[i] << " , ";
291  out << std::setw(sw) << rhs[D - 1] << " )";
292  } else {
293  out << "( " << std::setw(sw) << rhs[0] << " )";
294  }
295 
296  return out;
297 }
298 
299 #endif // VEKTOR_H
300 
301 /***************************************************************************
302  * $RCSfile: Vektor.h,v $ $Author: adelmann $
303  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:24 $
304  * IPPL_VERSION_ID: $Id: Vektor.h,v 1.1.1.1 2003/01/23 07:40:24 adelmann Exp $
305  ***************************************************************************/
Matrix< T > operator+(const Matrix< T > &, const Matrix< T > &)
Matrix addition.
Definition: Matrix.h:275
Vektor(const T &x00, const T &x01)
Definition: Vektor.h:61
Definition: TSVMeta.h:24
Definition: rbendmap.h:8
T X[D]
Definition: Vektor.h:187
Definition: rbendmap.h:8
Vektor()
Definition: Vektor.h:39
Vektor< T, D > & operator+=(const Vektor< T1, D > &rhs)
Definition: Vektor.h:105
Vektor< T, D > & operator/=(const Vektor< T1, D > &rhs)
Definition: Vektor.h:141
Vektor< T, D > & operator*=(const T &rhs)
Definition: Vektor.h:134
Vektor< T, D > & operator+=(const T &rhs)
Definition: Vektor.h:110
Vektor< T, D > & operator-=(const T &rhs)
Definition: Vektor.h:122
Element_t & operator[](unsigned int i)
Definition: Vektor.h:192
const Vektor< T, D > & operator=(const T &rhs)
Definition: Vektor.h:97
double dot(const Vector3D &lhs, const Vector3D &rhs)
Vector dot product.
Definition: Vector3D.cpp:118
Vektor(const Vektor< T, D > &rhs)
Definition: Vektor.h:44
bool operator==(const Vektor< T, D > &that) const
Definition: Vektor.h:164
double Max(double a, double b)
Vector3D cross(const Vector3D &lhs, const Vector3D &rhs)
Vector cross product.
Definition: Vector3D.cpp:111
Vektor(const T &x00, const T &x01, const T &x02)
Definition: Vektor.h:67
Vektor< T, D > & operator-=(const Vektor< T1, D > &rhs)
Definition: Vektor.h:117
Vektor(const Vektor< T1, D1 > &rhs)
Definition: Vektor.h:50
static bool apply(const T1 *lhs, const T2 *rhs)
Vektor< T, D > & operator*=(const Vektor< T1, D > &rhs)
Definition: Vektor.h:129
double Min(double a, double b)
~Vektor()
Definition: Vektor.h:83
Vektor< T, D > & operator/=(const T &rhs)
Definition: Vektor.h:147
#define PAssert(c)
Definition: PAssert.h:117
Message & putMessage(Message &m) const
Definition: Vektor.h:173
const Vektor< T, D > & operator=(const Vektor< T1, D > &rhs)
Definition: Vektor.h:92
Vektor(const T &x00, const T &x01, const T &x02, const T &x03)
Definition: Vektor.h:74
#define PInsist(c, m)
Definition: PAssert.h:135
Matrix< T > operator-(const Matrix< T > &, const Matrix< T > &)
Matrix subtraction.
Definition: Matrix.h:282
Element_t & operator()(unsigned int i)
Definition: Vektor.h:206
const Vektor< T, D > & operator=(const Vektor< T, D > &rhs)
Definition: Vektor.h:86
T Element_t
Definition: Vektor.h:34
Vektor(const T &x00)
Definition: Vektor.h:56
Message & getMessage(Message &m)
Definition: Vektor.h:179
bool operator!=(const Vektor< T, D > &that) const
Definition: Vektor.h:167
#define TSV_ELEMENTWISE_OPERATOR(TSV, OP, APP)
Definition: TSVMeta.h:58
Message & setCopy(const bool c)
Definition: Message.h:327