diff --git a/Eigen/src/Core/MatrixBase.h b/Eigen/src/Core/MatrixBase.h index e6af97bb3..a9d15032c 100644 --- a/Eigen/src/Core/MatrixBase.h +++ b/Eigen/src/Core/MatrixBase.h @@ -573,7 +573,7 @@ template class MatrixBase /////////// SVD module /////////// - const SVD svd() const; + SVD svd() const; /////////// Geometry module /////////// diff --git a/Eigen/src/SVD/SVD.h b/Eigen/src/SVD/SVD.h index f3dde3f52..39020fdfc 100644 --- a/Eigen/src/SVD/SVD.h +++ b/Eigen/src/SVD/SVD.h @@ -76,6 +76,7 @@ template class SVD const MatrixVType& matrixV() const { return m_matV; } void compute(const MatrixType& matrix); + SVD& sort(); protected: /** \internal */ @@ -464,6 +465,43 @@ void SVD::compute(const MatrixType& matrix) } // end iterations } +template +SVD& SVD::sort() +{ + int mu = m_matU.rows(); + int mv = m_matV.rows(); + int n = m_matU.cols(); + + for (int i=0; i p) + { + k = j; + p = m_sigma.coeff(j); + } + } + if (k != i) + { + m_sigma.coeffRef(k) = m_sigma.coeff(i); // i.e. + m_sigma.coeffRef(i) = p; // swaps the i-th and the k-th elements + + int j = mu; + for(int s=0; j!=0; ++s, --j) + std::swap(m_matU.coeffRef(s,i), m_matU.coeffRef(s,k)); + + j = mv; + for (int s=0; j!=0; ++s, --j) + std::swap(m_matV.coeffRef(s,i), m_matV.coeffRef(s,k)); + } + } + return *this; +} + /** \returns the solution of \f$ A x = b \f$ using the current SVD decomposition of A. * The parts of the solution corresponding to zero singular values are ignored. * @@ -476,6 +514,7 @@ void SVD::solve(const MatrixBase &b, ResultType* resul const int rows = m_matU.rows(); ei_assert(b.rows() == rows); + Scalar maxVal = m_sigma.cwise().abs().maxCoeff(); for (int j=0; j aux = m_matU.transpose() * b.col(j); @@ -483,10 +522,10 @@ void SVD::solve(const MatrixBase &b, ResultType* resul for (int i = 0; i col(j) = m_matV * aux; @@ -497,7 +536,7 @@ void SVD::solve(const MatrixBase &b, ResultType* resul * \returns the SVD decomposition of \c *this */ template -inline const SVD::EvalType> +inline SVD::EvalType> MatrixBase::svd() const { return SVD::type>(derived());