Xmipp  v3.23.11-Nereus
vector_ops.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Alberto Pascual Montano (pascual@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 
26 
27 //-----------------------------------------------------------------------------
28 // xmippVectorsOps.hh
29 //-----------------------------------------------------------------------------
30 
31 #ifndef XMIPP_VECTOR_OPS_H
32 #define XMIPP_VECTOR_OPS_H
33 
34 #include <cmath>
35 #include <stdexcept>
36 #include <vector>
37 #include <functional>
38 #include <algorithm>
39 #include <sstream>
40 #include <random>
41 
45 
51 template<class T> std::vector<T> operator+(const std::vector<T>& v, const T& a)
52 {
53  std::vector<T> tmp = v;
54  transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(std::plus<T>(), a));
55  return tmp;
56 }
57 
63 template<class T> std::vector<T> operator+(const T& a, const std::vector<T>& v)
64 {
65  std::vector<T> tmp = v;
66  transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(std::plus<T>(), a));
67  return tmp;
68 }
69 
75 template<class T> std::vector<T> operator-(const std::vector<T>& v, const T& a)
76 {
77  std::vector<T> tmp = v;
78  transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(std::minus<T>(), a));
79  return tmp;
80 }
81 
87 template<class T> std::vector<T> operator-(const T& a, const std::vector<T>& v)
88 {
89  std::vector<T> tmp(v.size(), a);
90  transform(tmp.begin(), tmp.end(), v.begin(), tmp.begin(), std::minus<T>());
91  return tmp;
92 }
93 
99 template<class T> std::vector<T> operator*(const std::vector<T>& v, T a)
100 {
101  std::vector<T> tmp = v;
102  transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(std::multiplies<T>(), a));
103  return tmp;
104 }
105 
111 template<class T> std::vector<T> operator*(T a, const std::vector<T>& v)
112 {
113  std::vector<T> tmp = v;
114  transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(std::multiplies<T>(), a));
115  return tmp;
116 }
117 
118 
124 template<class T> std::vector<T> operator/(const std::vector<T>& v, T a)
125 {
126  std::vector<T> tmp = v;
127  transform(tmp.begin(), tmp.end(), tmp.begin(), bind2nd(std::divides<T>(), a));
128  return tmp;
129 }
130 
136 template<class T> std::vector<T> operator/(T a, const std::vector<T>& v)
137 {
138  std::vector<T> tmp(v.size(), a);
139  transform(tmp.begin(), tmp.end(), v.begin(), tmp.begin(), std::divides<T>());
140  return tmp;
141 }
142 
148 template<class T> std::vector<T>& operator*=(std::vector<T>& v1, const std::vector<T>& v2)
149 {
150  transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), std::multiplies<T>());
151  return v1;
152 }
153 
159 template<class T> std::vector<T>& operator/=(std::vector<T>& v1, const std::vector<T>& v2)
160 {
161  transform(v1.begin(), v1.end(), v2.begin(), v1.begin(), std::divides<T>());
162  return v1;
163 }
164 
165 
166 //-----------------------------------------------------------------------------
167 
174 template <class T>
175 std::vector<T> randomVector(const unsigned& _size, const T& _lower,
176  const T& _upper)
177 {
178  std::vector<T> v(_size);
179  std::random_device rd; //Will be used to obtain a seed for the random number engine
180  std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
181  std::uniform_real_distribution<> dist(_lower, _upper);
182  std::generate(v.begin(), v.end(), [&]{ return dist(gen); });
183  return v;
184 }
185 
192 template <class T>
193 std::vector<T>& operator += (std::vector<T>& _v1, const std::vector<T>& _v2)
194 {
195  if (_v1.size() != _v2.size())
196  throw std::runtime_error("different size vectors in +=");
197 
198  transform(_v1.begin(), _v1.end(), _v2.begin(), _v1.begin(),
199  std::plus<T>());
200 
201  return _v1;
202 }
203 
210 template <class T>
211 std::vector<T>& operator -= (std::vector<T>& _v1, const std::vector<T>& _v2)
212 {
213  if (_v1.size() != _v2.size())
214  throw std::runtime_error("different size vectors in -=");
215 
216  transform(_v1.begin(), _v1.end(), _v2.begin(), _v1.begin(),
217  std::minus<T>());
218 
219  return _v1;
220 }
221 
228 template <class T>
229 std::vector<T> operator + (const std::vector<T>& _v1, const std::vector<T>& _v2)
230 {
231  if (_v1.size() != _v2.size())
232  throw std::runtime_error("different size vectors in +");
233 
234  // temp. container
235  std::vector<T> tmp(_v1.size());
236 
237  transform(_v1.begin(), _v1.end(), _v2.begin(), tmp.begin(),
238  std::plus<T>());
239  return tmp;
240 }
241 
248 template <class T>
249 std::vector<T> operator - (const std::vector<T>& _v1, const std::vector<T>& _v2)
250 {
251  if (_v1.size() != _v2.size())
252  throw std::runtime_error("different size vectors in -");
253 
254  // temp. container
255  std::vector<T> tmp(_v1.size());
256 
257  transform(_v1.begin(), _v1.end(), _v2.begin(), tmp.begin(),
258  std::minus<T>());
259  return tmp;
260 }
261 
268 template <class T>
269 T operator *(const std::vector<T>& _v1, const std::vector<T>& _v2)
270 {
271  if (_v1.size() != _v2.size())
272  throw std::runtime_error("different size vectors in *");
273 
274  T dotProd = 0;
275  typename std::vector<T>::const_iterator i;
276  typename std::vector<T>::const_iterator j;
277  for (i = _v1.begin(), j = _v2.begin() ; i != _v1.end() ; i++, j++)
278  dotProd += *i * *j;
279 
280  return dotProd;
281 }
282 
288 template <class T>
289 std::vector<T>& operator *= (std::vector<T>& _v, const T _alpha)
290 {
291  typename std::vector<T>::iterator i;
292  for (i = _v.begin() ; i != _v.end() ; i++)
293  *i *= _alpha;
294 
295  return _v;
296 }
297 
303 template <class T>
304 std::vector<T>& operator /= (std::vector<T>& _v, const T _alpha)
305 {
306  typename std::vector<T>::iterator i;
307  for (i = _v.begin() ; i != _v.end() ; i++)
308  *i /= _alpha;
309 
310  return _v;
311 }
312 
318 template <class T>
319 std::ostream& operator << (std::ostream& _os, const std::vector<T>& _v)
320 {
321  _os << "< ";
322  typename std::vector<T>::const_iterator i;
323  for (i = _v.begin(); i != _v.end(); i++) _os << *i << " ";
324  _os << ">";
325  return _os;
326 }
327 
334 template <class T>
335 std::istream& operator >> (std::istream& _is, std::vector<T>& _v)
336 {
337  _v.clear();
338 
339  char c;
340  _is >> c;
341 
342  if (_is && c != '<')
343  _is.setstate(std::ios::failbit);
344 
345  bool finish = false;
346 
347  while (_is && !finish)
348  {
349  _is >> c;
350 
351  if (!_is)
352  return _is;
353 
354  if (c == '>')
355  finish = true;
356  else
357  {
358  _is.putback(c);
359  T item;
360  _is >> item;
361 
362  if (_is)
363  _v.push_back(item);
364  else
365  return _is;
366  }
367  }
368 
369  if (!_is)
370  throw std::runtime_error("Error reading the vector");
371 
372  return _is;
373 }
374 
376 template <class T>
377 double euclideanDistance(const std::vector<T>& _v1, const std::vector<T>& _v2)
378 {
379  if (_v1.size() != _v2.size())
380  throw std::runtime_error("vector of different size in eDist");
381 
382  double dist = 0;
383  auto i = _v1.begin();
384  auto j = _v2.begin();
385  for (; i < _v1.end(); i++, j++)
386  {
387  double tmp = (double)(*i) - (double)(*j);
388  dist += tmp * tmp;
389  }
390 
391  return sqrt(dist);
392 }
393 
398 template <class T>
399 T norm(const std::vector<T>& v)
400 {
401  double sum = 0.0;
402  typename std::vector<T>::const_iterator i;
403  for (i = v.begin(); i != v.end(); i++)
404  sum += (double)(*i) * (double)(*i);
405  return (T) sqrt(sum);
406 }
408 #endif
409 
std::istream & operator>>(std::istream &_is, std::vector< T > &_v)
Definition: vector_ops.h:335
double euclideanDistance(const std::vector< T > &_v1, const std::vector< T > &_v2)
Definition: vector_ops.h:377
doublereal * c
void sqrt(Image< double > &op)
std::vector< T > operator+(const std::vector< T > &v, const T &a)
Definition: vector_ops.h:51
std::vector< T > operator/(const std::vector< T > &v, T a)
Definition: vector_ops.h:124
T norm(const std::vector< T > &v)
Definition: vector_ops.h:399
#define i
std::vector< T > operator-(const std::vector< T > &v, const T &a)
Definition: vector_ops.h:75
double v1
std::vector< T > & operator-=(std::vector< T > &_v1, const std::vector< T > &_v2)
Definition: vector_ops.h:211
basic_istream< char, std::char_traits< char > > istream
Definition: utilities.cpp:815
#define j
std::vector< T > & operator+=(std::vector< T > &_v1, const std::vector< T > &_v2)
Definition: vector_ops.h:193
std::vector< T > operator*(const std::vector< T > &v, T a)
Definition: vector_ops.h:99
std::vector< T > & operator/=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: vector_ops.h:159
std::vector< T > & operator*=(std::vector< T > &v1, const std::vector< T > &v2)
Definition: vector_ops.h:148
doublereal * a
std::vector< T > randomVector(const unsigned &_size, const T &_lower, const T &_upper)
Definition: vector_ops.h:175