Line |
Branch |
Exec |
Source |
1 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
2 |
|
|
// BSD 3-Clause License |
3 |
|
|
// |
4 |
|
|
// Copyright (C) 2019-2025, LAAS-CNRS, University of Edinburgh, |
5 |
|
|
// Heriot-Watt University |
6 |
|
|
// Copyright note valid unless otherwise stated in individual files. |
7 |
|
|
// All rights reserved. |
8 |
|
|
/////////////////////////////////////////////////////////////////////////////// |
9 |
|
|
|
10 |
|
|
namespace crocoddyl { |
11 |
|
|
|
12 |
|
|
template <typename Scalar> |
13 |
|
✗ |
StateVectorTpl<Scalar>::StateVectorTpl(const std::size_t nx) |
14 |
|
✗ |
: StateAbstractTpl<Scalar>(nx, nx) {} |
15 |
|
|
|
16 |
|
|
template <typename Scalar> |
17 |
|
✗ |
StateVectorTpl<Scalar>::~StateVectorTpl() {} |
18 |
|
|
|
19 |
|
|
template <typename Scalar> |
20 |
|
✗ |
typename MathBaseTpl<Scalar>::VectorXs StateVectorTpl<Scalar>::zero() const { |
21 |
|
✗ |
return VectorXs::Zero(nx_); |
22 |
|
|
} |
23 |
|
|
|
24 |
|
|
template <typename Scalar> |
25 |
|
✗ |
typename MathBaseTpl<Scalar>::VectorXs StateVectorTpl<Scalar>::rand() const { |
26 |
|
✗ |
return VectorXs::Random(nx_); |
27 |
|
|
} |
28 |
|
|
|
29 |
|
|
template <typename Scalar> |
30 |
|
✗ |
void StateVectorTpl<Scalar>::diff(const Eigen::Ref<const VectorXs>& x0, |
31 |
|
|
const Eigen::Ref<const VectorXs>& x1, |
32 |
|
|
Eigen::Ref<VectorXs> dxout) const { |
33 |
|
✗ |
if (static_cast<std::size_t>(x0.size()) != nx_) { |
34 |
|
✗ |
throw_pretty( |
35 |
|
|
"Invalid argument: " << "x0 has wrong dimension (it should be " + |
36 |
|
|
std::to_string(nx_) + ")"); |
37 |
|
|
} |
38 |
|
✗ |
if (static_cast<std::size_t>(x1.size()) != nx_) { |
39 |
|
✗ |
throw_pretty( |
40 |
|
|
"Invalid argument: " << "x1 has wrong dimension (it should be " + |
41 |
|
|
std::to_string(nx_) + ")"); |
42 |
|
|
} |
43 |
|
✗ |
if (static_cast<std::size_t>(dxout.size()) != ndx_) { |
44 |
|
✗ |
throw_pretty( |
45 |
|
|
"Invalid argument: " << "dxout has wrong dimension (it should be " + |
46 |
|
|
std::to_string(ndx_) + ")"); |
47 |
|
|
} |
48 |
|
✗ |
dxout = x1 - x0; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
template <typename Scalar> |
52 |
|
✗ |
void StateVectorTpl<Scalar>::integrate(const Eigen::Ref<const VectorXs>& x, |
53 |
|
|
const Eigen::Ref<const VectorXs>& dx, |
54 |
|
|
Eigen::Ref<VectorXs> xout) const { |
55 |
|
✗ |
if (static_cast<std::size_t>(x.size()) != nx_) { |
56 |
|
✗ |
throw_pretty( |
57 |
|
|
"Invalid argument: " << "x has wrong dimension (it should be " + |
58 |
|
|
std::to_string(nx_) + ")"); |
59 |
|
|
} |
60 |
|
✗ |
if (static_cast<std::size_t>(dx.size()) != ndx_) { |
61 |
|
✗ |
throw_pretty( |
62 |
|
|
"Invalid argument: " << "dx has wrong dimension (it should be " + |
63 |
|
|
std::to_string(ndx_) + ")"); |
64 |
|
|
} |
65 |
|
✗ |
if (static_cast<std::size_t>(xout.size()) != nx_) { |
66 |
|
✗ |
throw_pretty( |
67 |
|
|
"Invalid argument: " << "xout has wrong dimension (it should be " + |
68 |
|
|
std::to_string(nx_) + ")"); |
69 |
|
|
} |
70 |
|
✗ |
xout = x + dx; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
template <typename Scalar> |
74 |
|
✗ |
void StateVectorTpl<Scalar>::Jdiff(const Eigen::Ref<const VectorXs>&, |
75 |
|
|
const Eigen::Ref<const VectorXs>&, |
76 |
|
|
Eigen::Ref<MatrixXs> Jfirst, |
77 |
|
|
Eigen::Ref<MatrixXs> Jsecond, |
78 |
|
|
const Jcomponent firstsecond) const { |
79 |
|
✗ |
assert_pretty( |
80 |
|
|
is_a_Jcomponent(firstsecond), |
81 |
|
|
("firstsecond must be one of the Jcomponent {both, first, second}")); |
82 |
|
✗ |
if (firstsecond == first || firstsecond == both) { |
83 |
|
✗ |
if (static_cast<std::size_t>(Jfirst.rows()) != ndx_ || |
84 |
|
✗ |
static_cast<std::size_t>(Jfirst.cols()) != ndx_) { |
85 |
|
✗ |
throw_pretty( |
86 |
|
|
"Invalid argument: " << "Jfirst has wrong dimension (it should be " + |
87 |
|
|
std::to_string(ndx_) + "," + |
88 |
|
|
std::to_string(ndx_) + ")"); |
89 |
|
|
} |
90 |
|
✗ |
Jfirst.setZero(); |
91 |
|
✗ |
Jfirst.diagonal() = MathBase::VectorXs::Constant(ndx_, Scalar(-1.)); |
92 |
|
|
} |
93 |
|
✗ |
if (firstsecond == second || firstsecond == both) { |
94 |
|
✗ |
if (static_cast<std::size_t>(Jsecond.rows()) != ndx_ || |
95 |
|
✗ |
static_cast<std::size_t>(Jsecond.cols()) != ndx_) { |
96 |
|
✗ |
throw_pretty( |
97 |
|
|
"Invalid argument: " << "Jsecond has wrong dimension (it should be " + |
98 |
|
|
std::to_string(ndx_) + "," + |
99 |
|
|
std::to_string(ndx_) + ")"); |
100 |
|
|
} |
101 |
|
✗ |
Jsecond.setZero(); |
102 |
|
✗ |
Jsecond.diagonal() = MathBase::VectorXs::Constant(ndx_, Scalar(1.)); |
103 |
|
|
} |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
template <typename Scalar> |
107 |
|
✗ |
void StateVectorTpl<Scalar>::Jintegrate(const Eigen::Ref<const VectorXs>&, |
108 |
|
|
const Eigen::Ref<const VectorXs>&, |
109 |
|
|
Eigen::Ref<MatrixXs> Jfirst, |
110 |
|
|
Eigen::Ref<MatrixXs> Jsecond, |
111 |
|
|
const Jcomponent firstsecond, |
112 |
|
|
const AssignmentOp op) const { |
113 |
|
✗ |
assert_pretty( |
114 |
|
|
is_a_Jcomponent(firstsecond), |
115 |
|
|
("firstsecond must be one of the Jcomponent {both, first, second}")); |
116 |
|
✗ |
assert_pretty(is_a_AssignmentOp(op), |
117 |
|
|
("op must be one of the AssignmentOp {settop, addto, rmfrom}")); |
118 |
|
✗ |
if (firstsecond == first || firstsecond == both) { |
119 |
|
✗ |
if (static_cast<std::size_t>(Jfirst.rows()) != ndx_ || |
120 |
|
✗ |
static_cast<std::size_t>(Jfirst.cols()) != ndx_) { |
121 |
|
✗ |
throw_pretty( |
122 |
|
|
"Invalid argument: " << "Jfirst has wrong dimension (it should be " + |
123 |
|
|
std::to_string(ndx_) + "," + |
124 |
|
|
std::to_string(ndx_) + ")"); |
125 |
|
|
} |
126 |
|
✗ |
switch (op) { |
127 |
|
✗ |
case setto: |
128 |
|
✗ |
Jfirst.diagonal().array() = Scalar(1.); |
129 |
|
✗ |
break; |
130 |
|
✗ |
case addto: |
131 |
|
✗ |
Jfirst.diagonal().array() += Scalar(1.); |
132 |
|
✗ |
break; |
133 |
|
✗ |
case rmfrom: |
134 |
|
✗ |
Jfirst.diagonal().array() -= Scalar(1.); |
135 |
|
✗ |
break; |
136 |
|
✗ |
default: |
137 |
|
✗ |
throw_pretty( |
138 |
|
|
"Invalid argument: allowed operators: setto, addto, rmfrom"); |
139 |
|
|
break; |
140 |
|
|
} |
141 |
|
|
} |
142 |
|
✗ |
if (firstsecond == second || firstsecond == both) { |
143 |
|
✗ |
if (static_cast<std::size_t>(Jsecond.rows()) != ndx_ || |
144 |
|
✗ |
static_cast<std::size_t>(Jsecond.cols()) != ndx_) { |
145 |
|
✗ |
throw_pretty( |
146 |
|
|
"Invalid argument: " << "Jsecond has wrong dimension (it should be " + |
147 |
|
|
std::to_string(ndx_) + "," + |
148 |
|
|
std::to_string(ndx_) + ")"); |
149 |
|
|
} |
150 |
|
✗ |
switch (op) { |
151 |
|
✗ |
case setto: |
152 |
|
✗ |
Jsecond.diagonal().array() = Scalar(1.); |
153 |
|
✗ |
break; |
154 |
|
✗ |
case addto: |
155 |
|
✗ |
Jsecond.diagonal().array() += Scalar(1.); |
156 |
|
✗ |
break; |
157 |
|
✗ |
case rmfrom: |
158 |
|
✗ |
Jsecond.diagonal().array() -= Scalar(1.); |
159 |
|
✗ |
break; |
160 |
|
✗ |
default: |
161 |
|
✗ |
throw_pretty( |
162 |
|
|
"Invalid argument: allowed operators: setto, addto, rmfrom"); |
163 |
|
|
break; |
164 |
|
|
} |
165 |
|
|
} |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
template <typename Scalar> |
169 |
|
✗ |
void StateVectorTpl<Scalar>::JintegrateTransport( |
170 |
|
|
const Eigen::Ref<const VectorXs>&, const Eigen::Ref<const VectorXs>&, |
171 |
|
|
Eigen::Ref<MatrixXs>, const Jcomponent firstsecond) const { |
172 |
|
✗ |
assert_pretty(is_a_Jcomponent(firstsecond), ("")); |
173 |
|
✗ |
if (firstsecond != first && firstsecond != second) { |
174 |
|
✗ |
throw_pretty( |
175 |
|
|
"Invalid argument: firstsecond must be either first or second. both " |
176 |
|
|
"not supported for this operation."); |
177 |
|
|
} |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
template <typename Scalar> |
181 |
|
|
template <typename NewScalar> |
182 |
|
✗ |
StateVectorTpl<NewScalar> StateVectorTpl<Scalar>::cast() const { |
183 |
|
|
typedef StateVectorTpl<NewScalar> ReturnType; |
184 |
|
✗ |
ReturnType res(nx_); |
185 |
|
✗ |
return res; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
template <typename Scalar> |
189 |
|
✗ |
void StateVectorTpl<Scalar>::print(std::ostream& os) const { |
190 |
|
✗ |
os << "StateVector {nx=" << nx_ << "}"; |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
} // namespace crocoddyl |
194 |
|
|
|