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 Math with SymPy

Open in Colab
Source

SymPy is the SciPy ecosystem’s library for symbolic mathematics: it manipulates expressions exactly, like a free Mathematica built on Python. Where NumPy gives you numbers, SymPy gives you formulas: it can expand, factor, differentiate, integrate, and solve algebraically. Before a variable can appear in an expression it must be declared a symbol with symbols().

Loading...

Algebra

The everyday manipulations are expand, factor, and simplify.

Loading...
Loading...
Loading...

To solve an equation (SymPy sets the expression equal to zero), use solve:

[-3.20000000000000, 1.80000000000000]

Calculus

Differentiate with diff: the first argument is the expression, the rest are the variables to differentiate by. Repeating a variable (or giving a number) takes higher derivatives.

Loading...
Loading...

Integrate with integrate. With no limits you get an antiderivative; with limits, a definite integral. The symbol oo means infinity, so the Gaussian integral central to quantum mechanics is one line:

Loading...
Loading...

A Taylor series expansion uses series. By default it expands about x=0x = 0; extra arguments set the center and the order.

Loading...

A quantum-mechanical example: hydrogen radial functions

SymPy ships the hydrogen atom’s exact wavefunctions in its physics module. Here we pull the 2p2p radial function R21(r)R_{21}(r), find the radius where the radial probability density r2R212r^2 R_{21}^2 peaks, and plot it. This is a worked symbolic-to-numeric pipeline: solve exactly, then lambdify into a NumPy function to plot.

Loading...
4.0
<Figure size 960x720 with 1 Axes>

The peak of the 2p2p radial density sits at r=4 a0r = 4\,a_0, exactly the Bohr-model radius of the n=2n = 2 orbit.

Exercises

  1. Factor x2+x−6x^2 + x - 6.

  2. Expand (x−2)(x+5) x(x - 2)(x + 5)\,x.

  3. Evaluate ∫0ln⁡4exe2x+9 dx\displaystyle\int_0^{\ln 4} \frac{e^x}{\sqrt{e^{2x} + 9}}\,dx with integrate (use log(4) for the upper limit).

  4. The reversible isothermal work of a gas is w=∫ViVf−nRTV dVw = \int_{V_i}^{V_f} -\frac{nRT}{V}\,dV. Integrate it symbolically, then evaluate for n=2.44n = 2.44 mol, T=298T = 298 K, Vi=0.552V_i = 0.552 L, Vf=1.32V_f = 1.32 L.

Try it live

Loading...