You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"> 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
+
},
14
18
{
19
+
"cell_type": "markdown",
20
+
"metadata": {},
15
21
"source": [
16
22
"# 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,
26
28
"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,
37
39
"input": [
38
40
"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": {},
43
49
"source": [
44
50
"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,
54
56
"input": [
55
57
"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": {},
60
66
"source": [
61
67
"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,
71
73
"input": [
72
-
"x = Symbol('x')\n",
74
+
"x = Symbol('x')\n",
73
75
"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": {},
78
84
"source": [
79
85
"## Systems of equations"
80
-
],
81
-
"cell_type": "markdown",
82
-
"metadata": {}
83
-
},
86
+
]
87
+
},
84
88
{
89
+
"cell_type": "markdown",
90
+
"metadata": {},
85
91
"source": [
86
92
"This function also accepts systems of equations (here a linear system)."
"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,
130
132
"input": [
131
133
"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": {},
136
142
"source": [
137
143
"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,
147
149
"input": [
148
150
"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": {},
153
159
"source": [
154
160
"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,
164
166
"input": [
165
167
"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,
174
176
"input": [
175
177
"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": {},
180
186
"source": [
181
187
"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,
191
193
"input": [
192
194
"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": {},
197
203
"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"
0 commit comments