forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautodiffxd_make_coherent.h
30 lines (24 loc) · 1.18 KB
/
autodiffxd_make_coherent.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
#pragma once
#include "drake/common/autodiff.h"
#include "drake/common/symbolic.h"
namespace drake {
/// Makes the derviatives of the recipient coherent with respect to those of the
/// donor variable (see drake/common/autodiffxd.h). If the recipient's
/// derivatives are already populated with a vector of the same size as that of
/// the donor, variables pass through unchanged. An exception is thrown when
/// there are nonempty vectors of different sizes.
inline void autodiffxd_make_coherent(const AutoDiffXd& donor,
AutoDiffXd* recipient) {
DRAKE_ASSERT(recipient != nullptr);
if (recipient->derivatives().size() == 0) {
recipient->derivatives() =
Eigen::VectorXd::Zero(donor.derivatives().size());
} else if (recipient->derivatives().size() != donor.derivatives().size()) {
throw std::runtime_error("Nonempty recipient derivatives must match the "
"size of the donor derivatives.");
}
}
inline void autodiffxd_make_coherent(const double&, double*) {}
inline void autodiffxd_make_coherent(const symbolic::Expression&,
symbolic::Expression*) {}
} // namespace drake