Python sympy.replace() 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.replace() Method:

We can use the sympy.replace() method to replace the functions in a mathematical expression without having to edit the entire expression.

Syntax:

sympy.replace(old_function, new_function)

Return Value:

The updated mathematical expression after replacing the functions is returned.

sympy.replace() Method in Python

Method #1: Using replace() 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
  • Apply replace() function on the above given mathematical expression by passing old, new functions as arguments to it to replace the old function with the new one in the given expression.
  • Store it in a variable.
  • Here it replaces the tan function with the cot function.
  • Print the given expression after replacing the old function with the new one.
  • 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)*tan(y)

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

# Apply replace() function on the above given mathematical expression by passing
# old, new functions as arguments to it to replace the old function with the new one
# in the given expression.
# Store it in a variable.
# Here it replaces the tan function with the cot function.
rslt = gvn_expression.replace(tan, cot)

# Print the given expression after replacing the old function with the new one.
print("The given expression after replacing tan function with the cot:")
print(rslt)

Output:

The given mathematical expression is:
sin(x)*tan(y)
The given expression after replacing tan function with the cot:
sin(x)*cot(y)

Method #2: Using replace() 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.
  • Give the old function to be replaced as user input using the input() function
    and store it in another variable.
  • Give the new function with which to be replaced as user input using the input() function and store it in another variable.
  • Print the given mathematical expression
  • Apply replace() function on the above given mathematical expression by passing old, new functions as arguments to it to replace the old function with the new one in the given expression.
  • Store it in a variable.
  • Here it replaces the old function with the new function.
  • Print the given expression after replacing the old function with the new one.
  • 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 = ")

# Give the old function to be replaced as user input using the input() function 
# and store it in a variable.
old_func = input("Enter the old function to be replaced = ")

# Give the new function with which to be replaced as user input using the input() function 
# and store it in another variable.
new_func = input("Enter the new function with which to be replaced = ")
print()

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

# Apply replace() function on the above given mathematical expression by passing
# old, new functions as arguments to it to replace the old function with the new one
# in the given expression.
# Store it in a variable.
# Here it replaces the old function with the new function.
rslt = gvn_expression.replace(old_func, new_func)

# Print the given expression after replacing the old function with the new one.
print("The given expression after replacing {", old_func,"} function with the {",new_func,"} :")
print(rslt)

Output:

Enter some random mathematical expression = log(x)*sin(tan(y))
Enter the old function to be replaced = log
Enter the new function with which to be replaced = cos

The given mathematical expression is:
log(x)*sin(tan(y))
The given expression after replacing { log } function with the { cos } :
cos(x)*sin(tan(y))