Changeset 90 for foam/trunk/vision/src/Matrix.h
 Timestamp:
 07/22/2009 10:59:15 AM (11 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

foam/trunk/vision/src/Matrix.h
r89 r90 99 99 void Zero() { SetAll(0); } 100 100 bool IsInf(); 101 Matrix Transposed() ;101 Matrix Transposed() const; 102 102 103 103 Matrix &operator=(const Matrix &other); … … 105 105 Matrix operator(const Matrix &other) const; 106 106 Matrix operator*(const Matrix &other) const; 107 Vector<T> operator*(const Vector<T> &other) const; 108 Vector<T> VecMulTransposed(const Vector<T> &other) const; 107 109 Matrix &operator+=(const Matrix &other); 108 110 Matrix &operator=(const Matrix &other); 109 111 Matrix &operator*=(const Matrix &other); 110 112 111 113 void SortRows(Vector<T> &v); 112 114 void SortCols(Vector<T> &v); 115 116 Matrix CropRows(unsigned int s, unsigned int e); 117 Matrix CropCols(unsigned int s, unsigned int e); 118 119 void Save(FILE *f); 120 void Load(FILE *f); 113 121 114 122 static void RunTests(); … … 218 226 219 227 template<class T> 220 Matrix<T> Matrix<T>::Transposed() 228 Matrix<T> Matrix<T>::Transposed() const 221 229 { 222 230 Matrix<T> copy(*this); … … 281 289 { 282 290 ret[i][j]+=(*this)[i][k]*other[k][j]; 291 } 292 } 293 } 294 return ret; 295 } 296 297 template<class T> 298 Vector<T> Matrix<T>::operator*(const Vector<T> &other) const 299 { 300 assert(m_Cols==other.Size()); 301 302 Vector<T> ret(m_Rows); 303 304 for (unsigned int i=0; i<m_Rows; i++) 305 { 306 for (unsigned int j=0; j<other.Size(); j++) 307 { 308 ret[i]=0; 309 for (unsigned int k=0; k<m_Cols; k++) 310 { 311 ret[i]+=(*this)[i][k]*other[k]; 312 } 313 } 314 } 315 return ret; 316 } 317 318 template<class T> 319 Vector<T> Matrix<T>::VecMulTransposed(const Vector<T> &other) const 320 { 321 assert(m_Rows==other.Size()); 322 323 Vector<T> ret(m_Cols); 324 325 for (unsigned int i=0; i<m_Cols; i++) 326 { 327 for (unsigned int j=0; j<other.Size(); j++) 328 { 329 ret[i]=0; 330 for (unsigned int k=0; k<m_Rows; k++) 331 { 332 ret[i]+=(*this)[k][i]*other[k]; 283 333 } 284 334 } … … 411 461 } 412 462 } 413 463 464 template<class T> 465 Matrix<T> Matrix<T>::CropRows(unsigned int s, unsigned int e) 466 { 467 assert(s<e); 468 assert(s<m_Rows); 469 assert(e<=m_Rows); 470 471 Matrix r(es,m_Cols); 472 unsigned int c=0; 473 for(unsigned int i=s; i<e; i++) 474 { 475 r.SetRowVector(c,GetRowVector(i)); 476 c++; 477 } 478 479 return r; 480 } 481 482 template<class T> 483 Matrix<T> Matrix<T>::CropCols(unsigned int s, unsigned int e) 484 { 485 assert(s<e); 486 assert(s<m_Cols); 487 assert(e<=m_Cols); 488 489 Matrix r(m_Rows,es); 490 unsigned int c=0; 491 for(unsigned int i=s; i<e; i++) 492 { 493 r.SetColVector(c,GetColVector(i)); 494 c++; 495 } 496 497 return r; 498 } 499 500 template<class T> 501 void Matrix<T>::Save(FILE* f) 502 { 503 int version = 1; 504 fwrite(&version,1,sizeof(version),f); 505 fwrite(&m_Rows,1,sizeof(m_Rows),f); 506 fwrite(&m_Cols,1,sizeof(m_Cols),f); 507 fwrite(m_Data,1,sizeof(T)*m_Rows*m_Cols,f); 508 } 509 510 template<class T> 511 void Matrix<T>::Load(FILE* f) 512 { 513 int version; 514 fread(&version,sizeof(version),1,f); 515 fread(&m_Rows,sizeof(m_Rows),1,f); 516 fread(&m_Cols,sizeof(m_Cols),1,f); 517 m_Data=new T[m_Rows*m_Cols]; 518 fread(m_Data,sizeof(T)*m_Rows*m_Cols,1,f); 519 } 414 520 415 521 template<class T> 416 522 void Matrix<T>::RunTests() 417 523 { 524 Vector<T>::RunTests(); 525 418 526 Matrix<T> m(10,10); 419 527 m.SetAll(0); … … 432 540 Matrix<T> c=a*b; 433 541 assert(c[0][0]==11 && c[1][0]==29); 542 543 // test matrix * vector 544 Vector<T> d(3); 545 d[0]=3; 546 d[1]=1; 547 d[2]=2; 548 Vector<T> e=a*d; 549 assert(e[0]==11 && e[1]==29); 550 551 Matrix<T> f=a.CropCols(1,3); 552 assert(f.GetRows()==2 && f.GetCols()==2 && f[0][0]==2); 553 Matrix<T> g=a.CropRows(0,1); 554 assert(g.GetRows()==1 && g.GetCols()==3 && g[0][0]==1); 555 434 556 } 435 557
Note: See TracChangeset
for help on using the changeset viewer.