class BinaryMatrix(object):
def __init__(self, matrix):
self.matrix = matrix
def Multiplication(self,newMatrix):
result = []
for i in self.matrix:
row = []
counter = 0
for j in i:
product = 0;
for currenti in i:
product += int(currenti)*int(newMatrix[int(currenti)][counter])
if(product!=0):
product = 1
row.append(product)
counter+=1
result.append(row)
toReturn = BinaryMatrix(result)
return toReturn
def Intersection(self,newMatrix):
result = []
rowCounter=0
for i in self.matrix:
columncounter=0
row = []
for j in i:
row.append(int(j)*int(newMatrix[rowCounter][columncounter]))
columncounter+=1
result.append(row)
rowCounter+=1
toReturn = BinaryMatrix(result)
return toReturn
def Precedence(self, newMatrix):
result = []
rowCounter=0
for i in self.matrix:
columncounter=0
row = []
for j in i:
if(int(j)>int(newMatrix[rowCounter][columncounter])):
return False
columncounter+=1
result.append(row)
rowCounter+=1
return True
def Transposed(self):
result = []
row = 0
for i in self.matrix:
column = 0
newRow = []
for j in i:
newRow.append(self.matrix[column][row])
column+=1
row+=1
result.append(newRow)
return result
def equals(self,newMatrix):
result = []
rowCounter=0
for i in self.matrix:
columncounter=0
row = []
for j in i:
if(int(j)!=int(newMatrix[rowCounter][columncounter])):
return False
columncounter+=1
result.append(row)
rowCounter+=1
return True
class Relation(object):
def __init__(self, matrix):
super(Relation, self).__init__()
self.matrix = matrix
def isReflexive(self):
InColumn = []
row = 0
for i in self.matrix.matrix:
currentRow = []
column = 0
for j in i:
if(row==column):
currentRow.append(1)
else:
currentRow.append(0)
column+=1
InColumn.append(currentRow)
row+=1
In = BinaryMatrix(InColumn)
return(In.Precedence(self.matrix.matrix))
def isSymmetric(self):
return(self.matrix.equals(self.matrix.Transposed()))
def isAntisymmetric(self):
InColumn = []
row = 0
for i in self.matrix.matrix:
currentRow = []
column = 0
for j in i:
if(row==column):
currentRow.append(1)
else:
currentRow.append(0)
column += 1
InColumn.append(currentRow)
row += 1
In = InColumn
return((self.matrix.Intersection(self.matrix.Transposed())).Precedence(In))
def isTransitive(self):
return((self.matrix.Multiplication(self.matrix.matrix)).Precedence(self.matrix.matrix))
print("""
If you want to write the matrix:
_ _
| |
| 1 0 1 |
| 0 1 0 |
| 0 1 1 |
|_ _|
You must write:
Use "," for divide elements in a row and "|" for divide rows in the matrix
input: 1,0,1|0,1,0|0,1,1
""")
print("1.A relation R is reflexive if the matrix diagonal elements are 1.")
print("3.A relation R is symmetric if the transpose of relation matrix is equal to its original relation matrix. i.e. MR = (MR)T.")
print("4.A relation R is antisymmetric if either mij = 0 or mji =0 when i≠j.")
print("5.A matrix is said to be transitive if and only if the element of the matrix a is related to b and b is related to c, then a is also related to c.")
print(" ")
userInput = input("Write your input:\t\t")
newMatrix = userInput.split('|')
matrix = []
for row in newMatrix:
newRow = row.split(',')
matrix.append(newRow)
for row in matrix:
for element in row:
element = int(element)
myMatrix = BinaryMatrix(matrix)
myRelation = Relation(myMatrix)
print("------------------------------------------------------")
print(" The relation satisfies the following properties\n\n")
if(myRelation.isReflexive()):
print("\t Reflexive")
if(myRelation.isSymmetric()):
print("\t Symmetric")
if (myRelation.isAntisymmetric()):
print("\t Antisymmetric")
if (myRelation.isTransitive()):
print("\t Transitive")
print("------------------------------------------------------")