Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #accept and print matrix
- matrix = []
- for i in range(3):
- row = []
- for j in range(3):
- row.append(int(input(f"Enter element at position ({i+1}, {j+1}): ")))
- matrix.append(row)
- print("Entered 3x3 matrix:")
- for row in matrix:
- print(row)
- output:
- Enter element at position (1, 1): 1
- Enter element at position (1, 2): 2
- Enter element at position (1, 3): 3
- Enter element at position (2, 1): 4
- Enter element at position (2, 2): 5
- Enter element at position (2, 3): 6
- Enter element at position (3, 1): 7
- Enter element at position (3, 2): 8
- Enter element at position (3, 3): 9
- Entered 3x3 matrix:
- [1, 2, 3]
- [4, 5, 6]
- [7, 8, 9]
- #accept matrix and element sum
- matrix = []
- a=0
- sum=0
- for i in range(3):
- row = []
- for j in range(3):
- a=int(input(f"Enter element at position ({i+1}, {j+1}): "))
- row.append(a)
- sum+=a
- matrix.append(row)
- print("Entered 3x3 matrix:")
- for row in matrix:
- print(row)
- print("Sum = ",sum)
- Output:
- Enter element at position (1, 1): 1
- Enter element at position (1, 2): 2
- Enter element at position (1, 3): 3
- Enter element at position (2, 1): 4
- Enter element at position (2, 2): 5
- Enter element at position (2, 3): 6
- Enter element at position (3, 1): 7
- Enter element at position (3, 2): 8
- Enter element at position (3, 3): 9
- Entered 3x3 matrix:
- [1, 2, 3]
- [4, 5, 6]
- [7, 8, 9]
- Sum = 45
- #Accept matrix and trace
- matrix = []
- for i in range(3):
- row = []
- for j in range(3):
- element = int(input(f"Enter element at position ({i+1}, {j+1}): "))
- row.append(element)
- matrix.append(row)
- print("Entered 3x3 matrix:")
- for row in matrix:
- print(row)
- trace = 0
- for i in range(3):
- trace += matrix[i][i]
- print(f"Trace of the matrix: {trace}")
- Output:
- Enter element at position (1, 1): 1
- Enter element at position (1, 2): 2
- Enter element at position (1, 3): 3
- Enter element at position (2, 1): 4
- Enter element at position (2, 2): 5
- Enter element at position (2, 3): 6
- Enter element at position (3, 1): 7
- Enter element at position (3, 2): 8
- Enter element at position (3, 3): 9
- Entered 3x3 matrix:
- [1, 2, 3]
- [4, 5, 6]
- [7, 8, 9]
- Trace of the matrix: 15
- #Matrix multiplication
- import numpy as np
- matrix1 = []
- for i in range(3):
- row = []
- for j in range(3):
- element = int(input(f"Enter element for matrix1 at position ({i+1}, {j+1}): "))
- row.append(element)
- matrix1.append(row)
- matrix2 = []
- for i in range(3):
- row = []
- for j in range(3):
- element = int(input(f"Enter element for matrix2 at position ({i+1}, {j+1}): "))
- row.append(element)
- matrix2.append(row)
- print("Entered 3x3 matrix 1:")
- for row in matrix1:
- print(row)
- print("\nEntered 3x3 matrix 2:")
- for row in matrix2:
- print(row)
- array1 = np.array(matrix1)
- array2 = np.array(matrix2)
- result_array = np.dot(array1, array2)
- print("\nResult of matrix multiplication:")
- print(result_array)
- Output:
- Enter element for matrix1 at position (1, 1): 1
- Enter element for matrix1 at position (1, 2): 2
- Enter element for matrix1 at position (1, 3): 3
- Enter element for matrix1 at position (2, 1): 4
- Enter element for matrix1 at position (2, 2): 5
- Enter element for matrix1 at position (2, 3): 6
- Enter element for matrix1 at position (3, 1): 7
- Enter element for matrix1 at position (3, 2): 8
- Enter element for matrix1 at position (3, 3): 9
- Enter element for matrix2 at position (1, 1): 1
- Enter element for matrix2 at position (1, 2): 10
- Enter element for matrix2 at position (1, 3): 11
- Enter element for matrix2 at position (2, 1): 12
- Enter element for matrix2 at position (2, 2): 13
- Enter element for matrix2 at position (2, 3): 14
- Enter element for matrix2 at position (3, 1): 15
- Enter element for matrix2 at position (3, 2): 16
- Enter element for matrix2 at position (3, 3): 17
- Entered 3x3 matrix 1:
- [1, 2, 3]
- [4, 5, 6]
- [7, 8, 9]
- Entered 3x3 matrix 2:
- [1, 10, 11]
- [12, 13, 14]
- [15, 16, 17]
- Result of matrix multiplication:
- [[ 70 84 90]
- [154 201 216]
- [238 318 342]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement