Main MRPT website > C++ reference
MRPT logo

ops_vectors.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  mrpt_math_vector_ops_H
00029 #define  mrpt_math_vector_ops_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/math/CMatrixTemplateNumeric.h>
00033 
00034 // Many of the functions originally in this file are now in ops_containers.h
00035 #include <mrpt/math/ops_containers.h>
00036 
00037 
00038 namespace mrpt
00039 {
00040         namespace utils { class CFileStream; }
00041 
00042         namespace math
00043         {
00044 
00045                 /** \name Generic std::vector element-wise operations
00046                   * @{
00047                   */
00048 
00049                 /** a*=b (element-wise multiplication) */
00050                 template <typename T1,typename T2>
00051                 inline std::vector<T1>& operator *=(std::vector<T1>&a, const std::vector<T2>&b)
00052                 {
00053                         ASSERT_EQUAL_(a.size(),b.size())
00054                         const size_t N=a.size();
00055                         for (size_t i=0;i<N;i++) a[i]*=b[i];
00056                         return a;
00057                 }
00058 
00059                 /** a*=k (multiplication by a constant) */
00060                 template <typename T1>
00061                 inline std::vector<T1>& operator *=(std::vector<T1>&a, const T1 b)
00062                 {
00063                         const size_t N=a.size();
00064                         for (size_t i=0;i<N;i++) a[i]*=b;
00065                         return a;
00066                 }
00067 
00068                 /** a*b (element-wise multiplication) */
00069                 template <typename T1,typename T2>
00070                 inline std::vector<T1> operator *(const std::vector<T1>&a, const std::vector<T2>&b)
00071                 {
00072                         ASSERT_EQUAL_(a.size(),b.size())
00073                         const size_t N=a.size();
00074                         std::vector<T1> ret(N);
00075                         for (size_t i=0;i<N;i++) ret[i]=a[i]*b[i];
00076                         return ret;
00077                 }
00078 
00079                 /** a+=b (element-wise sum) */
00080                 template <typename T1,typename T2>
00081                 inline std::vector<T1>& operator +=(std::vector<T1>&a, const std::vector<T2>&b)
00082                 {
00083                         ASSERT_EQUAL_(a.size(),b.size())
00084                         const size_t N=a.size();
00085                         for (size_t i=0;i<N;i++) a[i]+=b[i];
00086                         return a;
00087                 }
00088 
00089                 /** a+=b (sum a constant) */
00090                 template <typename T1>
00091                 inline std::vector<T1>& operator +=(std::vector<T1>&a, const T1 b)
00092                 {
00093                         const size_t N=a.size();
00094                         for (size_t i=0;i<N;i++) a[i]+=b;
00095                         return a;
00096                 }
00097 
00098                 /** a+b (element-wise sum) */
00099                 template <typename T1,typename T2>
00100                 inline std::vector<T1> operator +(const std::vector<T1>&a, const std::vector<T2>&b)
00101                 {
00102                         ASSERT_EQUAL_(a.size(),b.size())
00103                         const size_t N=a.size();
00104                         std::vector<T1> ret(N);
00105                         for (size_t i=0;i<N;i++) ret[i]=a[i]+b[i];
00106                         return ret;
00107                 }
00108 
00109                 template <typename T1,typename T2>
00110                 inline std::vector<T1> operator -(const std::vector<T1> &v1, const std::vector<T2>&v2) {
00111                         ASSERT_EQUAL_(v1.size(),v2.size())
00112                         std::vector<T1> res(v1.size());
00113                         for (size_t i=0;i<v1.size();i++) res[i]=v1[i]-v2[i];
00114                         return res;
00115                 }
00116 
00117                 /** @} */
00118 
00119 
00120                 /** A template function for printing out the contents of a std::vector variable.
00121                         */
00122                 template <class T>
00123                 std::ostream& operator << (std::ostream& out, const std::vector<T> &d)
00124                 {
00125                         const std::streamsize old_pre = out.precision();
00126                         const std::ios_base::fmtflags old_flags = out.flags();
00127                         out << "[" << std::fixed << std::setprecision(4);
00128                         copy(d.begin(),d.end(), std::ostream_iterator<T>(out," "));
00129                         out << "]";
00130                         out.flags(old_flags);
00131                         out.precision(old_pre);
00132                         return out;
00133                 }
00134 
00135                 /** A template function for printing out the contents of a std::vector variable.
00136                         */
00137                 template <class T>
00138                 std::ostream& operator << (std::ostream& out, std::vector<T> *d)
00139                 {
00140                         const std::streamsize old_pre = out.precision();
00141                         const std::ios_base::fmtflags old_flags = out.flags();
00142                         out << "[" << std::fixed << std::setprecision(4);
00143                         copy(d->begin(),d->end(), std::ostream_iterator<T>(out," "));
00144                         out << "]";
00145                         out.flags(old_flags);
00146                         out.precision(old_pre);
00147                         return out;
00148                 }
00149 
00150                 /** Binary dump of a CArrayNumeric<T,N> to a stream. */
00151                 template <typename T,size_t N>
00152                 mrpt::utils::CStream& operator << (mrpt::utils::CStream& ostrm, const CArrayNumeric<T,N>& a)
00153                 {
00154                         ostrm << mrpt::utils::TTypeName< CArrayNumeric<T,N> >::get();
00155                         if (N) ostrm.WriteBufferFixEndianness<T>(&a[0],N);
00156                         return ostrm;
00157                 }
00158 
00159                 /** Binary read of a CArrayNumeric<T,N> from a stream. */
00160                 template <typename T,size_t N>
00161                 mrpt::utils::CStream& operator >> (mrpt::utils::CStream& istrm, CArrayNumeric<T,N>& a)
00162                 {
00163                         static const std::string namExpect = mrpt::utils::TTypeName< CArrayNumeric<T,N> >::get();
00164                         std::string nam;
00165                         istrm >> nam;
00166                         ASSERTMSG_(nam==namExpect, format("Error deserializing: expected '%s', got '%s'", namExpect.c_str(),nam.c_str() ) )
00167                         if (N) istrm.ReadBufferFixEndianness<T>(&a[0],N);
00168                         return istrm;
00169                 }
00170 
00171 
00172         } // End of math namespace
00173 
00174 } // End of mrpt namespace
00175 
00176 
00177 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:16:28 UTC 2011