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::math
20 {
21 // Frwd. decl.
22 template <typename T, std::size_t N>
23 class CArrayNumeric;
24 
25 /** \addtogroup container_ops_grp
26  * @{ */
27 
28 /** \name Generic std::vector element-wise operations
29  * @{
30  */
31 
32 /** a*=b (element-wise multiplication) */
33 template <typename T1, typename T2>
34 inline std::vector<T1>& operator*=(std::vector<T1>& a, const std::vector<T2>& b)
35 {
36  ASSERT_EQUAL_(a.size(), b.size());
37  const size_t N = a.size();
38  for (size_t i = 0; i < N; i++) a[i] *= b[i];
39  return a;
40 }
41 
42 /** a*=k (multiplication by a constant) */
43 template <typename T1>
44 inline std::vector<T1>& operator*=(std::vector<T1>& a, const T1 b)
45 {
46  const size_t N = a.size();
47  for (size_t i = 0; i < N; i++) a[i] *= b;
48  return a;
49 }
50 
51 /** a*b (element-wise multiplication) */
52 template <typename T1, typename T2>
53 inline std::vector<T1> operator*(
54  const std::vector<T1>& a, const std::vector<T2>& b)
55 {
56  ASSERT_EQUAL_(a.size(), b.size());
57  const size_t N = a.size();
58  std::vector<T1> ret(N);
59  for (size_t i = 0; i < N; i++) ret[i] = a[i] * b[i];
60  return ret;
61 }
62 
63 /** a+=b (element-wise sum) */
64 template <typename T1, typename T2>
65 inline std::vector<T1>& operator+=(std::vector<T1>& a, const std::vector<T2>& b)
66 {
67  ASSERT_EQUAL_(a.size(), b.size());
68  const size_t N = a.size();
69  for (size_t i = 0; i < N; i++) a[i] += b[i];
70  return a;
71 }
72 
73 /** a+=b (sum a constant) */
74 template <typename T1>
75 inline std::vector<T1>& operator+=(std::vector<T1>& a, const T1 b)
76 {
77  const size_t N = a.size();
78  for (size_t i = 0; i < N; i++) a[i] += b;
79  return a;
80 }
81 
82 /** a+b (element-wise sum) */
83 template <typename T1, typename T2>
84 inline std::vector<T1> operator+(
85  const std::vector<T1>& a, const std::vector<T2>& b)
86 {
87  ASSERT_EQUAL_(a.size(), b.size());
88  const size_t N = a.size();
89  std::vector<T1> ret(N);
90  for (size_t i = 0; i < N; i++) ret[i] = a[i] + b[i];
91  return ret;
92 }
93 
94 template <typename T1, typename T2>
95 inline std::vector<T1> operator-(
96  const std::vector<T1>& v1, const std::vector<T2>& v2)
97 {
98  ASSERT_EQUAL_(v1.size(), v2.size());
99  std::vector<T1> res(v1.size());
100  for (size_t i = 0; i < v1.size(); i++) res[i] = v1[i] - v2[i];
101  return res;
102 }
103 
104 /** @} */
105 
106 /** A template function for printing out the contents of a std::vector variable.
107  */
108 template <class T>
109 std::ostream& operator<<(std::ostream& out, const std::vector<T>& d)
110 {
111  const std::streamsize old_pre = out.precision();
112  const std::ios_base::fmtflags old_flags = out.flags();
113  out << "[" << std::fixed << std::setprecision(4);
114  std::copy(d.begin(), d.end(), std::ostream_iterator<T>(out, " "));
115  out << "]";
116  out.flags(old_flags);
117  out.precision(old_pre);
118  return out;
119 }
120 
121 /** A template function for printing out the contents of a std::vector variable.
122  */
123 template <class T>
124 std::ostream& operator<<(std::ostream& out, std::vector<T>* d)
125 {
126  const std::streamsize old_pre = out.precision();
127  const std::ios_base::fmtflags old_flags = out.flags();
128  out << "[" << std::fixed << std::setprecision(4);
129  copy(d->begin(), d->end(), std::ostream_iterator<T>(out, " "));
130  out << "]";
131  out.flags(old_flags);
132  out.precision(old_pre);
133  return out;
134 }
135 
136 /** Binary dump of a CArrayNumeric<T,N> to a stream. */
137 template <typename T, size_t N>
140 {
141  ostrm << mrpt::typemeta::TTypeName<CArrayNumeric<T, N>>::get();
142  if (N) ostrm.WriteBufferFixEndianness<T>(&a[0], N);
143  return ostrm;
144 }
145 
146 /** Binary read of a CArrayNumeric<T,N> from a stream. */
147 template <typename T, size_t N>
150 {
151  static const std::string namExpect =
153  std::string nam;
154  istrm >> nam;
155  ASSERTMSG_(
156  nam == namExpect, format(
157  "Error deserializing: expected '%s', got '%s'",
158  namExpect.c_str(), nam.c_str()));
159  if (N) istrm.ReadBufferFixEndianness<T>(&a[0], N);
160  return istrm;
161 }
162 
163 /** @} */ // end of grouping
164 
165 }
166 
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:127
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:84
This base provides a set of functions for maths stuff.
#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:65
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
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:52
GLfloat GLfloat v1
Definition: glext.h:4105
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
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:88
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:34
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:53



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020