# Python

## Python Numpy matrix.ptp() Function

NumPy Library

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.ptp() Function:

Using the matrix.ptp() method of the Numpy module, we can obtain the peek-to-peek value i.e, is (maximum-minimum) along the axis from a specified matrix.

Syntax:

 matrix.ptp(axis)

Return Value:

The peek-to-peek value from a given matrix is returned by the ptp() function.

## Numpy matrix.ptp() Function in Python

For 2-Dimensional (2D) Matrix

Approach:

• Import numpy module using the import keyword
• Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as arguments to it and store it in a variable
• Print the given matrix
• Pass random axis(0/1) as an argument to the ptp() function and apply it to the given matrix to get the peek-to-peek(maximum-minimum) value along the specified axis from a given matrix.
• Here 0 represents along the column, and 1 represents a row.
• Store it in another variable
• Print the peek-to-peek value along the specified axis from a given matrix.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing
# some random 2D matrix as arguments to it and store it in a variable
gvn_matrx = np.matrix('[10, 1; 6, 3]')
# Print the given matrix
print("The given matrix is:\n", gvn_matrx)

# Pass random axis(0/1) as an argument to the ptp() function and apply it to the given matrix
# to get the peek-to-peek(maximum-minimum) value along the specified axis from a given matrix.
# Here 0 represents along column, 1 represents row.
# Store it in another variable
rslt = gvn_matrx.ptp(1)
# Print the peek-to-peek value along the specified axis from a given matrix.
print("The peek-to-peek value along the specified axis from a given matrix:")
print(rslt)

Output:

The given matrix is:
[[10 1]
[ 6 3]]
The peek-to-peek value along the specified axis from a given matrix:
[
]

Explanation:

Here it prints 10-1 = 9,  6-3=3 i.e, along the row

NOTE:

Here 0 represents along the column, and 1 represents a row.
but generally 0 = row, 1=column.

For 3-Dimensional (3D) Matrix

Approach:

• Import numpy module using the import keyword
• Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as arguments to it and store it in a variable
• Print the given matrix
• Pass random axis(0/1) as an argument to the ptp() function and apply it to the given matrix to get the peek-to-peek(maximum-minimum) value along the specified axis from a given matrix.
• Here 0 represents along the column, and 1 represents a row.
• Store it in another variable
• Print the peek-to-peek value along the specified axis from a given matrix.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')
# Print the given matrix
print("The given matrix is:\n", gvn_matrx)

# Pass random axis(0/1) as an argument to the ptp() function and apply it to the given matrix
# to get the peek-to-peek(maximum-minimum) value along the specified axis from a given matrix.
# Here 0 represents along column, 1 represents row.
# Store it in another variable
rslt = gvn_matrx.ptp(0)
# Print the peek-to-peek value along the specified axis from a given matrix.
print("The peek-to-peek value along the specified axis from a given matrix:")
print(rslt)

Output:

The given matrix is:
[[ 2 4 1]
[ 8 7 3]
[10 9 5]]
The peek-to-peek value along the specified axis from a given matrix:
[[8 5 4]]

## Python Numpy matrix.put() Function

NumPy Library

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it into our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.put() Function:

Using the matrix.put() method of the Numpy module, we can put(insert) the value by passing index and value in a given particular matrix.

Syntax:

 matrix.put(index, value)

Return Value:

A new matrix (after putting values ) is returned by the put() function.

## Numpy matrix.put() Function in Python

For 2-Dimensional (2D) Matrix (Inserting an element at Multiple Indices)

Approach:

• Import numpy module using the import keyword
• Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
• Pass the index/indices(as a tuple) and value as arguments to the put() function and apply it to the given matrix
• Here we passed multiple indices and the value gets inserted at corresponding indices of the tuple.
• Store it in another variable
• Print the given matrix after inserting an element at the multiple indices.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing
# some random 2D matrix as arguments to it and store it in a variable
gvn_matrx = np.matrix('[2, 1; 6, 3]')

# Pass the index/indices(as a tuple) and value as an argument to the put() function and apply it to the given matrix
# Here we passed multiple indices and the value gets inserted at corresponding indices of the tuple.
# Store it in another variable
gvn_matrx.put((0, 2), 50)
# Print the given matrix after inserting an element at the multiple indices
print("The given matrix after inserting an element at the multiple indices:")
print(gvn_matrx)

Output:

The given matrix after inserting an element at the multiple indices:
[[50 1]
[50 3]]

Explanation:

Here it inserts 50 at the indices 0 and 2.

NOTE:

The matrix indices starts from '0'

For 3-Dimensional (3D) Matrix (Inserting an element at Single Index)

Approach:

• Import numpy module using the import keyword
• Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable
• Pass the index and value as arguments to the put() function and apply it to the given matrix
• Here we passed a single index and the value gets inserted at that corresponding index.
• Store it in another variable
• Print the given matrix after inserting an element at the given index.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')

# Pass the index and value as arguments to the put() function and apply it to the given matrix
# Here we passed single index and the value gets inserted at that corresponding index.
# Store it in another variable
gvn_matrx.put((4), 100)
# Print the given matrix after inserting an element at the given index.
print("The given matrix after inserting an element {100} at the given index{4}:")
print(gvn_matrx)

Output:

The given matrix after inserting an element {100} at the given index{4}:
[[ 2 4 1]
[ 8 100 3]
[ 10 9 5]]

## Python Numpy matrix.flatten() Function

NumPy Library

NumPy is a library in python that is created to work efficiently with arrays in python. It is fast, easy to learn, and provides efficient storage. It also provides a better way of handling data for the process. We can create an n-dimensional array in NumPy. To use NumPy simply have to import it in our program and then we can easily use the functionality of NumPy in our program.

NumPy is a Python library that is frequently used for scientific and statistical analysis. NumPy arrays are grids of the same datatype’s values.

Numpy matrix.flatten() Function:

We can flatten a given matrix using the matrix.flatten() method of the Numpy module, which returns a one-dimensional matrix as an output.

Syntax:

 matrix.flatten()

Return Value:

The flattened 1-Dimensional matrix from a given matrix is returned by the flatten() function.

What is a Flattened matrix?

Flattening a matrix means flattening the given n-dimensional matrix to a one-Dimensional(1D) matrix.

Example:

Let the matrix be:

[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]]

Matrix after Flattening:

[1 2 3 4 5 6 7 8 9]

## Numpy matrix.flatten() Function in Python

For 2-Dimensional (2D) Matrix

Approach:

• Import numpy module using the import keyword
• Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing some random 2D matrix as an argument to it and store it in a variable
• Apply flatten() function on the given matrix to get the flattened matrix from a given matrix.
• Here, it flattens to a 1-Dimensional matrix.
• Store it in another variable
• Print the flattened matrix from a given matrix.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Create a matrix(2-Dimensional) using the matrix() function of numpy module by passing
# some random 2D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[3, 2; 4, 5]')

# Apply flatten() function on the given matrix to get the flattened matrix from a given matrix.
# Here, it flattens to a 1-Dimensional matrix.
# Store it in another variable
rslt = gvn_matrx.flatten()
# Print the flattened matrix from a given matrix
print("The flattened matrix from a given matrix:")
print(rslt)

Output:

The flattened matrix from a given matrix:
[[3 2 4 5]]

For 3-Dimensional (3D) Matrix

Approach:

• Import numpy module using the import keyword
• Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing some random 3D matrix as an argument to it and store it in a variable
• Apply flatten() function on the given matrix to get the flattened matrix from a given matrix.
• Here, it flattens to a 1-Dimensional matrix.
• Store it in another variable
• Print the flattened matrix from a given matrix.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Create a matrix(3-Dimensional) using the matrix() function of numpy module by passing
# some random 3D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 4, 1; 8, 7, 3; 10, 9, 5]')

# Apply flatten() function on the given matrix to get the flattened matrix from a given matrix.
# Here, it flattens to a 1-Dimensional matrix.
# Store it in another variable
rslt = gvn_matrx.flatten()
# Print the flattened matrix from a given matrix
print("The flattened matrix from a given matrix:")
print(rslt)

Output:

The flattened matrix from a given matrix:
[[ 2 4 1 8 7 3 10 9 5]]

## Python Scipy stats.halfgennorm.entropy() Function

Scipy Library in Python:

• SciPy is a scientific computation package that uses the NumPy library underneath.
• SciPy is an abbreviation for Scientific Python.
• It includes additional utility functions for optimization, statistics, and signal processing.
• SciPy, like NumPy, is open source, so we can freely use it.
• Travis Olliphant, the developer of NumPy, created SciPy.
• SciPy has optimized and added/enhanced functions that are often used in NumPy and Data Science.

Scipy stats.halfgennorm.entropy() Function:

We can obtain the value of entropy of a random variate by using the stats.halfgennorm.entropy() function.

Syntax:

stats.halfgennorm.entropy(beta)

Return Value:

The entropy value of a random variate is returned by the stats.halfgennorm.entropy() Function.

### What is Entropy?

When entropy is discussed in information theory, it refers to the randomness in data. Another way to think about entropy is as the data’s unpredictability. So a high entropy indicates that the data is scattered, whereas a low entropy indicates that nearly all of the data is the same.

## Scipy stats.halfgennorm.entropy() Function in Python

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

Approach:

• Import halfgennorm() method from stats of scipy module using the import keyword
• Give the beta value as static input and store it in a variable.
• Calculate the entropy value using the entropy() function of halfgennorm by passing the given beta value as an argument to it.
• Store it in another variable.
• Print the entropy value for the given beta value.
• The Exit of the Program.

Below is the implementation:

# Import halfgennorm() method from stats of scipy module using the import keyword
from scipy.stats import halfgennorm

# Give the beta value as static input and store it in a variable.
gvn_beta = 3

# Calculate the entropy value using the entropy() function of halfgennorm by passing
# the given beta value as an argument to it.
# Store it in another variable.
rslt = halfgennorm.entropy(gvn_beta)

# Print the entropy value for the given beta value
print("The entropy value for the given beta {", gvn_beta,"} value = ", rslt)

Output:

The entropy value for the given beta { 3 } value = 0.22014169159299057

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

Approach:

• Import halfgennorm() method from stats of scipy module using the import keyword
• Give the beta value as user input using the int(input()) function and store it in a variable.
• Calculate the entropy value using the entropy() function of halfgennorm by passing the given beta value as an argument to it.
• Store it in another variable.
• Print the entropy value for the given beta value.
• The Exit of the Program.

Below is the implementation:

# Import halfgennorm() method from stats of scipy module using the import keyword
from scipy.stats import halfgennorm

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

# Calculate the entropy value using the entropy() function of halfgennorm by passing
# the given beta value as an arguments to it.
# Store it in another variable.
rslt = halfgennorm.entropy(gvn_beta)

# Print the entropy value for the given beta value
print("The entropy value for the given beta {", gvn_beta,"} value = ", rslt)

Output:

Enter some random number = 5
The entropy value for the given beta { 5 } value = 0.1146259099966842

## Python sympy.Matrix.col() 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.Matrix.col() Method:

The columns of the matrix can be extracted using the sympy.Matrix().col() method.

Syntax:

 sympy.Matrix().col()

Return Value:

The column values of a given matrix are returned by the sympy.Matrix().col() function.

## sympy.Matrix.col() Method in Python

For 2-Dimensional (2D) Matrix

Approach:

• Import all the methods from sympy using the import keyword
• Create a 2-Dimensional (2D) matrix using the Matrix() function and apply col() function on it by passing the column number as an argument to it to get the values of the specified column.
• Here it gives the values of the 0th column.
• Store it in a variable
• Print the values of the specified column.
• The Exit of the Program.

Below is the implementation:

# Import all the methods from sympy using the import keyword
from sympy import *

# Create a 2-Dimensional(2D)matrix using the Matrix() function and apply col() function on it by
# passing the column number as an argument to it to get the values of the specified column.
# Here it gives the values of 0th column.
# Store it in a variable
col_values = Matrix([[3, 2], [8, 7]]).col(0)

# Print the values of the specified column
print("The values of the 0th column:")
print(col_values)


Output:

The values of the 0th column:
Matrix([, ])

For 3-Dimensional (3D) Matrix

Approach:

• Import all the methods from sympy using the import keyword
• Create a 3-Dimensional (3D) matrix using the Matrix() function and apply col() function on it by passing the column number as an argument to it to get the values of the specified column.
• Here it gives the values of the 1st column.
• Store it in a variable
• Print the values of the specified column.
• The Exit of the Program.

Below is the implementation:

# Import all the methods from sympy using the import keyword
from sympy import *

# Create a 3-Dimensional(3D) matrix using the Matrix() function and apply col() function on it by
# passing the column number as an argument to it to get the values of the specified column.
# Here it gives the values of 1st column.
# Store it in a variable
col_values = Matrix([[1, 6, 10], [8, 7, 5], [3, 2, 9]]).col(1)

# Print the values of the specified column
print("The values of the 1st column:")
print(col_values)


Output:

The values of the 1st column:
Matrix([, , ])

## Python sympy.Matrix.col_del() 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.Matrix.col_del() Method:

We can delete the columns of a matrix using the sympy.Matrix.col_del() function.

Syntax:

 sympy.Matrix().col_del()

Return Value:

A new matrix after the deletion of the column is returned by the Matrix.col_del() function.

## sympy.Matrix.col_del() Method in Python

For 2-Dimensional (2D) Matrix

Approach:

• Import all the methods from sympy using the import keyword
• Create a 2-Dimensional(2D) matrix using the Matrix() function by passing some random 2D matrix as an argument to it and store it in a variable.
• Apply col_del() function on the above-given matrix by passing the column number as an argument to it
to delete the values of the specified column.
• Here it deletes the zero(0th) column.
• Print the given matrix after deleting the zero(0th) column.
• The Exit of the Program.

Below is the implementation:

# Import all the methods from sympy using the import keyword
from sympy import *

# Create a 2-Dimensional(2D) matrix using the Matrix() function by passing some random
# 2D matrix as an argument to it and store it in a variable.
gvn_matrx = Matrix([[3, 5], [6, 8]])
# Apply col_del() function on the above given matrix by passing the column number as an argument to it
# to delete the values of the specified column.
# Here it deletes the zero(0th) column.
gvn_matrx.col_del(0)

# Print the given matrix after deleting the zero(0th) column.
print("The given matrix after deleting the zero(0th) column:")
print(gvn_matrx)


Output:

The given matrix after deleting the zero(0th) column:
Matrix([, ])

For 3-Dimensional (3D) Matrix

Approach:

• Import all the methods from sympy using the import keyword
• Create a 3-Dimensional(3D) matrix using the Matrix() function by passing some random 3D matrix as an argument to it and store it in a variable.
• Apply col_del() function on the above-given matrix by passing the column number as an argument to it
to delete the values of the specified column.
• Here it deletes the 1st column.
• Print the given matrix after deleting the 1st column.
• The Exit of the Program.

Below is the implementation:

# Import all the methods from sympy using the import keyword
from sympy import *

# Create a 3-Dimensional(3D) matrix using the Matrix() function by passing some random
# 3D matrix as an argument to it and store it in a variable.
gvn_matrx = Matrix([[1, 6, 10], [8, 7, 5], [3, 2, 9]])
# Apply col_del() function on the above given matrix by passing the column number as an argument to it
# to delete the values of the specified column.
# Here it deletes the 1st column.
gvn_matrx.col_del(1)

# Print the given matrix after deleting the 1st column
print("The given matrix after deleting the 1st column:")
print(gvn_matrx)


Output:

The given matrix after deleting the 1st column:
Matrix([[1, 10], [8, 5], [3, 9]])

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

We can use expand_log() function of the sympy module to simplify the log terms in the mathematical equation by employing the properties stated below:

•  log(x*y)=log(x)+log(y)
•  log(x**n)=nlog(x)

Syntax:

  sympy.expand_log()

Return Value:

The simplified mathematical expression is returned by the expand_log() function.

## sympy.expand_log() Method in Python

Example1

Here it gives the result based on the log(x*y)=log(x)+log(y) property.

Approach:

• Import all the functions from sympy module using the import keyword
• Pass the symbols to be used and set positive as True 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 expression as an argument to the expand_log() method to simplify the log terms in the given expression based on the properties given.
• Here it gives the result based on the log(x*y)=log(x)+log(y) property.
• Store it in another variable.
• Print the result after the simplification of log terms in the given 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 and set positive as True as arguments to the
# symbols() function and store them in corresponding variables.
a, b, c = symbols('a b c', positive = True)
# Give the mathematical expression as static input and store it in a variable.
gvn_expression = log(a * b)

# Pass the above given expression as an argument to the expand_log() method
# to simplify the log terms in the given expression based on the properties given.
# Store it in another variable.
rslt = expand_log(gvn_expression)

# Print the result after the simplification of log terms in the given expression.
print(rslt)

Output:

log(a) + log(b)

Example2

Here it gives the result based on the log(x**n)=nlog(x) property.

Approach:

• Import all the functions from sympy module using the import keyword
• Pass the symbols to be used and set positive as True 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 expression as an argument to the expand_log() method to simplify the log terms in the given expression based on the properties given.
• Here it gives the result based on the log(x**n)=nlog(x) property.
• Store it in another variable.
• Print the result after the simplification of log terms in the given 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 and set set positive as True as arguments to the
# symbols() function and store them in corresponding variables.
a, b, c = symbols('a b c', positive = True)
# Give the mathematical expression as static input and store it in a variable.
gvn_expression = log(b**2)

# Pass the above given expression as an argument to the expand_log() method
# to simplify the log terms in the given expression based on the properties given.
# Here it gives the result based on the log(x**n)=nlog(x) property.
# Store it in another variable.
rslt = expand_log(gvn_expression)

# Print the result after the simplification of log terms in the given expression.
print(rslt)

Output:

2*log(b)

## Python sympy.coeff(x, n) 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.coeff(x, n) Method:

We can determine the coefficient of variables in mathematical expressions using the coeff(x, n) method of the sympy module.

Syntax:

 sympy.coeff(x, n)

Return Value:

The coefficient of variables is returned by the coeff() function.

## sympy.coeff(x, n) Method in Python

Example1

Here it gives the coefficient of x^2 in the given mathematical expression i.e, 1+3 = 4

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 variable and power as arguments to the coeff() method and apply it to the given expression to get the coefficient of the given mathematical expression.
• Here it gives the coefficient of x^2 i.e, 1+3 = 4
• Store it in another variable.
• Print the coefficient of x^2 in the 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, z = symbols('x y z')

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = x**2+3*y**2+3*x**2+x+y

# Pass the variable and power as arguments to the coeff() method and apply it on the
# given expression to get the coefficient of given mathematical expression.
# Here it gives the coefficient of x^2 i.e, 1+3 = 4
# Store it in another variable.
rslt = gvn_expression.coeff(x, 2)

# Print the coefficient of x^2 in the given mathematical expression.
print("The coefficient of x^2 in the given mathematical expression is:")
print(rslt)


Output:

The coefficient of x^2 in the given mathematical expression is:
4

Example2

Here it gives the coefficient of y^3 in the given mathematical expression i.e, 1+6-1 = 6

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 variable and power as arguments to the coeff() method and apply it to the given expression to get the coefficient of the given mathematical expression.
• Here it gives the coefficient of y^3 i.e, 1+6-1 = 6
• Store it in another variable.
• Print the coefficient of y^3 in the 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, z = symbols('x y z')

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = y**3+5*x**2+6*y**3+x-y**3+z

# Pass the variable and power as arguments to the coeff() method and apply it to the
# given expression to get the coefficient of given mathematical expression.
# Here it gives the coefficient of y^3 i.e, 1+6-1 = 6
# Store it in another variable.
rslt = gvn_expression.coeff(y, 3)

# Print the coefficient of y^3 in the given mathematical expression.
print("The coefficient of y^3 in the given mathematical expression is:")
print(rslt)


Output:

The coefficient of y^3 in the given mathematical expression is:
6

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

We can convert the expression of string type to general mathematical expression using the sympify() function of the sympy module.

Syntax:

 sympy.sympify()

Return Value:

The general mathematical expression is returned by the sympify() function.

## sympy.sympify() Method in Python

Example1

Approach:

• Import all the functions from sympy module using the import keyword
• Pass the symbols to be 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 expression as an argument to the sympify() method to get the string of expression into the real mathematical expression.
• Store it in another variable.
• Print the real 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 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**3 + x**2 - x - 1"

# Pass the above given expression as an argument to the sympify() method to get the
# string of expression into the real mathematical expression.
# Store it in another variable.
rslt = sympify(gvn_expression)

# Print the real mathematical expression.
print("The real mathematical expression = ", rslt)

Output:

The real mathematical expression = x**3 + x**2 - x - 1

Example2

Here we substitute 2 in place of x and get the simplified result using the subs() function.

Approach:

• Import all the functions from sympy module using the import keyword
• Pass the symbols to be 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 expression as an argument to the sympify() method to get the string of expression into the real mathematical expression.
• Store it in another variable.
• Pass the variable, number as arguments to the subs() function to substitute the given number in place of the variable given in the expression
• Here it substitutes 2 in place of x and gets the simplified result.
• Print the simplified result after substituting x=2 in the 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 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**3 + x**2 - x - 1"

# Pass the above given expression as an argument to the sympify() method to get the
# string of expression into the real mathematical expression.
# Store it in another variable.
rslt = sympify(gvn_expression)

# Pass the variable, number as arguments to the subs() function to substitute the
# given number in place of the variable given in the expression
# Here it substitutes 2 in place of x and get the simplified result.
simplified_rslt = rslt.subs(x, 2)

# Print the simplified result after substituting x=2 in the expression
print("The simplified result after substituting x=2 in the expression")
print(simplified_rslt)

Output:

The simplified result after substituting x=2 in the expression
9

Explanation:

Here we substitute 2 in place of x i.e, 2^3+2^2-2-1 = 8+4-2-1 = 9

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

The mathematical expressions can be evaluated using the sympy.evalf() method.

Syntax:

 sympy.evalf()

Return Value:

The evaluated mathematical expression is returned by the evalf() function.

## sympy.evalf() Method Python

Example1

Approach:

• Import all the functions from sympy using the import keyword
• Give the mathematical expression as static input and store it in a variable.
• Apply evalf() function on the above given mathematical expression to evaluate the given mathematical expression.
• Store it in another variable.
• Print the result of the evaluated mathematical expression.
• The Exit of the Program.

Below is the implementation:

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

# Give the mathematical expression as static input and store it in a variable.
gvn_expression = sqrt(16)

# Apply evalf() function on the above given mathematical expression to
# evaluate the given mathematical expression.
# Store it in another variable.
rslt = gvn_expression.evalf()
# Print the result of the evaluated mathematical expression.
print("The result of the evaluated mathematical expression:")
print(rslt)


Output:

The result of the evaluated mathematical expression:
4.00000000000000

Example2

Approach:

• Import all the functions from sympy using the import keyword
• Give the mathematical expression as static input and store it in a variable.
• Apply evalf() function on the above given mathematical expression to evaluate the given mathematical expression.
•  Here we multiply the values of sin(45) and cos(45).
• Store it in another variable.
• Print the result of the evaluated mathematical expression.
• The Exit of the Program.

Below is the implementation:

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

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

# Apply evalf() function on the above given mathematical expression to
# evaluate the given mathematical expression.
# Here we multiply the values of sin(45) and cos(45).
# Store it in another variable.
rslt = gvn_expression.evalf()
# Print the result of the evaluated mathematical expression.
print("The result of the evaluated mathematical expression:")
print(rslt)


Output:

The result of the evaluated mathematical expression:
0.446998331800279