fix LtNode simplification when lhs and rhs contain same variables (#3451)

* fix LtNode simplification when lhs and rhs contain same variables

`(Variable("a", 1, 5) < Variable("a", 1, 5))` should eval to `NumNode(0)`

* fix with less perf impact
This commit is contained in:
chenyu
2024-02-20 09:06:55 -05:00
committed by GitHub
parent 1b6e890ef2
commit 0d326a48b8
2 changed files with 6 additions and 3 deletions

View File

@@ -413,9 +413,11 @@ class TestSymbolicSymbolicOps(unittest.TestCase):
c = Variable("c", 1, 10)
d = Variable("d", 5, 10)
# if the value is always the same, it folds to num
assert (a < b) == 1
assert (b < a) == 0
assert (d < a) == 0
assert (a < b) == NumNode(1)
assert (b < a) == NumNode(0)
assert (d < a) == NumNode(0)
assert (a < a) == NumNode(0)
assert (a > a) == NumNode(0)
# if it remains as a LtNode, bool is always true and (min, max) == (0, 1)
assert isinstance((a < c), LtNode) and (a < c).min == 0 and (a < c).max == 1
assert a < c

View File

@@ -168,6 +168,7 @@ class OpNode(Node):
class LtNode(OpNode):
def get_bounds(self) -> Tuple[int, int]:
if self.a == self.b: return (0, 0)
if isinstance(self.b, int): return (1, 1) if self.a.max < self.b else (0, 0) if self.a.min >= self.b else (0, 1)
return (1, 1) if self.a.max < self.b.min else (0, 0) if self.a.min >= self.b.max else (0, 1)
def substitute(self, var_vals: Mapping[Variable, Union[NumNode, Variable]]) -> Node: