SymPy


SymPy is an open-source 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 or . SymPy is simple to install and to inspect because it is written entirely in Python with few dependencies. 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.
SymPy is free software and is licensed under New BSD License. The lead developers are Ondřej Čertík and Aaron Meurer. It was started in 2005 by Ondřej Čertík.

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, and its capabilities include:

Core capabilities

Note, plotting requires the external matplotlib or Pyglet module.
Since version 1.0, SymPy has the mpmath package as a dependency.
There are several optional dependencies that can enhance its capabilities:

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
>>> y = Symbol
>>> f = Function
>>> # pprint will default to unicode if available
>>> pprint
⎛ x⎞
⎝ℯ ⎠
x
>>> # An output without unicode
>>> pprint
/
|
| f dx
|
/
>>> # Compare with same expression but this time unicode is enabled
>>> pprint

⎮ f dx

>>> # Alternatively, you can call init_printing once and pretty-print without the pprint function.
>>> init_printing
>>> sqrt
____
4 ╱ x
╲╱ ℯ
>>>.series
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
>>> b = Symbol
>>> e = **5
>>> e
5

>>> 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 = 2**50 / Rational**50
>>> pprint
1/88817841970012523233890533447265625

Differentiation


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

Plotting


>>> from sympy import symbols, cos
>>> from sympy.plotting import plot3d
>>> x, y = symbols
>>> plot3d*cos-y,, )

Limits


>>> from sympy import init_printing, Symbol, limit, sqrt, oo
>>> init_printing
>>>
>>> x = Symbol
>>> limit
-5/2
>>> limit
1/2
>>> limit
>>> limit/)**x, x, oo)
-2

Differential equations


>>> from sympy import init_printing, Symbol, Function, Eq, dsolve, sin, diff
>>> init_printing
>>>
>>> x = Symbol
>>> f = Function
>>>
>>> eq = Eq.diff, f)
>>> eq
d
── = f
dx
>>>
>>> dsolve
x
f = C₁⋅ℯ
>>>
>>> eq = Eq.diff, -3*x*f + sin
>>> eq
2 d sin
x ⋅── = -3⋅x⋅f + ──────
dx x
>>>
>>> dsolve
C₁ - cos
f = ───────────
3
x

Integration


>>> from sympy import init_printing, integrate, Symbol, exp, cos, erf
>>> init_printing
>>> x = Symbol
>>> # Polynomial Function
>>> f = x**2 + x + 1
>>> f
2
x + x + 1
>>> integrate
3 2
x x
── + ── + x
3 2
>>> # Rational Function
>>> f = x/
>>> f
x
────────────
2
x + 2⋅x + 1
>>> integrate
1
log + ─────
x + 1
>>> # Exponential-polynomial functions
>>> f = x**2 * exp * cos
>>> f
2 x
x ⋅ℯ ⋅cos
>>> integrate
2 x 2 x x x
x ⋅ℯ ⋅sin x ⋅ℯ ⋅cos x ℯ ⋅sin ℯ ⋅cos
──────────── + ──────────── - x⋅ℯ ⋅sin + ───────── - ─────────
2 2 2 2
>>> # A non-elementary integral
>>> f = exp * erf
>>> f
2
-x
ℯ ⋅erf
>>> integrate
___ 2
╲╱ π ⋅erf
─────────────
4

Series


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

Logical reasoning

Example 1


>>> from sympy import *
>>> x = Symbol
>>> y = Symbol
>>> facts = Q.positive, Q.positive
>>> with assuming:
... print
True

Example 2


>>> from sympy import *
>>> x = Symbol
>>> # Assumption about x
>>> fact =
>>> with assuming:
... print
True