core/vnl/vnl_matrix_fixed_ref.txx

Go to the documentation of this file.
00001 // This is core/vnl/vnl_matrix_fixed_ref.txx
00002 #ifndef vnl_matrix_fixed_ref_txx_
00003 #define vnl_matrix_fixed_ref_txx_
00004 
00005 #include "vnl_matrix_fixed_ref.h"
00006 //:
00007 // \file
00008 
00009 #include <vcl_cmath.h>
00010 #include <vcl_iostream.h>
00011 #include <vcl_cstdlib.h> // for abort
00012 #include <vcl_cassert.h>
00013 
00014 #include <vnl/vnl_error.h>
00015 #include <vnl/vnl_math.h>
00016 
00017 #if 0 // commented out
00018 template<class T, unsigned nrows, unsigned ncols>
00019 void
00020 vnl_matrix_fixed_ref_const<T,nrows,ncols>::add( const T* a, const T* b, T* r )
00021 {
00022   unsigned int count = nrows*ncols;
00023   while ( count-- )
00024     *(r++) = *(a++) + *(b++);
00025 }
00026 
00027 
00028 template<class T, unsigned nrows, unsigned ncols>
00029 void
00030 vnl_matrix_fixed_ref_const<T,nrows,ncols>::add( const T* a, T b, T* r )
00031 {
00032   unsigned int count = nrows*ncols;
00033   while ( count-- )
00034     *(r++) = *(a++) + b;
00035 }
00036 
00037 template<class T, unsigned nrows, unsigned ncols>
00038 void
00039 vnl_matrix_fixed_ref_const<T,nrows,ncols>::sub( const T* a, const T* b, T* r )
00040 {
00041   unsigned int count = nrows*ncols;
00042   while ( count-- )
00043     *(r++) = *(a++) - *(b++);
00044 }
00045 
00046 template<class T, unsigned nrows, unsigned ncols>
00047 void
00048 vnl_matrix_fixed_ref_const<T,nrows,ncols>::sub( const T* a, T b, T* r )
00049 {
00050   unsigned int count = nrows*ncols;
00051   while ( count-- )
00052     *(r++) = *(a++) - b;
00053 }
00054 
00055 template<class T, unsigned nrows, unsigned ncols>
00056 void
00057 vnl_matrix_fixed_ref_const<T,nrows,ncols>::sub( T a, const T* b, T* r )
00058 {
00059   unsigned int count = nrows*ncols;
00060   while ( count-- )
00061     *(r++) = a - *(b++);
00062 }
00063 
00064 template<class T, unsigned nrows, unsigned ncols>
00065 void
00066 vnl_matrix_fixed_ref_const<T,nrows,ncols>::mul( const T* a, const T* b, T* r )
00067 {
00068   unsigned int count = nrows*ncols;
00069   while ( count-- )
00070     *(r++) = *(a++) * *(b++);
00071 }
00072 
00073 template<class T, unsigned nrows, unsigned ncols>
00074 void
00075 vnl_matrix_fixed_ref_const<T,nrows,ncols>::mul( const T* a, T b, T* r )
00076 {
00077   unsigned int count = nrows*ncols;
00078   while ( count-- )
00079     *(r++) = *(a++) * b;
00080 }
00081 
00082 template<class T, unsigned nrows, unsigned ncols>
00083 void
00084 vnl_matrix_fixed_ref_const<T,nrows,ncols>::div( const T* a, const T* b, T* r )
00085 {
00086   unsigned int count = nrows*ncols;
00087   while ( count-- )
00088     *(r++) = *(a++) / *(b++);
00089 }
00090 
00091 template<class T, unsigned nrows, unsigned ncols>
00092 void
00093 vnl_matrix_fixed_ref_const<T,nrows,ncols>::div( const T* a, T b, T* r )
00094 {
00095   unsigned int count = nrows*ncols;
00096   while ( count-- )
00097     *(r++) = *(a++) / b;
00098 }
00099 
00100 template<class T, unsigned nrows, unsigned ncols>
00101 bool
00102 vnl_matrix_fixed_ref_const<T,nrows,ncols>::equal( const T* a, const T* b )
00103 {
00104   unsigned int count = nrows*ncols;
00105   while ( count-- )
00106     if ( *(a++) != *(b++) )  return false;
00107   return true;
00108 }
00109 #endif // 0
00110 
00111 //------------------------------------------------------------
00112 
00113 
00114 template<class T, unsigned nrows, unsigned ncols>
00115 void
00116 vnl_matrix_fixed_ref<T,nrows,ncols>::fill (T value) const
00117 {
00118   for (unsigned int i = 0; i < nrows; i++)
00119     for (unsigned int j = 0; j < ncols; j++)
00120       (*this)(i,j) = value;
00121 }
00122 
00123 
00124 template<class T, unsigned nrows, unsigned ncols>
00125 void
00126 vnl_matrix_fixed_ref<T,nrows,ncols>::fill_diagonal (T value) const
00127 {
00128   for (unsigned int i = 0; i < nrows && i < ncols; i++)
00129     (*this)(i,i) = value;
00130 }
00131 
00132 
00133 template<class T, unsigned nrows, unsigned ncols>
00134 void
00135 vnl_matrix_fixed_ref_const<T,nrows,ncols>::print(vcl_ostream& os) const
00136 {
00137   for (unsigned int i = 0; i < nrows; i++)
00138   {
00139     os << (*this)(i,0);
00140     for (unsigned int j = 1; j < ncols; j++)
00141       os << ' ' << (*this)(i,j);
00142     os << '\n';
00143   }
00144 }
00145 
00146 
00147 template <class T, unsigned nrows, unsigned ncols>
00148 vnl_matrix_fixed<T,nrows,ncols>
00149 vnl_matrix_fixed_ref_const<T,nrows,ncols>::apply(T (*f)(T const&)) const
00150 {
00151   vnl_matrix_fixed<T,nrows,ncols> ret;
00152   vnl_c_vector<T>::apply(this->begin(), rows()*cols(), f, ret.data_block());
00153   return ret;
00154 }
00155 
00156 template <class T, unsigned nrows, unsigned ncols>
00157 vnl_matrix_fixed<T,nrows,ncols>
00158 vnl_matrix_fixed_ref_const<T,nrows,ncols>::apply(T (*f)(T)) const
00159 {
00160   vnl_matrix_fixed<T,nrows,ncols> ret;
00161   vnl_c_vector<T>::apply(this->begin(), rows()*cols(), f, ret.data_block());
00162   return ret;
00163 }
00164 
00165 ////--------------------------- Additions------------------------------------
00166 
00167 
00168 template<class T, unsigned nrows, unsigned ncols>
00169 vnl_matrix_fixed<T,ncols,nrows>
00170 vnl_matrix_fixed_ref_const<T,nrows,ncols>::transpose() const
00171 {
00172   vnl_matrix_fixed<T,ncols,nrows> result;
00173   for (unsigned int i = 0; i < cols(); i++)
00174     for (unsigned int j = 0; j < rows(); j++)
00175       result(i,j) = (*this)(j,i);
00176   return result;
00177 }
00178 
00179 template<class T, unsigned nrows, unsigned ncols>
00180 vnl_matrix_fixed<T,ncols,nrows>
00181 vnl_matrix_fixed_ref_const<T,nrows,ncols>::conjugate_transpose() const
00182 {
00183   vnl_matrix_fixed<T,ncols,nrows> result(transpose());
00184   vnl_c_vector<T>::conjugate(result.begin(),  // src
00185                              result.begin(),  // dst
00186                              result.size());  // size of block
00187   return result;
00188 }
00189 
00190 template<class T, unsigned nrows, unsigned ncols>
00191 vnl_matrix_fixed_ref<T,nrows,ncols> const&
00192 vnl_matrix_fixed_ref<T,nrows,ncols>::update (vnl_matrix<T> const& m,
00193                                              unsigned top, unsigned left) const
00194 {
00195   const unsigned int bottom = top + m.rows();
00196   const unsigned int right = left + m.cols();
00197 #ifndef NDEBUG
00198   if (nrows < bottom || ncols < right)
00199     vnl_error_matrix_dimension ("update",
00200                                 bottom, right, m.rows(), m.cols());
00201 #endif
00202   for (unsigned int i = top; i < bottom; i++)
00203     for (unsigned int j = left; j < right; j++)
00204       (*this)(i,j) = m(i-top,j-left);
00205   return *this;
00206 }
00207 
00208 
00209 template<class T, unsigned nrows, unsigned ncols>
00210 vnl_matrix<T>
00211 vnl_matrix_fixed_ref_const<T,nrows,ncols>::extract (unsigned rowz, unsigned colz,
00212                                                     unsigned top, unsigned left) const
00213 {
00214 #ifndef NDEBUG
00215   unsigned int bottom = top + rowz;
00216   unsigned int right = left + colz;
00217   if ((nrows < bottom) || (ncols < right))
00218     vnl_error_matrix_dimension ("extract",
00219                                 nrows, ncols, bottom, right);
00220 #endif
00221   vnl_matrix<T> result(rowz, colz);
00222   for (unsigned int i = 0; i < rowz; i++)      // actual copy of all elements
00223     for (unsigned int j = 0; j < colz; j++)    // in submatrix
00224       result(i,j) = (*this)(top+i,left+j);
00225   return result;
00226 }
00227 
00228 
00229 template<class T, unsigned nrows, unsigned ncols>
00230 void
00231 vnl_matrix_fixed_ref<T,nrows,ncols>::copy_in(T const *p) const
00232 {
00233   T* dp = this->data_block();
00234   unsigned int i = nrows * ncols;
00235   while (i--)
00236     *dp++ = *p++;
00237 }
00238 
00239 template<class T, unsigned nrows, unsigned ncols>
00240 void vnl_matrix_fixed_ref_const<T,nrows,ncols>::copy_out(T *p) const
00241 {
00242   T const* dp = this->data_block();
00243   unsigned int i = nrows*ncols;
00244   while (i--)
00245     *p++ = *dp++;
00246 }
00247 
00248 template<class T, unsigned nrows, unsigned ncols>
00249 void
00250 vnl_matrix_fixed_ref<T,nrows,ncols>::set_identity() const
00251 {
00252 #ifndef NDEBUG
00253   if (nrows != ncols) // Size?
00254     vnl_error_matrix_nonsquare ("set_identity");
00255 #endif
00256   // Two simple loops are generally better than having a branch inside
00257   // the loop. Probably worth the O(n) extra writes.
00258   for (unsigned int i = 0; i < nrows; i++)
00259     for (unsigned int j = 0; j < ncols; j++)
00260         (*this)(i,j) = T(0);
00261   for (unsigned int i = 0; i < nrows; i++)
00262     (*this)(i,i) = T(1);
00263 }
00264 
00265 //: Make each row of the matrix have unit norm.
00266 // All-zero rows are ignored.
00267 template<class T, unsigned nrows, unsigned ncols>
00268 void
00269 vnl_matrix_fixed_ref<T,nrows,ncols>::normalize_rows() const
00270 {
00271   typedef typename vnl_numeric_traits<T>::abs_t Abs_t;
00272   for (unsigned int i = 0; i < nrows; i++)
00273   {
00274     Abs_t norm(0); // double will not do for all types.
00275     for (unsigned int j = 0; j < ncols; j++)
00276       norm += vnl_math_squared_magnitude( (*this)(i,j) );
00277 
00278     if (norm != 0)
00279     {
00280       typedef typename vnl_numeric_traits<Abs_t>::real_t real_t;
00281       real_t scale = real_t(1)/vcl_sqrt((real_t)norm);
00282       for (unsigned int j = 0; j < ncols; j++)
00283       {
00284         // FIXME need correct rounding here
00285         // There is e.g. no *standard* operator*=(complex<float>, double), hence the T() cast.
00286         (*this)(i,j) *= (T)(scale);
00287       }
00288     }
00289   }
00290 }
00291 
00292 template<class T, unsigned nrows, unsigned ncols>
00293 void
00294 vnl_matrix_fixed_ref<T,nrows,ncols>::normalize_columns() const
00295 {
00296   typedef typename vnl_numeric_traits<T>::abs_t Abs_t;
00297   for (unsigned int j = 0; j < ncols; j++) {  // For each column in the Matrix
00298     Abs_t norm(0); // double will not do for all types.
00299     for (unsigned int i = 0; i < nrows; i++)
00300       norm += vnl_math_squared_magnitude( (*this)(i,j) );
00301 
00302     if (norm != 0)
00303     {
00304       typedef typename vnl_numeric_traits<Abs_t>::real_t real_t;
00305       real_t scale = real_t(1)/vcl_sqrt((real_t)norm);
00306       for (unsigned int i = 0; i < nrows; i++)
00307       {
00308         // FIXME need correct rounding here
00309         // There is e.g. no *standard* operator*=(complex<float>, double), hence the T() cast.
00310         (*this)(i,j) *= (T)(scale);
00311       }
00312     }
00313   }
00314 }
00315 
00316 template<class T, unsigned nrows, unsigned ncols>
00317 void
00318 vnl_matrix_fixed_ref<T,nrows,ncols>::scale_row(unsigned row_index, T value) const
00319 {
00320 #ifndef NDEBUG
00321   if (row_index >= nrows)
00322     vnl_error_matrix_row_index("scale_row", row_index);
00323 #endif
00324   for (unsigned int j = 0; j < ncols; j++)
00325     (*this)(row_index,j) *= value;
00326 }
00327 
00328 template<class T, unsigned nrows, unsigned ncols>
00329 void
00330 vnl_matrix_fixed_ref<T,nrows,ncols>::scale_column(unsigned column_index, T value) const
00331 {
00332 #ifndef NDEBUG
00333   if (column_index >= ncols)
00334     vnl_error_matrix_col_index("scale_column", column_index);
00335 #endif
00336   for (unsigned int j = 0; j < nrows; j++)
00337     (*this)(j,column_index) *= value;
00338 }
00339 
00340 //: Returns a copy of n rows, starting from "row"
00341 template<class T, unsigned nrows, unsigned ncols>
00342 vnl_matrix<T>
00343 vnl_matrix_fixed_ref_const<T,nrows,ncols>::get_n_rows (unsigned row, unsigned n) const
00344 {
00345 #ifndef NDEBUG
00346   if (row + n > nrows)
00347     vnl_error_matrix_row_index ("get_n_rows", row);
00348 #endif
00349 
00350   // Extract data rowwise.
00351   return vnl_matrix<T>((*this)[row], n, ncols);
00352 }
00353 
00354 template<class T, unsigned nrows, unsigned ncols>
00355 vnl_matrix<T>
00356 vnl_matrix_fixed_ref_const<T,nrows,ncols>::get_n_columns (unsigned column, unsigned n) const
00357 {
00358 #ifndef NDEBUG
00359   if (column + n > ncols)
00360     vnl_error_matrix_col_index ("get_n_columns", column);
00361 #endif
00362 
00363   vnl_matrix<T> result(nrows, n);
00364   for (unsigned int c = 0; c < n; ++c)
00365     for (unsigned int r = 0; r < nrows; ++r)
00366       result(r, c) = (*this)(r,column + c);
00367   return result;
00368 }
00369 
00370 #if 0 // commented out
00371 
00372 //: Create a vector out of row[row_index].
00373 template<class T, unsigned nrows, unsigned ncols>
00374 vnl_vector<T> vnl_matrix_fixed_ref_const<T,nrows,ncols>::get_row(unsigned row_index) const
00375 {
00376 #if ERROR_CHECKING
00377   if (row_index >= nrows)
00378     vnl_error_matrix_row_index ("get_row", row_index);
00379 #endif
00380 
00381   vnl_vector<T> v(ncols);
00382   for (unsigned int j = 0; j < ncols; j++)    // For each element in row
00383     v[j] = (*this)(row_index,j);
00384   return v;
00385 }
00386 
00387 //: Create a vector out of column[column_index].
00388 template<class T, unsigned nrows, unsigned ncols>
00389 vnl_vector<T> vnl_matrix_fixed_ref_const<T,nrows,ncols>::get_column(unsigned column_index) const
00390 {
00391 #if ERROR_CHECKING
00392   if (column_index >= ncols)
00393     vnl_error_matrix_col_index ("get_column", column_index);
00394 #endif
00395 
00396   vnl_vector<T> v(nrows);
00397   for (unsigned int j = 0; j < nrows; j++)
00398     v[j] = (*this)(j,column_index);
00399   return v;
00400 }
00401 
00402 #endif // 0
00403 
00404 //--------------------------------------------------------------------------------
00405 
00406 template<class T, unsigned nrows, unsigned ncols>
00407 void
00408 vnl_matrix_fixed_ref<T,nrows,ncols>::set_row(unsigned row_index, T const *v) const
00409 {
00410   for (unsigned int j = 0; j < ncols; j++)
00411     (*this)(row_index,j) = v[j];
00412 }
00413 
00414 template<class T, unsigned nrows, unsigned ncols>
00415 void
00416 vnl_matrix_fixed_ref<T,nrows,ncols>::set_row(unsigned row_index, vnl_vector<T> const &v) const
00417 {
00418   set_row(row_index,v.data_block());
00419 }
00420 
00421 template<class T, unsigned nrows, unsigned ncols>
00422 void
00423 vnl_matrix_fixed_ref<T,nrows,ncols>::set_row(unsigned row_index, T v) const
00424 {
00425   for (unsigned int j = 0; j < ncols; j++)
00426     (*this)(row_index,j) = v;
00427 }
00428 
00429 //--------------------------------------------------------------------------------
00430 
00431 template<class T, unsigned nrows, unsigned ncols>
00432 void
00433 vnl_matrix_fixed_ref<T,nrows,ncols>::set_column(unsigned column_index, T const *v) const
00434 {
00435   for (unsigned int i = 0; i < nrows; i++)
00436     (*this)(i,column_index) = v[i];
00437 }
00438 
00439 template<class T, unsigned nrows, unsigned ncols>
00440 void
00441 vnl_matrix_fixed_ref<T,nrows,ncols>::set_column(unsigned column_index, vnl_vector<T> const &v) const
00442 {
00443   set_column(column_index,v.data_block());
00444 }
00445 
00446 template<class T, unsigned nrows, unsigned ncols>
00447 void
00448 vnl_matrix_fixed_ref<T,nrows,ncols>::set_column(unsigned column_index, T v) const
00449 {
00450   for (unsigned int j = 0; j < nrows; j++)
00451     (*this)(j,column_index) = v;
00452 }
00453 
00454 
00455 template<class T, unsigned nrows, unsigned ncols>
00456 void
00457 vnl_matrix_fixed_ref<T,nrows,ncols>::set_columns(unsigned starting_column, vnl_matrix<T> const& m) const
00458 {
00459 #ifndef NDEBUG
00460   if (nrows != m.rows() ||
00461       ncols < m.cols() + starting_column)           // Size match?
00462     vnl_error_matrix_dimension ("set_columns",
00463                                 nrows, ncols,
00464                                 m.rows(), m.cols());
00465 #endif
00466 
00467   for (unsigned int j = 0; j < m.cols(); ++j)
00468     for (unsigned int i = 0; i < nrows; i++)
00469       (*this)(i,starting_column + j) = m(i,j);
00470 }
00471 
00472 
00473 template <class T, unsigned nrows, unsigned ncols>
00474 bool
00475 vnl_matrix_fixed_ref_const<T,nrows,ncols>::is_identity() const
00476 {
00477   T const zero(0);
00478   T const one(1);
00479   for (unsigned int i = 0; i < nrows; ++i)
00480     for (unsigned int j = 0; j < ncols; ++j)
00481     {
00482       T xm = (*this)(i,j);
00483       if ( !((i == j) ? (xm == one) : (xm == zero)) )
00484         return false;
00485     }
00486   return true;
00487 }
00488 
00489 //: Return true if maximum absolute deviation of M from identity is <= tol.
00490 template <class T, unsigned nrows, unsigned ncols>
00491 bool
00492 vnl_matrix_fixed_ref_const<T,nrows,ncols>::is_identity(double tol) const
00493 {
00494   T one(1);
00495   for (unsigned int i = 0; i < nrows; ++i)
00496     for (unsigned int j = 0; j < ncols; ++j)
00497     {
00498       T xm = (*this)(i,j);
00499       abs_t absdev = (i == j) ? vnl_math_abs(xm - one) : vnl_math_abs(xm);
00500       if (absdev > tol)
00501         return false;
00502     }
00503   return true;
00504 }
00505 
00506 template <class T, unsigned nrows, unsigned ncols>
00507 bool
00508 vnl_matrix_fixed_ref_const<T,nrows,ncols>::is_zero() const
00509 {
00510   T const zero(0);
00511   for (unsigned int i = 0; i < nrows; ++i)
00512     for (unsigned int j = 0; j < ncols; ++j)
00513       if ( !( (*this)(i, j) == zero) )
00514         return false;
00515 
00516   return true;
00517 }
00518 
00519 template <class T, unsigned nrows, unsigned ncols>
00520 bool
00521 vnl_matrix_fixed_ref_const<T,nrows,ncols>::is_zero(double tol) const
00522 {
00523   for (unsigned int i = 0; i < nrows; ++i)
00524     for (unsigned int j = 0; j < ncols; ++j)
00525       if (vnl_math_abs((*this)(i,j)) > tol)
00526         return false;
00527 
00528   return true;
00529 }
00530 
00531 template <class T, unsigned nrows, unsigned ncols>
00532 bool
00533 vnl_matrix_fixed_ref_const<T,nrows,ncols>::has_nans() const
00534 {
00535   for (unsigned int i = 0; i < nrows; ++i)
00536     for (unsigned int j = 0; j < ncols; ++j)
00537       if (vnl_math_isnan((*this)(i,j)))
00538         return true;
00539 
00540   return false;
00541 }
00542 
00543 template <class T, unsigned nrows, unsigned ncols>
00544 bool
00545 vnl_matrix_fixed_ref_const<T,nrows,ncols>::is_finite() const
00546 {
00547   for (unsigned int i = 0; i < nrows; ++i)
00548     for (unsigned int j = 0; j < ncols; ++j)
00549       if (!vnl_math_isfinite((*this)(i,j)))
00550         return false;
00551 
00552   return true;
00553 }
00554 
00555 //: Abort if any element of M is inf or nan
00556 template <class T, unsigned nrows, unsigned ncols>
00557 void
00558 vnl_matrix_fixed_ref_const<T,nrows,ncols>::assert_finite_internal() const
00559 {
00560   if (is_finite())
00561     return;
00562 
00563   vcl_cerr << "\n\n" << __FILE__ " : " << __LINE__ << ": matrix has non-finite elements\n";
00564 
00565   if (rows() <= 20 && cols() <= 20)
00566     vcl_cerr << __FILE__ ": here it is:\n" << *this << '\n';
00567   else
00568   {
00569     vcl_cerr << __FILE__ ": it is quite big (" << rows() << 'x' << cols() << ")\n"
00570              << __FILE__ ": in the following picture '-' means finite and '*' means non-finite:\n";
00571 
00572     for (unsigned int i=0; i<rows(); ++i)
00573     {
00574       for (unsigned int j=0; j<cols(); ++j)
00575         vcl_cerr << char(vnl_math_isfinite((*this)(i, j)) ? '-' : '*');
00576       vcl_cerr << '\n';
00577     }
00578   }
00579   vcl_cerr << __FILE__ ": calling abort()\n";
00580   vcl_abort();
00581 }
00582 
00583 //: Abort unless M has the given size.
00584 template <class T, unsigned nrows, unsigned ncols>
00585 void
00586 vnl_matrix_fixed_ref_const<T,nrows,ncols>::assert_size_internal(unsigned rs,unsigned cs) const
00587 {
00588   if (nrows!=rs || ncols!=cs)
00589   {
00590     vcl_cerr << __FILE__ ": size is " << nrows << 'x' << ncols
00591              << ". should be " << rs << 'x' << cs << vcl_endl;
00592     vcl_abort();
00593   }
00594 }
00595 
00596 template <class T, unsigned nrows, unsigned ncols>
00597 bool
00598 vnl_matrix_fixed_ref<T,nrows,ncols>::read_ascii(vcl_istream& s) const
00599 {
00600   if (!s.good())
00601   {
00602     vcl_cerr << __FILE__ ": vnl_matrix_fixed_ref_const<T,nrows,ncols>::read_ascii: Called with bad stream\n";
00603     return false;
00604   }
00605 
00606   for (unsigned int i = 0; i < nrows; ++i)
00607     for (unsigned int j = 0; j < ncols; ++j)
00608       s >> (*this)(i,j);
00609 
00610   return s.good() || s.eof();
00611 }
00612 
00613 
00614 template <class T, unsigned nrows, unsigned ncols>
00615 void
00616 vnl_matrix_fixed_ref<T,nrows,ncols>::flipud() const
00617 {
00618   for (unsigned int r1 = 0; 2*r1+1 < nrows; ++r1)
00619   {
00620     unsigned int r2 = nrows - 1 - r1;
00621     for (unsigned int c = 0; c < ncols; ++c)
00622     {
00623       T tmp = (*this)(r1, c);
00624       (*this)(r1, c) = (*this)(r2, c);
00625       (*this)(r2, c) = tmp;
00626     }
00627   }
00628 }
00629 
00630 
00631 template <class T, unsigned nrows, unsigned ncols>
00632 void
00633 vnl_matrix_fixed_ref<T,nrows,ncols>::fliplr() const
00634 {
00635   for (unsigned int c1 = 0; 2*c1+1 < ncols; ++c1)
00636   {
00637     unsigned int c2 = ncols - 1 - c1;
00638     for (unsigned int r = 0; r < nrows; ++r)
00639     {
00640       T tmp = (*this)(r, c1);
00641       (*this)(r, c1) = (*this)(r, c2);
00642       (*this)(r, c2) = tmp;
00643     }
00644   }
00645 }
00646 
00647 template <class T, unsigned nrows, unsigned ncols>
00648 typename vnl_matrix_fixed_ref_const<T,nrows,ncols>::abs_t
00649 vnl_matrix_fixed_ref_const<T,nrows,ncols>::operator_one_norm() const
00650 {
00651   abs_t m(0);
00652   for (unsigned int j=0; j<ncols; ++j)
00653   {
00654     abs_t t(0);
00655     for (unsigned int i=0; i<nrows; ++i)
00656       t += vnl_math_abs( (*this)(i,j) );
00657     if (t > m)
00658       m = t;
00659   }
00660   return m;
00661 }
00662 
00663 template <class T, unsigned nrows, unsigned ncols>
00664 typename vnl_matrix_fixed_ref_const<T,nrows,ncols>::abs_t
00665 vnl_matrix_fixed_ref_const<T,nrows,ncols>::operator_inf_norm() const
00666 {
00667   abs_t m(0);
00668   for (unsigned int i=0; i<nrows; ++i)
00669   {
00670     abs_t t(0);
00671     for (unsigned int j=0; j<ncols; ++j)
00672       t += vnl_math_abs( (*this)(i,j) );
00673     if (t > m)
00674       m = t;
00675   }
00676   return m;
00677 }
00678 
00679 //: Transpose square matrix M in place.
00680 template <class T, unsigned nrows, unsigned ncols>
00681 void vnl_matrix_fixed_ref<T,nrows,ncols>::inplace_transpose() const
00682 {
00683   assert(nrows==ncols); // cannot inplace_transpose non-square fixed size matrix
00684   for (unsigned i = 0; i < nrows; ++i)
00685   for (unsigned j = i+1; j < ncols; ++j)
00686   {
00687     T t = (*this)(i,j);
00688     (*this)(i,j) = (*this)(j,i);
00689     (*this)(j,i) = t;
00690   }
00691 }
00692 
00693 
00694 #define VNL_MATRIX_FIXED_REF_INSTANTIATE(T,m,n) \
00695 template class vnl_matrix_fixed_ref_const<T, m, n >; \
00696 template class vnl_matrix_fixed_ref<T, m, n >
00697 
00698 #endif // vnl_matrix_fixed_ref_txx_

Generated on Mon Mar 8 05:06:45 2010 for core/vnl by  doxygen 1.5.1