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.

class sage.logic.logictable.Truthtable(t, vo)#

Bases: object

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

get_table_list()#

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