summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--polymatrix/expression/to.py2
-rw-r--r--polymatrix/expressionstate.py20
2 files changed, 21 insertions, 1 deletions
diff --git a/polymatrix/expression/to.py b/polymatrix/expression/to.py
index c7f9af4..7f9bac7 100644
--- a/polymatrix/expression/to.py
+++ b/polymatrix/expression/to.py
@@ -54,7 +54,7 @@ def to_sympy(
sympy_poly_terms = []
for monomial, coeff in poly.terms():
sympy_monomial = math.prod(
- sympy.Symbol(state.get_symbol(variable.index)) ** variable.power
+ sympy.Symbol(state.get_name(variable.index)) ** variable.power
for variable in monomial)
if math.isclose(coeff, 1.):
diff --git a/polymatrix/expressionstate.py b/polymatrix/expressionstate.py
index 562f07a..c4d94ad 100644
--- a/polymatrix/expressionstate.py
+++ b/polymatrix/expressionstate.py
@@ -179,6 +179,26 @@ class ExpressionState(StateCacheMixin):
raise KeyError(f"There is no symbol named {name}")
+ def get_name(self, index: int) -> str:
+ """
+ Get the name of a variable given its index.
+
+ What is the difference between the name and the symbol? Suppose M is a
+ nonzero matrix, M is the symbol, but the matrix entry in the leftmost
+ corner has name M_1. If the symbol represent a scalar, the symbol and
+ the name concide.
+ """
+ for symbol, (start, end, _) in self.indices.items():
+ if start <= index < end:
+ # Variable is not scalar
+ if end - start > 1:
+ return f"{symbol}_{index - start}"
+
+ return symbol
+
+ raise IndexError(f"There is no variable with index {index}.")
+ raise KeyError(f"There is no symbol named {name}")
+
# -- Old API ---
@property