| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /////////////////////////////////////////////////////////////////////////////// | ||
| 2 | // BSD 3-Clause License | ||
| 3 | // | ||
| 4 | // Copyright (C) 2019-2025, LAAS-CNRS, New York University, | ||
| 5 | // Max Planck Gesellschaft, | ||
| 6 | // University of Edinburgh, Heriot-Watt University | ||
| 7 | // Copyright note valid unless otherwise stated in individual files. | ||
| 8 | // All rights reserved. | ||
| 9 | /////////////////////////////////////////////////////////////////////////////// | ||
| 10 | |||
| 11 | #ifndef CROCODDYL_CORE_NUMDIFF_STATE_HPP_ | ||
| 12 | #define CROCODDYL_CORE_NUMDIFF_STATE_HPP_ | ||
| 13 | |||
| 14 | #include "crocoddyl/core/fwd.hpp" | ||
| 15 | #include "crocoddyl/core/state-base.hpp" | ||
| 16 | |||
| 17 | namespace crocoddyl { | ||
| 18 | |||
| 19 | template <typename _Scalar> | ||
| 20 | class StateNumDiffTpl : public StateAbstractTpl<_Scalar> { | ||
| 21 | public: | ||
| 22 | EIGEN_MAKE_ALIGNED_OPERATOR_NEW | ||
| 23 | ✗ | CROCODDYL_DERIVED_CAST(StateBase, StateNumDiffTpl) | |
| 24 | |||
| 25 | typedef _Scalar Scalar; | ||
| 26 | typedef MathBaseTpl<Scalar> MathBase; | ||
| 27 | typedef StateAbstractTpl<_Scalar> Base; | ||
| 28 | typedef typename MathBase::VectorXs VectorXs; | ||
| 29 | typedef typename MathBase::MatrixXs MatrixXs; | ||
| 30 | |||
| 31 | explicit StateNumDiffTpl(std::shared_ptr<Base> state); | ||
| 32 | virtual ~StateNumDiffTpl(); | ||
| 33 | |||
| 34 | virtual VectorXs zero() const override; | ||
| 35 | virtual VectorXs rand() const override; | ||
| 36 | virtual void diff(const Eigen::Ref<const VectorXs>& x0, | ||
| 37 | const Eigen::Ref<const VectorXs>& x1, | ||
| 38 | Eigen::Ref<VectorXs> dxout) const override; | ||
| 39 | virtual void integrate(const Eigen::Ref<const VectorXs>& x, | ||
| 40 | const Eigen::Ref<const VectorXs>& dx, | ||
| 41 | Eigen::Ref<VectorXs> xout) const override; | ||
| 42 | /** | ||
| 43 | * @brief This computes the Jacobian of the diff method by finite | ||
| 44 | * differentiation: | ||
| 45 | * \f{equation}{ | ||
| 46 | * Jfirst[:,k] = diff(int(x_1, dx_dist), x_2) - diff(x_1, x_2)/disturbance | ||
| 47 | * \f} | ||
| 48 | * and | ||
| 49 | * \f{equation}{ | ||
| 50 | * Jsecond[:,k] = diff(x_1, int(x_2, dx_dist)) - diff(x_1, x_2)/disturbance | ||
| 51 | * \f} | ||
| 52 | * | ||
| 53 | * @param Jfirst | ||
| 54 | * @param Jsecond | ||
| 55 | * @param firstsecond | ||
| 56 | */ | ||
| 57 | virtual void Jdiff(const Eigen::Ref<const VectorXs>& x0, | ||
| 58 | const Eigen::Ref<const VectorXs>& x1, | ||
| 59 | Eigen::Ref<MatrixXs> Jfirst, Eigen::Ref<MatrixXs> Jsecond, | ||
| 60 | Jcomponent firstsecond = both) const override; | ||
| 61 | /** | ||
| 62 | * @brief This computes the Jacobian of the integrate method by finite | ||
| 63 | * differentiation: | ||
| 64 | * \f{equation}{ | ||
| 65 | * Jfirst[:,k] = diff( int(x, d_x), int( int(x, dx_dist), dx) )/disturbance | ||
| 66 | * \f} | ||
| 67 | * and | ||
| 68 | * \f{equation}{ | ||
| 69 | * Jsecond[:,k] = diff( int(x, d_x), int( x, dx + dx_dist) )/disturbance | ||
| 70 | * \f} | ||
| 71 | * | ||
| 72 | * @param Jfirst | ||
| 73 | * @param Jsecond | ||
| 74 | * @param firstsecond | ||
| 75 | */ | ||
| 76 | virtual void Jintegrate(const Eigen::Ref<const VectorXs>& x, | ||
| 77 | const Eigen::Ref<const VectorXs>& dx, | ||
| 78 | Eigen::Ref<MatrixXs> Jfirst, | ||
| 79 | Eigen::Ref<MatrixXs> Jsecond, | ||
| 80 | const Jcomponent firstsecond = both, | ||
| 81 | const AssignmentOp op = setto) const override; | ||
| 82 | |||
| 83 | virtual void JintegrateTransport( | ||
| 84 | const Eigen::Ref<const VectorXs>& x, const Eigen::Ref<const VectorXs>& dx, | ||
| 85 | Eigen::Ref<MatrixXs> Jin, | ||
| 86 | const Jcomponent firstsecond = both) const override; | ||
| 87 | |||
| 88 | template <typename NewScalar> | ||
| 89 | StateNumDiffTpl<NewScalar> cast() const; | ||
| 90 | |||
| 91 | /** | ||
| 92 | * @brief Return the disturbance constant used in the numerical | ||
| 93 | * differentiation routine | ||
| 94 | */ | ||
| 95 | const Scalar get_disturbance() const; | ||
| 96 | |||
| 97 | /** | ||
| 98 | * @brief Modify the disturbance constant used by the numerical | ||
| 99 | * differentiation routine | ||
| 100 | */ | ||
| 101 | void set_disturbance(const Scalar disturbance); | ||
| 102 | |||
| 103 | /** | ||
| 104 | * @brief Print relevant information of the state numdiff | ||
| 105 | * | ||
| 106 | * @param[out] os Output stream object | ||
| 107 | */ | ||
| 108 | virtual void print(std::ostream& os) const override; | ||
| 109 | |||
| 110 | private: | ||
| 111 | std::shared_ptr<Base> | ||
| 112 | state_; //!< state we need to compute the numerical differentiation | ||
| 113 | Scalar e_jac_; //!< Constant used for computing disturbances in Jacobian | ||
| 114 | //!< calculation | ||
| 115 | |||
| 116 | protected: | ||
| 117 | using Base::has_limits_; | ||
| 118 | using Base::lb_; | ||
| 119 | using Base::ndx_; | ||
| 120 | using Base::nq_; | ||
| 121 | using Base::nv_; | ||
| 122 | using Base::nx_; | ||
| 123 | using Base::ub_; | ||
| 124 | }; | ||
| 125 | |||
| 126 | } // namespace crocoddyl | ||
| 127 | |||
| 128 | /* --- Details -------------------------------------------------------------- */ | ||
| 129 | /* --- Details -------------------------------------------------------------- */ | ||
| 130 | /* --- Details -------------------------------------------------------------- */ | ||
| 131 | #include "crocoddyl/core/numdiff/state.hxx" | ||
| 132 | |||
| 133 | CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(crocoddyl::StateNumDiffTpl) | ||
| 134 | |||
| 135 | #endif // CROCODDYL_CORE_NUMDIFF_STATE_HPP_ | ||
| 136 |