5 #ifndef __pinocchio_math_lanczos_decomposition_hpp__
6 #define __pinocchio_math_lanczos_decomposition_hpp__
8 #include "pinocchio/math/fwd.hpp"
9 #include "pinocchio/math/tridiagonal-matrix.hpp"
10 #include "pinocchio/math/gram-schmidt-orthonormalisation.hpp"
17 template<
typename _Matrix>
20 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
22 typedef typename PINOCCHIO_EIGEN_PLAIN_TYPE(_Matrix) PlainMatrix;
23 typedef typename PINOCCHIO_EIGEN_PLAIN_TYPE(
typename PlainMatrix::ColXpr) Vector;
24 typedef typename _Matrix::Scalar Scalar;
27 Options = _Matrix::Options
32 template<
typename MatrixLikeType>
34 : m_Qs(mat.rows(), decomposition_size)
35 , m_Ts(decomposition_size)
36 , m_A_times_q(mat.rows())
39 PINOCCHIO_CHECK_INPUT_ARGUMENT(mat.rows() == mat.cols(),
"The input matrix is not square.");
40 PINOCCHIO_CHECK_INPUT_ARGUMENT(
41 decomposition_size >= 1,
"The size of the decomposition should be greater than one.");
42 PINOCCHIO_CHECK_INPUT_ARGUMENT(
43 decomposition_size <= mat.rows(),
44 "The size of the decomposition should not be larger than the number of rows.");
53 return m_Qs == other.m_Qs && m_Ts == other.m_Ts && m_rank == other.m_rank;
58 return !(*
this == other);
66 template<
typename MatrixLikeType>
69 PINOCCHIO_CHECK_INPUT_ARGUMENT(A.rows() == A.cols(),
"The input matrix is not square.");
70 PINOCCHIO_CHECK_INPUT_ARGUMENT(
71 A.rows() == m_Qs.rows(),
"The input matrix is of correct size.");
73 const Eigen::DenseIndex decomposition_size = m_Ts.cols();
74 auto & alphas = m_Ts.diagonal();
75 auto & betas = m_Ts.subDiagonal();
77 m_Qs.col(0).fill(Scalar(1) / math::sqrt(Scalar(m_Qs.rows())));
80 for (k = 0; k < decomposition_size; ++k)
82 const auto q = m_Qs.col(k);
83 m_A_times_q.noalias() = A * q;
84 alphas[k] = q.dot(m_A_times_q);
86 if (k < decomposition_size - 1)
88 auto q_next = m_Qs.col(k + 1);
89 m_A_times_q -= alphas[k] * q;
92 m_A_times_q -= betas[k - 1] * m_Qs.col(k - 1);
100 betas[k] = m_A_times_q.norm();
101 if (betas[k] <= 1e2 * Eigen::NumTraits<Scalar>::epsilon())
107 q_next.noalias() = m_A_times_q / betas[k];
111 m_rank = math::max(Eigen::DenseIndex(1), k);
122 template<
typename MatrixLikeType>
125 const Eigen::DenseIndex last_col_id = m_Ts.cols() - 1;
126 const auto & alphas = m_Ts.diagonal();
127 const auto & betas = m_Ts.subDiagonal();
129 PlainMatrix residual = A * m_Qs;
130 residual -= (m_Qs * m_Ts).eval();
132 const auto & q = m_Qs.col(last_col_id);
134 auto & tmp_vec = m_A_times_q;
135 tmp_vec.noalias() = A * q;
136 tmp_vec -= alphas[last_col_id] * q;
138 tmp_vec -= betas[last_col_id - 1] * m_Qs.col(last_col_id - 1);
140 residual.col(last_col_id) -= tmp_vec;
157 const PlainMatrix &
Qs()
const
175 TridiagonalMatrix m_Ts;
176 mutable Vector m_A_times_q;
177 Eigen::DenseIndex m_rank;
Main pinocchio namespace.
void orthonormalisation(const Eigen::MatrixBase< MatrixType > &basis, const Eigen::MatrixBase< VectorType > &vec_)
Perform the Gram-Schmidt orthonormalisation on the input/output vector for a given input basis.
Compute the largest eigenvalues and the associated principle eigenvector via power iteration.
TridiagonalMatrix & Ts()
Returns the tridiagonal matrix associated with the Lanczos decomposition.
PlainMatrix computeDecompositionResidual(const MatrixLikeType &A) const
Computes the residual associated with the decomposition, namely, the quantity .
const PlainMatrix & Qs() const
Returns the orthogonal basis associated with the Lanczos decomposition.
const TridiagonalMatrix & Ts() const
Returns the tridiagonal matrix associated with the Lanczos decomposition.
PlainMatrix & Qs()
Returns the orthogonal basis associated with the Lanczos decomposition.
Eigen::DenseIndex rank() const
Returns the rank of the decomposition.
void compute(const MatrixLikeType &A)
Computes the Lanczos decomposition of the input matrix A.
LanczosDecompositionTpl(const MatrixLikeType &mat, const Eigen::DenseIndex decomposition_size)
Default constructor for the Lanczos decomposition from an input matrix.