Skip to content

Commit 3e682e5

Browse files
committed
Generate Pelican site
0 parents  commit 3e682e5

File tree

491 files changed

+35320
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

491 files changed

+35320
-0
lines changed

101-analyzing-the-frequency-components-of-a-signal-with-a-fast-fourier-transform/index.html

+345
Large diffs are not rendered by default.

102-applying-a-linear-filter-to-a-digital-signal/index.html

+327
Large diffs are not rendered by default.

103-computing-the-autocorrelation-of-a-time-series/index.html

+339
Large diffs are not rendered by default.

11-introducing-ipython-and-the-jupyter-notebook/index.html

+337
Large diffs are not rendered by default.

111-manipulating-the-exposure-of-an-image/index.html

+214
Large diffs are not rendered by default.

112-applying-filters-on-an-image/index.html

+304
Large diffs are not rendered by default.

113-segmenting-an-image/index.html

+341
Large diffs are not rendered by default.

114-finding-points-of-interest-in-an-image/index.html

+284
Large diffs are not rendered by default.

115-detecting-faces-in-an-image-with-opencv/index.html

+206
Large diffs are not rendered by default.

116-applying-digital-filters-to-speech-sounds/index.html

+253
Large diffs are not rendered by default.

117-creating-a-sound-synthesizer-in-the-notebook/index.html

+271
Large diffs are not rendered by default.

12-getting-started-with-exploratory-data-analysis-in-the-jupyter-notebook/index.html

+265
Large diffs are not rendered by default.

121-plotting-the-bifurcation-diagram-of-a-chaotic-dynamical-system/index.html

+334
Large diffs are not rendered by default.

122-simulating-an-elementary-cellular-automaton/index.html

+271
Large diffs are not rendered by default.

123-simulating-an-ordinary-differential-equation-with-scipy/index.html

+294
Large diffs are not rendered by default.

124-simulating-a-partial-differential-equation-reaction-diffusion-systems-and-turing-patterns/index.html

+336
Large diffs are not rendered by default.

13-introducing-the-multidimensional-array-in-numpy-for-fast-array-computations/index.html

+343
Large diffs are not rendered by default.

131-simulating-a-discrete-time-markov-chain/index.html

+304
Large diffs are not rendered by default.

132-simulating-a-poisson-process/index.html

+295
Large diffs are not rendered by default.

133-simulating-a-brownian-motion/index.html

+249
Large diffs are not rendered by default.

134-simulating-a-stochastic-differential-equation/index.html

+301
Large diffs are not rendered by default.

14-creating-an-ipython-extension-with-custom-magic-commands/index.html

+279
Large diffs are not rendered by default.

141-manipulating-and-visualizing-graphs-with-networkx/index.html

+266
Large diffs are not rendered by default.

142-drawing-flight-routes-with-networkx/index.html

+324
Large diffs are not rendered by default.

143-resolving-dependencies-in-a-directed-acyclic-graph-with-a-topological-sort/index.html

+296
Large diffs are not rendered by default.

144-computing-connected-components-in-an-image/index.html

+269
Large diffs are not rendered by default.

145-computing-the-voronoi-diagram-of-a-set-of-points/index.html

+394
Large diffs are not rendered by default.

146-manipulating-geospatial-data-with-cartopy/index.html

+233
Large diffs are not rendered by default.

147-creating-a-route-planner-for-a-road-network/index.html

+350
Large diffs are not rendered by default.

15-mastering-ipythons-configuration-system/index.html

+300
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta name="description" content="IPython Cookbook, ">
7+
8+
9+
<!-- FAVICON -->
10+
<link rel="apple-touch-icon" sizes="57x57" href="/apple-touch-icon-57x57.png">
11+
<link rel="apple-touch-icon" sizes="114x114" href="/apple-touch-icon-114x114.png">
12+
<link rel="apple-touch-icon" sizes="72x72" href="/apple-touch-icon-72x72.png">
13+
<link rel="apple-touch-icon" sizes="144x144" href="/apple-touch-icon-144x144.png">
14+
<link rel="apple-touch-icon" sizes="60x60" href="/apple-touch-icon-60x60.png">
15+
<link rel="apple-touch-icon" sizes="120x120" href="/apple-touch-icon-120x120.png">
16+
<link rel="apple-touch-icon" sizes="76x76" href="/apple-touch-icon-76x76.png">
17+
<link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon-152x152.png">
18+
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon-180x180.png">
19+
<link rel="icon" type="image/png" href="/favicon-192x192.png" sizes="192x192">
20+
<link rel="icon" type="image/png" href="/favicon-160x160.png" sizes="160x160">
21+
<link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96">
22+
<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
23+
<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
24+
<meta name="msapplication-TileColor" content="#da532c">
25+
<meta name="msapplication-TileImage" content="/mstile-144x144.png">
26+
27+
28+
<link rel="alternate" href="http://ipython-books.github.io/feeds/all.atom.xml" type="application/atom+xml" title="IPython Cookbook Full Atom Feed"/>
29+
30+
<title>IPython Cookbook - 15.1. Diving into symbolic computing with SymPy</title>
31+
32+
<link href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
33+
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pure/0.3.0/pure-min.css">
34+
<!--[if lte IE 8]>
35+
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/grids-responsive-old-ie-min.css">
36+
<![endif]-->
37+
<!--[if gt IE 8]><!-->
38+
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/grids-responsive-min.css">
39+
<!--<![endif]-->
40+
<link rel="stylesheet" href="http://ipython-books.github.io/theme/css/styles.css">
41+
<link rel="stylesheet" href="http://ipython-books.github.io/theme/css/pygments.css">
42+
<!-- <link href='http://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'> -->
43+
<link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,500" rel="stylesheet" type="text/css">
44+
<link href='http://fonts.googleapis.com/css?family=Ubuntu+Mono' rel='stylesheet' type='text/css'>
45+
46+
47+
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
48+
</head>
49+
50+
<body>
51+
52+
53+
<header id="header" class="pure-g">
54+
<div class="pure-u-1 pure-u-md-3-4">
55+
<div id="menu">
56+
<div class="pure-menu pure-menu-open pure-menu-horizontal">
57+
<ul>
58+
<li><a href="/">home</a></li>
59+
<li><a href="https://github.com/ipython-books/cookbook-2nd-code">Jupyter notebooks</a></li>
60+
<li><a href="https://github.com/ipython-books/minibook-2nd-code">minibook</a></li>
61+
<li><a href="http://cyrille.rossant.net">author</a></li>
62+
</ul> </div>
63+
</div>
64+
</div>
65+
66+
<div class="pure-u-1 pure-u-md-1-4">
67+
<div id="social">
68+
<div class="pure-menu pure-menu-open pure-menu-horizontal">
69+
<ul>
70+
<li><a href="https://twitter.com/cyrillerossant"><i class="fa fa-twitter"></i></a></li>
71+
<li><a href="https://github.com/ipython-books/cookbook-2nd"><i class="fa fa-github"></i></a></li>
72+
</ul> </div>
73+
</div>
74+
</div>
75+
</header>
76+
77+
78+
79+
<div id="layout" class="pure-g">
80+
<section id="content" class="pure-u-1 pure-u-md-4-4">
81+
<div class="l-box">
82+
83+
<header id="page-header">
84+
<h1>15.1. Diving into symbolic computing with SymPy</h1>
85+
</header>
86+
87+
<section id="page">
88+
<p><a href="/"><img src="https://raw.githubusercontent.com/ipython-books/cookbook-2nd/master/cover-cookbook-2nd.png" align="left" alt="IPython Cookbook, Second Edition" height="130" style="margin-right: 20px; margin-bottom: 10px;" /></a> <em>This is one of the 100+ free recipes of the <a href="/">IPython Cookbook, Second Edition</a>, by <a href="http://cyrille.rossant.net">Cyrille Rossant</a>, a guide to numerical computing and data science in the Jupyter Notebook. The ebook and printed book are available for purchase at <a href="https://www.packtpub.com/big-data-and-business-intelligence/ipython-interactive-computing-and-visualization-cookbook-second-e">Packt Publishing</a>.</em></p>
89+
<p>▶&nbsp;&nbsp;<em><a href="https://github.com/ipython-books/cookbook-2nd">Text on GitHub</a> with a <a href="https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode">CC-BY-NC-ND license</a></em><br />
90+
▶&nbsp;&nbsp;<em><a href="https://github.com/ipython-books/cookbook-2nd-code">Code on GitHub</a> with a <a href="https://opensource.org/licenses/MIT">MIT license</a></em></p>
91+
<p>▶&nbsp;&nbsp;<a href="http://ipython-books.github.io/chapter-15-symbolic-and-numerical-mathematics/"><strong><em>Go to</em></strong> <em>Chapter 15 : Symbolic and Numerical Mathematics</em></a><br />
92+
▶&nbsp;&nbsp;<a href="https://github.com/ipython-books/cookbook-2nd-code/blob/master/chapter15_symbolic/01_sympy_intro.ipynb"><em><strong>Get</strong> the Jupyter notebook</em></a> </p>
93+
<p>In this recipe, we will give a brief introduction to symbolic computing with SymPy. We will see more advanced features of SymPy in the next recipes.</p>
94+
<h2>Getting ready</h2>
95+
<p>Anaconda should come with SymPy by default, but you can always install it with <code>conda install sympy</code>.</p>
96+
<h2>How to do it...</h2>
97+
<p>SymPy can be used from a Python module, or interactively in Jupyter/IPython. In the Notebook, all mathematical expressions are displayed with LaTeX, thanks to the MathJax JavaScript library.</p>
98+
<p>Here is an introduction to SymPy:</p>
99+
<p><strong>1.&nbsp;</strong> First, we import SymPy and enable LaTeX printing in the Jupyter Notebook:</p>
100+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="o">*</span>
101+
<span class="n">init_printing</span><span class="p">()</span>
102+
</pre></div>
103+
</div>
104+
105+
106+
<p><strong>2.&nbsp;</strong> To deal with symbolic variables, we first need to declare them:</p>
107+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="n">var</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">)</span>
108+
</pre></div>
109+
</div>
110+
111+
112+
<p><img alt="(x, y)" src="http://ipython-books.github.io/pages/chapter15_symbolic/01_sympy_intro_files/01_sympy_intro_10_0.png" /></p>
113+
<p><strong>3.&nbsp;</strong> The <code>var()</code> function creates symbols and injects them into the namespace. This function should only be used in the interactive mode. In a Python module, it is better to use the <code>symbols()</code> function that returns the symbols:</p>
114+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="n">x</span><span class="p">,</span> <span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;x y&#39;</span><span class="p">)</span>
115+
</pre></div>
116+
</div>
117+
118+
119+
<p><strong>4.&nbsp;</strong> We can create mathematical expressions with these symbols:</p>
120+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="n">expr1</span> <span class="o">=</span> <span class="p">(</span><span class="n">x</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">**</span> <span class="mi">2</span>
121+
<span class="n">expr2</span> <span class="o">=</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">x</span> <span class="o">+</span> <span class="mi">1</span>
122+
</pre></div>
123+
</div>
124+
125+
126+
<p><strong>5.&nbsp;</strong> Are these expressions equal?</p>
127+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="n">expr1</span> <span class="o">==</span> <span class="n">expr2</span>
128+
</pre></div>
129+
</div>
130+
131+
132+
<div class="highlight highlight-text"><div class="highlight"><pre><span></span>False
133+
</pre></div>
134+
</div>
135+
136+
137+
<p><strong>6.&nbsp;</strong> These expressions are mathematically equal, but not syntactically identical. To test whether they are mathematically equal, we can ask SymPy to simplify the difference algebraically:</p>
138+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="n">simplify</span><span class="p">(</span><span class="n">expr1</span> <span class="o">-</span> <span class="n">expr2</span><span class="p">)</span>
139+
</pre></div>
140+
</div>
141+
142+
143+
<p><img alt="0" src="http://ipython-books.github.io/pages/chapter15_symbolic/01_sympy_intro_files/01_sympy_intro_18_0.png" /></p>
144+
<p><strong>7.&nbsp;</strong> A very common operation with symbolic expressions is the substitution of a symbol by another symbol, expression, or a number, using the subs() method of a symbolic expression:</p>
145+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="n">expr1</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">expr1</span><span class="p">)</span>
146+
</pre></div>
147+
</div>
148+
149+
150+
<p><img alt="Output" src="http://ipython-books.github.io/pages/chapter15_symbolic/01_sympy_intro_files/01_sympy_intro_20_0.png" /></p>
151+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="n">expr1</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">pi</span><span class="p">)</span>
152+
</pre></div>
153+
</div>
154+
155+
156+
<p><img alt="Output" src="http://ipython-books.github.io/pages/chapter15_symbolic/01_sympy_intro_files/01_sympy_intro_21_0.png" /></p>
157+
<p><strong>8.&nbsp;</strong> A rational number cannot be written simply as <code>1/2</code> as this Python expression evaluates to 0.5. A possibility is to convert the number <code>1</code> into a SymPy integer object, for example by using the <code>S()</code> function:</p>
158+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="n">expr1</span><span class="o">.</span><span class="n">subs</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">S</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>
159+
</pre></div>
160+
</div>
161+
162+
163+
<p><img alt="9/4" src="http://ipython-books.github.io/pages/chapter15_symbolic/01_sympy_intro_files/01_sympy_intro_23_0.png" /></p>
164+
<p><strong>9.&nbsp;</strong> Exactly represented numbers can be evaluated numerically with <code>evalf()</code>:</p>
165+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="n">_</span><span class="o">.</span><span class="n">evalf</span><span class="p">()</span>
166+
</pre></div>
167+
</div>
168+
169+
170+
<p><img alt="2.25000000000000" src="http://ipython-books.github.io/pages/chapter15_symbolic/01_sympy_intro_files/01_sympy_intro_25_0.png" /></p>
171+
<p><strong>10.&nbsp;</strong> We can easily create a Python function from a SymPy symbolic expression using the <code>lambdify()</code> function. The resulting function can notably be evaluated on NumPy arrays. This is quite convenient when we need to go from the symbolic world to the numerical world:</p>
172+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="n">lambdify</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">expr1</span><span class="p">)</span>
173+
</pre></div>
174+
</div>
175+
176+
177+
<div class="highlight highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
178+
<span class="n">f</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="o">-</span><span class="mf">2.</span><span class="p">,</span> <span class="mf">2.</span><span class="p">,</span> <span class="mi">5</span><span class="p">))</span>
179+
</pre></div>
180+
</div>
181+
182+
183+
<div class="highlight highlight-text"><div class="highlight"><pre><span></span>array([ 1., 0., 1., 4., 9.])
184+
</pre></div>
185+
</div>
186+
187+
188+
<h2>How it works...</h2>
189+
<p>A core idea in SymPy is to use the standard Python syntax to manipulate exact expressions. Although this is very convenient and natural, there are a few caveats. Symbols such as <code>x</code>, which represent mathematical variables, cannot be used in Python before being instantiated (otherwise, a <code>NameError</code> exception is thrown by the interpreter). This is in contrast to most other computer algebra systems. For this reason, SymPy offers ways to declare symbolic variables beforehand.</p>
190+
<p>Another example is integer division; as <code>1/2</code> evaluates to <code>0.5</code> (or 0 in Python 2), SymPy has no way to know that the user intended to write a fraction instead. We need to convert the numerical integer 1 to the symbolic integer <code>1</code> before dividing it by 2.</p>
191+
<p>Also, the Python equality refers to the equality between syntax trees rather than between mathematical expressions.</p>
192+
<h2>See also</h2>
193+
<ul>
194+
<li>Solving equations and inequalities</li>
195+
<li>Getting started with Sage</li>
196+
</ul>
197+
</section>
198+
199+
</div>
200+
</section>
201+
202+
<footer id="footer" class="pure-u-1 pure-u-md-4-4">
203+
<div class="l-box">
204+
<div>
205+
<p>&copy; <a href="http://cyrille.rossant.net">Cyrille Rossant</a> &ndash;
206+
Built with <a href="https://github.com/PurePelicanTheme/pure-single">Pure Theme</a>
207+
for <a href="http://blog.getpelican.com/">Pelican</a>
208+
</p>
209+
</div>
210+
</div>
211+
</footer>
212+
213+
</div>
214+
215+
<!-- Start of StatCounter Code for Default Guide -->
216+
<script type="text/javascript">
217+
var sc_project=9752080;
218+
var sc_invisible=1;
219+
var sc_security="837928c1";
220+
var scJsHost = (("https:" == document.location.protocol) ?
221+
"https://secure." : "http://www.");
222+
document.write("<sc"+"ript type='text/javascript' src='" +
223+
scJsHost+
224+
"statcounter.com/counter/counter.js'></"+"script>");
225+
</script>
226+
<noscript><div class="statcounter"><a title="hit counter"
227+
href="http://statcounter.com/free-hit-counter/"
228+
target="_blank"><img class="statcounter"
229+
src="http://c.statcounter.com/9752080/0/837928c1/1/"
230+
alt="hit counter"></a></div></noscript>
231+
<!-- End of StatCounter Code for Default Guide -->
232+
</body>
233+
</html>
234+

0 commit comments

Comments
 (0)