diff --git a/Libs/Optimize/Function/EntropyUpdate.cpp b/Libs/Optimize/Function/EntropyUpdate.cpp index 972b6bf324..a9d3269401 100644 --- a/Libs/Optimize/Function/EntropyUpdate.cpp +++ b/Libs/Optimize/Function/EntropyUpdate.cpp @@ -12,43 +12,47 @@ void ComputeEntropyUpdate(const Eigen::Ref& Y, double alpha const double scale = 1.0 / static_cast(n - 1); const bool dual = force_dual >= 0 ? force_dual == 1 : n > d; - if (!dual) { - // gramMat = Yᵀ Y (N x N) is symmetric positive-semidefinite, so a self-adjoint eigensolver yields - // the same eigenpairs as a general SVD but much faster. - TIME_START("correspondence::gramMat"); - Eigen::MatrixXd gram = Y.transpose() * Y; - TIME_STOP("correspondence::gramMat"); - - TIME_START("correspondence::svd"); - Eigen::SelfAdjointEigenSolver eig(gram); - TIME_STOP("correspondence::svd"); - eigenvalues = eig.eigenvalues(); + // gram = YᵀY (N x N), or the d x d covariance YYᵀ when N > d. Both share their nonzero eigenvalues and give the + // same update: Y (YᵀY/(N-1) + αI)⁻¹ = (YYᵀ/(N-1) + αI)⁻¹ Y (#2705) + TIME_START("correspondence::gramMat"); + Eigen::MatrixXd gram = dual ? Eigen::MatrixXd(Y * Y.transpose()) : Eigen::MatrixXd(Y.transpose() * Y); + TIME_STOP("correspondence::gramMat"); + + // The energy only needs the eigenvalues, so skip the eigenvectors, which are most of the cost (#2711) + TIME_START("correspondence::svd"); + Eigen::SelfAdjointEigenSolver eig(gram, Eigen::EigenvaluesOnly); + TIME_STOP("correspondence::svd"); - // pinvMat = UG · diag(1 / (λ/(N-1) + α)) · UGᵀ (symmetric) - TIME_START("correspondence::pinvMat"); - Eigen::ArrayXd inv = (eigenvalues.array() * scale + alpha).inverse(); - Eigen::MatrixXd pinv = eig.eigenvectors() * inv.matrix().asDiagonal() * eig.eigenvectors().transpose(); - TIME_STOP("correspondence::pinvMat"); + // gram/(N-1) + αI is positive definite for α > 0, so the update is a Cholesky solve + TIME_START("correspondence::pinvMat"); + Eigen::MatrixXd system = gram * scale; + system.diagonal().array() += alpha; + Eigen::LLT llt(system); + TIME_STOP("correspondence::pinvMat"); + + if (llt.info() == Eigen::Success) { + if (dual) { + update.noalias() = llt.solve(Y); + } else { + // update = Y · system⁻¹ = (system⁻¹ Yᵀ)ᵀ, since system is symmetric + update.noalias() = llt.solve(Y.transpose()).transpose(); + } + } else { + // not numerically positive definite (α too small): fall back to the eigendecomposition + Eigen::SelfAdjointEigenSolver full(gram); + Eigen::ArrayXd inv = (full.eigenvalues().array() * scale + alpha).inverse(); + Eigen::MatrixXd inverse = full.eigenvectors() * inv.matrix().asDiagonal() * full.eigenvectors().transpose(); + if (dual) { + update.noalias() = inverse * Y; + } else { + update.noalias() = Y * inverse; + } + } - // update = Y · pinvMat (thesis eq. 2.35: gradient is Y × (YᵀY + αI)⁻¹) - update.noalias() = Y * pinv; + if (!dual) { + eigenvalues = eig.eigenvalues(); } else { - // Same update from the d x d covariance: Y (YᵀY/(N-1) + αI)⁻¹ = (YYᵀ/(N-1) + αI)⁻¹ Y - TIME_START("correspondence::gramMat"); - Eigen::MatrixXd cov = Y * Y.transpose(); - TIME_STOP("correspondence::gramMat"); - - TIME_START("correspondence::svd"); - Eigen::SelfAdjointEigenSolver eig(cov); - TIME_STOP("correspondence::svd"); - - TIME_START("correspondence::pinvMat"); - Eigen::ArrayXd inv = (eig.eigenvalues().array() * scale + alpha).inverse(); - Eigen::MatrixXd cov_inv = eig.eigenvectors() * inv.matrix().asDiagonal() * eig.eigenvectors().transpose(); - TIME_STOP("correspondence::pinvMat"); - update.noalias() = cov_inv * Y; - - // YᵀY and YYᵀ share their nonzero eigenvalues; the rest are zero + // YᵀY has the d eigenvalues of YYᵀ and N-d zeros eigenvalues = Eigen::VectorXd::Zero(n); if (n >= d) { eigenvalues.tail(d) = eig.eigenvalues(); diff --git a/Libs/Optimize/Function/EntropyUpdate.h b/Libs/Optimize/Function/EntropyUpdate.h index d47e9e5c26..75d2476fbb 100644 --- a/Libs/Optimize/Function/EntropyUpdate.h +++ b/Libs/Optimize/Function/EntropyUpdate.h @@ -10,6 +10,7 @@ using RowMatrixType = Eigen::Matrix d this is computed from the d x d covariance instead, using /// Y (YᵀY/(N-1) + αI)⁻¹ = (YYᵀ/(N-1) + αI)⁻¹ Y, so the cost no longer grows as N³ (#2705). +/// Only eigenvalues are computed; the update is a Cholesky solve (#2711). /// force_dual: -1 chooses automatically, 0 always uses the N x N form, 1 the d x d form. void ComputeEntropyUpdate(const Eigen::Ref& Y, double alpha, RowMatrixType& update, Eigen::VectorXd& eigenvalues, int force_dual = -1); diff --git a/Testing/ParticlesTests/ParticlesTests.cpp b/Testing/ParticlesTests/ParticlesTests.cpp index 9cfa799193..abbdf6cf8f 100644 --- a/Testing/ParticlesTests/ParticlesTests.cpp +++ b/Testing/ParticlesTests/ParticlesTests.cpp @@ -876,3 +876,40 @@ TEST(ParticlesTests, correspondence_dual_form_test) { EXPECT_LT((eig_gram - eig_dual).cwiseAbs().maxCoeff(), 1e-9 * eig_gram.cwiseAbs().maxCoeff()) << "d=" << d << " n=" << n; } } + +//--------------------------------------------------------------------------- +TEST(ParticlesTests, correspondence_cholesky_test) { + // The update is a Cholesky solve and only eigenvalues are computed (#2711). Compare with an explicit + // inverse and a full eigendecomposition, for both forms, more shapes than dimensions and fewer. + std::mt19937 rng(13); + std::normal_distribution dist(0.0, 5.0); + for (double alpha : {0.5, 1e-6}) { + for (auto [d, n] : {std::pair{9, 30}, std::pair{30, 9}}) { + RowMatrixType y(d, n); + for (int i = 0; i < y.size(); i++) { + y.data()[i] = dist(rng); + } + y.colwise() -= y.rowwise().mean(); + + Eigen::MatrixXd gram = y.transpose() * y; + Eigen::MatrixXd system = gram / (n - 1); + system.diagonal().array() += alpha; + const Eigen::MatrixXd expected = y * system.inverse(); + const Eigen::VectorXd expected_eig = Eigen::SelfAdjointEigenSolver(gram).eigenvalues(); + + for (int form : {0, 1}) { + RowMatrixType update; + Eigen::VectorXd eig; + ComputeEntropyUpdate(y, alpha, update, eig, form); + ASSERT_EQ(eig.size(), n); + // the accuracy of any solve is limited by the condition number of the system + const double condition = (expected_eig.maxCoeff() / (n - 1) + alpha) / alpha; + EXPECT_LT((update - expected).cwiseAbs().maxCoeff(), + std::max(1e-10, 1e-13 * condition) * expected.cwiseAbs().maxCoeff()) + << "alpha=" << alpha << " d=" << d << " n=" << n << " form=" << form; + EXPECT_LT((eig - expected_eig).cwiseAbs().maxCoeff(), 1e-9 * expected_eig.cwiseAbs().maxCoeff()) + << "alpha=" << alpha << " d=" << d << " n=" << n << " form=" << form; + } + } + } +}