SymPy is symbolic mathematics library written completely in Python and doesn't require any dependencies.
Finding Help:
![]() |
NumPyBase N-dimensional array package |
![]() |
SciPyFundamental library for scientific computing |
![]() |
MatplotlibComprehensive 2D Plotting |
![]() |
IPythonEnhanced Interactive Console |
![]() |
SymPySymbolic mathematics |
![]() |
PandasData structures & analysis |
SymPy is a Python library for symbolic mathematics. It aims to become a full-featured computer algebra system (CAS) while keeping the code as simple as possible in order to be comprehensible and easily extensible. SymPy is written entirely in Python and does not require any external libraries.¶
from sympy import *
3 + math.sqrt(3)
4.732050807568877
expr = 3 * sqrt(3)
expr
3*sqrt(3)
init_printing(use_latex='mathjax')
expr
expr = sqrt(8)
expr
symbols()
& Symbol()
¶x, y = symbols("x y")
expr = x**2 + y**2
expr
expr = (x+y)**3
expr
a = Symbol("a")
a.is_imaginary
b = Symbol("b", integer=True)
b.is_imaginary
False
c = Symbol("c", positive=True)
c.is_positive
True
c.is_imaginary
False
I
I ** 2
Rational()
¶Rational(1,3)
Rational(1,3) + Rational(1,2)
expr = Rational(1,3) + Rational(1,2)
N(expr)
N(pi, 100)
pi.evalf(100)
subs()
¶expr = x**2 + 2*x + 1
expr
expr.subs(x, 1)
expr = pi * x**2
expr
expr.subs(x, 3)
N(_)
factor()
and expand()
¶expr = (x + y) ** 2
expr
expand(expr)
factor(_)
simplify()
¶expr = (2*x + Rational(1,3)*x + 4) / x
expr
simplify(expr)
expr = "(2*x + 1/3*x + 4)/x"
simplify(expr)
expr = sin(x)/cos(x)
expr
simplify(expr)
apart()
and together()
¶expr = 1/(x**2 + 2*x)
expr
apart(expr)
together(_)
diff(sin(x), x)
diff(log(x**2 + 1) + 2*x, x)
integrate(cos(x), x)
Integral(sin(x), (x,0,pi))
N(_)
Sum()
¶expr = Sum(1/(x**2 + 2*x), (x, 1, 10))
expr
expr.doit()
Product()
¶expr = Product(1/(x**2 + 2*x), (x, 1, 10))
expr
expr.doit()
Solve()
¶expr = 2*x + 1
solve(expr)
expr = x**2 - 1
solve(expr)
expr_1 = 2*x + y + 3
expr_2 = 2*y - x
solve([expr_1, expr_2],(x,y))
from sympy.physics import units as u
5. * u.milligram
1./2 * u.inch
1. * u.nano
u.watt
u.ohm
kmph = u.km / u.hour
mph = u.mile / u.hour
N(mph / kmph)
80 * N(mph / kmph)
def sympy_expr(x_val):
expr = x**2 + sqrt(3)*x - Rational(1,3)
return expr.subs(x, x_val)
sympy_expr(3)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
list1 = np.arange(1,1000)
list2 = pd.Series(list1)
%timeit [sympy_expr(item) for item in list1]
%timeit [sympy_expr(item) for item in list2]
1 loops, best of 3: 103 ms per loop 10 loops, best of 3: 105 ms per loop
%timeit np.vectorize(sympy_expr)(list1)
%timeit list2.apply(sympy_expr)
10 loops, best of 3: 776 ms per loop 1 loops, best of 3: 1.09 s per loop
expr = x**2 + sqrt(3)*x - Rational(1,3)
lf = lambdify(x, expr)
%timeit lf(list1)
%timeit lf(list2)
1000 loops, best of 3: 242 µs per loop 100 loops, best of 3: 4.8 ms per loop
fig = plt.figure()
axes = fig.add_subplot(111)
x_vals = np.linspace(-5.,5.)
y_vals = lf(x_vals)
axes.grid()
axes.plot(x_vals, y_vals)
plt.show();