Advertisement
brandblox

python lab (12/03/2024)

Mar 12th, 2024
703
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.06 KB | None | 0 0
  1. #accept and print matrix
  2. matrix = []
  3.  
  4. for i in range(3):
  5.     row = []
  6.     for j in range(3):
  7.         row.append(int(input(f"Enter element at position ({i+1}, {j+1}): ")))
  8.     matrix.append(row)
  9.  
  10. print("Entered 3x3 matrix:")
  11. for row in matrix:
  12.     print(row)
  13.  
  14. output:
  15. Enter element at position (1, 1): 1
  16. Enter element at position (1, 2): 2
  17. Enter element at position (1, 3): 3
  18. Enter element at position (2, 1): 4
  19. Enter element at position (2, 2): 5
  20. Enter element at position (2, 3): 6
  21. Enter element at position (3, 1): 7
  22. Enter element at position (3, 2): 8
  23. Enter element at position (3, 3): 9
  24. Entered 3x3 matrix:
  25. [1, 2, 3]
  26. [4, 5, 6]
  27. [7, 8, 9]
  28.  
  29.  
  30. #accept matrix and element sum
  31. matrix = []
  32. a=0
  33. sum=0
  34.  
  35. for i in range(3):
  36.     row = []
  37.     for j in range(3):
  38.         a=int(input(f"Enter element at position ({i+1}, {j+1}): "))
  39.         row.append(a)
  40.         sum+=a
  41.     matrix.append(row)
  42.  
  43. print("Entered 3x3 matrix:")
  44. for row in matrix:
  45.     print(row)
  46.    
  47. print("Sum = ",sum)
  48.  
  49. Output:
  50. Enter element at position (1, 1): 1
  51. Enter element at position (1, 2): 2
  52. Enter element at position (1, 3): 3
  53. Enter element at position (2, 1): 4
  54. Enter element at position (2, 2): 5
  55. Enter element at position (2, 3): 6
  56. Enter element at position (3, 1): 7
  57. Enter element at position (3, 2): 8
  58. Enter element at position (3, 3): 9
  59. Entered 3x3 matrix:
  60. [1, 2, 3]
  61. [4, 5, 6]
  62. [7, 8, 9]
  63. Sum =  45
  64.  
  65.  
  66.  
  67. #Accept matrix and trace
  68. matrix = []
  69.  
  70. for i in range(3):
  71.     row = []
  72.     for j in range(3):
  73.         element = int(input(f"Enter element at position ({i+1}, {j+1}): "))
  74.         row.append(element)
  75.     matrix.append(row)
  76.  
  77. print("Entered 3x3 matrix:")
  78. for row in matrix:
  79.     print(row)
  80.  
  81. trace = 0
  82. for i in range(3):
  83.     trace += matrix[i][i]
  84.  
  85. print(f"Trace of the matrix: {trace}")
  86.  
  87. Output:
  88. Enter element at position (1, 1): 1
  89. Enter element at position (1, 2): 2
  90. Enter element at position (1, 3): 3
  91. Enter element at position (2, 1): 4
  92. Enter element at position (2, 2): 5
  93. Enter element at position (2, 3): 6
  94. Enter element at position (3, 1): 7
  95. Enter element at position (3, 2): 8
  96. Enter element at position (3, 3): 9
  97. Entered 3x3 matrix:
  98. [1, 2, 3]
  99. [4, 5, 6]
  100. [7, 8, 9]
  101. Trace of the matrix: 15
  102.  
  103.  
  104.  
  105. #Matrix multiplication
  106. import numpy as np
  107.  
  108. matrix1 = []
  109. for i in range(3):
  110.     row = []
  111.     for j in range(3):
  112.         element = int(input(f"Enter element for matrix1 at position ({i+1}, {j+1}): "))
  113.         row.append(element)
  114.     matrix1.append(row)
  115.  
  116. matrix2 = []
  117. for i in range(3):
  118.     row = []
  119.     for j in range(3):
  120.         element = int(input(f"Enter element for matrix2 at position ({i+1}, {j+1}): "))
  121.         row.append(element)
  122.     matrix2.append(row)
  123.  
  124. print("Entered 3x3 matrix 1:")
  125. for row in matrix1:
  126.     print(row)
  127.  
  128. print("\nEntered 3x3 matrix 2:")
  129. for row in matrix2:
  130.     print(row)
  131.  
  132. array1 = np.array(matrix1)
  133. array2 = np.array(matrix2)
  134.  
  135.  
  136. result_array = np.dot(array1, array2)
  137.  
  138. print("\nResult of matrix multiplication:")
  139. print(result_array)
  140.  
  141. Output:
  142. Enter element for matrix1 at position (1, 1): 1
  143. Enter element for matrix1 at position (1, 2): 2
  144. Enter element for matrix1 at position (1, 3): 3
  145. Enter element for matrix1 at position (2, 1): 4
  146. Enter element for matrix1 at position (2, 2): 5
  147. Enter element for matrix1 at position (2, 3): 6
  148. Enter element for matrix1 at position (3, 1): 7
  149. Enter element for matrix1 at position (3, 2): 8
  150. Enter element for matrix1 at position (3, 3): 9
  151. Enter element for matrix2 at position (1, 1): 1
  152. Enter element for matrix2 at position (1, 2): 10
  153. Enter element for matrix2 at position (1, 3): 11
  154. Enter element for matrix2 at position (2, 1): 12
  155. Enter element for matrix2 at position (2, 2): 13
  156. Enter element for matrix2 at position (2, 3): 14
  157. Enter element for matrix2 at position (3, 1): 15
  158. Enter element for matrix2 at position (3, 2): 16
  159. Enter element for matrix2 at position (3, 3): 17
  160. Entered 3x3 matrix 1:
  161. [1, 2, 3]
  162. [4, 5, 6]
  163. [7, 8, 9]
  164.  
  165. Entered 3x3 matrix 2:
  166. [1, 10, 11]
  167. [12, 13, 14]
  168. [15, 16, 17]
  169.  
  170. Result of matrix multiplication:
  171. [[ 70  84  90]
  172.  [154 201 216]
  173.  [238 318 342]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement