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

We can use the sympy.cofactors() method to get the cofactors of two numbers that are supplied as parameters to this function.

Syntax:

 sympy.cofactors(num1, num2)

Return Value:

A tuple of cofactors is returned by the cofactors() function.

sympy.cofactors() Method in Python

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the first number as static input and store it in a variable.
  • Give the second number as static input and store it in another variable.
  • Pass the above given two numbers as arguments to the cofactors() function to get the cofactors of the given two numbers.
  • Store it in another variable.
  • Print the cofactors of the given two numbers.
  • The Exit of the Program.

Below is the implementation:

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

# Give the first number as static input and store it in a variable.
gvn_num1 = 10
# Give the second number as static input and store it in another variable.
gvn_num2 = 6

# Pass the above given two numbers as arguments to the cofactors() function 
# to get the cofactors of the given two numbers.
# Store it in another variable.
rslt = cofactors(gvn_num1, gvn_num2)

# Print the cofactors of the given two numbers.
print("The cofactors of the given two numbers{",gvn_num1,",",gvn_num2,"} = ", rslt)

Output:

The cofactors of the given two numbers{ 10 , 6 } = (2, 5, 3)

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the first number as user input using the int(input()) function and store it in a variable.
  • Give the second number as user input using the int(input()) function and store it in another variable.
  • Pass the above given two numbers as arguments to the cofactors() function to get the cofactors of the given two numbers.
  • Store it in another variable.
  • Print the cofactors of the given two numbers.
  • The Exit of the Program.

Below is the implementation:

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

# Give the first number as user input using the int(input()) function
# and store it in a variable.
gvn_num1 = int(input("Enter some random number = "))
# Give the second number as user input using the int(input()) function
# and store it in another variable.
gvn_num2 = int(input("Enter some random number = "))

# Pass the above given two numbers as arguments to the cofactors() function 
# to get the cofactors of the given two numbers.
# Store it in another variable.
rslt = cofactors(gvn_num1, gvn_num2)

# Print the cofactors of the given two numbers.
print("The cofactors of the given two numbers{",gvn_num1,",",gvn_num2,"} = ", rslt)

Output:

Enter some random number = 15
Enter some random number = 20
The cofactors of the given two numbers{ 15 , 20 } = (5, 3, 4)