Skip to content

Commit

Permalink
Test that row-major output matrices give the same result.
Browse files Browse the repository at this point in the history
This ensures that we don't make any assumptions about the output matrices being
column-major (even though this is the default).

We also add some more (weak) checks for similarity with a naive SVD.
  • Loading branch information
LTLA committed Jul 3, 2024
1 parent 824051a commit ab12fda
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tests/src/compare.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void expect_equal_column_vectors(const Eigen::MatrixXd& left, const Eigen::Matri

for (Eigen::Index i = 0; i < left.cols(); ++i) {
for (Eigen::Index j = 0; j < left.rows(); ++j) {
EXPECT_TRUE(same_same(std::abs(left(j, i)), std::abs(right(j, i)), tol));
EXPECT_TRUE(same_same(std::abs(left(j, i)), std::abs(right(j, i)), tol)) << "(" << left(j, i) << " vs " << right(j, i) << ")" << std::endl;
}

double left_sum = std::abs(left.col(i).sum());
Expand Down
14 changes: 12 additions & 2 deletions tests/src/irlba.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,20 @@ TEST_P(IrlbaTester, Basic) {
ASSERT_EQ(res.U.cols(), rank);
ASSERT_EQ(res.D.size(), rank);

// Gives us singular values that are around about right. Unfortunately,
// the singular values don't converge enough for an exact comparison.
// Gives us singular values that are around about right. Unfortunately, the
// U and V values don't converge enough for a decent comparison; we'll
// limit this to the first column, as this seems to be the most accurate.
Eigen::BDCSVD svd(A, Eigen::ComputeThinU | Eigen::ComputeThinV);
expect_equal_vectors(res.D, svd.singularValues().head(rank), 1e-6);
expect_equal_column_vectors(res.U.leftCols(1), svd.matrixU().leftCols(1), 1e-4);
expect_equal_column_vectors(res.V.leftCols(1), svd.matrixV().leftCols(1), 1e-4);

// Also gives the same results when the matrices are row-major.
typedef Eigen::Matrix<double, -1, -1, Eigen::RowMajor> RowEigenXd;
auto rmres = irlba::compute<RowEigenXd>(A, rank, opt);
expect_equal_matrix(res.U, rmres.U);
expect_equal_matrix(res.V, rmres.V);
expect_equal_vectors(res.D, rmres.D);

// Also works with some custom initialization.
auto init = create_random_vector(A.cols(), 1239);
Expand Down

0 comments on commit ab12fda

Please sign in to comment.