Open In App

What is Symbolic Computation in SymPy?

Last Updated : 20 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to perform symbolic computation in Sympy in Python.

Symbolic Computation in Sympy is used to solve mathematical expressions by integrating mathematics with computer science using mathematical symbols. It manipulates mathematical objects and expressions. Sympy evaluates algebraic expressions exactly using traditional mathematical symbols but not approximately.

Let’s look into an example to differentiate Sympy mathematical operation and normal mathematical operation.

Basic Operations

Using the math module the square root value for 5 is calculated approximately. But using Sympy module the square root of 5 is left unevaluated because it is not a perfect square.

Python3




# import necessary packages
import math
import sympy
  
print('Using math')
print('Sqrt(25)=', math.sqrt(25))
print('Sqrt(5)=', math.sqrt(5))
  
print('\nUsing sympy')
print('Sqrt(25)=', sympy.sqrt(25))
print('Sqrt(5)=', sympy.sqrt(5))


Output

Using math
Sqrt(25)= 5.0
Sqrt(5)= 2.23606797749979

Using sympy
Sqrt(25)= 5
Sqrt(5)= sqrt(5)

Square root of 24 using math module is calculated approximately and sympy calculates square root for perfect squares 24 can be written as 4×6 and  4 can be taken out of square root as 2. So √(24)=2√6.

Python3




import math
import sympy
  
print('Using math')
print('Sqrt(24)=', math.sqrt(24))
  
print('\nUsing sympy')
print('Sqrt(24)=', sympy.sqrt(24))


Output

Using math
Sqrt(24)= 4.898979485566356

Using sympy
Sqrt(24)= 2*sqrt(6)

Sympy also represents the mathematical expressions and symbols in latex form using different methods like rational, sqrt and integrate etc.

Rational Number

Representing the rational numbers using the Rational() method.

Python3




from sympy import Rational
  
r_number = Rational(2, 5)
r_number


Output

Output

Explanation: Rational method accepts two numbers which are the numerator and denominator of rational numbers and represents them in rational form as a/b.

Square root representation

Here will we represent the square root symbols.

Python3




from sympy import sqrt
  
square_root_number = sqrt(2)
square_root_number


Output

Output

Equations

In this section, the equations are expanded and also can be factorized and even equations can be simplified using expand, factor and simplify methods respectively. In addition to the specified operations, we can also solve equations and perform substitution in the equations. All these topics are explained below with examples.

Example 1: Expanding equation

Here first an expression/equation is created i.e., 3P+6Q-R by declaring symbols P, Q, R, and expand() method is used by multiplying P with expression.

Syntax expand(expression)

where expression is a mathematical equation

Python3




from sympy import *
  
P, Q, R = symbols("P Q R")
expression = 3*P+6*Q-R
  
expanded_exp = expand(P*expression)
expanded_exp


Output

Output

Example 2: Factor the equation

Here the expanded expression 3P^2+6PQ-PR  is factored into P(3P+6Q-R) using the factor() method.

Syntax: factor(expanded_expression)

where expanded_expression is the expression we need to factor.

Python3




from sympy import *
  
P, Q, R = symbols("P Q R")
expression = 3*P+6*Q-R
# 3P+6Q-R
  
expanded_exp = expand(P*expression)
# 3P^2+6PQ-PR
  
factor(expanded_exp)


Output

Output

Example 3: Simplifying the equation

The expression \frac{3P2+9}{3} is simplified to P2+3 using simplify() method

Syntax: simplify(expression)

where expression is the any mathematical expression/equation.

Python3




from sympy import *
  
P = Symbol("P")
expression = (3*P**2+9)/3
# (3P^2+9)/3
  
simplified_exp = simplify(expression)
simplified_exp


Output

Output

Example 4: Solving equations

The solve() method returns list of integers containing roots of the equation.

Syntax: solve(equation, symbol)

where equation represents expression/equation to solve,

symbol is the variable present in the equation

Python3




from sympy import *
  
P = Symbol("P")
expression = (P**2+3*P-4)/3
# P^2+3P-4
  
solve(expression, P)


Output

[-4,1]

Example 5: Substitution

Here we will subs() method to apply substitution within the equation.

Syntax: expression.subs(symbol, constant)

where expression holds the equation we are applying substitution.

  • symbol is the variable present in the equation.
  • constant is the value that is replacing the symbol.

Python3




from sympy import *
  
P = Symbol("P")
expression = (P**2+3*P-4)
# P^2+3P-4
  
expression.subs(P, 3)


Output

14

Trigonometry

As everyone knows solving expressions that consist of Trigonometric identities is difficult to solve. It requires many formulae to remember to simplify the expression. In Sympy, there is a method called trigsimp() that simplifies the trigonometric expressions. Let’s look into an example code of implementing trigsimp() method.

Python3




from sympy import *
  
x = Symbol('x')
  
print(trigsimp(cos(x)/sin(x)))
print(trigsimp(sin(x)**2+cos(x)**2))


Output

1/tan(x)
1

Derivative, Integration and Limits

In Sympy, Derivatives, Integration, and Limits are applied to expressions using simple methods. Each of these topics is explained below with a sample code.

Example 1: Derivative

Derivative of an expression is calculated using diff() method. Syntax of diff method is given below-

Syntax diff(expression)

Here expression holds an equation/expression that we perform derivative on it.

Python3




from sympy import *
  
x = Symbol('x')
expression = x**10
diff(expression)


Output

10x9

Example 2: Integration

In Sympy, Integration on expression can be performed using Integrate() method. Syntax of Integrate method is mentioned below-

Python3




from sympy import *
  
x = Symbol('x')
e = Symbol('e')
  
expression = integrate(e**x, x)
expression


Output

Output

Explanation: Integrate method perform integration on ex expression i.e., ∫ex dx to give the above result. 

Example 3: Limits

The limit can be applied to the expression by using limit() method on the expression.

Syntax: limit(expression, symbol, value)

Parameters

  • expression- Mathematical expression/equation
  • symbol- On which symbol basis we need to perform derivative and substitution.
  • value- Value that substitutes the symbol

Python3




from sympy import *
  
x = Symbol('x')
  
print(limit(x**2, x, 5))
print(limit(x**3, x, oo))


Output

25
oo

Special Functions

Sympy provides various special functions like factorial and rewrite methods. Each of these are explained below with an example

Example 1: Factorial of a number

Python3




from sympy import *
  
factorial(5)
#5x4x3x2x1


Output

120

Explanation: factorial method accepts an integer and returns factorial of a number i.e., for 5 it returns 5×4×3×2×1=120

Example 2: rewrite

Using rewrite() method the secant(x) can be rewritten in terms of sine(x) as mentioned above.

Python3




from sympy import *
  
x = Symbol('x')
sec(x).rewrite(sin(x))


Output

Output

Latex

Latex method provides a latex form of a mathematical expression that can be used in words to represent expressions in a nice way. Let’s move into an example of the latex() method. Get the latex code for the fraction of two numbers let it be a/b.

Python3




from sympy import *
  
a = Symbol('a')
b = Symbol('b')
  
latex(a/b)


Output

'\\frac{a}{b}'

This resultant latex code can be used in word and can be converted to the original expression. These latex expressions are mostly used by article writers to represent the expressions in a clear way to avoid confusion.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads