Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 38 additions & 34 deletions Libs/Optimize/Function/EntropyUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,47 @@ void ComputeEntropyUpdate(const Eigen::Ref<const RowMatrixType>& Y, double alpha
const double scale = 1.0 / static_cast<double>(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<Eigen::MatrixXd> 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<Eigen::MatrixXd> 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<Eigen::MatrixXd> 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<Eigen::MatrixXd> 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<Eigen::MatrixXd> 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();
Expand Down
1 change: 1 addition & 0 deletions Libs/Optimize/Function/EntropyUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using RowMatrixType = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eige
/// update = Y (YᵀY/(N-1) + αI)⁻¹, and the N eigenvalues of YᵀY in increasing order.
/// When N > 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<const RowMatrixType>& Y, double alpha, RowMatrixType& update,
Eigen::VectorXd& eigenvalues, int force_dual = -1);
Expand Down
37 changes: 37 additions & 0 deletions Testing/ParticlesTests/ParticlesTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> dist(0.0, 5.0);
for (double alpha : {0.5, 1e-6}) {
for (auto [d, n] : {std::pair<int, int>{9, 30}, std::pair<int, int>{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<Eigen::MatrixXd>(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;
}
}
}
}
Loading