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