|
| 1 | + |
| 2 | +#if 0 |
| 3 | +namespace internal { |
| 4 | + |
| 5 | +/* ... See Eigen/src/Core/DenseStorage.h. */ |
| 6 | +class DenseStorage { |
| 7 | + public: |
| 8 | + DenseStorage() = default; |
| 9 | + explicit DenseStorage(Eigen::internal::constructor_without_unaligned_array_assert) {} |
| 10 | + DenseStorage(Eigen::Index size, Eigen::Index rows, Eigen::Index cols) { |
| 11 | + resize(size, rows, cols); |
| 12 | + } |
| 13 | + |
| 14 | + DenseStorage(const DenseStorage& other); |
| 15 | + DenseStorage& operator=(const DenseStorage& other); |
| 16 | + DenseStorage(DenseStorage&& other) noexcept; |
| 17 | + DenseStorage& operator=(DenseStorage&& other); |
| 18 | + |
| 19 | + ~DenseStorage() = default; |
| 20 | + |
| 21 | + Eigen::Index rows() const { return rows_; } |
| 22 | + Eigen::Index cols() const { return cols_; } |
| 23 | + const AutoDiffScalar* data() const { return data_.data(); } |
| 24 | + AutoDiffScalar* data() { return data_.data(); } |
| 25 | + |
| 26 | + void conservativeResize(Eigen::Index size, Eigen::Index rows, Eigen::Index cols) { |
| 27 | + DRAKE_ASSERT(size == rows * cols); |
| 28 | + data_.resize(size); |
| 29 | + rows_ = rows; |
| 30 | + cols_ = cols; |
| 31 | + } |
| 32 | + void resize(Eigen::Index size, Eigen::Index rows, Eigen::Index cols) { |
| 33 | + conservativeResize(size, rows, cols); |
| 34 | + } |
| 35 | + void swap(DenseStorage& other); |
| 36 | + |
| 37 | + private: |
| 38 | + std::vector<AutoDiffScalar> data_; |
| 39 | + Eigen::Index rows_{0}; |
| 40 | + Eigen::Index cols_{0}; |
| 41 | +}; |
| 42 | +} // namespace internal |
| 43 | +#endif |
| 44 | + |
| 45 | +#if 0 |
| 46 | +namespace Eigen { |
| 47 | + |
| 48 | +/** A specialization of DenseStorage for use by Drake's AutoDiffScalar. |
| 49 | +This is for fully-dynamic matrices. */ |
| 50 | +template <> |
| 51 | +class DenseStorage< |
| 52 | + /* T = */ drake::autodiff::AutoDiffScalar, |
| 53 | + /* Size = */ Dynamic, |
| 54 | + /* _Rows = */ Dynamic, |
| 55 | + /* _Cols = */ Dynamic, |
| 56 | + /* _Options = */ 0> |
| 57 | + : public drake::autodiff::internal::DenseStorage {}; |
| 58 | + |
| 59 | +} // namespace Eigen |
| 60 | +#endif |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +#if 0 |
| 65 | +/// Overloads copysign from <cmath>. |
| 66 | +template <typename DerType, typename T> |
| 67 | +Eigen::AutoDiffScalar<DerType> copysign(const Eigen::AutoDiffScalar<DerType>& x, |
| 68 | + const T& y) { |
| 69 | + using std::isnan; |
| 70 | + if (isnan(x)) return (y >= 0) ? NAN : -NAN; |
| 71 | + if ((x < 0 && y >= 0) || (x >= 0 && y < 0)) |
| 72 | + return -x; |
| 73 | + else |
| 74 | + return x; |
| 75 | +} |
| 76 | + |
| 77 | +/// Overloads copysign from <cmath>. |
| 78 | +template <typename DerType> |
| 79 | +double copysign(double x, const Eigen::AutoDiffScalar<DerType>& y) { |
| 80 | + using std::isnan; |
| 81 | + if (isnan(x)) return (y >= 0) ? NAN : -NAN; |
| 82 | + if ((x < 0 && y >= 0) || (x >= 0 && y < 0)) |
| 83 | + return -x; |
| 84 | + else |
| 85 | + return x; |
| 86 | +} |
| 87 | + |
| 88 | +/// Overloads pow for an AutoDiffScalar base and exponent, implementing the |
| 89 | +/// chain rule. |
| 90 | +template <typename DerTypeA, typename DerTypeB> |
| 91 | +Eigen::AutoDiffScalar< |
| 92 | + typename internal::remove_all<DerTypeA>::type::PlainObject> |
| 93 | +pow(const Eigen::AutoDiffScalar<DerTypeA>& base, |
| 94 | + const Eigen::AutoDiffScalar<DerTypeB>& exponent) { |
| 95 | + // The two AutoDiffScalars being exponentiated must have the same matrix |
| 96 | + // type. This includes, but is not limited to, the same scalar type and |
| 97 | + // the same dimension. |
| 98 | + static_assert( |
| 99 | + std::is_same_v< |
| 100 | + typename internal::remove_all<DerTypeA>::type::PlainObject, |
| 101 | + typename internal::remove_all<DerTypeB>::type::PlainObject>, |
| 102 | + "The derivative types must match."); |
| 103 | + |
| 104 | + internal::make_coherent(base.derivatives(), exponent.derivatives()); |
| 105 | + |
| 106 | + const auto& x = base.value(); |
| 107 | + const auto& xgrad = base.derivatives(); |
| 108 | + const auto& y = exponent.value(); |
| 109 | + const auto& ygrad = exponent.derivatives(); |
| 110 | + |
| 111 | + using std::pow; |
| 112 | + using std::log; |
| 113 | + const auto x_to_the_y = pow(x, y); |
| 114 | + if (ygrad.isZero(std::numeric_limits<double>::epsilon()) || |
| 115 | + ygrad.size() == 0) { |
| 116 | + // The derivative only depends on ∂(x^y)/∂x -- this prevents undefined |
| 117 | + // behavior in the corner case where ∂(x^y)/∂y is infinite when x = 0, |
| 118 | + // despite ∂y/∂v being 0. |
| 119 | + return Eigen::MakeAutoDiffScalar(x_to_the_y, y * pow(x, y - 1) * xgrad); |
| 120 | + } |
| 121 | + return Eigen::MakeAutoDiffScalar( |
| 122 | + // The value is x ^ y. |
| 123 | + x_to_the_y, |
| 124 | + // The multivariable chain rule states: |
| 125 | + // df/dv_i = (∂f/∂x * dx/dv_i) + (∂f/∂y * dy/dv_i) |
| 126 | + // ∂f/∂x is y*x^(y-1) |
| 127 | + y * pow(x, y - 1) * xgrad + |
| 128 | + // ∂f/∂y is (x^y)*ln(x) |
| 129 | + x_to_the_y * log(x) * ygrad); |
| 130 | +} |
| 131 | + |
| 132 | +#endif |
0 commit comments