OPAL (Object Oriented Parallel Accelerator Library) 2024.2
OPAL
ippl/src/AppTypes/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"
18#include "AppTypes/TSVMeta.h"
19
20#include <cmath>
21#include <iostream>
22#include <iomanip>
23
25//
26// Definition of class Vektor.
27//
29
30template<class T, unsigned D>
31class Vektor
32{
33public:
34
35 typedef T Element_t;
36 enum { ElemDim = 1 };
37 enum { Size = D };
38
39 // Default Constructor initializes to zero.
41 TSV_MetaAssignScalar<Vektor<T,D>,T,OpAssign>::apply(*this,T(0));
42 }
43
44 // Copy Constructor
45 Vektor(const Vektor<T,D> &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
88 {
89 TSV_MetaAssign< Vektor<T,D> , Vektor<T,D> ,OpAssign> :: apply(*this,rhs);
90 return *this;
91 }
92 template<class T1>
94 {
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
185private:
186
187 // Just store D elements of type T.
188 T X[D];
189
190};
191
192template<class T, unsigned D>
194{
195 PAssert (i<D);
196 return X[i];
197}
198
199template<class T, unsigned D>
201{
202 PAssert (i<D);
203 return X[i];
204}
205
206template<class T, unsigned D>
208{
209 PAssert (i<D);
210 return X[i];
211}
212
213template<class T, unsigned D>
214typename 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-
228template<class T, unsigned D>
230{
231 return TSV_MetaUnary< Vektor<T,D> , OpUnaryMinus > :: apply(op);
232}
233
234//----------------------------------------------------------------------
235// unary operator+
236template<class T, unsigned D>
237inline 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
263template < class T1, class T2, unsigned D >
265dot(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
274template < class T1, class T2, unsigned D >
276cross(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//----------------------------------------------------------------------
284template < class T, unsigned D >
285inline double
287{
288 return std::sqrt(dot(a, a));
289}
290
291
292//----------------------------------------------------------------------
293// I/O
294template<class T, unsigned D>
295inline 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
ReduceLoc< typename PETEBinaryReturn< T1, T2, FnMin >::type, LOC > Min(const ReduceLoc< T1, LOC > &lhs, const ReduceLoc< T2, LOC > &rhs)
ReduceLoc< typename PETEBinaryReturn< T1, T2, FnMax >::type, LOC > Max(const ReduceLoc< T1, LOC > &lhs, const ReduceLoc< T2, LOC > &rhs)
PETE_ComputeBinaryType< T1, T2, Op, Op::tag >::type type
std::complex< double > a
#define TSV_ELEMENTWISE_OPERATOR(TSV, OP, APP)
Definition TSVMeta.h:58
#define PInsist(c, m)
Definition PAssert.h:120
#define PAssert(c)
Definition PAssert.h:102
Vektor(const T &x00, const T &x01, const T &x02)
Vektor< T, D > & operator-=(const T &rhs)
Message & getMessage(Message &m)
Message & putMessage(Message &m) const
Vektor< T, D > & operator-=(const Vektor< T1, D > &rhs)
Vektor< T, D > & operator*=(const T &rhs)
Vektor(const T &x00)
Vektor(const Vektor< T, D > &rhs)
Vektor< T, D > & operator+=(const T &rhs)
Vektor(const T &x00, const T &x01, const T &x02, const T &x03)
Element_t operator()(unsigned int i) const
const Vektor< T, D > & operator=(const Vektor< T1, D > &rhs)
Vektor(const Vektor< T1, D1 > &rhs)
Vektor< T, D > & operator*=(const Vektor< T1, D > &rhs)
bool operator==(const Vektor< T, D > &that) const
Vektor< T, D > & operator+=(const Vektor< T1, D > &rhs)
Vektor(const T &x00, const T &x01)
const Vektor< T, D > & operator=(const Vektor< T, D > &rhs)
bool operator!=(const Vektor< T, D > &that) const
Element_t & operator()(unsigned int i)
Element_t operator[](unsigned int i) const
Vektor< T, D > & operator/=(const Vektor< T1, D > &rhs)
const Vektor< T, D > & operator=(const T &rhs)
Vektor< T, D > & operator/=(const T &rhs)
Element_t & operator[](unsigned int i)
static bool apply(const T1 *lhs, const T2 *rhs)
Message & setCopy(const bool c)
Definition Message.h:319
Vektor< T, D > operator-(const Vektor< T, D > &op)
const Vektor< T, D > & operator+(const Vektor< T, D > &op)
double euclidean_norm(const Vektor< T, D > &a)
PETEBinaryReturn< T1, T2, OpMultipply >::type dot(const Vektor< T1, D > &lhs, const Vektor< T2, D > &rhs)
std::ostream & operator<<(std::ostream &out, const Vektor< T, D > &rhs)
Vektor< typename PETEBinaryReturn< T1, T2, OpMultipply >::type, D > cross(const Vektor< T1, D > &lhs, const Vektor< T2, D > &rhs)