Disclaimer: Notebook taken from https://www.youtube.com/channel/UC98x5I1LVPhtnUHDyujq7zg
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 *
import math
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 [ ]:
x, y = symbols("x y")
In [ ]:
expr = x**2 + y**2
expr
In [ ]:
expr = (x+y)**3
expr
In [ ]:
a = Symbol("a")
In [ ]:
a.is_imaginary
In [ ]:
b = Symbol("b", integer=True)
In [ ]:
b.is_imaginary
In [ ]:
c = Symbol("c", positive=True)
In [ ]:
c.is_positive
In [ ]:
c.is_imaginary
In [ ]:
I
In [ ]:
I ** 2
In [ ]:
Rational(1,3)
In [ ]:
Rational(1,3) + Rational(1,2)
In [ ]:
expr = Rational(1,3) + Rational(1,2)
N(expr)
In [ ]:
N(pi, 100)
In [ ]:
pi.evalf(100)
In [ ]:
expr = x**2 + 2*x + 1
expr
In [ ]:
expr.subs(x, 1)
In [ ]:
expr = pi * x**2
expr
In [ ]:
expr.subs(x, 3)
In [ ]:
N(_)
In [ ]:
expr = (x + y) ** 2
expr
In [ ]:
expand(expr)
In [ ]:
factor(_)
In [ ]:
expr = (2*x + Rational(1,3)*x + 4) / x
expr
In [ ]:
simplify(expr)
In [ ]:
expr = "(2*x + 1/3*x + 4)/x"
simplify(expr)
In [ ]:
expr = sin(x)/cos(x)
expr
In [ ]:
simplify(expr)
In [ ]:
expr = 1/(x**2 + 2*x)
expr
In [ ]:
apart(expr)
In [ ]:
together(_)
In [ ]:
diff(sin(x), x)
In [ ]:
diff(log(x**2 + 1) + 2*x, x)
In [ ]:
integrate(cos(x), x)
In [ ]:
Integral(sin(x), (x,0,pi))
In [ ]:
N(_)
In [ ]:
expr = Sum(1/(x**2 + 2*x), (x, 1, 10))
expr
In [ ]:
expr.doit()
In [ ]:
expr = Product(1/(x**2 + 2*x), (x, 1, 10))
expr
In [ ]:
expr.doit()
In [ ]:
expr = 2*x + 1
solve(expr)
In [ ]:
expr = x**2 - 1
solve(expr)
In [ ]:
expr_1 = 2*x + y + 3
expr_2 = 2*y - x
solve([expr_1, expr_2],(x,y))
In [ ]:
from sympy.physics import units as u
In [ ]:
5. * u.milligram
In [ ]:
1./2 * u.inch
In [ ]:
1. * u.nano
In [ ]:
u.watt
In [ ]:
u.ohm
In [ ]:
kmph = u.km / u.hour
mph = u.mile / u.hour
N(mph / kmph)
In [ ]:
80 * N(mph / kmph)
In [ ]:
def sympy_expr(x_val):
expr = x**2 + sqrt(3)*x - Rational(1,3)
return expr.subs(x, x_val)
In [ ]:
sympy_expr(3)
In [ ]:
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [ ]:
list1 = np.arange(1,1000)
list2 = pd.Series(list1)
In [ ]:
%timeit [sympy_expr(item) for item in list1]
%timeit [sympy_expr(item) for item in list2]
In [ ]:
%timeit np.vectorize(sympy_expr)(list1)
%timeit list2.apply(sympy_expr)
In [ ]:
expr = x**2 + sqrt(3)*x - Rational(1,3)
lf = lambdify(x, expr)
In [ ]:
%timeit lf(list1)
%timeit lf(list2)
In [ ]:
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();
In [ ]: