aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2024-03-02 21:54:23 +0100
committerNao Pross <np@0hm.ch>2024-03-02 21:54:23 +0100
commitb14c19fda34a6611c9bc1378157fc85762ab3ce5 (patch)
tree8b7935e21a7470e2fd18de0fe0d1e55d46fd285c
parentMake state.State a dataclass (diff)
downloadmdpoly-b14c19fda34a6611c9bc1378157fc85762ab3ce5.tar.gz
mdpoly-b14c19fda34a6611c9bc1378157fc85762ab3ce5.zip
Fix subtle bug in PolyIndex and improve docstrings
Diffstat (limited to '')
-rw-r--r--mdpoly/types.py20
1 files changed, 14 insertions, 6 deletions
diff --git a/mdpoly/types.py b/mdpoly/types.py
index 0a4c68e..b580b99 100644
--- a/mdpoly/types.py
+++ b/mdpoly/types.py
@@ -76,9 +76,17 @@ class PolyVarIndex(NamedTuple):
@classmethod
@property
def constant(cls):
- """ Special index for constants, which have no variable """
+ """ Special index for constants.
+
+ Constants do not have an associated variable,
+ and the field power is ignored.
+ """
return cls(var_idx=-1, power=0)
+ @staticmethod
+ def is_constant(index: Self) -> bool:
+ return index.var_idx == -1
+
@classmethod
def of_var(cls, variable: Var, state: State, power: Number =1) -> Self:
return cls(var_idx=state.index(variable), power=power)
@@ -90,17 +98,17 @@ class PolyIndex(tuple[PolyVarIndex]):
Example:
- Suppose there are two variables x, y with indices 0, 1 respectively.
- Then the monomal xy^2 has PolyIndex
+ Suppose there are two variables x, y with indices 0, 1 respectively (in
+ the State object). Then the monomal xy^2 has PolyIndex
- (PolyVarIndex(varindex=0, power=1), PolyVarIndex(varindex=1, power=2)
+ (PolyVarIndex(var_idx=0, power=1), PolyVarIndex(var_idx=1, power=2)
Then given the polynomial
p(x, y) = 3x^2 + 5xy^2
The with the PolyIndex above we can retrieve the coefficient 5 in front
- of xy^2.
+ of xy^2 from the Repr object.
"""
def __repr__(self) -> str:
@@ -112,4 +120,4 @@ class PolyIndex(tuple[PolyVarIndex]):
@property
def constant(cls):
""" Index of the constant term """
- return cls(PolyVarIndex.constant)
+ return cls((PolyVarIndex.constant,))