OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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"
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 Vektor(std::initializer_list<T> init) {
62 unsigned i = 0;
63 for(auto it = init.begin(); it != init.end() && i < D; ++it, ++i) {
64 X[i] = *it; // copy at most D elements to avoid out-of-bounds access
65 }
66 for(; i < D; ++i) {
67 X[i] = T(0); // zero-fill remaining components if initializer list is shorter
68 }
69 }
70
71 // Destructor
72 ~Vektor() { }
73
74 // Assignment Operators
76 {
77 TSV_MetaAssign< Vektor<T,D> , Vektor<T,D> ,OpAssign> :: apply(*this,rhs);
78 return *this;
79 }
80 template<class T1>
82 {
84 return *this;
85 }
86 const Vektor<T,D>& operator=(const T& rhs)
87 {
88 TSV_MetaAssignScalar< Vektor<T,D> , T ,OpAssign > :: apply(*this,rhs);
89 return *this;
90 }
91
92 // Accumulation Operators
93 template<class T1>
95 {
97 return *this;
98 }
99 Vektor<T,D>& operator+=(const T& rhs)
100 {
101 TSV_MetaAssignScalar< Vektor<T,D> , T , OpAddAssign > :: apply(*this,rhs);
102 return *this;
103 }
104
105 template<class T1>
107 {
109 return *this;
110 }
111 Vektor<T,D>& operator-=(const T& rhs)
112 {
113 TSV_MetaAssignScalar< Vektor<T,D> , T , OpSubtractAssign > :: 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 , OpMultipplyAssign > :: apply(*this,rhs);
126 return *this;
127 }
128
129 template<class T1>
131 {
133 apply(*this,rhs);
134 return *this;
135 }
136 Vektor<T,D>& operator/=(const T& rhs)
137 {
139 apply(*this,rhs);
140 return *this;
141 }
142
143 // Get and Set Operations
144 Element_t& operator[](unsigned int i);
145
146 Element_t operator[](unsigned int i) const;
147
148 Element_t& operator()(unsigned int i);
149
150 Element_t operator()( unsigned int i) const;
151
152 // Comparison operators.
153 bool operator==(const Vektor<T,D>& that) const {
155 }
156 bool operator!=(const Vektor<T,D>& that) const {
157 return !(*this == that);
158 }
159
160 //----------------------------------------------------------------------
161 // parallel communication
163 m.setCopy(true);
164 ::putMessage(m, X, X + D);
165 return m;
166 }
167
169 ::getMessage(m, X, X + D);
170 return m;
171 }
172
173private:
174
175 // Just store D elements of type T.
176 T X[D];
177
178};
179
180template<class T, unsigned D>
182{
183 PAssert (i<D);
184 if (i >= D) i = D-1; // clamp index to avoid undefined behavior when assertions are disabled
185 return X[i];
186}
187
188template<class T, unsigned D>
190{
191 PAssert (i<D);
192 if (i >= D) i = D-1; // defensive fallback for release builds without runtime bounds checking
193 return X[i];
194}
195
196template<class T, unsigned D>
198{
199 PAssert (i<D);
200 return X[i];
201}
202
203template<class T, unsigned D>
204typename Vektor<T,D>::Element_t Vektor<T,D>::operator()( unsigned int i) const
205{
206 PAssert (i<D);
207 return X[i];
208}
209
211//
212// Unary Operators
213//
215
216//----------------------------------------------------------------------
217// unary operator-
218template<class T, unsigned D>
220{
221 return TSV_MetaUnary< Vektor<T,D> , OpUnaryMinus > :: apply(op);
222}
223
224//----------------------------------------------------------------------
225// unary operator+
226template<class T, unsigned D>
227inline const Vektor<T,D> &operator+(const Vektor<T,D> &op)
228{
229 return op;
230}
231
233//
234// Binary Operators
235//
237
238//
239// Elementwise operators.
240//
241
248
249//----------------------------------------------------------------------
250// dot product
251//----------------------------------------------------------------------
252
253template < class T1, class T2, unsigned D >
255dot(const Vektor<T1,D> &lhs, const Vektor<T2,D> &rhs)
256{
257 return TSV_MetaDot< Vektor<T1,D> , Vektor<T2,D> > :: apply(lhs,rhs);
258}
259
260//----------------------------------------------------------------------
261// cross product
262//----------------------------------------------------------------------
263
264template < class T1, class T2, unsigned D >
266cross(const Vektor<T1,D> &lhs, const Vektor<T2,D> &rhs)
267{
268 return TSV_MetaCross< Vektor<T1,D> , Vektor<T2,D> > :: apply(lhs,rhs);
269}
270
271//----------------------------------------------------------------------
272// euclidean norm
273//----------------------------------------------------------------------
274template < class T, unsigned D >
275inline double
277{
278 return std::sqrt(dot(a, a));
279}
280
281
282//----------------------------------------------------------------------
283// I/O
284template<class T, unsigned D>
285inline std::ostream& operator<<(std::ostream& out, const Vektor<T,D>& rhs)
286{
287 std::streamsize sw = out.width();
288 out << std::setw(1);
289 if (D >= 1) {
290 out << "( ";
291 for (unsigned int i=0; i<D - 1; i++)
292 out << std::setw(sw) << rhs[i] << " , ";
293 out << std::setw(sw) << rhs[D - 1] << " )";
294 } else {
295 out << "( " << std::setw(sw) << rhs[0] << " )";
296 }
297
298 return out;
299}
300
301#endif // VEKTOR_H
std::complex< double > a
#define X(arg)
Definition fftpack.cpp:106
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)
#define TSV_ELEMENTWISE_OPERATOR(TSV, OP, APP)
Definition TSVMeta.h:58
#define PAssert(c)
Definition PAssert.h:102
~Vektor()
Definition Vektor.h:72
Vektor< T, D > & operator-=(const T &rhs)
Definition Vektor.h:111
Message & getMessage(Message &m)
Definition Vektor.h:168
Message & putMessage(Message &m) const
Definition Vektor.h:162
Vektor(std::initializer_list< T > init)
Definition Vektor.h:61
Vektor< T, D > & operator-=(const Vektor< T1, D > &rhs)
Definition Vektor.h:106
Vektor< T, D > & operator*=(const T &rhs)
Definition Vektor.h:123
Vektor(const T &x00)
Definition Vektor.h:57
Vektor(const Vektor< T, D > &rhs)
Definition Vektor.h:45
Vektor< T, D > & operator+=(const T &rhs)
Definition Vektor.h:99
T X[D]
Definition Vektor.h:176
Element_t operator()(unsigned int i) const
Definition Vektor.h:204
const Vektor< T, D > & operator=(const Vektor< T1, D > &rhs)
Definition Vektor.h:81
Vektor(const Vektor< T1, D1 > &rhs)
Definition Vektor.h:51
@ ElemDim
Definition Vektor.h:36
T Element_t
Definition Vektor.h:35
Vektor< T, D > & operator*=(const Vektor< T1, D > &rhs)
Definition Vektor.h:118
bool operator==(const Vektor< T, D > &that) const
Definition Vektor.h:153
Vektor< T, D > & operator+=(const Vektor< T1, D > &rhs)
Definition Vektor.h:94
const Vektor< T, D > & operator=(const Vektor< T, D > &rhs)
Definition Vektor.h:75
@ Size
Definition Vektor.h:37
Vektor()
Definition Vektor.h:40
bool operator!=(const Vektor< T, D > &that) const
Definition Vektor.h:156
Element_t & operator()(unsigned int i)
Definition Vektor.h:197
Element_t operator[](unsigned int i) const
Definition Vektor.h:189
Vektor< T, D > & operator/=(const Vektor< T1, D > &rhs)
Definition Vektor.h:130
const Vektor< T, D > & operator=(const T &rhs)
Definition Vektor.h:86
Vektor< T, D > & operator/=(const T &rhs)
Definition Vektor.h:136
Element_t & operator[](unsigned int i)
Definition Vektor.h:181
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
Vektor< T, D > operator-(const Vektor< T, D > &op)
Definition Vektor.h:219
const Vektor< T, D > & operator+(const Vektor< T, D > &op)
Definition Vektor.h:227
double euclidean_norm(const Vektor< T, D > &a)
Definition Vektor.h:276
PETEBinaryReturn< T1, T2, OpMultipply >::type dot(const Vektor< T1, D > &lhs, const Vektor< T2, D > &rhs)
Definition Vektor.h:255
std::ostream & operator<<(std::ostream &out, const Vektor< T, D > &rhs)
Definition Vektor.h:285
Vektor< typename PETEBinaryReturn< T1, T2, OpMultipply >::type, D > cross(const Vektor< T1, D > &lhs, const Vektor< T2, D > &rhs)
Definition Vektor.h:266