PandA-2024.02
augmented_vector.hpp
Go to the documentation of this file.
1 /*
2  *
3  * _/_/_/ _/_/ _/ _/ _/_/_/ _/_/
4  * _/ _/ _/ _/ _/_/ _/ _/ _/ _/ _/
5  * _/_/_/ _/_/_/_/ _/ _/_/ _/ _/ _/_/_/_/
6  * _/ _/ _/ _/ _/ _/ _/ _/ _/
7  * _/ _/ _/ _/ _/ _/_/_/ _/ _/
8  *
9  * ***********************************************
10  * PandA Project
11  * URL: http://panda.dei.polimi.it
12  * Politecnico di Milano - DEIB
13  * System Architectures Group
14  * ***********************************************
15  * Copyright (C) 2004-2024 Politecnico di Milano
16  *
17  * This file is part of the PandA framework.
18  *
19  * The PandA framework is free software; you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation; either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  *
32  */
33 
45 #ifndef AUGMENTED_VECTOR_HPP
46 #define AUGMENTED_VECTOR_HPP
47 #include "exceptions.hpp" // for THROW_ASSERT
48 #include <cmath> // for sqrtl
49 #include <cstddef> // for size_t, ptrdiff_t
50 #include <string> // for string
51 #include <vector> // for vector
52 
53 template <class T>
55 {
56  private:
58  std::vector<T> internal_vector;
59 
60  public:
64  AugmentedVector<T>() = default;
65 
71  AugmentedVector<T>(const size_t _size, T element) : internal_vector(_size, element)
72  {
73  }
74 
79  explicit AugmentedVector<T>(const size_t _size) : internal_vector(_size)
80  {
81  }
82 
87  explicit AugmentedVector<T>(const std::vector<T>& vector) : internal_vector(vector)
88  {
89  }
90 
95  T& operator[](size_t position)
96  {
97  return internal_vector[position];
98  }
99 
104  const T& operator[](size_t position) const
105  {
106  return internal_vector[position];
107  }
108 
114  T operator*(const AugmentedVector<T>& other) const
115  {
117  T return_value = 0;
118  THROW_ASSERT(internal_vector.size() == other.size(),
119  "Different size in operands: " + std::to_string(internal_vector.size()) + " vs " +
120  std::to_string(other.size()));
121  for(size_t i = 0; i < internal_vector.size(); i++)
122  {
123  return_value += internal_vector[i] * other[i];
124  }
125  return return_value;
126  }
127 
134  {
135  AugmentedVector<T> return_value;
136  THROW_ASSERT(internal_vector.size() == other.size(),
137  "Different size in operands: " + std::to_string(internal_vector.size()) + " vs " +
138  std::to_string(other.size()));
139  for(size_t i = 0; i < internal_vector.size(); i++)
140  {
141  return_value.internal_vector[i] = internal_vector[i] - other.internal_vector[i];
142  }
143  return return_value;
144  }
145 
150  size_t size() const
151  {
152  return internal_vector.size();
153  }
154 
159  T Norm2() const
160  {
162  T return_value = 0;
163  for(size_t i = 0; i < internal_vector.size(); i++)
164  {
165  return_value += internal_vector[i] * internal_vector[i];
166  }
167  return sqrtl(return_value);
168  }
169 
174  void get_min(size_t& min) const
175  {
176  min = 0;
177  const size_t _size = this->size();
178  for(size_t index = 0; index < _size; index++)
179  {
180  if(this->internal_vector[index] < this->internal_vector[min])
181  {
182  min = index;
183  }
184  }
185  }
186 
192  void get_min_max(size_t& min, size_t& max) const
193  {
194  min = 0;
195  max = 0;
196  const size_t _size = this->size();
197  for(size_t index = 0; index < _size; index++)
198  {
199  if(this->internal_vector[index] > this->internal_vector[max])
200  {
201  max = index;
202  }
203  if(this->internal_vector[index] < this->internal_vector[min])
204  {
205  min = index;
206  }
207  }
208  }
209 
214  T get_sum() const
215  {
216  T return_value = 0;
217  const size_t _size = this->size();
218  for(size_t index = 0; index < _size; index++)
219  {
220  return_value += this->internal_vector[index];
221  }
222  return return_value;
223  }
224 
229  T get_mean() const
230  {
231  return this->get_sum() / this->size();
232  }
233 
238  void remove(const size_t index)
239  {
240  typename std::vector<T>::iterator it = internal_vector.begin();
242  it += static_cast<ptrdiff_t>(index);
243  internal_vector.erase(it);
244  }
245 
249  void clear()
250  {
251  this->internal_vector.clear();
252  }
253 
258  void push_back(const T& elem)
259  {
260  this->internal_vector.push_back(elem);
261  }
262 
268  void resize(size_t n, T t = T())
269  {
270  this->internal_vector.resize(n, t);
271  }
272 
276  void normalize()
277  {
278  const T sum = get_sum();
279  const size_t _size = this->size();
280  for(size_t index = 0; index < _size; index++)
281  {
282  this->internal_vector[index] /= sum;
283  }
284  }
285 
289  void Clear()
290  {
291  this->internal_vector.clear();
292  }
293 };
294 #endif
void normalize()
Normalize the row.
T & operator[](size_t position)
Redefinition of [] operator.
void get_min(size_t &min) const
Return the index of mininum element of the vector.
AugmentedVector< T > operator-(const AugmentedVector< T > &other) const
Redefinition of - operator as the difference of the single element.
void push_back(const T &elem)
Inserts a new elment at the end.
T get_mean() const
Return the mean of the vector.
exceptions managed by PandA
const T & operator[](size_t position) const
Redefinition of [] operator.
size_t size() const
Return the size of the vector.
#define min(x, y)
#define max
Definition: backprop.h:17
int sum
Definition: dotproduct.h:3
T get_sum() const
Return the sum of a row.
void get_min_max(size_t &min, size_t &max) const
Return the index of the minimum and maximum element of the vector.
void Clear()
Erase all elements.
#define index(x, y)
Definition: Keccak.c:74
T operator*(const AugmentedVector< T > &other) const
Redefinition of * operator as scalar multiplication.
T Norm2() const
Return the 2-norm of the vector.
void clear()
Erase all the elements.
std::vector< T > internal_vector
The class which it logical extended by this (inheritance from template is not allowed) ...
void resize(size_t n, T t=T())
Inserts or erases elements at the end such that the size becomes n.
#define THROW_ASSERT(cond, str_expr)
helper function used to check an assert and if needed to throw an error in a standard way ...
Definition: exceptions.hpp:289

Generated on Mon Feb 12 2024 13:02:56 for PandA-2024.02 by doxygen 1.8.13