Basics
Define Variables
# Symbols
# possible arguments: ['complex', 'extended_negative', 'extended_nonnegative', 'extended_nonpositive', 'extended_nonzero, 'extended_positive', 'extended_real', 'finite', 'imaginary', 'integer', 'irrational', 'negative', 'nonnegative', 'nonpositive', 'nonzero', 'positive', 'rational', 'real', ...]
x: smp.Symbol = smp.symbols('x', real=True, positive=True)
# Multiple symbols
symbols: list[smp.Symbol] = smp.symbols('x, y, z')
x, y, z = symbols
# smp.symbols supports latex syntax and ':' operation.
symbols: list[smp.Symbol] = smp.symbols(r'a:c, x1:5, \alpha, y:2(1:3)')
# (a, b, c, x1, x2, x3, x4, \alpha, y01, y02, y11, y12)
# Functions
f: smp.Function = smp.symbols('f', cls=smp.Function)Simplification and Evaluation
# make the expression simpler
f.simplify()
# Evaluate the expression (integrals, differentiations)
f.doit()
# Evaluate an expression in a number (float)
f.evalf() # = f.n()Solve an Equation
The function smp.solve(f, x) finds the value of x that satisfies .
x: smp.Symbol = smp.symbols('x')
f = (x**2 + 4*x + 3)**2
sols = smp.solve(f, x)
f = smp.sin(x)
sols = smp.solve(f, x)Convert Symbolic Sympy Expression into Numerical Numpy function
smp.lambdify() bridges between simpy and numpy and scipy.
x: smp.Symbol = smp.symbols('x')
expr = x**2 + smp.sin(x)
expr_f = smp.lambdify([x], expr)
x_range = np.linspace(-3, 3, 100)
y_range = expr_f(np.linspace(-3, 3, 100))Substitute Variables in an Expression
x: smp.Symbol = smp.symbols('x')
f = x**2 + smp.sin(x) + 3
f.subs([(x, smp.pi)])Calculus
Series
x: smp.Symbol = smp.symbols('x', real=True)
n: smp.Symbol = smp.symbols('n', integer=True, nonnegative=True)
expr = 6 / 4**n
res = smp.Sum(expr, (n, 0, smp.oo)).doit()
display(res)Limits
x: smp.Symbol = smp.symbols('x')
expr = smp.sin(x / 2 + smp.sin(x))
smp.limit(expr, x, smp.pi)Derivatives
x: smp.Symbol = smp.symbols('x')
expr = smp.sin(x / 2 + smp.sin(x))
smp.diff(expr, x)Integrals
x: smp.Symbol = smp.symbols('x')
expr = smp.sin(x / 2 + smp.sin(x))
smp.integrate(expr, x)Definite Integrals
x: smp.Symbol = smp.symbols('x')
expr = smp.sin(x / 2 + smp.sin(x))
smp.integrate(expr, (x, 0, 1)) # from 0 to 1Multivariable Calculus
Define Vectors and Matrices
symbols: list[smp.Symbol] = smp.symbols('u1:4, v1:4, t')
u1, u2, u3, v1, v2, v3, t = symbols
u = smp.Matrix([u1, u2, u3])
v = smp.Matrix([v1, v2, v3])
smp.Matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])Vector Operations
# Transpose
v.transpose()
# Vector addition/subtraction and scalar multiplication
res = 2 * u + v
# Dot product
u.dot(v)
# Cross product
u.cross(v)
# Norm of a vector
u.norm()
# projection
proj_v_u = u.dot(v) / v.norm() ** 2 * v
# Vector representation of a line
r0 = smp.Matrix([1, 1, 1])
v = smp.Matrix([1, 3, -1])
line = r0 + t * v
# Vector representation of a plane
P0 = smp.Matrix([4, 4, 8])
P = smp.Matrix([x, y, z])
n = smp.Matrix([1, 1, 1])
plane = n.dot(P0 - P)
# concat two matrices
u.col_join(v) # axis=0
u.row_join(v) # axis=1
Vector Derivatives
r = smp.Matrix([3 * t, smp.sin(t), t ** 2])
smp.diff(r, t) # dr/dtVector Integrals
r = smp.Matrix([smp.exp(t) * smp.cos(t), t ** 4, 1 / (t ** 2 + 1)])
smp.integrate(r, t))Numerical Integration
from scipy.integrate import quad_vec
r = smp.Matrix([smp.exp(t ** 2) * smp.cos(t) ** 3, smp.exp(-t ** 4), 1 / (t ** 2 + 3)])
display(r)
r_f = smp.lambdify([t], r)
quad_vec(r_f, 0, 1)[0] Arc-Length
r = smp.Matrix([0, t, t ** 2])
integrand = smp.diff(r, t).norm()
arc_len = smp.integrate(integrand, (t, 0, 1)) Partial Derivatives
symbols:list[smp.Symbol] = smp.symbols('x, y', real=True)
x, y = symbols
f = y**2 * smp.sin(x+y)
dfdx = smp.diff(f, x)
dfdy = smp.diff(f, y)
# d^3f/dxdy^2
smp.diff(f, x, y, y) # = smp.diff(f, x, (y, 2))Chain-Rule
t:smp.Symbol = smp.symbols('t', real=True)
functions:list[smp.Function] = smp.symbols('x, y, z, w', cls=smp.Function)
x, y, z, w = functions
x, y, z = x(t), y(t), z(t)
w = w(x, y, z)
smp.diff(w, t)