Main MRPT website > C++ reference for MRPT 1.9.9
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 namespace mrpt
22 {
23 namespace utils
24 {
25 class CFileStream;
26 }
27 
28 namespace math
29 {
30 // Frwd. decl.
31 template <typename T, std::size_t N>
32 class CArrayNumeric;
33 
34 /** \addtogroup container_ops_grp
35  * @{ */
36 
37 /** \name Generic std::vector element-wise operations
38  * @{
39  */
40 
41 /** a*=b (element-wise multiplication) */
42 template <typename T1, typename T2>
43 inline std::vector<T1>& operator*=(std::vector<T1>& a, const std::vector<T2>& b)
44 {
45  ASSERT_EQUAL_(a.size(), b.size())
46  const size_t N = a.size();
47  for (size_t i = 0; i < N; i++) a[i] *= b[i];
48  return a;
49 }
50 
51 /** a*=k (multiplication by a constant) */
52 template <typename T1>
53 inline std::vector<T1>& operator*=(std::vector<T1>& a, const T1 b)
54 {
55  const size_t N = a.size();
56  for (size_t i = 0; i < N; i++) a[i] *= b;
57  return a;
58 }
59 
60 /** a*b (element-wise multiplication) */
61 template <typename T1, typename T2>
62 inline std::vector<T1> operator*(
63  const std::vector<T1>& a, const std::vector<T2>& b)
64 {
65  ASSERT_EQUAL_(a.size(), b.size())
66  const size_t N = a.size();
67  std::vector<T1> ret(N);
68  for (size_t i = 0; i < N; i++) ret[i] = a[i] * b[i];
69  return ret;
70 }
71 
72 /** a+=b (element-wise sum) */
73 template <typename T1, typename T2>
74 inline std::vector<T1>& operator+=(std::vector<T1>& a, const std::vector<T2>& b)
75 {
76  ASSERT_EQUAL_(a.size(), b.size())
77  const size_t N = a.size();
78  for (size_t i = 0; i < N; i++) a[i] += b[i];
79  return a;
80 }
81 
82 /** a+=b (sum a constant) */
83 template <typename T1>
84 inline std::vector<T1>& operator+=(std::vector<T1>& a, const T1 b)
85 {
86  const size_t N = a.size();
87  for (size_t i = 0; i < N; i++) a[i] += b;
88  return a;
89 }
90 
91 /** a+b (element-wise sum) */
92 template <typename T1, typename T2>
93 inline std::vector<T1> operator+(
94  const std::vector<T1>& a, const std::vector<T2>& b)
95 {
96  ASSERT_EQUAL_(a.size(), b.size())
97  const size_t N = a.size();
98  std::vector<T1> ret(N);
99  for (size_t i = 0; i < N; i++) ret[i] = a[i] + b[i];
100  return ret;
101 }
102 
103 template <typename T1, typename T2>
104 inline std::vector<T1> operator-(
105  const std::vector<T1>& v1, const std::vector<T2>& v2)
106 {
107  ASSERT_EQUAL_(v1.size(), v2.size())
108  std::vector<T1> res(v1.size());
109  for (size_t i = 0; i < v1.size(); i++) res[i] = v1[i] - v2[i];
110  return res;
111 }
112 
113 /** @} */
114 
115 /** A template function for printing out the contents of a std::vector variable.
116  */
117 template <class T>
118 std::ostream& operator<<(std::ostream& out, const std::vector<T>& d)
119 {
120  const std::streamsize old_pre = out.precision();
121  const std::ios_base::fmtflags old_flags = out.flags();
122  out << "[" << std::fixed << std::setprecision(4);
123  std::copy(d.begin(), d.end(), std::ostream_iterator<T>(out, " "));
124  out << "]";
125  out.flags(old_flags);
126  out.precision(old_pre);
127  return out;
128 }
129 
130 /** A template function for printing out the contents of a std::vector variable.
131  */
132 template <class T>
133 std::ostream& operator<<(std::ostream& out, std::vector<T>* d)
134 {
135  const std::streamsize old_pre = out.precision();
136  const std::ios_base::fmtflags old_flags = out.flags();
137  out << "[" << std::fixed << std::setprecision(4);
138  copy(d->begin(), d->end(), std::ostream_iterator<T>(out, " "));
139  out << "]";
140  out.flags(old_flags);
141  out.precision(old_pre);
142  return out;
143 }
144 
145 /** Binary dump of a CArrayNumeric<T,N> to a stream. */
146 template <typename T, size_t N>
149 {
150  ostrm << mrpt::utils::TTypeName<CArrayNumeric<T, N>>::get();
151  if (N) ostrm.WriteBufferFixEndianness<T>(&a[0], N);
152  return ostrm;
153 }
154 
155 /** Binary read of a CArrayNumeric<T,N> from a stream. */
156 template <typename T, size_t N>
159 {
160  static const std::string namExpect =
162  std::string nam;
163  istrm >> nam;
164  ASSERTMSG_(
165  nam == namExpect, format(
166  "Error deserializing: expected '%s', got '%s'",
167  namExpect.c_str(), nam.c_str()))
168  if (N) istrm.ReadBufferFixEndianness<T>(&a[0], N);
169  return istrm;
170 }
171 
172 /** @} */ // end of grouping
173 
174 } // End of math namespace
175 
176 } // End of mrpt namespace
177 
178 #endif
#define ASSERT_EQUAL_(__A, __B)
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:55
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:26
void WriteBufferFixEndianness(const T *ptr, size_t ElementCount)
Writes a sequence of elemental datatypes, taking care of reordering their bytes from the running arch...
Definition: CStream.h:159
std::vector< T1 > operator+(const std::vector< T1 > &a, const std::vector< T2 > &b)
a+b (element-wise sum)
Definition: ops_vectors.h:93
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:41
std::ostream & operator<<(std::ostream &o, const TPoint2D &p)
std::vector< T1 > & operator+=(std::vector< T1 > &a, const std::vector< T2 > &b)
a+=b (element-wise sum)
Definition: ops_vectors.h:74
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:19
GLubyte GLubyte b
Definition: glext.h:6279
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::utils::CStream & operator>>(mrpt::utils::CStream &in, CMatrix::Ptr &pObj)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLfloat GLfloat v1
Definition: glext.h:4105
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:108
GLfloat GLfloat GLfloat v2
Definition: glext.h:4107
GLuint res
Definition: glext.h:7268
#define ASSERTMSG_(f, __ERROR_MSG)
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
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:43
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
Definition: ops_vectors.h:62



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019