How to Use SymPy for Symbolic Mathematics in Python

SymPy is a Python library used for symbolic mathematics. It allows you to manipulate mathematical expressions symbolically, solving equations, integrating functions, performing algebraic operations, and much more. In this tutorial, you will learn the basics of using SymPy for symbolic mathematics in Python.

Installation

SymPy comes pre-installed with Anaconda, so if you are using Anaconda distribution, you already have SymPy installed. If you are using a different Python distribution or need to install SymPy for any other reason, you can install it using pip:

pip install sympy

To verify the installation, open a Python interpreter or a Jupyter Notebook and import the SymPy library:

import sympy

If no errors occur, the installation was successful.

Creating Symbols

In SymPy, symbols are objects that represent mathematical symbols, such as variables or constants. To create a symbol, you can use the symbols() function:

import sympy as sp

# Create a symbol
x = sp.symbols('x')

In this example, we created a symbol x. You can create multiple symbols at once by providing multiple symbol names as a string separated by spaces:

x, y, z = sp.symbols('x y z')

Manipulating Expressions

Once you have created symbols, you can use them to define mathematical expressions. SymPy provides various functions and operators to manipulate these expressions. Here are some basic examples:

Addition and Subtraction

# Create symbols
x, y = sp.symbols('x y')

# Define expressions
expr1 = x + y
expr2 = x - y

# Print expressions
print(expr1)  # x + y
print(expr2)  # x - y

Multiplication and Division

# Create symbols
x, y = sp.symbols('x y')

# Define expressions
expr1 = x * y
expr2 = x / y

# Print expressions
print(expr1)  # x*y
print(expr2)  # x/y

Powers

# Create symbols
x, y = sp.symbols('x y')

# Define expressions
expr1 = x**2
expr2 = x**y

# Print expressions
print(expr1)  # x**2
print(expr2)  # x**y

Square Root and Other Functions

# Create symbols
x = sp.symbols('x')

# Define expressions
expr1 = sp.sqrt(x)
expr2 = sp.sin(x)

# Print expressions
print(expr1)  # sqrt(x)
print(expr2)  # sin(x)

Simplifying Expressions

SymPy provides a simplify() function to simplify expressions. It applies various mathematical rules and simplifies the expression as much as possible.

# Create a symbol
x = sp.symbols('x')

# Define an expression
expr = (x**2 + 2*x + 1) / (x + 1)

# Simplify the expression
simplified_expr = sp.simplify(expr)

# Print the simplified expression
print(simplified_expr)  # x + 1

In this example, the expression (x**2 + 2*x + 1) / (x + 1) is simplified to x + 1.

Solving Equations

SymPy provides a powerful solve() function to solve equations symbolically. It can solve linear and polynomial equations, transcendental equations, differential equations, and more.

Linear Equations

# Create symbols
x, y = sp.symbols('x y')

# Define an equation
eqn = sp.Eq(2*x + 3*y, 10)

# Solve the equation
sol = sp.solve(eqn, (x, y))

# Print the solutions
print(sol)  # [{x: -3*y/2 + 5}]

In this example, the equation 2*x + 3*y = 10 is solved for variables x and y. The solution is stored in a dictionary where the keys are the symbols and values are the solutions.

Transcendental Equations

# Create a symbol
x = sp.symbols('x')

# Define an equation
eqn = sp.Eq(sp.sin(x) + sp.cos(x), 1)

# Solve the equation
sol = sp.solve(eqn, x)

# Print the solutions
print(sol)  # [pi/4]

In this example, the equation sin(x) + cos(x) = 1 is solved for x. The solution is a list of values, in this case, [pi/4].

Differential Equations

# Create a symbol
x = sp.symbols('x')

# Define a differential equation
eqn = sp.Eq(sp.diff(x**2, x) - x, 0)

# Solve the differential equation
sol = sp.dsolve(eqn)

# Print the solution
print(sol)  # Eq(x**2/2 - x**2*log(x) + C1, 0)

In this example, the differential equation d(x^2)/dx - x = 0 is solved. The solution is x**2/2 - x**2*log(x) + C1 = 0, where C1 is an arbitrary constant.

Differentiation and Integration

SymPy provides functions to differentiate and integrate expressions symbolically.

Differentiation

# Create a symbol
x = sp.symbols('x')

# Define an expression
expr = sp.sin(x)

# Differentiate the expression
deriv = sp.diff(expr, x)

# Print the derivative
print(deriv)  # cos(x)

In this example, the expression sin(x) is differentiated with respect to x. The derivative is cos(x).

Integration

# Create a symbol
x = sp.symbols('x')

# Define an expression
expr = sp.exp(x)

# Integrate the expression
integral = sp.integrate(expr, x)

# Print the integral
print(integral)  # exp(x)

In this example, the expression exp(x) is integrated with respect to x. The integral is exp(x).

Limit Evaluation

SymPy allows you to evaluate limits of functions symbolically. The limit() function is used to find the limit as a variable approaches a specific value.

# Create a symbol
x = sp.symbols('x')

# Define a function
func = x / sp.sin(x)

# Evaluate the limit
limit_value = sp.limit(func, x, 0)

# Print the limit value
print(limit_value)  # 1

In this example, the limit of x / sin(x) as x approaches 0 is evaluated. The limit value is 1.

Linear Algebra

SymPy provides functions to perform basic linear algebra operations, such as matrix operations, solving linear systems, eigenvalues, eigenvectors, and more.

Matrix Operations

# Create symbols
x, y, z = sp.symbols('x y z')

# Create matrices
A = sp.Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 10]])
B = sp.Matrix([x, y, z])

# Matrix multiplication
C = A * B

# Print the result
print(C)  # Matrix([[x + 2*y + 3*z], [4*x + 5*y + 6*z], [7*x + 8*y + 10*z]])

In this example, matrix A is multiplied by vector B. The result is a new vector C.

Solving Linear Systems

# Create symbols
x, y, z = sp.symbols('x y z')

# Create a linear system
eqns = [sp.Eq(x + y + z, 1), sp.Eq(x - y + 2*z, 3), sp.Eq(2*x + y - z, 0)]

# Solve the linear system
sol = sp.solve(eqns, (x, y, z))

# Print the solutions
print(sol)  # {x: 1/4, y: 1/4, z: 1/2}

In this example, a linear system of equations is solved for variables x, y, and z. The solution is a dictionary where the keys are the symbols and values are the solutions.

Eigenvalues and Eigenvectors

# Create a symbol
x = sp.symbols('x')

# Create a matrix
A = sp.Matrix([[1, 2], [3, x]])

# Compute eigenvalues and eigenvectors
eigenvals = A.eigenvals()
eigenvects = A.eigenvects()

# Print the results
print(eigenvals)  # {x: sqrt(7)/2 - 1/2, sqrt(7)/2 + 1/2}
print(eigenvects)  # [(sqrt(7)/2 - 1/2, 1, [[-(1/3 - sqrt(7)/6)], [1]]), (sqrt(7)/2 + 1/2, 1, [[-(1/3 + sqrt(7)/6)], [1]])]

In this example, eigenvalues and eigenvectors of matrix A are computed. The eigenvalues are represented as a dictionary, and the eigenvectors are represented as a list of tuples.

Conclusion

In this tutorial, you learned the basics of using SymPy for symbolic mathematics in Python. You learned how to create symbols, manipulate expressions, simplify expressions, solve equations, differentiate and integrate expressions, evaluate limits, and perform basic linear algebra operations. SymPy is a powerful library for symbolic mathematics, and it can be used to solve a wide range of mathematical problems in Python.

Related Post