Twist_Nemo

matrix using numpy python

Mar 28th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.67 KB | None | 0 0
  1. #This code is valid for only 3x3 matrix
  2.  
  3. import numpy as np
  4.  
  5. R1 = int(input("Enter the number of rows:"))
  6. C1 = int(input("Enter the number of columns:"))
  7.  
  8. print("Enter the entries in a single line (separated by space): ")
  9.  
  10. # User input of entries in a
  11. # single line separated by space
  12. entries = list(map(int, input().split()))
  13.  
  14. # For printing the matrix
  15. matrix1 = np.array(entries).reshape(R1, C1)
  16. print(matrix1)
  17.  
  18. R2 = int(input("Enter the number of rows:"))
  19. C2 = int(input("Enter the number of columns:"))
  20. print("Enter the entries in a single line (separated by space): ")
  21. entries = list(map(int, input().split()))
  22. matrix2 = np.array(entries).reshape(R2, C2)
  23. print(matrix2)
  24. result = [[0,0,0],
  25.          [0,0,0],
  26.          [0,0,0]]
  27.  
  28. #adding
  29. for i in range(len(matrix1)):
  30.    # iterate through columns
  31.    for j in range(len(matrix1[0])):
  32.        result[i][j] = matrix1[i][j] + matrix2[i][j]
  33.  
  34. for r in result:
  35.    print(r)
  36.  
  37. # Subtracting
  38. for i in range(len(matrix1)):
  39.    # iterate through columns
  40.    for j in range(len(matrix1[0])):
  41.        result[i][j] = matrix1[i][j] - matrix2[i][j]
  42.  
  43. for a in result:
  44.    print(a)
  45.  
  46.  
  47. # multiplication
  48. for i in range(len(matrix1)):
  49.    # iterate through columns
  50.    for j in range(len(matrix1[0])):
  51.        result[i][j] = matrix1[i][j] * matrix2[i][j]
  52.  
  53. for m in result:
  54.    print(m)
  55.  
  56. #division
  57. for i in range(len(matrix1)):
  58.    # iterate through columns
  59.    for j in range(len(matrix1[0])):
  60.        result[i][j] = matrix1[i][j] / matrix2[i][j]
  61.  
  62. for d in result:
  63.    print(d)
  64.  
  65.  
  66.  
  67. #inverse of matrix1
  68.  
  69. print(np.linalg.inv(matrix1))
  70.  
  71.  
  72. #transpose of matrix1
  73. matrix1_transpose = np.transpose(matrix1)
  74. print(matrix1_transpose)
  75.  
  76.  
Advertisement
Add Comment
Please, Sign In to add comment