OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
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 
23 #include "FixedAlgebra/FArray1D.h"
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 
37 template<class T, int N>
38 class FVector: public FArray1D<T, N> {
39 
40 public:
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 
58  FVector operator-() const;
59 
61  FVector &operator*=(const T &);
62 
64  FVector &operator/=(const T &);
65 
68 
71 };
72 
73 
74 // ------------------------------------------------------------------------
75 
77 template<class T, int N>
79 
81 template<class T, int N>
83 
85 template<class T, int N>
86 T operator*(const FVector<T, N> &, const FVector<T, N> &);
87 
89 template<class T, int N>
90 FVector<T, N> operator*(const FVector<T, N> &, const T &);
91 
93 template<class T, int N>
94 FVector<T, N> operator/(const FVector<T, N> &, const T &);
95 
97 template<class T, int N>
98 FVector<T, N> operator*(const T &, const FVector<T, N> &);
99 
101 template<class T, int N>
103 
105 template<class T, int N>
106 T scaled_norm(const FArray1D<T, N> D, const FVector<T, N> &V);
107 
108 
109 // Implementation of template class FVector<T,N>.
110 // ------------------------------------------------------------------------
111 
112 template<class T, int N>
114  FArray1D<T, N>()
115 {}
116 
117 
118 template<class T, int N>
120  FArray1D<T, N>(val)
121 {}
122 
123 
124 template<class T, int N>
126  FArray1D<T, N>(rhs)
127 {}
128 
129 
130 template<class T, int N>
132  FArray1D<T, N>(rhs)
133 {}
134 
135 
136 template<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 
144 template<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 
152 template<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 
160 template<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 
168 template<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 
179 template<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 
188 template<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.
199 template<class T, int N>
200 T 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 
205 template<class T, int N>
206 FVector<T, N> operator*(const FVector<T, N> &lhs, const T &x) {
207  FVector<T, N> result(lhs);
208  return result *= x;
209 }
210 
211 
212 template<class T, int N>
213 FVector<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.
220 template<class T, int N>
221 FVector<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 
229 template<class T, int N>
231  return sqrt(std::inner_product(V.begin(), V.end(), V.begin(), T(0)));
232 }
233 
234 
235 template<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
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
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: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
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