In this article, we are going to see how to perform symbolic computation in Sympy in Python. Show
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 OperationsUsing 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
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
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 NumberRepresenting the rational numbers using the Rational() method. Python3
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 representationHere will we represent the square root symbols. Python3
Output Output EquationsIn 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 equationHere 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.
Python3
Output
Output Example 2: Factor the equationHere the expanded expression 3P^2+6PQ-PR is factored into P(3P+6Q-R) using the factor() method.
Python3
Output Output Example 3: Simplifying the equationThe expression \frac{3P2+9}{3} is simplified to P2+3 using simplify() method
Python3
Output Output Example 4: Solving equationsThe solve() method returns list of integers containing roots of the equation.
Python3
Output [-4,1] Example 5: SubstitutionHere we will subs() method to apply substitution within the equation.
Python3
Output 14 TrigonometryAs 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
Output 1/tan(x) 1 Derivative, Integration and LimitsIn 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: DerivativeDerivative of an expression is calculated using diff() method. Syntax of diff method is given below-
Python3
Output 10x9 Example 2: IntegrationIn Sympy, Integration on expression can be performed using Integrate() method. Syntax of Integrate method is mentioned below- Python3
Output Output Explanation: Integrate method perform integration on ex expression i.e., ∫ex dx to give the above result. Example 3: LimitsThe limit can be applied to the expression by using limit() method on the expression.
Python3
Output 25 oo Special FunctionsSympy provides various special functions like factorial and rewrite methods. Each of these are explained below with an example Example 1: Factorial of a numberPython3
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: rewriteUsing rewrite() method the secant(x) can be rewritten in terms of sine(x) as mentioned above. Python3
Output Output LatexLatex 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
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. |