-
Notifications
You must be signed in to change notification settings - Fork 96
/
Copy pathtruth_tables.py
79 lines (59 loc) · 2.55 KB
/
truth_tables.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import numpy as np
import pandas as pd
# Helper function to create truth tables
def create_truth_table(propositions, operation):
n = len(propositions)
m = 2 ** n
truth_table = np.zeros((m, n + 1), dtype=int)
for i in range(n):
truth_table[:, i] = (np.arange(m) // (2 ** i)) % 2
truth_table[:, n] = operation(*[truth_table[:, i] for i in range(n)]).astype(int)
return truth_table
# 1. Prove that X ⨁ Y ≅ (X ∧ ∼Y) ∨ (∼X ∧ Y).
X = np.array([True, True, False, False])
Y = np.array([True, False, True, False])
result_XOR = X ^ Y
result_AND_OR = (X & ~Y) | (~X & Y)
are_equivalent = np.array_equal(result_XOR, result_AND_OR)
print("Truth Table for X ⨁ Y:")
print(pd.DataFrame({"X": X, "Y": Y, "X ⨁ Y": result_XOR}))
print("\nTruth Table for (X ∧ ∼Y) ∨ (∼X ∧ Y):")
print(pd.DataFrame({"X": X, "Y": Y, "(X ∧ ∼Y) ∨ (∼X ∧ Y)": result_AND_OR}))
print("\nAre the expressions equivalent?", are_equivalent)
# 2. Show that (p ⨁ q) ∨ (p ↓ q) is equivalent to p ↑ q.
p = np.array([True, True, False, False])
q = np.array([True, False, True, False])
result_XOR_NOR = (p ^ q) | ~(p | q)
result_NAND = ~(p & q)
are_equivalent = np.array_equal(result_XOR_NOR, result_NAND)
print("\nTruth Table for (p ⨁ q) ∨ (p ↓ q):")
print(pd.DataFrame({"p": p, "q": q, "(p ⨁ q) ∨ (p ↓ q)": result_XOR_NOR}))
print("\nTruth Table for p ↑ q:")
print(pd.DataFrame({"p": p, "q": q, "p ↑ q": result_NAND}))
print("\nAre the expressions equivalent?", are_equivalent)
# Answer: Truth Table for X ⨁ Y:
# X Y X ⨁ Y
# 0 True True False
# 1 True False True
# 2 False True True
# 3 False False False
# Truth Table for (X ∧ ∼Y) ∨ (∼X ∧ Y):
# X Y (X ∧ ∼Y) ∨ (∼X ∧ Y)
# 0 True True False
# 1 True False True
# 2 False True True
# 3 False False False
# Are the expressions equivalent? True
# Truth Table for (p ⨁ q) ∨ (p ↓ q):
# p q (p ⨁ q) ∨ (p ↓ q)
# 0 True True False
# 1 True False True
# 2 False True True
# 3 False False True
# Truth Table for p ↑ q:
# p q p ↑ q
# 0 True True False
# 1 True False True
# 2 False True True
# 3 False False True
# Are the expressions equivalent? True