OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 <cmath>
21 #include <iostream>
22 #include <iomanip>
23 
25 //
26 // Definition of class Vektor.
27 //
29 
30 template<class T, unsigned D>
31 class Vektor
32 {
33 public:
34 
35  typedef T Element_t;
36  enum { ElemDim = 1 };
37  enum { Size = D };
38 
39  // Default Constructor initializes to zero.
40  Vektor() {
41  TSV_MetaAssignScalar<Vektor<T,D>,T,OpAssign>::apply(*this,T(0));
42  }
43 
44  // Copy Constructor
45  Vektor(const Vektor<T,D> &rhs) {
46  TSV_MetaAssign< Vektor<T,D> , Vektor<T,D> ,OpAssign >::apply(*this,rhs);
47  }
48 
49  // Templated Vektor constructor.
50  template<class T1, unsigned D1>
51  Vektor(const Vektor<T1,D1> &rhs) {
52  for (unsigned d=0; d<D; ++d)
53  X[d] = (d < D1) ? rhs[d] : T1(0);
54  }
55 
56  // Constructor from a single T
57  Vektor(const T& x00) {
58  TSV_MetaAssignScalar<Vektor<T,D>,T,OpAssign>::apply(*this,x00);
59  }
60 
61  // Constructors for fixed dimension
62  Vektor(const T& x00, const T& x01) {
63  PInsist(D==2, "Number of arguments does not match Vektor dimension!!");
64  X[0] = x00;
65  X[1] = x01;
66  }
67 
68  Vektor(const T& x00, const T& x01, const T& x02) {
69  PInsist(D==3, "Number of arguments does not match Vektor dimension!!");
70  X[0] = x00;
71  X[1] = x01;
72  X[2] = x02;
73  }
74 
75  Vektor(const T& x00, const T& x01, const T& x02, const T& x03) {
76  PInsist(D==4, "Number of arguments does not match Vektor dimension!!");
77  X[0] = x00;
78  X[1] = x01;
79  X[2] = x02;
80  X[3] = x03;
81  }
82 
83  // Destructor
84  ~Vektor() { }
85 
86  // Assignment Operators
87  const Vektor<T,D>& operator=(const Vektor<T,D> &rhs)
88  {
89  TSV_MetaAssign< Vektor<T,D> , Vektor<T,D> ,OpAssign> :: apply(*this,rhs);
90  return *this;
91  }
92  template<class T1>
93  const Vektor<T,D>& operator=(const Vektor<T1,D> &rhs)
94  {
95  TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> ,OpAssign> :: apply(*this,rhs);
96  return *this;
97  }
98  const Vektor<T,D>& operator=(const T& rhs)
99  {
100  TSV_MetaAssignScalar< Vektor<T,D> , T ,OpAssign > :: apply(*this,rhs);
101  return *this;
102  }
103 
104  // Accumulation Operators
105  template<class T1>
107  {
108  TSV_MetaAssign< Vektor<T,D> , Vektor<T1,D> , OpAddAssign > :: apply(*this,rhs);
109  return *this;
110  }
111  Vektor<T,D>& operator+=(const T& rhs)
112  {
113  TSV_MetaAssignScalar< Vektor<T,D> , T , OpAddAssign > :: apply(*this,rhs);
114  return *this;
115  }
116 
117  template<class T1>
119  {
121  return *this;
122  }
123  Vektor<T,D>& operator-=(const T& rhs)
124  {
125  TSV_MetaAssignScalar< Vektor<T,D> , T , OpSubtractAssign > :: apply(*this,rhs);
126  return *this;
127  }
128 
129  template<class T1>
131  {
133  return *this;
134  }
135  Vektor<T,D>& operator*=(const T& rhs)
136  {
137  TSV_MetaAssignScalar< Vektor<T,D> , T , OpMultipplyAssign > :: apply(*this,rhs);
138  return *this;
139  }
140 
141  template<class T1>
143  {
145  apply(*this,rhs);
146  return *this;
147  }
148  Vektor<T,D>& operator/=(const T& rhs)
149  {
151  apply(*this,rhs);
152  return *this;
153  }
154 
155  // Get and Set Operations
156  Element_t& operator[](unsigned int i);
157 
158  Element_t operator[](unsigned int i) const;
159 
160  Element_t& operator()(unsigned int i);
161 
162  Element_t operator()( unsigned int i) const;
163 
164  // Comparison operators.
165  bool operator==(const Vektor<T,D>& that) const {
167  }
168  bool operator!=(const Vektor<T,D>& that) const {
169  return !(*this == that);
170  }
171 
172  //----------------------------------------------------------------------
173  // parallel communication
175  m.setCopy(true);
176  ::putMessage(m, X, X + D);
177  return m;
178  }
179 
181  ::getMessage(m, X, X + D);
182  return m;
183  }
184 
185 private:
186 
187  // Just store D elements of type T.
188  T X[D];
189 
190 };
191 
192 template<class T, unsigned D>
194 {
195  PAssert (i<D);
196  return X[i];
197 }
198 
199 template<class T, unsigned D>
200 typename Vektor<T,D>::Element_t Vektor<T,D>::operator[](unsigned int i) const
201 {
202  PAssert (i<D);
203  return X[i];
204 }
205 
206 template<class T, unsigned D>
208 {
209  PAssert (i<D);
210  return X[i];
211 }
212 
213 template<class T, unsigned D>
214 typename Vektor<T,D>::Element_t Vektor<T,D>::operator()( unsigned int i) const
215 {
216  PAssert (i<D);
217  return X[i];
218 }
219 
221 //
222 // Unary Operators
223 //
225 
226 //----------------------------------------------------------------------
227 // unary operator-
228 template<class T, unsigned D>
230 {
231  return TSV_MetaUnary< Vektor<T,D> , OpUnaryMinus > :: apply(op);
232 }
233 
234 //----------------------------------------------------------------------
235 // unary operator+
236 template<class T, unsigned D>
237 inline const Vektor<T,D> &operator+(const Vektor<T,D> &op)
238 {
239  return op;
240 }
241 
243 //
244 // Binary Operators
245 //
247 
248 //
249 // Elementwise operators.
250 //
251 
258 
259 //----------------------------------------------------------------------
260 // dot product
261 //----------------------------------------------------------------------
262 
263 template < class T1, class T2, unsigned D >
265 dot(const Vektor<T1,D> &lhs, const Vektor<T2,D> &rhs)
266 {
267  return TSV_MetaDot< Vektor<T1,D> , Vektor<T2,D> > :: apply(lhs,rhs);
268 }
269 
270 //----------------------------------------------------------------------
271 // cross product
272 //----------------------------------------------------------------------
273 
274 template < class T1, class T2, unsigned D >
276 cross(const Vektor<T1,D> &lhs, const Vektor<T2,D> &rhs)
277 {
278  return TSV_MetaCross< Vektor<T1,D> , Vektor<T2,D> > :: apply(lhs,rhs);
279 }
280 
281 //----------------------------------------------------------------------
282 // euclidean norm
283 //----------------------------------------------------------------------
284 template < class T, unsigned D >
285 inline double
287 {
288  return std::sqrt(dot(a, a));
289 }
290 
291 
292 //----------------------------------------------------------------------
293 // I/O
294 template<class T, unsigned D>
295 inline std::ostream& operator<<(std::ostream& out, const Vektor<T,D>& rhs)
296 {
297  std::streamsize sw = out.width();
298  out << std::setw(1);
299  if (D >= 1) {
300  out << "( ";
301  for (unsigned int i=0; i<D - 1; i++)
302  out << std::setw(sw) << rhs[i] << " , ";
303  out << std::setw(sw) << rhs[D - 1] << " )";
304  } else {
305  out << "( " << std::setw(sw) << rhs[0] << " )";
306  }
307 
308  return out;
309 }
310 
311 #endif // VEKTOR_H
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
#define TSV_ELEMENTWISE_OPERATOR(TSV, OP, APP)
Definition: TSVMeta.h:58
#define X(arg)
Definition: fftpack.cpp:112
std::complex< double > a
#define PInsist(c, m)
Definition: PAssert.h:120
#define PAssert(c)
Definition: PAssert.h:102
double Min(double a, double b)
double Max(double a, double b)
Definition: Vektor.h:32
~Vektor()
Definition: Vektor.h:84
Vektor< T, D > & operator+=(const Vektor< T1, D > &rhs)
Definition: Vektor.h:106
Vektor(const T &x00, const T &x01, const T &x02)
Definition: Vektor.h:68
Vektor< T, D > & operator*=(const Vektor< T1, D > &rhs)
Definition: Vektor.h:130
const Vektor< T, D > & operator=(const T &rhs)
Definition: Vektor.h:98
Vektor< T, D > & operator+=(const T &rhs)
Definition: Vektor.h:111
Vektor(const T &x00)
Definition: Vektor.h:57
Vektor(const Vektor< T, D > &rhs)
Definition: Vektor.h:45
const Vektor< T, D > & operator=(const Vektor< T, D > &rhs)
Definition: Vektor.h:87
Vektor< T, D > & operator-=(const T &rhs)
Definition: Vektor.h:123
Vektor(const T &x00, const T &x01, const T &x02, const T &x03)
Definition: Vektor.h:75
T X[D]
Definition: Vektor.h:188
const Vektor< T, D > & operator=(const Vektor< T1, D > &rhs)
Definition: Vektor.h:93
Element_t operator()(unsigned int i) const
Definition: Vektor.h:214
Vektor< T, D > & operator/=(const T &rhs)
Definition: Vektor.h:148
Vektor(const Vektor< T1, D1 > &rhs)
Definition: Vektor.h:51
Vektor< T, D > & operator-=(const Vektor< T1, D > &rhs)
Definition: Vektor.h:118
@ ElemDim
Definition: Vektor.h:36
T Element_t
Definition: Vektor.h:35
bool operator==(const Vektor< T, D > &that) const
Definition: Vektor.h:165
Vektor(const T &x00, const T &x01)
Definition: Vektor.h:62
Vektor< T, D > & operator*=(const T &rhs)
Definition: Vektor.h:135
@ Size
Definition: Vektor.h:37
Vektor()
Definition: Vektor.h:40
Message & putMessage(Message &m) const
Definition: Vektor.h:174
bool operator!=(const Vektor< T, D > &that) const
Definition: Vektor.h:168
Vektor< T, D > & operator/=(const Vektor< T1, D > &rhs)
Definition: Vektor.h:142
Element_t & operator()(unsigned int i)
Definition: Vektor.h:207
Element_t operator[](unsigned int i) const
Definition: Vektor.h:200
Element_t & operator[](unsigned int i)
Definition: Vektor.h:193
Message & getMessage(Message &m)
Definition: Vektor.h:180
static bool apply(const T1 *lhs, const T2 *rhs)
Message & setCopy(const bool c)
Definition: Message.h:319
PETE_ComputeBinaryType< T1, T2, Op, Op::tag >::type type
double euclidean_norm(const Vektor< T, D > &a)
Definition: Vektor.h:286
const Vektor< T, D > & operator+(const Vektor< T, D > &op)
Definition: Vektor.h:237
std::ostream & operator<<(std::ostream &out, const Vektor< T, D > &rhs)
Definition: Vektor.h:295
Vektor< T, D > operator-(const Vektor< T, D > &op)
Definition: Vektor.h:229
PETEBinaryReturn< T1, T2, OpMultipply >::type dot(const Vektor< T1, D > &lhs, const Vektor< T2, D > &rhs)
Definition: Vektor.h:265
Vektor< typename PETEBinaryReturn< T1, T2, OpMultipply >::type, D > cross(const Vektor< T1, D > &lhs, const Vektor< T2, D > &rhs)
Definition: Vektor.h:276