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 CVectorTemplate_H
00029 #define CVectorTemplate_H
00030
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/math/CMatrixTemplateNumeric.h>
00033
00034 namespace mrpt
00035 {
00036 namespace math
00037 {
00049 template <class T>
00050 class CVectorTemplate : public std::vector<T>
00051 {
00052
00053 #if defined(_MSC_VER) && (_MSC_VER==1200) // Special for MSVC6
00054 #define base_class vector<T>
00055 #else
00056 typedef std::vector<T> base_class;
00057 #endif
00058
00059 public:
00060
00063 CVectorTemplate(size_t creationSize=0) : std::vector<T>(creationSize)
00064 {
00065 }
00066
00069 CVectorTemplate(size_t creationSize, const T& initVal) : std::vector<T>(creationSize,initVal)
00070 {
00071 }
00072
00073 virtual ~CVectorTemplate() { }
00074
00078 void extract_vector(const size_t &index, CVectorTemplate<T> &out)
00079 {
00080 size_t i;
00081
00082 if (index + out.size() > base_class::size())
00083 THROW_EXCEPTION("extract_vector: Index out of bounds");
00084
00085 for (i=index;i<index+out.size();i++)
00086 out[i-index] = (*this)[i];
00087 }
00088
00092 CVectorTemplate<T> extract_vector(const size_t index, const unsigned int length)
00093 {
00094 if (index + length > base_class::size())
00095 THROW_EXCEPTION("extract_vector: Index out of bounds");
00096
00097 size_t i;
00098 CVectorTemplate<T> out;
00099 out.resize(length);
00100
00101 for (i=index;i<index+length;i++)
00102 out[i-index] = (*this)[i];
00103
00104 return out;
00105 }
00106
00110 void insert_vector(const size_t &index, const CVectorTemplate<T> &in)
00111 {
00112 size_t i;
00113
00114 if (index + in.size()>base_class::size())
00115 THROW_EXCEPTION("insert_vector: Index out of bounds");
00116
00117 for (i=index;i<index+in.size();i++)
00118 (*this)[i] = in[i-index];
00119 }
00120
00121
00124 void operator +=(const CMatrixTemplateNumeric<T> &M)
00125 {
00126 MRPT_TRY_START
00127 if (M.getColCount()!=1 && M.getRowCount()!=1)
00128 THROW_EXCEPTION("Matrix must be a column or row matrix!");
00129
00130 if (M.getColCount()==1)
00131 {
00132
00133 ASSERT_(M.getRowCount()== base_class::size())
00134 size_t i,N = base_class::size();
00135
00136 for (i=0;i<N;i++)
00137 (*this)[i] += M(i,0);
00138 }
00139 else
00140 {
00141
00142 ASSERT_(M.getColCount()==base_class::size())
00143 size_t i,N = base_class::size();
00144
00145 for (i=0;i<N;i++)
00146 (*this)[i] += M(0,i);
00147 }
00148
00149 MRPT_TRY_END
00150 }
00151
00155 void concatenate(const CVectorTemplate &first, const CVectorTemplate &second)
00156 {
00157 size_t i;
00158 (*this).resize(first.size()+second.size());
00159 for (i=0;i<first.size();i++)
00160 (*this)[i] = first[i];
00161 for (i=0;i<second.size();i++)
00162 std::vector<T>::at(i+first.size()) = second[i];
00163 }
00164
00165
00169 void deconcatenate(CVectorTemplate &first, CVectorTemplate &second, const size_t &index)
00170 {
00171 if (index>base_class::size())
00172 THROW_EXCEPTION("Error in CVectorTemplate::deconcatenate. Index out of bounds");
00173
00174 size_t i;
00175
00176 first.resize(index);
00177 second.resize(base_class::size()-index);
00178 for (i=0;i<index;i++)
00179 first[i] = (*this)[i];
00180 for (i=0;i<second.size();i++)
00181 second[i] = std::vector<T>::at(i+index);
00182 }
00183
00184
00188 void find_max(size_t &index, T &val)
00189 {
00190 if (base_class::size()<1)
00191 THROW_EXCEPTION("vector without dimensions in CVectorTemplate::find_max");
00192
00193 val=std::vector<T>::at(0);
00194 index=0;
00195 for (size_t i=1; i<base_class::size();i++)
00196 {
00197 if (val<(*this)[i])
00198 {
00199 val=(*this)[i];
00200 index = i;
00201 }
00202 }
00203 }
00204
00208 void find_min(size_t &index, T &val)
00209 {
00210 if (base_class::size()<1)
00211 THROW_EXCEPTION("vector without dimensions in CVectorTemplate::find_max");
00212
00213 val=(*this)[0];
00214 index=0;
00215 for (size_t i=1; i<base_class::size();i++)
00216 {
00217 if (val>(*this)[i])
00218 {
00219 val=(*this)[i];
00220 index = i;
00221 }
00222 }
00223 }
00224
00225
00229 void find_min_max(size_t &index_min, size_t &index_max, T &min, T &max)
00230 {
00231 if (base_class::size()<1)
00232 THROW_EXCEPTION("vector without dimensions in CVectorTemplate::find_max");
00233
00234 min=(*this)[0];
00235 max=(*this)[0];
00236 index_min=0;
00237 index_max=0;
00238 for (size_t i=1; i<base_class::size();i++)
00239 {
00240 if (min>(*this)[i])
00241 {
00242 min=(*this)[i];
00243 index_min = i;
00244 }
00245 else if (max<(*this)[i])
00246 {
00247 max=(*this)[i];
00248 index_max = i;
00249 }
00250 }
00251 }
00252
00256 void abs()
00257 {
00258 for (size_t i=0; i<base_class::size();i++)
00259 if ((*this)[i] < 0)
00260 (*this)[i] *= -1;
00261
00262 }
00263
00267 void loadFromTextFile(const std::string &file)
00268 {
00269 CMatrixTemplateNumeric<T> aux;
00270 aux.loadFromTextFile(file);
00271 unsigned int row = aux.getRowCount();
00272 unsigned int col = aux.getColCount();
00273 if ((row!=1)&&(col!=1))
00274 THROW_EXCEPTION("Error loading vector from text file, isn't a vector");
00275
00276 if (row==1)
00277 {
00278 (*this).resize(col);
00279 for (size_t i=0;i<col;i++)
00280 (*this)[i] = aux(0,i);
00281 }
00282 else
00283 {
00284 (*this).resize(row);
00285 for (size_t j=0;j<row;j++)
00286 (*this)[j] = aux(j,0);
00287 }
00288 }
00289
00290
00294 void saveToTextFile(const std::string &file)
00295 {
00296 CMatrixTemplateNumeric<T> aux(1,base_class::size());
00297 for (size_t i=0;i<base_class::size();i++)
00298 aux(0,i) = (*this)[i];
00299 aux.saveToTextFile(file);
00300 }
00301
00305 T mean()
00306 {
00307 T sum=0.0;
00308 for (size_t i=0;i<base_class::size();i++)
00309 sum += (*this)[i];
00310 return sum/base_class::size();
00311 }
00312
00316 CMatrixTemplateNumeric<T> likeMatrix()
00317 {
00318 CMatrixTemplateNumeric<T> out;
00319 out.setSize(1,base_class::size());
00320 for( size_t i = 0; i < base_class::size() ; i++)
00321 out(0,i) = (*this)[i];
00322 return out;
00323 }
00324
00327 CVectorTemplate<T> operator + (CVectorTemplate &b)
00328 {
00329 ASSERT_(this->size()==b.size());
00330
00331 CVectorTemplate<T> res; res.assign(this->size(),0);
00332 typename std::vector<T>::iterator it_a,it_b,it_res;
00333
00334 for (it_a=this->begin(), it_b=b.begin(), it_res=res.begin(); it_a!=this->end(); it_a++, it_b++, it_res++)
00335 *it_res = *it_a + *it_b;
00336 return res;
00337 }
00338
00339
00342 CVectorTemplate<T> operator - (CVectorTemplate &b)
00343 {
00344 ASSERT_(this->size()==b.size());
00345
00346 CVectorTemplate<T> res; res.assign(this->size(),0);
00347 typename std::vector<T>::iterator it_a,it_b,it_res;
00348
00349 for (it_a=this->begin(), it_b=b.begin(), it_res=res.begin(); it_a!=this->end(); it_a++, it_b++, it_res++)
00350 *it_res = *it_a - *it_b;
00351 return res;
00352 }
00353
00354
00357 CVectorTemplate<T> operator * (CVectorTemplate &b)
00358 {
00359 ASSERT_(this->size()==b.size());
00360
00361 CVectorTemplate<T> res; res.assign(this->size(),0);
00362 typename std::vector<T>::iterator it_a,it_b,it_res;
00363
00364 for (it_a=this->begin(), it_b=b.begin(), it_res=res.begin(); it_a!=this->end(); it_a++, it_b++, it_res++)
00365 *it_res = (*it_a) * (*it_b);
00366 return res;
00367 }
00368
00369
00372 CVectorTemplate<T> operator / (CVectorTemplate &b)
00373 {
00374 ASSERT_(this->size()==b.size());
00375
00376 CVectorTemplate<T> res; res.assign(this->size(),0);
00377 typename std::vector<T>::iterator it_a,it_b,it_res;
00378
00379 for (it_a=this->begin(), it_b=b.begin(), it_res=res.begin(); it_a!=this->end(); it_a++, it_b++, it_res++)
00380 *it_res = (*it_a) / (*it_b);
00381 return res;
00382 }
00383
00384
00387 CVectorTemplate<T> operator + (T b)
00388 {
00389
00390 CVectorTemplate<T> res; res.assign(this->size(),0);
00391 typename std::vector<T>::iterator it_a,it_res;
00392
00393 for (it_a=this->begin(), it_res=res.begin(); it_a!=this->end(); it_a++, it_res++)
00394 *it_res = (*it_a) + b;
00395 return res;
00396 }
00397
00400 CVectorTemplate<T> operator - (T b)
00401 {
00402
00403 CVectorTemplate<T> res; res.assign(this->size(),0);
00404 typename std::vector<T>::iterator it_a,it_res;
00405
00406 for (it_a=this->begin(), it_res=res.begin(); it_a!=this->end(); it_a++, it_res++)
00407 *it_res = (*it_a) - b;
00408 return res;
00409 }
00410
00413 CVectorTemplate<T> operator * (T b)
00414 {
00415
00416 CVectorTemplate<T> res; res.assign(this->size(),0);
00417 typename std::vector<T>::iterator it_a,it_res;
00418
00419 for (it_a=this->begin(), it_res=res.begin(); it_a!=this->end(); it_a++, it_res++)
00420 *it_res = (*it_a) * b;
00421 return res;
00422 }
00423
00426 CVectorTemplate<T> operator / (T b)
00427 {
00428
00429 CVectorTemplate<T> res; res.assign(this->size(),0);
00430 typename std::vector<T>::iterator it_a,it_res;
00431
00432 for (it_a=this->begin(), it_res=res.begin(); it_a!=this->end(); it_a++, it_res++)
00433 *it_res = (*it_a) / b;
00434 return res;
00435 }
00436
00439 CMatrixTemplateNumeric<T> operator ~ ()
00440 {
00441 CMatrixTemplateNumeric<T> temp(base_class::size(),1);
00442 for (size_t i=0; i < base_class::size(); i++)
00443 temp(i,0) = (*this)[i];
00444 return temp;
00445 }
00446
00447 };
00448
00449
00453 typedef CVectorTemplate<float> CVectorFloat;
00454
00458 typedef CVectorTemplate<double> CVectorDouble;
00459
00460
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477 }
00478 }
00479
00480 #endif