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

from sympy.core.logic import _fuzzy_group 

from sympy.logic.boolalg import conjuncts 

from sympy.assumptions import Q, ask 

 

 

class AskHandler(object): 

"""Base class that all Ask Handlers must inherit""" 

pass 

 

 

class CommonHandler(AskHandler): 

"""Defines some useful methods common to most Handlers """ 

 

@staticmethod 

def AlwaysTrue(expr, assumptions): 

return True 

 

@staticmethod 

def AlwaysFalse(expr, assumptions): 

return False 

 

NaN = AlwaysFalse 

 

 

class AskCommutativeHandler(CommonHandler): 

""" 

Handler for key 'commutative' 

""" 

 

@staticmethod 

def Symbol(expr, assumptions): 

"""Objects are expected to be commutative unless otherwise stated""" 

assumps = conjuncts(assumptions) 

if expr.is_commutative is not None: 

return expr.is_commutative and not ~Q.commutative(expr) in assumps 

if Q.commutative(expr) in assumps: 

return True 

elif ~Q.commutative(expr) in assumps: 

return False 

return True 

 

@staticmethod 

def Basic(expr, assumptions): 

for arg in expr.args: 

if not ask(Q.commutative(arg), assumptions): 

return False 

return True 

 

Number, NaN = [staticmethod(CommonHandler.AlwaysTrue)]*2 

 

 

class TautologicalHandler(AskHandler): 

"""Wrapper allowing to query the truth value of a boolean expression.""" 

 

@staticmethod 

def bool(expr, assumptions): 

return expr 

 

BooleanTrue = staticmethod(CommonHandler.AlwaysTrue) 

BooleanFalse = staticmethod(CommonHandler.AlwaysFalse) 

 

@staticmethod 

def AppliedPredicate(expr, assumptions): 

return ask(expr, assumptions) 

 

@staticmethod 

def Not(expr, assumptions): 

value = ask(expr.args[0], assumptions=assumptions) 

if value in (True, False): 

return not value 

else: 

return None 

 

@staticmethod 

def Or(expr, assumptions): 

result = False 

for arg in expr.args: 

p = ask(arg, assumptions=assumptions) 

if p is True: 

return True 

if p is None: 

result = None 

return result 

 

@staticmethod 

def And(expr, assumptions): 

result = True 

for arg in expr.args: 

p = ask(arg, assumptions=assumptions) 

if p is False: 

return False 

if p is None: 

result = None 

return result 

 

@staticmethod 

def Implies(expr, assumptions): 

p, q = expr.args 

return ask(~p | q, assumptions=assumptions) 

 

@staticmethod 

def Equivalent(expr, assumptions): 

p, q = expr.args 

pt = ask(p, assumptions=assumptions) 

if pt is None: 

return None 

qt = ask(q, assumptions=assumptions) 

if qt is None: 

return None 

return pt == qt 

 

 

#### Helper methods 

def test_closed_group(expr, assumptions, key): 

""" 

Test for membership in a group with respect 

to the current operation 

""" 

return _fuzzy_group( 

(ask(key(a), assumptions) for a in expr.args), quick_exit=True)