From e975884b84ccb5fd3e401351116924b891b160e9 Mon Sep 17 00:00:00 2001 From: Michael Schneeberger Date: Tue, 20 Feb 2024 10:00:55 +0100 Subject: rename initexpressionbase.py to init.py --- polymatrix/expression/__init__.py | 8 +- polymatrix/expression/expression.py | 78 +-- polymatrix/expression/from_.py | 6 +- polymatrix/expression/impl.py | 355 ++++++++++++ polymatrix/expression/implexpressionbase.py | 352 ------------ polymatrix/expression/init.py | 604 +++++++++++++++++++++ polymatrix/expression/initexpressionbase.py | 604 --------------------- polymatrix/expression/mixins/fromtupleexprmixin.py | 2 - .../expression/mixins/parametrizeexprmixin.py | 44 +- polymatrix/expression/mixins/productexprmixin.py | 1 + polymatrix/expression/op.py | 12 +- .../expressionstate/mixins/expressionstatemixin.py | 5 +- 12 files changed, 1044 insertions(+), 1027 deletions(-) create mode 100644 polymatrix/expression/impl.py delete mode 100644 polymatrix/expression/implexpressionbase.py create mode 100644 polymatrix/expression/init.py delete mode 100644 polymatrix/expression/initexpressionbase.py diff --git a/polymatrix/expression/__init__.py b/polymatrix/expression/__init__.py index 75704ff..97a45cd 100644 --- a/polymatrix/expression/__init__.py +++ b/polymatrix/expression/__init__.py @@ -1,5 +1,5 @@ import polymatrix.expression.from_ -import polymatrix.expression.implexpressionbase +import polymatrix.expression.impl from collections.abc import Iterable @@ -19,7 +19,7 @@ def v_stack( yield polymatrix.expression.from_.from_(expr) return init_expression( - underlying=polymatrix.expression.implexpressionbase.VStackExprImpl( + underlying=polymatrix.expression.impl.VStackExprImpl( underlying=tuple(gen_underlying()), ), ) @@ -33,7 +33,7 @@ def block_diag( expressions: tuple[Expression], ) -> Expression: return init_expression( - underlying=polymatrix.expression.implexpressionbase.BlockDiagExprImpl( + underlying=polymatrix.expression.impl.BlockDiagExprImpl( underlying=expressions, ) ) @@ -43,7 +43,7 @@ def product( degrees: tuple[int, ...] = None, ): return init_expression( - underlying=polymatrix.expression.implexpressionbase.ProductExprImpl( + underlying=polymatrix.expression.impl.ProductExprImpl( underlying=tuple(expressions), degrees=degrees, stack=get_stack_lines(), diff --git a/polymatrix/expression/expression.py b/polymatrix/expression/expression.py index ca7d474..22349c2 100644 --- a/polymatrix/expression/expression.py +++ b/polymatrix/expression/expression.py @@ -4,7 +4,7 @@ import dataclassabc import typing import numpy as np -import polymatrix.expression.initexpressionbase +import polymatrix.expression.init from polymatrix.utils.getstacklines import get_stack_lines from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin @@ -29,7 +29,7 @@ class Expression( return self.underlying.apply(state) def __add__(self, other: ExpressionBaseMixin) -> 'Expression': - return self._binary(polymatrix.expression.initexpressionbase.init_addition_expr, self, other) + return self._binary(polymatrix.expression.init.init_addition_expr, self, other) def __getattr__(self, name): attr = getattr(self.underlying, name) @@ -46,17 +46,17 @@ class Expression( def __getitem__(self, key: tuple[int, int]): return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_get_item_expr( + underlying=polymatrix.expression.init.init_get_item_expr( underlying=self.underlying, index=key, ), ) def __matmul__(self, other: typing.Union[ExpressionBaseMixin, np.ndarray]) -> 'Expression': - return self._binary(polymatrix.expression.initexpressionbase.init_matrix_mult_expr, self, other) + return self._binary(polymatrix.expression.init.init_matrix_mult_expr, self, other) def __mul__(self, other) -> 'Expression': - return self._binary(polymatrix.expression.initexpressionbase.init_elem_mult_expr, self, other) + return self._binary(polymatrix.expression.init.init_elem_mult_expr, self, other) def __pow__(self, num): curr = 1 @@ -70,10 +70,10 @@ class Expression( return self * (-1) def __radd__(self, other): - return self._binary(polymatrix.expression.initexpressionbase.init_addition_expr, other, self) + return self._binary(polymatrix.expression.init.init_addition_expr, other, self) def __rmatmul__(self, other): - return self._binary(polymatrix.expression.initexpressionbase.init_matrix_mult_expr, other, self) + return self._binary(polymatrix.expression.init.init_matrix_mult_expr, other, self) def __rmul__(self, other): return self * other @@ -85,7 +85,7 @@ class Expression( return self + (-other) def __truediv__(self, other: ExpressionBaseMixin): - return self._binary(polymatrix.expression.initexpressionbase.init_division_expr, self, other) + return self._binary(polymatrix.expression.init.init_division_expr, self, other) @abc.abstractmethod def copy(self, underlying: ExpressionBaseMixin) -> 'Expression': @@ -97,7 +97,7 @@ class Expression( stack = get_stack_lines() if isinstance(left, Expression): - right = polymatrix.expression.initexpressionbase.init_from_expr_or_none(right) + right = polymatrix.expression.init.init_from_expr_or_none(right) # delegate to upper level if right is None: @@ -108,7 +108,7 @@ class Expression( ) else: - left = polymatrix.expression.initexpressionbase.init_from_expr_or_none(left) + left = polymatrix.expression.init.init_from_expr_or_none(left) # delegate to upper level if left is None: @@ -128,7 +128,7 @@ class Expression( def cache(self) -> 'Expression': return self.copy( - underlying=polymatrix.expression.initexpressionbase.init_cache_expr( + underlying=polymatrix.expression.init.init_cache_expr( underlying=self.underlying, ), ) @@ -138,7 +138,7 @@ class Expression( degrees: tuple[int, ...] | int, ): return self.copy( - underlying=polymatrix.expression.initexpressionbase.init_combinations_expr( + underlying=polymatrix.expression.init.init_combinations_expr( expression=self.underlying, degrees=degrees, ), @@ -147,7 +147,7 @@ class Expression( def determinant(self) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_determinant_expr( + underlying=polymatrix.expression.init.init_determinant_expr( underlying=self.underlying, ), ) @@ -155,7 +155,7 @@ class Expression( def diag(self): return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_diag_expr( + underlying=polymatrix.expression.init.init_diag_expr( underlying=self.underlying, ), ) @@ -180,7 +180,7 @@ class Expression( ) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_divergence_expr( + underlying=polymatrix.expression.init.init_divergence_expr( underlying=self.underlying, variables=variables, ), @@ -193,7 +193,7 @@ class Expression( ) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_eval_expr( + underlying=polymatrix.expression.init.init_eval_expr( underlying=self.underlying, variables=variable, values=value, @@ -208,7 +208,7 @@ class Expression( ) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_filter_expr( + underlying=polymatrix.expression.init.init_filter_expr( underlying=self.underlying, predicator=predicator, inverse=inverse, @@ -219,7 +219,7 @@ class Expression( def from_symmetric_matrix(self) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_from_symmetric_matrix_expr( + underlying=polymatrix.expression.init.init_from_symmetric_matrix_expr( underlying=self.underlying, ), ) @@ -232,7 +232,7 @@ class Expression( ) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_half_newton_polytope_expr( + underlying=polymatrix.expression.init.init_half_newton_polytope_expr( monomials=self.underlying, variables=variables, filter=filter, @@ -242,7 +242,7 @@ class Expression( def linear_matrix_in(self, variable: 'Expression') -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_linear_matrix_in_expr( + underlying=polymatrix.expression.init.init_linear_matrix_in_expr( underlying=self.underlying, variable=variable, ), @@ -293,7 +293,7 @@ class Expression( def max(self) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_max_expr( + underlying=polymatrix.expression.init.init_max_expr( underlying=self.underlying, ), ) @@ -301,7 +301,7 @@ class Expression( def max_degree(self) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_max_degree_expr( + underlying=polymatrix.expression.init.init_max_degree_expr( underlying=self.underlying, ), ) @@ -309,7 +309,7 @@ class Expression( def parametrize(self, name: str = None) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_parametrize_expr( + underlying=polymatrix.expression.init.init_parametrize_expr( underlying=self.underlying, name=name, ), @@ -332,7 +332,7 @@ class Expression( return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_quadratic_in_expr( + underlying=polymatrix.expression.init.init_quadratic_in_expr( underlying=self.underlying, monomials=monomials, variables=variables, @@ -346,7 +346,7 @@ class Expression( ) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_quadratic_monomials_expr( + underlying=polymatrix.expression.init.init_quadratic_monomials_expr( underlying=self.underlying, variables=variables, ), @@ -355,7 +355,7 @@ class Expression( def reshape(self, n: int, m: int) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_reshape_expr( + underlying=polymatrix.expression.init.init_reshape_expr( underlying=self.underlying, new_shape=(n, m), ), @@ -364,7 +364,7 @@ class Expression( def rep_mat(self, n: int, m: int) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_rep_mat_expr( + underlying=polymatrix.expression.init.init_rep_mat_expr( underlying=self.underlying, repetition=(n, m), ), @@ -379,11 +379,11 @@ class Expression( if isinstance(value, Expression): value = value.underlying else: - value = polymatrix.expression.initexpressionbase.init_from_expr(value) + value = polymatrix.expression.init.init_from_expr(value) return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_set_element_at_expr( + underlying=polymatrix.expression.init.init_set_element_at_expr( underlying=self.underlying, index=(row, col), value=value, @@ -396,7 +396,7 @@ class Expression( ) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_squeeze_expr( + underlying=polymatrix.expression.init.init_squeeze_expr( underlying=self.underlying, ), ) @@ -408,7 +408,7 @@ class Expression( ) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_subtract_monomials_expr( + underlying=polymatrix.expression.init.init_subtract_monomials_expr( underlying=self.underlying, monomials=monomials, ), @@ -421,7 +421,7 @@ class Expression( ) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_substitute_expr( + underlying=polymatrix.expression.init.init_substitute_expr( underlying=self.underlying, variables=variable, values=values, @@ -441,7 +441,7 @@ class Expression( def sum(self): return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_sum_expr( + underlying=polymatrix.expression.init.init_sum_expr( underlying=self.underlying, ), ) @@ -449,7 +449,7 @@ class Expression( def symmetric(self): return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_symmetric_expr( + underlying=polymatrix.expression.init.init_symmetric_expr( underlying=self.underlying, ), ) @@ -457,7 +457,7 @@ class Expression( def transpose(self) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_transpose_expr( + underlying=polymatrix.expression.init.init_transpose_expr( underlying=self.underlying, ), ) @@ -469,7 +469,7 @@ class Expression( def to_constant(self) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_to_constant_expr( + underlying=polymatrix.expression.init.init_to_constant_expr( underlying=self.underlying, ), ) @@ -485,7 +485,7 @@ class Expression( def to_symmetric_matrix(self) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_to_symmetric_matrix_expr( + underlying=polymatrix.expression.init.init_to_symmetric_matrix_expr( underlying=self.underlying, ), ) @@ -494,7 +494,7 @@ class Expression( def to_sorted_variables(self) -> 'Expression': return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_to_sorted_variables( + underlying=polymatrix.expression.init.init_to_sorted_variables( underlying=self.underlying, ), ) @@ -508,7 +508,7 @@ class Expression( ): return dataclasses.replace( self, - underlying=polymatrix.expression.initexpressionbase.init_truncate_expr( + underlying=polymatrix.expression.init.init_truncate_expr( underlying=self.underlying, variables=variables, degrees=degrees, diff --git a/polymatrix/expression/from_.py b/polymatrix/expression/from_.py index 637c9d1..c290a67 100644 --- a/polymatrix/expression/from_.py +++ b/polymatrix/expression/from_.py @@ -1,7 +1,7 @@ import numpy as np import sympy -import polymatrix.expression.initexpressionbase +import polymatrix.expression.init from polymatrix.expression.expression import init_expression, Expression from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin @@ -14,7 +14,7 @@ def from_expr_or_none( ) -> Expression | None: return init_expression( - underlying=polymatrix.expression.initexpressionbase.init_from_expr_or_none( + underlying=polymatrix.expression.init.init_from_expr_or_none( data=data, ), ) @@ -24,7 +24,7 @@ def from_( ) -> Expression: return init_expression( - underlying=polymatrix.expression.initexpressionbase.init_from_expr( + underlying=polymatrix.expression.init.init_from_expr( data=data, ), ) diff --git a/polymatrix/expression/impl.py b/polymatrix/expression/impl.py new file mode 100644 index 0000000..3e61941 --- /dev/null +++ b/polymatrix/expression/impl.py @@ -0,0 +1,355 @@ +from polymatrix.expression.mixins.legendreseriesmixin import LegendreSeriesMixin +from polymatrix.expression.mixins.productexprmixin import ProductExprMixin +from polymatrix.utils.getstacklines import FrameSummary +import dataclassabc + +from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin +from polymatrix.expression.mixins.additionexprmixin import AdditionExprMixin +from polymatrix.expression.mixins.blockdiagexprmixin import BlockDiagExprMixin +from polymatrix.expression.mixins.cacheexprmixin import CacheExprMixin +from polymatrix.expression.mixins.combinationsexprmixin import \ + CombinationsExprMixin +from polymatrix.expression.mixins.derivativeexprmixin import \ + DerivativeExprMixin +from polymatrix.expression.mixins.determinantexprmixin import \ + DeterminantExprMixin +from polymatrix.expression.mixins.diagexprmixin import DiagExprMixin +from polymatrix.expression.mixins.divergenceexprmixin import \ + DivergenceExprMixin +from polymatrix.expression.mixins.divisionexprmixin import DivisionExprMixin +from polymatrix.expression.mixins.elemmultexprmixin import ElemMultExprMixin +from polymatrix.expression.mixins.evalexprmixin import EvalExprMixin +from polymatrix.expression.mixins.eyeexprmixin import EyeExprMixin +from polymatrix.expression.mixins.filterexprmixin import FilterExprMixin +from polymatrix.expression.mixins.fromsymmetricmatrixexprmixin import \ + FromSymmetricMatrixExprMixin +from polymatrix.expression.mixins.fromtupleexprmixin import FromTupleExprMixin +from polymatrix.expression.mixins.fromtermsexprmixin import FromTermsExprMixin +from polymatrix.expression.mixins.getitemexprmixin import GetItemExprMixin +from polymatrix.expression.mixins.halfnewtonpolytopeexprmixin import \ + HalfNewtonPolytopeExprMixin +from polymatrix.expression.mixins.linearinexprmixin import LinearInExprMixin +from polymatrix.expression.mixins.linearmatrixinexprmixin import \ + LinearMatrixInExprMixin +from polymatrix.expression.mixins.linearmonomialsexprmixin import \ + LinearMonomialsExprMixin +from polymatrix.expression.mixins.matrixmultexprmixin import \ + MatrixMultExprMixin +from polymatrix.expression.mixins.maxdegreeexprmixin import MaxDegreeExprMixin +from polymatrix.expression.mixins.maxexprmixin import MaxExprMixin +from polymatrix.expression.mixins.parametrizeexprmixin import \ + ParametrizeExprMixin +from polymatrix.expression.mixins.parametrizematrixexprmixin import \ + ParametrizeMatrixExprMixin +from polymatrix.expression.mixins.quadraticinexprmixin import \ + QuadraticInExprMixin +from polymatrix.expression.mixins.quadraticmonomialsexprmixin import \ + QuadraticMonomialsExprMixin +from polymatrix.expression.mixins.repmatexprmixin import RepMatExprMixin +from polymatrix.expression.mixins.reshapeexprmixin import ReshapeExprMixin +from polymatrix.expression.mixins.setelementatexprmixin import \ + SetElementAtExprMixin +from polymatrix.expression.mixins.squeezeexprmixin import SqueezeExprMixin +from polymatrix.expression.mixins.substituteexprmixin import \ + SubstituteExprMixin +from polymatrix.expression.mixins.subtractmonomialsexprmixin import \ + SubtractMonomialsExprMixin +from polymatrix.expression.mixins.sumexprmixin import SumExprMixin +from polymatrix.expression.mixins.symmetricexprmixin import SymmetricExprMixin +from polymatrix.expression.mixins.toconstantexprmixin import \ + ToConstantExprMixin +from polymatrix.expression.mixins.toquadraticexprmixin import \ + ToQuadraticExprMixin +from polymatrix.expression.mixins.tosymmetricmatrixexprmixin import \ + ToSymmetricMatrixExprMixin +from polymatrix.expression.mixins.transposeexprmixin import TransposeExprMixin +from polymatrix.expression.mixins.truncateexprmixin import TruncateExprMixin +from polymatrix.expression.mixins.vstackexprmixin import VStackExprMixin +from polymatrix.expression.mixins.tosortedvariablesmixin import ToSortedVariablesExprMixin + + +@dataclassabc.dataclassabc(frozen=True) +class AdditionExprImpl(AdditionExprMixin): + left: ExpressionBaseMixin + right: ExpressionBaseMixin + stack: tuple[FrameSummary] + + def __repr__(self): + return f'{self.__class__.__name__}(left={self.left}, right={self.right})' + +@dataclassabc.dataclassabc(frozen=True) +class BlockDiagExprImpl(BlockDiagExprMixin): + underlying: tuple[ExpressionBaseMixin] + + +@dataclassabc.dataclassabc(frozen=True) +class CacheExprImpl(CacheExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class CombinationsExprImpl(CombinationsExprMixin): + expression: ExpressionBaseMixin + degrees: tuple[int, ...] + + +@dataclassabc.dataclassabc(frozen=True) +class DerivativeExprImpl(DerivativeExprMixin): + underlying: ExpressionBaseMixin + variables: tuple + introduce_derivatives: bool + stack: tuple[FrameSummary] + + def __repr__(self): + return f'{self.__class__.__name__}(variables={self.variables}, underlying={self.underlying})' + + +@dataclassabc.dataclassabc(frozen=True) +class DeterminantExprImpl(DeterminantExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class DiagExprImpl(DiagExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class DivergenceExprImpl(DivergenceExprMixin): + underlying: ExpressionBaseMixin + variables: tuple + +@dataclassabc.dataclassabc(frozen=True) +class DivisionExprImpl(DivisionExprMixin): + left: ExpressionBaseMixin + right: ExpressionBaseMixin + stack: tuple[FrameSummary] + + def __repr__(self): + return f'{self.__class__.__name__}(left={self.left}, right={self.right})' + + +@dataclassabc.dataclassabc(frozen=True) +class ElemMultExprImpl(ElemMultExprMixin): + left: ExpressionBaseMixin + right: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class EvalExprImpl(EvalExprMixin): + underlying: ExpressionBaseMixin + substitutions: tuple + + +@dataclassabc.dataclassabc(frozen=True) +class EyeExprImpl(EyeExprMixin): + variable: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class FilterExprImpl(FilterExprMixin): + underlying: ExpressionBaseMixin + predicator: ExpressionBaseMixin + inverse: bool == None + + +@dataclassabc.dataclassabc(frozen=True) +class FromSymmetricMatrixExprImpl(FromSymmetricMatrixExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class FromTupleExprImpl(FromTupleExprMixin): + data: tuple[tuple[float]] + stack: tuple[FrameSummary] + + def __repr__(self): + return f'{self.__class__.__name__}(data={self.data})' + +@dataclassabc.dataclassabc(frozen=True) +class FromTermsExprImpl(FromTermsExprMixin): + terms: tuple + shape: tuple[int, int] + + +@dataclassabc.dataclassabc(frozen=True) +class GetItemExprImpl(GetItemExprMixin): + underlying: ExpressionBaseMixin + index: tuple[tuple[int, ...], tuple[int, ...]] + + +@dataclassabc.dataclassabc(frozen=True) +class HalfNewtonPolytopeExprImpl(HalfNewtonPolytopeExprMixin): + monomials: ExpressionBaseMixin + variables: ExpressionBaseMixin + filter: ExpressionBaseMixin | None + + +@dataclassabc.dataclassabc(frozen=True) +class LinearInExprImpl(LinearInExprMixin): + underlying: ExpressionBaseMixin + monomials: ExpressionBaseMixin + variables: ExpressionBaseMixin + ignore_unmatched: bool + + +@dataclassabc.dataclassabc(frozen=True) +class LinearMatrixInExprImpl(LinearMatrixInExprMixin): + underlying: ExpressionBaseMixin + variable: int + + +@dataclassabc.dataclassabc(frozen=True) +class LinearMonomialsExprImpl(LinearMonomialsExprMixin): + underlying: ExpressionBaseMixin + variables: tuple + + +@dataclassabc.dataclassabc(frozen=True) +class LegendreSeriesImpl(LegendreSeriesMixin): + underlying: ExpressionBaseMixin + degrees: tuple[int, ...] | None + stack: tuple[FrameSummary] + + +@dataclassabc.dataclassabc(frozen=True) +class MatrixMultExprImpl(MatrixMultExprMixin): + left: ExpressionBaseMixin + right: ExpressionBaseMixin + stack: tuple[FrameSummary] + + def __repr__(self): + return f'{self.__class__.__name__}(left={self.left}, right={self.right})' + + +@dataclassabc.dataclassabc(frozen=True) +class MaxDegreeExprImpl(MaxDegreeExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class MaxExprImpl(MaxExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True, repr=False) +class ParametrizeExprImpl(ParametrizeExprMixin): + underlying: ExpressionBaseMixin + name: str + + def __repr__(self) -> str: + return f'{self.__class__.__name__}(name={self.name}, underlying={self.underlying})' + + +@dataclassabc.dataclassabc(frozen=True) +class ParametrizeMatrixExprImpl(ParametrizeMatrixExprMixin): + underlying: ExpressionBaseMixin + name: str + + +@dataclassabc.dataclassabc(frozen=True) +class ProductExprImpl(ProductExprMixin): + underlying: tuple[ExpressionBaseMixin] + degrees: tuple[int, ...] | None + stack: tuple[FrameSummary] + + +@dataclassabc.dataclassabc(frozen=True) +class QuadraticInExprImpl(QuadraticInExprMixin): + underlying: ExpressionBaseMixin + monomials: ExpressionBaseMixin + variables: tuple + stack: tuple[FrameSummary] + + def __repr__(self): + return f'{self.__class__.__name__}(variables={self.variables}, monomials={self.monomials}, underlying={self.underlying})' + + +@dataclassabc.dataclassabc(frozen=True) +class QuadraticMonomialsExprImpl(QuadraticMonomialsExprMixin): + underlying: ExpressionBaseMixin + variables: tuple + + +@dataclassabc.dataclassabc(frozen=True) +class RepMatExprImpl(RepMatExprMixin): + underlying: ExpressionBaseMixin + repetition: tuple + + +@dataclassabc.dataclassabc(frozen=True) +class ReshapeExprImpl(ReshapeExprMixin): + underlying: ExpressionBaseMixin + new_shape: tuple + + +@dataclassabc.dataclassabc(frozen=True) +class SetElementAtExprImpl(SetElementAtExprMixin): + underlying: ExpressionBaseMixin + index: tuple + value: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class SqueezeExprImpl(SqueezeExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class SubstituteExprImpl(SubstituteExprMixin): + underlying: ExpressionBaseMixin + substitutions: tuple + + +@dataclassabc.dataclassabc(frozen=True) +class SubtractMonomialsExprImpl(SubtractMonomialsExprMixin): + underlying: ExpressionBaseMixin + monomials: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class SumExprImpl(SumExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class SymmetricExprImpl(SymmetricExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class ToConstantExprImpl(ToConstantExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class ToQuadraticExprImpl(ToQuadraticExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class ToSortedVariablesImpl(ToSortedVariablesExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class ToSymmetricMatrixExprImpl(ToSymmetricMatrixExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class TransposeExprImpl(TransposeExprMixin): + underlying: ExpressionBaseMixin + + +@dataclassabc.dataclassabc(frozen=True) +class TruncateExprImpl(TruncateExprMixin): + underlying: ExpressionBaseMixin + variables: ExpressionBaseMixin + degrees: tuple[int] + inverse: bool + + +@dataclassabc.dataclassabc(frozen=True) +class VStackExprImpl(VStackExprMixin): + underlying: tuple diff --git a/polymatrix/expression/implexpressionbase.py b/polymatrix/expression/implexpressionbase.py deleted file mode 100644 index 79761f2..0000000 --- a/polymatrix/expression/implexpressionbase.py +++ /dev/null @@ -1,352 +0,0 @@ -from polymatrix.expression.mixins.legendreseriesmixin import LegendreSeriesMixin -from polymatrix.expression.mixins.productexprmixin import ProductExprMixin -from polymatrix.utils.getstacklines import FrameSummary -import dataclassabc - -from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin -from polymatrix.expression.mixins.additionexprmixin import AdditionExprMixin -from polymatrix.expression.mixins.blockdiagexprmixin import BlockDiagExprMixin -from polymatrix.expression.mixins.cacheexprmixin import CacheExprMixin -from polymatrix.expression.mixins.combinationsexprmixin import \ - CombinationsExprMixin -from polymatrix.expression.mixins.derivativeexprmixin import \ - DerivativeExprMixin -from polymatrix.expression.mixins.determinantexprmixin import \ - DeterminantExprMixin -from polymatrix.expression.mixins.diagexprmixin import DiagExprMixin -from polymatrix.expression.mixins.divergenceexprmixin import \ - DivergenceExprMixin -from polymatrix.expression.mixins.divisionexprmixin import DivisionExprMixin -from polymatrix.expression.mixins.elemmultexprmixin import ElemMultExprMixin -from polymatrix.expression.mixins.evalexprmixin import EvalExprMixin -from polymatrix.expression.mixins.eyeexprmixin import EyeExprMixin -from polymatrix.expression.mixins.filterexprmixin import FilterExprMixin -from polymatrix.expression.mixins.fromsymmetricmatrixexprmixin import \ - FromSymmetricMatrixExprMixin -from polymatrix.expression.mixins.fromtupleexprmixin import FromTupleExprMixin -from polymatrix.expression.mixins.fromtermsexprmixin import FromTermsExprMixin -from polymatrix.expression.mixins.getitemexprmixin import GetItemExprMixin -from polymatrix.expression.mixins.halfnewtonpolytopeexprmixin import \ - HalfNewtonPolytopeExprMixin -from polymatrix.expression.mixins.linearinexprmixin import LinearInExprMixin -from polymatrix.expression.mixins.linearmatrixinexprmixin import \ - LinearMatrixInExprMixin -from polymatrix.expression.mixins.linearmonomialsexprmixin import \ - LinearMonomialsExprMixin -from polymatrix.expression.mixins.matrixmultexprmixin import \ - MatrixMultExprMixin -from polymatrix.expression.mixins.maxdegreeexprmixin import MaxDegreeExprMixin -from polymatrix.expression.mixins.maxexprmixin import MaxExprMixin -from polymatrix.expression.mixins.parametrizeexprmixin import \ - ParametrizeExprMixin -from polymatrix.expression.mixins.parametrizematrixexprmixin import \ - ParametrizeMatrixExprMixin -from polymatrix.expression.mixins.quadraticinexprmixin import \ - QuadraticInExprMixin -from polymatrix.expression.mixins.quadraticmonomialsexprmixin import \ - QuadraticMonomialsExprMixin -from polymatrix.expression.mixins.repmatexprmixin import RepMatExprMixin -from polymatrix.expression.mixins.reshapeexprmixin import ReshapeExprMixin -from polymatrix.expression.mixins.setelementatexprmixin import \ - SetElementAtExprMixin -from polymatrix.expression.mixins.squeezeexprmixin import SqueezeExprMixin -from polymatrix.expression.mixins.substituteexprmixin import \ - SubstituteExprMixin -from polymatrix.expression.mixins.subtractmonomialsexprmixin import \ - SubtractMonomialsExprMixin -from polymatrix.expression.mixins.sumexprmixin import SumExprMixin -from polymatrix.expression.mixins.symmetricexprmixin import SymmetricExprMixin -from polymatrix.expression.mixins.toconstantexprmixin import \ - ToConstantExprMixin -from polymatrix.expression.mixins.toquadraticexprmixin import \ - ToQuadraticExprMixin -from polymatrix.expression.mixins.tosymmetricmatrixexprmixin import \ - ToSymmetricMatrixExprMixin -from polymatrix.expression.mixins.transposeexprmixin import TransposeExprMixin -from polymatrix.expression.mixins.truncateexprmixin import TruncateExprMixin -from polymatrix.expression.mixins.vstackexprmixin import VStackExprMixin -from polymatrix.expression.mixins.tosortedvariablesmixin import ToSortedVariablesExprMixin - - -@dataclassabc.dataclassabc(frozen=True) -class AdditionExprImpl(AdditionExprMixin): - left: ExpressionBaseMixin - right: ExpressionBaseMixin - stack: tuple[FrameSummary] - - def __repr__(self): - return f'{self.__class__.__name__}(left={self.left}, right={self.right})' - -@dataclassabc.dataclassabc(frozen=True) -class BlockDiagExprImpl(BlockDiagExprMixin): - underlying: tuple[ExpressionBaseMixin] - - -@dataclassabc.dataclassabc(frozen=True) -class CacheExprImpl(CacheExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class CombinationsExprImpl(CombinationsExprMixin): - expression: ExpressionBaseMixin - degrees: tuple[int, ...] - - -@dataclassabc.dataclassabc(frozen=True) -class DerivativeExprImpl(DerivativeExprMixin): - underlying: ExpressionBaseMixin - variables: tuple - introduce_derivatives: bool - stack: tuple[FrameSummary] - - def __repr__(self): - return f'{self.__class__.__name__}(variables={self.variables}, underlying={self.underlying})' - - -@dataclassabc.dataclassabc(frozen=True) -class DeterminantExprImpl(DeterminantExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class DiagExprImpl(DiagExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class DivergenceExprImpl(DivergenceExprMixin): - underlying: ExpressionBaseMixin - variables: tuple - -@dataclassabc.dataclassabc(frozen=True) -class DivisionExprImpl(DivisionExprMixin): - left: ExpressionBaseMixin - right: ExpressionBaseMixin - stack: tuple[FrameSummary] - - -@dataclassabc.dataclassabc(frozen=True) -class ElemMultExprImpl(ElemMultExprMixin): - left: ExpressionBaseMixin - right: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class EvalExprImpl(EvalExprMixin): - underlying: ExpressionBaseMixin - substitutions: tuple - - -@dataclassabc.dataclassabc(frozen=True) -class EyeExprImpl(EyeExprMixin): - variable: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class FilterExprImpl(FilterExprMixin): - underlying: ExpressionBaseMixin - predicator: ExpressionBaseMixin - inverse: bool == None - - -@dataclassabc.dataclassabc(frozen=True) -class FromSymmetricMatrixExprImpl(FromSymmetricMatrixExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class FromTupleExprImpl(FromTupleExprMixin): - data: tuple[tuple[float]] - stack: tuple[FrameSummary] - - def __repr__(self): - return f'{self.__class__.__name__}(data={self.data})' - -@dataclassabc.dataclassabc(frozen=True) -class FromTermsExprImpl(FromTermsExprMixin): - terms: tuple - shape: tuple[int, int] - - -@dataclassabc.dataclassabc(frozen=True) -class GetItemExprImpl(GetItemExprMixin): - underlying: ExpressionBaseMixin - index: tuple[tuple[int, ...], tuple[int, ...]] - - -@dataclassabc.dataclassabc(frozen=True) -class HalfNewtonPolytopeExprImpl(HalfNewtonPolytopeExprMixin): - monomials: ExpressionBaseMixin - variables: ExpressionBaseMixin - filter: ExpressionBaseMixin | None - - -@dataclassabc.dataclassabc(frozen=True) -class LinearInExprImpl(LinearInExprMixin): - underlying: ExpressionBaseMixin - monomials: ExpressionBaseMixin - variables: ExpressionBaseMixin - ignore_unmatched: bool - - -@dataclassabc.dataclassabc(frozen=True) -class LinearMatrixInExprImpl(LinearMatrixInExprMixin): - underlying: ExpressionBaseMixin - variable: int - - -@dataclassabc.dataclassabc(frozen=True) -class LinearMonomialsExprImpl(LinearMonomialsExprMixin): - underlying: ExpressionBaseMixin - variables: tuple - - -@dataclassabc.dataclassabc(frozen=True) -class LegendreSeriesImpl(LegendreSeriesMixin): - underlying: ExpressionBaseMixin - degrees: tuple[int, ...] | None - stack: tuple[FrameSummary] - - -@dataclassabc.dataclassabc(frozen=True) -class MatrixMultExprImpl(MatrixMultExprMixin): - left: ExpressionBaseMixin - right: ExpressionBaseMixin - stack: tuple[FrameSummary] - - def __repr__(self): - return f'{self.__class__.__name__}(left={self.left}, right={self.right})' - - -@dataclassabc.dataclassabc(frozen=True) -class MaxDegreeExprImpl(MaxDegreeExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class MaxExprImpl(MaxExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True, repr=False) -class ParametrizeExprImpl(ParametrizeExprMixin): - underlying: ExpressionBaseMixin - name: str - - def __repr__(self) -> str: - return f'{self.__class__.__name__}(name={self.name})' - - -@dataclassabc.dataclassabc(frozen=True) -class ParametrizeMatrixExprImpl(ParametrizeMatrixExprMixin): - underlying: ExpressionBaseMixin - name: str - - -@dataclassabc.dataclassabc(frozen=True) -class ProductExprImpl(ProductExprMixin): - underlying: tuple[ExpressionBaseMixin] - degrees: tuple[int, ...] | None - stack: tuple[FrameSummary] - - -@dataclassabc.dataclassabc(frozen=True) -class QuadraticInExprImpl(QuadraticInExprMixin): - underlying: ExpressionBaseMixin - monomials: ExpressionBaseMixin - variables: tuple - stack: tuple[FrameSummary] - - def __repr__(self): - return f'{self.__class__.__name__}(variables={self.variables}, monomials={self.monomials}, underlying={self.underlying})' - - -@dataclassabc.dataclassabc(frozen=True) -class QuadraticMonomialsExprImpl(QuadraticMonomialsExprMixin): - underlying: ExpressionBaseMixin - variables: tuple - - -@dataclassabc.dataclassabc(frozen=True) -class RepMatExprImpl(RepMatExprMixin): - underlying: ExpressionBaseMixin - repetition: tuple - - -@dataclassabc.dataclassabc(frozen=True) -class ReshapeExprImpl(ReshapeExprMixin): - underlying: ExpressionBaseMixin - new_shape: tuple - - -@dataclassabc.dataclassabc(frozen=True) -class SetElementAtExprImpl(SetElementAtExprMixin): - underlying: ExpressionBaseMixin - index: tuple - value: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class SqueezeExprImpl(SqueezeExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class SubstituteExprImpl(SubstituteExprMixin): - underlying: ExpressionBaseMixin - substitutions: tuple - - -@dataclassabc.dataclassabc(frozen=True) -class SubtractMonomialsExprImpl(SubtractMonomialsExprMixin): - underlying: ExpressionBaseMixin - monomials: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class SumExprImpl(SumExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class SymmetricExprImpl(SymmetricExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class ToConstantExprImpl(ToConstantExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class ToQuadraticExprImpl(ToQuadraticExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class ToSortedVariablesImpl(ToSortedVariablesExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class ToSymmetricMatrixExprImpl(ToSymmetricMatrixExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class TransposeExprImpl(TransposeExprMixin): - underlying: ExpressionBaseMixin - - -@dataclassabc.dataclassabc(frozen=True) -class TruncateExprImpl(TruncateExprMixin): - underlying: ExpressionBaseMixin - variables: ExpressionBaseMixin - degrees: tuple[int] - inverse: bool - - -@dataclassabc.dataclassabc(frozen=True) -class VStackExprImpl(VStackExprMixin): - underlying: tuple diff --git a/polymatrix/expression/init.py b/polymatrix/expression/init.py new file mode 100644 index 0000000..7a4ea36 --- /dev/null +++ b/polymatrix/expression/init.py @@ -0,0 +1,604 @@ +import typing +import numpy as np +import sympy + +import polymatrix.expression.impl + +from polymatrix.utils.getstacklines import FrameSummary +from polymatrix.utils.getstacklines import get_stack_lines +from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin +from polymatrix.expression.utils.formatsubstitutions import format_substitutions +from polymatrix.polymatrix.mixins.polymatrixmixin import PolyMatrixMixin +from polymatrix.expression.impl import FromTupleExprImpl, AdditionExprImpl + + +def init_addition_expr( + left: ExpressionBaseMixin, + right: ExpressionBaseMixin, + stack: tuple[FrameSummary], +): + return AdditionExprImpl( + left=left, + right=right, + stack=stack, +) + + +def init_block_diag_expr( + underlying: tuple, +): + return polymatrix.expression.impl.BlockDiagExprImpl( + underlying=underlying, + ) + + +def init_cache_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.CacheExprImpl( + underlying=underlying, +) + + +def init_combinations_expr( + expression: ExpressionBaseMixin, + degrees: tuple[int, ...] | int, +): + if isinstance(degrees, int): + degrees = (degrees,) + + return polymatrix.expression.impl.CombinationsExprImpl( + expression=expression, + degrees=degrees, +) + + +# def init_derivative_expr( +# underlying: ExpressionBaseMixin, +# variables: ExpressionBaseMixin, +# stack: tuple[FrameSummary], +# introduce_derivatives: bool = None, +# ): + +# assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' + +# if introduce_derivatives is None: +# introduce_derivatives = False + +# return polymatrix.expression.impl.DerivativeExprImpl( +# underlying=underlying, +# variables=variables, +# introduce_derivatives=introduce_derivatives, +# stack=stack, +# ) + + +def init_determinant_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.DeterminantExprImpl( + underlying=underlying, +) + + +def init_diag_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.DiagExprImpl( + underlying=underlying, +) + + +def init_divergence_expr( + underlying: ExpressionBaseMixin, + variables: tuple, +): + return polymatrix.expression.impl.DivergenceExprImpl( + underlying=underlying, + variables=variables, +) + + +def init_division_expr( + left: ExpressionBaseMixin, + right: ExpressionBaseMixin, + stack: tuple[FrameSummary], +): + return polymatrix.expression.impl.DivisionExprImpl( + left=left, + right=right, + stack=stack, +) + + +def init_elem_mult_expr( + left: ExpressionBaseMixin, + right: ExpressionBaseMixin, + stack: tuple[FrameSummary], +): + return polymatrix.expression.impl.ElemMultExprImpl( + left=left, + right=right, +) + + +def init_eval_expr( + underlying: ExpressionBaseMixin, + variables: typing.Union[typing.Any, tuple, dict], + values: typing.Union[float, tuple] = None, +): + + substitutions = format_substitutions( + variables=variables, + values=values, + ) + + def formatted_values(value): + if isinstance(value, np.ndarray): + return tuple(value.reshape(-1)) + + elif isinstance(value, tuple): + return value + + elif isinstance(value, int) or isinstance(value, float): + return (value,) + + else: + return (float(value),) + + substitutions = tuple((variable, formatted_values(value)) for variable, value in substitutions) + + return polymatrix.expression.impl.EvalExprImpl( + underlying=underlying, + substitutions=substitutions, + ) + + +def init_eye_expr( + variable: ExpressionBaseMixin, +): + return polymatrix.expression.impl.EyeExprImpl( + variable=variable, +) + + +def init_filter_expr( + underlying: ExpressionBaseMixin, + predicator: ExpressionBaseMixin, + inverse: bool = None, +): + if inverse is None: + inverse = False + + return polymatrix.expression.impl.FilterExprImpl( + underlying=underlying, + predicator=predicator, + inverse=inverse, +) + + +def init_from_symmetric_matrix_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.FromSymmetricMatrixExprImpl( + underlying=underlying, +) + + +DATA_TYPE = str | np.ndarray | sympy.Matrix | sympy.Expr | tuple | ExpressionBaseMixin + +def init_from_expr_or_none( + data: DATA_TYPE, +) -> ExpressionBaseMixin | None: + if isinstance(data, str): + return init_parametrize_expr( + underlying=init_from_expr_or_none(1), + name=data, + ) + + elif isinstance(data, np.ndarray): + assert len(data.shape) <= 2 + + def gen_elements(): + for row in data: + if isinstance(row, np.ndarray): + yield tuple(row) + else: + yield (row,) + + data = tuple(gen_elements()) + + elif isinstance(data, sympy.Matrix): + data = tuple(tuple(i for i in data.row(row)) for row in range(data.rows)) + + elif isinstance(data, sympy.Expr): + data = ((sympy.expand(data),),) + + elif isinstance(data, tuple): + + if isinstance(data[0], tuple): + n_col = len(data[0]) + assert all(len(col) == n_col for col in data) + + else: + data = tuple((e,) for e in data) + + elif isinstance(data, ExpressionBaseMixin): + return data + + else: + if not isinstance(data, (float, int, np.number)): + return None + + data = ((data,),) + + return FromTupleExprImpl( + data=data, + stack=get_stack_lines(), + ) + + +def init_from_expr( + data: DATA_TYPE +): + expr = init_from_expr_or_none(data) + + if expr is None: + raise Exception(f'{data=}') + + return expr + + +def init_from_terms_expr( + terms: typing.Union[tuple, PolyMatrixMixin], + shape: tuple[int, int] = None, +): + + if isinstance(terms, PolyMatrixMixin): + shape = terms.shape + gen_terms = terms.gen_terms() + + else: + assert shape is not None + + if isinstance(terms, tuple): + gen_terms = terms + + elif isinstance(terms, dict): + gen_terms = terms.items() + + else: + raise Exception(f'{terms=}') + + # Expression needs to be hashable + terms_formatted = tuple((key, tuple(monomials.items())) for key, monomials in gen_terms) + + return polymatrix.expression.impl.FromTermsExprImpl( + terms=terms_formatted, + shape=shape, + ) + + +def init_get_item_expr( + underlying: ExpressionBaseMixin, + index: tuple[tuple[int, ...], tuple[int, ...]], +): + + def get_hashable_slice(index): + if isinstance(index, slice): + return polymatrix.expression.impl.GetItemExprImpl.Slice(start=index.start, stop=index.stop, step=index.step) + else: + return index + + proper_index = (get_hashable_slice(index[0]), get_hashable_slice(index[1])) + + return polymatrix.expression.impl.GetItemExprImpl( + underlying=underlying, + index=proper_index, +) + + +def init_half_newton_polytope_expr( + monomials: ExpressionBaseMixin, + variables: ExpressionBaseMixin, + filter: ExpressionBaseMixin | None = None, +): + return polymatrix.expression.impl.HalfNewtonPolytopeExprImpl( + monomials=monomials, + variables=variables, + filter=filter +) + + +# def init_linear_in_expr( +# underlying: ExpressionBaseMixin, +# monomials: ExpressionBaseMixin, +# variables: ExpressionBaseMixin, +# ignore_unmatched: bool = None, +# ): +# assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' + +# return polymatrix.expression.impl.LinearInExprImpl( +# underlying=underlying, +# monomials=monomials, +# variables=variables, +# ignore_unmatched = ignore_unmatched, +# ) + + +def init_linear_matrix_in_expr( + underlying: ExpressionBaseMixin, + variable: int, +): + return polymatrix.expression.impl.LinearMatrixInExprImpl( + underlying=underlying, + variable=variable, +) + + +# def init_linear_monomials_expr( +# underlying: ExpressionBaseMixin, +# variables: ExpressionBaseMixin, +# ): + +# assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' + +# return polymatrix.expression.impl.LinearMonomialsExprImpl( +# underlying=underlying, +# variables=variables, +# ) + + +def init_matrix_mult_expr( + left: ExpressionBaseMixin, + right: ExpressionBaseMixin, + stack: tuple[FrameSummary], +): + return polymatrix.expression.impl.MatrixMultExprImpl( + left=left, + right=right, + stack=stack, +) + + +def init_max_degree_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.MaxDegreeExprImpl( + underlying=underlying, +) + + +def init_max_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.MaxExprImpl( + underlying=underlying, +) + + +def init_parametrize_expr( + underlying: ExpressionBaseMixin, + name: str = None, +): + if name is None: + name = 'undefined' + + return polymatrix.expression.impl.ParametrizeExprImpl( + underlying=underlying, + name=name, +) + + +def init_parametrize_matrix_expr( + underlying: ExpressionBaseMixin, + name: str, +): + return polymatrix.expression.impl.ParametrizeMatrixExprImpl( + underlying=underlying, + name=name, + ) + + +def init_quadratic_in_expr( + underlying: ExpressionBaseMixin, + monomials: ExpressionBaseMixin, + variables: ExpressionBaseMixin, + stack: tuple[FrameSummary], +): + + assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' + + return polymatrix.expression.impl.QuadraticInExprImpl( + underlying=underlying, + monomials=monomials, + variables=variables, + stack=stack, + ) + + +def init_quadratic_monomials_expr( + underlying: ExpressionBaseMixin, + variables: ExpressionBaseMixin, +): + + assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' + + return polymatrix.expression.impl.QuadraticMonomialsExprImpl( + underlying=underlying, + variables=variables, + ) + + +def init_rep_mat_expr( + underlying: ExpressionBaseMixin, + repetition: tuple, +): + return polymatrix.expression.impl.RepMatExprImpl( + underlying=underlying, + repetition=repetition, +) + + +def init_reshape_expr( + underlying: ExpressionBaseMixin, + new_shape: tuple, +): + return polymatrix.expression.impl.ReshapeExprImpl( + underlying=underlying, + new_shape=new_shape, +) + + +def init_set_element_at_expr( + underlying: ExpressionBaseMixin, + index: tuple, + value: ExpressionBaseMixin, +): + return polymatrix.expression.impl.SetElementAtExprImpl( + underlying=underlying, + index=index, + value=value, +) + + +def init_squeeze_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.SqueezeExprImpl( + underlying=underlying, +) + + +def init_substitute_expr( + underlying: ExpressionBaseMixin, + variables: tuple, + values: tuple = None, +): + + substitutions = format_substitutions( + variables=variables, + values=values, + ) + + def formatted_values(value) -> ExpressionBaseMixin: + if isinstance(value, ExpressionBaseMixin): + expr = value + + else: + expr = init_from_expr(value) + + return polymatrix.expression.impl.ReshapeExprImpl( + underlying=expr, + new_shape=(-1, 1), + ) + + substitutions = tuple((variable, formatted_values(value)) for variable, value in substitutions) + + return polymatrix.expression.impl.SubstituteExprImpl( + underlying=underlying, + substitutions=substitutions, + ) + + +def init_subtract_monomials_expr( + underlying: ExpressionBaseMixin, + monomials: ExpressionBaseMixin, +): + return polymatrix.expression.impl.SubtractMonomialsExprImpl( + underlying=underlying, + monomials=monomials, +) + + +def init_sum_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.SumExprImpl( + underlying=underlying, +) + + +def init_symmetric_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.SymmetricExprImpl( + underlying=underlying, +) + + +def init_to_constant_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.ToConstantExprImpl( + underlying=underlying, +) + + +def init_to_quadratic_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.ToQuadraticExprImpl( + underlying=underlying, +) + + +def init_to_sorted_variables( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.ToSortedVariablesImpl( + underlying=underlying, +) + + +def init_to_symmetric_matrix_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.ToSymmetricMatrixExprImpl( + underlying=underlying, +) + + +def init_transpose_expr( + underlying: ExpressionBaseMixin, +): + return polymatrix.expression.impl.TransposeExprImpl( + underlying=underlying, +) + + +def init_truncate_expr( + underlying: ExpressionBaseMixin, + variables: ExpressionBaseMixin, + degrees: tuple[int], + inverse: bool = None, +): + if isinstance(degrees, int): + degrees = (degrees,) + + if inverse is None: + inverse = False + + return polymatrix.expression.impl.TruncateExprImpl( + underlying=underlying, + variables=variables, + degrees=degrees, + inverse=inverse, + ) + + +# def init_v_stack_expr( +# underlying: tuple, +# ): + +# def gen_underlying(): + +# for e in underlying: +# if isinstance(e, ExpressionBaseMixin): +# yield e +# else: +# yield init_from_(e) + +# return polymatrix.expression.impl.VStackExprImpl( +# underlying=tuple(gen_underlying()), +# ) diff --git a/polymatrix/expression/initexpressionbase.py b/polymatrix/expression/initexpressionbase.py deleted file mode 100644 index 1a3e52c..0000000 --- a/polymatrix/expression/initexpressionbase.py +++ /dev/null @@ -1,604 +0,0 @@ -import typing -import numpy as np -import sympy - -import polymatrix.expression.implexpressionbase - -from polymatrix.utils.getstacklines import FrameSummary -from polymatrix.utils.getstacklines import get_stack_lines -from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin -from polymatrix.expression.utils.formatsubstitutions import format_substitutions -from polymatrix.polymatrix.mixins.polymatrixmixin import PolyMatrixMixin -from polymatrix.expression.implexpressionbase import FromTupleExprImpl, AdditionExprImpl - - -def init_addition_expr( - left: ExpressionBaseMixin, - right: ExpressionBaseMixin, - stack: tuple[FrameSummary], -): - return AdditionExprImpl( - left=left, - right=right, - stack=stack, -) - - -def init_block_diag_expr( - underlying: tuple, -): - return polymatrix.expression.implexpressionbase.BlockDiagExprImpl( - underlying=underlying, - ) - - -def init_cache_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.CacheExprImpl( - underlying=underlying, -) - - -def init_combinations_expr( - expression: ExpressionBaseMixin, - degrees: tuple[int, ...] | int, -): - if isinstance(degrees, int): - degrees = (degrees,) - - return polymatrix.expression.implexpressionbase.CombinationsExprImpl( - expression=expression, - degrees=degrees, -) - - -# def init_derivative_expr( -# underlying: ExpressionBaseMixin, -# variables: ExpressionBaseMixin, -# stack: tuple[FrameSummary], -# introduce_derivatives: bool = None, -# ): - -# assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' - -# if introduce_derivatives is None: -# introduce_derivatives = False - -# return polymatrix.expression.implexpressionbase.DerivativeExprImpl( -# underlying=underlying, -# variables=variables, -# introduce_derivatives=introduce_derivatives, -# stack=stack, -# ) - - -def init_determinant_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.DeterminantExprImpl( - underlying=underlying, -) - - -def init_diag_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.DiagExprImpl( - underlying=underlying, -) - - -def init_divergence_expr( - underlying: ExpressionBaseMixin, - variables: tuple, -): - return polymatrix.expression.implexpressionbase.DivergenceExprImpl( - underlying=underlying, - variables=variables, -) - - -def init_division_expr( - left: ExpressionBaseMixin, - right: ExpressionBaseMixin, - stack: tuple[FrameSummary], -): - return polymatrix.expression.implexpressionbase.DivisionExprImpl( - left=left, - right=right, - stack=stack, -) - - -def init_elem_mult_expr( - left: ExpressionBaseMixin, - right: ExpressionBaseMixin, - stack: tuple[FrameSummary], -): - return polymatrix.expression.implexpressionbase.ElemMultExprImpl( - left=left, - right=right, -) - - -def init_eval_expr( - underlying: ExpressionBaseMixin, - variables: typing.Union[typing.Any, tuple, dict], - values: typing.Union[float, tuple] = None, -): - - substitutions = format_substitutions( - variables=variables, - values=values, - ) - - def formatted_values(value): - if isinstance(value, np.ndarray): - return tuple(value.reshape(-1)) - - elif isinstance(value, tuple): - return value - - elif isinstance(value, int) or isinstance(value, float): - return (value,) - - else: - return (float(value),) - - substitutions = tuple((variable, formatted_values(value)) for variable, value in substitutions) - - return polymatrix.expression.implexpressionbase.EvalExprImpl( - underlying=underlying, - substitutions=substitutions, - ) - - -def init_eye_expr( - variable: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.EyeExprImpl( - variable=variable, -) - - -def init_filter_expr( - underlying: ExpressionBaseMixin, - predicator: ExpressionBaseMixin, - inverse: bool = None, -): - if inverse is None: - inverse = False - - return polymatrix.expression.implexpressionbase.FilterExprImpl( - underlying=underlying, - predicator=predicator, - inverse=inverse, -) - - -def init_from_symmetric_matrix_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.FromSymmetricMatrixExprImpl( - underlying=underlying, -) - - -DATA_TYPE = str | np.ndarray | sympy.Matrix | sympy.Expr | tuple | ExpressionBaseMixin - -def init_from_expr_or_none( - data: DATA_TYPE, -) -> ExpressionBaseMixin | None: - if isinstance(data, str): - return init_parametrize_expr( - underlying=init_from_expr_or_none(1), - name=data, - ) - - elif isinstance(data, np.ndarray): - assert len(data.shape) <= 2 - - def gen_elements(): - for row in data: - if isinstance(row, np.ndarray): - yield tuple(row) - else: - yield (row,) - - data = tuple(gen_elements()) - - elif isinstance(data, sympy.Matrix): - data = tuple(tuple(i for i in data.row(row)) for row in range(data.rows)) - - elif isinstance(data, sympy.Expr): - data = ((sympy.expand(data),),) - - elif isinstance(data, tuple): - - if isinstance(data[0], tuple): - n_col = len(data[0]) - assert all(len(col) == n_col for col in data) - - else: - data = tuple((e,) for e in data) - - elif isinstance(data, ExpressionBaseMixin): - return data - - else: - if not isinstance(data, (float, int, np.number)): - return None - - data = ((data,),) - - return FromTupleExprImpl( - data=data, - stack=get_stack_lines(), - ) - - -def init_from_expr( - data: DATA_TYPE -): - expr = init_from_expr_or_none(data) - - if expr is None: - raise Exception(f'{data=}') - - return expr - - -def init_from_terms_expr( - terms: typing.Union[tuple, PolyMatrixMixin], - shape: tuple[int, int] = None, -): - - if isinstance(terms, PolyMatrixMixin): - shape = terms.shape - gen_terms = terms.gen_terms() - - else: - assert shape is not None - - if isinstance(terms, tuple): - gen_terms = terms - - elif isinstance(terms, dict): - gen_terms = terms.items() - - else: - raise Exception(f'{terms=}') - - # Expression needs to be hashable - terms_formatted = tuple((key, tuple(monomials.items())) for key, monomials in gen_terms) - - return polymatrix.expression.implexpressionbase.FromTermsExprImpl( - terms=terms_formatted, - shape=shape, - ) - - -def init_get_item_expr( - underlying: ExpressionBaseMixin, - index: tuple[tuple[int, ...], tuple[int, ...]], -): - - def get_hashable_slice(index): - if isinstance(index, slice): - return polymatrix.expression.implexpressionbase.GetItemExprImpl.Slice(start=index.start, stop=index.stop, step=index.step) - else: - return index - - proper_index = (get_hashable_slice(index[0]), get_hashable_slice(index[1])) - - return polymatrix.expression.implexpressionbase.GetItemExprImpl( - underlying=underlying, - index=proper_index, -) - - -def init_half_newton_polytope_expr( - monomials: ExpressionBaseMixin, - variables: ExpressionBaseMixin, - filter: ExpressionBaseMixin | None = None, -): - return polymatrix.expression.implexpressionbase.HalfNewtonPolytopeExprImpl( - monomials=monomials, - variables=variables, - filter=filter -) - - -# def init_linear_in_expr( -# underlying: ExpressionBaseMixin, -# monomials: ExpressionBaseMixin, -# variables: ExpressionBaseMixin, -# ignore_unmatched: bool = None, -# ): -# assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' - -# return polymatrix.expression.implexpressionbase.LinearInExprImpl( -# underlying=underlying, -# monomials=monomials, -# variables=variables, -# ignore_unmatched = ignore_unmatched, -# ) - - -def init_linear_matrix_in_expr( - underlying: ExpressionBaseMixin, - variable: int, -): - return polymatrix.expression.implexpressionbase.LinearMatrixInExprImpl( - underlying=underlying, - variable=variable, -) - - -# def init_linear_monomials_expr( -# underlying: ExpressionBaseMixin, -# variables: ExpressionBaseMixin, -# ): - -# assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' - -# return polymatrix.expression.implexpressionbase.LinearMonomialsExprImpl( -# underlying=underlying, -# variables=variables, -# ) - - -def init_matrix_mult_expr( - left: ExpressionBaseMixin, - right: ExpressionBaseMixin, - stack: tuple[FrameSummary], -): - return polymatrix.expression.implexpressionbase.MatrixMultExprImpl( - left=left, - right=right, - stack=stack, -) - - -def init_max_degree_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.MaxDegreeExprImpl( - underlying=underlying, -) - - -def init_max_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.MaxExprImpl( - underlying=underlying, -) - - -def init_parametrize_expr( - underlying: ExpressionBaseMixin, - name: str = None, -): - if name is None: - name = 'undefined' - - return polymatrix.expression.implexpressionbase.ParametrizeExprImpl( - underlying=underlying, - name=name, -) - - -def init_parametrize_matrix_expr( - underlying: ExpressionBaseMixin, - name: str, -): - return polymatrix.expression.implexpressionbase.ParametrizeMatrixExprImpl( - underlying=underlying, - name=name, - ) - - -def init_quadratic_in_expr( - underlying: ExpressionBaseMixin, - monomials: ExpressionBaseMixin, - variables: ExpressionBaseMixin, - stack: tuple[FrameSummary], -): - - assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' - - return polymatrix.expression.implexpressionbase.QuadraticInExprImpl( - underlying=underlying, - monomials=monomials, - variables=variables, - stack=stack, - ) - - -def init_quadratic_monomials_expr( - underlying: ExpressionBaseMixin, - variables: ExpressionBaseMixin, -): - - assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' - - return polymatrix.expression.implexpressionbase.QuadraticMonomialsExprImpl( - underlying=underlying, - variables=variables, - ) - - -def init_rep_mat_expr( - underlying: ExpressionBaseMixin, - repetition: tuple, -): - return polymatrix.expression.implexpressionbase.RepMatExprImpl( - underlying=underlying, - repetition=repetition, -) - - -def init_reshape_expr( - underlying: ExpressionBaseMixin, - new_shape: tuple, -): - return polymatrix.expression.implexpressionbase.ReshapeExprImpl( - underlying=underlying, - new_shape=new_shape, -) - - -def init_set_element_at_expr( - underlying: ExpressionBaseMixin, - index: tuple, - value: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.SetElementAtExprImpl( - underlying=underlying, - index=index, - value=value, -) - - -def init_squeeze_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.SqueezeExprImpl( - underlying=underlying, -) - - -def init_substitute_expr( - underlying: ExpressionBaseMixin, - variables: tuple, - values: tuple = None, -): - - substitutions = format_substitutions( - variables=variables, - values=values, - ) - - def formatted_values(value) -> ExpressionBaseMixin: - if isinstance(value, ExpressionBaseMixin): - expr = value - - else: - expr = init_from_expr(value) - - return polymatrix.expression.implexpressionbase.ReshapeExprImpl( - underlying=expr, - new_shape=(-1, 1), - ) - - substitutions = tuple((variable, formatted_values(value)) for variable, value in substitutions) - - return polymatrix.expression.implexpressionbase.SubstituteExprImpl( - underlying=underlying, - substitutions=substitutions, - ) - - -def init_subtract_monomials_expr( - underlying: ExpressionBaseMixin, - monomials: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.SubtractMonomialsExprImpl( - underlying=underlying, - monomials=monomials, -) - - -def init_sum_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.SumExprImpl( - underlying=underlying, -) - - -def init_symmetric_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.SymmetricExprImpl( - underlying=underlying, -) - - -def init_to_constant_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.ToConstantExprImpl( - underlying=underlying, -) - - -def init_to_quadratic_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.ToQuadraticExprImpl( - underlying=underlying, -) - - -def init_to_sorted_variables( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.ToSortedVariablesImpl( - underlying=underlying, -) - - -def init_to_symmetric_matrix_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.ToSymmetricMatrixExprImpl( - underlying=underlying, -) - - -def init_transpose_expr( - underlying: ExpressionBaseMixin, -): - return polymatrix.expression.implexpressionbase.TransposeExprImpl( - underlying=underlying, -) - - -def init_truncate_expr( - underlying: ExpressionBaseMixin, - variables: ExpressionBaseMixin, - degrees: tuple[int], - inverse: bool = None, -): - if isinstance(degrees, int): - degrees = (degrees,) - - if inverse is None: - inverse = False - - return polymatrix.expression.implexpressionbase.TruncateExprImpl( - underlying=underlying, - variables=variables, - degrees=degrees, - inverse=inverse, - ) - - -# def init_v_stack_expr( -# underlying: tuple, -# ): - -# def gen_underlying(): - -# for e in underlying: -# if isinstance(e, ExpressionBaseMixin): -# yield e -# else: -# yield init_from_(e) - -# return polymatrix.expression.implexpressionbase.VStackExprImpl( -# underlying=tuple(gen_underlying()), -# ) diff --git a/polymatrix/expression/mixins/fromtupleexprmixin.py b/polymatrix/expression/mixins/fromtupleexprmixin.py index 4157ce7..551b20e 100644 --- a/polymatrix/expression/mixins/fromtupleexprmixin.py +++ b/polymatrix/expression/mixins/fromtupleexprmixin.py @@ -80,7 +80,6 @@ class FromTupleExprMixin(ExpressionBaseMixin): elif isinstance(poly_data, ExpressionBaseMixin): state, instance = poly_data.apply(state) - # assert instance.shape == (1, 1) if not (instance.shape == (1, 1)): raise AssertionError(to_operator_exception( message=f'{instance.shape=} is not (1, 1)', @@ -90,7 +89,6 @@ class FromTupleExprMixin(ExpressionBaseMixin): polynomial = instance.get_poly(0, 0) else: - # raise Exception(f'{poly_data=}, {type(poly_data)=}') raise AssertionError(to_operator_exception( message=f'unknown data type {type(poly_data)=}', stack=self.stack, diff --git a/polymatrix/expression/mixins/parametrizeexprmixin.py b/polymatrix/expression/mixins/parametrizeexprmixin.py index bb85387..198877e 100644 --- a/polymatrix/expression/mixins/parametrizeexprmixin.py +++ b/polymatrix/expression/mixins/parametrizeexprmixin.py @@ -24,34 +24,46 @@ class ParametrizeExprMixin(ExpressionBaseMixin): self, state: ExpressionStateMixin, ) -> tuple[ExpressionStateMixin, PolyMatrixMixin]: - - # cache polymatrix to not re-parametrize at every apply call - if self in state.cache: - return state, state.cache[self] - + state, underlying = self.underlying.apply(state) assert underlying.shape[1] == 1 + if self.name in state.offset_dict: + start, end = state.offset_dict[self.name] + + assert underlying.shape[0] == (end - start) + + else: + start = state.n_param + + state = state.register( + key=self.name, + n_param=underlying.shape[0], + ) + + # # cache polymatrix to not re-parametrize at every apply call + # if self in state.cache: + # return state, state.cache[self] + + # state, underlying = self.underlying.apply(state) + + # assert underlying.shape[1] == 1 + terms = {} for row in range(underlying.shape[0]): - var_index = state.n_param + row - terms[row, 0] = {((var_index, 1),): 1.0} - - state = state.register( - key=self.name, - n_param=underlying.shape[0], - ) + var_index = start + row + terms[row, 0] = {((var_index, 1),): 1} poly_matrix = init_poly_matrix( terms=terms, shape=underlying.shape, ) - state = dataclasses.replace( - state, - cache=state.cache | {self: poly_matrix}, - ) + # state = dataclasses.replace( + # state, + # cache=state.cache | {self: poly_matrix}, + # ) return state, poly_matrix diff --git a/polymatrix/expression/mixins/productexprmixin.py b/polymatrix/expression/mixins/productexprmixin.py index bacd29e..8299683 100644 --- a/polymatrix/expression/mixins/productexprmixin.py +++ b/polymatrix/expression/mixins/productexprmixin.py @@ -93,6 +93,7 @@ class ProductExprMixin(ExpressionBaseMixin): yield from filter(lambda v: sum(v) in self.degrees, product_indices) indices = tuple(gen_indices()) + # print(indices) # indices = filter(lambda v: sum(v) <= self.degree, itertools.product(*(range(self.degree) for _ in range(dim)))) diff --git a/polymatrix/expression/op.py b/polymatrix/expression/op.py index 0d98401..b62253c 100644 --- a/polymatrix/expression/op.py +++ b/polymatrix/expression/op.py @@ -1,4 +1,4 @@ -import polymatrix.expression.initexpressionbase +import polymatrix.expression.init from polymatrix.utils.getstacklines import get_stack_lines from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin @@ -11,12 +11,12 @@ def diff( ) -> ExpressionBaseMixin: if not isinstance(variables, ExpressionBaseMixin): - variables=polymatrix.expression.initexpressionbase.init_from_expr(variables) + variables=polymatrix.expression.init.init_from_expr(variables) if introduce_derivatives is None: introduce_derivatives = False - return polymatrix.expression.implexpressionbase.DerivativeExprImpl( + return polymatrix.expression.impl.DerivativeExprImpl( underlying=expression, variables=variables, introduce_derivatives=introduce_derivatives, @@ -29,7 +29,7 @@ def legendre( degrees: tuple[int, ...] = None, ) -> ExpressionBaseMixin: - return polymatrix.expression.implexpressionbase.LegendreSeriesImpl( + return polymatrix.expression.impl.LegendreSeriesImpl( underlying=expression, degrees=degrees, stack=get_stack_lines(), @@ -49,7 +49,7 @@ def linear_in( variables=variables, ) - return polymatrix.expression.implexpressionbase.LinearInExprImpl( + return polymatrix.expression.impl.LinearInExprImpl( underlying=expression, monomials=monomials, variables=variables, @@ -61,7 +61,7 @@ def linear_monomials( variables: ExpressionBaseMixin, ) -> ExpressionBaseMixin: - return polymatrix.expression.implexpressionbase.LinearMonomialsExprImpl( + return polymatrix.expression.impl.LinearMonomialsExprImpl( underlying=expression, variables=variables, ) diff --git a/polymatrix/expressionstate/mixins/expressionstatemixin.py b/polymatrix/expressionstate/mixins/expressionstatemixin.py index 8a8405c..aaaa8ba 100644 --- a/polymatrix/expressionstate/mixins/expressionstatemixin.py +++ b/polymatrix/expressionstate/mixins/expressionstatemixin.py @@ -53,7 +53,10 @@ class ExpressionStateMixin( ) -> 'ExpressionStateMixin': if key is None: - updated_state = dataclasses.replace(self, n_param=self.n_param + n_param) + updated_state = dataclasses.replace( + self, + n_param=self.n_param + n_param, + ) elif key not in self.offset_dict: updated_state = dataclasses.replace( -- cgit v1.2.1