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.
In [1]:
from sympy import *
In [2]:
3 + math.sqrt(3)
Out[2]:
In [3]:
expr = 3 * sqrt(3)
expr
Out[3]:
In [4]:
init_printing(use_latex='mathjax')
In [5]:
expr
Out[5]:
In [6]:
expr = sqrt(8)
expr
Out[6]:
In [7]:
x, y = symbols("x y")
In [8]:
expr = x**2 + y**2
expr
Out[8]:
In [9]:
expr = (x+y)**3
expr
Out[9]:
In [10]:
a = Symbol("a")
In [11]:
a.is_imaginary
In [12]:
b = Symbol("b", integer=True)
In [13]:
b.is_imaginary
Out[13]:
In [14]:
c = Symbol("c", positive=True)
In [15]:
c.is_positive
Out[15]:
In [16]:
c.is_imaginary
Out[16]:
In [17]:
I
Out[17]:
In [18]:
I ** 2
Out[18]:
In [19]:
Rational(1,3)
Out[19]:
In [20]:
Rational(1,3) + Rational(1,2)
Out[20]:
In [21]:
expr = Rational(1,3) + Rational(1,2)
N(expr)
Out[21]:
In [22]:
N(pi, 100)
Out[22]:
In [23]:
pi.evalf(100)
Out[23]:
In [24]:
expr = x**2 + 2*x + 1
expr
Out[24]:
In [25]:
expr.subs(x, 1)
Out[25]:
In [26]:
expr = pi * x**2
expr
Out[26]:
In [27]:
expr.subs(x, 3)
Out[27]:
In [28]:
N(_)
Out[28]:
In [29]:
expr = (x + y) ** 2
expr
Out[29]:
In [30]:
expand(expr)
Out[30]:
In [31]:
factor(_)
Out[31]:
In [32]:
expr = (2*x + Rational(1,3)*x + 4) / x
expr
Out[32]:
In [33]:
simplify(expr)
Out[33]:
In [34]:
expr = "(2*x + 1/3*x + 4)/x"
simplify(expr)
Out[34]:
In [35]:
expr = sin(x)/cos(x)
expr
Out[35]:
In [36]:
simplify(expr)
Out[36]:
In [37]:
expr = 1/(x**2 + 2*x)
expr
Out[37]:
In [38]:
apart(expr)
Out[38]:
In [39]:
together(_)
Out[39]:
In [40]:
diff(sin(x), x)
Out[40]:
In [41]:
diff(log(x**2 + 1) + 2*x, x)
Out[41]:
In [42]:
integrate(cos(x), x)
Out[42]:
In [43]:
Integral(sin(x), (x,0,pi))
Out[43]:
In [44]:
N(_)
Out[44]:
In [45]:
expr = Sum(1/(x**2 + 2*x), (x, 1, 10))
expr
Out[45]:
In [46]:
expr.doit()
Out[46]:
In [47]:
expr = Product(1/(x**2 + 2*x), (x, 1, 10))
expr
Out[47]:
In [48]:
expr.doit()
Out[48]:
In [49]:
expr = 2*x + 1
solve(expr)
Out[49]:
In [50]:
expr = x**2 - 1
solve(expr)
Out[50]:
In [51]:
expr_1 = 2*x + y + 3
expr_2 = 2*y - x
solve([expr_1, expr_2],(x,y))
Out[51]:
In [52]:
from sympy.physics import units as u
In [53]:
5. * u.milligram
Out[53]:
In [54]:
1./2 * u.inch
Out[54]:
In [55]:
1. * u.nano
Out[55]:
In [56]:
u.watt
Out[56]:
In [57]:
u.ohm
Out[57]:
In [58]:
kmph = u.km / u.hour
mph = u.mile / u.hour
N(mph / kmph)
Out[58]:
In [69]:
80 * N(mph / kmph)
Out[69]:
In [59]:
def sympy_expr(x_val):
expr = x**2 + sqrt(3)*x - Rational(1,3)
return expr.subs(x, x_val)
In [60]:
sympy_expr(3)
Out[60]:
In [61]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [62]:
list1 = np.arange(1,1000)
list2 = pd.Series(list1)
In [63]:
%timeit [sympy_expr(item) for item in list1]
%timeit [sympy_expr(item) for item in list2]
In [64]:
%timeit np.vectorize(sympy_expr)(list1)
%timeit list2.apply(sympy_expr)
In [65]:
expr = x**2 + sqrt(3)*x - Rational(1,3)
lf = lambdify(x, expr)
In [66]:
%timeit lf(list1)
%timeit lf(list2)
In [67]:
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();