SymPy

Not to be confused with SimPy, a discrete-event simulation language.
SymPy
Developer(s) Independent group of people
Initial release 2007 (2007)
Stable release
1.0 / March 8, 2016 (2016-03-08)
Repository github.com/sympy/sympy
Development status Active
Written in Python
Operating system Cross-platform
Type Computer algebra system
License New BSD License
Website sympy.org

SymPy is a Python library for symbolic computation. It provides computer algebra capabilities either as a standalone application, as a library to other applications, or live on the web as SymPy Live or SymPy Gamma. SymPy is trivial to install and to inspect because is written entirely in Python with few dependencies.[1][2] This ease of access combined with a simple and extensible code base in a well known language make SymPy a computer algebra system with a relatively low barrier to entry.

SymPy includes features ranging from basic symbolic arithmetic to calculus, algebra, discrete mathematics and quantum physics. It is capable of formatting the result of the computations as LaTeX code.[1][2]

SymPy is free software and is licensed under New BSD License. The lead developers are Ondřej Čertík and Aaron Meurer.

Features

The SymPy library is split into a core with many optional modules.

Currently, the core of SymPy has around 260,000 lines of code[3] (also includes a comprehensive set of self-tests: over 100,000 lines in 350 files as of version 0.7.5), and its capabilities include:[1][2][4][5][6]

Core capabilities

Polynomials

Calculus

Solving equations

Discrete math

Matrices

Geometry

Plotting

Note, plotting requires the external matplotlib or Pyglet module.

Physics

Statistics

Combinatorics

Printing

Dependencies

Since version 1.0, SymPy has the mpmath package as a dependency.

There are several optional dependencies that can enhance its capabilities:

Usage examples

Pretty-printing

Sympy allows outputs to be formatted into a more appealing format through the pprint function. Alternatively, the init_printing() method will enable pretty-printing, so pprint need not be called. Pretty-printing will use unicode symbols when available in the current environment, otherwise it will fall back to ASCII characters.

>>> from sympy import pprint, init_printing, Symbol, sin, cos, exp, sqrt, series, Integral, Function
>>>
>>> x = Symbol("x")
>>> y = Symbol("y")
>>> f = Function('f')
>>> # pprint will default to unicode if available
>>> pprint( x**exp(x) )
 ⎛ x⎞
 ⎝ℯ ⎠
x   
>>> # An output without unicode
>>> pprint(Integral(f(x), x), use_unicode=False)
  /       
 |        
 | f(x) dx
 |        
/        
>>> # Compare with same expression but this time unicode is enabled
>>> pprint(Integral(f(x), x), use_unicode=True)

⎮ f(x) dx

>>> # Alternatively, you can call init_printing() once and pretty-print without the pprint function.
>>> init_printing()
>>> sqrt(sqrt(exp(x)))
   ____
4 ╱  x 
╲╱  ℯ  
>>> (1/cos(x)).series(x, 0, 10)
     2      4       6        8         
    x    5⋅x    61⋅x    277⋅x     ⎛ 10⎞
1 + ── + ──── + ───── + ────── + O⎝x  ⎠
    2     24     720     8064

Expansion

>>> from sympy import init_printing, Symbol, expand
>>> init_printing()
>>>
>>> a = Symbol('a')
>>> b = Symbol('b')
>>> e = (a + b)**5
>>> e
       5
(a + b) 
>>> e.expand()
 5     4        3  2      2  3       4    5
a  + 5⋅a ⋅b + 10⋅a ⋅b  + 10⋅a ⋅b  + 5⋅a⋅b  + b

Arbitrary Precision Example

>>> from sympy import Rational, pprint
>>> e = Rational(2)**50 / Rational(10)**50
>>> pprint(e)
1/88817841970012523233890533447265625

Differentiation

>>> from sympy import init_printing, symbols, ln, diff
>>> init_printing()
>>> x,y = symbols('x y')
>>> f = x**2 / y + 2 * x - ln(y)
>>> diff(f,x)
 2⋅x    
 ─── + 2
  y 
>>> diff(f,y)
    2    
   x    1
 - ── - ─
    2   y
   y
>>> diff(diff(f,x),y)
 -2⋅x
 ────
   2 
  y

Plotting

Output of the plotting example
>>> from sympy import symbols, cos
>>> from sympy.plotting import plot3d
>>> x,y = symbols('x y')
>>> plot3d(cos(x*3)*cos(y*5)-y, (x, -1, 1), (y, -1, 1))
<sympy.plotting.plot.Plot object at 0x3b6d0d0>

Limits

>>> from sympy import init_printing, Symbol, limit, sqrt, oo
>>> init_printing()
>>> 
>>> x = Symbol('x')
>>> limit(sqrt(x**2 - 5*x + 6) - x, x, oo)
-5/2
>>> limit(x*(sqrt(x**2 + 1) - x), x, oo)
1/2
>>> limit(1/x**2, x, 0)

>>> limit(((x - 1)/(x + 1))**x, x, oo)
 -2

Differential Equations

>>> from sympy import init_printing, Symbol, Function, Eq, dsolve, sin, diff
>>> init_printing()
>>>
>>> x = Symbol("x")
>>> f = Function("f")
>>>
>>> eq = Eq(f(x).diff(x), f(x))
>>> eq
d              
──(f(x)) = f(x)
dx         
>>>    
>>> dsolve(eq, f(x))
           x
f(x) = C₁⋅ℯ

>>>
>>> eq = Eq(x**2*f(x).diff(x), -3*x*f(x) + sin(x)/x)
>>> eq
 2 d                      sin(x)
x ⋅──(f(x)) = -3⋅x⋅f(x) + ──────
   dx                       x   
>>>
>>> dsolve(eq, f(x))
       C₁ - cos(x)
f(x) = ───────────
             3    
            x

Integration

>>> from sympy import init_printing, integrate, Symbol, exp, cos, erf
>>> init_printing()
>>> x = Symbol('x')
>>> # Polynomial Function
>>> f = x**2 + x + 1
>>> f
 2        
x  + x + 1
>>> integrate(f,x)
 3    2    
x    x     
── + ── + x
3    2     
>>> # Rational Function
>>> f = x/(x**2+2*x+1)
>>> f
     x      
────────────
 2          
x  + 2⋅x + 1

>>> integrate(f, x)
               1  
log(x + 1) + ─────
             x + 1
>>> # Exponential-polynomial functions
>>> f = x**2 * exp(x) * cos(x)
>>> f
 2  x       
x ⋅ℯ ⋅cos(x)
>>> integrate(f, x)
 2  x           2  x                         x           x       
x ⋅ℯ ⋅sin(x)   x ⋅ℯ ⋅cos(x)      x          ℯ ⋅sin(x)   ℯ ⋅cos(x)
──────────── + ──────────── - x⋅ℯ ⋅sin(x) + ───────── - ─────────
     2              2                           2           2    
>>> # A non-elementary integral
>>> f = exp(-x**2) * erf(x)
>>> f
   2       
 -x        
ℯ   ⋅erf(x)
>>> integrate(f, x)

  ___    2   
╲╱ π ⋅erf (x)
─────────────
      4

Series

>>> from sympy import Symbol, cos, sin, pprint
>>> x = Symbol('x')
>>> e = 1/cos(x)
>>> pprint(e)
  1   
──────
cos(x)
>>> pprint(e.series(x, 0, 10))
     2      4       6        8         
    x    5⋅x    61⋅x    277⋅x     ⎛ 10⎞
1 + ── + ──── + ───── + ────── + O⎝x  ⎠
    2     24     720     8064          
>>> e = 1/sin(x)
>>> pprint(e)
  1   
──────
sin(x)
>>> pprint(e.series(x, 0, 4))
           3        
1   x   7⋅x     ⎛ 4⎞
─ + ─ + ──── + O⎝x ⎠
x   6   360

See also

References

  1. 1 2 3 "SymPy homepage". Retrieved 2014-10-13.
  2. 1 2 3 Joyner, David; Čertík, Ondřej; Meurer, Aaron; Granger, Brian E. (2012). "Open source computer algebra systems: SymPy". ACM Communications in Computer Algebra. 45 (3/4): 225–234. doi:10.1145/2110170.2110185.
  3. "Sympy project statistics on Open HUB". Retrieved 2014-10-13.
  4. Gede, Gilbert; Peterson, Dale L.; Nanjangud, Angadh; Moore, Jason K.; Hubbard, Mont (2013). "Constrained multibody dynamics with Python: From symbolic equation generation to publication". ASME 2013 International Design Engineering Technical Conferences and Computers and Information in Engineering Conference. American Society of Mechanical Engineers: V07BT10A051–V07BT10A051. doi:10.1115/DETC2013-13470.
  5. Rocklin, Matthew; Terrel, Andy (2012). "Symbolic Statistics with SymPy". Computing in Science & Engineering. 14 (3): 88–93. doi:10.1109/MCSE.2012.56.
  6. Asif, Mushtaq; Olaussen, Kåre (2014). "Automatic code generator for higher order integrators". Computer Physics Communications. 185 (5): 1461–1472. doi:10.1016/j.cpc.2014.01.012.
This article is issued from Wikipedia - version of the 10/29/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.