| 795 | | # def simplify(self): |
| 796 | | # r""" |
| 797 | | # This function uses the propcalc package to simplify an expression to |
| 798 | | # its minimal form. |
| 799 | | # |
| 800 | | # INPUT: |
| 801 | | # self -- the calling object. |
| 802 | | # |
| 803 | | # OUTPUT: |
| 804 | | # A simplified expression. |
| 805 | | # |
| 806 | | # EXAMPLES: |
| 807 | | # sage: import sage.logic.propcalc as propcalc |
| 808 | | # sage: f = propcalc.formula("a&((b|c)^a->c)<->b") |
| 809 | | # sage: f.truthtable() |
| 810 | | # a b c value |
| 811 | | # False False False True |
| 812 | | # False False True True |
| 813 | | # False True False False |
| 814 | | # False True True False |
| 815 | | # True False False True |
| 816 | | # True False True False |
| 817 | | # True True False True |
| 818 | | # True True True True |
| 819 | | # sage: f.simplify() |
| 820 | | # (~a&~b)|(a&~b&~c)|(a&b) |
| 821 | | # sage: f.truthtable() |
| 822 | | # a b c value |
| 823 | | # False False False True |
| 824 | | # False False True True |
| 825 | | # False True False False |
| 826 | | # False True True False |
| 827 | | # True False False True |
| 828 | | # True False True False |
| 829 | | # True True False True |
| 830 | | # True True True True |
| 831 | | # |
| 832 | | # NOTES: |
| 833 | | # If the instance of boolean formula has not been converted to |
| 834 | | # cnf form by a call to convert_cnf() or convert_cnf_recur() |
| 835 | | # satformat() will call convert_cnf(). Please see the notes for |
| 836 | | # convert_cnf() and convert_cnf_recur() for performance issues. |
| 837 | | # """ |
| 838 | | # exp = '' |
| 839 | | # self.__tree = logicparser.apply_func(self.__tree, self.reduce_op) |
| 840 | | # plf = logicparser.apply_func(self.__tree, self.convert_opt) |
| 841 | | # wff = boolopt.PLFtoWFF()(plf) # convert to positive-normal form |
| 842 | | # wtd = boolopt.WFFtoDNF() |
| 843 | | # dnf = wtd(wff) |
| 844 | | # dnf = wtd.clean(dnf) |
| 845 | | # if(dnf == [] or dnf == [[]]): |
| 846 | | # exp = self.__vars_order[0] + '&~' + self.__vars_order[0] + ' ' |
| 847 | | # opt = boolopt.optimize(dnf) |
| 848 | | # if(exp == '' and (opt == [] or opt == [[]])): |
| 849 | | # exp = self.__vars_order[0] + '|~' + self.__vars_order[0] + ' ' |
| 850 | | # if(exp == ''): |
| 851 | | # for con in opt: |
| 852 | | # s = '(' |
| 853 | | # for prop in con: |
| 854 | | # if(prop[0] == 'notprop'): |
| 855 | | # s += '~' |
| 856 | | # s += prop[1] + '&' |
| 857 | | # exp += s[:-1] + ')|' |
| 858 | | # self.__expression = exp[:-1] |
| 859 | | # self.__tree, self.__vars_order = logicparser.parse(self.__expression) |
| 860 | | # return BooleanFormula(self.__expression, self.__tree, self.__vars_order) |
| | 821 | def simplify(self): |
| | 822 | r""" |
| | 823 | This function uses the propcalc package to simplify an expression to |
| | 824 | its minimal form. |
| | 825 | |
| | 826 | INPUT: |
| | 827 | self -- the calling object. |
| | 828 | |
| | 829 | OUTPUT: |
| | 830 | A simplified expression. |
| | 831 | |
| | 832 | EXAMPLES: |
| | 833 | sage: import sage.logic.propcalc as propcalc |
| | 834 | sage: f = propcalc.formula("a&((b|c)^a->c)<->b") |
| | 835 | sage: f.truthtable() |
| | 836 | a b c value |
| | 837 | False False False True |
| | 838 | False False True True |
| | 839 | False True False False |
| | 840 | False True True False |
| | 841 | True False False True |
| | 842 | True False True False |
| | 843 | True True False True |
| | 844 | True True True True |
| | 845 | sage: f.simplify() |
| | 846 | (~a&~b)|(a&~b&~c)|(a&b) |
| | 847 | sage: f.truthtable() |
| | 848 | a b c value |
| | 849 | False False False True |
| | 850 | False False True True |
| | 851 | False True False False |
| | 852 | False True True False |
| | 853 | True False False True |
| | 854 | True False True False |
| | 855 | True True False True |
| | 856 | True True True True |
| | 857 | |
| | 858 | NOTES: |
| | 859 | If the instance of boolean formula has not been converted to |
| | 860 | cnf form by a call to convert_cnf() or convert_cnf_recur() |
| | 861 | satformat() will call convert_cnf(). Please see the notes for |
| | 862 | convert_cnf() and convert_cnf_recur() for performance issues. |
| | 863 | """ |
| | 864 | exp = '' |
| | 865 | self.__tree = logicparser.apply_func(self.__tree, self.reduce_op) |
| | 866 | plf = logicparser.apply_func(self.__tree, self.convert_opt) |
| | 867 | wff = boolopt.PLFtoWFF()(plf) # convert to positive-normal form |
| | 868 | wtd = boolopt.WFFtoDNF() |
| | 869 | dnf = wtd(wff) |
| | 870 | dnf = wtd.clean(dnf) |
| | 871 | if(dnf == [] or dnf == [[]]): |
| | 872 | exp = self.__vars_order[0] + '&~' + self.__vars_order[0] + ' ' |
| | 873 | opt = boolopt.optimize(dnf) |
| | 874 | if(exp == '' and (opt == [] or opt == [[]])): |
| | 875 | exp = self.__vars_order[0] + '|~' + self.__vars_order[0] + ' ' |
| | 876 | if(exp == ''): |
| | 877 | for con in opt: |
| | 878 | s = '(' |
| | 879 | for prop in con: |
| | 880 | if(prop[0] == 'notprop'): |
| | 881 | s += '~' |
| | 882 | s += prop[1] + '&' |
| | 883 | exp += s[:-1] + ')|' |
| | 884 | self.__expression = exp[:-1] |
| | 885 | self.__tree, self.__vars_order = logicparser.parse(self.__expression) |
| | 886 | return BooleanFormula(self.__expression, self.__tree, self.__vars_order) |