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-2018, 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 #pragma once
10 
12 #include <iomanip> // for setprecision(), etc.
13 #include <iterator> // std::ostream_iterator
15 
16 // Many of the functions originally in this file are now in ops_containers.h
18 
19 namespace mrpt
20 {
21 namespace math
22 {
23 // Frwd. decl.
24 template <typename T, std::size_t N>
25 class CArrayNumeric;
26 
27 /** \addtogroup container_ops_grp
28  * @{ */
29 
30 /** \name Generic std::vector element-wise operations
31  * @{
32  */
33 
34 /** a*=b (element-wise multiplication) */
35 template <typename T1, typename T2>
36 inline std::vector<T1>& operator*=(std::vector<T1>& a, const std::vector<T2>& b)
37 {
38  ASSERT_EQUAL_(a.size(), b.size());
39  const size_t N = a.size();
40  for (size_t i = 0; i < N; i++) a[i] *= b[i];
41  return a;
42 }
43 
44 /** a*=k (multiplication by a constant) */
45 template <typename T1>
46 inline std::vector<T1>& operator*=(std::vector<T1>& a, const T1 b)
47 {
48  const size_t N = a.size();
49  for (size_t i = 0; i < N; i++) a[i] *= b;
50  return a;
51 }
52 
53 /** a*b (element-wise multiplication) */
54 template <typename T1, typename T2>
55 inline std::vector<T1> operator*(
56  const std::vector<T1>& a, const std::vector<T2>& b)
57 {
58  ASSERT_EQUAL_(a.size(), b.size());
59  const size_t N = a.size();
60  std::vector<T1> ret(N);
61  for (size_t i = 0; i < N; i++) ret[i] = a[i] * b[i];
62  return ret;
63 }
64 
65 /** a+=b (element-wise sum) */
66 template <typename T1, typename T2>
67 inline std::vector<T1>& operator+=(std::vector<T1>& a, const std::vector<T2>& b)
68 {
69  ASSERT_EQUAL_(a.size(), b.size());
70  const size_t N = a.size();
71  for (size_t i = 0; i < N; i++) a[i] += b[i];
72  return a;
73 }
74 
75 /** a+=b (sum a constant) */
76 template <typename T1>
77 inline std::vector<T1>& operator+=(std::vector<T1>& a, const T1 b)
78 {
79  const size_t N = a.size();
80  for (size_t i = 0; i < N; i++) a[i] += b;
81  return a;
82 }
83 
84 /** a+b (element-wise sum) */
85 template <typename T1, typename T2>
86 inline std::vector<T1> operator+(
87  const std::vector<T1>& a, const std::vector<T2>& b)
88 {
89  ASSERT_EQUAL_(a.size(), b.size());
90  const size_t N = a.size();
91  std::vector<T1> ret(N);
92  for (size_t i = 0; i < N; i++) ret[i] = a[i] + b[i];
93  return ret;
94 }
95 
96 template <typename T1, typename T2>
97 inline std::vector<T1> operator-(
98  const std::vector<T1>& v1, const std::vector<T2>& v2)
99 {
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 /** A template function for printing out the contents of a std::vector variable.
109  */
110 template <class T>
111 std::ostream& operator<<(std::ostream& out, const std::vector<T>& d)
112 {
113  const std::streamsize old_pre = out.precision();
114  const std::ios_base::fmtflags old_flags = out.flags();
115  out << "[" << std::fixed << std::setprecision(4);
116  std::copy(d.begin(), d.end(), std::ostream_iterator<T>(out, " "));
117  out << "]";
118  out.flags(old_flags);
119  out.precision(old_pre);
120  return out;
121 }
122 
123 /** A template function for printing out the contents of a std::vector variable.
124  */
125 template <class T>
126 std::ostream& operator<<(std::ostream& out, std::vector<T>* d)
127 {
128  const std::streamsize old_pre = out.precision();
129  const std::ios_base::fmtflags old_flags = out.flags();
130  out << "[" << std::fixed << std::setprecision(4);
131  copy(d->begin(), d->end(), std::ostream_iterator<T>(out, " "));
132  out << "]";
133  out.flags(old_flags);
134  out.precision(old_pre);
135  return out;
136 }
137 
138 /** Binary dump of a CArrayNumeric<T,N> to a stream. */
139 template <typename T, size_t N>
142 {
143  ostrm << mrpt::typemeta::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>
152 {
153  static const std::string namExpect =
155  std::string nam;
156  istrm >> nam;
157  ASSERTMSG_(
158  nam == namExpect, format(
159  "Error deserializing: expected '%s', got '%s'",
160  namExpect.c_str(), nam.c_str()));
161  if (N) istrm.ReadBufferFixEndianness<T>(&a[0], N);
162  return istrm;
163 }
164 
165 /** @} */ // end of grouping
166 
167 } // namespace math
168 } // namespace mrpt
This file implements several operations that operate element-wise on individual or pairs of container...
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: CArchive.h:123
CArrayNumeric is an array for numeric types supporting several mathematical operations (actually...
Definition: CArrayNumeric.h:25
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:65
std::vector< T1 > operator+(const std::vector< T1 > &a, const std::vector< T2 > &b)
a+b (element-wise sum)
Definition: ops_vectors.h:86
#define ASSERT_EQUAL_(__A, __B)
Assert comparing two values, reporting their actual values upon failure.
Definition: exceptions.h:153
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:67
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
GLubyte GLubyte b
Definition: glext.h:6279
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:101
GLsizei const GLchar ** string
Definition: glext.h:4101
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:48
GLfloat GLfloat v1
Definition: glext.h:4105
GLfloat GLfloat GLfloat v2
Definition: glext.h:4107
GLuint res
Definition: glext.h:7268
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: CArchive.h:84
GLubyte GLubyte GLubyte a
Definition: glext.h:6279
TPose3D operator-(const TPose3D &p)
Unary $$ operator: computes inverse SE(3) element.
std::vector< T1 > & operator*=(std::vector< T1 > &a, const std::vector< T2 > &b)
a*=b (element-wise multiplication)
Definition: ops_vectors.h:36
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, CMatrix::Ptr &pObj)
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
Definition: ops_vectors.h:55



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019