summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Schneeberger <michael.schneeberger@fhnw.ch>2023-05-08 13:51:22 +0200
committerMichael Schneeberger <michael.schneeberger@fhnw.ch>2023-05-08 13:51:22 +0200
commit5f8999fc197a6ff4c1708a8c40d4d67c1ca52ea0 (patch)
tree6f50ab0180f7bc13bc808b49aad2be5ff12cae9d
parentrename dataclass_abc to dataclassabc (diff)
downloadpolymatrix-5f8999fc197a6ff4c1708a8c40d4d67c1ca52ea0.tar.gz
polymatrix-5f8999fc197a6ff4c1708a8c40d4d67c1ca52ea0.zip
add paraemtrizematrixexprmixin.py
-rw-r--r--polymatrix/expression/mixins/parametrizematrixexprmixin.py62
1 files changed, 62 insertions, 0 deletions
diff --git a/polymatrix/expression/mixins/parametrizematrixexprmixin.py b/polymatrix/expression/mixins/parametrizematrixexprmixin.py
new file mode 100644
index 0000000..f59ddb7
--- /dev/null
+++ b/polymatrix/expression/mixins/parametrizematrixexprmixin.py
@@ -0,0 +1,62 @@
+
+import abc
+import dataclasses
+
+from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix
+from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin
+from polymatrix.expressionstate.mixins.expressionstatemixin import ExpressionStateMixin
+from polymatrix.polymatrix.mixins.polymatrixmixin import PolyMatrixMixin
+
+
+# rename ParametrizeSymmetricMatrixExprMixin
+class ParametrizeMatrixExprMixin(ExpressionBaseMixin):
+ @property
+ @abc.abstractclassmethod
+ def underlying(self) -> ExpressionBaseMixin:
+ ...
+
+ @property
+ @abc.abstractclassmethod
+ def name(self) -> str:
+ ...
+
+ # overwrites abstract method of `ExpressionBaseMixin`
+ def apply(
+ self,
+ state: ExpressionStateMixin,
+ ) -> tuple[ExpressionStateMixin, PolyMatrixMixin]:
+
+ # cache polymatrix to not re-parametrize at every apply call
+ if self in state.cache:
+ return state, state.cache[self]
+
+ state, underlying = self.underlying.apply(state)
+
+ assert underlying.shape[1] == 1
+
+ terms = {}
+ var_index = 0
+
+ for row in range(underlying.shape[0]):
+ for _ in range(row, underlying.shape[0]):
+
+ terms[var_index, 0] = {((state.n_param + var_index, 1),): 1.0}
+
+ var_index += 1
+
+ state = state.register(
+ key=self,
+ n_param=var_index,
+ )
+
+ poly_matrix = init_poly_matrix(
+ terms=terms,
+ shape=(var_index, 1),
+ )
+
+ state = dataclasses.replace(
+ state,
+ cache=state.cache | {self: poly_matrix},
+ )
+
+ return state, poly_matrix