diff options
author | Nao Pross <np@0hm.ch> | 2024-06-06 00:23:04 +0200 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-06-06 00:23:04 +0200 |
commit | b5a116427d1cc11693c491897f66a03b202cc672 (patch) | |
tree | 86a9d61a51ec144992080168099bc5d2b98a20a7 | |
parent | Fix bug in from_name (diff) | |
download | polymatrix-b5a116427d1cc11693c491897f66a03b202cc672.tar.gz polymatrix-b5a116427d1cc11693c491897f66a03b202cc672.zip |
Re-add ExpressionState.get_name and fix to_sympy
Diffstat (limited to '')
-rw-r--r-- | polymatrix/expression/to.py | 2 | ||||
-rw-r--r-- | polymatrix/expressionstate.py | 20 |
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 |