0

I'm having trouble getting the correct output from numpy.array_equal. The identity array is created using np.eye(size), and the result_ab array I'm comparing it with was built with np.array([]).

I keep getting the opposite result from what I expect. I've tested with both integer and float arrays and keep getting False when it should return True.

I've tested with allclose and array_equiv, but I don't think those are the right methods for this (they also tend to fail or give false positives). I've also printed the type of the result_ab and identity arrays and it returned <class 'numpy.ndarray'> for both.

I am getting an empty array for the identity output.

rows = int(input("Enter the number of rows: "))
cols = int(input("Enter the number of columns: "))

#create placeholder for rows and cols lists
row_list = []
col_list = []

matrix_a = []
matrix_b = []

res_ab = np.array([])

#check if matrices have same number of rows, cols
def check(rows, cols):
  if rows != cols:
    print("The matrix must be square. Enter the same number of rows and columns.")
  else:
    m_a_input(rows)
    m_b_input(rows)

#create for loop to get 1 digit per row index
def m_a_input(rows):
  for i in range(rows):
    row = list(map(int, input("Enter row digits separated by a space").split()))
    matrix_a.append(row)
  print(matrix_a)

def m_b_input(rows):
  for i in range(rows):
    row = list(map(int, input("Enter row digits separated by a space").split()))
    matrix_b.append(row)
  print(matrix_b)

def mul_matrices(matrix_a, matrix_b):
  res_ab = np.matmul(matrix_a, matrix_b)
  print(res_ab)

def is_identity(res_ab):
    # Get the size of the matrix
    size = res_ab.shape[0]
    # Create the identity matrix of the same size
    identity = np.identity(size)
    print(identity)
    print(res_ab)
    # Check if the matrix is equal to the identity matrix using numpy.allclose
    if np.array_equal(res_ab, identity) == True:
      print("true")
    else:
      print("false")

check(rows, cols)
mul_matrices(matrix_a, matrix_b)
is_identity(res_ab)```
11
  • 2
    what's the dtypes? Commented Feb 20, 2024 at 18:57
  • Both dtypes are returning as float64. Commented Feb 20, 2024 at 19:04
  • your code seems to work correctly, when I run it on my end? Commented Feb 20, 2024 at 19:39
  • 1
    your function doesnt seem to be using rows parameter, please make sure you are not missing an important part of code to include here? Commented Feb 20, 2024 at 19:45
  • Be careful when comparing flosts. allclose may be better. Sometimes you need to actually look at the values, or do a elementwise == or difference. array_equal can fail in a number different ways. We call this 'debugging' Commented Feb 20, 2024 at 20:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.