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
- Basic arithmetic: *, /, +, -, **
- Simplification
- Expansion
- Functions: trigonometric, hyperbolic, exponential, roots, logarithms, absolute value, spherical harmonics, factorials and gamma functions, zeta functions, polynomials, hypergeometric, special functions,...
- Substitution
- Arbitrary precision integers, rationals and floats
- Noncommutative symbols
- Pattern matching
Polynomials
- Basic arithmetic: division, gcd,...
- Factorization
- Square-free factorization
- Gröbner bases
- Partial fraction decomposition
- Resultants
Calculus
- Limits
- Differentiation
- Integration: Implemented Risch–Norman heuristic
- Taylor series
Solving equations
- Polynomials
- Systems of equations
- Algebraic equations
- Differential equations
- Difference equations
Discrete math
- Binomial coefficients
- Summations
- Products
- Number theory: generating prime numbers, primality testing, integer factorization,...
- Logic expressions
- Logical Reasoning
Matrices
- Basic arithmetic
- Eigenvalues/eigenvectors
- Determinants
- Inversion
- Solving
Geometry
- Points, lines, rays, segments, ellipses, circles, polygons,...
- Intersections
- Tangency
- Similarity
Plotting
- Coordinate models
- Plotting Geometric Entities
- 2D and 3D
- Interactive interface
- Colors
- Animations
Physics
- Units
- Classical mechanics
- Continuum Mechanics
- Quantum mechanics
- Gaussian optics
- Pauli algebra
Statistics
- Normal distributions
- Uniform distributions
- Probability
Combinatorics
- Permutations
- Combinations
- Partitions
- Subsets
- Permutation group: Polyhedral, Rubik, Symmetric,...
- Prufer sequence and Gray Codes
Printing
- Pretty-printing: ASCII/Unicode pretty-printing, LaTeX
- Code generation: C, Fortran, Python
Related projects
- SageMath: an open source alternative to Mathematica, Maple, MATLAB, and Magma
- : a rewriting of SymPy's core in C++, in order to increase its performance. Work is currently in progress to make SymEngine the underlying engine of Sage too.
- : a Python library for arbitrary-precision floating-point arithmetic
- : another Python computer algebra system
- : Software for solving systems of coupled partial differential equations by the finite element method in 1D, 2D and 3D.
- : Geometric algebra module.
- : Quantum Monte Carlo in Python.
- : Experimental Python package for teaching linear circuit analysis.
- : Easy LaTeX typesetting of algebraic expressions in symbolic form with automatic substitution and result computation.
- : Adding statistical operations to complex physical models.
- : a fork of the SymPy, started by Sergey B Kirpichev
Dependencies
There are several optional dependencies that can enhance its capabilities:
- gmpy: If gmpy is installed, the SymPy's polynomial module will automatically use it for faster ground types. This can provide a several times boost in performance of certain operations.
- matplotlib: If matplotlib is installed, SymPy can use it for plotting.
- Pyglet: Alternative plotting package.
Usage examples
Pretty-printing
Sympy allows outputs to be formatted into a more appealing format through thepprint
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:
True
Example 2
>>> from sympy import *
>>> x = Symbol
>>> # Assumption about x
>>> fact =
>>> with assuming:
True