15
15
16
16
- Paul Scurek (2013-08-03): updated docstring formatting
17
17
"""
18
- #* ****************************************************************************
18
+ # ****************************************************************************
19
19
# Copyright (C) 2007 Chris Gorecki <[email protected] >
20
20
# Copyright (C) 2007 William Stein <[email protected] >
21
21
# Copyright (C) 2013 Paul Scurek <[email protected] >
22
22
#
23
23
# Distributed under the terms of the GNU General Public License (GPL)
24
24
# as published by the Free Software Foundation; either version 2 of
25
25
# the License, or (at your option) any later version.
26
- # http ://www.gnu.org/licenses/
27
- #* ****************************************************************************
26
+ # https ://www.gnu.org/licenses/
27
+ # ****************************************************************************
28
28
29
29
import string
30
30
36
36
vars = {}
37
37
vars_order = []
38
38
39
+
39
40
class SymbolicLogic :
40
41
"""
41
42
EXAMPLES:
@@ -184,7 +185,7 @@ def truthtable(self, statement, start=0, end=-1):
184
185
end = 2 ** len (vars )
185
186
table = [statement ]
186
187
keys = vars_order
187
- for i in range (start ,end ):
188
+ for i in range (start , end ):
188
189
j = 0
189
190
row = []
190
191
for key in reversed (keys ):
@@ -270,10 +271,7 @@ def print_table(self, table):
270
271
line = s = ""
271
272
i = 0
272
273
for e in row :
273
- if e == 'True' :
274
- j = 2
275
- else :
276
- j = 1
274
+ j = 2 if e == 'True' else 1
277
275
s = e + ' ' * j
278
276
if i < len (vars_len ):
279
277
while len (s ) <= vars_len [i ]:
@@ -370,6 +368,7 @@ def prove(self, statement):
370
368
"""
371
369
raise NotImplementedError
372
370
371
+
373
372
def get_bit (x , c ):
374
373
r"""
375
374
Determine if bit ``c`` of the number ``x`` is 1.
@@ -402,10 +401,7 @@ def get_bit(x, c):
402
401
"""
403
402
bits = []
404
403
while x > 0 :
405
- if x % 2 == 0 :
406
- b = 'False'
407
- else :
408
- b = 'True'
404
+ b = 'False' if x % 2 == 0 else 'True'
409
405
x = x // 2
410
406
bits .append (b )
411
407
if c > len (bits ) - 1 :
@@ -456,6 +452,7 @@ def eval(toks):
456
452
raise RuntimeError
457
453
return stack [0 ]
458
454
455
+
459
456
def eval_ltor_toks (lrtoks ):
460
457
r"""
461
458
Evaluates the expression contained in ``lrtoks``.
@@ -494,6 +491,7 @@ def eval_ltor_toks(lrtoks):
494
491
raise RuntimeError
495
492
return lrtoks [0 ]
496
493
494
+
497
495
def reduce_bins (lrtoks ):
498
496
r"""
499
497
Evaluate ``lrtoks`` to a single boolean value.
@@ -531,6 +529,7 @@ def reduce_bins(lrtoks):
531
529
reduce_bins (lrtoks )
532
530
i += 1
533
531
532
+
534
533
def reduce_monos (lrtoks ):
535
534
r"""
536
535
Replace monotonic operator/variable pairs with a boolean value.
@@ -566,6 +565,7 @@ def reduce_monos(lrtoks):
566
565
del lrtoks [i + 1 ]
567
566
i += 1
568
567
568
+
569
569
def eval_mon_op (args ):
570
570
r"""
571
571
Return a boolean value based on the truth table of the operator
@@ -592,21 +592,17 @@ def eval_mon_op(args):
592
592
593
593
sage: log = SymbolicLogic()
594
594
sage: s = log.statement("!(a&b)|!a"); s
595
- [['OPAREN', 'NOT', 'OPAREN', 'a', 'AND', 'b', 'CPAREN', 'OR', 'NOT', 'a', 'CPAREN'],
595
+ [['OPAREN', 'NOT', 'OPAREN', 'a', 'AND', 'b', 'CPAREN', 'OR',
596
+ 'NOT', 'a', 'CPAREN'],
596
597
{'a': 'False', 'b': 'False'},
597
598
['a', 'b']]
598
599
sage: sage.logic.logic.eval_mon_op(['NOT', 'a'])
599
600
'True'
600
601
"""
601
- if args [1 ] != 'True' and args [1 ] != 'False' :
602
- val = vars [args [1 ]]
603
- else :
604
- val = args [1 ]
602
+ val = vars [args [1 ]] if args [1 ] != 'True' and args [1 ] != 'False' else args [1 ]
603
+
604
+ return 'False' if val == 'True' else 'True'
605
605
606
- if val == 'True' :
607
- return 'False'
608
- else :
609
- return 'True'
610
606
611
607
def eval_bin_op (args ):
612
608
r"""
@@ -660,6 +656,7 @@ def eval_bin_op(args):
660
656
elif args [1 ] == 'IFF' :
661
657
return eval_iff_op (lval , rval )
662
658
659
+
663
660
def eval_and_op (lval , rval ):
664
661
r"""
665
662
Apply the 'and' operator to ``lval`` and ``rval``.
@@ -691,14 +688,8 @@ def eval_and_op(lval, rval):
691
688
sage: sage.logic.logic.eval_and_op('True', 'True')
692
689
'True'
693
690
"""
694
- if lval == 'False' and rval == 'False' :
695
- return 'False'
696
- elif lval == 'False' and rval == 'True' :
697
- return 'False'
698
- elif lval == 'True' and rval == 'False' :
699
- return 'False'
700
- elif lval == 'True' and rval == 'True' :
701
- return 'True'
691
+ return 'True' if (lval == 'True' == rval ) else 'False'
692
+
702
693
703
694
def eval_or_op (lval , rval ):
704
695
r"""
@@ -731,14 +722,8 @@ def eval_or_op(lval, rval):
731
722
sage: sage.logic.logic.eval_or_op('True', 'True')
732
723
'True'
733
724
"""
734
- if lval == 'False' and rval == 'False' :
735
- return 'False'
736
- elif lval == 'False' and rval == 'True' :
737
- return 'True'
738
- elif lval == 'True' and rval == 'False' :
739
- return 'True'
740
- elif lval == 'True' and rval == 'True' :
741
- return 'True'
725
+ return 'True' if (lval == 'True' or rval == 'True' ) else 'False'
726
+
742
727
743
728
def eval_ifthen_op (lval , rval ):
744
729
r"""
@@ -772,14 +757,8 @@ def eval_ifthen_op(lval, rval):
772
757
sage: sage.logic.logic.eval_ifthen_op('True', 'True')
773
758
'True'
774
759
"""
775
- if lval == 'False' and rval == 'False' :
776
- return 'True'
777
- elif lval == 'False' and rval == 'True' :
778
- return 'True'
779
- elif lval == 'True' and rval == 'False' :
780
- return 'False'
781
- elif lval == 'True' and rval == 'True' :
782
- return 'True'
760
+ return 'False' if (lval == 'True' and rval == 'False' ) else 'True'
761
+
783
762
784
763
def eval_iff_op (lval , rval ):
785
764
r"""
@@ -813,14 +792,8 @@ def eval_iff_op(lval, rval):
813
792
sage: sage.logic.logic.eval_iff_op('True', 'True')
814
793
'True'
815
794
"""
816
- if lval == 'False' and rval == 'False' :
817
- return 'True'
818
- elif lval == 'False' and rval == 'True' :
819
- return 'False'
820
- elif lval == 'True' and rval == 'False' :
821
- return 'False'
822
- elif lval == 'True' and rval == 'True' :
823
- return 'True'
795
+ return 'True' if (lval == rval ) else 'False'
796
+
824
797
825
798
def tokenize (s , toks ):
826
799
r"""
@@ -887,7 +860,8 @@ def tokenize(s, toks):
887
860
if tok [0 ] not in string .ascii_letters :
888
861
valid = 0
889
862
for c in tok :
890
- if c not in string .ascii_letters and c not in string .digits and c != '_' :
863
+ if not (c in string .ascii_letters
864
+ or c in string .digits or c == '_' ):
891
865
valid = 0
892
866
893
867
if valid == 1 :
0 commit comments