Implemented crisscross algorithm for solving LP problems.
[sympycore.git] / doc / html / demo0_1.html
blobc1817c9f0160292cf352dbf1b99c7ed0b6d3dcc5
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.1 Demo</title>
8 <meta name="authors" content="Pearu Peterson &lt;pearu.peterson AT gmail DOT com&gt;" />
9 <style type="text/css">
12 :Author: David Goodger
13 :Contact: goodger@users.sourceforge.net
14 :Date: $Date: 2005-12-18 01:56:14 +0100 (Sun, 18 Dec 2005) $
15 :Revision: $Revision: 4224 $
16 :Copyright: This stylesheet has been placed in the public domain.
18 Default cascading style sheet for the HTML output of Docutils.
20 See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
21 customize this style sheet.
24 /* used to remove borders from tables and images */
25 .borderless, table.borderless td, table.borderless th {
26 border: 0 }
28 table.borderless td, table.borderless th {
29 /* Override padding for "table.docutils td" with "! important".
30 The right padding separates the table cells. */
31 padding: 0 0.5em 0 0 ! important }
33 .first {
34 /* Override more specific margin styles with "! important". */
35 margin-top: 0 ! important }
37 .last, .with-subtitle {
38 margin-bottom: 0 ! important }
40 .hidden {
41 display: none }
43 a.toc-backref {
44 text-decoration: none ;
45 color: black }
47 blockquote.epigraph {
48 margin: 2em 5em ; }
50 dl.docutils dd {
51 margin-bottom: 0.5em }
53 /* Uncomment (and remove this text!) to get bold-faced definition list terms
54 dl.docutils dt {
55 font-weight: bold }
58 div.abstract {
59 margin: 2em 5em }
61 div.abstract p.topic-title {
62 font-weight: bold ;
63 text-align: center }
65 div.admonition, div.attention, div.caution, div.danger, div.error,
66 div.hint, div.important, div.note, div.tip, div.warning {
67 margin: 2em ;
68 border: medium outset ;
69 padding: 1em }
71 div.admonition p.admonition-title, div.hint p.admonition-title,
72 div.important p.admonition-title, div.note p.admonition-title,
73 div.tip p.admonition-title {
74 font-weight: bold ;
75 font-family: sans-serif }
77 div.attention p.admonition-title, div.caution p.admonition-title,
78 div.danger p.admonition-title, div.error p.admonition-title,
79 div.warning p.admonition-title {
80 color: red ;
81 font-weight: bold ;
82 font-family: sans-serif }
84 /* Uncomment (and remove this text!) to get reduced vertical space in
85 compound paragraphs.
86 div.compound .compound-first, div.compound .compound-middle {
87 margin-bottom: 0.5em }
89 div.compound .compound-last, div.compound .compound-middle {
90 margin-top: 0.5em }
93 div.dedication {
94 margin: 2em 5em ;
95 text-align: center ;
96 font-style: italic }
98 div.dedication p.topic-title {
99 font-weight: bold ;
100 font-style: normal }
102 div.figure {
103 margin-left: 2em ;
104 margin-right: 2em }
106 div.footer, div.header {
107 clear: both;
108 font-size: smaller }
110 div.line-block {
111 display: block ;
112 margin-top: 1em ;
113 margin-bottom: 1em }
115 div.line-block div.line-block {
116 margin-top: 0 ;
117 margin-bottom: 0 ;
118 margin-left: 1.5em }
120 div.sidebar {
121 margin-left: 1em ;
122 border: medium outset ;
123 padding: 1em ;
124 background-color: #ffffee ;
125 width: 40% ;
126 float: right ;
127 clear: right }
129 div.sidebar p.rubric {
130 font-family: sans-serif ;
131 font-size: medium }
133 div.system-messages {
134 margin: 5em }
136 div.system-messages h1 {
137 color: red }
139 div.system-message {
140 border: medium outset ;
141 padding: 1em }
143 div.system-message p.system-message-title {
144 color: red ;
145 font-weight: bold }
147 div.topic {
148 margin: 2em }
150 h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
151 h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
152 margin-top: 0.4em }
154 h1.title {
155 text-align: center }
157 h2.subtitle {
158 text-align: center }
160 hr.docutils {
161 width: 75% }
163 img.align-left {
164 clear: left }
166 img.align-right {
167 clear: right }
169 ol.simple, ul.simple {
170 margin-bottom: 1em }
172 ol.arabic {
173 list-style: decimal }
175 ol.loweralpha {
176 list-style: lower-alpha }
178 ol.upperalpha {
179 list-style: upper-alpha }
181 ol.lowerroman {
182 list-style: lower-roman }
184 ol.upperroman {
185 list-style: upper-roman }
187 p.attribution {
188 text-align: right ;
189 margin-left: 50% }
191 p.caption {
192 font-style: italic }
194 p.credits {
195 font-style: italic ;
196 font-size: smaller }
198 p.label {
199 white-space: nowrap }
201 p.rubric {
202 font-weight: bold ;
203 font-size: larger ;
204 color: maroon ;
205 text-align: center }
207 p.sidebar-title {
208 font-family: sans-serif ;
209 font-weight: bold ;
210 font-size: larger }
212 p.sidebar-subtitle {
213 font-family: sans-serif ;
214 font-weight: bold }
216 p.topic-title {
217 font-weight: bold }
219 pre.address {
220 margin-bottom: 0 ;
221 margin-top: 0 ;
222 font-family: serif ;
223 font-size: 100% }
225 pre.literal-block, pre.doctest-block {
226 margin-left: 2em ;
227 margin-right: 2em ;
228 background-color: #eeeeee }
230 span.classifier {
231 font-family: sans-serif ;
232 font-style: oblique }
234 span.classifier-delimiter {
235 font-family: sans-serif ;
236 font-weight: bold }
238 span.interpreted {
239 font-family: sans-serif }
241 span.option {
242 white-space: nowrap }
244 span.pre {
245 white-space: pre }
247 span.problematic {
248 color: red }
250 span.section-subtitle {
251 /* font-size relative to parent (h1..h6 element) */
252 font-size: 80% }
254 table.citation {
255 border-left: solid 1px gray;
256 margin-left: 1px }
258 table.docinfo {
259 margin: 2em 4em }
261 table.docutils {
262 margin-top: 0.5em ;
263 margin-bottom: 0.5em }
265 table.footnote {
266 border-left: solid 1px black;
267 margin-left: 1px }
269 table.docutils td, table.docutils th,
270 table.docinfo td, table.docinfo th {
271 padding-left: 0.5em ;
272 padding-right: 0.5em ;
273 vertical-align: top }
275 table.docutils th.field-name, table.docinfo th.docinfo-name {
276 font-weight: bold ;
277 text-align: left ;
278 white-space: nowrap ;
279 padding-left: 0 }
281 h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
282 h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
283 font-size: 100% }
285 tt.docutils {
286 background-color: #eeeeee }
288 ul.auto-toc {
289 list-style-type: none }
291 </style>
292 </head>
293 <body>
294 <div class="document" id="sympycore-release-0-1-demo">
295 <h1 class="title">SympyCore Release 0.1 Demo</h1>
296 <table class="docinfo" frame="void" rules="none">
297 <col class="docinfo-name" />
298 <col class="docinfo-content" />
299 <tbody valign="top">
300 <tr><th class="docinfo-name">Authors:</th>
301 <td>Pearu Peterson &lt;pearu.peterson AT gmail DOT com&gt;</td></tr>
302 <tr class="field"><th class="docinfo-name">Created:</th><td class="field-body">February 2008</td>
303 </tr>
304 <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>
305 </tr>
306 </tbody>
307 </table>
308 <!-- -*- rest -*- -->
309 <div class="sidebar">
310 <p class="first sidebar-title">Table of contents</p>
311 <div class="contents local last topic">
312 <ul class="simple">
313 <li><a class="reference" href="#getting-started" id="id2" name="id2">Getting started</a></li>
314 <li><a class="reference" href="#constructing-symbolic-expressions" id="id3" name="id3">Constructing symbolic expressions</a></li>
315 <li><a class="reference" href="#manipulations-with-symbolic-expressions" id="id4" name="id4">Manipulations with symbolic expressions</a></li>
316 <li><a class="reference" href="#transformation-methods" id="id5" name="id5">Transformation methods</a></li>
317 <li><a class="reference" href="#arithmetic-methods" id="id6" name="id6">Arithmetic methods</a></li>
318 <li><a class="reference" href="#calculus-methods" id="id7" name="id7">Calculus methods</a></li>
319 <li><a class="reference" href="#polynomial-rings" id="id8" name="id8">Polynomial rings</a></li>
320 <li><a class="reference" href="#matrix-rings" id="id9" name="id9">Matrix rings</a></li>
321 <li><a class="reference" href="#physical-units" id="id10" name="id10">Physical units</a></li>
322 </ul>
323 </div>
324 </div>
325 <div class="section">
326 <h1><a class="toc-backref" href="#id2" id="getting-started" name="getting-started">Getting started</a></h1>
327 <p>This document gives a short overview of SympyCore basic features. For
328 more information see the <a class="reference" href="http://sympycore.googlecode.com/svn/trunk/doc/html/userguide.html">SympyCore User's Guide</a>.</p>
329 <p>To use the <tt class="docutils literal"><span class="pre">sympycore</span></tt> package from Python, one must import it:</p>
330 <pre class="doctest-block">
331 &gt;&gt;&gt; from sympycore import *
332 </pre>
333 </div>
334 <div class="section">
335 <h1><a class="toc-backref" href="#id3" id="constructing-symbolic-expressions" name="constructing-symbolic-expressions">Constructing symbolic expressions</a></h1>
336 <p>Generally speaking, symbolic expressions consist of symbols and
337 operation between them. To create symbolic expressions using
338 SympyCore, one can either create symbol objects and perform operations
339 between them:</p>
340 <pre class="doctest-block">
341 &gt;&gt;&gt; x = Symbol('x')
342 &gt;&gt;&gt; y = Symbol('y')
343 &gt;&gt;&gt; z = Symbol('z')
344 &gt;&gt;&gt; x + y
345 Calculus('x + y')
346 </pre>
347 <p>or one can use symbolic expression parser to construct symbolic
348 expressions from a string:</p>
349 <pre class="doctest-block">
350 &gt;&gt;&gt; Calculus('x + y')
351 Calculus('x + y')
352 </pre>
353 <p>SympyCore converts symbolic expressions to a canonical form that is
354 efficient for further manipulations and are also often obvious
355 simplifications that users may expect:</p>
356 <pre class="doctest-block">
357 &gt;&gt;&gt; x + x
358 Calculus('2*x')
359 </pre>
360 <pre class="doctest-block">
361 &gt;&gt;&gt; x - x
362 Calculus('0')
363 </pre>
364 <p>General symbolic arithmetic expressions are instances of the Calculus
365 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>
366 <pre class="doctest-block">
367 &gt;&gt;&gt; print x + y
368 x + y
369 </pre>
370 </div>
371 <div class="section">
372 <h1><a class="toc-backref" href="#id4" id="manipulations-with-symbolic-expressions" name="manipulations-with-symbolic-expressions">Manipulations with symbolic expressions</a></h1>
373 <p>The most obvious manipulation task applied to symbolic expressions, is
374 substitution -- replacing a sub-expression of a given expression with a
375 new expression. For example,</p>
376 <pre class="doctest-block">
377 &gt;&gt;&gt; expr = x + y
378 &gt;&gt;&gt; print expr.subs(y, sin(x))
379 x + sin(x)
380 </pre>
381 <p>Other tasks include accessing parts of symbolic expressions:</p>
382 <pre class="doctest-block">
383 &gt;&gt;&gt; sorted(expr.args)
384 [Calculus('x'), Calculus('y')]
385 </pre>
386 <p>and constructing new expressions:</p>
387 <pre class="doctest-block">
388 &gt;&gt;&gt; print Mul(*expr.args)
390 </pre>
391 <p>An important presumption for implementing various algorithms is pattern
392 matching. Pattern matching means that given a pattern expression, the
393 pattern match method should first decide whether an expression can be
394 expressed in a form that the pattern defines, and second. it should
395 return information what sub-expression parts correspond to the pattern
396 sub-expressions. For example, given a pattern</p>
397 <pre class="doctest-block">
398 &gt;&gt;&gt; w = Symbol('w')
399 &gt;&gt;&gt; pattern = x * w ** 3
400 </pre>
401 <p>where symbol <tt class="docutils literal"><span class="pre">w</span></tt> is assumed to match any sub-expression, then expressions</p>
402 <pre class="doctest-block">
403 &gt;&gt;&gt; expr1 = x*sin(y)**3
404 &gt;&gt;&gt; expr2 = x*(x+y)**3
405 </pre>
406 <p>do match the given pattern:</p>
407 <pre class="doctest-block">
408 &gt;&gt;&gt; d1 = expr1.match(pattern, w)
409 &gt;&gt;&gt; print d1
410 {Calculus('w'): Calculus('sin(y)')}
411 </pre>
412 <pre class="doctest-block">
413 &gt;&gt;&gt; d2 = expr2.match(pattern, w)
414 &gt;&gt;&gt; print d2
415 {Calculus('w'): Calculus('x + y')}
416 </pre>
417 <p>The result of <tt class="docutils literal"><span class="pre">match</span></tt> method, when the match is found, is a dictionary
418 with the property</p>
419 <pre class="doctest-block">
420 &gt;&gt;&gt; pattern.subs(d1.items())==expr1
421 True
422 &gt;&gt;&gt; pattern.subs(d2.items())==expr2
423 True
424 </pre>
425 <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>
426 <pre class="doctest-block">
427 &gt;&gt;&gt; print (y*x**2).match(pattern, w)
428 None
429 </pre>
430 </div>
431 <div class="section">
432 <h1><a class="toc-backref" href="#id5" id="transformation-methods" name="transformation-methods">Transformation methods</a></h1>
433 <p>The most common transformation task is expansion of sub-expressions by
434 opening parenthesis:</p>
435 <pre class="doctest-block">
436 &gt;&gt;&gt; expr = (x+y)*z
437 &gt;&gt;&gt; print expr
438 z*(x + y)
439 &gt;&gt;&gt; print expr.expand()
440 x*z + y*z
441 </pre>
442 <p>In general, the <tt class="docutils literal"><span class="pre">expand</span></tt> method expands products of sums and
443 integer powers of sums:</p>
444 <pre class="doctest-block">
445 &gt;&gt;&gt; expr = (x+y)*(1+x)**3
446 &gt;&gt;&gt; print expr.expand()
447 x + y + x**4 + 3*x**2 + 3*x**3 + 3*x*y + 3*y*x**2 + y*x**3
448 </pre>
449 </div>
450 <div class="section">
451 <h1><a class="toc-backref" href="#id6" id="arithmetic-methods" name="arithmetic-methods">Arithmetic methods</a></h1>
452 <p>SympyCore provides exact rational and complex numbers:</p>
453 <pre class="doctest-block">
454 &gt;&gt;&gt; Calculus('3/12')
455 Calculus('1/4')
456 &gt;&gt;&gt; print (2 + 3*I/4)**4
457 721/256 + 165/8*I
458 </pre>
459 <p>Fractional powers of integers are evaluated to simpler
460 expressions when possible:</p>
461 <pre class="doctest-block">
462 &gt;&gt;&gt; Calculus('8**(1/3)')
463 Calculus('2')
464 &gt;&gt;&gt; Calculus('243**(1/5)')
465 Calculus('3')
466 </pre>
467 <p>SympyCore supports converting symbolic expressions with exact numbers
468 such as integers and rational numbers to expressions with arbitrary
469 precision floating-point numbers:</p>
470 <pre class="doctest-block">
471 &gt;&gt;&gt; expr = 2*pi + E**x
472 &gt;&gt;&gt; print expr
473 E**x + 2*pi
474 &gt;&gt;&gt; print expr.evalf(5)
475 6.2832 + 2.7183**x
476 &gt;&gt;&gt; print expr.evalf(25)
477 6.283185307179586476925287 + 2.718281828459045235360287**x
478 </pre>
479 </div>
480 <div class="section">
481 <h1><a class="toc-backref" href="#id7" id="calculus-methods" name="calculus-methods">Calculus methods</a></h1>
482 <p>SympyCore provides methods to differentiate symbolic expressions:</p>
483 <pre class="doctest-block">
484 &gt;&gt;&gt; expr = x+sin(x*y)*x
485 &gt;&gt;&gt; print expr.diff(x)
486 1 + sin(x*y) + x*y*cos(x*y)
487 </pre>
488 <p>as well as integrate symbolic expression representing polynomials:</p>
489 <pre class="doctest-block">
490 &gt;&gt;&gt; expr = x + 3*z*x**2
491 &gt;&gt;&gt; print expr.integrate(x)
492 1/2*x**2 + z*x**3
493 &gt;&gt;&gt; print expr.integrate((x, 2, y))
494 1/2*y**2 + z*(y**3 - 8) - 2
495 </pre>
496 <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>,
497 <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
498 their values in basic cases:</p>
499 <pre class="doctest-block">
500 &gt;&gt;&gt; print log(10000,10)
502 &gt;&gt;&gt; print sin(5*pi/6)
504 &gt;&gt;&gt; print cos(x+pi/2)
505 -sin(x)
506 </pre>
507 </div>
508 <div class="section">
509 <h1><a class="toc-backref" href="#id8" id="polynomial-rings" name="polynomial-rings">Polynomial rings</a></h1>
510 <p>SympyCore provides efficient ways to represent univariate and
511 multivariate polynomials. Currently there are two representation
512 supported. The first one is suitable for univariate dense polynomials:</p>
513 <pre class="doctest-block">
514 &gt;&gt;&gt; poly1 = UnivariatePolynomial([2,0,3,4], symbol='x')
515 &gt;&gt;&gt; poly2 = UnivariatePolynomial([0,1,0,5,6], symbol='x')
516 &gt;&gt;&gt; poly1
517 2 + 3*x**2 + 4*x**3
518 &gt;&gt;&gt; poly2
519 x + 5*x**3 + 6*x**4
520 &gt;&gt;&gt; poly1 + poly2
521 2 + x + 3*x**2 + 9*x**3 + 6*x**4
522 </pre>
523 <p>And the other representation is suitable for multivariate sparse
524 polynomials:</p>
525 <pre class="doctest-block">
526 &gt;&gt;&gt; P = PolynomialRing[(x,y)]
527 &gt;&gt;&gt; poly1 = P({(1,2):7, (300,4):5})
528 &gt;&gt;&gt; poly2 = P({(3,4):-7, (2,500):12})
529 &gt;&gt;&gt; poly1
530 PolynomialRing[(x, y), Calculus]('5*x**300*y**4 + 7*x*y**2')
531 &gt;&gt;&gt; print poly2
532 ((-7))*x**3*y**4 + 12*x**2*y**500
533 &gt;&gt;&gt; print poly1 + poly2
534 5*x**300*y**4 + ((-7))*x**3*y**4 + 12*x**2*y**500 + 7*x*y**2
535 </pre>
536 <p>Here the <tt class="docutils literal"><span class="pre">PolynomialRing[symbols,</span> <span class="pre">Algebra]</span></tt> represents a factory of
537 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>
538 </div>
539 <div class="section">
540 <h1><a class="toc-backref" href="#id9" id="matrix-rings" name="matrix-rings">Matrix rings</a></h1>
541 <p>SympyCore supports representing rectangular matrix ring elements using
542 similar idea of ring factory:</p>
543 <pre class="doctest-block">
544 &gt;&gt;&gt; M = MatrixRing[(3,4)]
545 &gt;&gt;&gt; matrix = M({(1,2):x+y, (0,0):x+z})
546 &gt;&gt;&gt; print matrix
547 x + z 0 0 0
548 0 0 x + y 0
549 0 0 0 0
550 </pre>
551 <p>Note that matrices are mutable in SympyCore and indexes start from 0:</p>
552 <pre class="doctest-block">
553 &gt;&gt;&gt; matrix[1,0] = 5
554 &gt;&gt;&gt; print matrix
555 x + z 0 0 0
556 5 0 x + y 0
557 0 0 0 0
558 </pre>
559 <p>SympyCore provides <tt class="docutils literal"><span class="pre">SquareMatrix</span></tt> and <tt class="docutils literal"><span class="pre">PermutationMatrix</span></tt>
560 factories for convenience:</p>
561 <pre class="doctest-block">
562 &gt;&gt;&gt; SqM = SquareMatrix[3]
563 &gt;&gt;&gt; m = SqM({(0,0): 1, (2,1): 3, (2,2):6, (1,2):-2, (2,0): -1})
564 &gt;&gt;&gt; print m
565 1 0 0
566 0 0 -2
567 -1 3 6
568 &gt;&gt;&gt; print PermutationMatrix[4]([2,1,3,0])
569 0 0 1 0
570 0 1 0 0
571 0 0 0 1
572 1 0 0 0
573 </pre>
574 <p>One can perform LU factorization on any rectangular matrix:</p>
575 <pre class="doctest-block">
576 &gt;&gt;&gt; p, l, u = m.lu()
577 &gt;&gt;&gt; print p
578 1 0 0
579 0 0 1
580 0 1 0
581 &gt;&gt;&gt; print l
582 1 0 0
583 -1 1 0
584 0 0 1
585 </pre>
586 <pre class="doctest-block">
587 &gt;&gt;&gt; print u
588 1 0 0
589 0 3 6
590 0 0 -2
591 </pre>
592 <p>The <tt class="docutils literal"><span class="pre">*</span></tt> denotes matrix multiplication:</p>
593 <pre class="doctest-block">
594 &gt;&gt;&gt; print p * l * u == m
595 True
596 </pre>
597 <p>SympyCore supports computing inverses of square
598 matrices:</p>
599 <pre class="doctest-block">
600 &gt;&gt;&gt; print m.inv()
601 1 0 0
602 1/3 1 1/3
603 0 -1/2 0
604 </pre>
605 <pre class="doctest-block">
606 &gt;&gt;&gt; m.inv() * m == SqM.one
607 True
608 </pre>
609 </div>
610 <div class="section">
611 <h1><a class="toc-backref" href="#id10" id="physical-units" name="physical-units">Physical units</a></h1>
612 <p>SympyCore has a basic support for dealing with symbolic expressions with
613 units:</p>
614 <pre class="doctest-block">
615 &gt;&gt;&gt; mass1 = 5 * kilogram
616 &gt;&gt;&gt; mass2 = x * kilogram
617 &gt;&gt;&gt; print mass1 + mass2
618 (5 + x)*kg
619 </pre>
620 </div>
621 </div>
622 </body>
623 </html>