Advertisement
mate2code

Predicate logic; 3 variables; implications (Python)

Jan 19th, 2015
625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.83 KB | None | 0 0
  1. # This scripts was used to find the edges in the following Hasse diagram:
  2. # https://commons.wikimedia.org/wiki/File:Predicate_logic;_3_variables;_implications.png
  3.  
  4. import numpy as np
  5.  
  6. # The "octal" representation is the base of the calculation.
  7. # The octal notation is motivated by the similarity of the following Hasse diagram with the cube:
  8. # https://commons.wikimedia.org/wiki/File:Predicate_logic;_2_variables;_implications;_pairs.svg
  9. octal = [
  10.     '777',
  11.     '733', '376', '667',
  12.     '731', '713', '374', '176', '647', '467',
  13.     '711', '174', '447',
  14.     '330', '603', '066',
  15.     '310', '130', '601', '403', '064', '046',
  16.     '110', '401', '044',
  17.     '000',
  18.     '577', '533', '266', '511', '244', '200',
  19.     '757', '356', '623', '154', '421', '020',
  20.     '775', '665', '332', '445', '112', '002',
  21.     '555', '222'
  22. ]
  23.  
  24. # The abbreviations are just used in the output.
  25. abbr = [
  26.     'e123',
  27.     'a3e12', 'a2e13', 'a1e23',
  28.     'e2a3e1', 'e1a3e2', 'e3a2e1', 'e1a2e3', 'e3a1e2', 'e2a1e3',
  29.     'e12a3', 'e13a2', 'e23a1',
  30.     'a23e1', 'a13e2', 'a12e3',
  31.     'a2e1a3', 'a3e1a2', 'a1e2a3', 'a3e2a1', 'a1e3a2', 'a2e3a1',
  32.     'e1a23', 'e2a13', 'e3a12',
  33.     'a123',
  34.     'e(12)3', 'a3e(12)', 'a(12)e3', 'e(12)a3', 'e3a(12)', 'a(12)3',
  35.     'e(13)2', 'a2e(13)', 'a(13)e2', 'e(13)a2', 'e2a(13)', 'a(13)2',
  36.     'e1(23)', 'a1e(23)', 'a(23)e1', 'e(23)a1', 'e1a(23)', 'a1(23)',
  37.     'e(123)', 'a(123)'
  38. ]
  39.  
  40. length = len(octal)
  41.  
  42. # This function checks if v1 implies v2.
  43. def follows(v1, v2):
  44.     def digit(d1, d2):
  45.         d1 = int(d1)
  46.         d2 = int(d2)
  47.         if (d1 & d2 == d1) or (d1 == 2 and d2 == 5):  # bitwise smaller or central arrow
  48.             return True
  49.         else:
  50.             return False
  51.  
  52.     result = True
  53.     for i in range(3):
  54.         if not digit(v1[i], v2[i]):
  55.             result = False
  56.  
  57.     return result
  58.  
  59. hasse = np.zeros((length, length), np.int8)
  60.  
  61. for (i, v1) in enumerate(octal):
  62.     for (j, v2) in enumerate(octal):
  63.         hasse[i, j] = follows(v1, v2)
  64.  
  65. for i in range(length):
  66.     hasse[i, i] = 0
  67.  
  68. # A Hasse diagram is supposed to contain no redundant edges,
  69. # so if i implies k and k implies j, there shall be no edge from i to j.
  70. def clean(mat):
  71.     for i in range(length):
  72.         for j in range(length):
  73.             if mat[i, j] == 1:
  74.                 for k in range(length):
  75.                     if hasse[i, k] == 1 and hasse[k, j] == 1:
  76.                         mat[i, j] = 0
  77.     return mat
  78.  
  79. hasse = clean(hasse)
  80.  
  81. if np.array_equal(hasse, clean(hasse)):
  82.     print 'cleaning successful'
  83.  
  84. # Print the number of edges in the Hasse diagram (139).
  85. print hasse.sum()
  86.  
  87. # Print all edges of the Hasse diagram.
  88. for i in range(length):
  89.     for j in range(length):
  90.         if hasse[i, j] == 1:
  91.             print abbr[i] + ' --> ' + abbr[j]
  92.  
  93. # The output can the found here: http://pastebin.com/ADwKEkgx
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement