OPAL (Object Oriented Parallel Accelerator Library) 2024.2
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// ------------------------------------------------------------------------
111template<class T, int N>
113 FArray1D<T, N>()
114{}
115
116template<class T, int N>
118 FArray1D<T, N>(val)
119{}
120
121template<class T, int N>
123 FArray1D<T, N>(rhs)
124{}
125
126template<class T, int N>
128 FArray1D<T, N>(rhs)
129{}
130
131template<class T, int N>
133 FVector<T, N> result;
134 std::transform(this->begin(), this->end(), result.begin(), std::negate<T>());
135 return result;
136}
137
138template<class T, int N>
140 std::transform(this->begin(), this->end(), this->begin(),
141 std::bind(std::multiplies<T>(), std::placeholders::_1, val));
142 return *this;
143}
144
145template<class T, int N>
147 std::transform(this->begin(), this->end(), this->begin(),
148 std::bind(std::divides<T>(), std::placeholders::_1, val));
149 return *this;
150}
151
152template<class T, int N>
154 std::transform(this->begin(), this->end(), rhs.begin(), this->begin(),
155 std::plus<T>());
156 return *this;
157}
158
159template<class T, int N>
161 std::transform(this->begin(), this->end(), rhs.begin(), this->begin(),
162 std::minus<T>());
163 return *this;
164}
165
166// Global template operators.
167// ------------------------------------------------------------------------
168template<class T, int N>
170 FVector<T, N> result;
171 std::transform(lhs.begin(), lhs.end(), rhs.begin(),
172 result.begin(), std::plus<T>());
173 return result;
174}
175
176template<class T, int N>
178 FVector<T, N> result;
179 std::transform(lhs.begin(), lhs.end(), rhs.begin(),
180 result.begin(), std::minus<T>());
181 return result;
182}
183
184// NOTE: this is the standard FVector dot product,
185// NOT memberwise multiplication.
186template<class T, int N>
187T operator*(const FVector<T, N> &lhs, const FVector<T, N> &rhs) {
188 return std::inner_product(lhs.begin(), lhs.end(), rhs.begin(), T(0));
189}
190
191template<class T, int N>
192FVector<T, N> operator*(const FVector<T, N> &lhs, const T &x) {
193 FVector<T, N> result(lhs);
194 return result *= x;
195}
196
197template<class T, int N>
198FVector<T, N> operator/(const FVector<T, N> &lhs, const T &x) {
199 FVector<T, N> result(lhs);
200 return result /= x;
201}
202
203// NOTE: this function assumes that multiplication for T is commutative.
204template<class T, int N>
205FVector<T, N> operator*(const T &x, const FVector<T, N> &lhs) {
206 FVector<T, N> result(lhs);
207 std::transform(lhs.begin(), lhs.end(), result.begin(),
208 std::bind(std::multiplies<T>(), std::placeholders::_1, x));
209 return result;
210}
211
212template<class T, int N>
214 return std::sqrt(std::inner_product(V.begin(), V.end(), V.begin(), T(0)));
215}
216
217template<class T, int N>
219 T sum(0);
220
221 for(int i = 0; i < V.size(); ++i) {
222 double dv = D[i] * V[i];
223 sum += dv * dv;
224 }
225
226 return std::sqrt(sum);
227}
228
229#endif // CLASSIC_FVector_HH
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
FVector< T, N > operator-(const FVector< T, N > &, const FVector< T, N > &)
Subtract.
Definition FVector.h:177
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:218
T euclidean_norm(const FVector< T, N > &)
Euclidean norm.
Definition FVector.h:213
FVector< T, N > operator/(const FVector< T, N > &, const T &)
Divide.
Definition FVector.h:198
FVector< T, N > operator+(const FVector< T, N > &, const FVector< T, N > &)
Add.
Definition FVector.h:169
T operator*(const FVector< T, N > &, const FVector< T, N > &)
Dot product.
Definition FVector.h:187
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:139
FVector(const T *t)
Constructor.
Definition FVector.h:122
FVector(const T &t)
Constructor.
Definition FVector.h:117
FVector(const FArray1D< T, N > &)
Conversion from one-dimensional array.
Definition FVector.h:127
FVector()
Constructor.
Definition FVector.h:112
FVector & operator+=(const FVector &)
Add FVector and assign.
Definition FVector.h:153
FVector & operator-=(const FVector &)
Subtract FVector and assign.
Definition FVector.h:160
FVector & operator/=(const T &)
Divide by scalar and assign.
Definition FVector.h:146
FVector operator-() const
Change sign.
Definition FVector.h:132