Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/logic/logictable.py
4056 views
1
r"""
2
Module designed for the creation and printing of truth tables that are
3
associated with a logical statement.
4
5
A logic table is essentially a 2-D array that is created by the statement class
6
and stored in the private global variable table, along with a list containing
7
the variable names to be used, in order.
8
9
The order in which the table is listed essentially amounts to counting in binary.
10
For instance, with the variables A, B, and C the truth table looks like:
11
12
A B C value
13
False False False ?
14
False False True ?
15
False True False ?
16
False True True ?
17
True False False ?
18
True False True ?
19
True True False ?
20
True True True ?
21
22
This is equivalent to counting in binary, where a table would appear thus;
23
24
2^2 2^1 2^0 value
25
0 0 0 0
26
0 0 1 1
27
0 1 0 2
28
0 1 1 3
29
1 0 0 4
30
1 0 1 5
31
1 1 0 6
32
1 1 1 7
33
34
Given that a table can be created corresponding to any range of acceptable
35
values for a given statement, it is easy to find the value of a statement
36
for arbitrary values of its variables.
37
38
EXAMPLES:
39
sage: import sage.logic.propcalc as propcalc
40
sage: s = propcalc.formula("a&b|~(c|a)")
41
sage: s.truthtable()
42
a b c value
43
False False False True
44
False False True False
45
False True False True
46
False True True False
47
True False False False
48
True False True False
49
True True False True
50
True True True True
51
52
sage: latex(s.truthtable(5,11))
53
\\\begin{tabular}{llll}c & b & a & value \\\hline True & False & True & False \\True & True & False & True \\True & True & True & True\end{tabular}
54
55
It is not an error to use nonsensical numeric inputs.
56
sage: s = propcalc.formula("a&b|~(c|a)")
57
sage: s.truthtable(5, 9)
58
a b c value
59
True False True False
60
True True False True
61
True True True True
62
63
sage: s.truthtable(9, 5)
64
a b c value
65
66
If one argument is provided, truthtable defaults to the end.
67
sage: s.truthtable(-1)
68
a b c value
69
False False False True
70
False False True False
71
False True False True
72
False True True False
73
True False False False
74
True False True False
75
True True False True
76
True True True True
77
78
If the second argument is negative, truthtable defaults to the end.
79
sage: s.truthtable(4, -2)
80
a b c value
81
True False False False
82
True False True False
83
True True False True
84
True True True True
85
86
NOTES:
87
For statements that contain a variable list that when printed is longer than
88
the \latex page, the columns of the table will run off the screen.
89
"""
90
#*************************************************************************************
91
# Copyright (C) 2006 William Stein <[email protected]>
92
# Copyright (C) 2006 Chris Gorecki <[email protected]>
93
#
94
# Distributed under the terms of the GNU General Public License (GPL)
95
# http://www.gnu.org/licenses/
96
#*************************************************************************************
97
98
#Global variables
99
__table = []
100
__vars_order = []
101
102
class Truthtable:
103
104
def __init__(self, t, vo):
105
r"""
106
This function initializes the data fields and is called when a
107
new table is created.
108
109
INPUT:
110
self -- the calling object.
111
t -- a 2-D array containing the table values
112
vo -- a list of the variables in the expression in order,
113
with each variable occurring only once.
114
115
OUTPUT:
116
Effectively returns an instance of this class.
117
118
EXAMPLES:
119
This example illustrates the creation of a table.
120
sage: import sage.logic.propcalc as propcalc
121
sage: s = propcalc.formula("a&b|~(c|a)")
122
sage: s.truthtable()
123
a b c value
124
False False False True
125
False False True False
126
False True False True
127
False True True False
128
True False False False
129
True False True False
130
True True False True
131
True True True True
132
133
There should be no errors.
134
"""
135
self.__table = t
136
self.__vars_order = vo
137
138
def _latex_(self):
139
r"""
140
Returns a string representation of the calling table object.
141
142
INPUT:
143
self -- the calling object.
144
145
OUTPUT:
146
Returns the \latex representation of this table.
147
148
EXAMPLES:
149
sage: import sage.logic.propcalc as propcalc
150
sage: s = propcalc.formula("man->monkey&human")
151
sage: latex(s.truthtable())
152
\\\begin{tabular}{llll}human & monkey & man & value \\\hline False & False & False & True \\False & False & True & True \\False & True & False & True \\False & True & True & True \\True & False & False & False \\True & False & True & False \\True & True & False & False \\True & True & True & True\end{tabular}
153
154
Strange parameters can lead to a table header with no body.
155
sage: latex(s.truthtable(2, 1))
156
\\\begin{tabular}{llll}human & monkey & man & value \\\hli\end{tabular}
157
"""
158
vars_len = []
159
rt = s = ""
160
self.__vars_order.reverse()
161
s = r'\\\begin{tabular}{'
162
s += 'l' * (len(self.__vars_order) + 1) + '}'
163
for var in self.__vars_order:
164
s += var + ' & '
165
rt += s + r'value \\' + r'\hline '
166
for row in self.__table:
167
s = ""
168
for i in row:
169
s += str(i) + ' & '
170
rt += s[:-3] + r' \\'
171
rt = rt[:-3] + r'\end{tabular}'
172
self.__vars_order.reverse()
173
return rt
174
175
def __repr__(self):
176
r"""
177
This function returns a string representation of the calling table object.
178
179
INPUT:
180
self -- the calling object: not used.
181
182
OUTPUT:
183
Returns a string representation of this table.
184
185
EXAMPLES:
186
sage: import sage.logic.propcalc as propcalc
187
sage: s = propcalc.formula("man->monkey&human")
188
sage: s.truthtable()
189
man monkey human value
190
False False False True
191
False False True True
192
False True False True
193
False True True True
194
True False False False
195
True False True False
196
True True False False
197
True True True True
198
199
Strange parameters can lead to the table header with no body.
200
sage: s.truthtable(2, 1)
201
man monkey human value
202
203
There should be no errors.
204
"""
205
vars_len = []
206
line = rt = s = ""
207
for var in self.__vars_order:
208
vars_len.append(len(var))
209
s = var + ' '
210
while(len(s) < len('False ')):
211
s += ' '
212
s += ' '
213
line += s
214
rt += line + 'value\n'
215
for row in self.__table:
216
line = s = ""
217
i = 0
218
for e in row:
219
if e == True:
220
j = 2
221
else:
222
j = 1
223
s = str(e) + ' ' * j
224
if(i < len(vars_len)):
225
while(len(s) <= vars_len[i]):
226
s += ' '
227
s += ' '
228
line += s
229
i += 1
230
rt += line + '\n'
231
return rt
232
233
def get_table_list(self):
234
r"""
235
This function returns a string representation of the calling table object.
236
237
INPUT:
238
self -- the calling object: not used.
239
240
OUTPUT:
241
Returns the list representation of this table.
242
243
EXAMPLES:
244
sage: import sage.logic.propcalc as propcalc
245
sage: s = propcalc.formula("man->monkey&human")
246
sage: s.truthtable().get_table_list()
247
[['man', 'monkey', 'human'], [False, False, False, True], [False, False, True, True], [False, True, False, True], [False, True, True, True], [True, False, False, False], [True, False, True, False], [True, True, False, False], [True, True, True, True]]
248
249
"""
250
t = self.__table[:]
251
t.insert(0, self.__vars_order)
252
return t
253
254