Skip to content

Commit

Permalink
Only call RationalFunction::CheckIndeterminates in debug mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
hongkai-dai committed Mar 14, 2019
1 parent 77a95f4 commit 2605d86
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 39 deletions.
10 changes: 5 additions & 5 deletions common/symbolic_rational_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -117,7 +117,7 @@ RationalFunction& RationalFunction::operator/=(const RationalFunction& f) {
}
numerator_ *= f.denominator();
denominator_ *= f.numerator();
CheckIndeterminates();
DRAKE_ASSERT_VOID(CheckIndeterminates());
return *this;
}

Expand All @@ -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;
}

Expand Down
94 changes: 60 additions & 34 deletions common/test/symbolic_rational_function_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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_);

Expand All @@ -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_);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 2605d86

Please sign in to comment.