-1

This python code should check the matrix symmetry and should return 1 if it is symmetric, -1 if it is skew symmetric and 0 otherwise. My code always returns 1. How come?

N=3
def isSymmetric(mat,N): 
for i in range(N): 
    for j in range(N): 
      return  (mat[i][j] == mat[j][i])
 def isSkew(mat,N):
for i in range(N):
    for j in range(N):
        return (mat[i][j]==-mat[j][i])
def test(mat,N):
if isSymmetric(mat,N):
    return 1
if isSkew(mat,N):
    return -1
else:
    return 0;

 # Driver code 
  mat = [ [0,1,0 ], [ 4,4,3 ], [5,4,6  ] ] 
  print(test(isSymmetric(mat,N)))
Usman
  • 1,983
  • 15
  • 28

1 Answers1

1

The logic of your code is kind of messed up. Every function is returning a boolean and those first two are only running a single comparison before they return a value and stop running. They aren't actually looking at the whole matrix and what you are returning wouldn't really make sense even if they did. A good rule of thumb is to think of the assignment (=) operator as making a statement (You are the team leader.) and the equality (==) operator as asking a question (Are you the team leader?). You use that to check if values are equal, not to assert that they are.

If you say return x==y all you will get is a boolean that tells you if those variables store the same value, and in the case of your loop they always will. This is because mat[0][0](the first element of mat[i][j]), will always equal mat[0][0] (the first element of mat[j][i]). What you want those first two functions to do is generate transposed and negative transposed matrices. Then your test function can test those matrices against your original and see if they are symmetric or skew. This works:

def isSymmetric(mat, N): 
  newmat = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
  for i in range(N): 
    for j in range(N): 
      newmat[i][j] = mat[j][i]
  return newmat

def isSkew(mat, N):
  newmat = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
  for i in range(N):
    for j in range(N):
        newmat[i][j] = -mat[j][i]
  return newmat

def test(mat, N):
  if isSymmetric(mat, N) == mat:
    return 1
  if isSkew(mat, N) == mat:
    return -1
  else:
    return 0;

N = 3
mat = [[0, 1, -2], [-1, 0, 3], [2, -3, 0]] 
print(test(mat, N))
>>> -1

N = 3
mat = [[1, 1, -1], [1, 2, 0], [-1, 0, 5]] 
print(test(mat, N))
>>> 1

N = 3
mat = [[1, 1, -1], [11, 2, 0], [4, 9, 5]] 
print(test(mat, N))
>>> 0
  • 1
    Thank you very much for your help. The code runs smoothly. However, I would like to ask you a few questions, since I am fairly new to python and I am not quite understanding how everything works. 1-when it comes to newmat=[0,0,0][0,0,0][0,0,0] what are you doing exactly? why are all the entries set to 0? I have personally chosen the parameter N after reading other codes online but i am not sure what N really is, moreover, why is it set to 3? In my previous code I have tested with N=1,2,3... and always obtained 1, instead with N=-1,-2,-3... I obtained 0. What exactly is N ? Thanks. – mcgrim8 mcgrim8 Mar 20 '19 at 08:51
  • Hi. Glad to help. `newmat` is an empty matrix that gets filled in as the `isSymmetric` and `isSkew` functions run through their loops. The zeroes are arbitrary. I just needed a matrix of the appropriate size to fill in. What each of those functions are doing is to run through the rows and columns of the matrix and performing an operation. `N` is how far `i` and `j` will go before resetting back to 0. It is set to 3 in this case because we are dealing with 3X3 matrices so we have 3 rows and 3 columns. The code above goes through each element `i,j` and transfers it to a new matrix as –  Mar 20 '19 at 13:59
  • element `j,i`, or its negative in the case of the skew function. The `test` function then checks these matrices against the original matrix, `mat`, to see if they match. The main reason your initial code didn't work was that it was returning inside the loop. Since a function ends when it returns, only the first element was being checked. Since element 0,0 is the same element even when you flip i and j, it always returned true. When you set a negative number as N, you are basically saying to loop from 0 to -2, which won't generate a matrix. Since you end up comparing `mat` to nothing, it –  Mar 20 '19 at 14:05
  • always returns `false`. –  Mar 20 '19 at 14:05