core/vnl/algo/vnl_matrix_update.h
Go to the documentation of this file.
00001 // This is core/vnl/algo/vnl_matrix_update.h
00002 #ifndef vnl_matrix_update_h_
00003 #define vnl_matrix_update_h_
00004 //:
00005 // \file
00006 // \brief Function to compute M=M+a*b'
00007 // \author  Tim Cootes
00008 
00009 #include <vnl/vnl_vector.h>
00010 #include <vnl/vnl_matrix.h>
00011 #include <vcl_cassert.h>
00012 
00013 //: Perform rank 1 update of M:   M+=(a*b')
00014 //  Requires a.size()==M.rows(),  b.size()==M.columns()
00015 //  \relatesalso vnl_matrix
00016 template<class T>
00017 inline void vnl_matrix_update(vnl_matrix<T>& M,
00018                               const vnl_vector<T>& a,
00019                               const vnl_vector<T>& b)
00020 {
00021   unsigned nr=M.rows();
00022   unsigned nc=M.columns();
00023   assert(a.size()==nr);
00024   assert(b.size()==nc);
00025   T** rows=M.data_array();
00026   for (unsigned i=0;i<nr;++i)
00027   {
00028     // Update row i of M
00029     double ai = a[i];
00030     T* row= rows[i]-1;
00031     const T* b_data=b.data_block()-1;
00032     // Fast loop through elements in row
00033     for (unsigned j=nc;j;--j) row[j] += ai*b_data[j];
00034   }
00035 }
00036 
00037 #endif // vnl_matrix_update_h_
00038