5#ifndef __pinocchio_algorithm_delassus_operator_sparse_hpp__
6#define __pinocchio_algorithm_delassus_operator_sparse_hpp__
8#include "pinocchio/algorithm/fwd.hpp"
9#include "pinocchio/algorithm/delassus-operator-base.hpp"
17 template<
typename Derived>
18 struct SimplicialCholeskyWrapper :
public Derived
20 typedef Eigen::SimplicialCholeskyBase<Derived> Base;
29 template<
typename Rhs,
typename Dest,
typename Temporary>
31 const Eigen::MatrixBase<Rhs> & b,
32 Eigen::MatrixBase<Dest> & dest,
33 Eigen::MatrixBase<Temporary> & tmp)
const
39 if (m_info != Eigen::Success)
43 tmp.noalias() = m_P * b;
47 if (m_matrix.nonZeros() > 0)
48 derived().matrixL().solveInPlace(tmp);
50 if (m_diag.size() > 0)
51 tmp = m_diag.asDiagonal().inverse() * tmp;
53 if (m_matrix.nonZeros() > 0)
54 derived().matrixU().solveInPlace(tmp);
57 dest.noalias() = m_Pinv * tmp;
62 template<
typename SparseCholeskySolver>
63 struct getSparseCholeskySolverBase;
65 template<
typename SparseCholeskySolver>
66 struct SparseSolveInPlaceMethod;
68#ifdef PINOCCHIO_WITH_ACCELERATE_SUPPORT
69 template<
typename MatrixType,
int UpLo, SparseFactorization_t Solver,
bool EnforceSquare>
70 struct SparseSolveInPlaceMethod<Eigen::AccelerateImpl<MatrixType, UpLo, Solver, EnforceSquare>>
72 typedef Eigen::AccelerateImpl<MatrixType, UpLo, Solver, EnforceSquare> SparseCholeskySolver;
74 template<
typename Rhs,
typename Dest,
typename Temporary>
76 const SparseCholeskySolver & solver,
77 const Eigen::MatrixBase<Rhs> & mat,
78 const Eigen::MatrixBase<Dest> & dest,
79 Eigen::MatrixBase<Temporary> & )
81 dest.const_cast_derived() = solver.solve(mat.derived());
86 template<
typename SparseCholeskySolver>
87 struct SparseSolveInPlaceMethod
89 template<
typename Rhs,
typename Dest,
typename Temporary>
91 const SparseCholeskySolver & solver,
92 const Eigen::MatrixBase<Rhs> & mat,
93 const Eigen::MatrixBase<Dest> & dest,
94 Eigen::MatrixBase<Temporary> & tmp)
98 Eigen::SimplicialCholeskyBase<SparseCholeskySolver>, SparseCholeskySolver>::value,
99 "The solver is not a base of SimplicialCholeskyBase.");
100 typedef SimplicialCholeskyWrapper<SparseCholeskySolver> CholeskyWrapper;
102 const CholeskyWrapper & wrapper =
reinterpret_cast<const CholeskyWrapper &
>(solver);
103 wrapper._solve_impl(mat, dest.const_cast_derived(), tmp.derived());
109 template<
typename _Scalar,
int _Options,
class _SparseCholeskyDecomposition>
113 typedef typename CholeskyDecomposition::MatrixType SparseMatrix;
119 RowsAtCompileTime = Eigen::Dynamic
122 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Options> Vector;
123 typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Options> DenseMatrix;
126 template<
typename _Scalar,
int _Options,
class SparseCholeskyDecomposition>
128 :
DelassusOperatorBase<DelassusOperatorSparseTpl<_Scalar, _Options, SparseCholeskyDecomposition>>
144 template<
typename MatrixDerived>
147 , delassus_matrix(
mat)
148 , delassus_matrix_plus_damping(
mat)
150 , damping(Vector::Zero(
mat.rows()))
153 PINOCCHIO_CHECK_ARGUMENT_SIZE(
mat.rows(),
mat.cols());
156 template<
typename VectorLike>
157 void updateDamping(
const Eigen::MatrixBase<VectorLike> &
vec)
159 for (Eigen::DenseIndex
k = 0;
k < size(); ++
k)
161 delassus_matrix_plus_damping.coeffRef(
k,
k) += -damping[
k] +
vec[
k];
164 PINOCCHIO_EIGEN_MALLOC_SAVE_STATUS();
165 PINOCCHIO_EIGEN_MALLOC_ALLOWED();
166 llt.factorize(delassus_matrix_plus_damping);
167 PINOCCHIO_EIGEN_MALLOC_RESTORE_STATUS();
170 void updateDamping(
const Scalar & mu)
172 updateDamping(Vector::Constant(size(), mu));
175 template<
typename MatrixLike>
176 void solveInPlace(
const Eigen::MatrixBase<MatrixLike> &
mat)
const
178 internal::SparseSolveInPlaceMethod<CholeskyDecomposition>::run(
179 llt,
mat.derived(),
mat.const_cast_derived(), tmp);
182 template<
typename MatrixLike>
183 typename PINOCCHIO_EIGEN_PLAIN_TYPE(
MatrixLike)
184 solve(
const Eigen::MatrixBase<MatrixLike> &
mat)
const
191 template<
typename MatrixDerivedIn,
typename MatrixDerivedOut>
193 const Eigen::MatrixBase<MatrixDerivedIn> & x,
194 const Eigen::MatrixBase<MatrixDerivedOut> & res)
const
196 res.const_cast_derived() = x;
197 llt._solve_impl(x, res.const_cast_derived());
200 template<
typename MatrixIn,
typename MatrixOut>
201 void applyOnTheRight(
202 const Eigen::MatrixBase<MatrixIn> & x,
const Eigen::MatrixBase<MatrixOut> &
res_)
const
205 res.noalias() = delassus_matrix * x;
206 res.array() += damping.array() * x.array();
209 template<
typename MatrixDerived>
211 operator*(
const Eigen::MatrixBase<MatrixDerived> & x)
const
213 typedef typename PINOCCHIO_EIGEN_PLAIN_TYPE(
MatrixDerived) ReturnType;
215 PINOCCHIO_CHECK_ARGUMENT_SIZE(x.rows(), size());
216 ReturnType res(x.rows(), x.cols());
217 applyOnTheRight(x, res);
221 Eigen::DenseIndex size()
const
223 return delassus_matrix.rows();
225 Eigen::DenseIndex rows()
const
227 return delassus_matrix.rows();
229 Eigen::DenseIndex cols()
const
231 return delassus_matrix.cols();
236 delassus_matrix_plus_damping = delassus_matrix;
237 delassus_matrix_plus_damping += damping.asDiagonal();
238 return delassus_matrix_plus_damping;
Main pinocchio namespace.
Common traits structure to fully define base classes for CRTP.