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://www.mrpt.org/                                |
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                 /** \addtogroup container_ops_grp
00045                   *  @{ */
00046 
00047                 /** \name Generic std::vector element-wise operations
00048                   * @{
00049                   */
00050 
00051                 /** a*=b (element-wise multiplication) */
00052                 template <typename T1,typename T2>
00053                 inline std::vector<T1>& operator *=(std::vector<T1>&a, const std::vector<T2>&b)
00054                 {
00055                         ASSERT_EQUAL_(a.size(),b.size())
00056                         const size_t N=a.size();
00057                         for (size_t i=0;i<N;i++) a[i]*=b[i];
00058                         return a;
00059                 }
00060 
00061                 /** a*=k (multiplication by a constant) */
00062                 template <typename T1>
00063                 inline std::vector<T1>& operator *=(std::vector<T1>&a, const T1 b)
00064                 {
00065                         const size_t N=a.size();
00066                         for (size_t i=0;i<N;i++) a[i]*=b;
00067                         return a;
00068                 }
00069 
00070                 /** a*b (element-wise multiplication) */
00071                 template <typename T1,typename T2>
00072                 inline std::vector<T1> operator *(const std::vector<T1>&a, const std::vector<T2>&b)
00073                 {
00074                         ASSERT_EQUAL_(a.size(),b.size())
00075                         const size_t N=a.size();
00076                         std::vector<T1> ret(N);
00077                         for (size_t i=0;i<N;i++) ret[i]=a[i]*b[i];
00078                         return ret;
00079                 }
00080 
00081                 /** a+=b (element-wise sum) */
00082                 template <typename T1,typename T2>
00083                 inline std::vector<T1>& operator +=(std::vector<T1>&a, const std::vector<T2>&b)
00084                 {
00085                         ASSERT_EQUAL_(a.size(),b.size())
00086                         const size_t N=a.size();
00087                         for (size_t i=0;i<N;i++) a[i]+=b[i];
00088                         return a;
00089                 }
00090 
00091                 /** a+=b (sum a constant) */
00092                 template <typename T1>
00093                 inline std::vector<T1>& operator +=(std::vector<T1>&a, const T1 b)
00094                 {
00095                         const size_t N=a.size();
00096                         for (size_t i=0;i<N;i++) a[i]+=b;
00097                         return a;
00098                 }
00099 
00100                 /** a+b (element-wise sum) */
00101                 template <typename T1,typename T2>
00102                 inline std::vector<T1> operator +(const std::vector<T1>&a, const std::vector<T2>&b)
00103                 {
00104                         ASSERT_EQUAL_(a.size(),b.size())
00105                         const size_t N=a.size();
00106                         std::vector<T1> ret(N);
00107                         for (size_t i=0;i<N;i++) ret[i]=a[i]+b[i];
00108                         return ret;
00109                 }
00110 
00111                 template <typename T1,typename T2>
00112                 inline std::vector<T1> operator -(const std::vector<T1> &v1, const std::vector<T2>&v2) {
00113                         ASSERT_EQUAL_(v1.size(),v2.size())
00114                         std::vector<T1> res(v1.size());
00115                         for (size_t i=0;i<v1.size();i++) res[i]=v1[i]-v2[i];
00116                         return res;
00117                 }
00118 
00119                 /** @} */
00120 
00121 
00122                 /** A template function for printing out the contents of a std::vector variable.
00123                         */
00124                 template <class T>
00125                 std::ostream& operator << (std::ostream& out, const std::vector<T> &d)
00126                 {
00127                         const std::streamsize old_pre = out.precision();
00128                         const std::ios_base::fmtflags old_flags = out.flags();
00129                         out << "[" << std::fixed << std::setprecision(4);
00130                         copy(d.begin(),d.end(), std::ostream_iterator<T>(out," "));
00131                         out << "]";
00132                         out.flags(old_flags);
00133                         out.precision(old_pre);
00134                         return out;
00135                 }
00136 
00137                 /** A template function for printing out the contents of a std::vector variable.
00138                         */
00139                 template <class T>
00140                 std::ostream& operator << (std::ostream& out, std::vector<T> *d)
00141                 {
00142                         const std::streamsize old_pre = out.precision();
00143                         const std::ios_base::fmtflags old_flags = out.flags();
00144                         out << "[" << std::fixed << std::setprecision(4);
00145                         copy(d->begin(),d->end(), std::ostream_iterator<T>(out," "));
00146                         out << "]";
00147                         out.flags(old_flags);
00148                         out.precision(old_pre);
00149                         return out;
00150                 }
00151 
00152                 /** Binary dump of a CArrayNumeric<T,N> to a stream. */
00153                 template <typename T,size_t N>
00154                 mrpt::utils::CStream& operator << (mrpt::utils::CStream& ostrm, const CArrayNumeric<T,N>& a)
00155                 {
00156                         ostrm << mrpt::utils::TTypeName< CArrayNumeric<T,N> >::get();
00157                         if (N) ostrm.WriteBufferFixEndianness<T>(&a[0],N);
00158                         return ostrm;
00159                 }
00160 
00161                 /** Binary read of a CArrayNumeric<T,N> from a stream. */
00162                 template <typename T,size_t N>
00163                 mrpt::utils::CStream& operator >> (mrpt::utils::CStream& istrm, CArrayNumeric<T,N>& a)
00164                 {
00165                         static const std::string namExpect = mrpt::utils::TTypeName< CArrayNumeric<T,N> >::get();
00166                         std::string nam;
00167                         istrm >> nam;
00168                         ASSERTMSG_(nam==namExpect, format("Error deserializing: expected '%s', got '%s'", namExpect.c_str(),nam.c_str() ) )
00169                         if (N) istrm.ReadBufferFixEndianness<T>(&a[0],N);
00170                         return istrm;
00171                 }
00172 
00173 
00174                 /**  @} */  // end of grouping
00175 
00176         } // End of math namespace
00177 
00178 } // End of mrpt namespace
00179 
00180 
00181 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011