How to Perform Basic Arithmetic Operations with SymPy

How to Perform Basic Arithmetic Operations with SymPy

SymPy is a Python library for symbolic mathematics. It provides a plethora of tools for algebraic manipulation, calculus, discrete mathematics, and other areas of mathematics.

Before we begin, make sure you have SymPy installed. You can do this using pip:

pip install sympy

After this, make sure you import SymPy into your Python script in order to be able to execute the code excerpts below.

import sympy as sp

Basic Arithmetic Operations

Let’s first have a look at declaring symbols and performing basic arithmetic.

Declaring Symbols

In SymPy, you use symbols to represent mathematical variables. These symbols can then be used in expressions and equations.

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

Addition and Subtraction

You can easily perform addition and subtraction with SymPy symbols:

# Addition
addition = x + y
print(addition)

# Subtraction
subtraction = x - y
print(subtraction)

Output:

x + y
x - y

Multiplication and Division

Similarly, you can perform multiplication and division:

# Multiplication
multiplication = x * y
print(multiplication)

# Division
division = x / y
print(division)

Output:

x*y
x/y

Simplification of Expressions

SymPy can simplify algebraic expressions to their simplest form, which is useful for making complex-looking expressions more manageable. Use SymPy’s simplify function for this.

expr = (x**2 + 2*x + 1)/(x + 1)
simplified_expr = sp.simplify(expr)
print(simplified_expr)

Output:

x + 1

Expanding Expressions

Expanding an expression is the process of distributing products and sums to get a polynomial form. SymPy provides the expand function to accomplish this.

expanded_expr = sp.expand((x + y)**2)
print(expanded_expr)

Output:

x**2 + 2*x*y + y**2

Factoring Expressions

Factoring is the reverse of expanding. With factorization, we want to rewrite a polynomial as a product of simpler polynomials. This can be helpful for solving equations. Use SymPy’s factor function to accomplish this.

factored_expr = sp.factor(x**2 - y**2)
print(factored_expr)

Output:

(x - y)*(x + y)

Substitution in Expressions

SymPy allows you to substitute a symbol with a specific value or another expression. This can be useful for evaluating expressions at specific points. Use the SymPy subs function to do so.

substituted_expr = (x + y).subs(x, 2)
print(substituted_expr)

Output:

y + 2

Evaluating Expressions

For numerical evaluation, you can use the N function, which substitutes numerical values for symbols and evaluates the result.

expr = x + y
expr_subs = expr.subs({x: 2, y: 3})
evaluated_expr = sp.N(expr_subs)
print(evaluated_expr)

Output:

5.00000000000000

Handling Complex Numbers

SymPy can handle complex numbers seamlessly. You can use the imaginary unit I for complex arithmetic.

z = sp.symbols('z')
complex_expr = sp.I * z + 1
print(complex_expr)

complex_expr_subs = complex_expr.subs({z: 2})
evaluated_complex_expr = sp.N(complex_expr_subs)
print(evaluated_complex_expr)

Output:

I*z + 1
1.0 + 2.0*I

Final Thoughts

SymPy is a clearly powerful tool for performing symbolic arithmetic operations. It simplifies complex algebraic manipulations, making it an essential tool for any Python programmer looking to perform such operations.

Leave a Reply

Your email address will not be published. Required fields are marked *