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 #include <mrpt/math/CVectorTemplate.h>
00034
00035 #include <iterator>
00036
00037 namespace mrpt
00038 {
00039 namespace utils { class CFileStream; }
00040
00041 namespace math
00042 {
00045 template <class T>
00046 std::ostream& operator << (std::ostream& out, const std::vector<T> &d)
00047 {
00048 out << "[";
00049 copy(d.begin(),d.end(), std::ostream_iterator<T>(out," "));
00050 out << "]";
00051 return out;
00052 }
00053
00056 template <class T>
00057 std::ostream& operator << (std::ostream& out, std::vector<T> *d)
00058 {
00059 out << "[";
00060 copy(d->begin(),d->end(), std::ostream_iterator<T>(out," "));
00061 out << "]";
00062 return out;
00063 }
00064
00065
00068 template <class T>
00069 std::vector<T> operator + (const std::vector<T> &a, T b)
00070 {
00071 typename std::vector<T> res(a.size());
00072 typename std::vector<T>::const_iterator it_a;
00073 typename std::vector<T>::iterator it_res;
00074
00075 for (it_a=a.begin(), it_res=res.begin(); it_a!=a.end(); it_a++, it_res++)
00076 *it_res = *it_a + b;
00077
00078 return res;
00079 }
00080
00083 template <class T>
00084 std::vector<T> operator - (const std::vector<T> &a, T b)
00085 {
00086 typename std::vector<T> res(a.size());
00087 typename std::vector<T>::const_iterator it_a;
00088 typename std::vector<T>::iterator it_res;
00089
00090 for (it_a=a.begin(), it_res=res.begin(); it_a!=a.end(); it_a++, it_res++)
00091 *it_res = *it_a - b;
00092
00093 return res;
00094 }
00095
00098 template <class T>
00099 std::vector<T> operator * (const std::vector<T> &a, T b)
00100 {
00101 typename std::vector<T> res(a.size());
00102 typename std::vector<T>::const_iterator it_a;
00103 typename std::vector<T>::iterator it_res;
00104
00105 for (it_a=a.begin(), it_res=res.begin(); it_a!=a.end(); it_a++, it_res++)
00106 *it_res = *it_a * b;
00107
00108 return res;
00109 }
00110
00113 template <class T>
00114 std::vector<T> operator / (const std::vector<T> &a, T b)
00115 {
00116 typename std::vector<T> res(a.size());
00117 typename std::vector<T>::const_iterator it_a;
00118 typename std::vector<T>::iterator it_res;
00119
00120 if (b==0)
00121 THROW_EXCEPTION("Division by zero: execution aborted!");
00122
00123 for (it_a=a.begin(), it_res=res.begin(); it_a!=a.end(); it_a++, it_res++)
00124 *it_res = *it_a / b;
00125
00126 return res;
00127 }
00128
00131 template <class T>
00132 std::vector<T> operator + (const std::vector<T> &a, const std::vector<T> &b)
00133 {
00134 ASSERT_(a.size()==b.size());
00135
00136 typename std::vector<T> res(a.size());
00137 typename std::vector<T>::const_iterator it_a,it_b;
00138 typename std::vector<T>::iterator it_res;
00139
00140 for (it_a=a.begin(), it_b=b.begin(), it_res=res.begin(); it_a!=a.end(); it_a++, it_b++, it_res++)
00141 *it_res = *it_a + *it_b;
00142
00143 return res;
00144 }
00145
00148 template <class T>
00149 std::vector<T> operator - (const std::vector<T> &a, const std::vector<T> &b)
00150 {
00151 ASSERT_(a.size()==b.size());
00152
00153 typename std::vector<T> res(a.size());
00154 typename std::vector<T>::const_iterator it_a,it_b;
00155 typename std::vector<T>::iterator it_res;
00156
00157 for (it_a=a.begin(), it_b=b.begin(), it_res=res.begin(); it_a!=a.end(); it_a++, it_b++, it_res++)
00158 *it_res = *it_a - *it_b;
00159
00160 return res;
00161 }
00162
00165 template <class T>
00166 std::vector<T> operator * (const std::vector<T> &a, const std::vector<T> &b)
00167 {
00168 ASSERT_(a.size()==b.size());
00169 typename std::vector<T> res(a.size());
00170 std::transform(a.begin(), a.end(), b.begin(), res.begin(), std::multiplies<T>() );
00171 return res;
00172 }
00173
00176 template <class T>
00177 std::vector<T> operator / (const std::vector<T> &a, const std::vector<T> &b)
00178 {
00179 ASSERT_(a.size()==b.size());
00180 typename std::vector<T> res(a.size());
00181 std::transform(a.begin(), a.end(), b.begin(), res.begin(), std::divides<T>() );
00182 return res;
00183 }
00184
00187 template <class T>
00188 void operator += (std::vector<T> &a, const std::vector<T> &b)
00189 {
00190 ASSERT_(a.size()==b.size());
00191
00192 typename std::vector<T>::iterator it_a;
00193 typename std::vector<T>::const_iterator it_b;
00194
00195 for (it_a=a.begin(), it_b=b.begin(); it_a!=a.end(); it_a++, it_b++)
00196 *it_a += *it_b;
00197 }
00198
00201 template <class T>
00202 void operator -= (std::vector<T> &a, const std::vector<T> &b)
00203 {
00204 ASSERT_(a.size()==b.size());
00205
00206 typename std::vector<T>::iterator it_a;
00207 typename std::vector<T>::const_iterator it_b;
00208
00209 for (it_a=a.begin(), it_b=b.begin(); it_a!=a.end(); it_a++, it_b++)
00210 *it_a -= *it_b;
00211 }
00212
00215 template <class T>
00216 void operator *= (std::vector<T> &a, const std::vector<T> &b)
00217 {
00218 ASSERT_(a.size()==b.size());
00219
00220 typename std::vector<T>::iterator it_a;
00221 typename std::vector<T>::const_iterator it_b;
00222
00223 for (it_a=a.begin(), it_b=b.begin(); it_a!=a.end(); it_a++, it_b++)
00224 *it_a *= *it_b;
00225 }
00226
00229 template <class T>
00230 void operator /= (std::vector<T> &a, const std::vector<T> &b)
00231 {
00232 ASSERT_(a.size()==b.size());
00233
00234 typename std::vector<T>::iterator it_a;
00235 typename std::vector<T>::const_iterator it_b;
00236
00237 for (it_a=a.begin(), it_b=b.begin(); it_a!=a.end(); it_a++, it_b++)
00238 {
00239 if (*it_b==0)
00240 THROW_EXCEPTION("Division by zero: execution aborted!")
00241 *it_a /= *it_b;
00242 }
00243 }
00244
00247 template <class T>
00248 void operator += (std::vector<T> &a, T b)
00249 {
00250 for (typename std::vector<T>::iterator it=a.begin();it!=a.end();it++) *it += b;
00251 }
00252
00255 template <class T>
00256 void operator -= (std::vector<T> &a, T b)
00257 {
00258 for (typename std::vector<T>::iterator it=a.begin();it!=a.end();it++) *it -= b;
00259 }
00260
00263 template <class T>
00264 void operator *= (std::vector<T> &a, T b)
00265 {
00266 for (typename std::vector<T>::iterator it=a.begin();it!=a.end();it++) *it *= b;
00267 }
00268
00271 template <class T>
00272 void operator /= (std::vector<T> &a, T b)
00273 {
00274 if (b==0) THROW_EXCEPTION("Division by zero: execution aborted!")
00275 for (typename std::vector<T>::iterator it=a.begin();it!=a.end();it++) *it /= b;
00276 }
00277
00278 }
00279
00280 }
00281
00282
00283 #endif