summaryrefslogtreecommitdiffstats
path: root/polymatrix/expression/mixins/degreeexprmixin.py
diff options
context:
space:
mode:
Diffstat (limited to 'polymatrix/expression/mixins/degreeexprmixin.py')
-rw-r--r--polymatrix/expression/mixins/degreeexprmixin.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/polymatrix/expression/mixins/degreeexprmixin.py b/polymatrix/expression/mixins/degreeexprmixin.py
new file mode 100644
index 0000000..273add2
--- /dev/null
+++ b/polymatrix/expression/mixins/degreeexprmixin.py
@@ -0,0 +1,52 @@
+
+import abc
+
+from polymatrix.polymatrix.init import init_poly_matrix
+from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin
+from polymatrix.polymatrix.abc import PolyMatrix
+from polymatrix.expressionstate.abc import ExpressionState
+from polymatrix.utils.getstacklines import FrameSummary
+
+
+class DegreeExprMixin(ExpressionBaseMixin):
+ @property
+ @abc.abstractmethod
+ def underlying(self) -> ExpressionBaseMixin:
+ ...
+
+ @property
+ @abc.abstractmethod
+ def stack(self) -> tuple[FrameSummary]:
+ ...
+
+ # overwrites the abstract method of `ExpressionBaseMixin`
+ def apply(
+ self,
+ state: ExpressionState,
+ ) -> tuple[ExpressionState, PolyMatrix]:
+ state, underlying = self.underlying.apply(state=state)
+
+ terms = {}
+
+ for row in range(underlying.shape[0]):
+ for col in range(underlying.shape[1]):
+
+ underlying_terms = underlying.get_poly(row, col)
+
+ if underlying_terms is None or len(underlying_terms) == 0:
+ continue
+
+ def gen_degrees():
+ for monomial, _ in underlying_terms.items():
+ yield sum(count for _, count in monomial)
+
+ # degrees = tuple(gen_degrees())
+
+ terms[row, col] = {tuple(): max(gen_degrees())}
+
+ poly_matrix = init_poly_matrix(
+ terms=terms,
+ shape=underlying.shape,
+ )
+
+ return state, poly_matrix