Skip to content

Commit a348691

Browse files
committed
Simplify division of expressions
1 parent 717ebb9 commit a348691

File tree

1 file changed

+4
-16
lines changed

1 file changed

+4
-16
lines changed

src/y0/dsl.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,9 @@ class Expression(Element, ABC):
555555
def __mul__(self, other):
556556
pass
557557

558-
@abstractmethod
559-
def __truediv__(self, other):
560-
pass
558+
def __truediv__(self, expression: Expression) -> Fraction:
559+
"""Divide this expression by another and create a fraction."""
560+
return Fraction(self, expression)
561561

562562
def marginalize(self, ranges: VariableHint) -> Fraction:
563563
"""Return this expression, marginalized by the given variables.
@@ -638,9 +638,6 @@ def __mul__(self, other: Expression) -> Expression:
638638
else:
639639
return Product((self, other))
640640

641-
def __truediv__(self, expression: Expression) -> Fraction:
642-
return Fraction(self, expression)
643-
644641
def intervene(self, variables: VariableHint) -> Probability:
645642
"""Return a new probability where the underlying distribution has been intervened by the given variables."""
646643
return Probability(self.distribution.intervene(variables))
@@ -846,9 +843,6 @@ def __mul__(self, other: Expression):
846843
else:
847844
return Product((*self.expressions, other))
848845

849-
def __truediv__(self, expression: Expression) -> Fraction:
850-
return Fraction(self, expression)
851-
852846
def _iter_variables(self) -> Iterable[Variable]:
853847
"""Get the union of the variables used in each expresison in this product."""
854848
for expression in self.expressions:
@@ -935,9 +929,6 @@ def __mul__(self, expression: Expression):
935929
else:
936930
return Product((self, expression))
937931

938-
def __truediv__(self, expression: Expression) -> Fraction:
939-
return Fraction(self, expression)
940-
941932
def _iter_variables(self) -> Iterable[Variable]:
942933
"""Get the union of the variables used in the range of this sum and variables in its summand."""
943934
yield from self.expression._iter_variables()
@@ -1100,9 +1091,6 @@ def __rmul__(self, expression: Expression) -> Expression:
11001091
def __mul__(self, expression: Expression) -> Expression:
11011092
return expression
11021093

1103-
def __truediv__(self, other: Expression) -> Fraction:
1104-
return Fraction(self, other)
1105-
11061094
def __eq__(self, other):
11071095
return isinstance(other, One) # all ones are equal
11081096

@@ -1199,7 +1187,7 @@ def __truediv__(self, expression: Expression) -> Fraction:
11991187
if isinstance(expression, Fraction):
12001188
return Fraction(self * expression.denominator, expression.numerator)
12011189
else:
1202-
return Fraction(self, expression)
1190+
return super().__truediv__(expression)
12031191

12041192
def _iter_variables(self) -> Iterable[Variable]:
12051193
yield from self.codomain

0 commit comments

Comments
 (0)