NumericalVector.h
Go to the documentation of this file.
1 /*
2  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
3  http://hpgem.org/about-the-code/team
4 
5  This code is distributed using BSD 3-Clause License. A copy of which can found below.
6 
7 
8  Copyright (c) 2014, University of Twente
9  All rights reserved.
10 
11  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
12 
13  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
14 
15  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.
16 
17  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.
18 
19  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.
20  */
21 
22 //Copyright (c) 2013-2023, The MercuryDPM Developers Team. All rights reserved.
23 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
24 //
25 //Redistribution and use in source and binary forms, with or without
26 //modification, are permitted provided that the following conditions are met:
27 // * Redistributions of source code must retain the above copyright
28 // notice, this list of conditions and the following disclaimer.
29 // * Redistributions in binary form must reproduce the above copyright
30 // notice, this list of conditions and the following disclaimer in the
31 // documentation and/or other materials provided with the distribution.
32 // * Neither the name MercuryDPM nor the
33 // names of its contributors may be used to endorse or promote products
34 // derived from this software without specific prior written permission.
35 //
36 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
37 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
40 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
41 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
45 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46 
47 //Note: This code is copied and adapted from hpGEM (see license above), version 17th of September 2015. It has been
48 //integrated into MercuryDPM at 17th of September 2015.
49 
50 
51 #ifndef MERCURYDPM_NUMERICALVECTOR_H_
52 #define MERCURYDPM_NUMERICALVECTOR_H_
53 
54 #include "Logger.h"
55 #include <vector>
56 
62 template<typename T = Mdouble>
64 {
65 public:
66 
68  : data_()
69  {
70  }
71 
72  explicit NumericalVector<T>(std::size_t m)
73  : data_(m)
74  {
75  }
76 
77  NumericalVector<T>(std::initializer_list<T> l)
78  : data_(l)
79  {
80  }
81 
83  : data_(other.data_)
84  {
85  }
86 
88  : data_(std::move(other.data_))
89  {
90  }
91 
92  NumericalVector<T>(const T array[], std::size_t size)
93  : data_(array, array + size)
94  {
95  }
96 
97  void resize(std::size_t size)
98  {
99  if (size != data_.size())
100  {
101  data_.resize(size);
102  }
103  }
104 
106  {
107  data_ = right.data_;
108  return *this;
109  }
110 
111  NumericalVector<T>& operator=(const std::initializer_list<T> l)
112  {
113  data_ = l;
114  return *this;
115  }
116 
118  {
119  NumericalVector<T> result(*this);
120  logger.assert_debug(data_.size() == right.data_.size(), "Vectors don't have the same size");
121  for (std::size_t i = 0; i < data_.size(); i++)
122  result.data_[i] += right.data_[i];
123  return result;
124  }
125 
127  {
128  NumericalVector<T> result(*this);
129  logger.assert_debug(data_.size() == right.data_.size(), "Vectors don't have the same size");
130  for (std::size_t i = 0; i < data_.size(); i++)
131  result.data_[i] -= right.data_[i];
132  return result;
133  }
134 
135  NumericalVector<T> operator*(const T& right) const
136  {
137  NumericalVector<T> result(*this);
138  for (T& d : result.data_)
139  d *= right;
140  return result;
141  }
142 
143 
144  T operator*(const NumericalVector& right) const
145  {
146  logger.assert_debug(data_.size() == right.data_.size(), "Vectors don't have equal length.");
147  T sum = 0;
148  for (std::size_t i = 0; i < data_.size(); i++)
149  sum += data_[i] * right.data_[i];
150  return sum;
151  }
152 
154  {
155  for (T& d : data_)
156  d /= right;
157 
158  return *this;
159  }
160 
161  NumericalVector<T> operator/(const T& right) const
162  {
163  NumericalVector<T> result(*this);
164  return (result /= right);
165 
166  }
167 
169  {
170  logger.assert_debug(data_.size() == right.data_.size(), "Vectors don't have the same size");
171  for (std::size_t i = 0; i < data_.size(); i++)
172  data_[i] += right.data_[i];
173  return *this;
174  }
175 
176 
178  {
179  logger.assert_debug(data_.size() == right.data_.size(), "Vectors don't have the same size");
180  for (std::size_t i = 0; i < data_.size(); i++)
181  data_[i] -= right.data_[i];
182  return *this;
183  }
184 
186  {
187  for (T& d : data_)
188  d *= right;
189  return *this;
190  }
191 
192  T& operator[](std::size_t n)
193  {
194  logger.assert_debug(n < data_.size(), "Requested entry %, but there are only % entries", n, data_.size());
195  return data_[n];
196  }
197 
198  const T& operator[](std::size_t n) const
199  {
200  logger.assert_debug(n < data_.size(), "Requested entry %, but there are only % entries", n, data_.size());
201  return data_[n];
202  }
203 
204  T& operator()(std::size_t n)
205  {
206  logger.assert_debug(n < data_.size(), "Requested entry %, but there are only % entries", n, data_.size());
207  return data_[n];
208  }
209 
210  const T& operator()(std::size_t n) const
211  {
212  logger.assert_debug(n < data_.size(), "Requested entry %, but there are only % entries", n, data_.size());
213  return data_[n];
214  }
215 
216  std::size_t size() const
217  {
218  return data_.size();
219  }
220 
221  const T* data() const
222  {
223  return data_.data();
224  }
225 
226  T* data()
227  {
228  return data_.data();
229  }
230 
231 private:
232  std::vector<T> data_;
233 
234 
235 };
236 
237 template<typename T = Mdouble>
238 NumericalVector<T> operator*(const T& left, const NumericalVector<T>& right);
239 
240 template<typename T = Mdouble>
242 
243 template<typename T = Mdouble>
244 std::ostream& operator<<(std::ostream& os, const NumericalVector<T>& A);
245 
246 
247 #endif //MERCURYDPM_NUMERICALVECTOR_H_
248 
249 
const unsigned n
Definition: CG3DPackingUnitTest.cpp:32
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)
NumericalVector< T > operator*(const T &left, const NumericalVector< T > &right)
std::ostream & operator<<(std::ostream &os, const NumericalVector< T > &A)
@ A
Definition: StatisticsVector.h:42
This is a vector of doubles.
Definition: NumericalVector.h:64
NumericalVector< T > & operator/=(const T &right)
Definition: NumericalVector.h:153
NumericalVector< T > & operator=(const NumericalVector< T > &right)
Definition: NumericalVector.h:105
NumericalVector< T > & operator=(const std::initializer_list< T > l)
Definition: NumericalVector.h:111
void resize(std::size_t size)
Definition: NumericalVector.h:97
const T & operator()(std::size_t n) const
Definition: NumericalVector.h:210
T & operator()(std::size_t n)
Definition: NumericalVector.h:204
T operator*(const NumericalVector &right) const
Definition: NumericalVector.h:144
NumericalVector< T > & operator-=(const NumericalVector< T > &right)
Definition: NumericalVector.h:177
NumericalVector< T > operator-(const NumericalVector< T > &right) const
Definition: NumericalVector.h:126
std::size_t size() const
Definition: NumericalVector.h:216
T * data()
Definition: NumericalVector.h:226
NumericalVector< T > & operator+=(const NumericalVector< T > &right)
Definition: NumericalVector.h:168
NumericalVector< T > operator/(const T &right) const
Definition: NumericalVector.h:161
const T * data() const
Definition: NumericalVector.h:221
std::vector< T > data_
Definition: NumericalVector.h:232
NumericalVector< T > operator+(const NumericalVector< T > &right) const
Definition: NumericalVector.h:117
T & operator[](std::size_t n)
Definition: NumericalVector.h:192
NumericalVector< T > operator*(const T &right) const
Definition: NumericalVector.h:135
const T & operator[](std::size_t n) const
Definition: NumericalVector.h:198
NumericalVector< T > & operator*=(const T &right)
Definition: NumericalVector.h:185
const std::complex< Mdouble > i
Definition: ExtendedMath.h:51