Main MRPT website > C++ reference
MRPT logo
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-2014, 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>
14 
15 // Many of the functions originally in this file are now in ops_containers.h
17 
18 
19 namespace mrpt
20 {
21  namespace utils { class CFileStream; }
22 
23  namespace math
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 *(const std::vector<T1>&a, const std::vector<T2>&b)
54  {
55  ASSERT_EQUAL_(a.size(),b.size())
56  const size_t N=a.size();
57  std::vector<T1> ret(N);
58  for (size_t i=0;i<N;i++) ret[i]=a[i]*b[i];
59  return ret;
60  }
61 
62  /** a+=b (element-wise sum) */
63  template <typename T1,typename T2>
64  inline std::vector<T1>& operator +=(std::vector<T1>&a, const std::vector<T2>&b)
65  {
66  ASSERT_EQUAL_(a.size(),b.size())
67  const size_t N=a.size();
68  for (size_t i=0;i<N;i++) a[i]+=b[i];
69  return a;
70  }
71 
72  /** a+=b (sum a constant) */
73  template <typename T1>
74  inline std::vector<T1>& operator +=(std::vector<T1>&a, const T1 b)
75  {
76  const size_t N=a.size();
77  for (size_t i=0;i<N;i++) a[i]+=b;
78  return a;
79  }
80 
81  /** a+b (element-wise sum) */
82  template <typename T1,typename T2>
83  inline std::vector<T1> operator +(const std::vector<T1>&a, const std::vector<T2>&b)
84  {
85  ASSERT_EQUAL_(a.size(),b.size())
86  const size_t N=a.size();
87  std::vector<T1> ret(N);
88  for (size_t i=0;i<N;i++) ret[i]=a[i]+b[i];
89  return ret;
90  }
91 
92  template <typename T1,typename T2>
93  inline std::vector<T1> operator -(const std::vector<T1> &v1, const std::vector<T2>&v2) {
94  ASSERT_EQUAL_(v1.size(),v2.size())
95  std::vector<T1> res(v1.size());
96  for (size_t i=0;i<v1.size();i++) res[i]=v1[i]-v2[i];
97  return res;
98  }
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  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 CArrayNumeric<T,N> to a stream. */
134  template <typename T,size_t N>
135  mrpt::utils::CStream& operator << (mrpt::utils::CStream& ostrm, const CArrayNumeric<T,N>& a)
136  {
137  ostrm << mrpt::utils::TTypeName< CArrayNumeric<T,N> >::get();
138  if (N) ostrm.WriteBufferFixEndianness<T>(&a[0],N);
139  return ostrm;
140  }
141 
142  /** Binary read of a CArrayNumeric<T,N> from a stream. */
143  template <typename T,size_t N>
145  {
146  static const std::string namExpect = mrpt::utils::TTypeName< CArrayNumeric<T,N> >::get();
147  std::string nam;
148  istrm >> nam;
149  ASSERTMSG_(nam==namExpect, format("Error deserializing: expected '%s', got '%s'", namExpect.c_str(),nam.c_str() ) )
150  if (N) istrm.ReadBufferFixEndianness<T>(&a[0],N);
151  return istrm;
152  }
153 
154 
155  /** @} */ // end of grouping
156 
157  } // End of math namespace
158 
159 } // End of mrpt namespace
160 
161 
162 #endif
#define ASSERT_EQUAL_(__A, __B)
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:46
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...
CArrayNumeric is an array for numeric types supporting several mathematical operations (actually...
Definition: CArray.h:305
std::vector< T1 > operator+(const std::vector< T1 > &a, const std::vector< T2 > &b)
a+b (element-wise sum)
Definition: ops_vectors.h:83
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:36
std::vector< T1 > & operator+=(std::vector< T1 > &a, const std::vector< T2 > &b)
a+=b (element-wise sum)
Definition: ops_vectors.h:64
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.
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:90
#define ASSERTMSG_(f, __ERROR_MSG)
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:34
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.1.0 SVN: at lun oct 28 00:54:49 CET 2019 Hosted on:
SourceForge.net Logo