summaryrefslogtreecommitdiffstats
path: root/polymatrix/expression/mixins/divisionexprmixin.py
blob: e23645685ea3f8464679ee211fd3f9a8b2b1ba36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import abc
import dataclasses

from polymatrix.expression.mixins.elemmultexprmixin import ElemMultExprMixin
from polymatrix.utils.getstacklines import FrameSummary
from polymatrix.utils.tooperatorexception import to_operator_exception
from polymatrix.polymatrix.init.initpolymatrix import init_poly_matrix
from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin
from polymatrix.polymatrix.polymatrix import PolyMatrix
from polymatrix.expressionstate.expressionstate import ExpressionState


class DivisionExprMixin(ExpressionBaseMixin):
    @property
    @abc.abstractmethod
    def left(self) -> ExpressionBaseMixin:
        ...

    @property
    @abc.abstractmethod
    def right(self) -> ExpressionBaseMixin:
        ...

    @property
    @abc.abstractmethod
    def stack(self) -> tuple[FrameSummary]:
        ...

    # overwrites abstract method of `ExpressionBaseMixin`
    def apply(
        self, 
        state: ExpressionState,
    ) -> tuple[ExpressionState, PolyMatrix]:

        state, left = self.left.apply(state=state)
        state, right = self.right.apply(state=state)

        # if left.shape == (1, 1):
        #     left, right = right, left

        # assert right.shape == (1, 1)

        if not (right.shape == (1, 1)):
            raise AssertionError(to_operator_exception(
                message=f'{right.shape=} is not (1, 1)',
                stack=self.stack,
            ))

        right_poly = right.get_poly(0, 0)

        if len(right_poly) == 1 and tuple() in right_poly:
            right_inv = {(0, 0): {tuple(): 1/right_poly[tuple()]}}
            return ElemMultExprMixin.elem_mult(state, left, right)

        # add an auxillary equation and, therefore, needs to be cached
        if self in state.cache:
            return state, state.cache[self]

        terms = {}

        division_variable = state.n_param
        state = state.register(n_param=1)

        for row in range(left.shape[0]):
            for col in range(left.shape[1]):

                underlying_terms = left.get_poly(row, col)
                if underlying_terms is None:
                    continue

                def gen_monomial_terms():
                    for monomial, value in underlying_terms.items():
                        yield monomial + ((division_variable, 1),), value

                terms[row, col] = dict(gen_monomial_terms())

        def gen_auxillary_terms():
            for monomial, value in right_poly.items():
                yield monomial + ((division_variable, 1),), value

        auxillary_terms = dict(gen_auxillary_terms())

        if tuple() not in auxillary_terms:
            auxillary_terms[tuple()] = 0

        auxillary_terms[tuple()] -= 1

        poly_matrix = init_poly_matrix(
            terms=terms,
            shape=left.shape,
        )

        state = dataclasses.replace(
            state, 
            auxillary_equations=state.auxillary_equations | {division_variable: auxillary_terms},
            cache=state.cache | {self: poly_matrix},
        )

        return state, poly_matrix