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.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: */
26 background-color: #ccc;
27 font-size:
13px; font-family: arial, sans-serif;
34 background-color: #fff;
36 border:
5px solid #ddd;
41 border: medium outset ;
43 background-color: #ffffee ;
48 div.sidebar p.rubric {
49 font-family: sans-serif ;
53 font-family: sans-serif ;
58 font-family: sans-serif ;
62 font-family: consolas, lucida console, courier new, monospace;
66 pre.literal-block, pre.doctest-block {
68 border-top:
1px solid #ccc;
69 border-bottom:
1px solid #ccc;
70 background-color:#f0f0f0;
74 div.admonition, div.attention, div.caution, div.danger, div.error,
75 div.hint, div.important, div.note, div.tip, div.warning {
77 border: medium outset ;
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 {
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 {
93 font-family: sans-serif }
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" />
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>
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>
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>
115 <!-- -*- rest -*- -->
116 <div class=
"sidebar">
117 <p class=
"first sidebar-title">Table of contents
</p>
118 <div class=
"contents local last topic">
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>
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 >>> from sympycore import *
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
147 <pre class=
"doctest-block">
148 >>> x = Symbol('x')
149 >>> y = Symbol('y')
150 >>> z = Symbol('z')
154 <p>or one can use symbolic expression parser to construct symbolic
155 expressions from a string:
</p>
156 <pre class=
"doctest-block">
157 >>> Calculus('x + y')
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">
167 <pre class=
"doctest-block">
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 >>> print x + y
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 >>> expr = x + y
185 >>> print expr.subs(y, sin(x))
188 <p>Other tasks include accessing parts of symbolic expressions:
</p>
189 <pre class=
"doctest-block">
190 >>> sorted(expr.args)
191 [Calculus('x'), Calculus('y')]
193 <p>and constructing new expressions:
</p>
194 <pre class=
"doctest-block">
195 >>> print Mul(*expr.args)
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 >>> w = Symbol('w')
206 >>> pattern = x * w **
3
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 >>> expr1 = x*sin(y)**
3
211 >>> expr2 = x*(x+y)**
3
213 <p>do match the given pattern:
</p>
214 <pre class=
"doctest-block">
215 >>> d1 = expr1.match(pattern, w)
216 >>> print d1
217 {Calculus('w'): Calculus('sin(y)')}
219 <pre class=
"doctest-block">
220 >>> d2 = expr2.match(pattern, w)
221 >>> print d2
222 {Calculus('w'): Calculus('x + y')}
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 >>> pattern.subs(d1.items())==expr1
229 >>> pattern.subs(d2.items())==expr2
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 >>> print (y*x**
2).match(pattern, w)
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 >>> expr = (x+y)*z
244 >>> print expr
246 >>> print expr.expand()
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 >>> expr = (x+y)*(
1+x)**
3
253 >>> print expr.expand()
254 x + y + x**
4 +
3*x**
2 +
3*x**
3 +
3*x*y +
3*y*x**
2 + y*x**
3
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 >>> Calculus('
3/
12')
263 >>> print (
2 +
3*I/
4)**
4
266 <p>Fractional powers of integers are evaluated to simpler
267 expressions when possible:
</p>
268 <pre class=
"doctest-block">
269 >>> Calculus('
8**(
1/
3)')
271 >>> Calculus('
243**(
1/
5)')
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 >>> expr =
2*pi + E**x
279 >>> print expr
281 >>> print expr.evalf(
5)
283 >>> print expr.evalf(
25)
284 6.283185307179586476925287 +
2.718281828459045235360287**x
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 >>> expr = x+sin(x*y)*x
292 >>> print expr.diff(x)
293 1 + sin(x*y) + x*y*cos(x*y)
295 <p>as well as integrate symbolic expression representing polynomials:
</p>
296 <pre class=
"doctest-block">
297 >>> expr = x +
3*z*x**
2
298 >>> print expr.integrate(x)
300 >>> print expr.integrate((x,
2, y))
301 1/
2*y**
2 + z*(y**
3 -
8) -
2
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 >>> print log(
10000,
10)
309 >>> print sin(
5*pi/
6)
311 >>> print cos(x+pi/
2)
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 >>> poly1 = UnivariatePolynomial([
2,
0,
3,
4])
322 >>> poly2 = UnivariatePolynomial([
0,
1,
0,
5,
6])
327 >>> poly1 + poly2
328 2 + x +
3*x**
2 +
9*x**
3 +
6*x**
4
330 <p>And the other representation is suitable for multivariate sparse
332 <pre class=
"doctest-block">
333 >>> P = PolynomialRing[(x,y)]
334 >>> poly1 = P({(
1,
2):
7, (
300,
4):
5})
335 >>> poly2 = P({(
3,
4):-
7, (
2,
500):
12})
337 PolynomialRing[(x, y), Calculus]('
5*x**
300*y**
4 +
7*x*y**
2')
338 >>> print poly2
339 ((-
7))*x**
3*y**
4 +
12*x**
2*y**
500
340 >>> print poly1 + poly2
341 5*x**
300*y**
4 + ((-
7))*x**
3*y**
4 +
12*x**
2*y**
500 +
7*x*y**
2
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>
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 >>> M = MatrixRing[(
3,
4)]
352 >>> matrix = M({(
1,
2):x+y, (
0,
0):x+z})
353 >>> print matrix
358 <p>Note that matrices are mutable in SympyCore and indexes start from
0:
</p>
359 <pre class=
"doctest-block">
360 >>> matrix[
1,
0] =
5
361 >>> print matrix
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 >>> SqM = SquareMatrix[
3]
370 >>> m = SqM({(
0,
0):
1, (
2,
1):
3, (
2,
2):
6, (
1,
2):-
2, (
2,
0): -
1})
375 >>> print PermutationMatrix[
4]([
2,
1,
3,
0])
381 <p>One can perform LU factorization on any rectangular matrix:
</p>
382 <pre class=
"doctest-block">
383 >>> p, l, u = m.lu()
393 <pre class=
"doctest-block">
399 <p>The
<tt class=
"docutils literal"><span class=
"pre">*
</span></tt> denotes matrix multiplication:
</p>
400 <pre class=
"doctest-block">
401 >>> print p * l * u == m
404 <p>SympyCore supports computing inverses of square
406 <pre class=
"doctest-block">
407 >>> print m.inv()
412 <pre class=
"doctest-block">
413 >>> m.inv() * m == SqM.one
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
421 <pre class=
"doctest-block">
422 >>> mass1 =
5 * kilogram
423 >>> mass2 = x * kilogram
424 >>> print mass1 + mass2