Remove random retry loops in tests (batch 3: geometry, sparse, umeyama)

libeigen/eigen!2262

Co-authored-by: Rasmus Munk Larsen <rmlarsen@gmail.com>
This commit is contained in:
Rasmus Munk Larsen
2026-03-09 00:35:26 -07:00
parent a3cb1c6591
commit 54458cb39d
3 changed files with 15 additions and 54 deletions

View File

@@ -223,13 +223,10 @@ void transformations() {
t4 *= aa3;
VERIFY_IS_APPROX(t3.matrix(), t4.matrix());
{
int guard = 0;
do {
v3 = Vector3::Random();
dont_over_optimize(v3);
} while (v3.cwiseAbs().minCoeff() < NumTraits<Scalar>::epsilon() && (++guard) < 100);
VERIFY(guard < 100);
v3 = Vector3::Random();
dont_over_optimize(v3);
for (int k = 0; k < 3; ++k) {
if (numext::abs(v3(k)) < NumTraits<Scalar>::epsilon()) v3(k) = NumTraits<Scalar>::epsilon();
}
Translation3 tv3(v3);
Transform3 t5(tv3);
@@ -384,13 +381,8 @@ void transformations() {
// test transform inversion
t0.setIdentity();
t0.translate(v0);
{
int guard = 0;
do {
t0.linear().setRandom();
} while (t0.linear().jacobiSvd().singularValues()(2) < test_precision<Scalar>() && (++guard) < 100);
VERIFY(guard < 100);
}
t0.linear().setRandom();
if (t0.linear().jacobiSvd().singularValues()(2) < test_precision<Scalar>()) t0.linear().setIdentity();
Matrix4 t044 = Matrix4::Zero();
t044(3, 3) = 1;
t044.block(0, 0, t0.matrix().rows(), 4) = t0.matrix();

View File

@@ -301,9 +301,11 @@ void sparse_product() {
SparseMatrixType mS(rows, rows);
SparseMatrixType mA(rows, rows);
initSparse<Scalar>(density, refA, mA);
do {
initSparse<Scalar>(density, refUp, mUp, ForceRealDiag | /*ForceNonZeroDiag|*/ MakeUpperTriangular);
} while (refUp.isZero());
initSparse<Scalar>(density, refUp, mUp, ForceRealDiag | /*ForceNonZeroDiag|*/ MakeUpperTriangular);
if (refUp.isZero()) {
refUp(0, 0) = Scalar(1);
mUp.coeffRef(0, 0) = Scalar(1);
}
refLo = refUp.adjoint();
mLo = mUp.adjoint();
refS = refUp + refLo;

View File

@@ -13,6 +13,7 @@
#include <Eigen/Geometry>
#include <Eigen/LU> // required for MatrixBase::determinant
#include <Eigen/QR> // required for HouseholderQR
#include <Eigen/SVD> // required for SVD
using namespace Eigen;
@@ -23,43 +24,9 @@ Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> randMatrixUnitary(int size) {
typedef T Scalar;
typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> MatrixType;
MatrixType Q;
int max_tries = 40;
bool is_unitary = false;
while (!is_unitary && max_tries > 0) {
// initialize random matrix
Q = MatrixType::Random(size, size);
// orthogonalize columns using the Gram-Schmidt algorithm
for (int col = 0; col < size; ++col) {
typename MatrixType::ColXpr colVec = Q.col(col);
for (int prevCol = 0; prevCol < col; ++prevCol) {
typename MatrixType::ColXpr prevColVec = Q.col(prevCol);
colVec -= colVec.dot(prevColVec) * prevColVec;
}
Q.col(col) = colVec.normalized();
}
// this additional orthogonalization is not necessary in theory but should enhance
// the numerical orthogonality of the matrix
for (int row = 0; row < size; ++row) {
typename MatrixType::RowXpr rowVec = Q.row(row);
for (int prevRow = 0; prevRow < row; ++prevRow) {
typename MatrixType::RowXpr prevRowVec = Q.row(prevRow);
rowVec -= rowVec.dot(prevRowVec) * prevRowVec;
}
Q.row(row) = rowVec.normalized();
}
// final check
is_unitary = Q.isUnitary();
--max_tries;
}
if (max_tries == 0) eigen_assert(false && "randMatrixUnitary: Could not construct unitary matrix!");
// The Q factor of the QR decomposition of a random matrix is a random unitary matrix.
// HouseholderQR is numerically stable and always succeeds, unlike Gram-Schmidt.
MatrixType Q = MatrixType::Random(size, size).householderQr().householderQ();
return Q;
}