OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
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 
36 template<class T>
37 class Vector: public Array1D<T> {
38 
39 public:
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();
58  Vector<T> &operator=(const Vector<T> &);
59 
61  Vector<T> operator-() const;
62 
64  Vector<T> &operator*=(const T &);
65 
67  Vector<T> &operator/=(const T &);
68 
70  Vector<T> &operator+=(const Vector<T> &);
71 
73  Vector<T> &operator-=(const Vector<T> &);
74 };
75 
76 
77 // Global Operators and Functions on Vector<T>
78 // ------------------------------------------------------------------------
79 
81 template<class T> Vector<T> operator+(const Vector<T> &, const Vector<T> &);
82 
84 template<class T> Vector<T> operator-(const Vector<T> &, const Vector<T> &);
85 
87 template<class T> T operator*(const Vector<T> &, const Vector<T> &);
88 
90 template<class T> Vector<T> operator*(const Vector<T> &, const T &);
91 
93 template<class T> Vector<T> operator/(const Vector<T> &, const T &);
94 
96 template<class T> Vector<T> operator*(const T &, const Vector<T> &);
97 
99 template<class T> T euclidean_norm(const Vector<T> &);
100 
102 template<class T> T scaled_norm(const Array1D<T> D, const Vector<T> &V);
103 
104 
105 // Implementation of template class Vector<T>.
106 // ------------------------------------------------------------------------
107 
108 template<class T>
110  Array1D<T>()
111 {}
112 
113 
114 template<class T>
116  Array1D<T>(n)
117 {}
118 
119 
120 template<class T>
121 Vector<T>::Vector(int n, const T &val):
122  Array1D<T>(n, val)
123 {}
124 
125 
126 template<class T>
128  Array1D<T>(array)
129 {}
130 
131 
132 template<class T>
134  Array1D<T>(array)
135 {}
136 
137 
138 template<class T>
140 {}
141 
142 
143 template<class T>
145  Array1D<T>::operator=(right);
146  return *this;
147 }
148 
149 
150 template<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 
158 template<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 
166 template<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 
174 template<class T>
176  std::transform(this->begin(), this->end(), array.begin(), this->begin(),
177  std::plus<T>());
178  return *this;
179 }
180 
181 
182 template<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 
193 template<class T>
194 Vector<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 
202 template<class T>
203 Vector<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.
213 template<class T>
214 T 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 
219 template<class T>
220 Vector<T> operator*(const Vector<T> &V1, const T &x) {
221  Vector<T> result = V1;
222  return result *= x;
223 }
224 
225 
226 template<class T>
227 Vector<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.
234 template<class T>
235 Vector<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 
243 template<class T> T euclidean_norm(const Vector<T> &V) {
244  return sqrt(std::inner_product(V.begin(), V.end(), V.begin(), T(0)));
245 }
246 
247 
248 template<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
Matrix< T > operator+(const Matrix< T > &, const Matrix< T > &)
Matrix addition.
Definition: Matrix.h:275
Matrix< T > operator/(const Matrix< T > &, const T &)
Matrix divided by scalar.
Definition: Matrix.h:329
Vector< T > & operator+=(const Vector< T > &)
Add vector and assign.
Definition: Vector.h:175
Definition: rbendmap.h:8
Size error exception.
Definition: SizeError.h:33
Array1D< T > & operator=(const Array1D< T > &)
Definition: Array1D.h:159
iterator end()
Get end of data.
Definition: Array1D.h:210
Matrix< T > operator*(const Matrix< T > &, const Matrix< T > &)
Matrix multiply.
Definition: Matrix.h:297
T::PETE_Expr_t::PETE_Return_t sum(const PETE_Expr< T > &expr)
Definition: PETE.h:1213
Vector< T > & operator=(const Vector< T > &)
Definition: Vector.h:144
int size() const
Get array size.
Definition: Array1D.h:228
iterator begin()
Get beginning of data.
Definition: Array1D.h:204
Vector< T > & operator/=(const T &)
Divide by scalar and assign.
Definition: Vector.h:167
Tps< T > sqrt(const Tps< T > &x)
Square root.
Definition: TpsMath.h:91
Vector< T > & operator-=(const Vector< T > &)
Subtract vector and assign.
Definition: Vector.h:183
T euclidean_norm(const Vector< T > &)
Euclidean norm.
Definition: Vector.h:243
~Vector()
Definition: Vector.h:139
Vector()
Default constructor.
Definition: Vector.h:109
T scaled_norm(const Array1D< T > D, const Vector< T > &V)
Euclidean norm of diagonal matrix D times vector V.
Definition: Vector.h:248
One-dimensional array.
Definition: Array1D.h:36
Vector< T > operator-() const
Change sign of vector.
Definition: Vector.h:151
Matrix< T > operator-(const Matrix< T > &, const Matrix< T > &)
Matrix subtraction.
Definition: Matrix.h:282
Vector< T > & operator*=(const T &)
Multiply by scalar and assign.
Definition: Vector.h:159
Vector.