check symmetrical matrix

Asked

Viewed 905 times

1

The exercise asks you to make a function that checks whether a matrix is symmetric.

Rows and columns are respectively the variables i and j the program always returns true, but I don’t know what’s wrong.

def simetrica(mat):
    for i in range(len(mat)):
        for j in range(len(mat[0])):
            if mat[i][j]!=mat[j][i]:
                return False
    return True
ela=[[3,2,5],[2,5,6,],[5,6,7]]
fazer=simetrica(ela)
  • 2

    It seems to be correct. I could do an optimization, to go through only half of the matrix, because make the comparison i, j = A, B will be the same comparison of i, j = B, A, being A and B positions in the matrix, so there is no need to do both; one of them is already sufficient to ensure symmetry.

2 answers

1

"the program always returns true"

It’s not true, I tested it with a non-symmetric matrix and it returned False:

m = [ [3, 2, 5], [1, 5, 6], [15, 4, 7] ]
print(simetrica(m)) # False

Just a few details to settle/improve.

Actually what you have is not really a matrix, but a list of lists (the only native form of the language to simulate matrices).

This is because in an array of dimensions M x N (M rows and N columns), all M rows have exactly N elements. But in a list list, there is no such obligation (there is nothing in the language that requires to have a certain size, it is you who have to control and ensure this): each "line" can have a different amount of elements. So nothing prevents me from having a "matrix-that-isn’t-a-matrix":

m = [
  [3],
  [2, 5, 6],
  [5, 6]
]

Thus, the function would also need to verify that all "lines" have the same amount of elements. For example, if we pass a list like:

m = [
  [3, 2, 5],
  [2, 5, 6, 8],
  [5, 6, 7]
]

The function returns True. That’s because the loop intern (for j) only goes as far as len(mat[0]), which is the size of the first "line", and since it only has 3 elements, the fourth element of the second "line" (number 8) is ignored by loop.

Also, as a symmetrical matrix can only be square (the number of rows is equal to the number of columns), we can use any to check if each of the "lines" has the same size as the "matrix". Something like this:

if any(len(linha) != len(mat) for linha in mat):
    return False

That is, if any "line" has a different size, I return False and no need to continue.

Another improvement is that you don’t need to go through all the elements. Checking the diagonal is redundant (because diagonally, i is equal to j, then in fact you will be comparing the element with itself). Just go to the diagonal and compare one of the "halves" with the other "half". For example, if I compared the element mat[0][1] with mat[1][0], is redundant to make another comparison of mat[1][0] with mat[0][1]. That is, the j doesn’t have to go from start to finish, it can only go up i:

def simetrica(mat):
    if any(len(linha) != len(mat) for linha in mat):
        return False

    for i in range(len(mat)):
        for j in range(i): # só vou até a diagonal
            if mat[i][j] != mat[j][i]:
                return False
    return True

One detail is that so we have to go through the matrix twice: one to check the sizes (the if any(...)), and another to compare the elements. But if you want, you can do everything at once:

def simetrica(mat):
    for i, linha in enumerate(mat):
        if len(linha) != len(mat): # tamanho diferente, não é matriz quadrada
            return False # então não pode ser simétrica
        for j in range(i):
            if linha[j] != mat[j][i]:
                return False
    return True

0

Come on. A matrix is symmetric when it equals its transposed. and for that to be true it necessarily has to be square. in other words Aij == Aji for any element of this matrix.

The code below demonstrates this:

M1 = [[3,5,6],
     [5,2,4],
     [6,4,8]]

M2 = [[3,5,6],
     [5,2,4],
     [6,7,8]]

def ver_simetri(M):
    for i in range(len(M)):
        for j  in range(len(M[0])):
            if M[i][j] != M[j][i]:
                return False
    return True

Applying the function on M1 will return True while on M2 will return False. It is enough that Aij is different from Aji only once for the matrix not to be symmetric. I hope I’ve helped

  • 1

    Isn’t that exactly the same code as the question? I didn’t quite understand what you meant by the answer.

  • I’m sorry. I didn’t notice the code. I just wanted to clarify the procedure.

  • If you want I can remove the answer.

  • That only depends on you... I only commented because I really didn’t understand your intention.

  • I think you can help people who have that doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.