Author name: Vikram Chiluka

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

Using the sympy.Lambda() function, we can do any mathematical operation by simply defining the formula and then passing the parameters with reference variables.

Syntax:

sympy.Lambda()

Return Value:

The result of the mathematical formula is returned by the Lambda() function.

sympy.Lambda() Method in Python

Method #1: Using Built-in Functions (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.
  • Pass the reference variables, and mathematical formula as arguments to the Lambda() function to compute the mathematical operation and get the result.
  • Store it in a variable.
  • Pass some random reference variable value to the above lambda function and print the result (here random value acts as an argument to the lambda function).
  • 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.
y = symbols('y')

# Pass the reference variables and mathematical formula as arguments to the Lambda()
# function to compute the mathematical operation and get the result.
# Store it in a variable.
rslt = Lambda(y, y*3)

# Pass some random reference variable value to the above lambda function and
# print the result(here random value acts as an argument to the lambda function).
print(rslt(5))

Output:

15

Explanation:

Here the variable "y" is replaced with 5 and the result of the 
mathematical formula is computed

Method #2: Using Built-in Functions (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.
  • Pass the reference variables, mathematical formula as arguments to the Lambda() function to compute the mathematical operation and get the result.
  • Store it in a variable.
  • Here we are adding the x,y, and z variable values.
  • Scan the arguments to be passed as user input using the map(), input(), and split() functions and store them in separate variables.
  • Pass the above reference variable values to the above lambda function and print the result(here random value acts as an argument to the lambda function).

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, z= symbols('x y z')

# Pass the reference variables, mathematical formula as arguments to the Lambda()
# function to compute the mathematical operation and get the result.
# Store it in a variable.
# Here we are adding the x,y,z variable values.
rslt = Lambda((x, y, z), x+y+z)

# Scan the arguments to be passed as user input using the map(), input(),
# split functions and store them in separate variables.
a, b, c = map(int, input("Enter 3 random values = ").split())

# Pass the above reference variable values to the above lambda function and 
# print the result(here random value acts as an argument to the lambda function).
print("The result of x+y+z = ", rslt(a, b, c))

Output:

Enter 3 random values = 10 20 15
The result of x+y+z = 45

Python sympy.Lambda() Method Read More »

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

Using the sympy.sets.Ropen() method, we can create a set of values by setting interval values such as right open, which indicates a set has a right open bracket and a left close bracket.

Syntax:

sympy.sets.Ropen(value_1, value_2)

Return Value:

A set of values with the right set is returned by the Ropen() function.

sympy.sets.Ropen() Method in Python

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

Approach:

  • Import Interval from sets of sympy module using the import keyword
  • Pass the lower and upper limits to the Ropen() function of the Interval of sympy module to get/open the set of values in the given range.
  • Here it includes the first value(1) while excluding the second value(8).
  • Store it in a variable.
  • Print the above-obtained result set.
  • Pass some random number to the contains() function to check whether the number passed exists in the above-obtained result set and print it.
  • The Exit of the Program.

Below is the implementation:

# Import Interval from sets of sympy module using the import keyword
from sympy.sets import Interval

# Pass the lower and upper limits to the Ropen() function of the Interval
# of sympy module to get/open the set of values in the given range.
# Here it includes the first value(1) while excluding the second value(8).
# Store it in a variable.
rslt_set = Interval.Ropen(1, 8)

# Print the above obtained result set
print("The above obtained result set = ", rslt_set)

# Pass some random number to the contains() function to check whether the
# number passed exists in the above obtained result set and print it.
print("Checking if 1 exists in the obtained result set:")
print(rslt_set.contains(1))

Output:

The above obtained result set = Interval.Ropen(1, 8)
Checking if 1 exists in the obtained result set:
True

Explanation:

Here, it opens the set containing the values from 1, 8 i,e the
set values includes the first value(1) while excluding the 
second value(8)

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

Approach:

  • Import Interval from sets of sympy module using the import keyword
  • Give the lower limit value as user input using the int(input()) function and store it in a variable.
  • Give the upper limit value as user input using the int(input()) function and store it in another variable.
  • Pass the above lower and upper limits as arguments to the Ropen() function of the Interval of sympy module to get/open the set of values in the given range.
  • Here it includes the lower limit value while excluding the upper limit value
  • Store it in a variable.
  • Print the above-obtained result set
  • Pass some random number to the contains() function to check whether the number passed exists in the above-obtained result set and print it.
  • The Exit of the Program.

Below is the implementation:

# Import Interval from sets of sympy module using the import keyword
from sympy.sets import Interval

# Give the lower limit value as user input using the int(input()) function 
# and store it in a variable.
lower_lmt = int(input("Enter some random number = "))

# Give the upper limit value as user input using the int(input()) function 
# and store it in another variable.
uppr_lmt = int(input("Enter some random number = "))

# Pass the above lower and upper limits as arguments to the Ropen() function of 
# the Interval of sympy module to get/open the set of values in the given range.
# Here it includes the lower limit value while excluding the upper limit value 
# Store it in a variable.
rslt_set = Interval.Ropen(lower_lmt, uppr_lmt)

# Print the above obtained result set
print("The above obtained result set = ", rslt_set)

# Pass some random number to the contains() function to check whether the
# number passed exists in the above obtained result set and print it.
print("Checking if 5 exists in the obtained result set:")
print(rslt_set.contains(5))

Output:

Enter some random number = -5
Enter some random number = 5
The above obtained result set = Interval.Ropen(-5, 5)
Checking if 5 exists in the obtained result set:
False

Explanation:

Here, it opens the set containing the values from -5, 5 i,e the
set values includes the first value(-5) while excluding the 
second value(5)

Python sympy.sets.Ropen() Method Read More »

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))

Python sympy.replace() Method Read More »

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

We can get the mathematical expression as a list of terms using the sympy.as_terms() function.

Syntax:

  sympy.as_terms()

Return Value:

A list of terms in the given mathematical expression is returned by the as_terms() function.

sympy.as_terms() Method in Python

 

Method #1: Using as_terms() 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.
  • Apply as_terms() function on the given mathematical expression to get the list of terms of a given mathematical expression
  • Store it in another variable.
  • Print the list of terms of a given mathematical expression.
  • 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 = y**2 + 3*y*x + y

# Apply as_terms() function on the given mathematical expression to get the 
# list of terms of a given mathematical expression
# Store it in another variable.
rslt = gvn_expression.as_terms()

# Print the list of terms of a given mathematical expression
print("The list of terms of a given mathematical expression {",gvn_expression,"} are:")
print(rslt)

Output:

The list of terms of a given mathematical expression { 3*x*y + y**2 + y } are:
([(y, ((1.0, 0.0), (0, 1), ())), (y**2, ((1.0, 0.0), (0, 2), ())), (3*x*y, ((3.0, 0.0), (1, 1), ()))], [x, y])

Method #2: Using as_terms() 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.
  • Pass the given expression as an argument to the simplify() function and apply as_terms() function on the given mathematical expression to get the list of terms of a given mathematical expression.
  • Store it in another variable.
  • Print the list of terms of a given mathematical expression.
  • 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 = ")

# Pass the given expression as an argument to the simplify() function and 
# apply as_terms() function on the given mathematical expression to get the 
# list of terms of a given mathematical expression
# Store it in another variable.
rslt = simplify(gvn_expression).as_terms()

# Print the list of terms of a given mathematical expression
print("The list of terms of a given mathematical expression {",gvn_expression,"} are:")
print(rslt)

Output:

Enter some random mathematical expression = 2*x+y
The list of terms of a given mathematical expression { 2*x+y } are:
([(y, ((1.0, 0.0), (0, 1), ())), (2*x, ((2.0, 0.0), (1, 0), ()))], [x, y])

Python sympy.as_terms() Method Read More »

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

We can use the sympy.is_polynomial() method to check if it is a polynomial. If a polynomial is found, it will return True as a boolean value.

Syntax:

  sympy.is_polynomial()

Return Value:

If a polynomial is found, returns a boolean value True, otherwise False.

sympy.is_polynomial() Method in Python

Method #1: Using is_polynomial() 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.
  • Pass the above given mathematical expression to the simplify() function and apply is_polynomial() function on it to check whether the given expression is a polynomial or Not.
  • Store it in another variable.
  • Print the result after checking whether the given expression is a polynomial or Not.
  • 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 = y**2 + 3*y*x + y

# Pass the above given mathematical expression to the simplify() function and
# apply is_polynomial() function on it to check whether the given expression
# is a polynomial or Not.
# Store it in another variable.
rslt = simplify(gvn_expression).is_polynomial()

# Print the result after checking whether the given expression is a polynomial or Not.
print("Checking whether the given expression{",gvn_expression,"} is a polynomial or Not:")
print(rslt)

Output:

Checking whether the given expression{ 3*x*y + y**2 + y } is a polynomial or Not:
True

Method #2: Using is_polynomial() 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.
  • Pass the above given mathematical expression to the simplify() function and apply is_polynomial() function on it to check whether the given expression is a polynomial or Not.
  • Store it in another variable.
  • Print the result after checking whether the given expression is a polynomial or Not.
  • 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 = ")

# Pass the above given mathematical expression to the simplify() function and
# apply is_polynomial() function on it to check whether the given expression
# is a polynomial or Not.
# Store it in another variable.
rslt = simplify(gvn_expression).is_polynomial()

# Print the result after checking whether the given expression is a polynomial or Not.
print("Checking whether the given expression{",gvn_expression,"} is a polynomial or Not:")
print(rslt)

Output:

Enter some random mathematical expression = log(x)+xy
Checking whether the given expression{ log(x)+xy } is a polynomial or Not:
False

Python sympy.is_polynomial() Method Read More »

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

Using the sympy.diag() method, we can generate a matrix with dimension nxn and filled with numbers in the diagonal.

Rest all the elements of the matrix are filled with zeroes.

Syntax:

  sympy.diag()

Return Value:

A matrix filled with numbers in the diagonal is returned by the diag() function.

sympy.diag() Method in Python

For 2-Dimensional (2D) Matrix

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the elements of a diagonal matrix to the diag() function to generate a diagonal matrix and store it in a variable.
  • Here it creates a 2-Dimensional(2D) matrix since two numbers are passed as arguments to it.
  • Print the resultant diagonal matrix.

Below is the implementation:

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

# Pass the elements of a diagonal matrix to the diag() function to generate a 
# diagonal matrix and store it in a variable.
# Here it creates a 2-Dimensional(2D) matrix since two numbers are passed as arguments to it.
rslt_matrx = diag(5, 2)

# Print the resultant diagonal matrix.
print("The resultant 2-Dimensional diagonal matrix is:")
print(rslt_matrx)

Output:

The resultant 2-Dimensional diagonal matrix is:
Matrix([[5, 0], 
        [0, 2]])

For 3-Dimensional (3D) Matrix

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the elements of a diagonal matrix to the diag() function to generate a diagonal matrix and store it in a variable.
  • Here it creates a 3-Dimensional(3D) matrix since three numbers are passed as arguments to it.
  • Print the resultant diagonal matrix.

Below is the implementation:

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

# Pass the elements of a diagonal matrix to the diag() function to generate a 
# diagonal matrix and store it in a variable.
# Here it creates a 2-Dimensional(2D) matrix since two numbers are passed as arguments to it.
rslt_matrx = diag(1, 4, 6)

# Print the resultant diagonal matrix.
print("The resultant 3-Dimensional diagonal matrix is:")
print(rslt_matrx)

Output:

The resultant 3-Dimensional diagonal matrix is:
Matrix([[1, 0, 0], 
        [0, 4, 0], 
        [0, 0, 6]])

Python sympy.diag() Method Read More »

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

We can determine the determinant of a matrix using the sympy.det() method.

Syntax:

  sympy.det()

Return Value:

The determinant of a matrix is determined by the det() function

sympy.det() Method in Python

For 2-Dimensional (2D) Matrix

Approach:

  • Import all the functions from sympy module using the import keyword
  • Create a matrix(2-Dimensional) using the Matrix() function by passing some random 2D matrix as an argument to it and store it in a variable.
  • Print the given matrix.
  • Apply det() function on the above-given matrix to get the determinant of a given matrix and store it in another variable.
  • Print the determinant of a given matrix.
  • The Exit of the Program.

Below is the implementation:

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

# Create a matrix(2-Dimensional) using the Matrix() function by passing 
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = Matrix([[10, 6], [4, 3]])

# Print the given matrix
print("The given matrix is:\n", gvn_matrx)

# Apply det() function on the above given matrix to get the determinant 
# of a given matrix and store it in another variable
rslt_determinant = gvn_matrx.det()

# Print the determinant of a given matrix.
print("The determinant of a given matrix = ", rslt_determinant)

Output:

The given matrix is:
Matrix([[10, 6], [4, 3]])
The determinant of a given matrix = 6

For 3-Dimensional (3D) Matrix

Approach:

  • Import all the functions from sympy module using the import keyword
  • Create a matrix(3-Dimensional) using the Matrix() function by passing some random 3D matrix as an argument to it and store it in a variable.
  • Print the given matrix.
  • Apply det() function on the above-given matrix to get the determinant of a given matrix and store it in another variable.
  • Print the determinant of a given matrix.
  • The Exit of the Program.

Below is the implementation:

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

# Create a matrix(3-Dimensional) using the Matrix() function by passing 
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = Matrix([[4, 2, 1], [-2, 1, 6], [5, 3, 2]])

# Print the given matrix
print("The given matrix is:\n", gvn_matrx)

# Apply det() function on the above given matrix to get the determinant 
# of a given matrix and store it in another variable
rslt_determinant = gvn_matrx.det()

# Print the determinant of a given matrix.
print("The determinant of a given matrix = ", rslt_determinant)

Output:

The given matrix is:
Matrix([[4, 2, 1], [-2, 1, 6], [5, 3, 2]])
The determinant of a given matrix = -7

Python sympy.det() Method Read More »

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

We can obtain the factorial of any number by using the factorial() method of the sympy module.

Syntax:

 sympy.factorial()

Return Value:

The factorial of a given number is returned by the factorial() function.

Examples:

Example1:

Input:

Given Number = 4

Output:

The factorial of a given number { 4 } = 24

Example2:

Input:

Given Number = 6

Output:

The factorial of a given number { 6 } = 720

sympy.factorial() Method in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number as static input and store it in a variable.
  • Pass the given number as an argument to the factorial() function to get the factorial of a given number.
  • Store it in another variable.
  • Print the factorial of a given number.
  • 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 number as static input and store it in a variable.
gvn_num = 4    

# Pass the given number as an argument to the factorial() function to
# get the factorial of a given number.
# Store it in another variable.
rslt = factorial(gvn_num)

# Print the factorial of a given number.  
print("The factorial of a given number {",gvn_num,"} = ", rslt)

Output:

The factorial of a given number { 4 } = 24

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number as user input using the int(input()) function and store it in a variable.
  • Pass the given number as an argument to the factorial() function to get the factorial of a given number.
  • Store it in another variable.
  • Print the factorial of a given number.
  • 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 number as user input using the int(input()) function and
# store it in a variable.
gvn_num = int(input("Enter some random number = "))   

# Pass the given number as an argument to the factorial() function to
# get the factorial of a given number.
# Store it in another variable.
rslt = factorial(gvn_num)

# Print the factorial of a given number.  
print("The factorial of a given number {",gvn_num,"} = ", rslt)

Output:

Enter some random number = 6
The factorial of a given number { 6 } = 720

Python sympy.factorial() Method Read More »

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

Using the sympy.powdenest() function, we can simplify the powers of mathematical expressions by using identity, (x^a)^b=x^ab.

Syntax:

sympy.powdenest()

Return Value:

The simplified mathematical expression using identity (x^a)^b=x^ab is returned by the powdenest() function.

sympy.powdenest() Method in Python

Method #1: Using powdenest() 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.
  • Pass the given mathematical expression as an argument to the powdenest() function to simplify the powers of the given mathematical expression using the identity (x^a)^b=x^ab.
  • Store it in another variable.
  • Print the simplified expression.
  • 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 = (x**4)**2

# Pass the given mathematical expression as an argument to the powdenest() function
# to simplify the powers of the given mathematical expression using the identity
# (x^a)^b=x^ab.
# Store it in another variable.
rslt = powdenest(gvn_expression)

# Print the simplified expression
print(rslt)

Output:

x**8

Method #2: Using powdenest() 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.
  • Pass the given mathematical expression as an argument to the powdenest() function to simplify the powers of the given mathematical expression using the identity (x^a)^b=x^ab.
  • Store it in another variable.
  • Print the simplified expression.
  • 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 Expression:\n")

# Pass the given mathematical expression as an argument to the powdenest() function
# to simplify the powers of the given mathematical expression using the identity
# (x^a)^b=x^ab.
# Store it in another variable.
rslt = powdenest(gvn_expression)

# Print the simplified expression
print("The simplified Expression is:")
print(rslt)

Output:

Enter some random Expression:
(x**(y + z))**2
The simplified Expression is:
x**(2*y + 2*z)

Python sympy.powdenest() Method Read More »

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

We can generate a matrix with dimension nxm filled with ones(1) by using the ones() method of the sympy module. Here n and m are passed as arguments.

where n = no of rows

m = no of columns

Syntax:

 sympy.ones()

Return Value:

A ones matrix is returned by the ones() function.

sympy.ones() Method in Python

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass the number of rows and columns as arguments to the ones() function to generate a one’s matrix with the given number of rows and columns.
  • Here it generates a ones matrix with 3 rows and 3 columns.
  • Print the one’s matrix with the given number of rows and columns.
  • 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 number of rows and columns as arguments to ones() function to 
# generate a one's matrix with the given number of rows and columns.
# Here it generates a ones matrix with 3 rows and 3 columns.
ones_matx = ones(3, 3)

# Print the ones matrix with the given number of rows and columns
print("The ones matrix with 3 rows and 3 columns:")
print(ones_matx)

Output:

The ones matrix with 3 rows and 3 columns:
Matrix([[1, 1, 1], [1, 1, 1], [1, 1, 1]])

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

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number of rows as user input using the int(input()) function and store it in a variable.
  • Give the number of columns as user input using the int(input()) function
    and store it in another variable.
  • Pass the above-given number of rows and columns as arguments to the ones() function to generate a one’s matrix with the given number of rows and columns.
  • Print the one’s matrix with the given number of rows and columns.
  • 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 number of rows as user input using the int(input()) function 
# and store it in a variable.
gvn_rows = int(input("Enter some random no of rows = "))

# Give the number of columns as user input using the int(input()) function 
# and store it in another variable.
gvn_columns = int(input("Enter some random no of columns = "))

# Pass the above given number of rows and columns as arguments to ones() function 
# to generate a ones matrix with the given number of rows and columns.
ones_matx = ones(gvn_rows, gvn_columns)

# Print the ones matrix with the given number of rows and columns.
print("The ones matrix with {", gvn_rows, "} rows and{", gvn_columns,"}columns:")
print(ones_matx)

Output:

Enter some random no of rows = 2
Enter some random no of columns = 4
The ones matrix with { 2 } rows and{ 4 }columns:
Matrix([[1, 1, 1, 1], [1, 1, 1, 1]])

Python sympy.ones() Method Read More »