diff options
author | Nao Pross <np@0hm.ch> | 2024-03-03 18:54:15 +0100 |
---|---|---|
committer | Nao Pross <np@0hm.ch> | 2024-03-03 18:54:15 +0100 |
commit | a8af7c0e3969670b10d9c68d81ca7ede05b72ff4 (patch) | |
tree | ee7521aa9405fe94222c64dc59c6cf27115e85a7 | |
parent | Update README, switch to reStructuredText (diff) | |
download | mdpoly-a8af7c0e3969670b10d9c68d81ca7ede05b72ff4.tar.gz mdpoly-a8af7c0e3969670b10d9c68d81ca7ede05b72ff4.zip |
Make State.from_index accept PolyVarIndex indices
-rw-r--r-- | README.rst | 2 | ||||
-rw-r--r-- | mdpoly/state.py | 7 |
2 files changed, 7 insertions, 2 deletions
@@ -36,7 +36,7 @@ to give an overview. for term in sparse.terms(entry): monomial_str = "" for idx in term: - var = state.from_index(idx.var_idx) + var = state.from_index(idx) monomial_str += f"{var.name}^{idx.power} " # Get the coefficient diff --git a/mdpoly/state.py b/mdpoly/state.py index f22d502..70fca24 100644 --- a/mdpoly/state.py +++ b/mdpoly/state.py @@ -2,6 +2,8 @@ from __future__ import annotations from typing import TYPE_CHECKING from dataclasses import dataclass, field +from .types import PolyVarIndex + if TYPE_CHECKING: from .types import Number from .leaves import Var, Param, Const @@ -33,10 +35,13 @@ class State: return self.variables[var] - def from_index(self, index: Index) -> Var | Const: + def from_index(self, index: Index | PolyVarIndex) -> Var | Const: """ Get a variable object from the index. This is a reverse lookup operation and it is **not** :math:`\mathcal{O}(1)`!""" + if isinstance(index, PolyVarIndex): + index = index.var_idx + if index == -1: from .leaves import Const return Const(value=1, name="1") |