Implemented crisscross algorithm for solving LP problems.
[sympycore.git] / doc / html / demo0_2.html
blob503eb4b289d91cf422453210e686ed52b473b107
1 <?xml version="1.0" encoding="utf-8" ?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
4 <head>
5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6 <meta name="generator" content="Docutils 0.4.1: http://docutils.sourceforge.net/" />
7 <title>SympyCore Release 0.2 Demo</title>
8 <meta name="authors" content="Pearu Peterson" />
9 <style type="text/css">
12 :Author: Pearu Peterson
13 :Contact: pearu.peterson AT gmail DOT com
14 :Copyright: This stylesheet has been placed in the public domain.
16 Stylesheet for use with Docutils.
19 @import url(html4css1.css);
21 /* Your customizations go here. For example: */
23 body {
24 margin:0px;
25 padding:25px;
26 background-color: #ccc;
27 font-size: 13px; font-family: arial, sans-serif;
28 line-height:1.5em;
31 div.document {
32 max-width: 700px;
33 color: #000;
34 background-color: #fff;
35 padding:25px;
36 border:5px solid #ddd;
39 div.sidebar {
40 margin-left: 1em ;
41 border: medium outset ;
42 padding: 1em ;
43 background-color: #ffffee ;
44 width: 40% ;
45 float: right ;
46 clear: right }
48 div.sidebar p.rubric {
49 font-family: sans-serif ;
50 font-size: medium }
52 p.sidebar-title {
53 font-family: sans-serif ;
54 font-weight: bold ;
55 font-size: larger }
57 p.sidebar-subtitle {
58 font-family: sans-serif ;
59 font-weight: bold }
61 pre, tt {
62 font-family: consolas, lucida console, courier new, monospace;
63 color: #000080 ;
66 pre.literal-block, pre.doctest-block {
67 line-height:1.3em;
68 border-top:1px solid #ccc;
69 border-bottom:1px solid #ccc;
70 background-color:#f0f0f0;
71 color: #000080 ;
74 div.admonition, div.attention, div.caution, div.danger, div.error,
75 div.hint, div.important, div.note, div.tip, div.warning {
76 margin: 2em ;
77 border: medium outset ;
78 padding: 0.5em;
79 background-color: #fffacd;
82 div.admonition p.admonition-title, div.hint p.admonition-title,
83 div.important p.admonition-title, div.note p.admonition-title,
84 div.tip p.admonition-title {
85 font-weight: bold ;
86 font-family: sans-serif }
88 div.attention p.admonition-title, div.caution p.admonition-title,
89 div.danger p.admonition-title, div.error p.admonition-title,
90 div.warning p.admonition-title {
91 color: red ;
92 font-weight: bold ;
93 font-family: sans-serif }
95 </style>
96 </head>
97 <body>
98 <div class="document" id="sympycore-release-0-2-demo">
99 <h1 class="title">SympyCore Release 0.2 Demo</h1>
100 <table class="docinfo" frame="void" rules="none">
101 <col class="docinfo-name" />
102 <col class="docinfo-content" />
103 <tbody valign="top">
104 <tr><th class="docinfo-name">Authors:</th>
105 <td>Pearu Peterson</td></tr>
106 <tr class="field"><th class="docinfo-name">Created:</th><td class="field-body">February 2008</td>
107 </tr>
108 <tr class="field"><th class="docinfo-name">Website:</th><td class="field-body"><a class="reference" href="http://sympycore.googlecode.com/">http://sympycore.googlecode.com/</a></td>
109 </tr>
110 <tr><th class="docinfo-name">Version:</th>
111 <td>sympycore 0.2-svn.
112 Other versions: <a class="reference" href="http://sympycore.googlecode.com/svn-history/r818/trunk/doc/html/demo0_1.html">0.1</a>.</td></tr>
113 </tbody>
114 </table>
115 <!-- -*- rest -*- -->
116 <div class="sidebar">
117 <p class="first sidebar-title">Table of contents</p>
118 <div class="contents local last topic">
119 <ul class="simple">
120 <li><a class="reference" href="#getting-started" id="id3" name="id3">Getting started</a></li>
121 <li><a class="reference" href="#constructing-symbolic-expressions" id="id4" name="id4">Constructing symbolic expressions</a></li>
122 <li><a class="reference" href="#manipulations-with-symbolic-expressions" id="id5" name="id5">Manipulations with symbolic expressions</a></li>
123 <li><a class="reference" href="#transformation-methods" id="id6" name="id6">Transformation methods</a></li>
124 <li><a class="reference" href="#arithmetic-methods" id="id7" name="id7">Arithmetic methods</a></li>
125 <li><a class="reference" href="#calculus-methods" id="id8" name="id8">Calculus methods</a></li>
126 <li><a class="reference" href="#polynomial-rings" id="id9" name="id9">Polynomial rings</a></li>
127 <li><a class="reference" href="#matrix-rings" id="id10" name="id10">Matrix rings</a></li>
128 <li><a class="reference" href="#physical-units" id="id11" name="id11">Physical units</a></li>
129 </ul>
130 </div>
131 </div>
132 <div class="section">
133 <h1><a class="toc-backref" href="#id3" id="getting-started" name="getting-started">Getting started</a></h1>
134 <p>This document gives a short overview of SympyCore basic features. For
135 more information see the <a class="reference" href="http://sympycore.googlecode.com/svn/trunk/doc/html/userguide.html">SympyCore User's Guide</a>.</p>
136 <p>To use the <tt class="docutils literal"><span class="pre">sympycore</span></tt> package from Python, one must import it:</p>
137 <pre class="doctest-block">
138 &gt;&gt;&gt; from sympycore import *
139 </pre>
140 </div>
141 <div class="section">
142 <h1><a class="toc-backref" href="#id4" id="constructing-symbolic-expressions" name="constructing-symbolic-expressions">Constructing symbolic expressions</a></h1>
143 <p>Generally speaking, symbolic expressions consist of symbols and
144 operation between them. To create symbolic expressions using
145 SympyCore, one can either create symbol objects and perform operations
146 between them:</p>
147 <pre class="doctest-block">
148 &gt;&gt;&gt; x = Symbol('x')
149 &gt;&gt;&gt; y = Symbol('y')
150 &gt;&gt;&gt; z = Symbol('z')
151 &gt;&gt;&gt; x + y
152 Calculus('x + y')
153 </pre>
154 <p>or one can use symbolic expression parser to construct symbolic
155 expressions from a string:</p>
156 <pre class="doctest-block">
157 &gt;&gt;&gt; Calculus('x + y')
158 Calculus('x + y')
159 </pre>
160 <p>SympyCore converts symbolic expressions to a canonical form that is
161 efficient for further manipulations and are also often obvious
162 simplifications that users may expect:</p>
163 <pre class="doctest-block">
164 &gt;&gt;&gt; x + x
165 Calculus('2*x')
166 </pre>
167 <pre class="doctest-block">
168 &gt;&gt;&gt; x - x
169 Calculus('0')
170 </pre>
171 <p>General symbolic arithmetic expressions are instances of the Calculus
172 class. Using the <tt class="docutils literal"><span class="pre">print</span></tt> statement (or <tt class="docutils literal"><span class="pre">str()</span></tt>) hides this information:</p>
173 <pre class="doctest-block">
174 &gt;&gt;&gt; print x + y
175 x + y
176 </pre>
177 </div>
178 <div class="section">
179 <h1><a class="toc-backref" href="#id5" id="manipulations-with-symbolic-expressions" name="manipulations-with-symbolic-expressions">Manipulations with symbolic expressions</a></h1>
180 <p>The most obvious manipulation task applied to symbolic expressions, is
181 substitution -- replacing a sub-expression of a given expression with a
182 new expression. For example,</p>
183 <pre class="doctest-block">
184 &gt;&gt;&gt; expr = x + y
185 &gt;&gt;&gt; print expr.subs(y, sin(x))
186 x + sin(x)
187 </pre>
188 <p>Other tasks include accessing parts of symbolic expressions:</p>
189 <pre class="doctest-block">
190 &gt;&gt;&gt; sorted(expr.args)
191 [Calculus('x'), Calculus('y')]
192 </pre>
193 <p>and constructing new expressions:</p>
194 <pre class="doctest-block">
195 &gt;&gt;&gt; print Mul(*expr.args)
197 </pre>
198 <p>An important presumption for implementing various algorithms is pattern
199 matching. Pattern matching means that given a pattern expression, the
200 pattern match method should first decide whether an expression can be
201 expressed in a form that the pattern defines, and second. it should
202 return information what sub-expression parts correspond to the pattern
203 sub-expressions. For example, given a pattern</p>
204 <pre class="doctest-block">
205 &gt;&gt;&gt; w = Symbol('w')
206 &gt;&gt;&gt; pattern = x * w ** 3
207 </pre>
208 <p>where symbol <tt class="docutils literal"><span class="pre">w</span></tt> is assumed to match any sub-expression, then expressions</p>
209 <pre class="doctest-block">
210 &gt;&gt;&gt; expr1 = x*sin(y)**3
211 &gt;&gt;&gt; expr2 = x*(x+y)**3
212 </pre>
213 <p>do match the given pattern:</p>
214 <pre class="doctest-block">
215 &gt;&gt;&gt; d1 = expr1.match(pattern, w)
216 &gt;&gt;&gt; print d1
217 {Calculus('w'): Calculus('sin(y)')}
218 </pre>
219 <pre class="doctest-block">
220 &gt;&gt;&gt; d2 = expr2.match(pattern, w)
221 &gt;&gt;&gt; print d2
222 {Calculus('w'): Calculus('x + y')}
223 </pre>
224 <p>The result of <tt class="docutils literal"><span class="pre">match</span></tt> method, when the match is found, is a dictionary
225 with the property</p>
226 <pre class="doctest-block">
227 &gt;&gt;&gt; pattern.subs(d1.items())==expr1
228 True
229 &gt;&gt;&gt; pattern.subs(d2.items())==expr2
230 True
231 </pre>
232 <p>If no match is found, then the <tt class="docutils literal"><span class="pre">match</span></tt> returns <tt class="docutils literal"><span class="pre">None</span></tt>:</p>
233 <pre class="doctest-block">
234 &gt;&gt;&gt; print (y*x**2).match(pattern, w)
235 None
236 </pre>
237 </div>
238 <div class="section">
239 <h1><a class="toc-backref" href="#id6" id="transformation-methods" name="transformation-methods">Transformation methods</a></h1>
240 <p>The most common transformation task is expansion of sub-expressions by
241 opening parenthesis:</p>
242 <pre class="doctest-block">
243 &gt;&gt;&gt; expr = (x+y)*z
244 &gt;&gt;&gt; print expr
245 z*(x + y)
246 &gt;&gt;&gt; print expr.expand()
247 x*z + y*z
248 </pre>
249 <p>In general, the <tt class="docutils literal"><span class="pre">expand</span></tt> method expands products of sums and
250 integer powers of sums:</p>
251 <pre class="doctest-block">
252 &gt;&gt;&gt; expr = (x+y)*(1+x)**3
253 &gt;&gt;&gt; print expr.expand()
254 x + y + x**4 + 3*x**2 + 3*x**3 + 3*x*y + 3*y*x**2 + y*x**3
255 </pre>
256 </div>
257 <div class="section">
258 <h1><a class="toc-backref" href="#id7" id="arithmetic-methods" name="arithmetic-methods">Arithmetic methods</a></h1>
259 <p>SympyCore provides exact rational and complex numbers:</p>
260 <pre class="doctest-block">
261 &gt;&gt;&gt; Calculus('3/12')
262 Calculus('1/4')
263 &gt;&gt;&gt; print (2 + 3*I/4)**4
264 721/256 + 165/8*I
265 </pre>
266 <p>Fractional powers of integers are evaluated to simpler
267 expressions when possible:</p>
268 <pre class="doctest-block">
269 &gt;&gt;&gt; Calculus('8**(1/3)')
270 Calculus('2')
271 &gt;&gt;&gt; Calculus('243**(1/5)')
272 Calculus('3')
273 </pre>
274 <p>SympyCore supports converting symbolic expressions with exact numbers
275 such as integers and rational numbers to expressions with arbitrary
276 precision floating-point numbers:</p>
277 <pre class="doctest-block">
278 &gt;&gt;&gt; expr = 2*pi + E**x
279 &gt;&gt;&gt; print expr
280 E**x + 2*pi
281 &gt;&gt;&gt; print expr.evalf(5)
282 6.2832 + 2.7183**x
283 &gt;&gt;&gt; print expr.evalf(25)
284 6.283185307179586476925287 + 2.718281828459045235360287**x
285 </pre>
286 </div>
287 <div class="section">
288 <h1><a class="toc-backref" href="#id8" id="calculus-methods" name="calculus-methods">Calculus methods</a></h1>
289 <p>SympyCore provides methods to differentiate symbolic expressions:</p>
290 <pre class="doctest-block">
291 &gt;&gt;&gt; expr = x+sin(x*y)*x
292 &gt;&gt;&gt; print expr.diff(x)
293 1 + sin(x*y) + x*y*cos(x*y)
294 </pre>
295 <p>as well as integrate symbolic expression representing polynomials:</p>
296 <pre class="doctest-block">
297 &gt;&gt;&gt; expr = x + 3*z*x**2
298 &gt;&gt;&gt; print expr.integrate(x)
299 1/2*x**2 + z*x**3
300 &gt;&gt;&gt; print expr.integrate((x, 2, y))
301 1/2*y**2 + z*(y**3 - 8) - 2
302 </pre>
303 <p>SympyCore implements the elementary functions <tt class="docutils literal"><span class="pre">exp</span></tt>, <tt class="docutils literal"><span class="pre">log</span></tt>,
304 <tt class="docutils literal"><span class="pre">sqrt</span></tt>, <tt class="docutils literal"><span class="pre">cos</span></tt>, <tt class="docutils literal"><span class="pre">sin</span></tt>, <tt class="docutils literal"><span class="pre">tan</span></tt>, <tt class="docutils literal"><span class="pre">cot</span></tt>, and simplifies
305 their values in basic cases:</p>
306 <pre class="doctest-block">
307 &gt;&gt;&gt; print log(10000,10)
309 &gt;&gt;&gt; print sin(5*pi/6)
311 &gt;&gt;&gt; print cos(x+pi/2)
312 -sin(x)
313 </pre>
314 </div>
315 <div class="section">
316 <h1><a class="toc-backref" href="#id9" id="polynomial-rings" name="polynomial-rings">Polynomial rings</a></h1>
317 <p>SympyCore provides efficient ways to represent univariate and
318 multivariate polynomials. Currently there are two representation
319 supported. The first one is suitable for univariate dense polynomials:</p>
320 <pre class="doctest-block">
321 &gt;&gt;&gt; poly1 = UnivariatePolynomial([2,0,3,4])
322 &gt;&gt;&gt; poly2 = UnivariatePolynomial([0,1,0,5,6])
323 &gt;&gt;&gt; poly1
324 2 + 3*x**2 + 4*x**3
325 &gt;&gt;&gt; poly2
326 x + 5*x**3 + 6*x**4
327 &gt;&gt;&gt; poly1 + poly2
328 2 + x + 3*x**2 + 9*x**3 + 6*x**4
329 </pre>
330 <p>And the other representation is suitable for multivariate sparse
331 polynomials:</p>
332 <pre class="doctest-block">
333 &gt;&gt;&gt; P = PolynomialRing[(x,y)]
334 &gt;&gt;&gt; poly1 = P({(1,2):7, (300,4):5})
335 &gt;&gt;&gt; poly2 = P({(3,4):-7, (2,500):12})
336 &gt;&gt;&gt; poly1
337 PolynomialRing[(x, y), Calculus]('5*x**300*y**4 + 7*x*y**2')
338 &gt;&gt;&gt; print poly2
339 ((-7))*x**3*y**4 + 12*x**2*y**500
340 &gt;&gt;&gt; print poly1 + poly2
341 5*x**300*y**4 + ((-7))*x**3*y**4 + 12*x**2*y**500 + 7*x*y**2
342 </pre>
343 <p>Here the <tt class="docutils literal"><span class="pre">PolynomialRing[symbols,</span> <span class="pre">Algebra]</span></tt> represents a factory of
344 a polynomial ring over <tt class="docutils literal"><span class="pre">Algebra</span></tt> with <tt class="docutils literal"><span class="pre">symbols</span></tt>.</p>
345 </div>
346 <div class="section">
347 <h1><a class="toc-backref" href="#id10" id="matrix-rings" name="matrix-rings">Matrix rings</a></h1>
348 <p>SympyCore supports representing rectangular matrix ring elements using
349 similar idea of ring factory:</p>
350 <pre class="doctest-block">
351 &gt;&gt;&gt; M = MatrixRing[(3,4)]
352 &gt;&gt;&gt; matrix = M({(1,2):x+y, (0,0):x+z})
353 &gt;&gt;&gt; print matrix
354 x + z 0 0 0
355 0 0 x + y 0
356 0 0 0 0
357 </pre>
358 <p>Note that matrices are mutable in SympyCore and indexes start from 0:</p>
359 <pre class="doctest-block">
360 &gt;&gt;&gt; matrix[1,0] = 5
361 &gt;&gt;&gt; print matrix
362 x + z 0 0 0
363 5 0 x + y 0
364 0 0 0 0
365 </pre>
366 <p>SympyCore provides <tt class="docutils literal"><span class="pre">SquareMatrix</span></tt> and <tt class="docutils literal"><span class="pre">PermutationMatrix</span></tt>
367 factories for convenience:</p>
368 <pre class="doctest-block">
369 &gt;&gt;&gt; SqM = SquareMatrix[3]
370 &gt;&gt;&gt; m = SqM({(0,0): 1, (2,1): 3, (2,2):6, (1,2):-2, (2,0): -1})
371 &gt;&gt;&gt; print m
372 1 0 0
373 0 0 -2
374 -1 3 6
375 &gt;&gt;&gt; print PermutationMatrix[4]([2,1,3,0])
376 0 0 1 0
377 0 1 0 0
378 0 0 0 1
379 1 0 0 0
380 </pre>
381 <p>One can perform LU factorization on any rectangular matrix:</p>
382 <pre class="doctest-block">
383 &gt;&gt;&gt; p, l, u = m.lu()
384 &gt;&gt;&gt; print p
385 1 0 0
386 0 0 1
387 0 1 0
388 &gt;&gt;&gt; print l
389 1 0 0
390 -1 1 0
391 0 0 1
392 </pre>
393 <pre class="doctest-block">
394 &gt;&gt;&gt; print u
395 1 0 0
396 0 3 6
397 0 0 -2
398 </pre>
399 <p>The <tt class="docutils literal"><span class="pre">*</span></tt> denotes matrix multiplication:</p>
400 <pre class="doctest-block">
401 &gt;&gt;&gt; print p * l * u == m
402 True
403 </pre>
404 <p>SympyCore supports computing inverses of square
405 matrices:</p>
406 <pre class="doctest-block">
407 &gt;&gt;&gt; print m.inv()
408 1 0 0
409 1/3 1 1/3
410 0 -1/2 0
411 </pre>
412 <pre class="doctest-block">
413 &gt;&gt;&gt; m.inv() * m == SqM.one
414 True
415 </pre>
416 </div>
417 <div class="section">
418 <h1><a class="toc-backref" href="#id11" id="physical-units" name="physical-units">Physical units</a></h1>
419 <p>SympyCore has a basic support for dealing with symbolic expressions with
420 units:</p>
421 <pre class="doctest-block">
422 &gt;&gt;&gt; mass1 = 5 * kilogram
423 &gt;&gt;&gt; mass2 = x * kilogram
424 &gt;&gt;&gt; print mass1 + mass2
425 (5 + x)*kg
426 </pre>
427 </div>
428 </div>
429 </body>
430 </html>