Thrill  0.1
vector.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/vector.hpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
9  ******************************************************************************/
10 
11 #pragma once
12 #ifndef THRILL_COMMON_VECTOR_HEADER
13 #define THRILL_COMMON_VECTOR_HEADER
14 
15 #include <tlx/die.hpp>
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <cmath>
20 #include <functional>
21 #include <vector>
22 
23 namespace thrill {
24 namespace common {
25 
26 //! A compile-time fixed-length D-dimensional point with double precision.
27 template <size_t D, typename Type = double>
28 class Vector
29 {
30 public:
31  //! coordinates array
32  Type x[D];
33 
34  using type = Type;
35 
36  static size_t dim() { return D; }
37  static size_t size() { return D; }
38 
39  static Vector Make(size_t D_) {
40  die_unless(D_ == D);
41  return Vector();
42  }
43  static Vector Origin() {
44  Vector p;
45  std::fill(p.x, p.x + D, 0.0);
46  return p;
47  }
48  Vector fill(const Type init_val) {
49  std::fill(x, x + D, init_val);
50  return *this;
51  }
52  template <typename Distribution, typename Generator>
53  static Vector Random(size_t dim, Distribution& dist, Generator& gen) {
54  die_unless(dim == D);
55  Vector p;
56  for (size_t i = 0; i < D; ++i) p.x[i] = dist(gen);
57  return p;
58  }
59  Type Norm() const {
60  Type sum = 0.0;
61  for (size_t i = 0; i < D; ++i) sum += x[i] * x[i];
62  return std::sqrt(sum);
63  }
64  Type DistanceSquare(const Vector& b) const {
65  Type sum = 0.0;
66  for (size_t i = 0; i < D; ++i) sum += (x[i] - b.x[i]) * (x[i] - b.x[i]);
67  return sum;
68  }
69  Type Distance(const Vector& b) const {
70  return std::sqrt(DistanceSquare(b));
71  }
72  Vector operator + (const Vector& b) const {
73  Vector p;
74  for (size_t i = 0; i < D; ++i) p.x[i] = x[i] + b.x[i];
75  return p;
76  }
77  Vector& operator += (const Vector& b) {
78  for (size_t i = 0; i < D; ++i) x[i] += b.x[i];
79  return *this;
80  }
81  Vector operator - (const Vector& b) const {
82  Vector p;
83  for (size_t i = 0; i < D; ++i) p.x[i] = x[i] - b.x[i];
84  return p;
85  }
86  Vector operator / (const Type& s) const {
87  Vector p;
88  for (size_t i = 0; i < D; ++i) p.x[i] = x[i] / s;
89  return p;
90  }
91  Vector& operator /= (const Type& s) {
92  for (size_t i = 0; i < D; ++i) x[i] /= s;
93  return *this;
94  }
95  friend std::ostream& operator << (std::ostream& os, const Vector& a) {
96  os << '(' << a.x[0];
97  for (size_t i = 1; i != D; ++i) os << ',' << a.x[i];
98  return os << ')';
99  }
100  Type dot(const Vector& b) const {
101  Type r = 0;
102  for (size_t i = 0; i < D; ++i) r += x[i] * b.x[i];
103  return r;
104  }
105 
106  //! serialization method for cereal. Keep this, even though Vector is a POD,
107  //! to serialize fixed-length Vectors that are contained in other structs.
108  template <typename Archive>
109  void serialize(Archive& archive) {
110  archive(x);
111  }
112 };
113 
114 template <size_t D>
115 Vector<D> operator * (const double a, const Vector<D>& b) {
116  Vector<D> p;
117  for (size_t i = 0; i < D; ++i) p.x[i] = a * b.x[i];
118  return p;
119 }
120 
121 //! A variable-length D-dimensional point with double precision
122 template <typename Type = double>
123 class VVector
124 {
125 public:
126  using TypeVector = std::vector<Type>;
127 
128  using type = Type;
129 
130  //! coordinates array
132 
133  explicit VVector(size_t D = 0) : x(D) { }
134  explicit VVector(TypeVector&& v) : x(std::move(v)) { }
135 
136  size_t dim() const { return x.size(); }
137  size_t size() const { return x.size(); }
138 
139  static VVector Make(size_t D) {
140  return VVector(D);
141  }
142  VVector fill(const Type init_val) {
143  std::fill(x.begin(), x.end(), init_val);
144  return *this;
145  }
146  template <typename Distribution, typename Generator>
147  static VVector Random(size_t D, Distribution& dist, Generator& gen) {
148  VVector p(D);
149  for (size_t i = 0; i < D; ++i) p.x[i] = dist(gen);
150  return p;
151  }
152  Type DistanceSquare(const VVector& b) const {
153  assert(x.size() == b.x.size());
154  Type sum = 0.0;
155  for (size_t i = 0; i < x.size(); ++i)
156  sum += (x[i] - b.x[i]) * (x[i] - b.x[i]);
157  return sum;
158  }
159  Type Distance(const VVector& b) const {
160  return std::sqrt(DistanceSquare(b));
161  }
162  Type Norm() const {
163  Type sum = 0.0;
164  for (size_t i = 0; i < x.size(); ++i) sum += x[i] * x[i];
165  return std::sqrt(sum);
166  }
167  VVector operator + (const VVector& b) const {
168  assert(x.size() == b.x.size());
169  VVector p(x.size());
170  for (size_t i = 0; i < x.size(); ++i) p.x[i] = x[i] + b.x[i];
171  return p;
172  }
174  assert(x.size() == b.x.size());
175  for (size_t i = 0; i < x.size(); ++i) x[i] += b.x[i];
176  return *this;
177  }
178  VVector operator - (const VVector& b) const {
179  assert(x.size() == b.x.size());
180  VVector p(x.size());
181  for (size_t i = 0; i < x.size(); ++i) p.x[i] = x[i] - b.x[i];
182  return p;
183  }
184  VVector operator / (const Type& s) const {
185  VVector p(x.size());
186  for (size_t i = 0; i < x.size(); ++i) p.x[i] = x[i] / s;
187  return p;
188  }
189  VVector& operator /= (const Type& s) {
190  for (size_t i = 0; i < x.size(); ++i) x[i] /= s;
191  return *this;
192  }
193  Type dot(const VVector& b) const {
194  Type r = 0;
195  for (size_t i = 0; i < x.size(); ++i) r += x[i] * b.x[i];
196  return r;
197  }
198  friend std::ostream& operator << (std::ostream& os, const VVector& a) {
199  os << '(' << a.x[0];
200  for (size_t i = 1; i != a.x.size(); ++i) os << ',' << a.x[i];
201  return os << ')';
202  }
203 
204  //! serialization method for cereal.
205  template <typename Archive>
206  void serialize(Archive& archive) {
207  archive(x);
208  }
209 };
210 
211 VVector<double> operator * (const double a, const VVector<double>& b) {
212  VVector<double> p(b.x.size());
213  for (size_t i = 0; i < b.x.size(); ++i) p.x[i] = a * b.x[i];
214  return p;
215 }
216 
217 } // namespace common
218 } // namespace thrill
219 
220 #endif // !THRILL_COMMON_VECTOR_HEADER
221 
222 /******************************************************************************/
static Vector Make(size_t D_)
Definition: vector.hpp:39
VVector(size_t D=0)
Definition: vector.hpp:133
#define die_unless(X)
Definition: die.hpp:27
size_t dim() const
Definition: vector.hpp:136
Vector operator-(const Vector &b) const
Definition: vector.hpp:81
A variable-length D-dimensional point with double precision.
Definition: vector.hpp:123
void serialize(Archive &archive)
Definition: vector.hpp:109
static VVector Random(size_t D, Distribution &dist, Generator &gen)
Definition: vector.hpp:147
static size_t size()
Definition: vector.hpp:37
Type
VFS object type.
Definition: file_io.hpp:52
VVector(TypeVector &&v)
Definition: vector.hpp:134
Vector operator/(const Type &s) const
Definition: vector.hpp:86
STL namespace.
void serialize(Archive &archive)
serialization method for cereal.
Definition: vector.hpp:206
size_t size() const
Definition: vector.hpp:137
Type dot(const VVector &b) const
Definition: vector.hpp:193
TypeVector x
coordinates array
Definition: vector.hpp:131
Vector & operator+=(const Vector &b)
Definition: vector.hpp:77
friend std::ostream & operator<<(std::ostream &os, const Vector &a)
Definition: vector.hpp:95
static Vector Random(size_t dim, Distribution &dist, Generator &gen)
Definition: vector.hpp:53
Type Norm() const
Definition: vector.hpp:162
Type DistanceSquare(const Vector &b) const
Definition: vector.hpp:64
Vector< D > operator*(const double a, const Vector< D > &b)
Definition: vector.hpp:115
Type dot(const Vector &b) const
Definition: vector.hpp:100
Vector fill(const Type init_val)
Definition: vector.hpp:48
thrill::common::Vector< D, double > Vector
static VVector Make(size_t D)
Definition: vector.hpp:139
std::vector< Type > TypeVector
Definition: vector.hpp:126
Vector & operator/=(const Type &s)
Definition: vector.hpp:91
Type Distance(const Vector &b) const
Definition: vector.hpp:69
Vector operator+(const Vector &b) const
Definition: vector.hpp:72
int D
Definition: gen_data.py:14
Type Norm() const
Definition: vector.hpp:59
VVector fill(const Type init_val)
Definition: vector.hpp:142
Type DistanceSquare(const VVector &b) const
Definition: vector.hpp:152
Type x[D]
coordinates array
Definition: vector.hpp:32
Type Distance(const VVector &b) const
Definition: vector.hpp:159
static Vector Origin()
Definition: vector.hpp:43
A compile-time fixed-length D-dimensional point with double precision.
Definition: vector.hpp:28
static size_t dim()
Definition: vector.hpp:36