delassus-operator-sparse.hpp
Go to the documentation of this file.
1 //
2 // Copyright (c) 2024 INRIA
3 //
4 
5 #ifndef __pinocchio_algorithm_delassus_operator_sparse_hpp__
6 #define __pinocchio_algorithm_delassus_operator_sparse_hpp__
7 
10 
11 namespace pinocchio
12 {
13 
14  namespace internal
15  {
16 
17  template<typename Derived>
18  struct SimplicialCholeskyWrapper : public Derived
19  {
20  typedef Eigen::SimplicialCholeskyBase<Derived> Base;
21 
22  using Base::derived;
23  using Base::m_diag;
24  using Base::m_info;
25  using Base::m_matrix;
26  using Base::m_P;
27  using Base::m_Pinv;
28 
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
34  {
35  // eigen_assert(m_factorizationIsOk && "The decomposition is not in a valid state for
36  // solving, you must first call either compute() or symbolic()/numeric()");
37  // eigen_assert(m_matrix.rows()==b.rows());
38 
39  if (m_info != Eigen::Success)
40  return;
41 
42  if (m_P.size() > 0)
43  tmp.noalias() = m_P * b;
44  else
45  tmp = b;
46 
47  if (m_matrix.nonZeros() > 0) // otherwise L==I
48  derived().matrixL().solveInPlace(tmp);
49 
50  if (m_diag.size() > 0)
51  tmp = m_diag.asDiagonal().inverse() * tmp;
52 
53  if (m_matrix.nonZeros() > 0) // otherwise U==I
54  derived().matrixU().solveInPlace(tmp);
55 
56  if (m_P.size() > 0)
57  dest.noalias() = m_Pinv * tmp;
58  }
59 
60  }; // SimplicialCholeskyWrapper
61 
62  template<typename SparseCholeskySolver>
64 
65  template<typename SparseCholeskySolver> //, typename Base = typename SparseCholeskySolver::Base>
67 
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>>
71  {
72  typedef Eigen::AccelerateImpl<MatrixType, UpLo, Solver, EnforceSquare> SparseCholeskySolver;
73 
74  template<typename Rhs, typename Dest, typename Temporary>
75  static void run(
76  const SparseCholeskySolver & solver,
77  const Eigen::MatrixBase<Rhs> & mat,
78  const Eigen::MatrixBase<Dest> & dest,
79  Eigen::MatrixBase<Temporary> & /*tmp*/)
80  {
81  dest.const_cast_derived() = solver.solve(mat.derived());
82  }
83  };
84 #endif
85 
86  template<typename SparseCholeskySolver>
87  struct SparseSolveInPlaceMethod
88  {
89  template<typename Rhs, typename Dest, typename Temporary>
90  static void run(
91  const SparseCholeskySolver & solver,
92  const Eigen::MatrixBase<Rhs> & mat,
93  const Eigen::MatrixBase<Dest> & dest,
94  Eigen::MatrixBase<Temporary> & tmp)
95  {
96  static_assert(
97  std::is_base_of<
98  Eigen::SimplicialCholeskyBase<SparseCholeskySolver>, SparseCholeskySolver>::value,
99  "The solver is not a base of SimplicialCholeskyBase.");
100  typedef SimplicialCholeskyWrapper<SparseCholeskySolver> CholeskyWrapper;
101 
102  const CholeskyWrapper & wrapper = reinterpret_cast<const CholeskyWrapper &>(solver);
103  wrapper._solve_impl(mat, dest.const_cast_derived(), tmp.derived());
104  }
105  };
106 
107  } // namespace internal
108 
109  template<typename _Scalar, int _Options, class _SparseCholeskyDecomposition>
110  struct traits<DelassusOperatorSparseTpl<_Scalar, _Options, _SparseCholeskyDecomposition>>
111  {
112  typedef _SparseCholeskyDecomposition CholeskyDecomposition;
113  typedef typename CholeskyDecomposition::MatrixType SparseMatrix;
114  typedef _Scalar Scalar;
115 
116  enum
117  {
118  Options = _Options,
119  RowsAtCompileTime = Eigen::Dynamic
120  };
121 
122  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, 1, Options> Vector;
123  typedef Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Options> DenseMatrix;
124  };
125 
126  template<typename _Scalar, int _Options, class SparseCholeskyDecomposition>
128  : DelassusOperatorBase<DelassusOperatorSparseTpl<_Scalar, _Options, SparseCholeskyDecomposition>>
129  {
131  typedef typename traits<Self>::Scalar Scalar;
132  enum
133  {
136  };
137 
139  typedef typename traits<Self>::Vector Vector;
141  typedef SparseCholeskyDecomposition CholeskyDecomposition;
143 
144  template<typename MatrixDerived>
145  explicit DelassusOperatorSparseTpl(const Eigen::SparseMatrixBase<MatrixDerived> & mat)
146  : Base(mat.rows())
149  , llt(mat)
150  , damping(Vector::Zero(mat.rows()))
151  , tmp(mat.rows())
152  {
153  PINOCCHIO_CHECK_ARGUMENT_SIZE(mat.rows(), mat.cols());
154  }
155 
156  template<typename VectorLike>
157  void updateDamping(const Eigen::MatrixBase<VectorLike> & vec)
158  {
159  for (Eigen::DenseIndex k = 0; k < size(); ++k)
160  {
161  delassus_matrix_plus_damping.coeffRef(k, k) += -damping[k] + vec[k];
162  }
163  damping = vec;
168  }
169 
170  void updateDamping(const Scalar & mu)
171  {
172  updateDamping(Vector::Constant(size(), mu));
173  }
174 
175  template<typename MatrixLike>
176  void solveInPlace(const Eigen::MatrixBase<MatrixLike> & mat) const
177  {
179  llt, mat.derived(), mat.const_cast_derived(), tmp);
180  }
181 
182  template<typename MatrixLike>
183  typename PINOCCHIO_EIGEN_PLAIN_TYPE(MatrixLike)
184  solve(const Eigen::MatrixBase<MatrixLike> & mat) const
185  {
186  typename PINOCCHIO_EIGEN_PLAIN_TYPE(MatrixLike) res(mat);
187  solveInPlace(res);
188  return res;
189  }
190 
191  template<typename MatrixDerivedIn, typename MatrixDerivedOut>
192  void solve(
193  const Eigen::MatrixBase<MatrixDerivedIn> & x,
194  const Eigen::MatrixBase<MatrixDerivedOut> & res) const
195  {
196  res.const_cast_derived() = x;
197  llt._solve_impl(x, res.const_cast_derived());
198  }
199 
200  template<typename MatrixIn, typename MatrixOut>
201  void applyOnTheRight(
202  const Eigen::MatrixBase<MatrixIn> & x, const Eigen::MatrixBase<MatrixOut> & res_) const
203  {
204  MatrixOut & res = res_.const_cast_derived();
205  res.noalias() = delassus_matrix * x;
206  res.array() += damping.array() * x.array();
207  }
208 
209  template<typename MatrixDerived>
210  typename PINOCCHIO_EIGEN_PLAIN_TYPE(MatrixDerived)
211  operator*(const Eigen::MatrixBase<MatrixDerived> & x) const
212  {
213  typedef typename PINOCCHIO_EIGEN_PLAIN_TYPE(MatrixDerived) ReturnType;
214 
216  ReturnType res(x.rows(), x.cols());
217  applyOnTheRight(x, res);
218  return res;
219  }
220 
221  Eigen::DenseIndex size() const
222  {
223  return delassus_matrix.rows();
224  }
225  Eigen::DenseIndex rows() const
226  {
227  return delassus_matrix.rows();
228  }
229  Eigen::DenseIndex cols() const
230  {
231  return delassus_matrix.cols();
232  }
233 
234  SparseMatrix matrix() const
235  {
237  delassus_matrix_plus_damping += damping.asDiagonal();
239  }
240 
241  SparseMatrix inverse() const
242  {
243  SparseMatrix identity_matrix(size(), size());
244  identity_matrix.setIdentity();
245  SparseMatrix res = llt.solve(identity_matrix);
246  return res;
247  }
248 
249  protected:
254  mutable Vector tmp;
255 
256  }; // struct DelassusOperatorSparseTpl
257 
258 } // namespace pinocchio
259 
260 #endif // ifndef __pinocchio_algorithm_delassus_operator_sparse_hpp__
PINOCCHIO_CHECK_ARGUMENT_SIZE
#define PINOCCHIO_CHECK_ARGUMENT_SIZE(...)
Macro to check if the size of an element is equal to the expected size.
Definition: include/pinocchio/macros.hpp:216
pinocchio::internal::SimplicialCholeskyWrapper::Base
Eigen::SimplicialCholeskyBase< Derived > Base
Definition: delassus-operator-sparse.hpp:20
pinocchio::DelassusOperatorSparseTpl::DelassusOperatorSparseTpl
DelassusOperatorSparseTpl(const Eigen::SparseMatrixBase< MatrixDerived > &mat)
Definition: delassus-operator-sparse.hpp:145
Eigen
pinocchio::internal::SparseSolveInPlaceMethod
Definition: delassus-operator-sparse.hpp:66
pinocchio::DelassusOperatorSparseTpl::SparseMatrix
traits< Self >::SparseMatrix SparseMatrix
Definition: delassus-operator-sparse.hpp:138
pinocchio::DelassusOperatorBase
Definition: delassus-operator-base.hpp:15
PINOCCHIO_EIGEN_MALLOC_SAVE_STATUS
#define PINOCCHIO_EIGEN_MALLOC_SAVE_STATUS()
Definition: eigen-macros.hpp:73
delassus-operator-base.hpp
pinocchio::Options
Options
Definition: joint-configuration.hpp:1082
pinocchio::internal::getSparseCholeskySolverBase
Definition: delassus-operator-sparse.hpp:63
pinocchio::DelassusOperatorSparseTpl::tmp
Vector tmp
Definition: delassus-operator-sparse.hpp:254
pinocchio::traits< DelassusOperatorSparseTpl< _Scalar, _Options, _SparseCholeskyDecomposition > >::SparseMatrix
CholeskyDecomposition::MatrixType SparseMatrix
Definition: delassus-operator-sparse.hpp:113
rows
int rows
pinocchio::DelassusOperatorSparseTpl::updateDamping
void updateDamping(const Scalar &mu)
Definition: delassus-operator-sparse.hpp:170
pinocchio::DelassusOperatorSparseTpl
Definition: delassus-operator-sparse.hpp:127
pinocchio::DelassusOperatorSparseTpl::damping
Vector damping
Definition: delassus-operator-sparse.hpp:253
pinocchio::DelassusOperatorSparseTpl::llt
CholeskyDecomposition llt
Definition: delassus-operator-sparse.hpp:252
pinocchio::DelassusOperatorSparseTpl::delassus_matrix_plus_damping
PINOCCHIO_EIGEN_PLAIN_TYPE(MatrixLike) solve(const Eigen SparseMatrix delassus_matrix_plus_damping
Definition: delassus-operator-sparse.hpp:183
vec
vec
pinocchio::traits< DelassusOperatorSparseTpl< _Scalar, _Options, _SparseCholeskyDecomposition > >::Scalar
_Scalar Scalar
Definition: delassus-operator-sparse.hpp:114
pinocchio::DelassusOperatorSparseTpl::Options
@ Options
Definition: delassus-operator-sparse.hpp:134
pinocchio::res
ReturnType res
Definition: spatial/classic-acceleration.hpp:57
pinocchio::traits< DelassusOperatorSparseTpl< _Scalar, _Options, _SparseCholeskyDecomposition > >::Vector
Eigen::Matrix< Scalar, Eigen::Dynamic, 1, Options > Vector
Definition: delassus-operator-sparse.hpp:122
pinocchio::operator*
TridiagonalSymmetricMatrixApplyOnTheLeftReturnType< LhsMatrixType, TridiagonalSymmetricMatrixTpl< S, O > > operator*(const Eigen::MatrixBase< LhsMatrixType > &lhs, const TridiagonalSymmetricMatrixTpl< S, O > &rhs)
Definition: math/tridiagonal-matrix.hpp:319
pinocchio::traits< DelassusOperatorSparseTpl< _Scalar, _Options, _SparseCholeskyDecomposition > >::CholeskyDecomposition
_SparseCholeskyDecomposition CholeskyDecomposition
Definition: delassus-operator-sparse.hpp:112
b
Vec3f b
pinocchio::DelassusOperatorBase< DelassusOperatorSparseTpl< _Scalar, _Options, SparseCholeskyDecomposition > >::PINOCCHIO_EIGEN_PLAIN_TYPE
PINOCCHIO_EIGEN_PLAIN_TYPE(MatrixLike) solve(const Eigen
Definition: delassus-operator-base.hpp:125
pinocchio::cholesky::solve
Mat & solve(const ModelTpl< Scalar, Options, JointCollectionTpl > &model, const DataTpl< Scalar, Options, JointCollectionTpl > &data, const Eigen::MatrixBase< Mat > &y)
Return the solution of using the Cholesky decomposition stored in data given the entry ....
pinocchio::traits< DelassusOperatorSparseTpl< _Scalar, _Options, _SparseCholeskyDecomposition > >::DenseMatrix
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic, Options > DenseMatrix
Definition: delassus-operator-sparse.hpp:123
pinocchio::DelassusOperatorSparseTpl::Vector
traits< Self >::Vector Vector
Definition: delassus-operator-sparse.hpp:139
pinocchio::DelassusOperatorSparseTpl::updateDamping
void updateDamping(const Eigen::MatrixBase< VectorLike > &vec)
Definition: delassus-operator-sparse.hpp:157
pinocchio::DelassusOperatorSparseTpl::CholeskyDecomposition
SparseCholeskyDecomposition CholeskyDecomposition
Definition: delassus-operator-sparse.hpp:141
PINOCCHIO_EIGEN_MALLOC_ALLOWED
#define PINOCCHIO_EIGEN_MALLOC_ALLOWED()
Definition: eigen-macros.hpp:71
pinocchio::DelassusOperatorSparseTpl::Scalar
traits< Self >::Scalar Scalar
Definition: delassus-operator-sparse.hpp:131
pinocchio::Dynamic
const int Dynamic
Definition: fwd.hpp:140
mat
mat
pinocchio::internal::SparseSolveInPlaceMethod::run
static void run(const SparseCholeskySolver &solver, const Eigen::MatrixBase< Rhs > &mat, const Eigen::MatrixBase< Dest > &dest, Eigen::MatrixBase< Temporary > &tmp)
Definition: delassus-operator-sparse.hpp:90
size
FCL_REAL size() const
value
float value
x
x
pinocchio::inverse
void inverse(const Eigen::MatrixBase< MatrixIn > &m_in, const Eigen::MatrixBase< MatrixOut > &dest)
Definition: math/matrix.hpp:273
PINOCCHIO_EIGEN_MALLOC_RESTORE_STATUS
#define PINOCCHIO_EIGEN_MALLOC_RESTORE_STATUS()
Definition: eigen-macros.hpp:74
pinocchio::DelassusOperatorSparseTpl::Base
DelassusOperatorBase< Self > Base
Definition: delassus-operator-sparse.hpp:142
contact-cholesky.delassus_matrix
delassus_matrix
Definition: contact-cholesky.py:40
pinocchio::DelassusOperatorSparseTpl::Self
DelassusOperatorSparseTpl Self
Definition: delassus-operator-sparse.hpp:130
mu
double mu
Definition: delassus.cpp:58
pinocchio::DelassusOperatorSparseTpl::solveInPlace
void solveInPlace(const Eigen::MatrixBase< MatrixLike > &mat) const
Definition: delassus-operator-sparse.hpp:176
pinocchio::DelassusOperatorSparseTpl::DenseMatrix
traits< Self >::DenseMatrix DenseMatrix
Definition: delassus-operator-sparse.hpp:140
pinocchio::internal::SimplicialCholeskyWrapper::_solve_impl
void _solve_impl(const Eigen::MatrixBase< Rhs > &b, Eigen::MatrixBase< Dest > &dest, Eigen::MatrixBase< Temporary > &tmp) const
Definition: delassus-operator-sparse.hpp:30
fwd.hpp
cols
int cols
pinocchio::traits
Common traits structure to fully define base classes for CRTP.
Definition: fwd.hpp:71
pinocchio::internal::SimplicialCholeskyWrapper
Definition: delassus-operator-sparse.hpp:18
pinocchio::DelassusOperatorSparseTpl::RowsAtCompileTime
@ RowsAtCompileTime
Definition: delassus-operator-sparse.hpp:135
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:27


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