![]() |
VOOZH | about |
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.
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.
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.
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.
Representing the rational numbers using the Rational() method.
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.
Here will we represent the square root symbols.
Output
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.
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
Output
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.
Output
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.
Output
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
Output
[-4,1]
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.
Output
14
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.
Output
1/tan(x) 1
In Sympy, Derivatives, Integration, and Limits are applied to expressions using simple methods. Each of these topics is explained below with a sample code.
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.
Output
10x9
In Sympy, Integration on expression can be performed using Integrate() method. Syntax of Integrate method is mentioned below-
Output
Explanation: Integrate method perform integration on ex expression i.e., âĢex dx to give the above result.
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
Output
25 oo
Sympy provides various special functions like factorial and rewrite methods. Each of these are explained below with an example
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
Using rewrite() method the secant(x) can be rewritten in terms of sine(x) as mentioned above.
Output
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.
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.