summaryrefslogtreecommitdiffstats
path: root/README.md
blob: 5c56079bf8dd1995a443193cfb8fb6a3fcc99e07 (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
# Multivariate polynomial library

`polymatrix` is a library to represent and operate on multivariate polynomials. 

It is used to define Sum Of Squares optimization problems.

## Key aspects

Some aspects of the library:

* it has a lazy behavior
    * the library implements operators to build polynomial expression
    * the actual polynomial is created by calling the [`apply(state)`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/expression/mixins/expressionbasemixin.py) method on the polynomial expression
* a `sympy` expression is converted to a polynomial expression using the [`polymatrix.from_sympy`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/__init__.py) function
* multiple polynomial expressions are combined using functions like 
    * [`polymatrix.v_stack`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/__init__.py)
    * [`polymatrix.block_diag`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/__init__.py).
* polynomial expressions are manipulated using methods like 
    * [`diff`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/expression/mixins/expressionmixin.py), 
    * [`reshape`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/expression/mixins/expressionmixin.py)
    * [`substitute`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/expression/mixins/expressionmixin.py)
    * [`sum`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/expression/mixins/expressionmixin.py)
    * [`to_constant`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/expression/mixins/expressionmixin.py)
* an expression is converted to matrix representation using [`polymatrix.to_matrix_repr`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/__init__.py). To get the actual representation, the [`apply(state)`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/statemonad/mixins/statemonadmixin.py) method needs to be called.

## Example

In this example, two polynomial expressions are defined using sympy expressions

f_1(x_1, x_2) = x_1 + x_2

f_2(x_1, x_2) = x_1 + x_1 x_2

Then, the two expression are combined using the [`__add__`](https://gitlab.nccr-automation.ch/michael.schneeberger/polymatrix/-/blob/main/polymatrix/expression/mixins/expressionmixin.py) (or equivalently `+`) method

f_3(x_1, x_2) = f_1(x_1, x_2) + f_2(x_1, x_2) = 2 x_1 + x_2 + x_1 x_2

Finally, different representations of the polynomial are printed.

``` python
import sympy
import polymatrix

state = polymatrix.init_expression_state()

x1, x2 = sympy.symbols('x1, x2')
x = polymatrix.from_((x1, x2))

f1 = polymatrix.from_(x1 + x2)
f2 = polymatrix.from_(x1 + x1*x2)

f3 = f1 + f2          

# prints the data structure of the expression
# AdditionExprImpl(left=FromSympyExprImpl(data=((x1 + x2,),)), right=FromSympyExprImpl(data=((x1*x2 + x1,),)))
print(f3)

state, poly_matrix = f3.apply(state)

# prints the data structure of the polynomial matrix
# PolyMatrixImpl(terms={(0, 0): {((0, 1), (1, 1)): 1, ((0, 1),): 2, ((1, 1),): 1}}, shape=(1, 1))
print(poly_matrix)

state, matrix_repr = polymatrix.to_matrix_repr((f3,), x).apply(state)

# prints the numpy matrix representations of the polynomial matrix
# array([[2., 1.]])
# array([[0. , 0.5, 0.5, 0. ]])
print(matrix_repr.data[0][1])               # numpy array
print(matrix_repr.data[0][2].toarray())     # sparse scipy array
```