OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
Vector.h
Go to the documentation of this file.
1#ifndef CLASSIC_Vector_HH
2#define CLASSIC_Vector_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: Vector.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.4.2.2 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: Vector
13//
14// ------------------------------------------------------------------------
15// Class category: Algebra
16// ------------------------------------------------------------------------
17//
18// $Date: 2004/11/18 22:50:59 $
19// $Author: jsberg $
20//
21// ------------------------------------------------------------------------
22
23#include "Algebra/Array1D.h"
24#include "Utilities/SizeError.h"
25#include <algorithm>
26#include <numeric>
27#include <cmath>
28#include <functional>
29
30// Template class Vector<T>
31// ------------------------------------------------------------------------
33// A templated representation for vectors.
34// This class implements the arithmetic operations.
35
36template<class T>
37class Vector: public Array1D<T> {
38
39public:
40
42 // Constructs vector of zero length.
43 Vector();
44
46 // Reserve [b]n[/b] members and leave them undefined.
47 explicit Vector(int n);
48
50 // Reserve [b]n[/b] members and set them to [b]t[/b].
51 Vector(int n, const T &t);
52
54 Vector(const Array1D<T> &);
55
56 Vector(const Vector<T> &);
57 ~Vector();
59
61 Vector<T> operator-() const;
62
64 Vector<T> &operator*=(const T &);
65
67 Vector<T> &operator/=(const T &);
68
71
74};
75
76
77// Global Operators and Functions on Vector<T>
78// ------------------------------------------------------------------------
79
81template<class T> Vector<T> operator+(const Vector<T> &, const Vector<T> &);
82
84template<class T> Vector<T> operator-(const Vector<T> &, const Vector<T> &);
85
87template<class T> T operator*(const Vector<T> &, const Vector<T> &);
88
90template<class T> Vector<T> operator*(const Vector<T> &, const T &);
91
93template<class T> Vector<T> operator/(const Vector<T> &, const T &);
94
96template<class T> Vector<T> operator*(const T &, const Vector<T> &);
97
99template<class T> T euclidean_norm(const Vector<T> &);
100
102template<class T> T scaled_norm(const Array1D<T> D, const Vector<T> &V);
103
104
105// Implementation of template class Vector<T>.
106// ------------------------------------------------------------------------
107
108template<class T>
110 Array1D<T>()
111{}
112
113
114template<class T>
116 Array1D<T>(n)
117{}
118
119
120template<class T>
121Vector<T>::Vector(int n, const T &val):
122 Array1D<T>(n, val)
123{}
124
125
126template<class T>
128 Array1D<T>(array)
129{}
130
131
132template<class T>
134 Array1D<T>(array)
135{}
136
137
138template<class T>
140{}
141
142
143template<class T>
146 return *this;
147}
148
149
150template<class T>
152 Vector<T> result(this->size());
153 std::transform(this->begin(), this->end(), result.begin(), std::negate<T>());
154 return result;
155}
156
157
158template<class T>
160 std::transform(this->begin(), this->end(), this->begin(),
161 std::bind(std::multiplies<T>(), std::placeholders::_1, val));
162 return *this;
163}
164
165
166template<class T>
168 std::transform(this->begin(), this->end(), this->begin(),
169 std::bind(std::divides<T>(), std::placeholders::_1, val));
170 return *this;
171}
172
173
174template<class T>
176 std::transform(this->begin(), this->end(), array.begin(), this->begin(),
177 std::plus<T>());
178 return *this;
179}
180
181
182template<class T>
184 std::transform(this->begin(), this->end(), array.begin(), this->begin(),
185 std::minus<T>());
186 return *this;
187}
188
189
190// Global template operators.
191// ------------------------------------------------------------------------
192
193template<class T>
194Vector<T> operator+(const Vector<T> &V1, const Vector<T> &V2) {
195 Vector<T> result(V1.size());
196 std::transform(V1.begin(), V1.end(), V2.begin(), result.begin(),
197 std::plus<T>());
198 return result;
199}
200
201
202template<class T>
203Vector<T> operator-(const Vector<T> &V1, const Vector<T> &V2) {
204 Vector<T> result(V1.size());
205 std::transform(V1.begin(), V1.end(), V2.begin(), result.begin(),
206 std::minus<T>());
207 return result;
208}
209
210
211// NOTE: this is the standard vector dot product,
212// NOT memberwise multiplication.
213template<class T>
214T operator*(const Vector<T> &V1, const Vector<T> &V2) {
215 return std::inner_product(V1.begin(), V1.end(), V2.begin(), T(0));
216}
217
218
219template<class T>
220Vector<T> operator*(const Vector<T> &V1, const T &x) {
221 Vector<T> result = V1;
222 return result *= x;
223}
224
225
226template<class T>
227Vector<T> operator/(const Vector<T> &V1, const T &x) {
228 Vector<T> result = V1;
229 return result /= x;
230}
231
232
233// NOTE: this function assumes that multiplication for T is commutative.
234template<class T>
235Vector<T> operator*(const T &x, const Vector<T> &V1) {
236 Vector<T> result = V1;
237 std::transform(V1.begin(), V1.end(), result.begin(),
238 std::bind(std::multiplies<T>(), std::placeholders::_1, x));
239 return result;
240}
241
242
243template<class T> T euclidean_norm(const Vector<T> &V) {
244 return std::sqrt(std::inner_product(V.begin(), V.end(), V.begin(), T(0)));
245}
246
247
248template<class T> T scaled_norm(const Array1D<T> D, const Vector<T> &V) {
249 if(D.size() != V.size()) {
250 throw SizeError("scaled_norm()", "Inconsistent dimensions.");
251 }
252
253 T sum(0);
254 for(int i = 0; i < V.size(); ++i) {
255 double dv = D[i] * V[i];
256 sum += dv * dv;
257 }
258 return std::sqrt(sum);
259}
260
261#endif // CLASSIC_Vector_HH
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
T scaled_norm(const Array1D< T > D, const Vector< T > &V)
Euclidean norm of diagonal matrix D times vector V.
Definition: Vector.h:248
T operator*(const Vector< T > &, const Vector< T > &)
Vector dot product.
Definition: Vector.h:214
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition: Vector.h:243
Vector< T > operator/(const Vector< T > &, const T &)
Vector divided by scalar.
Definition: Vector.h:227
Vector< T > operator+(const Vector< T > &, const Vector< T > &)
Vector addition.
Definition: Vector.h:194
Vector< T > operator-(const Vector< T > &, const Vector< T > &)
Vector subtraction.
Definition: Vector.h:203
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1111
One-dimensional array.
Definition: Array1D.h:36
iterator begin()
Get beginning of data.
Definition: Array1D.h:204
int size() const
Get array size.
Definition: Array1D.h:228
iterator end()
Get end of data.
Definition: Array1D.h:210
Array1D< T > & operator=(const Array1D< T > &)
Definition: Array1D.h:159
Vector.
Definition: Vector.h:37
Vector< T > & operator/=(const T &)
Divide by scalar and assign.
Definition: Vector.h:167
Vector()
Default constructor.
Definition: Vector.h:109
Vector< T > & operator-=(const Vector< T > &)
Subtract vector and assign.
Definition: Vector.h:183
Vector< T > & operator=(const Vector< T > &)
Definition: Vector.h:144
Vector< T > & operator*=(const T &)
Multiply by scalar and assign.
Definition: Vector.h:159
Vector< T > & operator+=(const Vector< T > &)
Add vector and assign.
Definition: Vector.h:175
Vector< T > operator-() const
Change sign of vector.
Definition: Vector.h:151
~Vector()
Definition: Vector.h:139
Size error exception.
Definition: SizeError.h:33