Main MRPT website > C++ reference
MRPT logo
ops_vectors.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef mrpt_math_vector_ops_H
29 #define mrpt_math_vector_ops_H
30 
31 #include <mrpt/utils/utils_defs.h>
33 
34 // Many of the functions originally in this file are now in ops_containers.h
36 
37 
38 namespace mrpt
39 {
40  namespace utils { class CFileStream; }
41 
42  namespace math
43  {
44  /** \addtogroup container_ops_grp
45  * @{ */
46 
47  /** \name Generic std::vector element-wise operations
48  * @{
49  */
50 
51  /** a*=b (element-wise multiplication) */
52  template <typename T1,typename T2>
53  inline std::vector<T1>& operator *=(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  for (size_t i=0;i<N;i++) a[i]*=b[i];
58  return a;
59  }
60 
61  /** a*=k (multiplication by a constant) */
62  template <typename T1>
63  inline std::vector<T1>& operator *=(std::vector<T1>&a, const T1 b)
64  {
65  const size_t N=a.size();
66  for (size_t i=0;i<N;i++) a[i]*=b;
67  return a;
68  }
69 
70  /** a*b (element-wise multiplication) */
71  template <typename T1,typename T2>
72  inline std::vector<T1> operator *(const std::vector<T1>&a, const std::vector<T2>&b)
73  {
74  ASSERT_EQUAL_(a.size(),b.size())
75  const size_t N=a.size();
76  std::vector<T1> ret(N);
77  for (size_t i=0;i<N;i++) ret[i]=a[i]*b[i];
78  return ret;
79  }
80 
81  /** a+=b (element-wise sum) */
82  template <typename T1,typename T2>
83  inline std::vector<T1>& operator +=(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  for (size_t i=0;i<N;i++) a[i]+=b[i];
88  return a;
89  }
90 
91  /** a+=b (sum a constant) */
92  template <typename T1>
93  inline std::vector<T1>& operator +=(std::vector<T1>&a, const T1 b)
94  {
95  const size_t N=a.size();
96  for (size_t i=0;i<N;i++) a[i]+=b;
97  return a;
98  }
99 
100  /** a+b (element-wise sum) */
101  template <typename T1,typename T2>
102  inline std::vector<T1> operator +(const std::vector<T1>&a, const std::vector<T2>&b)
103  {
104  ASSERT_EQUAL_(a.size(),b.size())
105  const size_t N=a.size();
106  std::vector<T1> ret(N);
107  for (size_t i=0;i<N;i++) ret[i]=a[i]+b[i];
108  return ret;
109  }
110 
111  template <typename T1,typename T2>
112  inline std::vector<T1> operator -(const std::vector<T1> &v1, const std::vector<T2>&v2) {
113  ASSERT_EQUAL_(v1.size(),v2.size())
114  std::vector<T1> res(v1.size());
115  for (size_t i=0;i<v1.size();i++) res[i]=v1[i]-v2[i];
116  return res;
117  }
118 
119  /** @} */
120 
121 
122  /** A template function for printing out the contents of a std::vector variable.
123  */
124  template <class T>
125  std::ostream& operator << (std::ostream& out, const std::vector<T> &d)
126  {
127  const std::streamsize old_pre = out.precision();
128  const std::ios_base::fmtflags old_flags = out.flags();
129  out << "[" << std::fixed << std::setprecision(4);
130  copy(d.begin(),d.end(), std::ostream_iterator<T>(out," "));
131  out << "]";
132  out.flags(old_flags);
133  out.precision(old_pre);
134  return out;
135  }
136 
137  /** A template function for printing out the contents of a std::vector variable.
138  */
139  template <class T>
140  std::ostream& operator << (std::ostream& out, std::vector<T> *d)
141  {
142  const std::streamsize old_pre = out.precision();
143  const std::ios_base::fmtflags old_flags = out.flags();
144  out << "[" << std::fixed << std::setprecision(4);
145  copy(d->begin(),d->end(), std::ostream_iterator<T>(out," "));
146  out << "]";
147  out.flags(old_flags);
148  out.precision(old_pre);
149  return out;
150  }
151 
152  /** Binary dump of a CArrayNumeric<T,N> to a stream. */
153  template <typename T,size_t N>
154  mrpt::utils::CStream& operator << (mrpt::utils::CStream& ostrm, const CArrayNumeric<T,N>& a)
155  {
156  ostrm << mrpt::utils::TTypeName< CArrayNumeric<T,N> >::get();
157  if (N) ostrm.WriteBufferFixEndianness<T>(&a[0],N);
158  return ostrm;
159  }
160 
161  /** Binary read of a CArrayNumeric<T,N> from a stream. */
162  template <typename T,size_t N>
164  {
165  static const std::string namExpect = mrpt::utils::TTypeName< CArrayNumeric<T,N> >::get();
166  std::string nam;
167  istrm >> nam;
168  ASSERTMSG_(nam==namExpect, format("Error deserializing: expected '%s', got '%s'", namExpect.c_str(),nam.c_str() ) )
169  if (N) istrm.ReadBufferFixEndianness<T>(&a[0],N);
170  return istrm;
171  }
172 
173 
174  /** @} */ // end of grouping
175 
176  } // End of math namespace
177 
178 } // End of mrpt namespace
179 
180 
181 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013