summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2024-04-18 10:09:51 +0200
committerNao Pross <np@0hm.ch>2024-04-18 10:09:51 +0200
commit377aa43481b0afff609167344a86fd9b05b0b1c0 (patch)
treeb8475e97febc5fb307c54fb8d49fbc043c17aae5
parentMinor changes (diff)
downloadpolymatrix-377aa43481b0afff609167344a86fd9b05b0b1c0.tar.gz
polymatrix-377aa43481b0afff609167344a86fd9b05b0b1c0.zip
Add function to construct variable from_names
-rw-r--r--polymatrix/expression/from_.py17
-rw-r--r--polymatrix/expression/mixins/variablemixin.py2
2 files changed, 17 insertions, 2 deletions
diff --git a/polymatrix/expression/from_.py b/polymatrix/expression/from_.py
index 011d5d7..c34e75d 100644
--- a/polymatrix/expression/from_.py
+++ b/polymatrix/expression/from_.py
@@ -1,9 +1,14 @@
+from typing import Iterable
+from typing_extensions import override
+
from polymatrix.expression.typing import FromDataTypes
import polymatrix.expression.init
from polymatrix.expression.expression import init_expression, Expression
from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin
+from polymatrix.expression.mixins.variablemixin import VariableMixin
+from polymatrix.expression.init import init_variable_expr
from polymatrix.statemonad.abc import StateMonad
@@ -30,3 +35,15 @@ def from_(
data=data,
),
)
+
+
+def from_names(names: str, shape: tuple[int, int] = (1,1)) -> VariableMixin | tuple[VariableMixin]:
+ """ Construct one or multiple variables from comma separated a list of names. """
+ variables = tuple(init_variable_expr(name.strip(), shape)
+ for name in names.split(","))
+
+ if len(variables) == 1:
+ return variables[0]
+
+ return variables
+
diff --git a/polymatrix/expression/mixins/variablemixin.py b/polymatrix/expression/mixins/variablemixin.py
index c2b0bc1..67ee481 100644
--- a/polymatrix/expression/mixins/variablemixin.py
+++ b/polymatrix/expression/mixins/variablemixin.py
@@ -26,8 +26,6 @@ class VariableMixin(ExpressionBaseMixin):
@override
def apply(self, state: ExpressionStateMixin) -> tuple[ExpressionStateMixin, PolyMatrixMixin]:
- """ See :py:meth:`ExpressionBaseMixin.apply`. """
-
state, indices = state.index(self)
p = PolyMatrixDict()