Directory: | ./ |
---|---|
File: | include/crocoddyl/core/numdiff/activation.hxx |
Date: | 2025-01-30 11:01:55 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 32 | 34 | 94.1% |
Branches: | 19 | 68 | 27.9% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | ||
2 | // BSD 3-Clause License | ||
3 | // | ||
4 | // Copyright (C) 2019-2023, LAAS-CNRS, University of Edinburgh, | ||
5 | // New York University, Max Planck Gesellschaft | ||
6 | // Copyright note valid unless otherwise stated in individual files. | ||
7 | // All rights reserved. | ||
8 | /////////////////////////////////////////////////////////////////////////////// | ||
9 | |||
10 | #include "crocoddyl/core/utils/exception.hpp" | ||
11 | |||
12 | namespace crocoddyl { | ||
13 | |||
14 | template <typename Scalar> | ||
15 | 10 | ActivationModelNumDiffTpl<Scalar>::ActivationModelNumDiffTpl( | |
16 | std::shared_ptr<Base> model) | ||
17 | : Base(model->get_nr()), | ||
18 | 10 | model_(model), | |
19 | 10 | e_jac_(std::sqrt(2.0 * std::numeric_limits<Scalar>::epsilon())) {} | |
20 | |||
21 | template <typename Scalar> | ||
22 | 24 | ActivationModelNumDiffTpl<Scalar>::~ActivationModelNumDiffTpl() {} | |
23 | |||
24 | template <typename Scalar> | ||
25 | 9 | void ActivationModelNumDiffTpl<Scalar>::calc( | |
26 | const std::shared_ptr<ActivationDataAbstract>& data, | ||
27 | const Eigen::Ref<const VectorXs>& r) { | ||
28 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
|
9 | if (static_cast<std::size_t>(r.size()) != model_->get_nr()) { |
29 | ✗ | throw_pretty( | |
30 | "Invalid argument: " << "r has wrong dimension (it should be " + | ||
31 | std::to_string(model_->get_nr()) + ")"); | ||
32 | } | ||
33 | 9 | std::shared_ptr<Data> data_nd = std::static_pointer_cast<Data>(data); | |
34 |
1/2✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
|
9 | model_->calc(data_nd->data_0, r); |
35 | 9 | data->a_value = data_nd->data_0->a_value; | |
36 | 9 | } | |
37 | |||
38 | template <typename Scalar> | ||
39 | 9 | void ActivationModelNumDiffTpl<Scalar>::calcDiff( | |
40 | const std::shared_ptr<ActivationDataAbstract>& data, | ||
41 | const Eigen::Ref<const VectorXs>& r) { | ||
42 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
|
9 | if (static_cast<std::size_t>(r.size()) != model_->get_nr()) { |
43 | ✗ | throw_pretty( | |
44 | "Invalid argument: " << "r has wrong dimension (it should be " + | ||
45 | std::to_string(model_->get_nr()) + ")"); | ||
46 | } | ||
47 | 9 | std::shared_ptr<Data> data_nd = std::static_pointer_cast<Data>(data); | |
48 | |||
49 | 9 | const Scalar a_value0 = data_nd->data_0->a_value; | |
50 | 9 | data->a_value = data_nd->data_0->a_value; | |
51 | 9 | const std::size_t nr = model_->get_nr(); | |
52 | |||
53 | // Computing the d activation(r) / dr | ||
54 |
1/2✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
|
9 | const Scalar rh_jac = e_jac_ * std::max(1., r.norm()); |
55 |
1/2✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 | data_nd->rp = r; |
56 |
2/2✓ Branch 0 taken 45 times.
✓ Branch 1 taken 9 times.
|
54 | for (unsigned int i_r = 0; i_r < nr; ++i_r) { |
57 |
1/2✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
|
45 | data_nd->rp(i_r) += rh_jac; |
58 |
2/4✓ Branch 3 taken 45 times.
✗ Branch 4 not taken.
✓ Branch 8 taken 45 times.
✗ Branch 9 not taken.
|
45 | model_->calc(data_nd->data_rp[i_r], data_nd->rp); |
59 |
1/2✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
|
45 | data_nd->rp(i_r) -= rh_jac; |
60 |
1/2✓ Branch 5 taken 45 times.
✗ Branch 6 not taken.
|
45 | data->Ar(i_r) = (data_nd->data_rp[i_r]->a_value - a_value0) / rh_jac; |
61 | } | ||
62 | |||
63 | // Computing the d^2 action(r) / dr^2 | ||
64 |
4/8✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
✓ Branch 10 taken 9 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 9 times.
✗ Branch 14 not taken.
|
9 | data_nd->Arr_.noalias() = data->Ar * data->Ar.transpose(); |
65 |
2/4✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 7 taken 9 times.
✗ Branch 8 not taken.
|
9 | data->Arr.diagonal() = data_nd->Arr_.diagonal(); |
66 | 9 | } | |
67 | |||
68 | template <typename Scalar> | ||
69 | std::shared_ptr<ActivationDataAbstractTpl<Scalar> > | ||
70 | 11 | ActivationModelNumDiffTpl<Scalar>::createData() { | |
71 |
1/2✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
|
11 | return std::allocate_shared<Data>(Eigen::aligned_allocator<Data>(), this); |
72 | } | ||
73 | |||
74 | template <typename Scalar> | ||
75 | const std::shared_ptr<ActivationModelAbstractTpl<Scalar> >& | ||
76 | 137 | ActivationModelNumDiffTpl<Scalar>::get_model() const { | |
77 | 137 | return model_; | |
78 | } | ||
79 | |||
80 | template <typename Scalar> | ||
81 | 9 | const Scalar ActivationModelNumDiffTpl<Scalar>::get_disturbance() const { | |
82 | 9 | return e_jac_; | |
83 | } | ||
84 | |||
85 | template <typename Scalar> | ||
86 | void ActivationModelNumDiffTpl<Scalar>::set_disturbance( | ||
87 | const Scalar disturbance) { | ||
88 | if (disturbance < 0.) { | ||
89 | throw_pretty("Invalid argument: " << "Disturbance constant is positive"); | ||
90 | } | ||
91 | e_jac_ = disturbance; | ||
92 | } | ||
93 | |||
94 | } // namespace crocoddyl | ||
95 |