Skip to content

Commit

Permalink
Merge pull request RobotLocomotion#5213 from soonho-tri/pr-add-expres…
Browse files Browse the repository at this point in the history
…sion-expand

Add symbolic::Expression::Expand()
  • Loading branch information
jwnimmer-tri authored Feb 19, 2017
2 parents b8e5600 + f78f8c5 commit 57aa69f
Show file tree
Hide file tree
Showing 7 changed files with 482 additions and 1 deletion.
9 changes: 9 additions & 0 deletions drake/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,15 @@ drake_cc_googletest(
],
)

drake_cc_googletest(
name = "symbolic_expansion_test",
deps = [
":common",
":symbolic",
":symbolic_test_util",
],
)

drake_cc_googletest(
name = "symbolic_expression_test",
deps = [
Expand Down
7 changes: 6 additions & 1 deletion drake/common/symbolic_expression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,15 @@ double Expression::Evaluate(const Environment& env) const {
return ptr_->Evaluate(env);
}

Expression Expression::Expand() const {
DRAKE_ASSERT(ptr_ != nullptr);
return ptr_->Expand();
}

Expression Expression::Substitute(const Variable& var,
const Expression& e) const {
DRAKE_ASSERT(ptr_ != nullptr);
return Expression{ptr_->Substitute({{var, e}})};
return ptr_->Substitute({{var, e}});
}

Expression Expression::Substitute(const Substitution& s) const {
Expand Down
9 changes: 9 additions & 0 deletions drake/common/symbolic_expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,15 @@ class Expression {
*/
double Evaluate(const Environment& env = Environment{}) const;

/** Expands out products and positive integer powers in expression. For
* example, <tt>(x + 1) * (x - 1)</tt> is expanded to <tt>x^2 - 1</tt> and
* <tt>(x + y)^2</tt> is expanded to <tt>x^2 + 2xy + y^2</tt>. Note that
* Expand applies recursively to sub-expressions. For instance, <tt>sin(2 * (x
* + y))</tt> is expanded to <tt>sin(2x + 2y)</tt>.
* @throws std::runtime_error if NaN is detected during expansion.
*/
Expression Expand() const;

/** Returns a copy of this expression replacing all occurrences of @p var
* with @p e.
* @throws std::runtime_error if NaN is detected during substitution.
Expand Down
Loading

0 comments on commit 57aa69f

Please sign in to comment.