Drake
autodiff_test_utilities.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 namespace drake {
7 namespace automotive {
8 namespace test {
9 
10 // Helper to set the derivatives to the desired n-dimensional values, resizing
11 // its derivatives vector according to the size of `desired`.
12 inline void SetDerivatives(AutoDiffXd* variable,
13  const Eigen::VectorXd& desired) {
14  variable->derivatives() = desired;
15 }
16 
17 inline void SetDerivatives(double*, const Eigen::VectorXd&) {}
18 
19 // Helper to check that the derivatives match, to within the given tolerance,
20 // the provided expected values.
21 inline void CheckDerivatives(const AutoDiffXd& variable,
22  const Eigen::VectorXd& expected,
23  const double tol = 1e-9) {
24  if (expected.isZero() && (variable.derivatives().size() == 0)) {
25  // Zero and empty are functionally equivalent.
26  return;
27  }
28  EXPECT_TRUE(CompareMatrices(expected, variable.derivatives(), tol));
29 }
30 
31 inline void CheckDerivatives(const double&, const Eigen::VectorXd&) {}
32 
33 // Helper to check positivity of the i-th derivative of the variable.
34 inline void CheckDerivativePositivity(int i, const AutoDiffXd& variable) {
35  DRAKE_DEMAND(i >= 0);
36  DRAKE_DEMAND(i < variable.derivatives().size());
37  EXPECT_LT(0., variable.derivatives()(i));
38 }
39 
40 inline void CheckDerivativePositivity(int, const double&) {}
41 
42 // Helper to check negativity of the i-th derivative of the variable.
43 inline void CheckDerivativeNegativity(int i, const AutoDiffXd& variable) {
44  DRAKE_DEMAND(i >= 0);
45  DRAKE_DEMAND(i < variable.derivatives().size());
46  EXPECT_GT(0., variable.derivatives()(i));
47 }
48 
49 inline void CheckDerivativeNegativity(int, const double&) {}
50 
51 } // namespace test
52 } // namespace automotive
53 } // namespace drake
Eigen::AutoDiffScalar< Eigen::VectorXd > AutoDiffXd
An autodiff variable with a dynamic number of partials.
Definition: eigen_autodiff_types.h:25
void CheckDerivativeNegativity(int i, const AutoDiffXd &variable)
Definition: autodiff_test_utilities.h:43
Definition: automotive_demo.cc:88
#define DRAKE_DEMAND(condition)
Evaluates condition and iff the value is false will trigger an assertion failure with a message showi...
Definition: drake_assert.h:45
void CheckDerivatives(const AutoDiffXd &variable, const Eigen::VectorXd &expected, const double tol=1e-9)
Definition: autodiff_test_utilities.h:21
::testing::AssertionResult CompareMatrices(const Eigen::MatrixBase< DerivedA > &m1, const Eigen::MatrixBase< DerivedB > &m2, double tolerance=0.0, MatrixCompareType compare_type=MatrixCompareType::absolute)
Compares two matrices to determine whether they are equal to within a certain threshold.
Definition: eigen_matrix_compare.h:31
void CheckDerivativePositivity(int i, const AutoDiffXd &variable)
Definition: autodiff_test_utilities.h:34
void SetDerivatives(AutoDiffXd *variable, const Eigen::VectorXd &desired)
Definition: autodiff_test_utilities.h:12