程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python implementation --- Nanyou Discrete Mathematics Experiment II: Determination of the properties of binary relations on sets

編輯:Python

Catalog

One 、 requirement :

Two 、 The sufficient and necessary condition conclusion proved :

3、 ... and 、 Code


One 、 requirement :

'''
Content : Input set A And in A The binary relationship on R, Judge binary relation R Some basic properties of .
requirement : can Correctly determine the reflexivity of any binary relation 、 symmetry 、 Transitivity 、 Reflexivity and antisymmetry .
'''

Two 、 The sufficient and necessary condition conclusion proved :

Proof of simplified transitivity .

3、 ... and 、 Code

'''
Content : Input set A And in A The binary relationship on R, Judge binary relation R Some basic properties of .
requirement : Can correctly determine the reflexivity of any binary relation 、 symmetry 、 Transitivity 、 Reflexivity and antisymmetry .
'''
import numpy as np
import time as t
''' The functionality : Complete user input and initialize coefficient matrix '''
def init_Info(aArray):
# Let the user enter Preface me , Modify the corresponding relation matrix accordingly
mylist=[]# A special list for storing two tuples
numstr = input(" Please enter a sequence pair ( Numbers , Space separates the middle , example :2 3 4 5 Express <2,3>,<4,5>)):")
plist=numstr.split(" ")# The input data , Cut with a space as a separator , Into the sequence
for i in range(0,len(plist),2):# Every two numbers as a binary , The third parameter is zero step
mylist.append(tuple((plist[i],plist[i+1])))# Be careful , At this time, the two numbers of the two tuples are stored in the form of strings , Remember to convert the format when using
for x in range(len(mylist)):# The initialization relation matrix is 1 The location of
aArray[int(mylist[x][0]),int(mylist[x][1])]=1
''' The functionality : Calculate whether reflexivity is satisfied '''
def reflection(arr):
flag=1
for i in range(n):
if arr[i,i]==0:# Check the diagonal for 0 Can reflect reflexivity
flag=0
break
return flag
''' The functionality : Whether the calculation satisfies anti reflexivity '''
def irreflection(arr):
flag = 1
for i in range(n):
if arr[i, i] == 1:# Check the diagonal for 0 Can reflect reflexivity
flag = 0
break
return flag
''' The functionality : Calculate whether symmetry is satisfied '''
def symmetry(arr):
flag=1
arr_t=arr.T# use arr_t Storage arr The transpose of the matrix
if not((arr_t==arr).all()):# If the transpose matrix is not equal to the original matrix , It just doesn't satisfy the symmetry
flag=0
return flag
''' The functionality : Calculate whether the antisymmetry is satisfied '''
def irsymmetry(arr):
arr_t = arr.T # use arr_t Storage arr The transpose of the matrix
for i in range(1,n):
for j in range(i):
if arr_t[i][j]==arr[i][j] and arr_t[i][j]==1 and arr[i][j]==1 :# Be sure to pay attention to , Different at the same time 1
return False
return True
''' The functionality : Calculate whether transitivity is satisfied '''
def transmission(arr):
b=np.dot(arr,arr)#R²
arr=np.array(arr,dtype=bool)#R (bool Value storage version )
ARR=np.array(b,dtype=bool)# R²=R and R The combination of (bool Storage version )
# Judge R and R² The inclusive relationship of
A=arr+ARR
if (A==arr).all()==0:
# If the two are not equal , return false(0) Just explain R Not including R² Of , From the necessary and sufficient conditions, we can know ,R Does not satisfy transitivity
return 0
else:
return 1
''' The main function '''
if __name__=='__main__':
# User entered n, It means there will be 0-(n-1) The elements of
n = eval(input(" Please enter the set A Number of elements of n:"))
# Open one n*n Array of , The initial value defaults to 0
aArray = np.zeros((n, n))
# Initialization data ( The relational matrix )
init_Info(aArray)
start=t.perf_counter()
# Judge reflexivity and reflexivity
print(" Binary relationship R{} Satisfy reflexivity ".format(''if reflection(aArray) else ' No '))# use python The branching statement of makes the code more concise
print(" Binary relationship R{} Satisfy reflexivity ".format('' if irreflection(aArray) else ' No '))
# Judge symmetry and antisymmetry
print(" Binary relationship R{} Satisfy symmetry ".format('' if symmetry(aArray) else ' No '))
print(" Binary relationship R{} Satisfy antisymmetry ".format('' if irsymmetry(aArray) else ' No '))
# Judge transitivity
print(" Binary relationship R{} Meet transitivity ".format('' if transmission(aArray) else ' No '))
end=t.perf_counter()
print(" After initializing data , Get the binary relation R The time calculated by the relevant properties of :{}".format(end-start))


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved