Matrix mathematical operations in Python Using Numpy
Here we are covering different mathematical operations such as addition subtraction,
multiplication, and division using Numpy.
Python3
# initializing matrices
x = numpy.array([[1, 2], [4, 5]])
y = numpy.array([[7, 8], [9, 10]])
# using add() to add matrices
print ("The element wise addition of matrix is : ")
print (numpy.add(x,y))
# using subtract() to subtract matrices
print ("The element wise subtraction of matrix is : ")
print (numpy.subtract(x,y))
print ("The element wise multiplication of matrix is : ")
print (numpy.multiply(x,y))
# using divide() to divide matrices
print ("The element wise division of matrix is : ")
print (numpy.divide(x,y))
Output:
The element wise addition of matrix is :
[[8 10]
[13 15]]
The element wise subtraction of matrix is :
[[-6 -6]
[-5 -5]]
The element wise multiplication of matrix is:
[[7 16]
[36 50]]
The element wise division of matrix is:
[[0.14285714 0.25 ]
[0.44444444 0.5 ]]
Dot and cross product with Matrix
Here, we will find the inner, outer, and cross products of matrices and vectors
using NumPy in Python.
Python3
X = [[1, 2, 3],[4, 5, 6],[7, 8, 9]]
Y = [[9, 8, 7], [6, 5, 4],[3, 2, 1]]
dotproduct = np.dot(X, Y)
print("Dot product of two array is:", dotproduct)
dotproduct = np.cross(X, Y)
print("Cross product of two array is:", dotproduct)