Added a trace() function.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402881
This commit is contained in:
Davis King 2009-02-28 19:32:57 +00:00
parent d4072818c3
commit 2d7982cf41
2 changed files with 35 additions and 0 deletions

View File

@ -1388,6 +1388,28 @@ convergence:
w = diagm(W); w = diagm(W);
} }
// ----------------------------------------------------------------------------------------
template <
typename EXP
>
const typename matrix_exp<EXP>::type trace (
const matrix_exp<EXP>& m
)
{
COMPILE_TIME_ASSERT(matrix_exp<EXP>::NR == matrix_exp<EXP>::NC ||
matrix_exp<EXP>::NR == 0 ||
matrix_exp<EXP>::NC == 0
);
DLIB_ASSERT(m.nr() == m.nc(),
"\tconst matrix_exp::type trace(const matrix_exp& m)"
<< "\n\tYou can only apply trace() to a square matrix"
<< "\n\tm.nr(): " << m.nr()
<< "\n\tm.nc(): " << m.nc()
);
return sum(diag(m));
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
template < template <

View File

@ -153,6 +153,19 @@ namespace dlib
- returns the determinant of m - returns the determinant of m
!*/ !*/
// ----------------------------------------------------------------------------------------
const matrix_exp::type trace (
const matrix_exp& m
);
/*!
requires
- m is a square matrix
ensures
- returns the trace of m
(i.e. returns sum(diag(m)))
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
const matrix_exp::matrix_type chol ( const matrix_exp::matrix_type chol (