Main MRPT website > C++ reference for MRPT 1.4.0
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-2016, 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
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
22namespace 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>
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
CArrayNumeric is an array for numeric types supporting several mathematical operations (actually,...
Definition: CArrayNumeric.h:26
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:39
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
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:139
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
std::vector< T1 > & operator+=(std::vector< T1 > &a, const std::vector< T2 > &b)
a+=b (element-wise sum)
Definition: ops_vectors.h:70
std::vector< T1 > operator+(const std::vector< T1 > &a, const std::vector< T2 > &b)
a+b (element-wise sum)
Definition: ops_vectors.h:89
TPoint3D operator-(const TPoint3D &p1)
Unary minus operator for 3D points.
std::ostream BASE_IMPEXP & operator<<(std::ostream &o, const TPoint2D &p)
#define ASSERT_EQUAL_(__A, __B)
Definition: mrpt_macros.h:264
#define ASSERTMSG_(f, __ERROR_MSG)
Definition: mrpt_macros.h:260
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.
std::string BASE_IMPEXP format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
This file implements several operations that operate element-wise on individual or pairs of container...
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:48



Page generated by Doxygen 1.9.6 for MRPT 1.4.0 SVN: at Sat Jan 21 06:46:15 UTC 2023