Skip to content

Commit

Permalink
Add getters for if-then-else symbolic::Expression
Browse files Browse the repository at this point in the history
  • Loading branch information
soonho-tri committed Feb 13, 2018
1 parent d4cf8ab commit 85e92a0
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions common/symbolic_expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,18 @@ const string& get_uninterpreted_function_name(const Expression& e) {
return to_uninterpreted_function(e)->get_name();
}

const Formula& get_conditional_formula(const Expression& e) {
return to_if_then_else(e)->get_conditional_formula();
}

const Expression& get_then_expression(const Expression& e) {
return to_if_then_else(e)->get_then_expression();
}

const Expression& get_else_expression(const Expression& e) {
return to_if_then_else(e)->get_else_expression();
}

// NOLINTNEXTLINE(runtime/references) per C++ standard signature.
Expression& operator+=(Expression& lhs, const Variable& rhs) {
return lhs += Expression{rhs};
Expand Down
15 changes: 15 additions & 0 deletions common/symbolic_expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,21 @@ get_base_to_exponent_map_in_multiplication(const Expression& e);
*/
const std::string& get_uninterpreted_function_name(const Expression& e);

/** Returns the conditional formula in the if-then-else expression @p e.
* @pre @p e is an if-then-else expression.
*/
const Formula& get_conditional_formula(const Expression& e);

/** Returns the 'then' expression in the if-then-else expression @p e.
* @pre @p e is an if-then-else expression.
*/
const Expression& get_then_expression(const Expression& e);

/** Returns the 'else' expression in the if-then-else expression @p e.
* @pre @p e is an if-then-else expression.
*/
const Expression& get_else_expression(const Expression& e);

// Matrix<Expression> * Matrix<double> => Matrix<Expression>
template <typename MatrixL, typename MatrixR>
typename std::enable_if<
Expand Down
11 changes: 11 additions & 0 deletions common/test/symbolic_expression_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ using test::ExprEqual;
using test::ExprLess;
using test::ExprNotEqual;
using test::ExprNotLess;
using test::FormulaEqual;

template <typename T>
size_t get_std_hash(const T& item) {
Expand Down Expand Up @@ -472,6 +473,16 @@ TEST_F(SymbolicExpressionTest, GetProductsInMultiplication) {
EXPECT_PRED2(ExprEqual, products.at(z_), y_);
}

TEST_F(SymbolicExpressionTest, GetIfThenElse) {
const Formula conditional{x_ > y_};
const Expression e1{x_ + y_};
const Expression e2{x_ - y_};
const Expression e{if_then_else(conditional, e1, e2)};
EXPECT_PRED2(FormulaEqual, get_conditional_formula(e), conditional);
EXPECT_PRED2(ExprEqual, get_then_expression(e), e1);
EXPECT_PRED2(ExprEqual, get_else_expression(e), e2);
}

TEST_F(SymbolicExpressionTest, IsPolynomial) {
const vector<pair<Expression, bool>> test_vec{
{e_constant_, true}, {e_var_, true}, {e_neg_, true},
Expand Down

0 comments on commit 85e92a0

Please sign in to comment.