Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
00046
00047
00048
00049
00050
00051
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
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
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
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
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
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
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
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
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
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
00175
00176 }
00177
00178 }
00179
00180
00181 #endif