Q:

Python program for matrix operations

belongs to collection: Python Array Programs

0

Operations that we are performing here, (Here, x and y are the matrices)...

Operations Function Operator
Adding matrix elements add(x,y) x+y
Subtracting matrix elements subtract(x,y) x-y
Multiplying matrix elements multiply(x,y) x*y
Dividing matrix elements divide(x,y) x/y
Product of the matrices dot(x,y) ---
Square root of the matrix elements sqrt(x) ---

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example:

# Python program for matrix operations

# importing numpy
import numpy as np

mat1 = np.array([[10,20,30],[40,50,60],[70,80,90]])
mat2 = np.array([[1,2,3],[4,5,6],[7,8,9]])

# adding matrices
print("mat1+mat2...")
print(mat1+mat2)
print("np.add(mat1,mat2)...")
print(np.add(mat1,mat2))
print() # prints newline

# subtracting  matrices
print("mat1-mat2...")
print(mat1-mat2)
print("np.subtract(mat1,mat2)...")
print(np.subtract(mat1,mat2))
print() # prints newline

# dividing  matrices
print("mat1/mat2...")
print(mat1/mat2)
print("np.divide(mat1,mat2)...")
print(np.divide(mat1,mat2))
print() # prints newline

# multiplying  matrices
print("mat1*mat2...")
print(mat1*mat2)
print("np.multiply(mat1,mat2)...")
print(np.multiply(mat1,mat2))
print() # prints newline

# producting matrices
print("np.dot(mat1,mat2)...")
print(np.dot(mat1,mat2))
print() # prints newline

# Square root of matrix elements
print("np.sqrt(mat1)...")
print(np.sqrt(mat1))
print() # prints newline

# Square root of matrix elements
print("np.sqrt(mat2)...")
print(np.sqrt(mat2))
print() # prints newline

Output

mat1+mat2...
[[11 22 33]
 [44 55 66]
 [77 88 99]]
np.add(mat1,mat2)...
[[11 22 33]
mat1+mat2...
[[11 22 33]
 [44 55 66]
 [77 88 99]]
mat1+mat2...
[[11 22 33]
 [44 55 66]
 [77 88 99]]
np.add(mat1,mat2)...
[[11 22 33]
 [44 55 66]
 [77 88 99]]

mat1-mat2...
[[ 9 18 27]
 [36 45 54]
 [63 72 81]]
np.subtract(mat1,mat2)...
[[ 9 18 27]
 [36 45 54]
mat1+mat2...
[[11 22 33]
 [44 55 66]
 [77 88 99]]
np.add(mat1,mat2)...
[[11 22 33]
 [44 55 66]
 [77 88 99]]

mat1-mat2...
[[ 9 18 27]
 [36 45 54]
 [63 72 81]]
np.subtract(mat1,mat2)...
[[ 9 18 27]
 [36 45 54]
 [63 72 81]]

mat1/mat2...
[[10. 10. 10.]
 [10. 10. 10.]
 [10. 10. 10.]]
np.divide(mat1,mat2)...
[[10. 10. 10.]
 [10. 10. 10.]
 [10. 10. 10.]]

mat1*mat2...
[[ 10  40  90]
 [160 250 360]
 [490 640 810]]
np.multiply(mat1,mat2)...
[[ 10  40  90]
 [160 250 360]
 [490 640 810]]

np.dot(mat1,mat2)...
[[ 300  360  420]
 [ 660  810  960]
 [1020 1260 1500]]

np.sqrt(mat1)...
[[3.16227766 4.47213595 5.47722558]
 [6.32455532 7.07106781 7.74596669]
 [8.36660027 8.94427191 9.48683298]]

np.sqrt(mat2)...
[[1.         1.41421356 1.73205081]
 [2.         2.23606798 2.44948974]
 [2.64575131 2.82842712 3.        ]]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python program to get matrix as input from user an... >>
<< Python program to create matrix using numpy...