Commit ea575d03 authored by Davis King's avatar Davis King

Added the real_eigenvalues() function and also cleaned up a few things.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%402853
parent 3e664e8d
...@@ -1508,6 +1508,49 @@ convergence: ...@@ -1508,6 +1508,49 @@ convergence:
} }
}; };
// ----------------------------------------------------------------------------------------
template <typename EXP>
const matrix<typename EXP::type, EXP::NR, 1, typename EXP::mem_manager_type, typename EXP::layout_type> real_eigenvalues (
const matrix_exp<EXP>& m
)
{
DLIB_ASSERT(m.nr() == m.nc(),
"\tconst matrix real_eigenvalues()"
<< "\n\tYou have given an invalidly sized matrix"
<< "\n\tm.nr(): " << m.nr()
<< "\n\tm.nc(): " << m.nc()
);
if (m.nr() == 2)
{
typedef typename EXP::type T;
const T m00 = m(0,0);
const T m01 = m(0,1);
const T m10 = m(1,0);
const T m11 = m(1,1);
const T b = -(m00 + m11);
const T c = m00*m11 - m01*m10;
matrix<T,EXP::NR,1, typename EXP::mem_manager_type, typename EXP::layout_type> v(2);
T disc = b*b - 4*c;
if (disc >= 0)
disc = std::sqrt(disc);
else
disc = 0;
v(0) = (-b + disc)/2;
v(1) = (-b - disc)/2;
return v;
}
else
{
return eigenvalue_decomposition<EXP>(m).get_real_eigenvalues();
}
}
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
} }
......
...@@ -124,6 +124,22 @@ namespace dlib ...@@ -124,6 +124,22 @@ namespace dlib
- #v.nc() == m.nc() - #v.nc() == m.nc()
!*/ !*/
// ----------------------------------------------------------------------------------------
const matrix real_eigenvalues (
const matrix_exp& m
);
/*!
requires
- m.nr() == m.nc()
ensures
- returns a matrix E such that:
- E.nr() == m.nr()
- E.nc() == 1
- E contains the real part of all eigenvalues of the matrix m.
(note that the eigenvalues are not sorted)
!*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
const matrix_exp::type det ( const matrix_exp::type det (
......
...@@ -423,10 +423,7 @@ namespace dlib ...@@ -423,10 +423,7 @@ namespace dlib
/*! /*!
ensures ensures
- returns a temporary matrix object that is a copy of m. - returns a temporary matrix object that is a copy of m.
(This is useful because it allows you to easily force a matrix_exp to (This allows you to easily force a matrix_exp to fully evaluate)
fully evaluate before giving it to some other function that queries
the elements of the matrix more than once each, such as the matrix
multiplication operator.)
!*/ !*/
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -263,6 +263,12 @@ namespace ...@@ -263,6 +263,12 @@ namespace
DLIB_CASSERT(equal(real(diag(test.get_d())), test.get_real_eigenvalues(), eps), ""); DLIB_CASSERT(equal(real(diag(test.get_d())), test.get_real_eigenvalues(), eps), "");
DLIB_CASSERT(equal(imag(diag(test.get_d())), test.get_imag_eigenvalues(), eps), ""); DLIB_CASSERT(equal(imag(diag(test.get_d())), test.get_imag_eigenvalues(), eps), "");
matrix<type> eig1 ( real_eigenvalues(m));
matrix<type> eig2 ( test.get_real_eigenvalues());
sort(&eig1(0), &eig1(0) + eig1.size());
sort(&eig2(0), &eig2(0) + eig2.size());
DLIB_CASSERT(max(abs(eig1 - eig2)) < eps, "");
const matrix<type> V = test.get_pseudo_v(); const matrix<type> V = test.get_pseudo_v();
const matrix<type> D = test.get_pseudo_d(); const matrix<type> D = test.get_pseudo_d();
const matrix<complex<type> > CV = test.get_v(); const matrix<complex<type> > CV = test.get_v();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment