Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

r""" 

Logic Tables 

 

A logic table is essentially a 2-D array that is created by the statement class 

and stored in the private global variable table, along with a list containing 

the variable names to be used, in order. 

 

The order in which the table is listed essentially amounts to counting in 

binary. For instance, with the variables `A`, `B`, and `C` the truth table 

looks like:: 

 

A B C value 

False False False ? 

False False True ? 

False True False ? 

False True True ? 

True False False ? 

True False True ? 

True True False ? 

True True True ? 

 

This is equivalent to counting in binary, where a table would appear thus; 

 

:: 

 

2^2 2^1 2^0 value 

0 0 0 0 

0 0 1 1 

0 1 0 2 

0 1 1 3 

1 0 0 4 

1 0 1 5 

1 1 0 6 

1 1 1 7 

 

Given that a table can be created corresponding to any range of acceptable 

values for a given statement, it is easy to find the value of a statement 

for arbitrary values of its variables. 

 

AUTHORS: 

 

- William Stein (2006): initial version 

 

- Chris Gorecki (2006): initial version 

 

- Paul Scurek (2013-08-03): updated docstring formatting 

 

EXAMPLES: 

 

Create a truth table of a boolean formula:: 

 

sage: import sage.logic.propcalc as propcalc 

sage: s = propcalc.formula("a&b|~(c|a)") 

sage: s.truthtable() 

a b c value 

False False False True 

False False True False 

False True False True 

False True True False 

True False False False 

True False True False 

True True False True 

True True True True 

 

Get the letex code for a truth table:: 

 

sage: latex(s.truthtable(5,11)) 

\\\begin{tabular}{llll}c & b & a & value \\\hline True & False & True & False \\True & True & False & True \\True & True & True & True\end{tabular} 

 

It is not an error to use nonsensical numeric inputs:: 

 

sage: s = propcalc.formula("a&b|~(c|a)") 

sage: s.truthtable(5, 9) 

a b c value 

True False True False 

True True False True 

True True True True 

 

sage: s.truthtable(9, 5) 

a b c value 

 

If one argument is provided, truthtable defaults to the end:: 

 

sage: s.truthtable(-1) 

a b c value 

False False False True 

False False True False 

False True False True 

False True True False 

True False False False 

True False True False 

True True False True 

True True True True 

 

If the second argument is negative, truthtable defaults to the end:: 

 

sage: s.truthtable(4, -2) 

a b c value 

True False False False 

True False True False 

True True False True 

True True True True 

 

.. NOTE:: 

 

For statements that contain a variable list that when printed is longer 

than the latex page, the columns of the table will run off the screen. 

""" 

#***************************************************************************** 

# Copyright (C) 2006 William Stein <wstein@gmail.com> 

# Copyright (C) 2006 Chris Gorecki <chris.k.gorecki@gmail.com> 

# Copyright (C) 2013 Paul Scurek <scurek86@gmail.com> 

# 

# Distributed under the terms of the GNU General Public License (GPL) 

# as published by the Free Software Foundation; either version 2 of 

# the License, or (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

 

# Global variables 

__table = [] 

__vars_order = [] 

 

class Truthtable: 

""" 

A truth table. 

 

INPUT: 

 

- ``t`` -- a 2-D array containing the table values 

 

- ``vo`` -- a list of the variables in the expression in order, 

with each variable occurring only once 

""" 

def __init__(self, t, vo): 

r""" 

Initialize the data fields. 

 

EXAMPLES: 

 

This example illustrates the creation of a table 

 

:: 

 

sage: import sage.logic.propcalc as propcalc 

sage: s = propcalc.formula("a&b|~(c|a)") 

sage: s.truthtable() 

a b c value 

False False False True 

False False True False 

False True False True 

False True True False 

True False False False 

True False True False 

True True False True 

True True True True 

""" 

self.__table = t 

self.__vars_order = vo 

 

def _latex_(self): 

r""" 

Return a latex representation of the calling table object. 

 

OUTPUT: 

 

The latex representation of the table. 

 

EXAMPLES: 

 

This example illustrates how to get the latex representation of 

the table:: 

 

sage: import sage.logic.propcalc as propcalc 

sage: s = propcalc.formula("man->monkey&human") 

sage: latex(s.truthtable()) 

\\\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} 

 

Now, we show that strange parameters can lead to a table header 

with no body:: 

 

sage: latex(s.truthtable(2, 1)) 

\\\begin{tabular}{llll}human & monkey & man & value \\\hli\end{tabular} 

""" 

rt = s = "" 

self.__vars_order.reverse() 

s = r'\\\begin{tabular}{' 

s += 'l' * (len(self.__vars_order) + 1) + '}' 

for var in self.__vars_order: 

s += var + ' & ' 

rt += s + r'value \\' + r'\hline ' 

for row in self.__table: 

s = "" 

for i in row: 

s += str(i) + ' & ' 

rt += s[:-3] + r' \\' 

rt = rt[:-3] + r'\end{tabular}' 

self.__vars_order.reverse() 

return rt 

 

def __repr__(self): 

r""" 

Return a string representation of the calling table object. 

 

OUTPUT: 

 

The table as a 2-D string array. 

 

EXAMPLES: 

 

This example illustrates how to display the truth table of a 

boolean formula:: 

 

sage: import sage.logic.propcalc as propcalc 

sage: s = propcalc.formula("man->monkey&human") 

sage: s.truthtable() 

man monkey human value 

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 

 

We now show that strange parameters can lead to the table 

header with no body:: 

 

sage: s.truthtable(2, 1) 

man monkey human value 

""" 

vars_len = [] 

line = rt = s = "" 

for var in self.__vars_order: 

vars_len.append(len(var)) 

s = var + ' ' 

while len(s) < len('False '): 

s += ' ' 

s += ' ' 

line += s 

rt += line + 'value\n' 

for row in self.__table: 

line = s = "" 

i = 0 

for e in row: 

if e: 

j = 2 

else: 

j = 1 

s = str(e) + ' ' * j 

if i < len(vars_len): 

while len(s) <= vars_len[i]: 

s += ' ' 

s += ' ' 

line += s 

i += 1 

rt += line + '\n' 

return rt 

 

def get_table_list(self): 

r""" 

Return a list representation of the calling table object. 

 

OUTPUT: 

 

A list representation of the table. 

 

EXAMPLES: 

 

This example illustrates how to show the table as a list:: 

 

sage: import sage.logic.propcalc as propcalc 

sage: s = propcalc.formula("man->monkey&human") 

sage: s.truthtable().get_table_list() 

[['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]] 

 

""" 

t = self.__table[:] 

t.insert(0, self.__vars_order) 

return t