Python sympy.diff() Method

Python SymPy Module:

SymPy is a Python symbolic mathematics library. It aims to be a full-featured computer algebra system (CAS) while keeping the code as basic(simple) as possible in order to be understandable and easily expandable. SymPy is entirely written in Python. SymPy is simple to use because it only depends on mpmath, a pure Python library for arbitrary floating-point arithmetic.

Rational and Integer are the numerical types defined by SymPy. A rational number is represented by the Rational class as a pair of two Integers, numerator and denominator, therefore Rational(1, 2) is 1/2, Rational(3, 2) is 3/2, and so on. Integer numbers are represented by the Integer class.

SymPy uses mpmath in the background, allowing it to execute arbitrary-precision arithmetic computations. Some special constants, such as exp, pi, and oo (Infinity), are thus considered as symbols and can be evaluated with arbitrary precision.

Installation:

pip install sympy

Python sympy.diff() Method:

We can obtain the differentiation of mathematical expressions in the form of variables with the help of the sympy.diff() method.

Syntax:

  sympy.diff(expression, reference_variable)

Return Value:

Differentiate a mathematical expression and return the result.

sympy.diff() Method in Python

Method #1: Using diff() Function (Static Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
  • Give the mathematical expression as static input and store it in a variable.
  • Print the given mathematical expression
  • Pass the above-given expression, reference variable(Differentiate with respect to this variable)
    as arguments to the diff() function to get the differentiation of the given mathematical expression and store it in another variable.
  • Print the given mathematical expression after performing differentiation.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be used as arguments to the symbols() function 
# and store them in corresponding variables.
x, y = symbols('x y')

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = sin(x)*cos(x)

# Print the given mathematical expression
print("The given mathematical expression is:")
print(gvn_expression)

# Pass the above given expression, reference variable(Differentiate with respect to this variable)
# as arguments to the diff() function to get the differentiation of the given
# mathematical expression and store it in another variable.
rslt = diff(gvn_expression, x)

# Print the given mathematical expression after performing differentiation.
print("The given mathematical expression after performing Differentiation:")
print(rslt)

Output:

The given mathematical expression is:
sin(x)*cos(x)
The given mathematical expression after performing Differentiation:
-sin(x)**2 + cos(x)**2

Method #2: Using diff() Function (User Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the symbols to be used as arguments to the symbols() function and store them in corresponding variables.
  • Give the mathematical expression as user input using the input() function and store it in a variable.
  • Print the given mathematical expression
  • Pass the above-given expression, reference variable(Differentiate with respect to this variable)
    as arguments to the diff() function to get the differentiation of the given mathematical expression and store it in another variable.
  • Print the given mathematical expression after performing differentiation.
  • The Exit of the Program.

Below is the implementation:

# Import all the functions from sympy module using the import keyword
from sympy import *

# Pass the symbols to be used as arguments to the symbols() function 
# and store them in corresponding variables.
x, y = symbols('x y')

# Give the mathematical expression as user input using the input() function
# and store it in a variable.
gvn_expression = input("Enter some random mathematical expression = ")

# Print the given mathematical expression
print("The given mathematical expression is:")
print(gvn_expression)

# Pass the above given expression, reference variable(Differentiate with respect to this variable)
# as arguments to the diff() function to get the differentiation of the given
# mathematical expression and store it in another variable.
rslt = diff(gvn_expression, x)

# Print the given mathematical expression after performing differentiation.
print("The given mathematical expression after performing Differentiation:")
print(rslt)

Output:

Enter some random mathematical expression = sin(2*x)
The given mathematical expression is:
sin(2*x)
The given mathematical expression after performing Differentiation:
2*cos(2*x)