summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--polymatrix/polymatrix/index.py29
-rw-r--r--polymatrix/polymatrix/utils/mergemonomialindices.py2
-rw-r--r--polymatrix/polymatrix/utils/multiplypolynomial.py2
3 files changed, 32 insertions, 1 deletions
diff --git a/polymatrix/polymatrix/index.py b/polymatrix/polymatrix/index.py
index 2a46625..7fe3620 100644
--- a/polymatrix/polymatrix/index.py
+++ b/polymatrix/polymatrix/index.py
@@ -1,8 +1,10 @@
from __future__ import annotations
+import math
+
from typing import NamedTuple, Iterable, cast
from collections import UserDict
-from itertools import dropwhile, combinations_with_replacement
+from itertools import product, dropwhile, combinations_with_replacement
from functools import reduce
# TODO: remove these types, they are here for backward compatiblity
@@ -72,6 +74,8 @@ class MonomialIndex(tuple[VariableIndex]):
return self.degree < other.degree
+ # --- Helper / util static methods ---
+
@staticmethod
def empty() -> MonomialIndex:
""" Get an empty monomial index. """
@@ -194,6 +198,29 @@ class PolyDict(UserDict[MonomialIndex, int | float]):
# This is an alias for readability
yield from self.values()
+ # --- Helper / util static methods ---
+
+ @staticmethod
+ def product(left: PolyDict, right: PolyDict) -> PolyDict:
+ """ Compute the product of two polynomials """
+ # product of left and right
+ p = PolyDict.empty()
+
+ for (lm, lv), (rm, rv) in product(left.items(), right.items()):
+ pv = lv + rv
+
+ if math.isclose(pv, 0):
+ continue
+
+ pm = MonomialIndex.product(lm, rm)
+ if pm not in p:
+ p[pm] = pv
+
+ elif not math.isclose(p[pm] + pv, 0):
+ p[pm] += pv
+
+ return p
+
class MatrixIndex(NamedTuple):
""" Index to represent an entry of a matrix. """
diff --git a/polymatrix/polymatrix/utils/mergemonomialindices.py b/polymatrix/polymatrix/utils/mergemonomialindices.py
index 6680ebd..c84a59c 100644
--- a/polymatrix/polymatrix/utils/mergemonomialindices.py
+++ b/polymatrix/polymatrix/utils/mergemonomialindices.py
@@ -1,9 +1,11 @@
from polymatrix.polymatrix.index import MonomialIndex
from polymatrix.polymatrix.utils.sortmonomialindices import sort_monomial_indices
+from polymatrix.utils.deprecation import deprecated
# NP: this is a werid case of the product
# NP: merge is not intutive IMHO, find better name
+@deprecated("Replaced by MonomialIndex.product")
def merge_monomial_indices(
monomials: tuple[MonomialIndex, ...],
) -> MonomialIndex:
diff --git a/polymatrix/polymatrix/utils/multiplypolynomial.py b/polymatrix/polymatrix/utils/multiplypolynomial.py
index 712b34e..0199b98 100644
--- a/polymatrix/polymatrix/utils/multiplypolynomial.py
+++ b/polymatrix/polymatrix/utils/multiplypolynomial.py
@@ -3,9 +3,11 @@ import math
from polymatrix.polymatrix.utils.mergemonomialindices import merge_monomial_indices
from polymatrix.polymatrix.index import PolyDict
+from polymatrix.utils.deprecation import deprecated
# NP: compute index of product of polynomials
+@deprecated("Replaced by PolyDict.product")
def multiply_polynomial(
left: PolyDict,
right: PolyDict,