Main MRPT website > C++ reference for MRPT 1.5.6
ops_vectors.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 #ifndef mrpt_math_vector_ops_H
10 #define mrpt_math_vector_ops_H
11 
12 #include <mrpt/utils/utils_defs.h>
13 #include <mrpt/utils/CStream.h>
15 #include <iomanip> // for setprecision(), etc.
16 #include <iterator> // std::ostream_iterator
17 
18 // Many of the functions originally in this file are now in ops_containers.h
20 
21 
22 namespace mrpt
23 {
24  namespace utils { class CFileStream; }
25 
26  namespace math
27  {
28  // Frwd. decl.
29  template <typename T, std::size_t N> class CArrayNumeric;
30 
31  /** \addtogroup container_ops_grp
32  * @{ */
33 
34  /** \name Generic std::vector element-wise operations
35  * @{
36  */
37 
38  /** a*=b (element-wise multiplication) */
39  template <typename T1,typename T2>
40  inline std::vector<T1>& operator *=(std::vector<T1>&a, const std::vector<T2>&b)
41  {
42  ASSERT_EQUAL_(a.size(),b.size())
43  const size_t N=a.size();
44  for (size_t i=0;i<N;i++) a[i]*=b[i];
45  return a;
46  }
47 
48  /** a*=k (multiplication by a constant) */
49  template <typename T1>
50  inline std::vector<T1>& operator *=(std::vector<T1>&a, const T1 b)
51  {
52  const size_t N=a.size();
53  for (size_t i=0;i<N;i++) a[i]*=b;
54  return a;
55  }
56 
57  /** a*b (element-wise multiplication) */
58  template <typename T1,typename T2>
59  inline std::vector<T1> operator *(const std::vector<T1>&a, const std::vector<T2>&b)
60  {
61  ASSERT_EQUAL_(a.size(),b.size())
62  const size_t N=a.size();
63  std::vector<T1> ret(N);
64  for (size_t i=0;i<N;i++) ret[i]=a[i]*b[i];
65  return ret;
66  }
67 
68  /** a+=b (element-wise sum) */
69  template <typename T1,typename T2>
70  inline std::vector<T1>& operator +=(std::vector<T1>&a, const std::vector<T2>&b)
71  {
72  ASSERT_EQUAL_(a.size(),b.size())
73  const size_t N=a.size();
74  for (size_t i=0;i<N;i++) a[i]+=b[i];
75  return a;
76  }
77 
78  /** a+=b (sum a constant) */
79  template <typename T1>
80  inline std::vector<T1>& operator +=(std::vector<T1>&a, const T1 b)
81  {
82  const size_t N=a.size();
83  for (size_t i=0;i<N;i++) a[i]+=b;
84  return a;
85  }
86 
87  /** a+b (element-wise sum) */
88  template <typename T1,typename T2>
89  inline std::vector<T1> operator +(const std::vector<T1>&a, const std::vector<T2>&b)
90  {
91  ASSERT_EQUAL_(a.size(),b.size())
92  const size_t N=a.size();
93  std::vector<T1> ret(N);
94  for (size_t i=0;i<N;i++) ret[i]=a[i]+b[i];
95  return ret;
96  }
97 
98  template <typename T1,typename T2>
99  inline std::vector<T1> operator -(const std::vector<T1> &v1, const std::vector<T2>&v2) {
100  ASSERT_EQUAL_(v1.size(),v2.size())
101  std::vector<T1> res(v1.size());
102  for (size_t i=0;i<v1.size();i++) res[i]=v1[i]-v2[i];
103  return res;
104  }
105 
106  /** @} */
107 
108 
109  /** A template function for printing out the contents of a std::vector variable.
110  */
111  template <class T>
112  std::ostream& operator << (std::ostream& out, const std::vector<T> &d)
113  {
114  const std::streamsize old_pre = out.precision();
115  const std::ios_base::fmtflags old_flags = out.flags();
116  out << "[" << std::fixed << std::setprecision(4);
117  std::copy(d.begin(),d.end(), std::ostream_iterator<T>(out," "));
118  out << "]";
119  out.flags(old_flags);
120  out.precision(old_pre);
121  return out;
122  }
123 
124  /** A template function for printing out the contents of a std::vector variable.
125  */
126  template <class T>
127  std::ostream& operator << (std::ostream& out, std::vector<T> *d)
128  {
129  const std::streamsize old_pre = out.precision();
130  const std::ios_base::fmtflags old_flags = out.flags();
131  out << "[" << std::fixed << std::setprecision(4);
132  copy(d->begin(),d->end(), std::ostream_iterator<T>(out," "));
133  out << "]";
134  out.flags(old_flags);
135  out.precision(old_pre);
136  return out;
137  }
138 
139  /** Binary dump of a CArrayNumeric<T,N> to a stream. */
140  template <typename T,size_t N>
141  mrpt::utils::CStream& operator << (mrpt::utils::CStream& ostrm, const CArrayNumeric<T,N>& a)
142  {
143  ostrm << mrpt::utils::TTypeName< CArrayNumeric<T,N> >::get();
144  if (N) ostrm.WriteBufferFixEndianness<T>(&a[0],N);
145  return ostrm;
146  }
147 
148  /** Binary read of a CArrayNumeric<T,N> from a stream. */
149  template <typename T,size_t N>
151  {
152  static const std::string namExpect = mrpt::utils::TTypeName< CArrayNumeric<T,N> >::get();
153  std::string nam;
154  istrm >> nam;
155  ASSERTMSG_(nam==namExpect, format("Error deserializing: expected '%s', got '%s'", namExpect.c_str(),nam.c_str() ) )
156  if (N) istrm.ReadBufferFixEndianness<T>(&a[0],N);
157  return istrm;
158  }
159 
160 
161  /** @} */ // end of grouping
162 
163  } // End of math namespace
164 
165 } // End of mrpt namespace
166 
167 
168 #endif
#define ASSERT_EQUAL_(__A, __B)
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:47
This file implements several operations that operate element-wise on individual or pairs of container...
CArrayNumeric is an array for numeric types supporting several mathematical operations (actually...
Definition: CArrayNumeric.h:25
std::vector< T1 > operator+(const std::vector< T1 > &a, const std::vector< T2 > &b)
a+b (element-wise sum)
Definition: ops_vectors.h:89
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:38
std::vector< T1 > & operator+=(std::vector< T1 > &a, const std::vector< T2 > &b)
a+=b (element-wise sum)
Definition: ops_vectors.h:70
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:21
GLubyte GLubyte b
Definition: glext.h:5575
GLsizei const GLchar ** string
Definition: glext.h:3919
BASE_IMPEXP ::mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CMatrixPtr &pObj)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLfloat GLfloat v1
Definition: glext.h:3922
size_t ReadBufferFixEndianness(T *ptr, size_t ElementCount)
Reads a sequence of elemental datatypes, taking care of reordering their bytes from the MRPT stream s...
Definition: CStream.h:95
GLfloat GLfloat GLfloat v2
Definition: glext.h:3923
GLuint res
Definition: glext.h:6298
#define ASSERTMSG_(f, __ERROR_MSG)
GLubyte GLubyte GLubyte a
Definition: glext.h:5575
TPoint3D operator-(const TPoint3D &p1)
Unary minus operator for 3D points.
std::vector< T1 > & operator*=(std::vector< T1 > &a, const std::vector< T2 > &b)
a*=b (element-wise multiplication)
Definition: ops_vectors.h:40
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
Definition: ops_vectors.h:59



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019