vnl_trace.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_trace.h
2 #ifndef vnl_trace_h_
3 #define vnl_trace_h_
4 //:
5 // \file
6 // \brief Calculate trace of a matrix
7 // \author fsm
8 //
9 // \verbatim
10 // Modifications
11 // LSB (Manchester) 19/3/01 Documentation tidied
12 // Peter Vanroose 27-Jun-2003 made inline and added trace(matrix_fixed)
13 // \endverbatim
14 
15 #include <vnl/vnl_matrix.h>
16 #include <vnl/vnl_matrix_fixed.h>
17 #include "vnl/vnl_export.h"
18 
19 //: Calculate trace of a matrix
20 // \relatesalso vnl_matrix
21 template <class T>
23 {
24  T sum(0);
25  const unsigned int N = M.rows()<M.cols() ? M.rows() : M.cols();
26  for (unsigned int i=0; i<N; ++i)
27  sum += M(i, i);
28  return sum;
29 }
30 
31 //: Calculate trace of a matrix
32 // \relatesalso vnl_matrix_fixed
33 template <class T, unsigned int N1, unsigned int N2>
35 {
36  T sum(0);
37  for (unsigned int i=0; i<N1 && i<N2; ++i)
38  sum += M(i, i);
39  return sum;
40 }
41 
42 #endif // vnl_trace_h_
unsigned int cols() const
Return the number of columns.
Definition: vnl_matrix.h:183
An ordinary mathematical matrix.
Fixed size, stack-stored, space-efficient matrix.
Definition: vnl_fwd.h:23
An ordinary mathematical matrix.
Definition: vnl_adjugate.h:22
T vnl_trace(vnl_matrix< T > const &M)
Calculate trace of a matrix.
Definition: vnl_trace.h:22
fixed size matrix
unsigned int rows() const
Return the number of rows.
Definition: vnl_matrix.h:179