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.byteswap() Function:
Using the matrix.byteswap() method, we can swap the bytes position of an element in a specified matrix with one or more dimensions. It would not work on a string or character matrix.
Syntax:
matrix.byteswap()
Return Value:
The byte swapped matrix is returned by the byteswap() function.
Numpy matrix.byteswap() 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
- Apply byteswap() function on the given matrix to swap the place/position of bytes of an element in a given matrix.
- Store it in another variable
- Print the given matrix after byte swapping.
- 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, 3]') # Apply byteswap() function on the given matrix to swap the place/position of bytes of an # element in a given matrix. # Store it in another variable rslt = gvn_matrx.byteswap() # Print the given matrix after byte swapping. print("The given matrix after byte swapping:") print(rslt)
Output:
The given matrix after byte swapping: [[144115188075855872 72057594037927936 432345564227567616 216172782113783808]]
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 byteswap() function on the given matrix to swap the place/position of bytes of an element in a given matrix.
- Store it in another variable
- Print the given matrix after byte swapping.
- 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('[5, 4; 1, 3]') # Apply byteswap() function on the given matrix to swap the place/position of bytes of an # element in a given matrix. # Store it in another variable rslt = gvn_matrx.byteswap() # Print the given matrix after byte swapping. print("The given matrix after byte swapping:") print(rslt)
Output:
The given matrix after byte swapping: [[360287970189639680 288230376151711744] [ 72057594037927936 216172782113783808]]