core/vnl/vnl_trace.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_trace.h
00002 #ifndef vnl_trace_h_
00003 #define vnl_trace_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 //  \file
00009 //  \brief Calculate trace of a matrix
00010 //  \author fsm
00011 //
00012 // \verbatim
00013 //  Modifications
00014 //   LSB (Manchester) 19/3/01 Documentation tidied
00015 //   Peter Vanroose   27-Jun-2003  made inline and added trace(matrix_fixed)
00016 // \endverbatim
00017 
00018 #include <vnl/vnl_matrix.h>
00019 #include <vnl/vnl_matrix_fixed.h>
00020 
00021 //: Calculate trace of a matrix
00022 // \relatesalso vnl_matrix
00023 template <class T>
00024 T vnl_trace(vnl_matrix<T> const& M)
00025 {
00026   T sum(0);
00027   const unsigned int N = M.rows()<M.cols() ? M.rows() : M.cols();
00028   for (unsigned int i=0; i<N; ++i)
00029     sum += M(i, i);
00030   return sum;
00031 }
00032 
00033 //: Calculate trace of a matrix
00034 // \relatesalso vnl_matrix_fixed
00035 template <class T, unsigned int N1, unsigned int N2>
00036 T vnl_trace(vnl_matrix_fixed<T,N1,N2> const& M)
00037 {
00038   T sum(0);
00039   for (unsigned int i=0; i<N1 && i<N2; ++i)
00040     sum += M(i, i);
00041   return sum;
00042 }
00043 
00044 #endif // vnl_trace_h_