aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNao Pross <np@0hm.ch>2024-03-08 22:22:56 +0100
committerNao Pross <np@0hm.ch>2024-03-08 22:22:56 +0100
commitbb8029588b5234e32d15047f80c1068869e35015 (patch)
treec2a19e0db6b60256c05d94276963512f83a2b4f1
parentFix typos in docstring (diff)
downloadmdpoly-bb8029588b5234e32d15047f80c1068869e35015.tar.gz
mdpoly-bb8029588b5234e32d15047f80c1068869e35015.zip
Pass shape parameter to Repr constructor
-rw-r--r--mdpoly/abc.py4
-rw-r--r--mdpoly/algebra.py20
-rw-r--r--mdpoly/representations.py12
3 files changed, 20 insertions, 16 deletions
diff --git a/mdpoly/abc.py b/mdpoly/abc.py
index c59c3e1..34cf090 100644
--- a/mdpoly/abc.py
+++ b/mdpoly/abc.py
@@ -389,6 +389,10 @@ class Repr(ABC):
"""
@abstractmethod
+ def __init__(self, shape: Shape): ...
+
+
+ @abstractmethod
def at(self, entry: MatrixIndex, term: PolyIndex) -> Number:
""" Access polynomial coefficient. """
diff --git a/mdpoly/algebra.py b/mdpoly/algebra.py
index 81fae18..f4656d1 100644
--- a/mdpoly/algebra.py
+++ b/mdpoly/algebra.py
@@ -206,7 +206,7 @@ class PolyVar(Var, PolyRingExpr):
shape: Shape = Shape.scalar() # ovearloads PolyRingExpr.shape
def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]:
- r = repr_type()
+ r = repr_type(self.shape)
idx = PolyVarIndex.from_var(self, state), # important comma!
r.set(MatrixIndex.scalar(), PolyIndex(idx), 1)
return r, state
@@ -220,7 +220,7 @@ class PolyConst(Const, PolyRingExpr):
shape: Shape = Shape.scalar() # ovearloads PolyRingExpr.shape
def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]:
- r = repr_type()
+ r = repr_type(self.shape)
r.set(MatrixIndex.scalar(), PolyIndex.constant(), self.value)
return r, state
@@ -245,7 +245,7 @@ class PolyAdd(BinaryOp, PolyRingExpr):
def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]:
""" See :py:meth:`mdpoly.abc.Expr.to_repr`. """
# Make a new empty representation
- r = repr_type()
+ r = repr_type(self.shape)
# Make representations for existing stuff
for node in self.children():
@@ -279,7 +279,7 @@ class PolyMul(BinaryOp, PolyRingExpr):
def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]:
""" See :py:meth:`mdpoly.abc.Expr.to_repr`. """
- r = repr_type()
+ r = repr_type(self.shape)
lrepr, state = self.left.to_repr(repr_type, state)
rrepr, state = self.right.to_repr(repr_type, state)
@@ -347,7 +347,7 @@ class PolyPartialDiff(UnaryOp, PolyRingExpr):
def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]:
""" See :py:meth:`mdpoly.abc.Expr.to_repr`. """
- r = repr_type()
+ r = repr_type(self.shape)
lrepr, state = self.inner.to_repr(repr_type, state)
entry = MatrixIndex.scalar()
@@ -465,7 +465,7 @@ class MatConst(Const, MatrixExpr):
name: str = "" # overloads Leaf.name
def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]:
- r = repr_type()
+ r = repr_type(self.shape)
for r, row in enumerate(self.value):
for c, val in enumerate(row):
@@ -501,7 +501,7 @@ class MatVar(Var, MatrixExpr):
yield entry, var
def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]:
- r = repr_type()
+ r = repr_type(self.shape)
# FIXME: do not hardcode scalar type
for entry, var in self.to_scalars(PolyVar):
@@ -549,7 +549,7 @@ class MatAdd(BinaryOp, MatrixExpr):
def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]:
""" See :py:meth:`mdpoly.abc.Expr.to_repr`. """
# Make a new empty representation
- r = repr_type()
+ r = repr_type(self.shape)
# Make representations for existing stuff
for node in self.children():
@@ -600,7 +600,7 @@ class MatElemMul(BinaryOp, MatrixExpr):
def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]:
""" See :py:meth:`mdpoly.abc.Expr.to_repr`. """
- r = repr_type()
+ r = repr_type(self.shape)
lrepr, state = self.left.to_repr(repr_type, state)
rrepr, state = self.right.to_repr(repr_type, state)
@@ -640,7 +640,7 @@ class MatScalarMul(BinaryOp, MatrixExpr):
def to_repr(self, repr_type: type, state: State) -> tuple[Repr, State]:
""" See :py:meth:`mdpoly.abc.Expr.to_repr`. """
- r = repr_type()
+ r = repr_type(self.shape)
scalar_repr, state = self.left.to_repr(repr_type, state)
mat_repr, state = self.right.to_repr(repr_type, state)
diff --git a/mdpoly/representations.py b/mdpoly/representations.py
index 7b7028c..8268c42 100644
--- a/mdpoly/representations.py
+++ b/mdpoly/representations.py
@@ -12,7 +12,7 @@ class SparseRepr(Repr):
data: dict[MatrixIndex, dict[PolyIndex, Number]]
- def __init__(self):
+ def __init__(self, shape: Shape):
self.data = {}
def at(self, entry: MatrixIndex, term: PolyIndex) -> Number:
@@ -49,7 +49,7 @@ class SparseRepr(Repr):
return tuple()
-class SparseMatrixRepr(Repr):
+class ScipySparseMatrixRepr(Repr):
""" Sparse matrix representation of a polynomial.
Collect the monomials in a vector :math:`x` (the "basis") and store a
@@ -63,13 +63,13 @@ class SparseMatrixRepr(Repr):
raise NotImplementedError
-class DenseRepr(Repr):
+class NumpyDenseRepr(Repr):
""" Dense representation of a polynomial that uses Numpy arrays. """
data: npt.NDArray
- def __init__(self, shape: Shape, dtype:type =float):
- self.data = np.zeros(shape, dtype)
+ def __init__(self, shape: Shape):
+ self.data = np.zeros(shape)
-class DenseMatrixRepr(Repr):
+class NumpyDenseMatrixRepr(Repr):
""" Dense matrix representation of a polynomials """