NumericalVector.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 17th of September 2015. It has been
28 //integrated into MercuryDPM at 17th of September 2015.
29 
30 
31 #ifndef MERCURYDPM_NUMERICALVECTOR_H_
32 #define MERCURYDPM_NUMERICALVECTOR_H_
33 
34 #include "Logger.h"
35 #include <vector>
36 
42 template<typename T = Mdouble>
44 {
45 public:
46 
48  : data_()
49  {
50  }
51 
52  explicit NumericalVector(std::size_t m)
53  : data_(m)
54  {
55  }
56 
57  NumericalVector(std::initializer_list<T> l)
58  : data_(l)
59  {
60  }
61 
63  : data_(other.data_)
64  {
65  }
66 
68  : data_(std::move(other.data_))
69  {
70  }
71 
72  NumericalVector(const T array[], std::size_t size)
73  : data_(array, array + size)
74  {
75  }
76 
77  void resize(std::size_t size)
78  {
79  if (size != data_.size())
80  {
81  data_.resize(size);
82  }
83  }
84 
86  {
87  data_ = right.data_;
88  return *this;
89  }
90 
91  NumericalVector<T>& operator=(const std::initializer_list<T> l)
92  {
93  data_ = l;
94  return *this;
95  }
96 
98  {
99  NumericalVector<T> result(*this);
100  logger.assert_debug(data_.size() == right.data_.size(), "Vectors don't have the same size");
101  for (std::size_t i = 0; i < data_.size(); i++)
102  result.data_[i] += right.data_[i];
103  return result;
104  }
105 
107  {
108  NumericalVector<T> result(*this);
109  logger.assert_debug(data_.size() == right.data_.size(), "Vectors don't have the same size");
110  for (std::size_t i = 0; i < data_.size(); i++)
111  result.data_[i] -= right.data_[i];
112  return result;
113  }
114 
115  NumericalVector<T> operator*(const T& right) const
116  {
117  NumericalVector<T> result(*this);
118  for (T& d : result.data_)
119  d *= right;
120  return result;
121  }
122 
123 
124  T operator*(const NumericalVector& right) const
125  {
126  logger.assert_debug(data_.size() == right.data_.size(), "Vectors don't have equal length.");
127  T sum = 0;
128  for (std::size_t i = 0; i < data_.size(); i++)
129  sum += data_[i] * right.data_[i];
130  return sum;
131  }
132 
134  {
135  for (T& d : data_)
136  d /= right;
137 
138  return *this;
139  }
140 
141  NumericalVector<T> operator/(const T& right) const
142  {
143  NumericalVector<T> result(*this);
144  return (result /= right);
145 
146  }
147 
149  {
150  logger.assert_debug(data_.size() == right.data_.size(), "Vectors don't have the same size");
151  for (std::size_t i = 0; i < data_.size(); i++)
152  data_[i] += right.data_[i];
153  return *this;
154  }
155 
156 
158  {
159  logger.assert_debug(data_.size() == right.data_.size(), "Vectors don't have the same size");
160  for (std::size_t i = 0; i < data_.size(); i++)
161  data_[i] -= right.data_[i];
162  return *this;
163  }
164 
166  {
167  for (T& d : data_)
168  d *= right;
169  return *this;
170  }
171 
172  T& operator[](std::size_t n)
173  {
174  logger.assert_debug(n < data_.size(), "Requested entry %, but there are only % entries", n, data_.size());
175  return data_[n];
176  }
177 
178  const T& operator[](std::size_t n) const
179  {
180  logger.assert_debug(n < data_.size(), "Requested entry %, but there are only % entries", n, data_.size());
181  return data_[n];
182  }
183 
184  T& operator()(std::size_t n)
185  {
186  logger.assert_debug(n < data_.size(), "Requested entry %, but there are only % entries", n, data_.size());
187  return data_[n];
188  }
189 
190  const T& operator()(std::size_t n) const
191  {
192  logger.assert_debug(n < data_.size(), "Requested entry %, but there are only % entries", n, data_.size());
193  return data_[n];
194  }
195 
196  std::size_t size() const
197  {
198  return data_.size();
199  }
200 
201  const T* data() const
202  {
203  return data_.data();
204  }
205 
206  T* data()
207  {
208  return data_.data();
209  }
210 
211 private:
212  std::vector<T> data_;
213 
214 
215 };
216 
217 template<typename T = Mdouble>
219 
220 template<typename T = Mdouble>
222 
223 template<typename T = Mdouble>
224 std::ostream& operator<<(std::ostream& os, const NumericalVector<T>& A);
225 
226 
227 #endif //MERCURYDPM_NUMERICALVECTOR_H_
228 
229 
int i
Definition: BiCGSTAB_step_by_step.cpp:9
const unsigned n
Definition: CG3DPackingUnitTest.cpp:11
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
NumericalVector< T > operator-(const NumericalVector< T > &right)
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
This is a vector of doubles.
Definition: NumericalVector.h:44
NumericalVector()
Definition: NumericalVector.h:47
NumericalVector< T > & operator/=(const T &right)
Definition: NumericalVector.h:133
NumericalVector< T > & operator=(const NumericalVector< T > &right)
Definition: NumericalVector.h:85
NumericalVector< T > & operator=(const std::initializer_list< T > l)
Definition: NumericalVector.h:91
NumericalVector(std::size_t m)
Definition: NumericalVector.h:52
void resize(std::size_t size)
Definition: NumericalVector.h:77
const T & operator()(std::size_t n) const
Definition: NumericalVector.h:190
T & operator()(std::size_t n)
Definition: NumericalVector.h:184
T operator*(const NumericalVector &right) const
Definition: NumericalVector.h:124
NumericalVector< T > & operator-=(const NumericalVector< T > &right)
Definition: NumericalVector.h:157
NumericalVector< T > operator-(const NumericalVector< T > &right) const
Definition: NumericalVector.h:106
NumericalVector(const NumericalVector &other)
Definition: NumericalVector.h:62
std::size_t size() const
Definition: NumericalVector.h:196
T * data()
Definition: NumericalVector.h:206
NumericalVector< T > & operator+=(const NumericalVector< T > &right)
Definition: NumericalVector.h:148
NumericalVector(NumericalVector &&other)
Definition: NumericalVector.h:67
NumericalVector< T > operator/(const T &right) const
Definition: NumericalVector.h:141
const T * data() const
Definition: NumericalVector.h:201
NumericalVector(const T array[], std::size_t size)
Definition: NumericalVector.h:72
std::vector< T > data_
Definition: NumericalVector.h:212
NumericalVector< T > operator+(const NumericalVector< T > &right) const
Definition: NumericalVector.h:97
T & operator[](std::size_t n)
Definition: NumericalVector.h:172
NumericalVector< T > operator*(const T &right) const
Definition: NumericalVector.h:115
const T & operator[](std::size_t n) const
Definition: NumericalVector.h:178
NumericalVector< T > & operator*=(const T &right)
Definition: NumericalVector.h:165
NumericalVector(std::initializer_list< T > l)
Definition: NumericalVector.h:57
int * m
Definition: level2_cplx_impl.h:294
std::array< T, N > array
Definition: EmulateArray.h:231