mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Add bidiagonal SVD API to BDCSVD and remove dead debug code
libeigen/eigen!2238 Co-authored-by: Rasmus Munk Larsen <rmlarsen@gmail.com>
This commit is contained in:
@@ -19,21 +19,10 @@
|
||||
|
||||
#ifndef EIGEN_BDCSVD_H
|
||||
#define EIGEN_BDCSVD_H
|
||||
// #define EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
// #define EIGEN_BDCSVD_SANITY_CHECKS
|
||||
|
||||
#ifdef EIGEN_BDCSVD_SANITY_CHECKS
|
||||
#undef eigen_internal_assert
|
||||
#define eigen_internal_assert(X) assert(X);
|
||||
#endif
|
||||
|
||||
// IWYU pragma: private
|
||||
#include "./InternalHeaderCheck.h"
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
// Internal D&C implementation, templated only on RealScalar.
|
||||
#include "BDCSVDImpl.h"
|
||||
|
||||
@@ -168,6 +157,20 @@ class BDCSVD : public SVDBase<BDCSVD<MatrixType_, Options_> > {
|
||||
compute_impl(matrix, internal::get_computation_options(Options));
|
||||
}
|
||||
|
||||
/** \brief Constructor performing the SVD of an upper bidiagonal matrix given its diagonal and superdiagonal.
|
||||
*
|
||||
* This skips the bidiagonalization step and directly runs the divide-and-conquer algorithm.
|
||||
* The input vectors must be real-valued. For an n x n bidiagonal matrix, \a diagonal has n entries
|
||||
* and \a superdiagonal has n-1 entries.
|
||||
*
|
||||
* \param diagonal the diagonal entries of the bidiagonal matrix
|
||||
* \param superdiagonal the superdiagonal entries of the bidiagonal matrix
|
||||
*/
|
||||
template <typename DerivedD, typename DerivedE>
|
||||
BDCSVD(const MatrixBase<DerivedD>& diagonal, const MatrixBase<DerivedE>& superdiagonal) : m_numIters(0) {
|
||||
compute_bidiagonal_impl(diagonal, superdiagonal, internal::get_computation_options(Options));
|
||||
}
|
||||
|
||||
/** \brief Constructor performing the decomposition of given matrix using specified options
|
||||
* for computing unitaries.
|
||||
*
|
||||
@@ -215,6 +218,20 @@ class BDCSVD : public SVDBase<BDCSVD<MatrixType_, Options_> > {
|
||||
return compute_impl(matrix, computationOptions);
|
||||
}
|
||||
|
||||
/** \brief Compute the SVD of an upper bidiagonal matrix given its diagonal and superdiagonal.
|
||||
*
|
||||
* This skips the bidiagonalization step and directly runs the divide-and-conquer algorithm.
|
||||
* The input vectors must be real-valued. For an n x n bidiagonal matrix, \a diagonal has n entries
|
||||
* and \a superdiagonal has n-1 entries.
|
||||
*
|
||||
* \param diagonal the diagonal entries of the bidiagonal matrix
|
||||
* \param superdiagonal the superdiagonal entries of the bidiagonal matrix
|
||||
*/
|
||||
template <typename DerivedD, typename DerivedE>
|
||||
BDCSVD& compute(const MatrixBase<DerivedD>& diagonal, const MatrixBase<DerivedE>& superdiagonal) {
|
||||
return compute_bidiagonal_impl(diagonal, superdiagonal, m_computationOptions);
|
||||
}
|
||||
|
||||
void setSwitchSize(int s) {
|
||||
eigen_assert(s >= 3 && "BDCSVD the size of the algo switch has to be at least 3.");
|
||||
m_impl.setAlgoSwap(s);
|
||||
@@ -223,6 +240,9 @@ class BDCSVD : public SVDBase<BDCSVD<MatrixType_, Options_> > {
|
||||
private:
|
||||
template <typename Derived>
|
||||
BDCSVD& compute_impl(const MatrixBase<Derived>& matrix, unsigned int computationOptions);
|
||||
template <typename DerivedD, typename DerivedE>
|
||||
BDCSVD& compute_bidiagonal_impl(const MatrixBase<DerivedD>& diagonal, const MatrixBase<DerivedE>& superdiagonal,
|
||||
unsigned int computationOptions);
|
||||
template <typename HouseholderU, typename HouseholderV, typename NaiveU, typename NaiveV>
|
||||
void copyUV(const HouseholderU& householderU, const HouseholderV& householderV, const NaiveU& naiveU,
|
||||
const NaiveV& naivev);
|
||||
@@ -292,10 +312,6 @@ EIGEN_DONT_INLINE BDCSVD<MatrixType, Options>& BDCSVD<MatrixType, Options>::comp
|
||||
EIGEN_STATIC_ASSERT((std::is_same<typename Derived::Scalar, typename MatrixType::Scalar>::value),
|
||||
Input matrix must have the same Scalar type as the BDCSVD object.);
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "\n\n\n================================================================================================="
|
||||
"=====================\n\n\n";
|
||||
#endif
|
||||
using std::abs;
|
||||
|
||||
allocate(matrix.rows(), matrix.cols(), computationOptions);
|
||||
@@ -419,6 +435,119 @@ EIGEN_DONT_INLINE void BDCSVD<MatrixType, Options>::copyUV(const HouseholderU& h
|
||||
}
|
||||
}
|
||||
|
||||
template <typename MatrixType, int Options>
|
||||
template <typename DerivedD, typename DerivedE>
|
||||
EIGEN_DONT_INLINE BDCSVD<MatrixType, Options>& BDCSVD<MatrixType, Options>::compute_bidiagonal_impl(
|
||||
const MatrixBase<DerivedD>& diagonal, const MatrixBase<DerivedE>& superdiagonal, unsigned int computationOptions) {
|
||||
EIGEN_STATIC_ASSERT(DerivedD::IsVectorAtCompileTime, THIS_METHOD_IS_ONLY_FOR_VECTORS);
|
||||
EIGEN_STATIC_ASSERT(DerivedE::IsVectorAtCompileTime, THIS_METHOD_IS_ONLY_FOR_VECTORS);
|
||||
EIGEN_STATIC_ASSERT((NumTraits<typename DerivedD::Scalar>::IsComplex == 0),
|
||||
THIS_FUNCTION_IS_NOT_FOR_COMPLEX_VALUED_MATRICES);
|
||||
EIGEN_STATIC_ASSERT((NumTraits<typename DerivedE::Scalar>::IsComplex == 0),
|
||||
THIS_FUNCTION_IS_NOT_FOR_COMPLEX_VALUED_MATRICES);
|
||||
|
||||
using std::abs;
|
||||
const Index n = diagonal.size();
|
||||
eigen_assert((n == 0 || superdiagonal.size() == n - 1) && "superdiagonal must have size diagonal.size() - 1");
|
||||
|
||||
// For a bidiagonal matrix, rows == cols == n.
|
||||
allocate(n, n, computationOptions);
|
||||
|
||||
if (n == 0) {
|
||||
m_isInitialized = true;
|
||||
m_info = Success;
|
||||
m_nonzeroSingularValues = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Check for non-finite inputs.
|
||||
const RealScalar diagScale = diagonal.cwiseAbs().template maxCoeff<PropagateNaN>();
|
||||
const RealScalar superdiagScale = n > 1 ? superdiagonal.cwiseAbs().template maxCoeff<PropagateNaN>() : RealScalar(0);
|
||||
RealScalar scale = numext::maxi(diagScale, superdiagScale);
|
||||
if (!(numext::isfinite)(scale)) {
|
||||
m_isInitialized = true;
|
||||
m_info = InvalidInput;
|
||||
return *this;
|
||||
}
|
||||
|
||||
const RealScalar considerZero = (std::numeric_limits<RealScalar>::min)();
|
||||
if (numext::is_exactly_zero(scale)) scale = Literal(1);
|
||||
|
||||
//**** Small problem: build dense bidiagonal and delegate to JacobiSVD.
|
||||
if (n < m_impl.algoSwap()) {
|
||||
// Build the dense upper bidiagonal matrix.
|
||||
MatrixX B = MatrixX::Zero(n, n);
|
||||
B.diagonal() = diagonal.template cast<Scalar>() / Scalar(scale);
|
||||
if (n > 1) B.diagonal(1) = superdiagonal.template cast<Scalar>() / Scalar(scale);
|
||||
smallSvd.compute(B);
|
||||
m_isInitialized = true;
|
||||
m_info = smallSvd.info();
|
||||
if (m_info == Success || m_info == NoConvergence) {
|
||||
m_singularValues = smallSvd.singularValues() * scale;
|
||||
m_nonzeroSingularValues = smallSvd.nonzeroSingularValues();
|
||||
if (computeU()) m_matrixU = smallSvd.matrixU();
|
||||
if (computeV()) m_matrixV = smallSvd.matrixV();
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
//**** Fill m_computed with transposed bidiagonal format.
|
||||
// D&C operates on B^T: m_computed(i,i) = d_i, m_computed(i+1,i) = e_i.
|
||||
m_impl.naiveU().setZero();
|
||||
m_impl.naiveV().setZero();
|
||||
m_impl.computed().setZero();
|
||||
for (Index i = 0; i < n; ++i) {
|
||||
m_impl.computed()(i, i) = RealScalar(diagonal.coeff(i)) / scale;
|
||||
}
|
||||
for (Index i = 0; i < n - 1; ++i) {
|
||||
m_impl.computed()(i + 1, i) = RealScalar(superdiagonal.coeff(i)) / scale;
|
||||
}
|
||||
|
||||
m_isTranspose = false;
|
||||
|
||||
//**** Run D&C.
|
||||
m_impl.divide(0, n - 1, 0, 0, 0);
|
||||
m_info = m_impl.info();
|
||||
m_numIters = m_impl.numIters();
|
||||
if (m_info != Success && m_info != NoConvergence) {
|
||||
m_isInitialized = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
//**** Extract singular values.
|
||||
for (int i = 0; i < diagSize(); i++) {
|
||||
RealScalar a = abs(m_impl.computed().coeff(i, i));
|
||||
m_singularValues.coeffRef(i) = a * scale;
|
||||
if (a < considerZero) {
|
||||
m_nonzeroSingularValues = i;
|
||||
m_singularValues.tail(diagSize() - i - 1).setZero();
|
||||
break;
|
||||
} else if (i == diagSize() - 1) {
|
||||
m_nonzeroSingularValues = i + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
//**** Copy U and V directly (no Householder to apply).
|
||||
// D&C computes B^T = naiveU * S * naiveV^T, so B = naiveV * S * naiveU^T.
|
||||
// Thus U_of_B = naiveV, V_of_B = naiveU.
|
||||
if (computeU()) {
|
||||
Index Ucols = m_computeThinU ? diagSize() : rows();
|
||||
m_matrixU = MatrixX::Identity(rows(), Ucols);
|
||||
m_matrixU.topLeftCorner(diagSize(), diagSize()) =
|
||||
m_impl.naiveV().template cast<Scalar>().topLeftCorner(diagSize(), diagSize());
|
||||
}
|
||||
if (computeV()) {
|
||||
Index Vcols = m_computeThinV ? diagSize() : cols();
|
||||
m_matrixV = MatrixX::Identity(cols(), Vcols);
|
||||
m_matrixV.topLeftCorner(diagSize(), diagSize()) =
|
||||
m_impl.naiveU().template cast<Scalar>().topLeftCorner(diagSize(), diagSize());
|
||||
}
|
||||
|
||||
m_isInitialized = true;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/** \svd_module
|
||||
*
|
||||
* \return the singular value decomposition of \c *this computed by Divide & Conquer algorithm
|
||||
|
||||
@@ -25,18 +25,8 @@
|
||||
|
||||
namespace Eigen {
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
IOFormat bdcsvdfmt(8, 0, ", ", "\n", " [", "]");
|
||||
#endif
|
||||
|
||||
namespace internal {
|
||||
|
||||
#ifdef EIGEN_BDCSVD_SANITY_CHECKS
|
||||
#define BDCSVD_SANITY_CHECK(expr) eigen_internal_assert(expr)
|
||||
#else
|
||||
#define BDCSVD_SANITY_CHECK(expr)
|
||||
#endif
|
||||
|
||||
/** \internal
|
||||
* Implementation of the divide-and-conquer phase of BDCSVD.
|
||||
*
|
||||
@@ -256,10 +246,6 @@ void bdcsvd_impl<RealScalar_>::divide(Index firstCol, Index lastCol, Index first
|
||||
s0 = betaK * phi / r0;
|
||||
}
|
||||
|
||||
BDCSVD_SANITY_CHECK(m_naiveU.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_naiveV.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_computed.allFinite());
|
||||
|
||||
if (m_compU) {
|
||||
MatrixXr q1(m_naiveU.col(firstCol + k).segment(firstCol, k + 1));
|
||||
// we shiftW Q1 to the right
|
||||
@@ -290,39 +276,18 @@ void bdcsvd_impl<RealScalar_>::divide(Index firstCol, Index lastCol, Index first
|
||||
m_naiveU.row(0).segment(firstCol + k + 1, n - k - 1).setZero();
|
||||
}
|
||||
|
||||
BDCSVD_SANITY_CHECK(m_naiveU.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_naiveV.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_computed.allFinite());
|
||||
|
||||
m_computed(firstCol + shift, firstCol + shift) = r0;
|
||||
m_computed.col(firstCol + shift).segment(firstCol + shift + 1, k) = alphaK * l.transpose();
|
||||
m_computed.col(firstCol + shift).segment(firstCol + shift + k + 1, n - k - 1) = betaK * f.transpose();
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
ArrayXr tmp1 = (m_computed.block(firstCol + shift, firstCol + shift, n, n)).jacobiSvd().singularValues();
|
||||
#endif
|
||||
// Second part: try to deflate singular values in combined matrix
|
||||
deflation(firstCol, lastCol, k, firstRowW, firstColW, shift);
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
ArrayXr tmp2 = (m_computed.block(firstCol + shift, firstCol + shift, n, n)).jacobiSvd().singularValues();
|
||||
std::cout << "\n\nj1 = " << tmp1.transpose().format(bdcsvdfmt) << "\n";
|
||||
std::cout << "j2 = " << tmp2.transpose().format(bdcsvdfmt) << "\n\n";
|
||||
std::cout << "err: " << ((tmp1 - tmp2).abs() > 1e-12 * tmp2.abs()).transpose() << "\n";
|
||||
static int count = 0;
|
||||
std::cout << "# " << ++count << "\n\n";
|
||||
eigen_internal_assert((tmp1 - tmp2).matrix().norm() < 1e-14 * tmp2.matrix().norm());
|
||||
// eigen_internal_assert(count<681);
|
||||
// eigen_internal_assert(((tmp1-tmp2).abs()<1e-13*tmp2.abs()).all());
|
||||
#endif
|
||||
|
||||
// Third part: compute SVD of combined matrix
|
||||
MatrixXr UofSVD, VofSVD;
|
||||
VectorType singVals;
|
||||
computeSVDofM(firstCol + shift, n, UofSVD, singVals, VofSVD);
|
||||
|
||||
BDCSVD_SANITY_CHECK(UofSVD.allFinite());
|
||||
BDCSVD_SANITY_CHECK(VofSVD.allFinite());
|
||||
|
||||
if (m_compU)
|
||||
structured_update(m_naiveU.block(firstCol, firstCol, n + 1, n + 1), UofSVD, (n + 2) / 2);
|
||||
else {
|
||||
@@ -333,10 +298,6 @@ void bdcsvd_impl<RealScalar_>::divide(Index firstCol, Index lastCol, Index first
|
||||
|
||||
if (m_compV) structured_update(m_naiveV.block(firstRowW, firstColW, n, n), VofSVD, (n + 1) / 2);
|
||||
|
||||
BDCSVD_SANITY_CHECK(m_naiveU.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_naiveV.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_computed.allFinite());
|
||||
|
||||
m_computed.block(firstCol + shift, firstCol + shift, n, n).setZero();
|
||||
m_computed.block(firstCol + shift, firstCol + shift, n, n).diagonal() = singVals;
|
||||
} // end divide
|
||||
@@ -364,10 +325,6 @@ void bdcsvd_impl<RealScalar_>::computeSVDofM(Index firstCol, Index n, MatrixXr&
|
||||
U.resize(n + 1, n + 1);
|
||||
if (m_compV) V.resize(n, n);
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
if (col0.hasNaN() || diag.hasNaN()) std::cout << "\n\nHAS NAN\n\n";
|
||||
#endif
|
||||
|
||||
// Many singular values might have been deflated, the zero ones have been moved to the end,
|
||||
// but others are interleaved and we must ignore them at this stage.
|
||||
// To this end, let's compute a permutation skipping them:
|
||||
@@ -385,63 +342,14 @@ void bdcsvd_impl<RealScalar_>::computeSVDofM(Index firstCol, Index n, MatrixXr&
|
||||
Map<ArrayXr> mus(m_workspace.data() + 2 * n, n);
|
||||
Map<ArrayXr> zhat(m_workspace.data() + 3 * n, n);
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "computeSVDofM using:\n";
|
||||
std::cout << " z: " << col0.transpose() << "\n";
|
||||
std::cout << " d: " << diag.transpose() << "\n";
|
||||
#endif
|
||||
|
||||
// Compute singVals, shifts, and mus
|
||||
computeSingVals(col0, diag, perm, singVals, shifts, mus);
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << " j: "
|
||||
<< (m_computed.block(firstCol, firstCol, n, n)).jacobiSvd().singularValues().transpose().reverse()
|
||||
<< "\n\n";
|
||||
std::cout << " sing-val: " << singVals.transpose() << "\n";
|
||||
std::cout << " mu: " << mus.transpose() << "\n";
|
||||
std::cout << " shift: " << shifts.transpose() << "\n";
|
||||
|
||||
{
|
||||
std::cout << "\n\n mus: " << mus.head(actual_n).transpose() << "\n\n";
|
||||
std::cout << " check1 (expect0) : "
|
||||
<< ((singVals.array() - (shifts + mus)) / singVals.array()).head(actual_n).transpose() << "\n\n";
|
||||
eigen_internal_assert((((singVals.array() - (shifts + mus)) / singVals.array()).head(actual_n) >= 0).all());
|
||||
std::cout << " check2 (>0) : " << ((singVals.array() - diag) / singVals.array()).head(actual_n).transpose()
|
||||
<< "\n\n";
|
||||
eigen_internal_assert((((singVals.array() - diag) / singVals.array()).head(actual_n) >= 0).all());
|
||||
}
|
||||
#endif
|
||||
|
||||
BDCSVD_SANITY_CHECK(singVals.allFinite());
|
||||
BDCSVD_SANITY_CHECK(mus.allFinite());
|
||||
BDCSVD_SANITY_CHECK(shifts.allFinite());
|
||||
|
||||
// Compute zhat
|
||||
perturbCol0(col0, diag, perm, singVals, shifts, mus, zhat);
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << " zhat: " << zhat.transpose() << "\n";
|
||||
#endif
|
||||
|
||||
BDCSVD_SANITY_CHECK(zhat.allFinite());
|
||||
|
||||
computeSingVecs(zhat, diag, perm, singVals, shifts, mus, U, V);
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "U^T U: " << (U.transpose() * U - MatrixXr(MatrixXr::Identity(U.cols(), U.cols()))).norm() << "\n";
|
||||
std::cout << "V^T V: " << (V.transpose() * V - MatrixXr(MatrixXr::Identity(V.cols(), V.cols()))).norm() << "\n";
|
||||
#endif
|
||||
|
||||
BDCSVD_SANITY_CHECK(m_naiveU.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_naiveV.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_computed.allFinite());
|
||||
BDCSVD_SANITY_CHECK(U.allFinite());
|
||||
BDCSVD_SANITY_CHECK(V.allFinite());
|
||||
// BDCSVD_SANITY_CHECK((U.transpose() * U - MatrixXr(MatrixXr::Identity(U.cols(),U.cols()))).norm() <
|
||||
// 100*NumTraits<RealScalar>::epsilon() * n);
|
||||
// BDCSVD_SANITY_CHECK((V.transpose() * V -
|
||||
// MatrixXr(MatrixXr::Identity(V.cols(),V.cols()))).norm() < 100*NumTraits<RealScalar>::epsilon() * n);
|
||||
|
||||
// Because of deflation, the singular values might not be completely sorted.
|
||||
// Fortunately, reordering them is a O(n) problem
|
||||
for (Index i = 0; i < actual_n - 1; ++i) {
|
||||
@@ -453,28 +361,11 @@ void bdcsvd_impl<RealScalar_>::computeSVDofM(Index firstCol, Index n, MatrixXr&
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef EIGEN_BDCSVD_SANITY_CHECKS
|
||||
{
|
||||
bool singular_values_sorted =
|
||||
(((singVals.segment(1, actual_n - 1) - singVals.head(actual_n - 1))).array() >= 0).all();
|
||||
if (!singular_values_sorted)
|
||||
std::cout << "Singular values are not sorted: " << singVals.segment(1, actual_n).transpose() << "\n";
|
||||
eigen_internal_assert(singular_values_sorted);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Reverse order so that singular values in increased order
|
||||
// Because of deflation, the zeros singular-values are already at the end
|
||||
singVals.head(actual_n).reverseInPlace();
|
||||
U.leftCols(actual_n).rowwise().reverseInPlace();
|
||||
if (m_compV) V.leftCols(actual_n).rowwise().reverseInPlace();
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
JacobiSVD<MatrixXr> jsvd(m_computed.block(firstCol, firstCol, n, n));
|
||||
std::cout << " * j: " << jsvd.singularValues().transpose() << "\n\n";
|
||||
std::cout << " * sing-val: " << singVals.transpose() << "\n";
|
||||
// std::cout << " * err: " << ((jsvd.singularValues()-singVals)>1e-13*singVals.norm()).transpose() << "\n";
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename RealScalar_>
|
||||
@@ -537,25 +428,6 @@ void bdcsvd_impl<RealScalar_>::computeSingVals(const ArrayRef& col0, const Array
|
||||
// first decide whether it's closer to the left end or the right end
|
||||
RealScalar mid = left + (right - left) / Literal(2);
|
||||
RealScalar fMid = secularEq(mid, col0, diag, perm, diag, Literal(0));
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "right-left = " << right - left << "\n";
|
||||
// std::cout << "fMid = " << fMid << " " << secularEq(mid-left, col0, diag, perm, ArrayXr(diag-left), left)
|
||||
// << " " << secularEq(mid-right, col0, diag, perm, ArrayXr(diag-right), right) <<
|
||||
// "\n";
|
||||
std::cout << " = " << secularEq(left + RealScalar(0.000001) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.1) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.2) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.3) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.4) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.49) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.5) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.51) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.6) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.7) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.8) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.9) * (right - left), col0, diag, perm, diag, 0) << " "
|
||||
<< secularEq(left + RealScalar(0.999999) * (right - left), col0, diag, perm, diag, 0) << "\n";
|
||||
#endif
|
||||
RealScalar shift = (k == actual_n - 1 || fMid > Literal(0)) ? left : right;
|
||||
|
||||
// measure everything relative to shift
|
||||
@@ -612,8 +484,6 @@ void bdcsvd_impl<RealScalar_>::computeSingVals(const ArrayRef& col0, const Array
|
||||
RealScalar muZero = -a / b;
|
||||
RealScalar fZero = secularEq(muZero, col0, diag, perm, diagShifted, shift);
|
||||
|
||||
BDCSVD_SANITY_CHECK((numext::isfinite)(fZero));
|
||||
|
||||
muPrev = muCur;
|
||||
fPrev = fCur;
|
||||
muCur = muZero;
|
||||
@@ -627,9 +497,6 @@ void bdcsvd_impl<RealScalar_>::computeSingVals(const ArrayRef& col0, const Array
|
||||
|
||||
// fall back on bisection method if rational interpolation did not work
|
||||
if (useBisection) {
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "useBisection for k = " << k << ", actual_n = " << actual_n << "\n";
|
||||
#endif
|
||||
RealScalar leftShifted, rightShifted;
|
||||
// we can test exact equality here, because shift comes from `... ? left : right`
|
||||
if (numext::equal_strict(shift, left)) {
|
||||
@@ -642,8 +509,6 @@ void bdcsvd_impl<RealScalar_>::computeSingVals(const ArrayRef& col0, const Array
|
||||
// check that we did it right:
|
||||
eigen_internal_assert(
|
||||
(numext::isfinite)((col0(k) / leftShifted) * (col0(k) / (diag(k) + shift + leftShifted))));
|
||||
// It is unclear why k==0 would need special handling here:
|
||||
// if (k == 0) rightShifted = right - left; else
|
||||
rightShifted = (k == actual_n - 1)
|
||||
? right
|
||||
: ((right - left) * RealScalar(0.51)); // theoretically we can take 0.5, but let's be safe
|
||||
@@ -658,33 +523,6 @@ void bdcsvd_impl<RealScalar_>::computeSingVals(const ArrayRef& col0, const Array
|
||||
RealScalar fLeft = secularEq(leftShifted, col0, diag, perm, diagShifted, shift);
|
||||
eigen_internal_assert(fLeft < Literal(0));
|
||||
|
||||
#if defined EIGEN_BDCSVD_DEBUG_VERBOSE || defined EIGEN_BDCSVD_SANITY_CHECKS || defined EIGEN_INTERNAL_DEBUGGING
|
||||
RealScalar fRight = secularEq(rightShifted, col0, diag, perm, diagShifted, shift);
|
||||
#endif
|
||||
|
||||
#ifdef EIGEN_BDCSVD_SANITY_CHECKS
|
||||
if (!(numext::isfinite)(fLeft))
|
||||
std::cout << "f(" << leftShifted << ") =" << fLeft << " ; " << left << " " << shift << " " << right << "\n";
|
||||
eigen_internal_assert((numext::isfinite)(fLeft));
|
||||
|
||||
if (!(numext::isfinite)(fRight))
|
||||
std::cout << "f(" << rightShifted << ") =" << fRight << " ; " << left << " " << shift << " " << right << "\n";
|
||||
// eigen_internal_assert((numext::isfinite)(fRight));
|
||||
#endif
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
if (!(fLeft * fRight < 0)) {
|
||||
std::cout << "f(leftShifted) using leftShifted=" << leftShifted
|
||||
<< " ; diagShifted(1:10):" << diagShifted.head(10).transpose() << "\n ; "
|
||||
<< "left==shift=" << bool(left == shift) << " ; left-shift = " << (left - shift) << "\n";
|
||||
std::cout << "k=" << k << ", " << fLeft << " * " << fRight << " == " << fLeft * fRight << " ; "
|
||||
<< "[" << left << " .. " << right << "] -> [" << leftShifted << " " << rightShifted
|
||||
<< "], shift=" << shift << " , f(right)=" << secularEq(0, col0, diag, perm, diagShifted, shift)
|
||||
<< " == " << secularEq(right, col0, diag, perm, diag, 0) << " == " << fRight << "\n";
|
||||
}
|
||||
#endif
|
||||
eigen_internal_assert(fLeft * fRight < Literal(0));
|
||||
|
||||
if (fLeft < Literal(0)) {
|
||||
while (rightShifted - leftShifted > Literal(2) * NumTraits<RealScalar>::epsilon() *
|
||||
numext::maxi<RealScalar>(abs(leftShifted), abs(rightShifted))) {
|
||||
@@ -714,20 +552,6 @@ void bdcsvd_impl<RealScalar_>::computeSingVals(const ArrayRef& col0, const Array
|
||||
singVals[k] = shift + muCur;
|
||||
shifts[k] = shift;
|
||||
mus[k] = muCur;
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
if (k + 1 < n)
|
||||
std::cout << "found " << singVals[k] << " == " << shift << " + " << muCur << " from " << diag(k) << " .. "
|
||||
<< diag(k + 1) << "\n";
|
||||
#endif
|
||||
BDCSVD_SANITY_CHECK(k == 0 || singVals[k] >= singVals[k - 1]);
|
||||
BDCSVD_SANITY_CHECK(singVals[k] >= diag(k));
|
||||
|
||||
// perturb singular value slightly if it equals diagonal entry to avoid division by zero later
|
||||
// (deflation is supposed to avoid this from happening)
|
||||
// - this does no seem to be necessary anymore -
|
||||
// if (singVals[k] == left) singVals[k] *= 1 + NumTraits<RealScalar>::epsilon();
|
||||
// if (singVals[k] == right) singVals[k] *= 1 - NumTraits<RealScalar>::epsilon();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -752,59 +576,15 @@ void bdcsvd_impl<RealScalar_>::perturbCol0(const ArrayRef& col0, const ArrayRef&
|
||||
// see equation (3.6)
|
||||
RealScalar dk = diag(k);
|
||||
RealScalar prod = (singVals(lastIdx) + dk) * (mus(lastIdx) + (shifts(lastIdx) - dk));
|
||||
#ifdef EIGEN_BDCSVD_SANITY_CHECKS
|
||||
if (prod < 0) {
|
||||
std::cout << "k = " << k << " ; z(k)=" << col0(k) << ", diag(k)=" << dk << "\n";
|
||||
std::cout << "prod = "
|
||||
<< "(" << singVals(lastIdx) << " + " << dk << ") * (" << mus(lastIdx) << " + (" << shifts(lastIdx)
|
||||
<< " - " << dk << "))"
|
||||
<< "\n";
|
||||
std::cout << " = " << singVals(lastIdx) + dk << " * " << mus(lastIdx) + (shifts(lastIdx) - dk) << "\n";
|
||||
}
|
||||
eigen_internal_assert(prod >= 0);
|
||||
#endif
|
||||
|
||||
for (Index l = 0; l < m; ++l) {
|
||||
Index i = perm(l);
|
||||
if (i != k) {
|
||||
#ifdef EIGEN_BDCSVD_SANITY_CHECKS
|
||||
if (i >= k && (l == 0 || l - 1 >= m)) {
|
||||
std::cout << "Error in perturbCol0\n";
|
||||
std::cout << " " << k << "/" << n << " " << l << "/" << m << " " << i << "/" << n << " ; " << col0(k)
|
||||
<< " " << diag(k) << " "
|
||||
<< "\n";
|
||||
std::cout << " " << diag(i) << "\n";
|
||||
Index j = (i < k /*|| l==0*/) ? i : perm(l - 1);
|
||||
std::cout << " "
|
||||
<< "j=" << j << "\n";
|
||||
}
|
||||
#endif
|
||||
Index j = i < k ? i : l > 0 ? perm(l - 1) : i;
|
||||
#ifdef EIGEN_BDCSVD_SANITY_CHECKS
|
||||
if (!(dk != Literal(0) || diag(i) != Literal(0))) {
|
||||
std::cout << "k=" << k << ", i=" << i << ", l=" << l << ", perm.size()=" << perm.size() << "\n";
|
||||
}
|
||||
eigen_internal_assert(dk != Literal(0) || diag(i) != Literal(0));
|
||||
#endif
|
||||
prod *= ((singVals(j) + dk) / ((diag(i) + dk))) * ((mus(j) + (shifts(j) - dk)) / ((diag(i) - dk)));
|
||||
BDCSVD_SANITY_CHECK(prod >= 0);
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
if (i != k &&
|
||||
numext::abs(((singVals(j) + dk) * (mus(j) + (shifts(j) - dk))) / ((diag(i) + dk) * (diag(i) - dk)) - 1) >
|
||||
0.9)
|
||||
std::cout << " "
|
||||
<< ((singVals(j) + dk) * (mus(j) + (shifts(j) - dk))) / ((diag(i) + dk) * (diag(i) - dk))
|
||||
<< " == (" << (singVals(j) + dk) << " * " << (mus(j) + (shifts(j) - dk)) << ") / ("
|
||||
<< (diag(i) + dk) << " * " << (diag(i) - dk) << ")\n";
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "zhat(" << k << ") = sqrt( " << prod << ") ; " << (singVals(lastIdx) + dk) << " * "
|
||||
<< mus(lastIdx) + shifts(lastIdx) << " - " << dk << "\n";
|
||||
#endif
|
||||
RealScalar tmp = sqrt(prod);
|
||||
BDCSVD_SANITY_CHECK((numext::isfinite)(tmp));
|
||||
zhat(k) = col0(k) > Literal(0) ? RealScalar(tmp) : RealScalar(-tmp);
|
||||
}
|
||||
}
|
||||
@@ -883,14 +663,6 @@ void bdcsvd_impl<RealScalar_>::deflation44(Index firstColu, Index firstColm, Ind
|
||||
RealScalar s = m_computed(firstColm + i, firstColm);
|
||||
RealScalar c = m_computed(firstColm + j, firstColm);
|
||||
RealScalar r = numext::hypot(c, s);
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "deflation 4.4: " << i << "," << j << " -> " << c << " " << s << " " << r << " ; "
|
||||
<< m_computed(firstColm + i - 1, firstColm) << " " << m_computed(firstColm + i, firstColm) << " "
|
||||
<< m_computed(firstColm + i + 1, firstColm) << " " << m_computed(firstColm + i + 2, firstColm) << "\n";
|
||||
std::cout << m_computed(firstColm + i - 1, firstColm + i - 1) << " " << m_computed(firstColm + i, firstColm + i)
|
||||
<< " " << m_computed(firstColm + i + 1, firstColm + i + 1) << " "
|
||||
<< m_computed(firstColm + i + 2, firstColm + i + 2) << "\n";
|
||||
#endif
|
||||
if (numext::is_exactly_zero(r)) {
|
||||
m_computed(firstColm + j, firstColm + j) = m_computed(firstColm + i, firstColm + i);
|
||||
return;
|
||||
@@ -927,51 +699,23 @@ void bdcsvd_impl<RealScalar_>::deflation(Index firstCol, Index lastCol, Index k,
|
||||
RealScalar epsilon_coarse =
|
||||
Literal(8) * NumTraits<RealScalar>::epsilon() * numext::maxi<RealScalar>(col0.cwiseAbs().maxCoeff(), maxDiag);
|
||||
|
||||
BDCSVD_SANITY_CHECK(m_naiveU.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_naiveV.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_computed.allFinite());
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "\ndeflate:" << diag.head(k + 1).transpose() << " | "
|
||||
<< diag.segment(k + 1, length - k - 1).transpose() << "\n";
|
||||
#endif
|
||||
|
||||
// condition 4.1
|
||||
if (diag(0) < epsilon_coarse) {
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "deflation 4.1, because " << diag(0) << " < " << epsilon_coarse << "\n";
|
||||
#endif
|
||||
diag(0) = epsilon_coarse;
|
||||
}
|
||||
|
||||
// condition 4.2
|
||||
for (Index i = 1; i < length; ++i)
|
||||
if (abs(col0(i)) < epsilon_strict) {
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "deflation 4.2, set z(" << i << ") to zero because " << abs(col0(i)) << " < " << epsilon_strict
|
||||
<< " (diag(" << i << ")=" << diag(i) << ")\n";
|
||||
#endif
|
||||
col0(i) = Literal(0);
|
||||
}
|
||||
|
||||
// condition 4.3
|
||||
for (Index i = 1; i < length; i++)
|
||||
if (diag(i) < epsilon_coarse) {
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "deflation 4.3, cancel z(" << i << ")=" << col0(i) << " because diag(" << i << ")=" << diag(i)
|
||||
<< " < " << epsilon_coarse << "\n";
|
||||
#endif
|
||||
deflation43(firstCol, shift, i, length);
|
||||
}
|
||||
|
||||
BDCSVD_SANITY_CHECK(m_naiveU.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_naiveV.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_computed.allFinite());
|
||||
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "to be sorted: " << diag.transpose() << "\n\n";
|
||||
std::cout << " : " << col0.transpose() << "\n\n";
|
||||
#endif
|
||||
{
|
||||
// Check for total deflation:
|
||||
// If we have a total deflation, then we have to consider col0(0)==diag(0) as a singular value during sorting.
|
||||
@@ -1052,10 +796,6 @@ void bdcsvd_impl<RealScalar_>::deflation(Index firstCol, Index lastCol, Index k,
|
||||
realInd[i] = pi;
|
||||
}
|
||||
}
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "sorted: " << diag.transpose().format(bdcsvdfmt) << "\n";
|
||||
std::cout << " : " << col0.transpose() << "\n\n";
|
||||
#endif
|
||||
|
||||
// condition 4.4
|
||||
{
|
||||
@@ -1065,23 +805,12 @@ void bdcsvd_impl<RealScalar_>::deflation(Index firstCol, Index lastCol, Index k,
|
||||
|
||||
for (; i > 1; --i)
|
||||
if ((diag(i) - diag(i - 1)) < epsilon_strict) {
|
||||
#ifdef EIGEN_BDCSVD_DEBUG_VERBOSE
|
||||
std::cout << "deflation 4.4 with i = " << i << " because " << diag(i) << " - " << diag(i - 1)
|
||||
<< " == " << (diag(i) - diag(i - 1)) << " < " << epsilon_strict << "\n";
|
||||
#endif
|
||||
eigen_internal_assert(abs(diag(i) - diag(i - 1)) < epsilon_coarse &&
|
||||
" diagonal entries are not properly sorted");
|
||||
deflation44(firstCol, firstCol + shift, firstRowW, firstColW, i, i - 1, length);
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef EIGEN_BDCSVD_SANITY_CHECKS
|
||||
for (Index j = 2; j < length; ++j) eigen_internal_assert(diag(j - 1) <= diag(j) || abs(diag(j)) < considerZero);
|
||||
#endif
|
||||
|
||||
BDCSVD_SANITY_CHECK(m_naiveU.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_naiveV.allFinite());
|
||||
BDCSVD_SANITY_CHECK(m_computed.allFinite());
|
||||
} // end deflation
|
||||
|
||||
} // end namespace internal
|
||||
|
||||
210
test/bdcsvd.cpp
210
test/bdcsvd.cpp
@@ -85,6 +85,212 @@ void bdcsvd_check_convergence(const MatrixType& input) {
|
||||
VERIFY_IS_APPROX(input, D);
|
||||
}
|
||||
|
||||
// Verify SVD of bidiagonal matrix given as diagonal + superdiagonal vectors.
|
||||
template <typename RealScalar>
|
||||
void verify_bidiagonal_svd(const Matrix<RealScalar, Dynamic, 1>& diag,
|
||||
const Matrix<RealScalar, Dynamic, 1>& superdiag) {
|
||||
typedef Matrix<RealScalar, Dynamic, Dynamic> MatrixXr;
|
||||
typedef Matrix<RealScalar, Dynamic, 1> VectorXr;
|
||||
const Index n = diag.size();
|
||||
|
||||
BDCSVD<MatrixXr, ComputeFullU | ComputeFullV> bdcsvd(diag, superdiag);
|
||||
VERIFY(bdcsvd.info() == Success);
|
||||
|
||||
const VectorXr& sv = bdcsvd.singularValues();
|
||||
|
||||
// Singular values must be non-negative.
|
||||
for (Index i = 0; i < sv.size(); ++i) {
|
||||
VERIFY(sv(i) >= RealScalar(0));
|
||||
}
|
||||
|
||||
// Singular values must be sorted descending.
|
||||
for (Index i = 1; i < sv.size(); ++i) {
|
||||
VERIFY(sv(i - 1) >= sv(i));
|
||||
}
|
||||
|
||||
// Orthogonality of U and V.
|
||||
VERIFY_IS_APPROX(bdcsvd.matrixU().transpose() * bdcsvd.matrixU(), MatrixXr::Identity(n, n));
|
||||
VERIFY_IS_APPROX(bdcsvd.matrixV().transpose() * bdcsvd.matrixV(), MatrixXr::Identity(n, n));
|
||||
|
||||
// Reconstruction: U * S * V^T should equal the original bidiagonal.
|
||||
MatrixXr B = MatrixXr::Zero(n, n);
|
||||
B.diagonal() = diag;
|
||||
if (n > 1) B.diagonal(1) = superdiag;
|
||||
MatrixXr recon = bdcsvd.matrixU() * sv.asDiagonal() * bdcsvd.matrixV().transpose();
|
||||
VERIFY_IS_APPROX(recon, B);
|
||||
|
||||
// Cross-validate singular values against JacobiSVD.
|
||||
JacobiSVD<MatrixXr> jacobi(B);
|
||||
VERIFY_IS_APPROX(sv, jacobi.singularValues());
|
||||
}
|
||||
|
||||
// Verify that bidiagonal API and matrix API produce matching singular values.
|
||||
template <typename RealScalar>
|
||||
void verify_bidiagonal_vs_matrix_svd(const Matrix<RealScalar, Dynamic, 1>& diag,
|
||||
const Matrix<RealScalar, Dynamic, 1>& superdiag) {
|
||||
typedef Matrix<RealScalar, Dynamic, Dynamic> MatrixXr;
|
||||
const Index n = diag.size();
|
||||
|
||||
// Build dense bidiagonal matrix.
|
||||
MatrixXr B = MatrixXr::Zero(n, n);
|
||||
B.diagonal() = diag;
|
||||
if (n > 1) B.diagonal(1) = superdiag;
|
||||
|
||||
BDCSVD<MatrixXr> bidiag_svd(diag, superdiag);
|
||||
BDCSVD<MatrixXr> matrix_svd(B);
|
||||
|
||||
VERIFY(bidiag_svd.info() == Success);
|
||||
VERIFY(matrix_svd.info() == Success);
|
||||
VERIFY_IS_APPROX(bidiag_svd.singularValues(), matrix_svd.singularValues());
|
||||
}
|
||||
|
||||
template <typename RealScalar>
|
||||
void bdcsvd_bidiagonal_hard_cases() {
|
||||
using std::abs;
|
||||
using std::cos;
|
||||
using std::pow;
|
||||
using std::sin;
|
||||
typedef Matrix<RealScalar, Dynamic, 1> VectorXr;
|
||||
|
||||
Eigen::internal::set_is_malloc_allowed(true);
|
||||
|
||||
const RealScalar eps = NumTraits<RealScalar>::epsilon();
|
||||
|
||||
// Test sizes: cover n=1, very small, below/above algoSwap (16), and larger.
|
||||
const int sizes[] = {1, 2, 3, 5, 10, 16, 20, 50, 100};
|
||||
const int numSizes = sizeof(sizes) / sizeof(sizes[0]);
|
||||
|
||||
for (int si = 0; si < numSizes; ++si) {
|
||||
const Index n = sizes[si];
|
||||
VectorXr diag(n), superdiag(n > 1 ? n - 1 : 0);
|
||||
|
||||
// 1. Identity: d=[1,...,1], e=[0,...,0]
|
||||
diag.setOnes();
|
||||
superdiag.setZero();
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
verify_bidiagonal_vs_matrix_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 2. Zero: d=[0,...,0], e=[0,...,0]
|
||||
diag.setZero();
|
||||
superdiag.setZero();
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 3. Scalar (only meaningful for n=1, but runs for all)
|
||||
if (n == 1) {
|
||||
diag(0) = RealScalar(3.14);
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
}
|
||||
|
||||
// 4. Golub-Kahan: d=[1,...,1], e=[1,...,1]
|
||||
diag.setOnes();
|
||||
if (n > 1) superdiag.setOnes();
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 5. Kahan matrix: d_i = s^(i-1), e_i = -c*s^(i-1)
|
||||
// Clamp exponents so condition number stays bounded by 1/eps.
|
||||
{
|
||||
const RealScalar theta = RealScalar(0.3);
|
||||
const RealScalar s = sin(theta);
|
||||
const RealScalar c = cos(theta);
|
||||
using std::log;
|
||||
const RealScalar maxPower = -log(eps) / (-log(s));
|
||||
for (Index i = 0; i < n; ++i) diag(i) = pow(s, numext::mini(RealScalar(i), maxPower));
|
||||
for (Index i = 0; i < n - 1; ++i) superdiag(i) = -c * pow(s, numext::mini(RealScalar(i), maxPower));
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
}
|
||||
|
||||
// 6. Geometric decay diagonal: d_i = 0.5^i, e=[0,...,0]
|
||||
// Clamp so condition number stays bounded by 1/eps.
|
||||
{
|
||||
using std::log;
|
||||
const RealScalar base = RealScalar(0.5);
|
||||
const RealScalar maxPower = -log(eps) / (-log(base));
|
||||
for (Index i = 0; i < n; ++i) diag(i) = pow(base, numext::mini(RealScalar(i), maxPower));
|
||||
superdiag.setZero();
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
}
|
||||
|
||||
// 7. Geometric decay superdiagonal: d=[1,...,1], e_i = 0.5^i
|
||||
diag.setOnes();
|
||||
for (Index i = 0; i < n - 1; ++i) superdiag(i) = pow(RealScalar(0.5), RealScalar(i));
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 8. Clustered at 1: d_i = 1 + i*eps, e=[0,...,0]
|
||||
for (Index i = 0; i < n; ++i) diag(i) = RealScalar(1) + RealScalar(i) * eps;
|
||||
superdiag.setZero();
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 9. Two clusters: half ≈ 1, half ≈ eps
|
||||
for (Index i = 0; i < n; ++i) diag(i) = (i < n / 2) ? RealScalar(1) : eps;
|
||||
superdiag.setZero();
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 10. Single tiny singular value: d=[1,...,1,eps], e=[eps^2,...]
|
||||
diag.setOnes();
|
||||
diag(n - 1) = eps;
|
||||
for (Index i = 0; i < n - 1; ++i) superdiag(i) = eps * eps;
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 11. Graded: d_i = 10^(-i), e_i = 10^(-i)
|
||||
for (Index i = 0; i < n; ++i) diag(i) = pow(RealScalar(10), -RealScalar(i));
|
||||
for (Index i = 0; i < n - 1; ++i) superdiag(i) = pow(RealScalar(10), -RealScalar(i));
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 12. Nearly diagonal: random diag, eps * random superdiag
|
||||
diag = VectorXr::Random(n).cwiseAbs() + VectorXr::Constant(n, RealScalar(0.1));
|
||||
for (Index i = 0; i < n - 1; ++i) superdiag(i) = eps * (RealScalar(0.5) + abs(internal::random<RealScalar>()));
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 13. All equal: d=[c,...,c], e=[c,...,c]
|
||||
diag.setConstant(RealScalar(2.5));
|
||||
if (n > 1) superdiag.setConstant(RealScalar(2.5));
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 14. Wilkinson: d_i = |n/2 - i|, e=[1,...,1]
|
||||
for (Index i = 0; i < n; ++i) diag(i) = abs(RealScalar(n / 2) - RealScalar(i));
|
||||
if (n > 1) superdiag.setOnes();
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 15. Overflow/underflow: alternating big/tiny diagonal, tiny/big superdiagonal
|
||||
{
|
||||
const RealScalar big = (std::numeric_limits<RealScalar>::max)() / RealScalar(1000);
|
||||
const RealScalar tiny = (std::numeric_limits<RealScalar>::min)() * RealScalar(1000);
|
||||
for (Index i = 0; i < n; ++i) diag(i) = (i % 2 == 0) ? big : tiny;
|
||||
for (Index i = 0; i < n - 1; ++i) superdiag(i) = (i % 2 == 0) ? tiny : big;
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
}
|
||||
|
||||
// 16. Prescribed condition number: d_i = kappa^(-i/(n-1)), e_i = eps * random
|
||||
if (n > 1) {
|
||||
const RealScalar kappa = RealScalar(1) / eps;
|
||||
for (Index i = 0; i < n; ++i) diag(i) = pow(kappa, -RealScalar(i) / RealScalar(n - 1));
|
||||
for (Index i = 0; i < n - 1; ++i) superdiag(i) = eps * abs(internal::random<RealScalar>());
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
}
|
||||
|
||||
// 17. Rank-deficient: d=[1,..,0,..,0,..,1], e=[0,...,0]
|
||||
for (Index i = 0; i < n; ++i) diag(i) = (i < n / 3 || i >= 2 * n / 3) ? RealScalar(1) : RealScalar(0);
|
||||
superdiag.setZero();
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 18. Arrowhead stress: d_i = linspace(1, n), e_i = 1/(i+1)
|
||||
for (Index i = 0; i < n; ++i) diag(i) = RealScalar(1) + RealScalar(i);
|
||||
for (Index i = 0; i < n - 1; ++i) superdiag(i) = RealScalar(1) / RealScalar(i + 1);
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 19. Repeated singular values: d=[1,2,3,1,2,3,...], e=[0,...,0]
|
||||
for (Index i = 0; i < n; ++i) diag(i) = RealScalar((i % 3) + 1);
|
||||
superdiag.setZero();
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
|
||||
// 20. Glued identity: d=[1,...,1], e=0 except e[n/2-1]=eps
|
||||
diag.setOnes();
|
||||
superdiag.setZero();
|
||||
if (n > 2) superdiag(n / 2 - 1) = eps;
|
||||
verify_bidiagonal_svd<RealScalar>(diag, superdiag);
|
||||
}
|
||||
}
|
||||
|
||||
EIGEN_DECLARE_TEST(bdcsvd) {
|
||||
CALL_SUBTEST_1((bdcsvd_verify_assert<Matrix3f>()));
|
||||
CALL_SUBTEST_2((bdcsvd_verify_assert<Matrix4d>()));
|
||||
@@ -174,4 +380,8 @@ EIGEN_DECLARE_TEST(bdcsvd) {
|
||||
|
||||
// Convergence for large constant matrix (https://gitlab.com/libeigen/eigen/-/issues/2491)
|
||||
CALL_SUBTEST_49(bdcsvd_check_convergence<MatrixXf>(MatrixXf::Constant(500, 500, 1)));
|
||||
|
||||
// Bidiagonal SVD hard test cases
|
||||
CALL_SUBTEST_50((bdcsvd_bidiagonal_hard_cases<float>()));
|
||||
CALL_SUBTEST_51((bdcsvd_bidiagonal_hard_cases<double>()));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user