diff options
-rw-r--r-- | polymatrix/expression/__init__.py | 8 | ||||
-rw-r--r-- | polymatrix/expression/expression.py | 78 | ||||
-rw-r--r-- | polymatrix/expression/from_.py | 6 | ||||
-rw-r--r-- | polymatrix/expression/impl.py (renamed from polymatrix/expression/implexpressionbase.py) | 5 | ||||
-rw-r--r-- | polymatrix/expression/init.py (renamed from polymatrix/expression/initexpressionbase.py) | 90 | ||||
-rw-r--r-- | polymatrix/expression/mixins/fromtupleexprmixin.py | 2 | ||||
-rw-r--r-- | polymatrix/expression/mixins/parametrizeexprmixin.py | 44 | ||||
-rw-r--r-- | polymatrix/expression/mixins/productexprmixin.py | 1 | ||||
-rw-r--r-- | polymatrix/expression/op.py | 12 | ||||
-rw-r--r-- | polymatrix/expressionstate/mixins/expressionstatemixin.py | 5 |
10 files changed, 134 insertions, 117 deletions
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/implexpressionbase.py b/polymatrix/expression/impl.py index 79761f2..3e61941 100644 --- a/polymatrix/expression/implexpressionbase.py +++ b/polymatrix/expression/impl.py @@ -125,6 +125,9 @@ class DivisionExprImpl(DivisionExprMixin): 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): @@ -235,7 +238,7 @@ class ParametrizeExprImpl(ParametrizeExprMixin): name: str def __repr__(self) -> str: - return f'{self.__class__.__name__}(name={self.name})' + return f'{self.__class__.__name__}(name={self.name}, underlying={self.underlying})' @dataclassabc.dataclassabc(frozen=True) diff --git a/polymatrix/expression/initexpressionbase.py b/polymatrix/expression/init.py index 1a3e52c..7a4ea36 100644 --- a/polymatrix/expression/initexpressionbase.py +++ b/polymatrix/expression/init.py @@ -2,14 +2,14 @@ import typing import numpy as np import sympy -import polymatrix.expression.implexpressionbase +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.implexpressionbase import FromTupleExprImpl, AdditionExprImpl +from polymatrix.expression.impl import FromTupleExprImpl, AdditionExprImpl def init_addition_expr( @@ -27,7 +27,7 @@ def init_addition_expr( def init_block_diag_expr( underlying: tuple, ): - return polymatrix.expression.implexpressionbase.BlockDiagExprImpl( + return polymatrix.expression.impl.BlockDiagExprImpl( underlying=underlying, ) @@ -35,7 +35,7 @@ def init_block_diag_expr( def init_cache_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.CacheExprImpl( + return polymatrix.expression.impl.CacheExprImpl( underlying=underlying, ) @@ -47,7 +47,7 @@ def init_combinations_expr( if isinstance(degrees, int): degrees = (degrees,) - return polymatrix.expression.implexpressionbase.CombinationsExprImpl( + return polymatrix.expression.impl.CombinationsExprImpl( expression=expression, degrees=degrees, ) @@ -65,7 +65,7 @@ def init_combinations_expr( # if introduce_derivatives is None: # introduce_derivatives = False -# return polymatrix.expression.implexpressionbase.DerivativeExprImpl( +# return polymatrix.expression.impl.DerivativeExprImpl( # underlying=underlying, # variables=variables, # introduce_derivatives=introduce_derivatives, @@ -76,7 +76,7 @@ def init_combinations_expr( def init_determinant_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.DeterminantExprImpl( + return polymatrix.expression.impl.DeterminantExprImpl( underlying=underlying, ) @@ -84,7 +84,7 @@ def init_determinant_expr( def init_diag_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.DiagExprImpl( + return polymatrix.expression.impl.DiagExprImpl( underlying=underlying, ) @@ -93,7 +93,7 @@ def init_divergence_expr( underlying: ExpressionBaseMixin, variables: tuple, ): - return polymatrix.expression.implexpressionbase.DivergenceExprImpl( + return polymatrix.expression.impl.DivergenceExprImpl( underlying=underlying, variables=variables, ) @@ -104,7 +104,7 @@ def init_division_expr( right: ExpressionBaseMixin, stack: tuple[FrameSummary], ): - return polymatrix.expression.implexpressionbase.DivisionExprImpl( + return polymatrix.expression.impl.DivisionExprImpl( left=left, right=right, stack=stack, @@ -116,7 +116,7 @@ def init_elem_mult_expr( right: ExpressionBaseMixin, stack: tuple[FrameSummary], ): - return polymatrix.expression.implexpressionbase.ElemMultExprImpl( + return polymatrix.expression.impl.ElemMultExprImpl( left=left, right=right, ) @@ -148,7 +148,7 @@ def init_eval_expr( substitutions = tuple((variable, formatted_values(value)) for variable, value in substitutions) - return polymatrix.expression.implexpressionbase.EvalExprImpl( + return polymatrix.expression.impl.EvalExprImpl( underlying=underlying, substitutions=substitutions, ) @@ -157,7 +157,7 @@ def init_eval_expr( def init_eye_expr( variable: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.EyeExprImpl( + return polymatrix.expression.impl.EyeExprImpl( variable=variable, ) @@ -170,7 +170,7 @@ def init_filter_expr( if inverse is None: inverse = False - return polymatrix.expression.implexpressionbase.FilterExprImpl( + return polymatrix.expression.impl.FilterExprImpl( underlying=underlying, predicator=predicator, inverse=inverse, @@ -180,7 +180,7 @@ def init_filter_expr( def init_from_symmetric_matrix_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.FromSymmetricMatrixExprImpl( + return polymatrix.expression.impl.FromSymmetricMatrixExprImpl( underlying=underlying, ) @@ -273,7 +273,7 @@ def init_from_terms_expr( # Expression needs to be hashable terms_formatted = tuple((key, tuple(monomials.items())) for key, monomials in gen_terms) - return polymatrix.expression.implexpressionbase.FromTermsExprImpl( + return polymatrix.expression.impl.FromTermsExprImpl( terms=terms_formatted, shape=shape, ) @@ -286,13 +286,13 @@ def init_get_item_expr( def get_hashable_slice(index): if isinstance(index, slice): - return polymatrix.expression.implexpressionbase.GetItemExprImpl.Slice(start=index.start, stop=index.stop, step=index.step) + 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.implexpressionbase.GetItemExprImpl( + return polymatrix.expression.impl.GetItemExprImpl( underlying=underlying, index=proper_index, ) @@ -303,7 +303,7 @@ def init_half_newton_polytope_expr( variables: ExpressionBaseMixin, filter: ExpressionBaseMixin | None = None, ): - return polymatrix.expression.implexpressionbase.HalfNewtonPolytopeExprImpl( + return polymatrix.expression.impl.HalfNewtonPolytopeExprImpl( monomials=monomials, variables=variables, filter=filter @@ -318,7 +318,7 @@ def init_half_newton_polytope_expr( # ): # assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' -# return polymatrix.expression.implexpressionbase.LinearInExprImpl( +# return polymatrix.expression.impl.LinearInExprImpl( # underlying=underlying, # monomials=monomials, # variables=variables, @@ -330,7 +330,7 @@ def init_linear_matrix_in_expr( underlying: ExpressionBaseMixin, variable: int, ): - return polymatrix.expression.implexpressionbase.LinearMatrixInExprImpl( + return polymatrix.expression.impl.LinearMatrixInExprImpl( underlying=underlying, variable=variable, ) @@ -343,7 +343,7 @@ def init_linear_matrix_in_expr( # assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' -# return polymatrix.expression.implexpressionbase.LinearMonomialsExprImpl( +# return polymatrix.expression.impl.LinearMonomialsExprImpl( # underlying=underlying, # variables=variables, # ) @@ -354,7 +354,7 @@ def init_matrix_mult_expr( right: ExpressionBaseMixin, stack: tuple[FrameSummary], ): - return polymatrix.expression.implexpressionbase.MatrixMultExprImpl( + return polymatrix.expression.impl.MatrixMultExprImpl( left=left, right=right, stack=stack, @@ -364,7 +364,7 @@ def init_matrix_mult_expr( def init_max_degree_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.MaxDegreeExprImpl( + return polymatrix.expression.impl.MaxDegreeExprImpl( underlying=underlying, ) @@ -372,7 +372,7 @@ def init_max_degree_expr( def init_max_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.MaxExprImpl( + return polymatrix.expression.impl.MaxExprImpl( underlying=underlying, ) @@ -384,7 +384,7 @@ def init_parametrize_expr( if name is None: name = 'undefined' - return polymatrix.expression.implexpressionbase.ParametrizeExprImpl( + return polymatrix.expression.impl.ParametrizeExprImpl( underlying=underlying, name=name, ) @@ -394,7 +394,7 @@ def init_parametrize_matrix_expr( underlying: ExpressionBaseMixin, name: str, ): - return polymatrix.expression.implexpressionbase.ParametrizeMatrixExprImpl( + return polymatrix.expression.impl.ParametrizeMatrixExprImpl( underlying=underlying, name=name, ) @@ -409,7 +409,7 @@ def init_quadratic_in_expr( assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' - return polymatrix.expression.implexpressionbase.QuadraticInExprImpl( + return polymatrix.expression.impl.QuadraticInExprImpl( underlying=underlying, monomials=monomials, variables=variables, @@ -424,7 +424,7 @@ def init_quadratic_monomials_expr( assert isinstance(variables, ExpressionBaseMixin), f'{variables=}' - return polymatrix.expression.implexpressionbase.QuadraticMonomialsExprImpl( + return polymatrix.expression.impl.QuadraticMonomialsExprImpl( underlying=underlying, variables=variables, ) @@ -434,7 +434,7 @@ def init_rep_mat_expr( underlying: ExpressionBaseMixin, repetition: tuple, ): - return polymatrix.expression.implexpressionbase.RepMatExprImpl( + return polymatrix.expression.impl.RepMatExprImpl( underlying=underlying, repetition=repetition, ) @@ -444,7 +444,7 @@ def init_reshape_expr( underlying: ExpressionBaseMixin, new_shape: tuple, ): - return polymatrix.expression.implexpressionbase.ReshapeExprImpl( + return polymatrix.expression.impl.ReshapeExprImpl( underlying=underlying, new_shape=new_shape, ) @@ -455,7 +455,7 @@ def init_set_element_at_expr( index: tuple, value: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.SetElementAtExprImpl( + return polymatrix.expression.impl.SetElementAtExprImpl( underlying=underlying, index=index, value=value, @@ -465,7 +465,7 @@ def init_set_element_at_expr( def init_squeeze_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.SqueezeExprImpl( + return polymatrix.expression.impl.SqueezeExprImpl( underlying=underlying, ) @@ -488,14 +488,14 @@ def init_substitute_expr( else: expr = init_from_expr(value) - return polymatrix.expression.implexpressionbase.ReshapeExprImpl( + 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.implexpressionbase.SubstituteExprImpl( + return polymatrix.expression.impl.SubstituteExprImpl( underlying=underlying, substitutions=substitutions, ) @@ -505,7 +505,7 @@ def init_subtract_monomials_expr( underlying: ExpressionBaseMixin, monomials: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.SubtractMonomialsExprImpl( + return polymatrix.expression.impl.SubtractMonomialsExprImpl( underlying=underlying, monomials=monomials, ) @@ -514,7 +514,7 @@ def init_subtract_monomials_expr( def init_sum_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.SumExprImpl( + return polymatrix.expression.impl.SumExprImpl( underlying=underlying, ) @@ -522,7 +522,7 @@ def init_sum_expr( def init_symmetric_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.SymmetricExprImpl( + return polymatrix.expression.impl.SymmetricExprImpl( underlying=underlying, ) @@ -530,7 +530,7 @@ def init_symmetric_expr( def init_to_constant_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.ToConstantExprImpl( + return polymatrix.expression.impl.ToConstantExprImpl( underlying=underlying, ) @@ -538,7 +538,7 @@ def init_to_constant_expr( def init_to_quadratic_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.ToQuadraticExprImpl( + return polymatrix.expression.impl.ToQuadraticExprImpl( underlying=underlying, ) @@ -546,7 +546,7 @@ def init_to_quadratic_expr( def init_to_sorted_variables( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.ToSortedVariablesImpl( + return polymatrix.expression.impl.ToSortedVariablesImpl( underlying=underlying, ) @@ -554,7 +554,7 @@ def init_to_sorted_variables( def init_to_symmetric_matrix_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.ToSymmetricMatrixExprImpl( + return polymatrix.expression.impl.ToSymmetricMatrixExprImpl( underlying=underlying, ) @@ -562,7 +562,7 @@ def init_to_symmetric_matrix_expr( def init_transpose_expr( underlying: ExpressionBaseMixin, ): - return polymatrix.expression.implexpressionbase.TransposeExprImpl( + return polymatrix.expression.impl.TransposeExprImpl( underlying=underlying, ) @@ -579,7 +579,7 @@ def init_truncate_expr( if inverse is None: inverse = False - return polymatrix.expression.implexpressionbase.TruncateExprImpl( + return polymatrix.expression.impl.TruncateExprImpl( underlying=underlying, variables=variables, degrees=degrees, @@ -599,6 +599,6 @@ def init_truncate_expr( # else: # yield init_from_(e) -# return polymatrix.expression.implexpressionbase.VStackExprImpl( +# return polymatrix.expression.impl.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( |