Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Symbolic Computing

Open in Colab

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 sympy

A good place to start is the SymPy tutorial

Let’s compare how sympy evaluates sqrt vs. math

Note that jupyter notebooks render the output nicely.

the math package gives a floating point approximation to 8\sqrt{8}, whereas sympy simplifies the expression by removing any divisors that are perfect squares.

Algebraic Expressions

Symbolic computing is particularly useful for manipulating algebraic expressions.

You can build expressions using symbols:

Let’s say we forgot how to mulitply monomials

we can expand an expression by multiplying everything out and combining like terms:

We can also factor an expression that has been expanded:

You can substitute values (or other symbols) for symbols:

You can evaluate expressions numerically:

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

Symbolic Mathematics

Sympy supports just about everything you might have learned in courses on algebra and calculus (and more)

Differentiation

Integration

the sympy variable oo (two o symbols) is used to represent ∞\infty

Limits

You can compute limits. For example:

lim⁡x→0sin⁡(x)x\lim_{x\to 0} \frac{\sin(x)}{x}

Roots

You can compute roots of functions using solve (i.e. we solve the system f(x) = 0 for x)

Symbolic to Numerical Functions

You can turn sympy functions into lambda functions that are compatible with numpy using lambdify:

Plotting Functions

You can plot a symbolic function using the sympy plot function

you can change the domain using a tuple with the variable and lower and upper bounds

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

  1. What is ∫1∞x−2 dx\int_{1}^{\infty} x^{-2}\, dx?

  2. Find a value of x where sin(x) = cos(x)