MRPT  2.0.2
ops_vectors.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
12 #include <mrpt/math/CVectorFixed.h>
14 #include <iomanip> // for setprecision(), etc.
15 #include <iterator> // std::ostream_iterator
16 
17 // Many of the functions originally in this file are now in ops_containers.h
19 
20 namespace mrpt::math
21 {
22 /** \addtogroup container_ops_grp
23  * @{ */
24 
25 /** \name Generic std::vector element-wise operations
26  * @{
27  */
28 
29 /** a*=b (element-wise multiplication) */
30 template <typename T1, typename T2>
31 inline std::vector<T1>& operator*=(std::vector<T1>& a, const std::vector<T2>& b)
32 {
33  ASSERT_EQUAL_(a.size(), b.size());
34  const size_t N = a.size();
35  for (size_t i = 0; i < N; i++) a[i] *= b[i];
36  return a;
37 }
38 
39 /** a*=k (multiplication by a constant) */
40 template <typename T1>
41 inline std::vector<T1>& operator*=(std::vector<T1>& a, const T1 b)
42 {
43  const size_t N = a.size();
44  for (size_t i = 0; i < N; i++) a[i] *= b;
45  return a;
46 }
47 
48 /** a*b (element-wise multiplication) */
49 template <typename T1, typename T2>
50 inline std::vector<T1> operator*(
51  const std::vector<T1>& a, const std::vector<T2>& b)
52 {
53  ASSERT_EQUAL_(a.size(), b.size());
54  const size_t N = a.size();
55  std::vector<T1> ret(N);
56  for (size_t i = 0; i < N; i++) ret[i] = a[i] * b[i];
57  return ret;
58 }
59 
60 /** a+=b (element-wise sum) */
61 template <typename T1, typename T2>
62 inline std::vector<T1>& operator+=(std::vector<T1>& a, const std::vector<T2>& b)
63 {
64  ASSERT_EQUAL_(a.size(), b.size());
65  const size_t N = a.size();
66  for (size_t i = 0; i < N; i++) a[i] += b[i];
67  return a;
68 }
69 
70 /** a+=b (sum a constant) */
71 template <typename T1>
72 inline std::vector<T1>& operator+=(std::vector<T1>& a, const T1 b)
73 {
74  const size_t N = a.size();
75  for (size_t i = 0; i < N; i++) a[i] += b;
76  return a;
77 }
78 
79 /** a+b (element-wise sum) */
80 template <typename T1, typename T2>
81 inline std::vector<T1> operator+(
82  const std::vector<T1>& a, const std::vector<T2>& b)
83 {
84  ASSERT_EQUAL_(a.size(), b.size());
85  const size_t N = a.size();
86  std::vector<T1> ret(N);
87  for (size_t i = 0; i < N; i++) ret[i] = a[i] + b[i];
88  return ret;
89 }
90 
91 template <typename T1, typename T2>
92 inline std::vector<T1> operator-(
93  const std::vector<T1>& v1, const std::vector<T2>& v2)
94 {
95  ASSERT_EQUAL_(v1.size(), v2.size());
96  std::vector<T1> res(v1.size());
97  for (size_t i = 0; i < v1.size(); i++) res[i] = v1[i] - v2[i];
98  return res;
99 }
100 
101 /** @} */
102 
103 /** A template function for printing out the contents of a std::vector variable.
104  */
105 template <class T>
106 std::ostream& operator<<(std::ostream& out, const std::vector<T>& d)
107 {
108  const std::streamsize old_pre = out.precision();
109  const std::ios_base::fmtflags old_flags = out.flags();
110  out << "[" << std::fixed << std::setprecision(4);
111  std::copy(d.begin(), d.end(), std::ostream_iterator<T>(out, " "));
112  out << "]";
113  out.flags(old_flags);
114  out.precision(old_pre);
115  return out;
116 }
117 
118 /** A template function for printing out the contents of a std::vector variable.
119  */
120 template <class T>
121 std::ostream& operator<<(std::ostream& out, std::vector<T>* d)
122 {
123  const std::streamsize old_pre = out.precision();
124  const std::ios_base::fmtflags old_flags = out.flags();
125  out << "[" << std::fixed << std::setprecision(4);
126  copy(d->begin(), d->end(), std::ostream_iterator<T>(out, " "));
127  out << "]";
128  out.flags(old_flags);
129  out.precision(old_pre);
130  return out;
131 }
132 
133 /** Binary dump of a CVectorFixed<T,N> to a stream. */
134 template <typename T, size_t N>
137 {
138  ostrm << mrpt::typemeta::TTypeName<CVectorFixed<T, N>>::get();
139  if (N) ostrm.WriteBufferFixEndianness<T>(&a[0], N);
140  return ostrm;
141 }
142 
143 /** Binary read of a CVectorFixed<T,N> from a stream. */
144 template <typename T, size_t N>
147 {
148  static const std::string namExpect =
150  std::string nam;
151  istrm >> nam;
152  ASSERTMSG_(
153  nam == namExpect, format(
154  "Error deserializing: expected '%s', got '%s'",
155  namExpect.c_str(), nam.c_str()));
156  if (N) istrm.ReadBufferFixEndianness<T>(&a[0], N);
157  return istrm;
158 }
159 
160 /** @} */ // end of grouping
161 
162 } // namespace mrpt::math
std::vector< T1 > operator-(const std::vector< T1 > &v1, const std::vector< T2 > &v2)
Definition: ops_vectors.h:92
A compile-time fixed-size numeric matrix container.
Definition: CMatrixFixed.h:33
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
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:133
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:69
mrpt::serialization::CArchive & operator>>(mrpt::serialization::CArchive &in, CMatrixD::Ptr &pObj)
std::vector< T1 > operator+(const std::vector< T1 > &a, const std::vector< T2 > &b)
a+b (element-wise sum)
Definition: ops_vectors.h:81
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:137
std::vector< T1 > & operator+=(std::vector< T1 > &a, const std::vector< T2 > &b)
a+=b (element-wise sum)
Definition: ops_vectors.h:62
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
Definition: exceptions.h:108
Virtual base class for "archives": classes abstracting I/O streams.
Definition: CArchive.h:54
mrpt::vision::TStereoCalibResults out
mrpt::serialization::CArchive & operator<<(mrpt::serialization::CArchive &s, const CVectorFloat &a)
Definition: math.cpp:626
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:94
std::vector< T1 > & operator*=(std::vector< T1 > &a, const std::vector< T2 > &b)
a*=b (element-wise multiplication)
Definition: ops_vectors.h:31
std::vector< T1 > operator*(const std::vector< T1 > &a, const std::vector< T2 > &b)
a*b (element-wise multiplication)
Definition: ops_vectors.h:50



Page generated by Doxygen 1.8.14 for MRPT 2.0.2 Git: 9b4fd2465 Mon May 4 16:59:08 2020 +0200 at lun may 4 17:26:07 CEST 2020