forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ad] Add support for comparison operations (RobotLocomotion#17636)
- Loading branch information
1 parent
39e74ce
commit 8246466
Showing
8 changed files
with
418 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "drake/common/ad/auto_diff.h" | ||
#include "drake/common/ad/test/standard_operations_test.h" | ||
|
||
namespace drake { | ||
namespace test { | ||
namespace { | ||
|
||
TEST_F(StandardOperationsTest, Abs2) { | ||
CHECK_UNARY_FUNCTION(abs2, x, y, 0.1); | ||
CHECK_UNARY_FUNCTION(abs2, x, y, -0.1); | ||
CHECK_UNARY_FUNCTION(abs2, y, x, 0.1); | ||
CHECK_UNARY_FUNCTION(abs2, y, x, -0.1); | ||
} | ||
|
||
} // namespace | ||
} // namespace test | ||
} // namespace drake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include "drake/common/ad/auto_diff.h" | ||
#include "drake/common/ad/test/standard_operations_test.h" | ||
|
||
namespace drake { | ||
namespace test { | ||
namespace { | ||
|
||
TEST_F(StandardOperationsTest, Abs) { | ||
CHECK_UNARY_FUNCTION(abs, x, y, 1.1); | ||
CHECK_UNARY_FUNCTION(abs, x, y, -1.1); | ||
CHECK_UNARY_FUNCTION(abs, y, x, 1.1); | ||
CHECK_UNARY_FUNCTION(abs, y, x, -1.1); | ||
} | ||
|
||
} // namespace | ||
} // namespace test | ||
} // namespace drake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include <sstream> | ||
|
||
#include "drake/common/ad/auto_diff.h" | ||
#include "drake/common/ad/test/standard_operations_test.h" | ||
|
||
namespace drake { | ||
namespace test { | ||
namespace { | ||
|
||
#define DRAKE_CHECK_CMP(cmp) \ | ||
EXPECT_EQ(0 cmp 1, AutoDiffDut{0} cmp AutoDiffDut{1}); \ | ||
EXPECT_EQ(1 cmp 0, AutoDiffDut{1} cmp AutoDiffDut{0}); \ | ||
EXPECT_EQ(1 cmp 1, AutoDiffDut{1} cmp AutoDiffDut{1}); \ | ||
EXPECT_EQ(0 cmp 1, AutoDiffDut{0} cmp 1); /* NOLINT */ \ | ||
EXPECT_EQ(1 cmp 0, AutoDiffDut{1} cmp 0); /* NOLINT */ \ | ||
EXPECT_EQ(1 cmp 1, AutoDiffDut{1} cmp 1); /* NOLINT */ \ | ||
EXPECT_EQ(0 cmp 1, 0 cmp AutoDiffDut{1}); \ | ||
EXPECT_EQ(1 cmp 0, 1 cmp AutoDiffDut{0}); \ | ||
EXPECT_EQ(1 cmp 1, 1 cmp AutoDiffDut{1}) | ||
|
||
TEST_F(StandardOperationsTest, CmpLt) { | ||
DRAKE_CHECK_CMP(<); // NOLINT | ||
} | ||
|
||
TEST_F(StandardOperationsTest, CmpLe) { | ||
DRAKE_CHECK_CMP(<=); | ||
} | ||
|
||
TEST_F(StandardOperationsTest, CmpGt) { | ||
DRAKE_CHECK_CMP(>); // NOLINT | ||
} | ||
|
||
TEST_F(StandardOperationsTest, CmpGe) { | ||
DRAKE_CHECK_CMP(>=); | ||
} | ||
|
||
TEST_F(StandardOperationsTest, CmpEq) { | ||
DRAKE_CHECK_CMP(==); | ||
} | ||
|
||
TEST_F(StandardOperationsTest, CmpNe) { | ||
DRAKE_CHECK_CMP(!=); | ||
} | ||
|
||
#undef DRAKE_CHECK_CMP | ||
|
||
} // namespace | ||
} // namespace test | ||
} // namespace drake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "drake/common/ad/auto_diff.h" | ||
#include "drake/common/ad/test/standard_operations_test.h" | ||
|
||
namespace drake { | ||
namespace test { | ||
namespace { | ||
|
||
TEST_F(StandardOperationsTest, MaxBothAds) { | ||
CHECK_BINARY_FUNCTION_ADS_ADS(max, x, y, 0.3); | ||
CHECK_BINARY_FUNCTION_ADS_ADS(max, x, y, -0.3); | ||
CHECK_BINARY_FUNCTION_ADS_ADS(max, y, x, 0.4); | ||
CHECK_BINARY_FUNCTION_ADS_ADS(max, y, x, -0.4); | ||
} | ||
|
||
TEST_F(StandardOperationsTest, MaxLhsAds) { | ||
CHECK_BINARY_FUNCTION_ADS_SCALAR(max, x, y, 0.3); | ||
CHECK_BINARY_FUNCTION_ADS_SCALAR(max, x, y, -0.3); | ||
CHECK_BINARY_FUNCTION_ADS_SCALAR(max, y, x, 0.4); | ||
CHECK_BINARY_FUNCTION_ADS_SCALAR(max, y, x, -0.4); | ||
} | ||
|
||
TEST_F(StandardOperationsTest, MaxRhsAds) { | ||
CHECK_BINARY_FUNCTION_SCALAR_ADS(max, x, y, 0.3); | ||
CHECK_BINARY_FUNCTION_SCALAR_ADS(max, x, y, -0.3); | ||
CHECK_BINARY_FUNCTION_SCALAR_ADS(max, y, x, 0.4); | ||
CHECK_BINARY_FUNCTION_SCALAR_ADS(max, y, x, -0.4); | ||
} | ||
|
||
TEST_F(StandardOperationsTest, TieBreakingCheckMaxBothNonEmpty) { | ||
// Given `max(v1, v2)`, our overload returns the first argument `v1` when | ||
// `v1 == v2` holds if both `v1` and `v2` have non-empty derivatives. In | ||
// Drake, we rely on this implementation-detail. This test checks if the | ||
// property holds so that we can detect a possible change in future. | ||
const AutoDiffDut v1{1.0, Vector1<double>(3.)}; | ||
const AutoDiffDut v2{1.0, Vector1<double>(2.)}; | ||
EXPECT_EQ(max(v1, v2).derivatives()[0], 3.0); // Returns v1, not v2. | ||
} | ||
|
||
TEST_F(StandardOperationsTest, TieBreakingCheckMaxOneNonEmpty) { | ||
// Given `max(v1, v2)`, our overload returns whichever argument has non-empty | ||
// derivatives in the case where only one has non-empty derivatives. In | ||
// Drake, we rely on this implementation-detail. This test checks if the | ||
// property holds so that we can detect a possible change in future. | ||
const AutoDiffDut v1{1.0}; | ||
const AutoDiffDut v2{1.0, Vector1<double>(2.)}; | ||
EXPECT_TRUE(CompareMatrices(min(v1, v2).derivatives(), | ||
Vector1<double>(2.))); // Returns v2, not v1. | ||
EXPECT_TRUE(CompareMatrices(min(v2, v1).derivatives(), | ||
Vector1<double>(2.))); // Returns v2, not v1. | ||
} | ||
} // namespace | ||
} // namespace test | ||
} // namespace drake |
Oops, something went wrong.