lanczos-decomposition.cpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2024 INRIA
3 //
4 
5 #include <iostream>
6 
8 
9 #include <boost/variant.hpp> // to avoid C99 warnings
10 
11 #include <boost/test/unit_test.hpp>
12 #include <boost/utility/binary.hpp>
13 
14 BOOST_AUTO_TEST_SUITE(BOOST_TEST_MODULE)
15 
16 using namespace pinocchio;
17 
18 BOOST_AUTO_TEST_CASE(test_identity)
19 {
20  const Eigen::DenseIndex mat_size = 20;
21  const Eigen::MatrixXd identity_matrix = Eigen::MatrixXd::Identity(mat_size, mat_size);
22 
23  typedef LanczosDecompositionTpl<Eigen::MatrixXd> LanczosDecomposition;
24  LanczosDecomposition lanczos_decomposition(identity_matrix, 10);
25 
26  const auto residual = lanczos_decomposition.computeDecompositionResidual(identity_matrix);
27  BOOST_CHECK(residual.isZero());
28 
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())
32  .isIdentity());
33 }
34 
35 BOOST_AUTO_TEST_CASE(test_diagonal_matrix)
36 {
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();
40 
41  typedef LanczosDecompositionTpl<Eigen::MatrixXd> LanczosDecomposition;
42  LanczosDecomposition lanczos_decomposition(diagonal_matrix, 10);
43 
44  const auto residual = lanczos_decomposition.computeDecompositionResidual(diagonal_matrix);
45  BOOST_CHECK(residual.isZero());
46 
47  for (Eigen::DenseIndex col_id = 0; col_id < lanczos_decomposition.Ts().cols(); ++col_id)
48  {
49  BOOST_CHECK(math::fabs(lanczos_decomposition.Qs().col(col_id).norm() - 1.) <= 1e-12);
50  }
51 
52  BOOST_CHECK(lanczos_decomposition.rank() == lanczos_decomposition.Ts().cols());
53  BOOST_CHECK((lanczos_decomposition.Qs().transpose() * lanczos_decomposition.Qs()).isIdentity());
54 }
55 
57 {
58  typedef LanczosDecompositionTpl<Eigen::MatrixXd> LanczosDecomposition;
59  const Eigen::DenseIndex mat_size = 20;
60 
61  for (int it = 0; it < 1000; ++it)
62  {
63  const Eigen::MatrixXd A = Eigen::MatrixXd::Random(mat_size, mat_size);
64  const Eigen::MatrixXd matrix = A.transpose() * A;
65 
66  LanczosDecomposition lanczos_decomposition(matrix, 10);
67 
68  const auto residual = lanczos_decomposition.computeDecompositionResidual(matrix);
69 
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());
76 
77  for (Eigen::DenseIndex col_id = 0; col_id < lanczos_decomposition.Ts().cols(); ++col_id)
78  {
79  BOOST_CHECK(math::fabs(lanczos_decomposition.Qs().col(col_id).norm() - 1.) <= 1e-12);
80  }
81  // Check orthonormality
82  BOOST_CHECK(lanczos_decomposition.rank() == lanczos_decomposition.Ts().cols());
83  BOOST_CHECK((lanczos_decomposition.Qs().transpose() * lanczos_decomposition.Qs()).isIdentity());
84  }
85 }
86 
87 BOOST_AUTO_TEST_SUITE_END()
pinocchio::LanczosDecompositionTpl
Compute the largest eigenvalues and the associated principle eigenvector via power iteration.
Definition: math/lanczos-decomposition.hpp:18
lanczos-decomposition.hpp
simulation-contact-dynamics.A
A
Definition: simulation-contact-dynamics.py:110
inverse-kinematics-3d.it
int it
Definition: inverse-kinematics-3d.py:17
cols
int cols
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_identity)
Definition: lanczos-decomposition.cpp:18
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


pinocchio
Author(s):
autogenerated on Sun Dec 22 2024 03:41:11