core/vnl/vnl_sym_matrix.txx
Go to the documentation of this file.
00001 // This is core/vnl/vnl_sym_matrix.txx
00002 #ifndef vnl_sym_matrix_txx_
00003 #define vnl_sym_matrix_txx_
00004 //:
00005 // \file
00006 
00007 #include "vnl_sym_matrix.h"
00008 #include <vcl_iostream.h>
00009 #include <vnl/vnl_config.h> // for VNL_CONFIG_CHECK_BOUNDS
00010 
00011 // ==========================================================================
00012 //: Replaces the symmetric submatrix of THIS matrix, starting at top left corner, by the elements of matrix m.
00013 // O(m*m).
00014 template<class T>
00015 vnl_sym_matrix<T>& vnl_sym_matrix<T>::update (vnl_sym_matrix<T> const& m,
00016                                               unsigned diagonal_start)
00017 {
00018   unsigned int end_val = diagonal_start + m.nn_;
00019 #if VNL_CONFIG_CHECK_BOUNDS  && (!defined NDEBUG)
00020   if (this->nn_ < end_val)
00021     vnl_error_matrix_dimension ("vnl_sym_matrix::update",
00022                                 end_val, end_val, m.nn_, m.nn_);
00023 #endif
00024   for (unsigned int i = diagonal_start; i < end_val; i++)
00025     for (unsigned int j = diagonal_start; j <= i; j++)
00026       this->fast(i,j) = m.fast(i-diagonal_start,j-diagonal_start);
00027   return *this;
00028 }
00029 
00030 // ==========================================================================
00031 //: Swap contents of m with THIS
00032 template <class T>
00033 void vnl_sym_matrix<T>::swap(vnl_sym_matrix<T> &m)
00034 {
00035   unsigned nn = nn_;
00036   T **index   = index_;
00037   T *data     = data_;
00038   nn_    =m.nn_;
00039   index_ =m.index_;
00040   data_  =m.data_;
00041   m.nn_    =nn;
00042   m.index_ =index;
00043   m.data_  =data;
00044 }
00045 
00046 // ==========================================================================
00047 
00048 template <class T>
00049 vnl_sym_matrix<T>& vnl_sym_matrix<T>::operator=(vnl_sym_matrix<T> const& that)
00050 {
00051   if (&that == this) return *this;
00052 
00053   set_size(that.rows());
00054   update(that);
00055   return *this;
00056 }
00057 
00058 // ==========================================================================
00059 //: Set the first i values of row i
00060 // or the top i values of column i
00061 template <class T>
00062 void vnl_sym_matrix<T>::set_half_row (const vnl_vector<T> &half_row, unsigned i)
00063 {
00064 #if VNL_CONFIG_CHECK_BOUNDS  && (!defined NDEBUG)
00065   if (half_row.size() != i+1)
00066     vnl_error_vector_dimension ("vnl_sym_matrix::set_half_row wrong size for half row",
00067                                 half_row.size(), i+1);
00068   if ( i > nn_)
00069     vnl_error_vector_dimension ("vnl_sym_matrix::set_half_row wrong sizes",
00070                                 i+1, rows());
00071 #endif
00072   half_row.copy_out(index_[i]);
00073 }
00074 
00075 // ==========================================================================
00076 //: print in lower triangular form
00077 template <class T>
00078 vcl_ostream& operator<< (vcl_ostream& s, const vnl_sym_matrix<T>& M)
00079 {
00080   for (unsigned i=0; i<M.rows(); ++i)
00081   {
00082     for (unsigned j=0; j<=i; ++j)
00083       s << M.fast(i,j) << ' ';
00084     s  << '\n';
00085   }
00086   return s;
00087 }
00088 
00089 // ==========================================================================
00090 
00091 template <class T>
00092 bool operator==(const vnl_sym_matrix<T> &a, const vnl_sym_matrix<T> &b)
00093 {
00094   if (a.rows() != b.rows()) return false;
00095   const T* a_data = a.data_block();
00096   const T* b_data = b.data_block();
00097   const unsigned mn = a.size();
00098   for (unsigned i = 0; i < mn; ++i)
00099     if (a_data[i] != b_data[i]) return false;
00100   return true;
00101 }
00102 
00103 // ==========================================================================
00104 
00105 template <class T>
00106 bool operator==(const vnl_sym_matrix<T> &a, const vnl_matrix<T> &b)
00107 {
00108   if (a.rows() != b.rows() || a.cols() != b.cols()) return false;
00109 
00110   const unsigned n = a.rows();
00111   for (unsigned i=0; i< n; ++i)
00112   {
00113     for (unsigned j=0; j<i; ++j)
00114       if (a.fast(i,j) != b(i,j) || a.fast(i,j) != b(j,i)) return false;
00115     if (a.fast(i,i) != b(i,i)) return false;
00116   }
00117   return true;
00118 }
00119 
00120 // ==========================================================================
00121 
00122 template <class T>
00123 bool operator==(const vnl_matrix<T> &a, const vnl_sym_matrix<T> &b)
00124 {
00125   return operator==(b,a);
00126 }
00127 
00128 // ==========================================================================
00129 
00130 #undef VNL_SYM_MATRIX_INSTANTIATE
00131 #define VNL_SYM_MATRIX_INSTANTIATE(T) \
00132 template class vnl_sym_matrix<T >; \
00133 template vcl_ostream& operator<< (vcl_ostream& s, vnl_sym_matrix<T > const &); \
00134 template bool operator==(const vnl_sym_matrix<T > &a, const vnl_sym_matrix<T > &b); \
00135 template bool operator==(const vnl_sym_matrix<T > &a, const vnl_matrix<T > &b); \
00136 template bool operator==(const vnl_matrix<T > &a, const vnl_sym_matrix<T > &b)
00137 
00138 #endif // vnl_sym_matrix_txx_