forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gradient_util.h
293 lines (268 loc) · 11.1 KB
/
gradient_util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#pragma once
#include <array>
#include <vector>
#include <Eigen/Core>
#include <Eigen/Dense>
#include "drake/common/drake_assert.h"
#include "drake/math/gradient.h"
namespace drake {
namespace math {
template <std::size_t Size>
std::array<int, Size> intRange(int start) {
std::array<int, Size> ret;
for (unsigned int i = 0; i < Size; i++) {
ret[i] = i + start;
}
return ret;
}
/*
* Output type of matGradMultMat
*/
template <typename DerivedA, typename DerivedB, typename DerivedDA>
struct MatGradMultMat {
typedef typename Eigen::Matrix<
typename DerivedA::Scalar,
(DerivedA::RowsAtCompileTime == Eigen::Dynamic ||
DerivedB::ColsAtCompileTime == Eigen::Dynamic
? Eigen::Dynamic
: DerivedA::RowsAtCompileTime * DerivedB::ColsAtCompileTime),
DerivedDA::ColsAtCompileTime> type;
};
/*
* Output type of matGradMult
*/
template <typename DerivedDA, typename DerivedB>
struct MatGradMult {
typedef typename Eigen::Matrix<
typename DerivedDA::Scalar,
(DerivedDA::RowsAtCompileTime == Eigen::Dynamic ||
DerivedB::SizeAtCompileTime == Eigen::Dynamic
? Eigen::Dynamic
: DerivedDA::RowsAtCompileTime / DerivedB::RowsAtCompileTime *
DerivedB::ColsAtCompileTime),
DerivedDA::ColsAtCompileTime> type;
};
/*
* Output type and array types of getSubMatrixGradient for std::arrays
* specifying rows and columns
*/
template <int QSubvectorSize, typename Derived, std::size_t NRows,
std::size_t NCols>
struct GetSubMatrixGradientArray {
typedef typename Eigen::Matrix<typename Derived::Scalar, (NRows * NCols),
((QSubvectorSize == Eigen::Dynamic)
? Derived::ColsAtCompileTime
: QSubvectorSize)> type;
};
/*
* Output type of getSubMatrixGradient for a single element of the input matrix
*/
template <int QSubvectorSize, typename Derived>
struct GetSubMatrixGradientSingleElement {
typedef typename Eigen::Block<const Derived, 1,
((QSubvectorSize == Eigen::Dynamic)
? Derived::ColsAtCompileTime
: QSubvectorSize)> type;
};
/*
* Profile results: looks like return value optimization works; a version that
* sets a reference
* instead of returning a value is just as fast and this is cleaner.
*/
template <typename Derived>
typename Derived::PlainObject transposeGrad(
const Eigen::MatrixBase<Derived>& dX, typename Derived::Index rows_X) {
typename Derived::PlainObject dX_transpose(dX.rows(), dX.cols());
typename Derived::Index numel = dX.rows();
typename Derived::Index index = 0;
for (int i = 0; i < numel; i++) {
dX_transpose.row(i) = dX.row(index);
index += rows_X;
if (index >= numel) {
index = (index % numel) + 1;
}
}
return dX_transpose;
}
template <typename DerivedA, typename DerivedB, typename DerivedDA,
typename DerivedDB>
typename MatGradMultMat<DerivedA, DerivedB, DerivedDA>::type matGradMultMat(
const Eigen::MatrixBase<DerivedA>& A, const Eigen::MatrixBase<DerivedB>& B,
const Eigen::MatrixBase<DerivedDA>& dA,
const Eigen::MatrixBase<DerivedDB>& dB) {
DRAKE_ASSERT(dA.cols() == dB.cols());
typename MatGradMultMat<DerivedA, DerivedB, DerivedDA>::type ret(
A.rows() * B.cols(), dA.cols());
for (int col = 0; col < B.cols(); col++) {
auto block = ret.template block<DerivedA::RowsAtCompileTime,
DerivedDA::ColsAtCompileTime>(
col * A.rows(), 0, A.rows(), dA.cols());
// A * dB part:
block.noalias() = A *
dB.template block<DerivedA::ColsAtCompileTime,
DerivedDA::ColsAtCompileTime>(
col * A.cols(), 0, A.cols(), dA.cols());
for (int row = 0; row < B.rows(); row++) {
// B * dA part:
block.noalias() += B(row, col) *
dA.template block<DerivedA::RowsAtCompileTime,
DerivedDA::ColsAtCompileTime>(
row * A.rows(), 0, A.rows(), dA.cols());
}
}
return ret;
// much slower and requires eigen/unsupported:
// return Eigen::kroneckerProduct(Eigen::MatrixXd::Identity(B.cols(),
// B.cols()), A) * dB + Eigen::kroneckerProduct(B.transpose(),
// Eigen::MatrixXd::Identity(A.rows(), A.rows())) * dA;
}
template <typename DerivedDA, typename DerivedB>
typename MatGradMult<DerivedDA, DerivedB>::type matGradMult(
const Eigen::MatrixBase<DerivedDA>& dA,
const Eigen::MatrixBase<DerivedB>& B) {
DRAKE_ASSERT(B.rows() == 0 ? dA.rows() == 0 : dA.rows() % B.rows() == 0);
typename DerivedDA::Index A_rows = B.rows() == 0 ? 0 : dA.rows() / B.rows();
const int A_rows_at_compile_time =
(DerivedDA::RowsAtCompileTime == Eigen::Dynamic ||
DerivedB::RowsAtCompileTime == Eigen::Dynamic)
? Eigen::Dynamic
: static_cast<int>(DerivedDA::RowsAtCompileTime /
DerivedB::RowsAtCompileTime);
typename MatGradMult<DerivedDA, DerivedB>::type ret(A_rows * B.cols(),
dA.cols());
ret.setZero();
for (int col = 0; col < B.cols(); col++) {
auto block = ret.template block<A_rows_at_compile_time,
DerivedDA::ColsAtCompileTime>(
col * A_rows, 0, A_rows, dA.cols());
for (int row = 0; row < B.rows(); row++) {
// B * dA part:
block.noalias() += B(row, col) *
dA.template block<A_rows_at_compile_time,
DerivedDA::ColsAtCompileTime>(
row * A_rows, 0, A_rows, dA.cols());
}
}
return ret;
}
// TODO(tkoolen): could save copies once
// http://eigen.tuxfamily.org/bz/show_bug.cgi?id=329 is fixed
template <typename Derived>
Eigen::Matrix<typename Derived::Scalar, Eigen::Dynamic, Eigen::Dynamic>
getSubMatrixGradient(const Eigen::MatrixBase<Derived>& dM,
const std::vector<int>& rows, const std::vector<int>& cols,
typename Derived::Index M_rows, int q_start = 0,
typename Derived::Index q_subvector_size = -1) {
if (q_subvector_size < 0) {
q_subvector_size = dM.cols() - q_start;
}
Eigen::MatrixXd dM_submatrix(rows.size() * cols.size(), q_subvector_size);
int index = 0;
for (std::vector<int>::const_iterator col = cols.begin(); col != cols.end();
++col) {
for (std::vector<int>::const_iterator row = rows.begin(); row != rows.end();
++row) {
dM_submatrix.row(index) =
dM.block(*row + *col * M_rows, q_start, 1, q_subvector_size);
index++;
}
}
return dM_submatrix;
}
template <int QSubvectorSize, typename Derived, std::size_t NRows,
std::size_t NCols>
typename GetSubMatrixGradientArray<QSubvectorSize, Derived, NRows, NCols>::type
getSubMatrixGradient(
const Eigen::MatrixBase<Derived>& dM, const std::array<int, NRows>& rows,
const std::array<int, NCols>& cols, typename Derived::Index M_rows,
int q_start = 0,
typename Derived::Index q_subvector_size = QSubvectorSize) {
if (q_subvector_size == Eigen::Dynamic) {
q_subvector_size = dM.cols() - q_start;
}
Eigen::Matrix<typename Derived::Scalar, NRows * NCols,
Derived::ColsAtCompileTime> dM_submatrix(NRows * NCols,
q_subvector_size);
int index = 0;
for (typename std::array<int, NCols>::const_iterator col = cols.begin();
col != cols.end(); ++col) {
for (typename std::array<int, NRows>::const_iterator row = rows.begin();
row != rows.end(); ++row) {
dM_submatrix.row(index++) = dM.template block<1, QSubvectorSize>(
(*row) + (*col) * M_rows, q_start, 1, q_subvector_size);
}
}
return dM_submatrix;
}
template <int QSubvectorSize, typename Derived>
typename GetSubMatrixGradientSingleElement<QSubvectorSize, Derived>::type
getSubMatrixGradient(
const Eigen::MatrixBase<Derived>& dM, int row, int col,
typename Derived::Index M_rows, typename Derived::Index q_start = 0,
typename Derived::Index q_subvector_size = QSubvectorSize) {
if (q_subvector_size == Eigen::Dynamic) {
q_subvector_size = dM.cols() - q_start;
}
return dM.template block<1, QSubvectorSize>(row + col * M_rows, q_start, 1,
q_subvector_size);
}
template <typename DerivedA, typename DerivedB>
// TODO(#2274) Fix NOLINTNEXTLINE(runtime/references).
void setSubMatrixGradient(Eigen::MatrixBase<DerivedA>& dM,
const Eigen::MatrixBase<DerivedB>& dM_submatrix,
const std::vector<int>& rows,
const std::vector<int>& cols,
typename DerivedA::Index M_rows,
typename DerivedA::Index q_start = 0,
typename DerivedA::Index q_subvector_size = -1) {
if (q_subvector_size < 0) {
q_subvector_size = dM.cols() - q_start;
}
int index = 0;
for (typename std::vector<int>::const_iterator col = cols.begin();
col != cols.end(); ++col) {
for (typename std::vector<int>::const_iterator row = rows.begin();
row != rows.end(); ++row) {
dM.block((*row) + (*col) * M_rows, q_start, 1, q_subvector_size) =
dM_submatrix.row(index++);
}
}
}
template <int QSubvectorSize, typename DerivedA, typename DerivedB,
std::size_t NRows, std::size_t NCols>
void setSubMatrixGradient(
// TODO(#2274) Fix NOLINTNEXTLINE(runtime/references).
Eigen::MatrixBase<DerivedA>& dM,
const Eigen::MatrixBase<DerivedB>& dM_submatrix,
const std::array<int, NRows>& rows, const std::array<int, NCols>& cols,
typename DerivedA::Index M_rows, typename DerivedA::Index q_start = 0,
typename DerivedA::Index q_subvector_size = QSubvectorSize) {
if (q_subvector_size == Eigen::Dynamic) {
q_subvector_size = dM.cols() - q_start;
}
int index = 0;
for (typename std::array<int, NCols>::const_iterator col = cols.begin();
col != cols.end(); ++col) {
for (typename std::array<int, NRows>::const_iterator row = rows.begin();
row != rows.end(); ++row) {
dM.template block<1, QSubvectorSize>((*row) + (*col) * M_rows, q_start, 1,
q_subvector_size) =
dM_submatrix.row(index++);
}
}
}
template <int QSubvectorSize, typename DerivedDM, typename DerivedDMSub>
void setSubMatrixGradient(
// TODO(#2274) Fix NOLINTNEXTLINE(runtime/references).
Eigen::MatrixBase<DerivedDM>& dM,
const Eigen::MatrixBase<DerivedDMSub>& dM_submatrix, int row, int col,
typename DerivedDM::Index M_rows, typename DerivedDM::Index q_start = 0,
typename DerivedDM::Index q_subvector_size = QSubvectorSize) {
if (q_subvector_size == Eigen::Dynamic) {
q_subvector_size = dM.cols() - q_start;
}
dM.template block<1, QSubvectorSize>(row + col * M_rows, q_start, 1,
q_subvector_size) = dM_submatrix;
}
} // namespace math
} // namespace drake