From 94c893b8e4ac8ed0b16b0d1e6c032eb7d2e990e5 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 4 Mar 2024 23:36:19 +0100 Subject: Get rid of algebra.ExprWithRepr --- mdpoly/algebra.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/mdpoly/algebra.py b/mdpoly/algebra.py index a2d2325..3328d19 100644 --- a/mdpoly/algebra.py +++ b/mdpoly/algebra.py @@ -73,11 +73,7 @@ class AlgebraicStructure(Protocol): # f"objects with different shapes {cls.shape} and {other.shape}") -class ExprWithRepr(Expr, HasRepr, Protocol): - """ Expression that has a representation """ - - -class ReducibleExpr(ExprWithRepr, Protocol): +class ReducibleExpr(Expr, Protocol): """ Reducible Expression Algebraic expression that can be written in terms of other (existing) @@ -91,10 +87,11 @@ class ReducibleExpr(ExprWithRepr, Protocol): return self.reduce().to_repr(repr_type, state) @abstractmethod - def reduce(self) -> ExprWithRepr: + def reduce(self) -> HasRepr: """ Reduce the expression to its basic elements """ +# FIXME: The type checker really hates this trick, what is a better solution? def binary_operator(left_type: AlgebraicStructure, right_type: AlgebraicStructure): """ Class decorator that adds constructor for binary operations of Expr. @@ -129,6 +126,7 @@ def binary_operator(left_type: AlgebraicStructure, right_type: AlgebraicStructur return decorator +# FIXME: same as binary_operator def unary_operator(inner_type: AlgebraicStructure): """ Class decorator that adss constructor for unary operations of Expr @@ -197,7 +195,7 @@ class Sub(ReducibleExpr): raise InvalidShape(f"Cannot add shapes {self.left.shape} and {self.right.shape}.") return self.left.shape - def reduce(self) -> ExprWithRepr: + def reduce(self) -> HasRepr: """ See :py:meth:`mdpoly.algebra.ReducibleExpr.reduce`. """ # return self.left + (-1 * self.right) return Add(self.left, Mul(Const(value=-1), self.right)) @@ -252,7 +250,7 @@ class Exp(ReducibleExpr): raise InvalidShape(f"Cannot add shapes {self.left.shape} and {self.right.shape}.") return self.left.shape - def reduce(self) -> ExprWithRepr: + def reduce(self) -> HasRepr: """ See :py:meth:`mdpoly.algebra.ReducibleExpr.reduce`. """ var = self.left ntimes = self.right.value - 1 @@ -337,7 +335,7 @@ class PolyMul(Mul, PolyRingAlgebra): class PolyDiv(ReducibleExpr, PolyRingAlgebra): """ Division of scalar polynomial by scalar. """ - def reduce(self) -> ExprWithRepr: + def reduce(self) -> HasRepr: """ See :py:meth:`mdpoly.algebra.ReducibleExpr.reduce`. """ inverse = self._constant(value=1/self.right.value) return PolyMul(inverse, self.left) -- cgit v1.2.1