OPAL (Object Oriented Parallel Accelerator Library) 2022.1
OPAL
FVector.h
Go to the documentation of this file.
1#ifndef CLASSIC_FVector_HH
2#define CLASSIC_FVector_HH
3
4// ------------------------------------------------------------------------
5// $RCSfile: FVector.h,v $
6// ------------------------------------------------------------------------
7// $Revision: 1.1.1.1.2.1 $
8// ------------------------------------------------------------------------
9// Copyright: see Copyright.readme
10// ------------------------------------------------------------------------
11//
12// Template class: FVector<T,N>
13//
14// ------------------------------------------------------------------------
15// Class category: FixedAlgebra
16// ------------------------------------------------------------------------
17//
18// $Date: 2004/11/18 22:18:06 $
19// $Author: jsberg $
20//
21// ------------------------------------------------------------------------
22
24#include <algorithm>
25#include <numeric>
26#include <cmath>
27#include <functional>
28
29// Template class FVector<T,N>
30// ------------------------------------------------------------------------
32// This class implements the arithmetic operations.
33// The copy constructor, destructor, and assignment operator generated
34// by the compiler perform the correct operation. For speed reasons
35// they are not implemented.
36
37template<class T, int N>
38class FVector: public FArray1D<T, N> {
39
40public:
41
43 // Construct zero FVector.
45
47 // Set all vector elements to [b]t[/b].
48 explicit FVector(const T &t);
49
51 // Fill all vector elements from the C-array [b]t[/b].
52 explicit FVector(const T *t);
53
56
59
61 FVector &operator*=(const T &);
62
64 FVector &operator/=(const T &);
65
68
71};
72
73
74// ------------------------------------------------------------------------
75
77template<class T, int N>
79
81template<class T, int N>
83
85template<class T, int N>
86T operator*(const FVector<T, N> &, const FVector<T, N> &);
87
89template<class T, int N>
90FVector<T, N> operator*(const FVector<T, N> &, const T &);
91
93template<class T, int N>
94FVector<T, N> operator/(const FVector<T, N> &, const T &);
95
97template<class T, int N>
98FVector<T, N> operator*(const T &, const FVector<T, N> &);
99
101template<class T, int N>
103
105template<class T, int N>
106T scaled_norm(const FArray1D<T, N> D, const FVector<T, N> &V);
107
108
109// Implementation of template class FVector<T,N>.
110// ------------------------------------------------------------------------
111
112template<class T, int N>
114 FArray1D<T, N>()
115{}
116
117
118template<class T, int N>
120 FArray1D<T, N>(val)
121{}
122
123
124template<class T, int N>
126 FArray1D<T, N>(rhs)
127{}
128
129
130template<class T, int N>
132 FArray1D<T, N>(rhs)
133{}
134
135
136template<class T, int N>
138 FVector<T, N> result;
139 std::transform(this->begin(), this->end(), result.begin(), std::negate<T>());
140 return result;
141}
142
143
144template<class T, int N>
146 std::transform(this->begin(), this->end(), this->begin(),
147 std::bind(std::multiplies<T>(), std::placeholders::_1, val));
148 return *this;
149}
150
151
152template<class T, int N>
154 std::transform(this->begin(), this->end(), this->begin(),
155 std::bind(std::divides<T>(), std::placeholders::_1, val));
156 return *this;
157}
158
159
160template<class T, int N>
162 std::transform(this->begin(), this->end(), rhs.begin(), this->begin(),
163 std::plus<T>());
164 return *this;
165}
166
167
168template<class T, int N>
170 std::transform(this->begin(), this->end(), rhs.begin(), this->begin(),
171 std::minus<T>());
172 return *this;
173}
174
175
176// Global template operators.
177// ------------------------------------------------------------------------
178
179template<class T, int N>
181 FVector<T, N> result;
182 std::transform(lhs.begin(), lhs.end(), rhs.begin(),
183 result.begin(), std::plus<T>());
184 return result;
185}
186
187
188template<class T, int N>
190 FVector<T, N> result;
191 std::transform(lhs.begin(), lhs.end(), rhs.begin(),
192 result.begin(), std::minus<T>());
193 return result;
194}
195
196
197// NOTE: this is the standard FVector dot product,
198// NOT memberwise multiplication.
199template<class T, int N>
200T operator*(const FVector<T, N> &lhs, const FVector<T, N> &rhs) {
201 return std::inner_product(lhs.begin(), lhs.end(), rhs.begin(), T(0));
202}
203
204
205template<class T, int N>
206FVector<T, N> operator*(const FVector<T, N> &lhs, const T &x) {
207 FVector<T, N> result(lhs);
208 return result *= x;
209}
210
211
212template<class T, int N>
213FVector<T, N> operator/(const FVector<T, N> &lhs, const T &x) {
214 FVector<T, N> result(lhs);
215 return result /= x;
216}
217
218
219// NOTE: this function assumes that multiplication for T is commutative.
220template<class T, int N>
221FVector<T, N> operator*(const T &x, const FVector<T, N> &lhs) {
222 FVector<T, N> result(lhs);
223 std::transform(lhs.begin(), lhs.end(), result.begin(),
224 std::bind(std::multiplies<T>(), std::placeholders::_1, x));
225 return result;
226}
227
228
229template<class T, int N>
231 return sqrt(std::inner_product(V.begin(), V.end(), V.begin(), T(0)));
232}
233
234
235template<class T, int N>
237 T sum(0);
238
239 for(int i = 0; i < V.size(); ++i) {
240 double dv = D[i] * V[i];
241 sum += dv * dv;
242 }
243
244 return sqrt(sum);
245}
246
247#endif // CLASSIC_FVector_HH
FVector< T, N > operator-(const FVector< T, N > &, const FVector< T, N > &)
Subtract.
Definition: FVector.h:189
T scaled_norm(const FArray1D< T, N > D, const FVector< T, N > &V)
Euclidean norm of diagonal matrix D times FVector V.
Definition: FVector.h:236
T euclidean_norm(const FVector< T, N > &)
Euclidean norm.
Definition: FVector.h:230
FVector< T, N > operator/(const FVector< T, N > &, const T &)
Divide.
Definition: FVector.h:213
FVector< T, N > operator+(const FVector< T, N > &, const FVector< T, N > &)
Add.
Definition: FVector.h:180
T operator*(const FVector< T, N > &, const FVector< T, N > &)
Dot product.
Definition: FVector.h:200
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::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1111
A templated representation for one-dimensional arrays.
Definition: FArray1D.h:39
int size() const
Get array size.
Definition: FArray1D.h:216
iterator end()
Get iterator pointing past end of array.
Definition: FArray1D.h:198
iterator begin()
Get iterator pointing to beginning of array.
Definition: FArray1D.h:192
A templated representation for vectors.
Definition: FVector.h:38
FVector & operator*=(const T &)
Multiply by scalar and assign.
Definition: FVector.h:145
FVector(const T *t)
Constructor.
Definition: FVector.h:125
FVector(const T &t)
Constructor.
Definition: FVector.h:119
FVector(const FArray1D< T, N > &)
Conversion from one-dimensional array.
Definition: FVector.h:131
FVector()
Constructor.
Definition: FVector.h:113
FVector & operator+=(const FVector &)
Add FVector and assign.
Definition: FVector.h:161
FVector & operator-=(const FVector &)
Subtract FVector and assign.
Definition: FVector.h:169
FVector & operator/=(const T &)
Divide by scalar and assign.
Definition: FVector.h:153
FVector operator-() const
Change sign.
Definition: FVector.h:137