Author name: Vikram Chiluka

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

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

where n = no of rows

m = no of columns

Syntax:

 sympy.zeros()

Return Value:

A zero matrix is returned by the zeros() function.

sympy.zeros() Method in Python

Method #1: Using zeros Function (Static Input)

Approach:

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

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

Output:

The zero matrix with 3 rows and 3 columns:
Matrix([[0, 0, 0], [0, 0, 0], [0, 0, 0]])

Method #2: Using zeros 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 zeros() function to generate a zero matrix with the given number of rows and columns.
  • Print the zero 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 zeros() function 
# to generate a zero matrix with the given number of rows and columns.
zero_matx = zeros(gvn_rows, gvn_columns)

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

Output:

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

Python sympy.zeros() Method Read More »

Python sympy.is_rational 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_rational Method:

We can use the sympy.is_rational method to determine whether an element is rational or not. This method returns a boolean value, True or False.

Syntax:

  sympy.is_rational

Return Value:

Returns True if the element is rational, otherwise False.

sympy.is_rational Method in Python

Method #1: Using is_rational Function (Static Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give some random number as static input and store it in a variable.
  • Pass the above-given number as an argument to the simplify() function and apply is_rational
    method on it to check whether the given number is a rational number or Not.
  • Store it in another variable.
  • Print the result after checking whether the given number is a rational number 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 *

# Give some random number as static input and store it in a variable.
gvn_numb = 2*I

# Pass the above given number as an argument to the simplify() function and apply is_rational
# method on it to check whether the given number is a rational number or Not.
# Store it in another variable.
rslt = simplify(gvn_numb).is_rational

# Print the result after checking whether the given number is a rational number or Not. 
print("Checking whether the given number{",gvn_numb,"} is a rational number or Not =", rslt)

Output:

Checking whether the given number{ 2*I } is a rational number or Not = False

Method #2: Using is_rational Function (User Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give some random number as user input using the input() function and store it in a variable.
  • Pass the above-given number as an argument to the simplify() function and apply is_rational
    method on it to check whether the given number is a rational number or Not.
  • Store it in another variable.
  • Print the result after checking whether the given number is a rational number 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 *

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

# Pass the above given number as an argument to the simplify() function and apply is_rational
# method on it to check whether the given number is a rational number or Not.
# Store it in another variable.
rslt = simplify(gvn_numb).is_rational

# Print the result after checking whether the given number is a rational number or Not. 
print("Checking whether the given number{",gvn_numb,"} is a rational number or Not =", rslt)

Output:

Enter some random number = 5/3
Checking whether the given number{ 5/3 } is a rational number or Not = True

Python sympy.is_rational Method Read More »

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

We can get the integration of mathematical expressions in the form of variables with the help of the sympy.integrate() method.

Syntax:

  sympy.integrate(expression, reference variable)

Return Value:

Integrate a mathematical expression and return the result.

sympy.integrate() Method in Python

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

Approach:

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

Below is the implementation:

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

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

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

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

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

# Print the given mathematical expression after performing Integration
print("The given mathematical expression after performing Integration:")
print(rslt)

Output:

The given mathematical expression is:
sin(x)*cos(x)
The given mathematical expression after performing Integration:
sin(x)**2/2

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

Approach:

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

Below is the implementation:

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

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

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

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

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

# Print the given mathematical expression after performing Integration
print("The given mathematical expression after performing Integration:")
print(rslt)

Output:

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

Python sympy.integrate() Method Read More »

Python sympy.diff() Method

Python SymPy Module:

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

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

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

Installation:

pip install sympy

Python sympy.diff() Method:

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

Syntax:

  sympy.diff(expression, reference_variable)

Return Value:

Differentiate a mathematical expression and return the result.

sympy.diff() Method in Python

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

Approach:

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

Below is the implementation:

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

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

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

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

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

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

Output:

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

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

Approach:

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

Below is the implementation:

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

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

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

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

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

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

Output:

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

Python sympy.diff() Method Read More »

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

Using the Mul() method of the sympy module, we can multiply two variables and generate a mathematical expression.

Syntax:

 sympy.Mul()

Return Value:

The multiplication/product of two variables is returned by the Mul() function.

sympy.Mul() Method in Python

Example1

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 two above variables to the Mul() function to multiply both the variables and form a mathematical expression.
  • Store it in another variable.
  • Print the result mathematical expression after the multiplication of two variables.
  •  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.
a, b = symbols('a b')

# Pass the two above variables to the Mul() function to multiply both the variables 
# and form a mathematical expression.
# Store it in another variable.
rslt_expresn = Mul(a, b)
    
# Print the result mathematical expression after the multiplication of two variables
print(rslt_expresn)

Output:

a*b

Example2

Here we multiply the variables using the Mul() function and add the variables using the ‘+’ operator.

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 two above variables to be multiplied to the Mul() function to multiply both the variables and form a resultant mathematical expression.
  • Store it in another variable.
  • Print the result mathematical expression after the multiplication of two variables.
  •  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.
a, b = symbols('a b')

# Pass the two above variables to be multiplied to the Mul() function to 
# multiply both the variables and form a resultant mathematical expression.
# Store it in another variable.
rslt_expresn = Mul(a, a) + Mul(b, b)
    
# Print the result mathematical expression after the multiplication of two variables
print(rslt_expresn)

Output:

a**2 + b**2

Python sympy.Mul() Method Read More »

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

We can convert integer values to floating-point values using the Float() method of the sympy module, and we can also set the precision values. Precision is set at 15 by default.

Syntax:

 sympy.Float()

Return Value:

The floating-point number is returned by the Float() function.

sympy.Float() Method in Python

Method #1: Without Specifying the Precision Value

Here we do not give Precision Value. Hence it takes 15 by default.

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass some random Integer value to the Float() function to convert the given integer number to the floating-point number.
  • Here it takes precision as 15 by default.
  • Store it in a variable.
  • Print the given Integer after converting it to a floating-point number.
  • The Exit of the Program.

Below is the implementation:

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

# Pass some random Integer value to the Float() function to convert the 
# given integer number to the floating-point number.
# Here it takes precision as 15 by default.
# Store it in a variable.
float_num = Float(25)

# Print the given Integer after converting it to a floating-point number.
print("The given Integer after converting it to a floating-point number:")
print(float_num)

Output:

The given Integer after converting it to a floating-point number:
25.0000000000000

Method #2: With Specifying the Precision Value

Approach:

  • Import all the functions from sympy module using the import keyword
  • Pass some random Integer, precision values as arguments to the Float() function to convert the given integer number to the floating-point number with the given precision
  • Store it in a variable.
  • Print the given Integer after converting it to a floating-point number with the given precision.
  • The Exit of the Program.

Below is the implementation:

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

# Pass some random Integer, precision values as arguments to the Float() function
# to convert the given integer number to the floating-point number with the given precision
# Store it in a variable.
float_num = Float(25, 5)

# Print the given Integer after converting it to a floating-point number with the given precision
print("Converting given Integer to a floating-point number with the given precision:")
print(float_num)

Output:

Converting given Integer to a floating-point number with the given precision:
25.000

Python sympy.Float() Method Read More »

Python sympy.is_integer 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_integer Method:

We can use the sympy.is_ integer method to determine whether an element is an integer or not. This method returns a boolean value, True or False.

Syntax:

 sympy.is_ integer()

Return Value:

If the value is an integer, return True; otherwise, return False.

sympy.is_integer Method in Python

Method #1: Using is_integer Function (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 to the simplify() function and apply is_integer method on it to check whether the given number is an integer or Not.
  • Store it in another variable.
  • Print the result after checking if the given number is an integer 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 *

# Give the number as static input and store it in a variable.
gvn_numb = 6

# Pass the given number to the simplify() function and apply is_integer method on it 
# to check whether the given number is an integer or Not
# Store it in another variable.
rslt = simplify(gvn_numb).is_integer

# Print the result after checking if the given number is an integer or Not
print("Checking if the given number{", gvn_numb,"} is an integer or Not = ", rslt)

Output:

Checking if the given number{ 6 } is an integer or Not = True

Method #2: Using is_integer Function (User Input)

Approach:

  • Import all the functions from sympy module using the import keyword
  • Give the number as user input using the input() function and store it in a variable.
  • Pass the given number to the simplify() function and apply is_integer method on it to check whether the given number is an integer or Not.
  • Store it in another variable.
  • Print the result after checking if the given number is an integer 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 *

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

# Pass the given number to the simplify() function and apply is_integer method on it 
# to check whether the given number is an integer or Not
# Store it in another variable.
rslt = simplify(gvn_numb).is_integer

# Print the result after checking if the given number is an integer or Not
print("Checking if the given number{", gvn_numb,"} is an integer or Not = ", rslt)

Output:

Enter some random number = 2.5
Checking if the given number{ 2.5 } is an integer or Not = False

 

Python sympy.is_integer Method Read More »

Python Numpy matrix.round() 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.round() Function:

We can round off the values of the specified matrix using the matrix.round() function of the Numpy module.

Syntax:

 matrix.round()

Return Value:

The rounded values of a given matrix are returned by the round() function.

Numpy matrix.round() 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
  • Print the given matrix
  • Apply the round() function on the given matrix to round off the values of the given matrix and store it in another variable.
  • Print the given matrix after rounding off the values.
  • 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('[10.3, 1.9; 2.5, 7.79]')

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

# Apply the round() function on the given matrix to round off the values
# of the given matrix and store it in another variable
rslt = gvn_matrx.round()

# Print the given matrix after rounding off the values
print("The given matrix after rounding off the values:")
print(rslt)

Output:

The given matrix is:
[[10.3 1.9 ]
 [ 2.5 7.79]]
The given matrix after rounding off the values:
[[10. 2.]
 [ 2. 8.]]

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
  • Print the given matrix
  • Apply the round() function on the given matrix to round off the values of the given matrix and store it in another variable.
  • Print the given matrix after rounding off the values.
  • 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('[5.3, 6, 4.6; 2.8, 7.79, 8.1; 9.99, 2, 1.02]')

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

# Apply the round() function on the given matrix to round off the values
# of the given matrix and store it in another variable
rslt = gvn_matrx.round()

# Print the given matrix after rounding off the values
print("The given matrix after rounding off the values:")
print(rslt)

Output:

The given matrix is:
[[5.3 6. 4.6 ]
 [2.8 7.79 8.1 ]
 [9.99 2. 1.02]]
The given matrix after rounding off the values:
[[ 5. 6. 5.]
 [ 3. 8. 8.]
 [10. 2. 1.]]

Python Numpy matrix.round() Function Read More »

Python Numpy matrix.argsort() 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.argsort() Function:

Using the argsort() method of the Numpy module, we can sort the elements in a specified matrix including one or more dimensions, and it will return the index value of the sorted elements.

Syntax:

 matrix.argsort()

Return Value:

The index number of sorted elements in the matrix is returned by the argsort() Function.

Numpy matrix.argsort() Function in Python

For 1-Dimensional (1D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing
    some random 1D matrix as an argument to it and store it in a variable
  • Print the given matrix.
  • Apply argsort() function on the given matrix to sort the elements of the given matrix and return the index value of the sorted elements.
  • Store it in another variable.
  • Print the index value of sorted elements in 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(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 8, 5 -9]')

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

# Apply argsort() function on the given matrix to sort the elements of the
# given matrix and return the index value of the sorted elements.
# Store it in another variable
rslt = gvn_matrx.argsort()

# Print the index value of sorted elements in a given matrix
print("The index value of sorted elements in a given matrix: ")
print(rslt)

Output:

The given matrix is:
[[ 2 8 5 -9]]
The index value of sorted elements in a given matrix: 
[[3 0 2 1]]

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
  • Print the given matrix.
  • Apply argsort() function on the given matrix to sort the elements of the given matrix and return the index value of the sorted elements.
  • Store it in another variable.
  • Print the index value of sorted elements in 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('[5, 6, -4; -2, 7, 8; 3, -1, 9]')

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

# Apply argsort() function on the given matrix to sort the elements of the
# given matrix and return the index value of the sorted elements.
# Store it in another variable
rslt = gvn_matrx.argsort()

# Print the index value of sorted elements in a given matrix
print("The index value of sorted elements in a given matrix: ")
print(rslt)

Output:

The given matrix is:
[[ 5 6 -4]
 [-2 7 8]
 [ 3 -1 9]]
The index value of sorted elements in a given matrix: 
[[2 0 1]
 [0 1 2]
 [1 0 2]]

 

Python Numpy matrix.argsort() Function Read More »

Python Numpy matrix.compress() 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.compress() Function:

We can choose items from a matrix using the matrix.compress() method of the Numpy module by giving a parameter as an array with the value 0 to not include the element or 1 to include the element in a matrix. To Put simply, the boolean array is passed to the matrix.compress() method.

0 – NOT include/selected

1 – Include

Syntax:

 matrix.compress()

Return Value:

A compressed array is returned by the compress() function.

Numpy matrix.compress() Function in Python

For 1-Dimensional (1D) Matrix

Approach:

  • Import numpy module using the import keyword
  • Create a matrix(1-Dimensional) using the matrix() function of numpy module by passing some random 1D matrix as an argument to it and store it in a variable
  • Print the given matrix.
  • Pass the boolean array(same length as given matrix) i.e with 0/1, given matrix as arguments to the compress() function of the numpy module to select the specific elements from the given matrix to be included.
  • Here 0 means – NOT include, 1 means – Include
  • So, the values corresponding to 1 are only shown here.
  • Print the compressed matrix for the 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(1-Dimensional) using the matrix() function of numpy module by passing 
# some random 1D matrix as an argument to it and store it in a variable
gvn_matrx = np.matrix('[2, 1, 6, 4, 7]')

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

# Pass the boolean array(same length as given matrix) i.e with 0/1, given matrix 
# as arguments to the compress() function of the numpy module to select the
# specific elements from the given matrix to be included.
# Here 0 means - NOT include, 1 means - Include 
# So, the values corresponding to 1 are only shown here.
rslt = np.compress([1, 0, 0, 1, 1], gvn_matrx)

# Print the compressed matrix for the given matrix.     
print("The compressed matrix for the given matrix is:")
print(rslt)

Output:

The given matrix is:
[[2 1 6 4 7]]
The compressed matrix for the given matrix is:
[[2 4 7]]

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
  • Print the given matrix.
  • Pass the boolean array(same length as given matrix) i.e with 0/1, given matrix as arguments to the compress() function of the numpy module to select the specific elements from the given matrix to be included.
  • Here 0 means – NOT include, 1 means – Include
  • So, the values corresponding to 1 are only shown here.
  • Print the compressed matrix for the 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('[10, 1, 4; 2, 7, 3; 8, 9, 5]')

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

# Pass the boolean array(same length as given matrix) i.e with 0/1, given matrix 
# as arguments to the compress() function of the numpy module to select the
# specific elements from the given matrix to be included.
# Here 0 means - NOT include, 1 means - Include 
# So, the values corresponding to 1 are only shown here.
rslt = np.compress([1, 0, 0, 1, 1, 0, 1, 0, 1], gvn_matrx)

# Print the compressed matrix for the given matrix.     
print("The compressed matrix for the given matrix is:")
print(rslt)

Output:

The given matrix is:
[[10 1 4]
 [ 2 7 3]
 [ 8 9 5]]
The compressed matrix for the given matrix is:
[[10 2 7 8 5]]

Python Numpy matrix.compress() Function Read More »