summaryrefslogtreecommitdiffstats
path: root/polymatrix/__init__.py
blob: de3f9f3f3883e8e222b3aae27d385347c6b9274f (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
import collections
import dataclasses
import itertools
import math
import typing
import numpy as np
import scipy.sparse
import sympy

from polymatrix.expression.expression import Expression
from polymatrix.expression.mixins.expressionbasemixin import ExpressionBaseMixin
from polymatrix.expression.mixins.expressionmixin import ExpressionMixin
from polymatrix.expression.mixins.parametrizeexprmixin import ParametrizeExprMixin
from polymatrix.expressionstate.expressionstate import ExpressionState
from polymatrix.expression.init.initblockdiagexpr import init_block_diag_expr
from polymatrix.expression.init.initexpression import init_expression
from polymatrix.expression.init.initeyeexpr import init_eye_expr
from polymatrix.expression.init.initfromsympyexpr import init_from_sympy_expr
from polymatrix.expression.init.initfromtermsexpr import init_from_terms_expr
from polymatrix.expression.init.initvstackexpr import init_v_stack_expr
from polymatrix.polymatrix.polymatrix import PolyMatrix
from polymatrix.expression.utils.getvariableindices import get_variable_indices, get_variable_indices_from_variable
from polymatrix.statemonad.init.initstatemonad import init_state_monad
from polymatrix.statemonad.mixins.statemonadmixin import StateMonadMixin
from polymatrix.expression.utils.monomialtoindex import monomial_to_index
from polymatrix.expressionstate.init.initexpressionstate import init_expression_state as original_init_expression_state
from polymatrix.statemonad.statemonad import StateMonad


def init_expression_state():
    return original_init_expression_state()

def from_sympy(
    data: tuple[tuple[float]],
):
    return init_expression(
        init_from_sympy_expr(data)
    )

def from_state_monad(
    data: StateMonad,
):
    return init_expression(
        data.flat_map(lambda inner_data: init_from_sympy_expr(inner_data)),
    )

def from_polymatrix(
    polymatrix: PolyMatrix,
):
    return init_expression(
        init_from_terms_expr(polymatrix)
    )

def from_(
    data: tuple[tuple[float]],
):
    return from_sympy(data)

def v_stack(
    expressions: tuple[Expression],
):
    return init_expression(
        init_v_stack_expr(expressions)
    )

def h_stack(
    expressions: tuple[Expression],
):
    return init_expression(
        init_v_stack_expr(tuple(expr.T for expr in expressions))
    ).T

def block_diag(
    expressions: tuple[Expression],
):
    return init_expression(
        init_block_diag_expr(expressions)
    )

def eye(
    variable: tuple[Expression],
):
    return init_expression(
        init_eye_expr(variable=variable)
    )

def kkt_equality(
    variables: Expression,
    equality: Expression = None,
):

    self_variables = variables
    self_equality = equality

    def func(state: ExpressionState):

        state, equality = self_equality.apply(state=state)

        state, equality_der = self_equality.diff(
            self_variables, 
            introduce_derivatives=True,
        ).apply(state)

        def acc_nu_variables(acc, v):
            state, nu_variables = acc

            nu_variable = state.n_param
            state = state.register(n_param=1)
            
            return state, nu_variables + [nu_variable]

        *_, (state, nu_variables) = tuple(itertools.accumulate(
            range(equality.shape[0]), 
            acc_nu_variables, 
            initial=(state, []),
        ))

        terms = {}

        for row in range(equality_der.shape[1]):

            monomial_terms = collections.defaultdict(float)

            for eq_idx, nu_variable in enumerate(nu_variables):

                underlying_terms = equality_der.get_poly(eq_idx, row)
                if underlying_terms is None:
                    continue

                for monomial, value in underlying_terms.items():
                    new_monomial = monomial + (nu_variable,)

                    monomial_terms[new_monomial] += value

            # terms[row, 0] = dict(monomial_terms)
            terms[row, 0] = monomial_terms

        cost_expr = init_expression(init_from_terms_expr(
            terms=terms,
            shape=(equality_der.shape[1], 1),
        ))

        nu_terms = {}

        for eq_idx, nu_variable in enumerate(nu_variables):
            nu_terms[eq_idx, 0] = {(nu_variable,): 1}

        nu_expr = init_expression(init_from_terms_expr(
            terms=nu_terms,
            shape=(len(nu_variables), 1),
        ))

        return state, (nu_expr, cost_expr)   

    return init_state_monad(func)

def kkt_inequality(
    variables: Expression,
    inequality: Expression = None,
):

    self_variables = variables
    self_inequality = inequality

    def func(state: ExpressionState):

        state, inequality = self_inequality.apply(state=state)

        state, inequality_der = self_inequality.diff(
            self_variables, 
            introduce_derivatives=True,
        ).apply(state)

        def acc_lambda_variables(acc, v):
            state, lambda_variables = acc

            lambda_variable = state.n_param
            state = state.register(n_param=3)
            
            return state, lambda_variables + [lambda_variable]

        *_, (state, lambda_variables) = tuple(itertools.accumulate(
            range(inequality.shape[0]), 
            acc_lambda_variables, 
            initial=(state, []),
        ))

        terms = {}

        for row in range(inequality_der.shape[1]):

            monomial_terms = collections.defaultdict(float)

            for inequality_idx, lambda_variable in enumerate(lambda_variables):

                polynomial = inequality_der.get_poly(inequality_idx, row)
                if polynomial is None:
                    continue

                for monomial, value in polynomial.items():
                    new_monomial = monomial + (lambda_variable,)

                    monomial_terms[new_monomial] += value

            # terms[row, 0] = dict(monomial_terms)
            terms[row, 0] = monomial_terms

        cost_expr = init_expression(init_from_terms_expr(
            terms=terms,
            shape=(inequality_der.shape[1], 1),
        ))

        # inequality, dual feasibility, complementary slackness
        # -----------------------------------------------------

        inequality_terms = {}
        feasibility_terms = {}
        complementary_terms = {}

        for inequality_idx, lambda_variable in enumerate(lambda_variables):
            r_lambda = lambda_variable + 1
            r_inequality = lambda_variable + 2

            polynomial = inequality.get_poly(inequality_idx, 0)
            if polynomial is None:
                continue

            # f(x) <= -0.01
            inequality_terms[inequality_idx, 0] = polynomial | {(r_inequality, r_inequality): 1}

            # dual feasibility, lambda >= 0
            feasibility_terms[inequality_idx, 0] = {(lambda_variable,): 1, (r_lambda, r_lambda): -1}

            # complementary slackness
            complementary_terms[inequality_idx, 0] = {(r_lambda, r_inequality): 1}

        inequality_expr = init_expression(init_from_terms_expr(
            terms=inequality_terms,
            shape=(len(lambda_variables), 1),
        ))

        feasibility_expr = init_expression(init_from_terms_expr(
            terms=feasibility_terms,
            shape=(len(lambda_variables), 1),
        ))

        complementary_expr = init_expression(init_from_terms_expr(
            terms=complementary_terms,
            shape=(len(lambda_variables), 1),
        ))

        # lambda expression
        # -----------------

        terms = {}
        for inequality_idx, lambda_variable in enumerate(lambda_variables):
            terms[inequality_idx, 0] = {(lambda_variable,): 1}

        lambda_expr = init_expression(init_from_terms_expr(
            terms=terms,
            shape=(len(lambda_variables), 1),
        ))

        return state, (lambda_expr, cost_expr, inequality_expr, feasibility_expr, complementary_expr)   

    return init_state_monad(func)

def rows(
    expr: Expression,
) -> StateMonadMixin[ExpressionState, np.ndarray]:

    def func(state: ExpressionState):
        state, underlying = expr.apply(state)

        def gen_row_terms():
            for row in range(underlying.shape[0]):

                terms = {}

                for col in range(underlying.shape[1]):
                    polynomial = underlying.get_poly(row, col)
                    if polynomial == None:
                        continue

                    terms[0, col] = polynomial

                yield init_expression(underlying=init_from_terms_expr(
                    terms=terms,
                    shape=(1, underlying.shape[1])
                ))

        row_terms = tuple(gen_row_terms())

        return state, row_terms

    return init_state_monad(func)

def shape(
    expr: Expression,
) -> StateMonadMixin[ExpressionState, tuple[int, ...]]:
    def func(state: ExpressionState):
        state, polymatrix = expr.apply(state)

        return state, polymatrix.shape

    return init_state_monad(func)


@dataclasses.dataclass
class MatrixBuffer:
    data: dict[int, ...]
    n_row: int
    n_param: int

    def get_max_degree(self):
        return max(degree for degree in self.data.keys())

    def add_buffer(self, index: int):
        if index <= 1:
            buffer = np.zeros((self.n_row, self.n_param**index), dtype=np.double)

        else:
            buffer = scipy.sparse.dok_array((self.n_row, self.n_param**index), dtype=np.double)
            
        self.data[index] = buffer

    def add(self, row: int, col: int, index: int, value: float):
        if index not in self.data:
            self.add_buffer(index)

        self.data[index][row, col] = value

    def __getitem__(self, key):
        if key not in self.data:
            self.add_buffer(key)

        return self.data[key]


@dataclasses.dataclass
class MatrixRepresentations:
    data: tuple[MatrixBuffer, ...]
    aux_data: typing.Optional[MatrixBuffer]
    variable_mapping: tuple[int, ...]
    state: ExpressionState

    def merge_matrix_equations(self):
        def gen_matrices(index: int):
            for equations in self.data:
                if index < len(equations):
                    yield equations[index]

            if index < len(self.aux_data):
                yield self.aux_data[index]

        indices = set(key for equations in self.data + (self.aux_data,) for key in equations.keys())

        def gen_matrices():
            for index in indices:
                if index <= 1:
                    yield index, np.vstack(tuple(gen_matrices(index)))
                else:
                    yield index, scipy.sparse.vstack(tuple(gen_matrices(index)))

        return dict(gen_matrices())

    def get_value(self, variable, value):
        variable_indices = get_variable_indices_from_variable(self.state, variable)[1]

        def gen_value_index():
            for variable_index in variable_indices:
                try:
                    yield self.variable_mapping.index(variable_index)
                except ValueError:
                    raise ValueError(f'{variable_index} not found in {self.variable_mapping}')

        value_index = list(gen_value_index())

        return value[value_index]

    def set_value(self, variable, value):
        variable_indices = get_variable_indices_from_variable(self.state, variable)[1]
        value_index = list(self.variable_mapping.index(variable_index) for variable_index in variable_indices)
        vec = np.zeros(len(self.variable_mapping))
        vec[value_index] = value
        return vec

    def get_func(self, eq_idx: int):
        equations = self.data[eq_idx].data
        max_idx = max(equations.keys())

        if 2 <= max_idx:
            def func(x: np.ndarray) -> np.ndarray:
                if isinstance(x, tuple) or isinstance(x, list):
                    x = np.array(x).reshape(-1, 1)

                def acc_x_powers(acc, _):
                    next = (acc @ x.T).reshape(-1, 1)
                    return next

                x_powers = tuple(itertools.accumulate(
                    range(max_idx - 1),
                    acc_x_powers,
                    initial=x,
                ))[1:]

                def gen_value():
                    for idx, equation in equations.items():
                        if idx == 0:
                            yield equation

                        elif idx == 1:
                            yield equation @ x

                        else:
                            yield equation @ x_powers[idx-2]

                return sum(gen_value())

        else:
            def func(x: np.ndarray) -> np.ndarray:
                def gen_value():
                    for idx, equation in equations.items():
                        if idx == 0:
                            yield equation

                        else:
                            yield equation @ x

                return sum(gen_value())

        return func

def to_matrix_repr(
    expressions: Expression | tuple[Expression],
    variables: Expression,
) -> StateMonadMixin[ExpressionState, tuple[tuple[tuple[np.ndarray, ...], ...], tuple[int, ...]]]:

    if isinstance(expressions, Expression):
        expressions = (expressions,)

    assert isinstance(variables, ExpressionBaseMixin), f'{variables=}'

    def func(state: ExpressionState):

        def acc_underlying_application(acc, v):
            state, underlying_list = acc

            state, underlying = v.apply(state)

            assert underlying.shape[1] == 1, f'{underlying.shape[1]=} is not 1'
            
            return state, underlying_list + (underlying,)

        *_, (state, underlying_list) = tuple(itertools.accumulate(
            expressions, 
            acc_underlying_application, 
            initial=(state, tuple()),
        ))

        state, ordered_variable_index = get_variable_indices_from_variable(state, variables)

        assert len(ordered_variable_index) == len(set(ordered_variable_index)), f'{ordered_variable_index=} contains repeated variables'

        variable_index_map = {old: new for new, old in enumerate(ordered_variable_index)}

        n_param = len(ordered_variable_index)

        def gen_underlying_matrices():
            for underlying in underlying_list:

                n_row = underlying.shape[0]

                buffer = MatrixBuffer(
                    data={},
                    n_row=n_row,
                    n_param=n_param,
                )

                for row in range(n_row):
                    underlying_terms = underlying.get_poly(row, 0)
                    if underlying_terms is None:
                        continue
                    
                    for monomial, value in underlying_terms.items():
                        
                        def gen_new_monomial():
                            for var, count in monomial:
                                try:
                                    index = variable_index_map[var]
                                except KeyError:
                                    raise KeyError(f'{var=} ({state.get_key_from_offset(var)}) is incompatible with {variable_index_map=}')

                                for _ in range(count):
                                    yield index

                        new_monomial = tuple(gen_new_monomial())

                        cols = monomial_to_index(n_param, new_monomial)

                        col_value = value / len(cols)

                        for col in cols:
                            buffer.add(row, col, sum(count for _, count in monomial), col_value)
                
                yield buffer

        underlying_matrices = tuple(gen_underlying_matrices())

        def gen_auxillary_equations():
            for key, monomial_terms in state.auxillary_equations.items():
                if key in ordered_variable_index:
                    yield key, monomial_terms

        auxillary_equations = tuple(gen_auxillary_equations())

        n_row = len(auxillary_equations)

        if n_row == 0:
            auxillary_matrix_equations = None

        else:
            buffer = MatrixBuffer(
                data={},
                n_row=n_row,
                n_param=n_param,
            )

            for row, (key, monomial_terms) in enumerate(auxillary_equations):
                for monomial, value in monomial_terms.items():
                    new_monomial = tuple(variable_index_map[var] for var, count in monomial for _ in range(count))

                    cols = monomial_to_index(n_param, new_monomial)

                    col_value = value / len(cols)

                    for col in cols:
                        buffer.add(row, col, sum(count for _, count in monomial), col_value)

            auxillary_matrix_equations = buffer

        result = MatrixRepresentations(
            data=underlying_matrices,
            aux_data=auxillary_matrix_equations,
            variable_mapping=ordered_variable_index,
            state=state,
        )

        return state, result

    return init_state_monad(func)

def to_constant_repr(
    expr: Expression,
    assert_constant: bool = True,
) -> StateMonadMixin[ExpressionState, np.ndarray]:

    def func(state: ExpressionState):
        state, underlying = expr.apply(state)

        A = np.zeros(underlying.shape, dtype=np.double)

        for (row, col), polynomial in underlying.gen_terms():
            for monomial, value in polynomial.items():
                if len(monomial) == 0:
                    A[row, col] = value
                
                elif assert_constant:
                    raise Exception(f'non-constant term {monomial=}')

        return state, A

    return init_state_monad(func)


def degrees(
    expr: Expression,
    variables: Expression,
) -> StateMonadMixin[ExpressionState, np.ndarray]:

    def func(state: ExpressionState):
        state, underlying = expr.apply(state)
        state, variable_indices = get_variable_indices_from_variable(state, variables)

        def gen_rows():
            for row in range(underlying.shape[0]):
                def gen_cols():
                    for col in range(underlying.shape[1]):
                        
                        def gen_degrees():
                            polynomial = underlying.get_poly(row, col)

                            if polynomial is None:
                                yield 0

                            else:
                                for monomial, _ in polynomial.items():
                                    yield sum(count for var, count in monomial if var in variable_indices)

                        yield tuple(set(gen_degrees()))

                yield tuple(gen_cols())

        return state, tuple(gen_rows())

    return init_state_monad(func)


def to_sympy_repr(
    expr: Expression,
) -> StateMonadMixin[ExpressionState, sympy.Expr]:

    def func(state: ExpressionState):
        state, underlying = expr.apply(state)

        A = np.zeros(underlying.shape, dtype=object)

        for (row, col), polynomial in underlying.gen_terms():

            sympy_polynomial = 0

            for monomial, value in polynomial.items():
                sympy_monomial = 1

                for offset, count in monomial:

                    variable = state.get_key_from_offset(offset)
                    # def get_variable_from_offset(offset: int):
                    #     for variable, (start, end) in state.offset_dict.items():
                    #         if start <= offset < end:
                                # assert end - start == 1, f'{start=}, {end=}, {variable=}'

                    if isinstance(variable, sympy.core.symbol.Symbol):
                        variable_name = variable.name
                    elif isinstance(variable, ParametrizeExprMixin):
                        variable_name = variable.name
                    elif isinstance(variable, str):
                        variable_name = variable
                    else:
                        raise Exception(f'{variable=}')

                    start, end = state.offset_dict[variable]

                    if end - start == 1:
                        sympy_var = sympy.Symbol(variable_name)
                    else:
                        sympy_var = sympy.Symbol(f'{variable_name}_{offset - start + 1}')

                    # var = get_variable_from_offset(offset)
                    sympy_monomial *= sympy_var**count

                sympy_polynomial += value * sympy_monomial

            A[row, col] = sympy_polynomial

        return state, A

    return init_state_monad(func)