Skip to content

Instantly share code, notes, and snippets.

@leangaurav
Last active May 29, 2019 16:39
Show Gist options
  • Save leangaurav/2f8fe5d404cf50c9cfec0741cd9552ce to your computer and use it in GitHub Desktop.
Save leangaurav/2f8fe5d404cf50c9cfec0741cd9552ce to your computer and use it in GitHub Desktop.

matrix.py

def is_symmetric(mat):
    if len(mat) != 0 and (len(mat) != len(mat[0])):# check if square matrix
        return False
    
    for i in range(len(mat)):
        for j in range( len(mat[0]) ):
            if mat[i][j] != mat[j][i]:
                return False
    return True

Test Code

import matrix

mat1 = matrix.create_linear_matrix(3,3)

mat2 = matrix.transpose(mat1)

mat3 = matrix.add(mat1, mat2) # M + Mt

print("Mat1:", matrix.is_symmetric(mat1))
matrix.print_matrix(mat1)

print("\nMat3:", matrix.is_symmetric(mat3))
matrix.print_matrix(mat3)

Output
Mat1: False
1 2 3
4 5 6
7 8 9

Mat3: True
2 6 10
6 10 14
10 14 18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment