Thumbnail article
Matrix operation in python
A matrix is a collection of numbers arranged in the order of rows and columns, and it is delimited by a parenthesis. A Python matrix is a specialized two-dimensional rectangular array of data stored in columns and rows. The data in a matrix can be expressions, symbols, numbers, strings, etc. Matrix is one of the important data structures that can be used in mathematical and scientific calculations.There is a python library called numpy that can be used to do a matrix operation. Numpy is commonly used to do numerical calculations. In numpy matrix operations are also covered by special classes and sub-packages. By skipping multiple for loops, vectorization allows numpy to perform matrix operations more efficiently. The code below is the code to do a several matrix operation (addition, subtraction, division, multiplication, dot product and transposition matrix).
import numpy

# Two matrices are initialized by value
x = numpy.array([[1, 2], [4, 5]])
y = numpy.array([[7, 8], [9, 10]])

print ("Addition of two matrices: ")
print (numpy.add(x,y))

print ("Subtraction of two matrices : ")
print (numpy.subtract(x,y))

print ("Multiplication of two matrices: ")
print (numpy.multiply(x,y))

print ("Matrix transposition : ")
print (x.T)
Two matrices can be added if they have the same order. The result of the addition operation is a new matrix that has the same order as the original matrix, with its elements consisting of the sum of the elements in the matrix.Subtraction of matrices has the same concept as addition. Two matrices can be subtracted if they have the same order. The result of the subtraction operation is a new matrix that has the same order as the original matrix, with its elements consisting of the result of subtraction with the elements in the matrix.Multiplication of a matrix by a scalar is done by multiplying each element of the matrix by the scalar, and producing a matrix with the order of the matrix being multiplied.Transpose matrix is a new matrix that is formed by swapping rows and columns in the original matrix.For an intermediate use you can read the documentation here. https://numpy.org/doc/stable/

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *