diff options
18 files changed, 13 insertions, 184 deletions
diff --git a/polymatrix/expression/impl/linearinmonomialsinexprimpl.py b/polymatrix/expression/impl/linearinmonomialsinexprimpl.py deleted file mode 100644 index 537f164..0000000 --- a/polymatrix/expression/impl/linearinmonomialsinexprimpl.py +++ /dev/null @@ -1,9 +0,0 @@ -import dataclass_abc -from polymatrix.expression.linearinmonomialsinexpr import LinearInMonomialsInExpr - -from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin - -@dataclass_abc.dataclass_abc(frozen=True) -class LinearInMonomialsInExprImpl(LinearInMonomialsInExpr): - underlying: ExpressionBaseMixin - variables: tuple diff --git a/polymatrix/expression/impl/traceexprimpl.py b/polymatrix/expression/impl/traceexprimpl.py deleted file mode 100644 index 19efeeb..0000000 --- a/polymatrix/expression/impl/traceexprimpl.py +++ /dev/null @@ -1,8 +0,0 @@ -import dataclass_abc -from polymatrix.expression.traceexpr import TraceExpr - -from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin - -@dataclass_abc.dataclass_abc(frozen=True) -class TraceExprImpl(TraceExpr): - underlying: ExpressionBaseMixin diff --git a/polymatrix/expression/init/initlinearinmonomialsinexpr.py b/polymatrix/expression/init/initlinearinmonomialsinexpr.py deleted file mode 100644 index 77208a7..0000000 --- a/polymatrix/expression/init/initlinearinmonomialsinexpr.py +++ /dev/null @@ -1,12 +0,0 @@ -from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin -from polymatrix.expression.impl.linearinmonomialsinexprimpl import LinearInMonomialsInExprImpl - - -def init_linear_in_monomials_in( - underlying: ExpressionBaseMixin, - variables: tuple, -): - return LinearInMonomialsInExprImpl( - underlying=underlying, - variables=variables, -) diff --git a/polymatrix/expression/init/inittraceexpr.py b/polymatrix/expression/init/inittraceexpr.py deleted file mode 100644 index 9069ee6..0000000 --- a/polymatrix/expression/init/inittraceexpr.py +++ /dev/null @@ -1,10 +0,0 @@ -from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin -from polymatrix.expression.impl.traceexprimpl import TraceExprImpl - - -def init_trace_expr( - underlying: ExpressionBaseMixin, -): - return TraceExprImpl( - underlying=underlying, -) diff --git a/polymatrix/expression/linearinmonomialsinexpr.py b/polymatrix/expression/linearinmonomialsinexpr.py deleted file mode 100644 index a78fb40..0000000 --- a/polymatrix/expression/linearinmonomialsinexpr.py +++ /dev/null @@ -1,4 +0,0 @@ -from polymatrix.expression.mixins.linearinmutipleexprmixin import LinearInMultipleExprMixin - -class LinearInMonomialsInExpr(LinearInMultipleExprMixin): - pass diff --git a/polymatrix/expression/mixins/derivativeexprmixin.py b/polymatrix/expression/mixins/derivativeexprmixin.py index 32823e5..87d5f5b 100644 --- a/polymatrix/expression/mixins/derivativeexprmixin.py +++ b/polymatrix/expression/mixins/derivativeexprmixin.py @@ -1,8 +1,5 @@ import abc -import collections -import dataclasses -import itertools import typing from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix @@ -35,11 +32,10 @@ class DerivativeExprMixin(ExpressionBaseMixin): state: ExpressionState, ) -> tuple[ExpressionState, PolyMatrix]: - state, underlying = self.underlying.apply(state=state) + state, underlying = self.underlying.apply(state=state) + state, diff_wrt_variables = get_variable_indices(state, self.variables) assert underlying.shape[1] == 1, f'{underlying.shape=}' - - state, diff_wrt_variables = get_variable_indices(state, self.variables) terms = {} diff --git a/polymatrix/expression/mixins/expressionmixin.py b/polymatrix/expression/mixins/expressionmixin.py index 43b1e41..9ea3529 100644 --- a/polymatrix/expression/mixins/expressionmixin.py +++ b/polymatrix/expression/mixins/expressionmixin.py @@ -14,7 +14,6 @@ from polymatrix.expression.init.initelemmultexpr import init_elem_mult_expr from polymatrix.expression.init.initevalexpr import init_eval_expr from polymatrix.expression.init.initfilterexpr import init_filter_expr from polymatrix.expression.init.initlinearinexpr import init_linear_in_expr -from polymatrix.expression.init.initlinearinmonomialsinexpr import init_linear_in_monomials_in from polymatrix.expression.init.initfromsympyexpr import init_from_sympy_expr from polymatrix.expression.init.initgetitemexpr import init_get_item_expr from polymatrix.expression.init.initlinearmatrixinexpr import init_linear_matrix_in_expr @@ -33,7 +32,7 @@ from polymatrix.expression.init.initsumexpr import init_sum_expr from polymatrix.expression.init.initsymmetricexpr import init_symmetric_expr from polymatrix.expression.init.inittoconstantexpr import init_to_constant_expr from polymatrix.expression.init.inittoquadraticexpr import init_to_quadratic_expr -from polymatrix.expression.init.inittraceexpr import init_trace_expr +from polymatrix.expression.init.initdiagexpr import init_diag_expr from polymatrix.expression.init.inittransposeexpr import init_transpose_expr from polymatrix.expression.init.inittruncateexpr import init_truncate_expr @@ -193,6 +192,14 @@ class ExpressionMixin( ), ) + def diag(self): + return dataclasses.replace( + self, + underlying=init_diag_expr( + underlying=self.underlying, + ), + ) + def diff( self, variables: tuple, @@ -447,14 +454,6 @@ class ExpressionMixin( ), ) - def trace(self): - return dataclasses.replace( - self, - underlying=init_trace_expr( - underlying=self.underlying, - ), - ) - def truncate(self, variables: tuple, degrees: tuple[int]): return dataclasses.replace( self, diff --git a/polymatrix/expression/mixins/filterlinearpartexprmixin.py b/polymatrix/expression/mixins/filterlinearpartexprmixin.py index 5670b79..2a83742 100644 --- a/polymatrix/expression/mixins/filterlinearpartexprmixin.py +++ b/polymatrix/expression/mixins/filterlinearpartexprmixin.py @@ -1,13 +1,11 @@ import abc import collections -from numpy import var from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin from polymatrix.polymatrix.polymatrix import PolyMatrix from polymatrix.expressionstate.expressionstate import ExpressionState -from polymatrix.expression.utils.getvariableindices import get_variable_indices # is this class needed? @@ -28,7 +26,6 @@ class FilterLinearPartExprMixin(ExpressionBaseMixin): state: ExpressionState, ) -> tuple[ExpressionState, PolyMatrix]: state, underlying = self.underlying.apply(state=state) - state, variables = self.variables.apply(state=state) def gen_variable_monomials(): diff --git a/polymatrix/expression/mixins/linearinexprmixin.py b/polymatrix/expression/mixins/linearinexprmixin.py index 7f10b1c..1300903 100644 --- a/polymatrix/expression/mixins/linearinexprmixin.py +++ b/polymatrix/expression/mixins/linearinexprmixin.py @@ -1,7 +1,6 @@ import abc import collections -from numpy import var from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin @@ -37,6 +36,7 @@ class LinearInExprMixin(ExpressionBaseMixin): self, state: ExpressionState, ) -> tuple[ExpressionState, PolyMatrix]: + state, underlying = self.underlying.apply(state=state) state, monomials = get_monomial_indices(state, self.monomials) state, variable_indices = get_variable_indices(state, self.variables) diff --git a/polymatrix/expression/mixins/linearinmutipleexprmixin.py b/polymatrix/expression/mixins/linearinmutipleexprmixin.py deleted file mode 100644 index b80f562..0000000 --- a/polymatrix/expression/mixins/linearinmutipleexprmixin.py +++ /dev/null @@ -1,73 +0,0 @@ - -import abc -import collections - -from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix -from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin -from polymatrix.polymatrix.polymatrix import PolyMatrix -from polymatrix.expressionstate.expressionstate import ExpressionState -from polymatrix.expression.utils.getvariableindices import get_variable_indices - - -class LinearInMultipleExprMixin(ExpressionBaseMixin): - @property - @abc.abstractmethod - def underlying(self) -> ExpressionBaseMixin: - ... - - @property - @abc.abstractmethod - def variables(self) -> ExpressionBaseMixin: - ... - - # overwrites abstract method of `ExpressionBaseMixin` - def apply( - self, - state: ExpressionState, - ) -> tuple[ExpressionState, PolyMatrix]: - state, underlying = self.underlying.apply(state=state) - - state, variables = self.variables.apply(state=state) - - def gen_variable_monomials(): - for _, term in variables.get_terms(): - assert len(term) == 1, f'{term} should have only a single monomial' - - for monomial in term.keys(): - yield monomial - - variable_monomials = tuple(gen_variable_monomials()) - all_variables = set(variable for monomial in variable_monomials for variable in monomial) - - terms = {} - idx_row = 0 - - for row in range(underlying.shape[0]): - for col in range(underlying.shape[1]): - - try: - underlying_terms = underlying.get_poly(row, col) - except KeyError: - continue - - x_monomial_terms = collections.defaultdict(lambda: collections.defaultdict(float)) - - for monomial, value in underlying_terms.items(): - - assert tuple(sorted(monomial)) == monomial, f'{monomial} is not sorted' - - x_monomial = tuple((var_idx for var_idx in monomial if var_idx in all_variables)) - p_monomial = tuple(var_idx for var_idx in monomial if var_idx not in all_variables) - - x_monomial_terms[x_monomial][p_monomial] += value - - for data in x_monomial_terms.values(): - terms[idx_row, 0] = dict(data) - idx_row += 1 - - poly_matrix = init_poly_matrix( - terms=terms, - shape=(idx_row, 1), - ) - - return state, poly_matrix diff --git a/polymatrix/expression/mixins/linearmonomialsexprmixin.py b/polymatrix/expression/mixins/linearmonomialsexprmixin.py index 64ba66d..f53669e 100644 --- a/polymatrix/expression/mixins/linearmonomialsexprmixin.py +++ b/polymatrix/expression/mixins/linearmonomialsexprmixin.py @@ -6,10 +6,8 @@ from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin from polymatrix.expressionstate.mixins.expressionstatemixin import ExpressionStateMixin from polymatrix.polymatrix.mixins.polymatrixmixin import PolyMatrixMixin -from polymatrix.expression.mixins.quadraticinexprmixin import QuadraticInExprMixin from polymatrix.expression.utils.getvariableindices import get_variable_indices from polymatrix.expression.utils.sortmonomials import sort_monomials -from polymatrix.expression.utils.splitmonomialindices import split_monomial_indices class LinearMonomialsExprMixin(ExpressionBaseMixin): diff --git a/polymatrix/expression/mixins/quadraticinexprmixin.py b/polymatrix/expression/mixins/quadraticinexprmixin.py index 8c1a531..ea71185 100644 --- a/polymatrix/expression/mixins/quadraticinexprmixin.py +++ b/polymatrix/expression/mixins/quadraticinexprmixin.py @@ -32,6 +32,7 @@ class QuadraticInExprMixin(ExpressionBaseMixin): self, state: ExpressionState, ) -> tuple[ExpressionState, PolyMatrix]: + state, underlying = self.underlying.apply(state=state) state, sos_monomials = get_monomial_indices(state, self.monomials) state, variable_indices = get_variable_indices(state, self.variables) diff --git a/polymatrix/expression/mixins/quadraticmonomialsexprmixin.py b/polymatrix/expression/mixins/quadraticmonomialsexprmixin.py index dbc401b..c53741b 100644 --- a/polymatrix/expression/mixins/quadraticmonomialsexprmixin.py +++ b/polymatrix/expression/mixins/quadraticmonomialsexprmixin.py @@ -6,7 +6,6 @@ from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin from polymatrix.expressionstate.mixins.expressionstatemixin import ExpressionStateMixin from polymatrix.polymatrix.mixins.polymatrixmixin import PolyMatrixMixin -from polymatrix.expression.mixins.quadraticinexprmixin import QuadraticInExprMixin from polymatrix.expression.utils.getvariableindices import get_variable_indices from polymatrix.expression.utils.splitmonomialindices import split_monomial_indices diff --git a/polymatrix/expression/mixins/subtractmonomialsexprmixin.py b/polymatrix/expression/mixins/subtractmonomialsexprmixin.py index 33dfc97..b55f412 100644 --- a/polymatrix/expression/mixins/subtractmonomialsexprmixin.py +++ b/polymatrix/expression/mixins/subtractmonomialsexprmixin.py @@ -6,9 +6,7 @@ from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin from polymatrix.expressionstate.mixins.expressionstatemixin import ExpressionStateMixin from polymatrix.polymatrix.mixins.polymatrixmixin import PolyMatrixMixin -from polymatrix.expression.mixins.quadraticinexprmixin import QuadraticInExprMixin from polymatrix.expression.utils.getmonomialindices import get_monomial_indices -from polymatrix.expression.utils.getvariableindices import get_variable_indices from polymatrix.expression.utils.sortmonomials import sort_monomials from polymatrix.expression.utils.subtractmonomialindices import subtract_monomial_indices diff --git a/polymatrix/expression/mixins/toconstantexprmixin.py b/polymatrix/expression/mixins/toconstantexprmixin.py index e585d3c..547b012 100644 --- a/polymatrix/expression/mixins/toconstantexprmixin.py +++ b/polymatrix/expression/mixins/toconstantexprmixin.py @@ -1,13 +1,11 @@ import abc import collections -from numpy import var from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin from polymatrix.polymatrix.polymatrix import PolyMatrix from polymatrix.expressionstate.expressionstate import ExpressionState -from polymatrix.expression.utils.getvariableindices import get_variable_indices class ToConstantExprMixin(ExpressionBaseMixin): diff --git a/polymatrix/expression/mixins/traceexprmixin.py b/polymatrix/expression/mixins/traceexprmixin.py deleted file mode 100644 index fa46967..0000000 --- a/polymatrix/expression/mixins/traceexprmixin.py +++ /dev/null @@ -1,35 +0,0 @@ -import abc -import dataclass_abc - -from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin -from polymatrix.expressionstate.mixins.expressionstatemixin import ExpressionStateMixin -from polymatrix.polymatrix.mixins.polymatrixmixin import PolyMatrixMixin - -class TraceExprMixin(ExpressionBaseMixin): - @property - @abc.abstractclassmethod - def underlying(self) -> ExpressionBaseMixin: - ... - - # overwrites abstract method of `ExpressionBaseMixin` - def apply( - self, - state: ExpressionStateMixin, - ) -> tuple[ExpressionStateMixin, PolyMatrixMixin]: - - state, underlying = self.underlying.apply(state) - - assert underlying.shape[0] == underlying.shape[1], f'{underlying.shape=}' - - @dataclass_abc.dataclass_abc(frozen=True) - class TracePolyMatrix(PolyMatrixMixin): - underlying: PolyMatrixMixin - shape: tuple[int, int] - - def get_poly(self, row: int, _) -> dict[tuple[int, ...], float]: - return self.underlying.get_poly(row, row) - - return state, TracePolyMatrix( - underlying=underlying, - shape=(underlying.shape[0], 1), - )
\ No newline at end of file diff --git a/polymatrix/expression/mixins/truncateexprmixin.py b/polymatrix/expression/mixins/truncateexprmixin.py index 3f23652..2421f48 100644 --- a/polymatrix/expression/mixins/truncateexprmixin.py +++ b/polymatrix/expression/mixins/truncateexprmixin.py @@ -1,7 +1,5 @@ import abc -import collections -from numpy import var from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin diff --git a/polymatrix/expression/traceexpr.py b/polymatrix/expression/traceexpr.py deleted file mode 100644 index f35bbca..0000000 --- a/polymatrix/expression/traceexpr.py +++ /dev/null @@ -1,4 +0,0 @@ -from polymatrix.expression.mixins.traceexprmixin import TraceExprMixin - -class TraceExpr(TraceExprMixin): - pass |