Skip to content

Commit 75b6854

Browse files
committed
added tests as jupyter notebooks
1 parent 83232c4 commit 75b6854

File tree

4 files changed

+864
-1
lines changed

4 files changed

+864
-1
lines changed

readme.ipynb

+53-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
{
22
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "b5473e6d",
6+
"metadata": {},
7+
"source": [
8+
"PyLMI-SDP\n",
9+
"=========\n",
10+
"\n",
11+
"*Symbolic linear matrix inequalities (LMI) and semi-definite programming (SDP) tools for Python*\n",
12+
"\n",
13+
"This package includes a set of classes to represent and manipulate LMIs symbolically using [SymPy](http://sympy.org).\n",
14+
"It also includes tools to export LMIs to [CVXOPT](http://abel.ee.ucla.edu/cvxopt/userguide/coneprog.html#semidefinite-programming) SDP input and to the [SDPA](http://sdpa.sourceforge.net/) format.\n",
15+
"\n",
16+
"Depends on [SymPy](http://sympy.org) and [NumPy](http://www.numpy.org/); and optionally on [CVXOPT](http://cvxopt.org) and on [SciPy](http://www.scipy.org) (for sparse matrices).\n",
17+
"Single codebase supporting both Python 2.7 and Python 3.x.\n",
18+
"PyLMI-SDP is tested for various combinations of Python and sympy. See [here](https://github.com/cdsousa/PyLMI-SDP/actions/workflows/ci.yaml).\n",
19+
"\n",
20+
"PyLMI-SDP is at [GitHub](https://github.com/cdsousa/PyLMI-SDP).\n",
21+
"\n",
22+
"[![Build Status](https://github.com/cdsousa/PyLMI-SDP/actions/workflows/ci.yaml/badge.svg?branch=master)](https://github.com/cdsousa/PyLMI-SDP/actions/workflows/ci.yaml)\n",
23+
"[![Coverage Status](https://coveralls.io/repos/cdsousa/PyLMI-SDP/badge.png?branch=master)](https://coveralls.io/r/cdsousa/PyLMI-SDP?branch=master)"
24+
]
25+
},
326
{
427
"cell_type": "markdown",
528
"id": "0a7fafdf",
@@ -389,10 +412,39 @@
389412
"print(dat)"
390413
]
391414
},
415+
{
416+
"cell_type": "markdown",
417+
"id": "b626817a",
418+
"metadata": {},
419+
"source": [
420+
"Author\n",
421+
"------\n",
422+
"\n",
423+
"[Cristóvão Duarte Sousa](https://github.com/cdsousa)\n",
424+
"\n",
425+
"Install\n",
426+
"-------\n",
427+
"\n",
428+
"From PyPi:\n",
429+
"\n",
430+
" pip install PyLMI-SDP\n",
431+
"\n",
432+
"From git source:\n",
433+
"\n",
434+
" git clone https://github.com/cdsousa/PyLMI-SDP.git\n",
435+
" cd PyLMI-SDP\n",
436+
" python setup.py install\n",
437+
"\n",
438+
"License\n",
439+
"-------\n",
440+
"\n",
441+
"Simplified BSD License. See [License File](LICENSE.txt)\n"
442+
]
443+
},
392444
{
393445
"cell_type": "code",
394446
"execution_count": null,
395-
"id": "12742999",
447+
"id": "80b3b32c",
396448
"metadata": {},
397449
"outputs": [],
398450
"source": []

test_lm.ipynb

+233
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "795391a0",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from sympy import Matrix, zeros, MatAdd, MatMul\n",
11+
"from sympy.abc import x, y, z\n",
12+
"import numpy as np\n",
13+
"from lmi_sdp import NonLinearExpressionError, NonLinearMatrixError, \\\n",
14+
" lin_expr_coeffs, lm_sym_to_coeffs, lm_coeffs_to_sym, lm_sym_expanded"
15+
]
16+
},
17+
{
18+
"cell_type": "markdown",
19+
"id": "e0453e3a",
20+
"metadata": {},
21+
"source": [
22+
"### test_lin_expr_coeffs()"
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"execution_count": 2,
28+
"id": "f88d087f",
29+
"metadata": {},
30+
"outputs": [],
31+
"source": [
32+
"e = 1.2 + 3*x - 4.5*y + z\n",
33+
"coeffs, const = lin_expr_coeffs(e, [x, y, z])\n",
34+
"assert coeffs == [3.0, -4.5, 1.0]\n",
35+
"assert const == 1.2"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"id": "30cdc900",
41+
"metadata": {},
42+
"source": [
43+
"### test_lin_expr_coeffs_exceptions()"
44+
]
45+
},
46+
{
47+
"cell_type": "code",
48+
"execution_count": 3,
49+
"id": "b1a4daa5",
50+
"metadata": {},
51+
"outputs": [],
52+
"source": [
53+
"except_ok = False\n",
54+
"try:\n",
55+
" lin_expr_coeffs(1.2 + x + y*z, [x, y, z])\n",
56+
"except NonLinearExpressionError:\n",
57+
" except_ok = True\n",
58+
"assert except_ok\n",
59+
"\n",
60+
"except_ok = False\n",
61+
"try:\n",
62+
" lin_expr_coeffs(1.2 + x*y, [x])\n",
63+
"except NonLinearExpressionError:\n",
64+
" except_ok = True\n",
65+
"assert except_ok"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"id": "34e44ff5",
71+
"metadata": {},
72+
"source": [
73+
"### test_lm_sym_to_coeffs()"
74+
]
75+
},
76+
{
77+
"cell_type": "code",
78+
"execution_count": 4,
79+
"id": "b37d52a5",
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"m = Matrix([[1.2, x], [3.4*y, 1.2 + 3*x - 4.5*y + z]])\n",
84+
"coeffs = lm_sym_to_coeffs(m, [x, y, z])\n",
85+
"assert len(coeffs) == 2\n",
86+
"assert len(coeffs[0]) == 3\n",
87+
"assert (coeffs[0][0] == np.array([[0.0, 1.0], [0.0, 3.0]])).all()\n",
88+
"assert (coeffs[0][1] == np.array([[0.0, 0.0], [3.4, -4.5]])).all()\n",
89+
"assert (coeffs[0][2] == np.array([[0.0, 0.0], [0.0, 1.0]])).all()\n",
90+
"assert (coeffs[1] == np.array([[1.2, 0.0], [0.0, 1.2]])).all()\n",
91+
"\n",
92+
"assert lm_sym_to_coeffs(Matrix([0.0]), [x, y, z]) == \\\n",
93+
" ([np.array([[0.0]]), np.array([[0.0]]), np.array([[0.0]])],\n",
94+
" np.array([[0.0]]))"
95+
]
96+
},
97+
{
98+
"cell_type": "markdown",
99+
"id": "5cfb7bbf",
100+
"metadata": {},
101+
"source": [
102+
"### test_lm_sym_to_coeffs_sparse()"
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": 5,
108+
"id": "ca9cbbd7",
109+
"metadata": {},
110+
"outputs": [],
111+
"source": [
112+
"try:\n",
113+
" import scipy\n",
114+
"except ImportError: # pragma: no cover\n",
115+
" pass\n",
116+
"else:\n",
117+
" m = Matrix([[1.2, x], [3.4*y, 1.2 + 3*x - 4.5*y + z]])\n",
118+
" coeffs = lm_sym_to_coeffs(m, [x, y, z], sparse=True)\n",
119+
" assert len(coeffs) == 2\n",
120+
" assert len(coeffs[0]) == 3\n",
121+
" assert (coeffs[0][0].toarray() ==\n",
122+
" np.array([[0.0, 1.0], [0.0, 3.0]])).all()\n",
123+
" assert (coeffs[0][1].toarray() ==\n",
124+
" np.array([[0.0, 0.0], [3.4, -4.5]])).all()\n",
125+
" assert (coeffs[0][2].toarray() ==\n",
126+
" np.array([[0.0, 0.0], [0.0, 1.0]])).all()\n",
127+
" assert (coeffs[1].toarray() == np.array([[1.2, 0.0], [0.0, 1.2]])).all()"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"id": "8e7c6bcd",
133+
"metadata": {},
134+
"source": [
135+
"### test_lm_sym_to_coeffs_exceptions()"
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": 6,
141+
"id": "d04b79b5",
142+
"metadata": {},
143+
"outputs": [],
144+
"source": [
145+
"except_ok = False\n",
146+
"try:\n",
147+
" lm_sym_to_coeffs(Matrix([1.2 + x + y*z]), [x, y, z])\n",
148+
"except NonLinearMatrixError:\n",
149+
" except_ok = True\n",
150+
"assert except_ok\n",
151+
"\n",
152+
"except_ok = False\n",
153+
"try:\n",
154+
" lm_sym_to_coeffs(Matrix([1.2 + x*y]), [x])\n",
155+
"except NonLinearMatrixError:\n",
156+
" except_ok = True\n",
157+
"assert except_ok"
158+
]
159+
},
160+
{
161+
"cell_type": "markdown",
162+
"id": "771ff882",
163+
"metadata": {},
164+
"source": [
165+
"### test_lm_coeffs_to_sym()"
166+
]
167+
},
168+
{
169+
"cell_type": "code",
170+
"execution_count": 7,
171+
"id": "d2a2bb3a",
172+
"metadata": {},
173+
"outputs": [],
174+
"source": [
175+
"var_coeffs = [None]*3\n",
176+
"var_coeffs[0] = np.array([[0.0, 1.0], [0.0, 3.0]])\n",
177+
"var_coeffs[1] = np.array([[0.0, 0.0], [3.4, -4.5]])\n",
178+
"var_coeffs[2] = np.array([[0.0, 0.0], [0.0, 1.0]])\n",
179+
"consts = np.array([[1.2, 0.0], [0.0, 1.2]])\n",
180+
"coeffs = (var_coeffs, consts)\n",
181+
"m = Matrix([[1.2, x], [3.4*y, 1.2 + 3*x - 4.5*y + z]])\n",
182+
"assert lm_coeffs_to_sym(coeffs, [x, y, z]) - m == zeros(2)"
183+
]
184+
},
185+
{
186+
"cell_type": "markdown",
187+
"id": "f9a1d7a3",
188+
"metadata": {},
189+
"source": [
190+
"### test_lm_sym_expanded()"
191+
]
192+
},
193+
{
194+
"cell_type": "code",
195+
"execution_count": 8,
196+
"id": "dcba0418",
197+
"metadata": {},
198+
"outputs": [],
199+
"source": [
200+
"m = Matrix([[0, x], [3.4*y, 3*x - 4.5*y + z]])\n",
201+
"c = Matrix([[1.2, 0], [0, 1.2]])\n",
202+
"cx = MatMul(Matrix([[0.0, 1.0], [0.0, 3.0]]), x)\n",
203+
"cy = MatMul(Matrix([[0.0, 0.0], [3.4, -4.5]]), y)\n",
204+
"cz = MatMul(Matrix([[0.0, 0.0], [0.0, 1.0]]), z)\n",
205+
"cc = Matrix([[1.2, 0.0], [0.0, 1.2]])\n",
206+
"assert MatAdd(cx, cy, cz, cc) == lm_sym_expanded(m+c, [x, y, z])\n",
207+
"assert MatAdd(cx, cy, cz) == lm_sym_expanded(m, [x, y, z])\n",
208+
"assert cc == lm_sym_expanded(c, [x, y, z])"
209+
]
210+
}
211+
],
212+
"metadata": {
213+
"kernelspec": {
214+
"display_name": "py38",
215+
"language": "python",
216+
"name": "py38"
217+
},
218+
"language_info": {
219+
"codemirror_mode": {
220+
"name": "ipython",
221+
"version": 3
222+
},
223+
"file_extension": ".py",
224+
"mimetype": "text/x-python",
225+
"name": "python",
226+
"nbconvert_exporter": "python",
227+
"pygments_lexer": "ipython3",
228+
"version": "3.8.12"
229+
}
230+
},
231+
"nbformat": 4,
232+
"nbformat_minor": 5
233+
}

0 commit comments

Comments
 (0)