9 #include <boost/variant.hpp>
11 #include <boost/test/unit_test.hpp>
12 #include <boost/utility/binary.hpp>
14 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
20 const Eigen::DenseIndex mat_size = 20;
21 const Eigen::MatrixXd identity_matrix = Eigen::MatrixXd::Identity(mat_size, mat_size);
24 LanczosDecomposition lanczos_decomposition(identity_matrix, 10);
26 const auto residual = lanczos_decomposition.computeDecompositionResidual(identity_matrix);
27 BOOST_CHECK(residual.isZero());
29 BOOST_CHECK(lanczos_decomposition.rank() == 1);
30 BOOST_CHECK((lanczos_decomposition.Qs().transpose() * lanczos_decomposition.Qs())
31 .topLeftCorner(lanczos_decomposition.rank(), lanczos_decomposition.rank())
37 const Eigen::DenseIndex mat_size = 20;
38 const Eigen::VectorXd diagonal_terms = Eigen::VectorXd::LinSpaced(mat_size, 0.0, mat_size - 1);
39 const Eigen::MatrixXd diagonal_matrix = diagonal_terms.asDiagonal();
42 LanczosDecomposition lanczos_decomposition(diagonal_matrix, 10);
44 const auto residual = lanczos_decomposition.computeDecompositionResidual(diagonal_matrix);
45 BOOST_CHECK(residual.isZero());
47 for (Eigen::DenseIndex col_id = 0; col_id < lanczos_decomposition.Ts().
cols(); ++col_id)
49 BOOST_CHECK(math::fabs(lanczos_decomposition.Qs().col(col_id).norm() - 1.) <= 1e-12);
52 BOOST_CHECK(lanczos_decomposition.rank() == lanczos_decomposition.Ts().cols());
53 BOOST_CHECK((lanczos_decomposition.Qs().transpose() * lanczos_decomposition.Qs()).isIdentity());
59 const Eigen::DenseIndex mat_size = 20;
61 for (
int it = 0;
it < 1000; ++
it)
63 const Eigen::MatrixXd
A = Eigen::MatrixXd::Random(mat_size, mat_size);
64 const Eigen::MatrixXd matrix =
A.transpose() *
A;
66 LanczosDecomposition lanczos_decomposition(matrix, 10);
68 const auto residual = lanczos_decomposition.computeDecompositionResidual(matrix);
70 const auto & Ts = lanczos_decomposition.Ts();
71 const auto & Qs = lanczos_decomposition.Qs();
72 const Eigen::MatrixXd residual_bis = matrix * Qs - Qs * Ts.matrix();
73 const Eigen::MatrixXd residual_tierce = Qs.transpose() * matrix * Qs - Ts.matrix();
74 const Eigen::MatrixXd residual_fourth = matrix - Qs * Ts.matrix() * Qs.transpose();
75 BOOST_CHECK(residual.isZero());
77 for (Eigen::DenseIndex col_id = 0; col_id < lanczos_decomposition.Ts().
cols(); ++col_id)
79 BOOST_CHECK(math::fabs(lanczos_decomposition.Qs().col(col_id).norm() - 1.) <= 1e-12);
82 BOOST_CHECK(lanczos_decomposition.rank() == lanczos_decomposition.Ts().cols());
83 BOOST_CHECK((lanczos_decomposition.Qs().transpose() * lanczos_decomposition.Qs()).isIdentity());
87 BOOST_AUTO_TEST_SUITE_END()