Vector.h
Go to the documentation of this file.
1 /*
2  * Vector.h
3  *
4  * Created on: 12.03.2014
5  * Author: Michael Wegner (michael.wegner@student.kit.edu)
6  */
7
8 #ifndef VECTOR_H_
9 #define VECTOR_H_
10
11 #include <vector>
12 #include "../Globals.h"
13 #include "AlgebraicGlobals.h"
14 #include <cassert>
15
16 namespace NetworKit {
17
18 // forward declaration of DynamicMatrix class
19 class DynamicMatrix;
20
25 class Vector {
26 private:
27  std::vector<double> values;
28  bool transposed;
29
30 public:
32  Vector();
33
40  Vector(const count dimension, const double initialValue = 0, const bool transpose = false);
41
47  Vector(const std::vector<double> &values, const bool transpose = false);
48
53  Vector(const std::initializer_list<double> &list);
54
56  Vector(const Vector &other) = default;
57
59  Vector(Vector &&other) = default;
60
62  virtual ~Vector() = default;
63
65  Vector& operator=(const Vector &other) = default;
66
68  Vector& operator=(Vector &&other) = default;
69
70
74  inline count getDimension() const {
75  return values.size();
76  }
77
82  bool isTransposed() const;
83
87  Vector transpose() const;
88
89
94  double length() const;
95
96
101  double mean() const;
102
108  inline double& operator[](const index idx) {
109  assert(idx < values.size());
110  return values[idx];
111  }
112
118  inline const double& operator[](const index idx) const {
119  assert(idx < values.size());
120  return values[idx];
121  }
122
128  double &at(const index idx) {
129  if (idx >= values.size()) {
130  throw std::runtime_error("index out of range");
131  } else {
132  return values[idx];
133  }
134  }
135
140  bool operator==(const Vector &other) const;
141
146  bool operator!=(const Vector &other) const;
147
148
155  template<class Matrix = DynamicMatrix>
156  static Matrix outerProduct(const Vector& v1, const Vector& v2);
157
162  static double innerProduct(const Vector &v1, const Vector &v2);
163
168  double operator*(const Vector &other) const;
169
174  template<typename Matrix = DynamicMatrix>
175  Vector operator*(const Matrix& matrix) const;
176
181  Vector operator*(const double &scalar) const;
182
187  Vector& operator*=(const double &scalar);
188
189
194  Vector operator/(const double &divisor) const;
195
200  Vector& operator/=(const double &divisor);
201
207  Vector operator+(const Vector &other) const;
208
212  Vector operator+(const double value) const;
213
219  Vector& operator+=(const Vector &other);
220
224  Vector& operator+=(const double value);
225
232  Vector operator-(const Vector &other) const;
233
237  Vector operator-(const double value) const;
238
244  Vector& operator-=(const Vector &other);
245
249  Vector& operator-=(const double value);
250
256  template<typename F>
257  void apply(const F unaryElementFunction);
258
262  template<typename L> void forElements(L handle);
263
267  template<typename L> void forElements(L handle) const;
268
273  template<typename L> void parallelForElements(L handle);
274
278  template<typename L> void parallelForElements(L handle) const;
279
280 };
281
286 inline Vector operator*(const double &scalar, const Vector &v) {
287  return v.operator*(scalar);
288 }
289
290 template<class Matrix>
291 Matrix Vector::outerProduct(const Vector& v1, const Vector& v2) {
292  std::vector<Triplet> triplets;
293
294  for (index i = 0; i < v1.getDimension(); ++i) {
295  for (index j = 0; j < v2.getDimension(); ++j) {
296  double result = v1[i] * v2[j];
297  if (fabs(result) >= FLOAT_EPSILON) {
298  triplets.push_back({i,j,result});
299  }
300  }
301  }
302
303  return Matrix(v1.getDimension(), v2.getDimension(), triplets);
304 }
305
306 template<class Matrix>
307 Vector Vector::operator*(const Matrix& matrix) const {
308  assert(isTransposed()); // vector must be of the form 1xn
309  assert(getDimension() == matrix.numberOfRows()); // dimensions of vector and matrix must match
310
311  Vector result(matrix.numberOfColumns(), 0.0, true);
312 #pragma omp parallel for
313  for (count k = 0; k < matrix.numberOfColumns(); ++k) {
314  Vector column = matrix.column(k);
315  result[k] = (*this) * column;
316  }
317
318  return result;
319 }
320
321 template<typename F>
322 void Vector::apply(const F unaryElementFunction) {
323 #pragma omp parallel for
324  for (index i = 0; i < getDimension(); ++i) {
325  values[i] = unaryElementFunction(values[i]);
326  }
327 }
328
329 template<typename L>
330 inline void Vector::forElements(L handle) {
331  for (uint64_t i = 0; i < getDimension(); i++) {
332  handle(values[i]);
333  }
334 }
335
336 template<typename L>
337 inline void Vector::forElements(L handle) const {
338  for (uint64_t i = 0; i < getDimension(); i++) {
339  handle(values[i]);
340  }
341 }
342
343 template<typename L>
344 inline void Vector::parallelForElements(L handle) {
345 #pragma omp parallel for
346  for (uint64_t i = 0; i < getDimension(); i++) {
347  handle(i, values[i]);
348  }
349 }
350
351 template<typename L>
352 inline void Vector::parallelForElements(L handle) const {
353 #pragma omp parallel for
354  for (uint64_t i = 0; i < getDimension(); i++) {
355  handle(i, values[i]);
356  }
357 }
358
359
360 } /* namespace NetworKit */
361
362
363 #endif /* VECTOR_H_ */
Vector operator/(const double &divisor) const
Divides this vector by a divisor specified in divisor and returns the result in a new vector...
Definition: Vector.cpp:91
double & at(const index idx)
Returns a reference to the element at index idx.
Definition: Vector.h:128
Vector & operator*=(const double &scalar)
Multiplies this vector with a scalar specified in scalar.
Definition: Vector.cpp:82
Vector & operator+=(const Vector &other)
Definition: Vector.cpp:107
static double innerProduct(const Vector &v1, const Vector &v2)
Computes the inner product (dot product) of the vectors v1 and v2.
Definition: Vector.cpp:61
count getDimension() const
Definition: Vector.h:74
Vector & operator=(const Vector &other)=default
Default copy assignment operator.
uint64_t index
Typedefs.
Definition: Globals.h:20
void parallelForElements(L handle)
Iterate in parallel over all elements of the vector and call handler (lambda closure).
Definition: Vector.h:344
Vector()
Default constructor.
Definition: Vector.cpp:14
bool operator!=(const Vector &other) const
Compares this vector and other element-wise.
Definition: Vector.cpp:57
double operator*(const Vector &other) const
Computes the inner product (dot product) of this vector and other.
Definition: Vector.cpp:71
static Matrix outerProduct(const Vector &v1, const Vector &v2)
Computes the outer product of v1 and v2.
Definition: Vector.h:291
virtual ~Vector()=default
Default destructor.
Vector transpose() const
Definition: Vector.cpp:28
const double & operator[](const index idx) const
Returns a constant reference to the element at index idx without checking the range of this vector...
Definition: Vector.h:118
Vector operator+(const Vector &other) const
Adds this vector to other and returns the result.
Definition: Vector.cpp:99
Vector operator*(const double &scalar, const Vector &v)
Multiplies the vector v with a scalar specified in scalar and returns the result. ...
Definition: Vector.h:286
uint64_t count
Definition: Globals.h:21
std::vector< std::vector< count > > Matrix
Definition: DynamicNMIDistance.h:16
constexpr double FLOAT_EPSILON
Floating point epsilon to use in comparisons.
Definition: AlgebraicGlobals.h:23
bool operator==(const Vector &other) const
Compares this vector and other element-wise.
Definition: Vector.cpp:47
Vector & operator-=(const Vector &other)
Subtracts other from this vector.
Definition: Vector.cpp:136
double length() const
Calculates and returns the Euclidean length of this vector.
Definition: Vector.cpp:34
The Vector class represents a basic vector with double coefficients.
Definition: Vector.h:25
double mean() const
Calculates and returns the arithmetic mean of this vector.
Definition: Vector.cpp:38
void apply(const F unaryElementFunction)
Applies the unary function unaryElementFunction to each value in the Vector.
Definition: Vector.h:322
Vector & operator/=(const double &divisor)
Divides this vector by a divisor specified in divisor.
Definition: Vector.cpp:95
double & operator[](const index idx)
Returns a reference to the element at index idx without checking the range of this vector...
Definition: Vector.h:108
void forElements(L handle)
Iterate over all elements of the vector and call handler (lambda closure).
Definition: Vector.h:330
Vector operator-(const Vector &other) const
Subtracts other from this vector and returns the result.
Definition: Vector.cpp:128
bool isTransposed() const
A transposed vector is a row vector.
Definition: Vector.cpp:24