diff --git a/common/symbolic_rational_function.cc b/common/symbolic_rational_function.cc index 1ba0c6456ddf..9c83c1fef72c 100644 --- a/common/symbolic_rational_function.cc +++ b/common/symbolic_rational_function.cc @@ -14,7 +14,7 @@ RationalFunction::RationalFunction(const Polynomial& numerator, throw std::invalid_argument( "RationalFunction: the denominator should not be 0."); } - CheckIndeterminates(); + DRAKE_ASSERT_VOID(CheckIndeterminates()); } RationalFunction::RationalFunction(const Polynomial& p) @@ -96,13 +96,13 @@ RationalFunction& RationalFunction::operator-=(double c) { return *this += -c; } RationalFunction& RationalFunction::operator*=(const RationalFunction& f) { numerator_ *= f.numerator(); denominator_ *= f.denominator(); - CheckIndeterminates(); + DRAKE_ASSERT_VOID(CheckIndeterminates()); return *this; } RationalFunction& RationalFunction::operator*=(const Polynomial& p) { numerator_ *= p; - CheckIndeterminates(); + DRAKE_ASSERT_VOID(CheckIndeterminates()); return *this; } @@ -117,7 +117,7 @@ RationalFunction& RationalFunction::operator/=(const RationalFunction& f) { } numerator_ *= f.denominator(); denominator_ *= f.numerator(); - CheckIndeterminates(); + DRAKE_ASSERT_VOID(CheckIndeterminates()); return *this; } @@ -126,7 +126,7 @@ RationalFunction& RationalFunction::operator/=(const Polynomial& p) { throw std::logic_error("RationalFunction: operator/=: The divider is 0."); } denominator_ *= p; - CheckIndeterminates(); + DRAKE_ASSERT_VOID(CheckIndeterminates()); return *this; } diff --git a/common/test/symbolic_rational_function_test.cc b/common/test/symbolic_rational_function_test.cc index 1907816647a8..91671233b815 100644 --- a/common/test/symbolic_rational_function_test.cc +++ b/common/test/symbolic_rational_function_test.cc @@ -83,22 +83,28 @@ TEST_F(SymbolicRationalFunctionTest, ConstructorWithError) { // numerator. const Polynomial p1(var_x_ * var_a_, {var_x_}); const Polynomial p2(var_x_ * var_b_ + var_y_, {var_b_, var_y_}); - DRAKE_EXPECT_THROWS_MESSAGE( - RationalFunction(p2, p1), std::logic_error, - "[^]* are used as decision variables in the numerator [^]*"); + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE( + RationalFunction(p2, p1), std::logic_error, + "[^]* are used as decision variables in the numerator [^]*"); + } // The indeterminate in the numerator is a decision variable in the // denominator. const symbolic::Polynomial p3(var_x_ * var_y_, {var_y_}); - DRAKE_EXPECT_THROWS_MESSAGE( - RationalFunction(p1, p3), std::logic_error, - "[^]* are used as indeterminates in the numerator [^]*"); - - DRAKE_EXPECT_THROWS_MESSAGE( - RationalFunction(Polynomial(var_a_ * var_x_, {var_x_}), - Polynomial(var_a_ * var_x_, {var_a_})), - std::logic_error, - "[^]* are used as indeterminates in the numerator [^]* are used as " - "decision variables in the numerator [^]*"); + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE( + RationalFunction(p1, p3), std::logic_error, + "[^]* are used as indeterminates in the numerator [^]*"); + } + + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE( + RationalFunction(Polynomial(var_a_ * var_x_, {var_x_}), + Polynomial(var_a_ * var_x_, {var_a_})), + std::logic_error, + "[^]* are used as indeterminates in the numerator [^]* are used as " + "decision variables in the numerator [^]*"); + } } TEST_F(SymbolicRationalFunctionTest, EqualTo) { @@ -245,21 +251,29 @@ TEST_F(SymbolicRationalFunctionTest, Product) { const RationalFunction f3(p1_, p3_); DRAKE_EXPECT_THROWS_MESSAGE(f3 * RationalFunction(p5_, p6_), std::runtime_error, polynomial_invariant_error_); - DRAKE_EXPECT_THROWS_MESSAGE(f3 * RationalFunction(p5_, p2_), std::logic_error, - rational_function_indeterminates_error_); + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE(f3 * RationalFunction(p5_, p2_), + std::logic_error, + rational_function_indeterminates_error_); + } DRAKE_EXPECT_THROWS_MESSAGE(f3 * RationalFunction(p2_, p5_), std::runtime_error, polynomial_invariant_error_); DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p5_, p6_) * f3, std::runtime_error, polynomial_invariant_error_); - DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p5_, p2_) * f3, std::logic_error, - rational_function_indeterminates_error_); + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p5_, p2_) * f3, + std::logic_error, + rational_function_indeterminates_error_); + } DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p2_, p5_) * f3, std::runtime_error, polynomial_invariant_error_); DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p5_, p6_) *= f3, std::runtime_error, polynomial_invariant_error_); - DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p5_, p2_) *= f3, - std::logic_error, - rational_function_indeterminates_error_); + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p5_, p2_) *= f3, + std::logic_error, + rational_function_indeterminates_error_); + } DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p2_, p5_) *= f3, std::runtime_error, polynomial_invariant_error_); @@ -270,10 +284,12 @@ TEST_F(SymbolicRationalFunctionTest, Product) { f1_times_p3 *= p3_; EXPECT_PRED2(RationalFunctionEqual, f1_times_p3, f1_times_p3_expected); // p5 contains variable a in its indeterminates. - DRAKE_EXPECT_THROWS_MESSAGE(f3 * p5_, std::logic_error, - rational_function_indeterminates_error_); - DRAKE_EXPECT_THROWS_MESSAGE(p5_ * f3, std::logic_error, - rational_function_indeterminates_error_); + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE(f3 * p5_, std::logic_error, + rational_function_indeterminates_error_); + DRAKE_EXPECT_THROWS_MESSAGE(p5_ * f3, std::logic_error, + rational_function_indeterminates_error_); + } const double c = 2; const RationalFunction f1_times_c_expected(p1_ * c, p2_); @@ -299,21 +315,29 @@ TEST_F(SymbolicRationalFunctionTest, Division) { std::runtime_error, polynomial_invariant_error_); DRAKE_EXPECT_THROWS_MESSAGE(f3 / RationalFunction(p5_, p2_), std::runtime_error, polynomial_invariant_error_); - DRAKE_EXPECT_THROWS_MESSAGE(f3 / RationalFunction(p2_, p5_), std::logic_error, - rational_function_indeterminates_error_); + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE(f3 / RationalFunction(p2_, p5_), + std::logic_error, + rational_function_indeterminates_error_); + } DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p5_, p6_) / f3, std::runtime_error, polynomial_invariant_error_); DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p5_, p2_) / f3, std::runtime_error, polynomial_invariant_error_); - DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p2_, p5_) / f3, std::logic_error, - rational_function_indeterminates_error_); + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p2_, p5_) / f3, + std::logic_error, + rational_function_indeterminates_error_); + } DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p5_, p6_) /= f3, std::runtime_error, polynomial_invariant_error_); DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p5_, p2_) /= f3, std::runtime_error, polynomial_invariant_error_); - DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p2_, p5_) /= f3, - std::logic_error, - rational_function_indeterminates_error_); + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p2_, p5_) /= f3, + std::logic_error, + rational_function_indeterminates_error_); + } const RationalFunction p3_divides_f1_expected(p1_, p2_ * p3_); EXPECT_PRED2(RationalFunctionEqual, f1 / p3_, p3_divides_f1_expected); @@ -325,9 +349,11 @@ TEST_F(SymbolicRationalFunctionTest, Division) { polynomial_invariant_error_); DRAKE_EXPECT_THROWS_MESSAGE(p5_ / f3, std::runtime_error, polynomial_invariant_error_); - DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p3_, p1_) / p5_, - std::logic_error, - rational_function_indeterminates_error_); + if (kDrakeAssertIsArmed) { + DRAKE_EXPECT_THROWS_MESSAGE(RationalFunction(p3_, p1_) / p5_, + std::logic_error, + rational_function_indeterminates_error_); + } const double c = 2; const RationalFunction c_divides_f1_expected(p1_, p2_ * c);