Skip to content

Commit

Permalink
[py symbolic] SymPy conversion supports integers (#20415)
Browse files Browse the repository at this point in the history
  • Loading branch information
drewhamiltonasdf authored Nov 2, 2023
1 parent 707efc2 commit 87b0a44
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
8 changes: 4 additions & 4 deletions bindings/pydrake/symbolic/_symbolic_extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ def _reduce_mul(*args):


def to_sympy(
x: typing.Union[float, bool, Variable, Expression, Formula],
x: typing.Union[float, int, bool, Variable, Expression, Formula],
*,
memo: typing.Dict = None
) -> typing.Union[float, bool, "sympy.Expr"]:
) -> typing.Union[float, int, bool, "sympy.Expr"]:
"""Converts a pydrake object to the corresponding SymPy Expr.
Certain expressions are not supported and will raise NotImplementedError.
Expand Down Expand Up @@ -63,10 +63,10 @@ def to_sympy(


def from_sympy(
x: typing.Union[float, bool, "sympy.Expr"],
x: typing.Union[float, int, bool, "sympy.Expr"],
*,
memo: typing.Dict = None
) -> typing.Union[float, bool, Variable, Expression, Formula]:
) -> typing.Union[float, int, bool, Variable, Expression, Formula]:
"""Converts a SymPy Expr to the corresponding pydrake object.
Certain expressions are not supported and will raise NotImplementedError.
Expand Down
12 changes: 6 additions & 6 deletions bindings/pydrake/symbolic/_symbolic_sympy.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ def _var_from_sympy(sympy_var: sympy.Dummy, *, memo: Dict):


def _to_sympy(
x: Union[float, bool, Variable, Expression, Formula],
x: Union[float, int, bool, Variable, Expression, Formula],
*,
memo: Dict = None
) -> Union[float, bool, sympy.Expr]:
) -> Union[float, int, bool, sympy.Expr]:
"""This is the private implementation of pydrake.symbolic.to_sympy().
Refer to that module-level function for the full docstring.
TODO(jwnimmer-tri) Also support Polynomial, Monomial, etc.
"""
if isinstance(x, (float, bool)):
if isinstance(x, (float, int, bool)):
return x
if isinstance(x, Variable):
return _var_to_sympy(drake_var=x, memo=memo)
Expand Down Expand Up @@ -222,15 +222,15 @@ def _lambdify(*, expr, args):


def _from_sympy(
x: Union[float, bool, sympy.Expr],
x: Union[float, int, bool, sympy.Expr],
*,
memo: Dict = None
) -> Union[float, bool, Variable, Expression, Formula]:
) -> Union[float, int, bool, Variable, Expression, Formula]:
"""This is the private implementation of pydrake.symbolic.from_sympy().
Refer to that module-level function for the full docstring.
"""
# Return non-SymPy inputs as-is.
if isinstance(x, (float, bool)):
if isinstance(x, (float, int, bool)):
return x
# Return constants quickly.
if x.is_number:
Expand Down
5 changes: 3 additions & 2 deletions bindings/pydrake/symbolic/test/sympy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def test_round_trip(self):
q, r = [Variable(name, BOOLEAN) for name in "q r".split()]
inputs = [
# Constants.
1,
1.0,
np.nan,
Expression(1.0),
Expand Down Expand Up @@ -99,7 +100,7 @@ def _test_one_round_trip(self, item):
readback = mut.from_sympy(converted, memo=memo)

# When the input was a built-in type, np.testing works well.
if isinstance(item, (float, bool)):
if isinstance(item, (float, int, bool)):
np.testing.assert_equal(item, readback)
return

Expand All @@ -120,7 +121,7 @@ def _test_one_round_trip(self, item):
f"{readback_str} (from {converted_str})")

def _test_one_eval(self, item):
if isinstance(item, (float, bool, Variable)):
if isinstance(item, (float, int, bool, Variable)):
# Skip this case. Evaluation is not particularly interesting.
return

Expand Down

0 comments on commit 87b0a44

Please sign in to comment.