summaryrefslogtreecommitdiffstats
path: root/polymatrix/expression/mixins/oldlinearexprmixin.py
blob: 1bdebd96634c8e6955ea6d8f9a1a7fe0425f250d (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import abc
import collections
from numpy import var

from polymatrix.expression.init.initpolymatrix import init_poly_matrix
from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin
from polymatrix.expression.polymatrix import PolyMatrix
from polymatrix.expression.expressionstate import ExpressionState
from polymatrix.expression.utils.getvariableindices import get_variable_indices


class OldLinearExprMixin(ExpressionBaseMixin):
    @property
    @abc.abstractmethod
    def underlying(self) -> ExpressionBaseMixin:
        ...

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

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

        state, variable_indices = get_variable_indices(state, self.variables)

        underlying_terms = underlying.get_terms()

        def gen_variable_terms():
            for _, monomial_term in underlying_terms:
                for monomial in monomial_term.keys():

                    x_monomial = tuple(var_idx for var_idx in monomial if var_idx in variable_indices)
                    yield x_monomial

        variable_terms = tuple(set(gen_variable_terms()))

        terms = {}

        for (row, _), monomial_term in underlying_terms:

            x_monomial_terms = collections.defaultdict(lambda: collections.defaultdict(float))

            for monomial, value in monomial_term.items():

                x_monomial = tuple(var_idx for var_idx in monomial if var_idx in variable_indices)
                p_monomial = tuple(var_idx for var_idx in monomial if var_idx not in variable_indices)

                assert tuple(sorted(x_monomial)) == x_monomial, f'{x_monomial} is not sorted'

                x_monomial_terms[x_monomial][p_monomial] += value

            for x_monomial, data in x_monomial_terms.items():
                terms[row, variable_terms.index(x_monomial)] = dict(data)

        poly_matrix = init_poly_matrix(
            terms=terms,
            shape=(underlying.shape[0], len(variable_terms)),
        )

        return state, poly_matrix   


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

        #         try:
        #             underlying_terms = underlying.get_poly(row, col)
        #         except KeyError:
        #             continue

        #         x_monomial_terms = collections.defaultdict(lambda: collections.defaultdict(float))

        #         for monomial, value in underlying_terms.items():

        #             x_monomial = tuple(var_idx for var_idx in monomial if var_idx in variable_indices)
        #             p_monomial = tuple(var_idx for var_idx in monomial if var_idx not in variable_indices)

        #             assert tuple(sorted(x_monomial)) == x_monomial, f'{x_monomial} is not sorted'

        #             x_monomial_terms[x_monomial][p_monomial] += value

        #         for data in x_monomial_terms.values():
        #             terms[idx_row, 0] = dict(data)
        #             idx_row += 1

        # poly_matrix = init_poly_matrix(
        #     terms=terms,
        #     shape=(idx_row, 1),
        # )

        # return state, poly_matrix   

        # terms = {}
        # idx_row = 0

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

        #         try:
        #             underlying_terms = underlying.get_poly(row, col)
        #         except KeyError:
        #             continue

        #         x_monomial_terms = collections.defaultdict(lambda: collections.defaultdict(float))

        #         for monomial, value in underlying_terms.items():

        #             x_monomial = tuple(var_idx for var_idx in monomial if var_idx in variable_indices)
        #             p_monomial = tuple(var_idx for var_idx in monomial if var_idx not in variable_indices)

        #             assert tuple(sorted(x_monomial)) == x_monomial, f'{x_monomial} is not sorted'

        #             x_monomial_terms[x_monomial][p_monomial] += value

        #         for data in x_monomial_terms.values():
        #             terms[idx_row, 0] = dict(data)
        #             idx_row += 1

        # poly_matrix = init_poly_matrix(
        #     terms=terms,
        #     shape=(idx_row, 1),
        # )

        # return state, poly_matrix