SmallVector.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 /*
6  This file forms part of hpGEM. This package has been developed over a number of years by various people at the University of Twente and a full list of contributors can be found at
7  http://hpgem.org/about-the-code/team
8 
9  This code is distributed using BSD 3-Clause License. A copy of which can found below.
10 
11 
12  Copyright (c) 2014, University of Twente
13  All rights reserved.
14 
15  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
16 
17  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
18 
19  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
20 
21  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
22 
23  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 
27 //Note: This code is copied and adapted from hpGEM (see license above), version 22th of January 2016. It has been
28 //integrated into MercuryDPM at 16th of March 2017.
29 
30 #ifndef MERCURYDPM_SMALLVECTOR_H
31 #define MERCURYDPM_SMALLVECTOR_H
32 
33 #include "GeneralDefine.h"
34 #include "Logger.h"
35 #include <cmath>
36 #include <algorithm>
37 #include <numeric>
38 #include <array>
39 
40 template<unsigned int numberOfRows>
42 {
43 
44 public:
45 
47  : data_()
48  {
49  }
50 
51  SmallVector(const SmallVector& other)
52  : data_(other.data_)
53  {
54  }
55 
57  : data_(std::move(other.data_))
58  {
59  }
60 
62  : data_()
63  {
64  std::copy(array, array + numberOfRows, data_.begin());
65  }
66 
67  SmallVector(std::initializer_list<Mdouble> data)
68  : data_()
69  {
70  logger.assert_debug(data.size() == numberOfRows, "provided array has size %, but should have size %",
71  data.size(), numberOfRows);
72  std::copy(data.begin(), data.end(), data_.begin());
73  }
74 
76  {
77  std::copy(right.data_.begin(), right.data_.end(), data_.begin());
78  return *this;
79  }
80 
81  SmallVector& operator=(const std::array<Mdouble, numberOfRows> l)
82  {
83  std::copy(l.begin(), l.end(), data_.begin());
84  return *this;
85  }
86 
87  SmallVector operator+(const SmallVector& right) const
88  {
89  SmallVector result;
90  std::transform(data_.begin(), data_.end(), right.data_.begin(), result.data_.begin(), std::plus<Mdouble>());
91  return result;
92  }
93 
94  SmallVector operator-(const SmallVector& right) const
95  {
96  SmallVector result;
97  std::transform(data_.begin(), data_.end(), right.data_.begin(), result.data_.begin(), std::minus<Mdouble>());
98  return result;
99  }
100 
101  SmallVector operator*(const Mdouble& right) const
102  {
103  SmallVector result;
104  std::transform(data_.begin(), data_.end(), result.data_.begin(), std::bind(std::multiplies<Mdouble>(),
105  std::placeholders::_1, right));
106  return result;
107  }
108 
110  Mdouble operator*(const SmallVector& right) const
111  {
112  return std::inner_product(right.data_.begin(), right.data_.end(), data_.begin(), 0.0);
113  }
114 
116  {
117  std::transform(data_.begin(), data_.end(), data_.begin(), std::bind(std::divides<Mdouble>(),
118  std::placeholders::_1, right));
119  return *this;
120  }
121 
122  SmallVector operator/(const Mdouble& right) const
123  {
124  SmallVector result;
125  std::transform(data_.begin(), data_.end(), result.data_.begin(), std::bind(std::divides<Mdouble>(),
126  std::placeholders::_1, right));
127  return result;
128  }
129 
130  void axpy(Mdouble a, const SmallVector& x)
131  {
132  for (unsigned int i = 0; i < numberOfRows; ++i)
133  {
134  data_[i] += a * x[i];
135  }
136  }
137 
140  bool operator==(const SmallVector& right) const
141  {
142  for (unsigned int i = 0; i < numberOfRows; ++i)
143  {
144  if (data_[i] != right[i])
145  {
146  return false;
147  }
148  }
149  return true;
150  }
151 
154  bool operator<(const SmallVector& right) const
155  {
156  for (unsigned int i = 0; i < numberOfRows; ++i)
157  {
158  if (data_[i] < right[i])
159  {
160  return true;
161  }
162  if (data_[i] > right[i])
163  {
164  return false;
165  }
166  }
167  return false;
168  }
169 
171  {
172  std::transform(data_.begin(), data_.end(), right.data_.begin(), data_.begin(), std::plus<Mdouble>());
173  return *this;
174  }
175 
177  {
178  std::transform(data_.begin(), data_.end(), right.data_.begin(), data_.begin(), std::minus<Mdouble>());
179  return *this;
180  }
181 
182  SmallVector& operator*=(const double& right)
183  {
184  std::transform(data_.begin(), data_.end(), data_.begin(), std::bind(std::multiplies<Mdouble>(),
185  std::placeholders::_1, right));
186  return *this;
187  }
188 
189  Mdouble& operator[](unsigned int n)
190  {
191  logger.assert_debug(n < numberOfRows, "Requested entry %, but there are only % entries", n, numberOfRows);
192  return data_[n];
193  }
194 
195  const Mdouble& operator[](unsigned int n) const
196  {
197  logger.assert_debug(n < numberOfRows, "Requested entry %, but there are only % entries", n, numberOfRows);
198  return data_[n];
199  }
200 
201  Mdouble& operator()(unsigned int n)
202  {
203  logger.assert_debug(n < numberOfRows, "Requested entry %, but there are only % entries", n, numberOfRows);
204  return data_[n];
205  }
206 
207  const Mdouble& operator()(unsigned int n) const
208  {
209  logger.assert_debug(n < numberOfRows, "Requested entry %, but there are only % entries", n, numberOfRows);
210  return data_[n];
211  }
212 
213  unsigned int size() const
214  {
215  return numberOfRows;
216  }
217 
218  const Mdouble* data() const
219  {
220  return data_.data();
221  }
222 
224  {
225  return data_.data();
226  }
227 
229  {
230  return *this * -1.0;
231  }
232 
233  Mdouble length() const
234  {
235  Mdouble sum = 0;
236  for (Mdouble x : data_)
237  {
238  sum += x * x;
239  logger(DEBUG, "x: %, sum: %", x, sum);
240  }
241  return std::sqrt(sum);
242  }
243 
245  {
246  return (*this) / length();
247  }
248 
249 private:
250  std::array<Mdouble, numberOfRows> data_;
251 
252 };
253 
254 template<unsigned int numberOfRows>
256 {
257  return right * left;
258 }
259 
260 template<unsigned int numberOfRows>
261 std::ostream& operator<<(std::ostream& os, const SmallVector<numberOfRows>& A)
262 {
263  os << "(";
264  for (std::size_t i = 0; i < numberOfRows; ++i)
265  {
266  os << A[i] << " ";
267  }
268  os << ")";
269  return os;
270 }
271 
272 #endif //MERCURYDPM_SMALLVECTOR_H
AnnoyingScalar sqrt(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:134
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
double Mdouble
Definition: GeneralDefine.h:13
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
@ DEBUG
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:47
std::ostream & operator<<(std::ostream &s, const DenseBase< Derived > &m)
Definition: IO.h:222
operator*(const MatrixBase< Derived > &matrix, const UniformScaling< Scalar > &s)
Definition: Eigen/src/Geometry/Scaling.h:133
Definition: SmallVector.h:42
Mdouble operator*(const SmallVector &right) const
Computes inner product between two vectors.
Definition: SmallVector.h:110
SmallVector< numberOfRows > getNormalised() const
Definition: SmallVector.h:244
Mdouble length() const
Definition: SmallVector.h:233
const Mdouble & operator()(unsigned int n) const
Definition: SmallVector.h:207
bool operator<(const SmallVector &right) const
Definition: SmallVector.h:154
SmallVector & operator*=(const double &right)
Definition: SmallVector.h:182
unsigned int size() const
Definition: SmallVector.h:213
SmallVector(const Mdouble array[])
Definition: SmallVector.h:61
SmallVector operator*(const Mdouble &right) const
Definition: SmallVector.h:101
SmallVector operator/(const Mdouble &right) const
Definition: SmallVector.h:122
SmallVector & operator=(const std::array< Mdouble, numberOfRows > l)
Definition: SmallVector.h:81
Mdouble * data()
Definition: SmallVector.h:223
Mdouble & operator[](unsigned int n)
Definition: SmallVector.h:189
SmallVector(const SmallVector &other)
Definition: SmallVector.h:51
const Mdouble & operator[](unsigned int n) const
Definition: SmallVector.h:195
SmallVector(std::initializer_list< Mdouble > data)
Definition: SmallVector.h:67
SmallVector & operator=(const SmallVector &right)
Definition: SmallVector.h:75
std::array< Mdouble, numberOfRows > data_
Definition: SmallVector.h:250
SmallVector operator-() const
Definition: SmallVector.h:228
void axpy(Mdouble a, const SmallVector &x)
Definition: SmallVector.h:130
SmallVector(SmallVector &&other)
Definition: SmallVector.h:56
SmallVector & operator+=(const SmallVector &right)
Definition: SmallVector.h:170
Mdouble & operator()(unsigned int n)
Definition: SmallVector.h:201
SmallVector operator-(const SmallVector &right) const
Definition: SmallVector.h:94
const Mdouble * data() const
Definition: SmallVector.h:218
SmallVector & operator-=(const SmallVector &right)
Definition: SmallVector.h:176
SmallVector()
Definition: SmallVector.h:46
bool operator==(const SmallVector &right) const
Definition: SmallVector.h:140
SmallVector operator+(const SmallVector &right) const
Definition: SmallVector.h:87
SmallVector & operator/=(const Mdouble &right)
Definition: SmallVector.h:115
EIGEN_DONT_INLINE void transform(const Transformation &t, Data &data)
Definition: geometry.cpp:25
EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:32
const Scalar * a
Definition: level2_cplx_impl.h:32
std::array< T, N > array
Definition: EmulateArray.h:231
list x
Definition: plotDoE.py:28