-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_symmetry_test.py
50 lines (44 loc) · 1.24 KB
/
matrix_symmetry_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# A function that test whether or not a matrix is symmetric
# The Implicit Way (using numpy functions)
# Also using the manual way
import numpy as np
def is_symmetric1(matrix):
# Assuming input matrix is a numpy array
symmetry = True
I, J = matrix.shape
# A symmetric matrix is a square matrix
if I != J:
return False
# A symmetric matrix transpose is itself
for i in range(I):
for j in range(J):
if matrix[i, j] != matrix[j, i]:
return False
return symmetry
def is_symmetric2(matrix):
# Assuming input matrix is a numpy array
transpose = matrix.transpose()
if np.array_equal(matrix, transpose):
return True
return False
#Testing our functions
Sym1 = np.zeros((10, 10))
Sym2 = np.ones((5, 5))
Asym1 = np.zeros((10,4))
Asym2 = np.ones((3, 6))
print("Sym1 is symmetric: ")
print(is_symmetric1(Sym1))
print("Sym1 is symmetric :")
print(is_symmetric2(Sym1))
print("Sym2 is symmetric: ")
print(is_symmetric1(Sym2))
print("Sym2 is symmetric: ")
print(is_symmetric2(Sym2))
print("Asym1 is symmetric: ")
print(is_symmetric1(Asym1))
print("Asym2 is symmetric: ")
print(is_symmetric2(Asym2))
print("Asym1 is symmetric:")
print(is_symmetric2(Asym1))
print("Asym2 is symmetric")
print(is_symmetric1(Asym2))