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 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
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
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
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
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
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
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
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
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
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 }
00173
00174 }
00175
00176
00177 #endif