Example #1
0
    def truthtable(self, start=0, end=-1):
        r"""
        This function returns a truthtable object corresponding to the given 
        statement.  Each row of the table corresponds to a binary number, with 
        each variable associated to a column of the number, and taking on 
        a true value if that column has a value of 1.  Please see the 
        logictable module for details.  The function returns a table that 
        start inclusive and end exclusive so truthtable(0, 2) will include
        row 0, but not row 2.     

        INPUT:
            self -- the calling object.
            start -- an integer representing the row of the truth 
                     table from which to start initialized to 0, which 
                     is the first row when all the variables are 
                     false.
            end -- an integer representing the last row of the truthtable 
                   to be created.  It is initialized to the last row of the 
                   full table.
     
        OUTPUT:
            Returns the truthtable (a 2-D array with the creating statement
            tacked on the front) corresponding to the statement.
    
        EXAMPLES:
        This example illustrates the creation of a statement.
            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
        
        We can now create truthtable of rows 1 to 4, inclusive
            sage: s.truthtable(1, 5)
            a      b      c      value
            False  False  True   False
            False  True   False  True
            False  True   True   False
            True   False  False  False
        
        There should be no errors.

        NOTES:
            When sent with no start or end parameters, this is an 
            exponential time function requiring O(2**n) time, where
            n is the number of variables in the expression.        
        """
        max = 2**len(self.__vars_order)
        if (end < 0):
            end = max
        if (end > max):
            end = max
        if (start < 0):
            start = 0
        if (start > max):
            start = max
        keys, table = [], []
        vars = {}
        for var in self.__vars_order:
            vars[var] = False
            keys.insert(0, var)
        keys = list(keys)
        for i in range(start, end):
            j = 0
            row = []
            for key in keys:
                bit = self.get_bit(i, j)
                vars[key] = bit
                j += 1
                row.insert(0, bit)
            row.append(booleval.eval_formula(self.__tree, vars))
            table.append(row)
        keys.reverse()
        table = logictable.Truthtable(table, keys)
        return table
Example #2
0
    def truthtable(self, start=0, end=-1):
        r"""
        Return a truth table for the calling formula.

        INPUT:

        - ``self`` -- calling object

        - ``start`` -- (default: 0) an integer. This is the first
          row of the truth table to be created.

        - ``end`` -- (default: -1) an integer. This is the laste
          row of the truth table to be created.

        OUTPUT:

        The truth table as a 2-D array

        EXAMPLES:

        This example illustrates the creation of a truth 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

        We can now create a truthtable of rows 1 to 4, inclusive.

        ::

            sage: s.truthtable(1, 5)
            a      b      c      value
            False  False  True   False
            False  True   False  True
            False  True   True   False
            True   False  False  False

        .. NOTE::

            Each row of the table corresponds to a binary number, with
            each variable associated to a column of the number, and taking on
            a true value if that column has a value of 1.  Please see the
            logictable module for details.  The function returns a table that
            start inclusive and end exclusive so truthtable(0, 2) will include
            row 0, but not row 2.

            When sent with no start or end parameters, this is an
            exponential time function requiring O(2**n) time, where
            n is the number of variables in the expression.
        """
        max = 2**len(self.__vars_order)
        if end < 0:
            end = max
        if end > max:
            end = max
        if start < 0:
            start = 0
        if start > max:
            start = max
        keys, table = [], []
        vars = {}
        for var in self.__vars_order:
            vars[var] = False
            keys.insert(0, var)
        keys = list(keys)
        for i in range(start, end):
            j = 0
            row = []
            for key in keys:
                bit = self.get_bit(i, j)
                vars[key] = bit
                j += 1
                row.insert(0, bit)
            row.append(booleval.eval_formula(self.__tree, vars))
            table.append(row)
        keys.reverse()
        table = logictable.Truthtable(table, keys)
        return table