core/vnl/vnl_diag_matrix.txx
Go to the documentation of this file.
00001 // This is core/vnl/vnl_diag_matrix.txx
00002 #ifndef vnl_diag_matrix_txx_
00003 #define vnl_diag_matrix_txx_
00004 //:
00005 // \file
00006 
00007 #include "vnl_diag_matrix.h"
00008 
00009 #include <vcl_iostream.h>
00010 
00011 
00012 //: Return inv(D) * b.
00013 template <class T>
00014 vnl_vector<T> vnl_diag_matrix<T>::solve(vnl_vector<T> const& b) const
00015 {
00016   unsigned len = diagonal_.size();
00017   vnl_vector<T> ret(len);
00018   for (unsigned i = 0; i < len; ++i)
00019     ret[i] = b[i] / diagonal_[i];
00020   return ret;
00021 }
00022 
00023 //: Return inv(D) * b.
00024 template <class T>
00025 void vnl_diag_matrix<T>::solve(vnl_vector<T> const& b, vnl_vector<T>* out) const
00026 {
00027   unsigned len = diagonal_.size();
00028   for (unsigned i = 0; i < len; ++i)
00029     (*out)[i] = b[i] / diagonal_[i];
00030 }
00031 
00032 //: Print in MATLAB diag([1 2 3]) form.
00033 template <class T>
00034 vcl_ostream& operator<< (vcl_ostream& s, const vnl_diag_matrix<T>& D)
00035 {
00036   s << "diag([ ";
00037   for (unsigned i=0; i<D.rows(); ++i)
00038     s << D(i,i) << ' ';
00039   return s << "])";
00040 }
00041 
00042 #if 0
00043 //: Compares two matrices for component-wise equality within a small epsilon
00044 template<class T>
00045 bool epsilon_equals (const vnl_diag_matrix<T>& m1, const vnl_diag_matrix<T>& m2,
00046                      double alt_epsilon)
00047 {
00048   if (alt_epsilon < 0)
00049   {
00050     vcl_cerr << "Negative alt_epsilon passed to epsilon_equals: returning false\n";
00051     return false;
00052   }
00053 
00054   if (m1.rows() != m2.rows())
00055      return false;              // different sizes.
00056 
00057   double local_epsilon;
00058   if (alt_epsilon == 0)
00059     local_epsilon = comparison_epsilon<T>::epsilon;
00060   else
00061     local_epsilon = alt_epsilon;
00062 
00063   for (unsigned long i = 0; i < m1.rows(); i++) {
00064 #if 0
00065     T result = m1(i,i) - m2(i,i);
00066     if (result < 0)
00067       result = 0 - result;
00068     if (result > local_epsilon)
00069       return false;
00070 #endif
00071     if (m1(i,i) - m2(i,i) > local_epsilon ||
00072         m2(i,i) - m1(i,i) > local_epsilon) // avoid using vcl_abs()
00073       return false;
00074   }
00075   return true;
00076 }
00077 #endif
00078 
00079 
00080 #undef VNL_DIAG_MATRIX_INSTANTIATE
00081 #define VNL_DIAG_MATRIX_INSTANTIATE(T) \
00082 template class vnl_diag_matrix<T >; \
00083 VCL_INSTANTIATE_INLINE(vnl_matrix<T > operator* (vnl_matrix<T > const &, vnl_diag_matrix<T > const &));\
00084 VCL_INSTANTIATE_INLINE(vnl_matrix<T > operator* (vnl_diag_matrix<T > const &, vnl_matrix<T > const &));\
00085 VCL_INSTANTIATE_INLINE(vnl_matrix<T > operator+ (vnl_matrix<T > const &, vnl_diag_matrix<T > const &));\
00086 VCL_INSTANTIATE_INLINE(vnl_matrix<T > operator+ (vnl_diag_matrix<T > const &, vnl_matrix<T > const &));\
00087 VCL_INSTANTIATE_INLINE(vnl_matrix<T > operator- (vnl_matrix<T > const &, vnl_diag_matrix<T > const &));\
00088 VCL_INSTANTIATE_INLINE(vnl_matrix<T > operator- (vnl_diag_matrix<T > const &, vnl_matrix<T > const &));\
00089 VCL_INSTANTIATE_INLINE(vnl_vector<T > operator* (const vnl_vector<T >&, vnl_diag_matrix<T > const &));\
00090 VCL_INSTANTIATE_INLINE(vnl_vector<T > operator* (vnl_diag_matrix<T > const &, const vnl_vector<T >&));\
00091 template vcl_ostream& operator<< (vcl_ostream& s, vnl_diag_matrix<T > const &)
00092 
00093 //template bool epsilon_equals (vnl_diag_matrix<T > const & , vnl_diag_matrix<T > const & , double)
00094 
00095 #endif // vnl_diag_matrix_txx_