Symbolic Mathematics with SymPy
Overview
In this article, I will explain SymPy, a Python library for symbolic mathematics. Using SymPy allows you to perform algebraic calculations, calculus, matrix operations, and more symbolically (analytically).
This article covers the following topics:
- Basic Operations
- Algebra (Expansion, Factorization, Solving Equations)
- Calculus (Differentiation, Integration)
- Linear Algebra (Matrices)
Author’s Environment
The author’s environment is as follows.
!sw_vers
ProductName: macOS
ProductVersion: 15.5
BuildVersion: 24F74
!python -V
Python 3.12.12
Load necessary libraries. We will mainly use sympy this time.
import sympy
from sympy import symbols, expand, factor, solve, diff, integrate, sin, cos, Matrix, init_printing, pprint
import numpy as np
from pprint import pprint as py_pprint
# Settings for pretty printing of math formulas
init_printing()
print("sympy version :", sympy.__version__)
sympy version : 1.14.0
1. Basic Operations
In SymPy, by defining variables as Symbols, you can handle mathematical expressions as they are. Here, we define variables $x, y$ and create a simple expression.
x, y = symbols('x y')
expr = x + 2*y
expr
$\displaystyle x + 2 y$
2. Algebra
Expansion
We use the expand function to expand mathematical expressions.
As an example, we expand $(x + 1)^2$.
expr = (x + 1)**2
expanded_expr = expand(expr)
expanded_expr
$\displaystyle x^{2} + 2 x + 1$
Factorization
We use the factor function for factorization.
As an example, we factorize $x^2 + 2x + 1$.
factored_expr = factor(expanded_expr)
factored_expr
$\displaystyle \left(x + 1\right)^{2}$
Solving Equations
We use the solve function to solve equations.
As an example, we solve the quadratic equation $x^2 - 3x + 2 = 0$ for $x$.
eq = x**2 - 3*x + 2
solutions = solve(eq, x)
# Output list results with pprint
py_pprint(solutions)
[1, 2]
3. Calculus
Differentiation
We use the diff function to perform differentiation.
As an example, we differentiate $\sin(x)$ with respect to $x$.
diff_expr = diff(sin(x), x)
diff_expr
$\displaystyle \cos{\left(x \right)}$
Integration
We use the integrate function to perform integration.
As an example, we find the indefinite integral of $\cos(x)$ with respect to $x$.
int_expr = integrate(cos(x), x)
int_expr
$\displaystyle \sin{\left(x \right)}$
Definite integration is also possible. We calculate $\int_{0}^{\pi} \sin(x) dx$.
def_int_expr = integrate(sin(x), (x, 0, sympy.pi))
def_int_expr
$\displaystyle 2$
4. Linear Algebra (Matrices)
Matrices are defined using the Matrix class.
Here, we define matrix $\mathbf{A}$ as follows:
$$ \mathbf{A} = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} $$
A = Matrix([[1, 2], [3, 4]])
A
$\displaystyle \left[\begin{matrix}1 & 2\\3 & 4\end{matrix}\right]$
Determinant
The determinant can be calculated with the det method.
det_A = A.det()
det_A
$\displaystyle -2$
Inverse Matrix
The inverse matrix can be calculated with the inv method.
inv_A = A.inv()
inv_A
$\displaystyle \left[\begin{matrix}-2 & 1\\\frac{3}{2} & - \frac{1}{2}\end{matrix}\right]$
Eigenvalues and Eigenvectors
Eigenvalues and eigenvectors can be calculated with the eigenvects method.
eigen_info = A.eigenvects()
display(eigen_info)
$\displaystyle \left[ \left( \frac{5}{2} - \frac{\sqrt{33}}{2}, \ 1, \ \left[ \left[\begin{matrix}- \frac{\sqrt{33}}{6} - \frac{1}{2}\\1\end{matrix}\right]\right]\right), \ \left( \frac{5}{2} + \frac{\sqrt{33}}{2}, \ 1, \ \left[ \left[\begin{matrix}- \frac{1}{2} + \frac{\sqrt{33}}{6}\\1\end{matrix}\right]\right]\right)\right]$
Conclusion
In this article, I explained the basics of symbolic mathematics using Python’s SymPy library.
Using SymPy allows for accurate execution of complex calculations within programs. The features introduced in this article are just a part of SymPy, but combining them will likely enable you to tackle a wide variety of mathematical problems.