Symbolic computing allows you to use a computer to do all the algebra you might otherwise do by hand.
SymPy is a popular package for symbolic computing.
conda install sympyimport math
import numpy as np
import sympy as sym
A good place to start is the SymPy tutorial
Let’s compare how sympy evaluates sqrt vs. math
print(math.sqrt(8))
print(np.sqrt(8))sym.sqrt(8)Note that jupyter notebooks render the output nicely.
the math package gives a floating point approximation to , whereas sympy simplifies the expression by removing any divisors that are perfect squares.
a = sym.sqrt(8)
print(a)
type(a)Algebraic Expressions¶
Symbolic computing is particularly useful for manipulating algebraic expressions.
from sympy import symbols, expand, factorYou can build expressions using symbols:
x, y = symbols('x y')
expr = 2*x + y
exprLet’s say we forgot how to mulitply monomials
a, b, c, d = symbols('a b c d')
x, y = symbols('x, y')
ex1 = a*x + b
ex2 = c*x + d
ex1 * ex2we can expand an expression by multiplying everything out and combining like terms:
ex = expand(ex1 * ex2)
exWe can also factor an expression that has been expanded:
factor(ex)You can substitute values (or other symbols) for symbols:
ex.subs([(a, 1), (b, 2), (c, 3), (d, y)])ex_sub = ex.subs([(a, 1), (b, 2), (c, 2), (d, 3)])
ex_subYou can evaluate expressions numerically:
ex_sub.evalf(subs={x: 0.1})SymPy uses arbitrary precision arithmetic. The default precision is 10 decimal digits (between single and double precision), but you can increase the precision in evalf
sym.pi.evalf()sym.pi.evalf(100)Symbolic Mathematics¶
Sympy supports just about everything you might have learned in courses on algebra and calculus (and more)
Differentiation¶
expr = sym.sin(x)
print(expr)
print(sym.diff(expr)) # symbolic differentiation w.r.t xexpr = x * y
sym.diff(expr, x) # differentiate w.r.t. xIntegration¶
expr = sym.sin(x)
print(expr)
print(sym.integrate(expr, x)) # indefinite integral w.r.t xthe sympy variable oo (two o symbols) is used to represent
from sympy import oo
expr = sym.exp(-(x**2))
sym.integrate(expr, (x, -oo, oo)) # integration with limitssym.integrate(expr, (x, -1, 1))sym.limit(sym.sin(x)/x, x, 0) # limit as x \to 0Roots¶
You can compute roots of functions using solve (i.e. we solve the system f(x) = 0 for x)
sym.solve(x**2 - 3, x)Symbolic to Numerical Functions¶
You can turn sympy functions into lambda functions that are compatible with numpy using lambdify:
f = sym.sin(x)
g = sym.lambdify(x, f, 'numpy')
g(np.array(np.linspace(0,1,10)))Plotting Functions¶
You can plot a symbolic function using the sympy plot function
f = sym.sin(x)
sym.plot(f)you can change the domain using a tuple with the variable and lower and upper bounds
sym.plot(f, (x, -5,5))There are a variety of keyword arguments you can use for formatting as well. See help(sym.plot) for more information.
Exercises¶
Use sympy to compute solutions to answer the following questions
What is ?
Find a value of
xwheresin(x) = cos(x)
sym.integrate((x)**(-2), (x, 1, oo))from sympy.solvers import solve
from sympy import *
x = Symbol('x')
solve(sin(x)-cos(x),x)