3 ==========================
4 SympyCore Release 0.2 Demo
5 ==========================
13 :Website: http://sympycore.googlecode.com/
15 :Version: sympycore 0.2-svn.
16 Other versions: `0.1`__.
18 __ http://sympycore.googlecode.com/svn-history/r818/trunk/doc/html/demo0_1.html
20 .. sidebar:: Table of contents
29 This document gives a short overview of SympyCore basic features. For
30 more information see the `SympyCore User's Guide`__.
32 __ http://sympycore.googlecode.com/svn/trunk/doc/html/userguide.html
34 To use the ``sympycore`` package from Python, one must import it:
36 >>> from sympycore import *
38 Constructing symbolic expressions
39 =================================
41 Generally speaking, symbolic expressions consist of symbols and
42 operation between them. To create symbolic expressions using
43 SympyCore, one can either create symbol objects and perform operations
52 or one can use symbolic expression parser to construct symbolic
53 expressions from a string:
58 SympyCore converts symbolic expressions to a canonical form that is
59 efficient for further manipulations and are also often obvious
60 simplifications that users may expect:
68 General symbolic arithmetic expressions are instances of the Calculus
69 class. Using the ``print`` statement (or ``str()``) hides this information:
74 Manipulations with symbolic expressions
75 =======================================
77 The most obvious manipulation task applied to symbolic expressions, is
78 substitution -- replacing a sub-expression of a given expression with a
79 new expression. For example,
82 >>> print expr.subs(y, sin(x))
85 Other tasks include accessing parts of symbolic expressions:
88 [Calculus('x'), Calculus('y')]
90 and constructing new expressions:
92 >>> print Mul(*expr.args)
95 An important presumption for implementing various algorithms is pattern
96 matching. Pattern matching means that given a pattern expression, the
97 pattern match method should first decide whether an expression can be
98 expressed in a form that the pattern defines, and second. it should
99 return information what sub-expression parts correspond to the pattern
100 sub-expressions. For example, given a pattern
103 >>> pattern = x * w ** 3
105 where symbol ``w`` is assumed to match any sub-expression, then expressions
107 >>> expr1 = x*sin(y)**3
108 >>> expr2 = x*(x+y)**3
110 do match the given pattern:
112 >>> d1 = expr1.match(pattern, w)
114 {Calculus('w'): Calculus('sin(y)')}
116 >>> d2 = expr2.match(pattern, w)
118 {Calculus('w'): Calculus('x + y')}
120 The result of ``match`` method, when the match is found, is a dictionary
123 >>> pattern.subs(d1.items())==expr1
125 >>> pattern.subs(d2.items())==expr2
128 If no match is found, then the ``match`` returns ``None``:
130 >>> print (y*x**2).match(pattern, w)
133 Transformation methods
134 ======================
136 The most common transformation task is expansion of sub-expressions by
142 >>> print expr.expand()
145 In general, the ``expand`` method expands products of sums and
146 integer powers of sums:
148 >>> expr = (x+y)*(1+x)**3
149 >>> print expr.expand()
150 x + y + x**4 + 3*x**2 + 3*x**3 + 3*x*y + 3*y*x**2 + y*x**3
155 SympyCore provides exact rational and complex numbers:
159 >>> print (2 + 3*I/4)**4
162 Fractional powers of integers are evaluated to simpler
163 expressions when possible:
165 >>> Calculus('8**(1/3)')
167 >>> Calculus('243**(1/5)')
170 SympyCore supports converting symbolic expressions with exact numbers
171 such as integers and rational numbers to expressions with arbitrary
172 precision floating-point numbers:
174 >>> expr = 2*pi + E**x
177 >>> print expr.evalf(5)
179 >>> print expr.evalf(25)
180 6.283185307179586476925287 + 2.718281828459045235360287**x
185 SympyCore provides methods to differentiate symbolic expressions:
187 >>> expr = x+sin(x*y)*x
188 >>> print expr.diff(x)
189 1 + sin(x*y) + x*y*cos(x*y)
191 as well as integrate symbolic expression representing polynomials:
193 >>> expr = x + 3*z*x**2
194 >>> print expr.integrate(x)
196 >>> print expr.integrate((x, 2, y))
197 1/2*y**2 + z*(y**3 - 8) - 2
199 SympyCore implements the elementary functions ``exp``, ``log``,
200 ``sqrt``, ``cos``, ``sin``, ``tan``, ``cot``, and simplifies
201 their values in basic cases:
203 >>> print log(10000,10)
205 >>> print sin(5*pi/6)
207 >>> print cos(x+pi/2)
213 SympyCore provides efficient ways to represent univariate and
214 multivariate polynomials. Currently there are two representation
215 supported. The first one is suitable for univariate dense polynomials:
217 >>> poly1 = UnivariatePolynomial([2,0,3,4])
218 >>> poly2 = UnivariatePolynomial([0,1,0,5,6])
224 2 + x + 3*x**2 + 9*x**3 + 6*x**4
226 And the other representation is suitable for multivariate sparse
229 >>> P = PolynomialRing[(x,y)]
230 >>> poly1 = P({(1,2):7, (300,4):5})
231 >>> poly2 = P({(3,4):-7, (2,500):12})
233 PolynomialRing[(x, y), Calculus]('5*x**300*y**4 + 7*x*y**2')
235 ((-7))*x**3*y**4 + 12*x**2*y**500
236 >>> print poly1 + poly2
237 5*x**300*y**4 + ((-7))*x**3*y**4 + 12*x**2*y**500 + 7*x*y**2
239 Here the ``PolynomialRing[symbols, Algebra]`` represents a factory of
240 a polynomial ring over ``Algebra`` with ``symbols``.
245 SympyCore supports representing rectangular matrix ring elements using
246 similar idea of ring factory:
248 >>> M = MatrixRing[(3,4)]
249 >>> matrix = M({(1,2):x+y, (0,0):x+z})
255 Note that matrices are mutable in SympyCore and indexes start from 0:
263 SympyCore provides ``SquareMatrix`` and ``PermutationMatrix``
264 factories for convenience:
266 >>> SqM = SquareMatrix[3]
267 >>> m = SqM({(0,0): 1, (2,1): 3, (2,2):6, (1,2):-2, (2,0): -1})
272 >>> print PermutationMatrix[4]([2,1,3,0])
278 One can perform LU factorization on any rectangular matrix:
295 The ``*`` denotes matrix multiplication:
297 >>> print p * l * u == m
300 SympyCore supports computing inverses of square
308 >>> m.inv() * m == SqM.one
314 SympyCore has a basic support for dealing with symbolic expressions with
317 >>> mass1 = 5 * kilogram
318 >>> mass2 = x * kilogram
319 >>> print mass1 + mass2