Drake
autodiff.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <cmath>
7 
8 #include <Eigen/Dense>
9 #include <unsupported/Eigen/AutoDiff>
10 
11 #include "drake/math/gradient.h"
12 
16 template <typename DerType>
17 double round(const Eigen::AutoDiffScalar<DerType>& x) {
18  return round(x.value());
19 }
20 
24 template <typename DerType>
25 double floor(const Eigen::AutoDiffScalar<DerType>& x) {
26  return floor(x.value());
27 }
28 
29 namespace drake {
30 namespace math {
31 
32 template <typename Derived>
34  typedef typename Eigen::Matrix<typename Derived::Scalar::Scalar,
35  Derived::RowsAtCompileTime,
36  Derived::ColsAtCompileTime> type;
37 };
38 
39 template <typename Derived>
41  const Eigen::MatrixBase<Derived>& auto_diff_matrix) {
42  typename AutoDiffToValueMatrix<Derived>::type ret(auto_diff_matrix.rows(),
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();
47  }
48  }
49  return ret;
50 }
51 
52 template <typename Derived>
54  typedef typename Gradient<
55  Eigen::Matrix<typename Derived::Scalar::Scalar,
56  Derived::RowsAtCompileTime, Derived::ColsAtCompileTime>,
57  Eigen::Dynamic>::type type;
58 };
59 
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()));
69  }
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());
80  }
81 
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++) {
86  auto gradient_row =
87  gradient.row(row + col * auto_diff_matrix.rows()).transpose();
88  if (auto_diff_matrix(row, col).derivatives().size() == 0) {
89  gradient_row.setZero();
90  } else {
91  gradient_row = auto_diff_matrix(row, col).derivatives();
92  }
93  }
94  }
95  return gradient;
96 }
97 
98 } // namespace math
99 } // namespace drake
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