Skip to content

Commit f98d531

Browse files
committed
Updated recipes from chapter 15.
1 parent d6a357d commit f98d531

File tree

5 files changed

+635
-627
lines changed

5 files changed

+635
-627
lines changed
Lines changed: 158 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -1,211 +1,213 @@
11
{
2-
"nbformat": 3,
3-
"nbformat_minor": 0,
2+
"metadata": {
3+
"name": "",
4+
"signature": "sha256:df1c115c86837c0f0c3e0207e7fab7b16aa871306b22ac8b0f0e3d8789926413"
5+
},
6+
"nbformat": 3,
7+
"nbformat_minor": 0,
48
"worksheets": [
59
{
610
"cells": [
711
{
12+
"cell_type": "markdown",
13+
"metadata": [],
814
"source": [
915
"> This is one of the 100 recipes of the [IPython Cookbook](http://ipython-books.github.io/), the definitive guide to high-performance scientific computing and data science in Python.\n"
10-
],
11-
"cell_type": "markdown",
12-
"metadata": []
13-
},
16+
]
17+
},
1418
{
19+
"cell_type": "markdown",
20+
"metadata": {},
1521
"source": [
1622
"# 15.2. Solving equations and inequalities"
17-
],
18-
"cell_type": "markdown",
19-
"metadata": {}
20-
},
21-
{
22-
"cell_type": "code",
23-
"language": "python",
24-
"outputs": [],
25-
"collapsed": false,
23+
]
24+
},
25+
{
26+
"cell_type": "code",
27+
"collapsed": false,
2628
"input": [
27-
"from sympy import *\n",
28-
"init_printing(pretty_print=False)"
29-
],
30-
"metadata": {}
31-
},
32-
{
33-
"cell_type": "code",
34-
"language": "python",
35-
"outputs": [],
36-
"collapsed": false,
29+
"from sympy import *\n",
30+
"init_printing()"
31+
],
32+
"language": "python",
33+
"metadata": {},
34+
"outputs": []
35+
},
36+
{
37+
"cell_type": "code",
38+
"collapsed": false,
3739
"input": [
3840
"var('x y z a')"
39-
],
40-
"metadata": {}
41-
},
42-
{
41+
],
42+
"language": "python",
43+
"metadata": {},
44+
"outputs": []
45+
},
46+
{
47+
"cell_type": "markdown",
48+
"metadata": {},
4349
"source": [
4450
"Use the function solve to resolve equations (the right hand side is always 0)."
45-
],
46-
"cell_type": "markdown",
47-
"metadata": {}
48-
},
49-
{
50-
"cell_type": "code",
51-
"language": "python",
52-
"outputs": [],
53-
"collapsed": false,
51+
]
52+
},
53+
{
54+
"cell_type": "code",
55+
"collapsed": false,
5456
"input": [
5557
"solve(x**2 - a, x)"
56-
],
57-
"metadata": {}
58-
},
59-
{
58+
],
59+
"language": "python",
60+
"metadata": {},
61+
"outputs": []
62+
},
63+
{
64+
"cell_type": "markdown",
65+
"metadata": {},
6066
"source": [
6167
"You can also solve inequations. You may need to specify the domain of your variables. Here, we tell SymPy that x is a real variable."
62-
],
63-
"cell_type": "markdown",
64-
"metadata": {}
65-
},
66-
{
67-
"cell_type": "code",
68-
"language": "python",
69-
"outputs": [],
70-
"collapsed": false,
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"collapsed": false,
7173
"input": [
72-
"x = Symbol('x')\n",
74+
"x = Symbol('x')\n",
7375
"solve_univariate_inequality(x**2 > 4, x)"
74-
],
75-
"metadata": {}
76-
},
77-
{
76+
],
77+
"language": "python",
78+
"metadata": {},
79+
"outputs": []
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
7884
"source": [
7985
"## Systems of equations"
80-
],
81-
"cell_type": "markdown",
82-
"metadata": {}
83-
},
86+
]
87+
},
8488
{
89+
"cell_type": "markdown",
90+
"metadata": {},
8591
"source": [
8692
"This function also accepts systems of equations (here a linear system)."
87-
],
88-
"cell_type": "markdown",
89-
"metadata": {}
90-
},
91-
{
92-
"cell_type": "code",
93-
"language": "python",
94-
"outputs": [],
95-
"collapsed": false,
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"collapsed": false,
9698
"input": [
9799
"solve([x + 2*y + 1, x - 3*y - 2], x, y)"
98-
],
99-
"metadata": {}
100-
},
101-
{
100+
],
101+
"language": "python",
102+
"metadata": {},
103+
"outputs": []
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
102108
"source": [
103109
"Non-linear systems are also supported."
104-
],
105-
"cell_type": "markdown",
106-
"metadata": {}
107-
},
108-
{
109-
"cell_type": "code",
110-
"language": "python",
111-
"outputs": [],
112-
"collapsed": false,
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"collapsed": false,
113115
"input": [
114116
"solve([x**2 + y**2 - 1, x**2 - y**2 - S(1)/2], x, y)"
115-
],
116-
"metadata": {}
117-
},
118-
{
117+
],
118+
"language": "python",
119+
"metadata": {},
120+
"outputs": []
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
119125
"source": [
120126
"Singular linear systems can also be solved (here, there are infinitely many equations because the two equations are colinear)."
121-
],
122-
"cell_type": "markdown",
123-
"metadata": {}
124-
},
125-
{
126-
"cell_type": "code",
127-
"language": "python",
128-
"outputs": [],
129-
"collapsed": false,
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"collapsed": false,
130132
"input": [
131133
"solve([x + 2*y + 1, -x - 2*y - 1], x, y)"
132-
],
133-
"metadata": {}
134-
},
135-
{
134+
],
135+
"language": "python",
136+
"metadata": {},
137+
"outputs": []
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"metadata": {},
136142
"source": [
137143
"Now, let's solve a linear system using matrices with symbolic variables."
138-
],
139-
"cell_type": "markdown",
140-
"metadata": {}
141-
},
142-
{
143-
"cell_type": "code",
144-
"language": "python",
145-
"outputs": [],
146-
"collapsed": false,
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"collapsed": false,
147149
"input": [
148150
"var('a b c d u v')"
149-
],
150-
"metadata": {}
151-
},
152-
{
151+
],
152+
"language": "python",
153+
"metadata": {},
154+
"outputs": []
155+
},
156+
{
157+
"cell_type": "markdown",
158+
"metadata": {},
153159
"source": [
154160
"We create the augmented matrix, which is the horizontal concatenation of the system's matrix with the linear coefficients, and the right-hand side vector."
155-
],
156-
"cell_type": "markdown",
157-
"metadata": {}
158-
},
159-
{
160-
"cell_type": "code",
161-
"language": "python",
162-
"outputs": [],
163-
"collapsed": false,
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"collapsed": false,
164166
"input": [
165167
"M = Matrix([[a, b, u], [c, d, v]]); M"
166-
],
167-
"metadata": {}
168-
},
169-
{
170-
"cell_type": "code",
171-
"language": "python",
172-
"outputs": [],
173-
"collapsed": false,
168+
],
169+
"language": "python",
170+
"metadata": {},
171+
"outputs": []
172+
},
173+
{
174+
"cell_type": "code",
175+
"collapsed": false,
174176
"input": [
175177
"solve_linear_system(M, x, y)"
176-
],
177-
"metadata": {}
178-
},
179-
{
178+
],
179+
"language": "python",
180+
"metadata": {},
181+
"outputs": []
182+
},
183+
{
184+
"cell_type": "markdown",
185+
"metadata": {},
180186
"source": [
181187
"This system needs to be non-singular to have a unique solution, which is equivalent to say that the determinant of the system's matrix needs to be non-zero (otherwise the denominators in the fractions above are equal to zero)."
182-
],
183-
"cell_type": "markdown",
184-
"metadata": {}
185-
},
186-
{
187-
"cell_type": "code",
188-
"language": "python",
189-
"outputs": [],
190-
"collapsed": false,
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"collapsed": false,
191193
"input": [
192194
"det(M[:2,:2])"
193-
],
194-
"metadata": {}
195-
},
196-
{
195+
],
196+
"language": "python",
197+
"metadata": {},
198+
"outputs": []
199+
},
200+
{
201+
"cell_type": "markdown",
202+
"metadata": {},
197203
"source": [
198-
"> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n\n> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (400 pages). [Get a 50% discount by pre-ordering now](http://www.packtpub.com/ipython-interactive-computing-and-visualization-cookbook/book) with the code `mK00gPxQM` (time-limited offer)!\n"
199-
],
200-
"cell_type": "markdown",
201-
"metadata": {}
204+
"> You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).\n",
205+
"\n",
206+
"> [IPython Cookbook](http://ipython-books.github.io/), by [Cyrille Rossant](http://cyrille.rossant.net), Packt Publishing, 2014 (400 pages). [Get a 50% discount by pre-ordering now](http://www.packtpub.com/ipython-interactive-computing-and-visualization-cookbook/book) with the code `mK00gPxQM` (time-limited offer)!\n"
207+
]
202208
}
203-
],
209+
],
204210
"metadata": {}
205211
}
206-
],
207-
"metadata": {
208-
"name": "",
209-
"signature": "sha256:9c7e3e0b97a99628970b81c0399d123c7cf15ac8674737134d7f5b58b8679df3"
210-
}
212+
]
211213
}

0 commit comments

Comments
 (0)