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">
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 <pearu.peterson AT gmail DOT com>" />
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 {
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 }
34 /* Override more specific margin styles with
"! important". */
35 margin-top:
0 ! important }
37 .last, .with-subtitle {
38 margin-bottom:
0 ! important }
44 text-decoration: none ;
51 margin-bottom:
0.5em }
53 /* Uncomment (and remove this text!) to get bold-faced definition list terms
61 div.abstract p.topic-title {
65 div.admonition, div.attention, div.caution, div.danger, div.error,
66 div.hint, div.important, div.note, div.tip, div.warning {
68 border: medium outset ;
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 {
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 {
82 font-family: sans-serif }
84 /* Uncomment (and remove this text!) to get reduced vertical space in
86 div.compound .compound-first, div.compound .compound-middle {
87 margin-bottom:
0.5em }
89 div.compound .compound-last, div.compound .compound-middle {
98 div.dedication p.topic-title {
106 div.footer, div.header {
115 div.line-block div.line-block {
122 border: medium outset ;
124 background-color: #ffffee ;
129 div.sidebar p.rubric {
130 font-family: sans-serif ;
133 div.system-messages {
136 div.system-messages h1 {
140 border: medium outset ;
143 div.system-message p.system-message-title {
150 h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
151 h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
169 ol.simple, ul.simple {
173 list-style: decimal }
176 list-style: lower-alpha }
179 list-style: upper-alpha }
182 list-style: lower-roman }
185 list-style: upper-roman }
199 white-space: nowrap }
208 font-family: sans-serif ;
213 font-family: sans-serif ;
225 pre.literal-block, pre.doctest-block {
228 background-color: #eeeeee }
231 font-family: sans-serif ;
232 font-style: oblique }
234 span.classifier-delimiter {
235 font-family: sans-serif ;
239 font-family: sans-serif }
242 white-space: nowrap }
250 span.section-subtitle {
251 /* font-size relative to parent (h1..h6 element) */
255 border-left: solid
1px gray;
263 margin-bottom:
0.5em }
266 border-left: solid
1px black;
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 {
278 white-space: nowrap ;
281 h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
282 h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
286 background-color: #eeeeee }
289 list-style-type: none }
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" />
300 <tr><th class=
"docinfo-name">Authors:
</th>
301 <td>Pearu Peterson
<pearu.peterson AT gmail DOT com
></td></tr>
302 <tr class=
"field"><th class=
"docinfo-name">Created:
</th><td class=
"field-body">February
2008</td>
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>
308 <!-- -*- rest -*- -->
309 <div class=
"sidebar">
310 <p class=
"first sidebar-title">Table of contents
</p>
311 <div class=
"contents local last topic">
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>
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 >>> from sympycore import *
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
340 <pre class=
"doctest-block">
341 >>> x = Symbol('x')
342 >>> y = Symbol('y')
343 >>> z = Symbol('z')
347 <p>or one can use symbolic expression parser to construct symbolic
348 expressions from a string:
</p>
349 <pre class=
"doctest-block">
350 >>> Calculus('x + y')
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">
360 <pre class=
"doctest-block">
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 >>> print x + y
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 >>> expr = x + y
378 >>> print expr.subs(y, sin(x))
381 <p>Other tasks include accessing parts of symbolic expressions:
</p>
382 <pre class=
"doctest-block">
383 >>> sorted(expr.args)
384 [Calculus('x'), Calculus('y')]
386 <p>and constructing new expressions:
</p>
387 <pre class=
"doctest-block">
388 >>> print Mul(*expr.args)
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 >>> w = Symbol('w')
399 >>> pattern = x * w **
3
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 >>> expr1 = x*sin(y)**
3
404 >>> expr2 = x*(x+y)**
3
406 <p>do match the given pattern:
</p>
407 <pre class=
"doctest-block">
408 >>> d1 = expr1.match(pattern, w)
409 >>> print d1
410 {Calculus('w'): Calculus('sin(y)')}
412 <pre class=
"doctest-block">
413 >>> d2 = expr2.match(pattern, w)
414 >>> print d2
415 {Calculus('w'): Calculus('x + y')}
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 >>> pattern.subs(d1.items())==expr1
422 >>> pattern.subs(d2.items())==expr2
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 >>> print (y*x**
2).match(pattern, w)
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 >>> expr = (x+y)*z
437 >>> print expr
439 >>> print expr.expand()
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 >>> expr = (x+y)*(
1+x)**
3
446 >>> print expr.expand()
447 x + y + x**
4 +
3*x**
2 +
3*x**
3 +
3*x*y +
3*y*x**
2 + y*x**
3
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 >>> Calculus('
3/
12')
456 >>> print (
2 +
3*I/
4)**
4
459 <p>Fractional powers of integers are evaluated to simpler
460 expressions when possible:
</p>
461 <pre class=
"doctest-block">
462 >>> Calculus('
8**(
1/
3)')
464 >>> Calculus('
243**(
1/
5)')
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 >>> expr =
2*pi + E**x
472 >>> print expr
474 >>> print expr.evalf(
5)
476 >>> print expr.evalf(
25)
477 6.283185307179586476925287 +
2.718281828459045235360287**x
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 >>> expr = x+sin(x*y)*x
485 >>> print expr.diff(x)
486 1 + sin(x*y) + x*y*cos(x*y)
488 <p>as well as integrate symbolic expression representing polynomials:
</p>
489 <pre class=
"doctest-block">
490 >>> expr = x +
3*z*x**
2
491 >>> print expr.integrate(x)
493 >>> print expr.integrate((x,
2, y))
494 1/
2*y**
2 + z*(y**
3 -
8) -
2
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 >>> print log(
10000,
10)
502 >>> print sin(
5*pi/
6)
504 >>> print cos(x+pi/
2)
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 >>> poly1 = UnivariatePolynomial([
2,
0,
3,
4], symbol='x')
515 >>> poly2 = UnivariatePolynomial([
0,
1,
0,
5,
6], symbol='x')
520 >>> poly1 + poly2
521 2 + x +
3*x**
2 +
9*x**
3 +
6*x**
4
523 <p>And the other representation is suitable for multivariate sparse
525 <pre class=
"doctest-block">
526 >>> P = PolynomialRing[(x,y)]
527 >>> poly1 = P({(
1,
2):
7, (
300,
4):
5})
528 >>> poly2 = P({(
3,
4):-
7, (
2,
500):
12})
530 PolynomialRing[(x, y), Calculus]('
5*x**
300*y**
4 +
7*x*y**
2')
531 >>> print poly2
532 ((-
7))*x**
3*y**
4 +
12*x**
2*y**
500
533 >>> print poly1 + poly2
534 5*x**
300*y**
4 + ((-
7))*x**
3*y**
4 +
12*x**
2*y**
500 +
7*x*y**
2
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>
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 >>> M = MatrixRing[(
3,
4)]
545 >>> matrix = M({(
1,
2):x+y, (
0,
0):x+z})
546 >>> print matrix
551 <p>Note that matrices are mutable in SympyCore and indexes start from
0:
</p>
552 <pre class=
"doctest-block">
553 >>> matrix[
1,
0] =
5
554 >>> print matrix
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 >>> SqM = SquareMatrix[
3]
563 >>> m = SqM({(
0,
0):
1, (
2,
1):
3, (
2,
2):
6, (
1,
2):-
2, (
2,
0): -
1})
568 >>> print PermutationMatrix[
4]([
2,
1,
3,
0])
574 <p>One can perform LU factorization on any rectangular matrix:
</p>
575 <pre class=
"doctest-block">
576 >>> p, l, u = m.lu()
586 <pre class=
"doctest-block">
592 <p>The
<tt class=
"docutils literal"><span class=
"pre">*
</span></tt> denotes matrix multiplication:
</p>
593 <pre class=
"doctest-block">
594 >>> print p * l * u == m
597 <p>SympyCore supports computing inverses of square
599 <pre class=
"doctest-block">
600 >>> print m.inv()
605 <pre class=
"doctest-block">
606 >>> m.inv() * m == SqM.one
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
614 <pre class=
"doctest-block">
615 >>> mass1 =
5 * kilogram
616 >>> mass2 = x * kilogram
617 >>> print mass1 + mass2