9 #include <unsupported/Eigen/AutoDiff> 16 template <
typename DerType>
17 double round(
const Eigen::AutoDiffScalar<DerType>&
x) {
18 return round(x.value());
24 template <
typename DerType>
25 double floor(
const Eigen::AutoDiffScalar<DerType>&
x) {
26 return floor(x.value());
32 template <
typename Derived>
34 typedef typename Eigen::Matrix<
typename Derived::Scalar::Scalar,
35 Derived::RowsAtCompileTime,
36 Derived::ColsAtCompileTime>
type;
39 template <
typename Derived>
41 const Eigen::MatrixBase<Derived>& auto_diff_matrix) {
43 auto_diff_matrix.cols());
44 for (
int i = 0; i < auto_diff_matrix.rows(); i++) {
45 for (
int j = 0; j < auto_diff_matrix.cols(); ++j) {
46 ret(i, j) = auto_diff_matrix(i, j).value();
52 template <
typename Derived>
55 Eigen::Matrix<
typename Derived::Scalar::Scalar,
56 Derived::RowsAtCompileTime, Derived::ColsAtCompileTime>,
60 template <
typename Derived>
62 const Eigen::MatrixBase<Derived>& auto_diff_matrix,
63 int num_variables = Eigen::Dynamic) {
64 int num_variables_from_matrix = 0;
65 for (
int i = 0; i < auto_diff_matrix.size(); ++i) {
66 num_variables_from_matrix =
67 std::max(num_variables_from_matrix,
68 static_cast<int>(auto_diff_matrix(i).derivatives().
size()));
70 if (num_variables == Eigen::Dynamic) {
71 num_variables = num_variables_from_matrix;
72 }
else if (num_variables_from_matrix != 0 &&
73 num_variables_from_matrix != num_variables) {
74 std::stringstream buf;
75 buf <<
"Input matrix has derivatives w.r.t " << num_variables_from_matrix
76 <<
" variables, whereas num_variables is " << num_variables <<
".\n";
77 buf <<
"Either num_variables_from_matrix should be zero, or it should " 78 "match num_variables.";
79 throw std::runtime_error(buf.str());
83 auto_diff_matrix.size(), num_variables);
84 for (
int row = 0; row < auto_diff_matrix.rows(); row++) {
85 for (
int col = 0; col < auto_diff_matrix.cols(); col++) {
87 gradient.row(row + col * auto_diff_matrix.rows()).transpose();
88 if (auto_diff_matrix(row, col).derivatives().size() == 0) {
89 gradient_row.setZero();
91 gradient_row = auto_diff_matrix(row, col).derivatives();
Definition: autodiff.h:53
AutoDiffToValueMatrix< Derived >::type autoDiffToValueMatrix(const Eigen::MatrixBase< Derived > &auto_diff_matrix)
Definition: autodiff.h:40
std::vector< Number > x
Definition: IpoptSolver.cpp:169
Definition: constants.h:3
Definition: autodiff.h:33
AutoDiffToGradientMatrix< Derived >::type autoDiffToGradientMatrix(const Eigen::MatrixBase< Derived > &auto_diff_matrix, int num_variables=Eigen::Dynamic)
Definition: autodiff.h:61
double round(const Eigen::AutoDiffScalar< DerType > &x)
Overloads round to mimic std::round from <cmath>.
Definition: autodiff.h:17
Eigen::Matrix< typename Derived::Scalar::Scalar, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime > type
Definition: autodiff.h:36
double floor(const Eigen::AutoDiffScalar< DerType > &x)
Overloads floor to mimic std::floor from <cmath>.
Definition: autodiff.h:25
Gradient< Eigen::Matrix< typename Derived::Scalar::Scalar, Derived::RowsAtCompileTime, Derived::ColsAtCompileTime >, Eigen::Dynamic >::type type
Definition: autodiff.h:57
Definition: gradient.h:13
std::size_t size(const VecType &vec)
size()
Definition: Vector.h:113