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