applied-ai-018 commited on
Commit
5a178ff
·
verified ·
1 Parent(s): 0436c22

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__init__.py +18 -0
  2. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/__init__.cpython-310.pyc +0 -0
  3. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask.cpython-310.pyc +0 -0
  4. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask_generated.cpython-310.pyc +0 -0
  5. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/assume.cpython-310.pyc +0 -0
  6. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/cnf.cpython-310.pyc +0 -0
  7. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/facts.cpython-310.pyc +0 -0
  8. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/refine.cpython-310.pyc +0 -0
  9. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/satask.cpython-310.pyc +0 -0
  10. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/sathandlers.cpython-310.pyc +0 -0
  11. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/wrapper.cpython-310.pyc +0 -0
  12. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/ask_generated.py +264 -0
  13. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/assume.py +485 -0
  14. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/facts.py +220 -0
  15. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__init__.py +13 -0
  16. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/__init__.cpython-310.pyc +0 -0
  17. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/calculus.cpython-310.pyc +0 -0
  18. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/common.cpython-310.pyc +0 -0
  19. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/matrices.cpython-310.pyc +0 -0
  20. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/ntheory.cpython-310.pyc +0 -0
  21. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/order.cpython-310.pyc +0 -0
  22. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/sets.cpython-310.pyc +0 -0
  23. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/calculus.py +258 -0
  24. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/common.py +156 -0
  25. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/matrices.py +716 -0
  26. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/ntheory.py +267 -0
  27. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/order.py +436 -0
  28. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/sets.py +772 -0
  29. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__init__.py +5 -0
  30. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/__init__.cpython-310.pyc +0 -0
  31. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/calculus.cpython-310.pyc +0 -0
  32. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/common.cpython-310.pyc +0 -0
  33. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/matrices.cpython-310.pyc +0 -0
  34. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/ntheory.cpython-310.pyc +0 -0
  35. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/order.cpython-310.pyc +0 -0
  36. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/sets.cpython-310.pyc +0 -0
  37. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/calculus.py +82 -0
  38. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/common.py +81 -0
  39. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/matrices.py +511 -0
  40. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/ntheory.py +126 -0
  41. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/order.py +390 -0
  42. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/sets.py +387 -0
  43. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/refine.py +405 -0
  44. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/__init__.py +13 -0
  45. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/__init__.cpython-310.pyc +0 -0
  46. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/binrel.cpython-310.pyc +0 -0
  47. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/equality.cpython-310.pyc +0 -0
  48. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/binrel.py +212 -0
  49. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/equality.py +302 -0
  50. llmeval-env/lib/python3.10/site-packages/sympy/assumptions/satask.py +361 -0
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__init__.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ A module to implement logical predicates and assumption system.
3
+ """
4
+
5
+ from .assume import (
6
+ AppliedPredicate, Predicate, AssumptionsContext, assuming,
7
+ global_assumptions
8
+ )
9
+ from .ask import Q, ask, register_handler, remove_handler
10
+ from .refine import refine
11
+ from .relation import BinaryRelation, AppliedBinaryRelation
12
+
13
+ __all__ = [
14
+ 'AppliedPredicate', 'Predicate', 'AssumptionsContext', 'assuming',
15
+ 'global_assumptions', 'Q', 'ask', 'register_handler', 'remove_handler',
16
+ 'refine',
17
+ 'BinaryRelation', 'AppliedBinaryRelation'
18
+ ]
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (695 Bytes). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask.cpython-310.pyc ADDED
Binary file (18.4 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask_generated.cpython-310.pyc ADDED
Binary file (7.96 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/assume.cpython-310.pyc ADDED
Binary file (14.5 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/cnf.cpython-310.pyc ADDED
Binary file (16.6 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/facts.cpython-310.pyc ADDED
Binary file (6.11 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/refine.cpython-310.pyc ADDED
Binary file (9.94 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/satask.cpython-310.pyc ADDED
Binary file (10.6 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/sathandlers.cpython-310.pyc ADDED
Binary file (10.3 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/__pycache__/wrapper.cpython-310.pyc ADDED
Binary file (5.42 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/ask_generated.py ADDED
@@ -0,0 +1,264 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Do NOT manually edit this file.
3
+ Instead, run ./bin/ask_update.py.
4
+ """
5
+
6
+ from sympy.assumptions.ask import Q
7
+ from sympy.assumptions.cnf import Literal
8
+ from sympy.core.cache import cacheit
9
+
10
+ @cacheit
11
+ def get_all_known_facts():
12
+ """
13
+ Known facts between unary predicates as CNF clauses.
14
+ """
15
+ return {
16
+ frozenset((Literal(Q.algebraic, False), Literal(Q.imaginary, True), Literal(Q.transcendental, False))),
17
+ frozenset((Literal(Q.algebraic, False), Literal(Q.negative, True), Literal(Q.transcendental, False))),
18
+ frozenset((Literal(Q.algebraic, False), Literal(Q.positive, True), Literal(Q.transcendental, False))),
19
+ frozenset((Literal(Q.algebraic, False), Literal(Q.rational, True))),
20
+ frozenset((Literal(Q.algebraic, False), Literal(Q.transcendental, False), Literal(Q.zero, True))),
21
+ frozenset((Literal(Q.algebraic, True), Literal(Q.finite, False))),
22
+ frozenset((Literal(Q.algebraic, True), Literal(Q.transcendental, True))),
23
+ frozenset((Literal(Q.antihermitian, False), Literal(Q.hermitian, False), Literal(Q.zero, True))),
24
+ frozenset((Literal(Q.antihermitian, False), Literal(Q.imaginary, True))),
25
+ frozenset((Literal(Q.commutative, False), Literal(Q.finite, True))),
26
+ frozenset((Literal(Q.commutative, False), Literal(Q.infinite, True))),
27
+ frozenset((Literal(Q.complex_elements, False), Literal(Q.real_elements, True))),
28
+ frozenset((Literal(Q.composite, False), Literal(Q.even, True), Literal(Q.positive, True), Literal(Q.prime, False))),
29
+ frozenset((Literal(Q.composite, True), Literal(Q.even, False), Literal(Q.odd, False))),
30
+ frozenset((Literal(Q.composite, True), Literal(Q.positive, False))),
31
+ frozenset((Literal(Q.composite, True), Literal(Q.prime, True))),
32
+ frozenset((Literal(Q.diagonal, False), Literal(Q.lower_triangular, True), Literal(Q.upper_triangular, True))),
33
+ frozenset((Literal(Q.diagonal, True), Literal(Q.lower_triangular, False))),
34
+ frozenset((Literal(Q.diagonal, True), Literal(Q.normal, False))),
35
+ frozenset((Literal(Q.diagonal, True), Literal(Q.symmetric, False))),
36
+ frozenset((Literal(Q.diagonal, True), Literal(Q.upper_triangular, False))),
37
+ frozenset((Literal(Q.even, False), Literal(Q.odd, False), Literal(Q.prime, True))),
38
+ frozenset((Literal(Q.even, False), Literal(Q.zero, True))),
39
+ frozenset((Literal(Q.even, True), Literal(Q.odd, True))),
40
+ frozenset((Literal(Q.even, True), Literal(Q.rational, False))),
41
+ frozenset((Literal(Q.finite, False), Literal(Q.transcendental, True))),
42
+ frozenset((Literal(Q.finite, True), Literal(Q.infinite, True))),
43
+ frozenset((Literal(Q.fullrank, False), Literal(Q.invertible, True))),
44
+ frozenset((Literal(Q.fullrank, True), Literal(Q.invertible, False), Literal(Q.square, True))),
45
+ frozenset((Literal(Q.hermitian, False), Literal(Q.negative, True))),
46
+ frozenset((Literal(Q.hermitian, False), Literal(Q.positive, True))),
47
+ frozenset((Literal(Q.hermitian, False), Literal(Q.zero, True))),
48
+ frozenset((Literal(Q.imaginary, True), Literal(Q.negative, True))),
49
+ frozenset((Literal(Q.imaginary, True), Literal(Q.positive, True))),
50
+ frozenset((Literal(Q.imaginary, True), Literal(Q.zero, True))),
51
+ frozenset((Literal(Q.infinite, False), Literal(Q.negative_infinite, True))),
52
+ frozenset((Literal(Q.infinite, False), Literal(Q.positive_infinite, True))),
53
+ frozenset((Literal(Q.integer_elements, True), Literal(Q.real_elements, False))),
54
+ frozenset((Literal(Q.invertible, False), Literal(Q.positive_definite, True))),
55
+ frozenset((Literal(Q.invertible, False), Literal(Q.singular, False))),
56
+ frozenset((Literal(Q.invertible, False), Literal(Q.unitary, True))),
57
+ frozenset((Literal(Q.invertible, True), Literal(Q.singular, True))),
58
+ frozenset((Literal(Q.invertible, True), Literal(Q.square, False))),
59
+ frozenset((Literal(Q.irrational, False), Literal(Q.negative, True), Literal(Q.rational, False))),
60
+ frozenset((Literal(Q.irrational, False), Literal(Q.positive, True), Literal(Q.rational, False))),
61
+ frozenset((Literal(Q.irrational, False), Literal(Q.rational, False), Literal(Q.zero, True))),
62
+ frozenset((Literal(Q.irrational, True), Literal(Q.negative, False), Literal(Q.positive, False), Literal(Q.zero, False))),
63
+ frozenset((Literal(Q.irrational, True), Literal(Q.rational, True))),
64
+ frozenset((Literal(Q.lower_triangular, False), Literal(Q.triangular, True), Literal(Q.upper_triangular, False))),
65
+ frozenset((Literal(Q.lower_triangular, True), Literal(Q.triangular, False))),
66
+ frozenset((Literal(Q.negative, False), Literal(Q.positive, False), Literal(Q.rational, True), Literal(Q.zero, False))),
67
+ frozenset((Literal(Q.negative, True), Literal(Q.negative_infinite, True))),
68
+ frozenset((Literal(Q.negative, True), Literal(Q.positive, True))),
69
+ frozenset((Literal(Q.negative, True), Literal(Q.positive_infinite, True))),
70
+ frozenset((Literal(Q.negative, True), Literal(Q.zero, True))),
71
+ frozenset((Literal(Q.negative_infinite, True), Literal(Q.positive, True))),
72
+ frozenset((Literal(Q.negative_infinite, True), Literal(Q.positive_infinite, True))),
73
+ frozenset((Literal(Q.negative_infinite, True), Literal(Q.zero, True))),
74
+ frozenset((Literal(Q.normal, False), Literal(Q.unitary, True))),
75
+ frozenset((Literal(Q.normal, True), Literal(Q.square, False))),
76
+ frozenset((Literal(Q.odd, True), Literal(Q.rational, False))),
77
+ frozenset((Literal(Q.orthogonal, False), Literal(Q.real_elements, True), Literal(Q.unitary, True))),
78
+ frozenset((Literal(Q.orthogonal, True), Literal(Q.positive_definite, False))),
79
+ frozenset((Literal(Q.orthogonal, True), Literal(Q.unitary, False))),
80
+ frozenset((Literal(Q.positive, False), Literal(Q.prime, True))),
81
+ frozenset((Literal(Q.positive, True), Literal(Q.positive_infinite, True))),
82
+ frozenset((Literal(Q.positive, True), Literal(Q.zero, True))),
83
+ frozenset((Literal(Q.positive_infinite, True), Literal(Q.zero, True))),
84
+ frozenset((Literal(Q.square, False), Literal(Q.symmetric, True))),
85
+ frozenset((Literal(Q.triangular, False), Literal(Q.unit_triangular, True))),
86
+ frozenset((Literal(Q.triangular, False), Literal(Q.upper_triangular, True)))
87
+ }
88
+
89
+ @cacheit
90
+ def get_known_facts_dict():
91
+ """
92
+ Logical relations between unary predicates as dictionary.
93
+
94
+ Each key is a predicate, and item is two groups of predicates.
95
+ First group contains the predicates which are implied by the key, and
96
+ second group contains the predicates which are rejected by the key.
97
+
98
+ """
99
+ return {
100
+ Q.algebraic: (set([Q.algebraic, Q.commutative, Q.complex, Q.finite]),
101
+ set([Q.infinite, Q.negative_infinite, Q.positive_infinite,
102
+ Q.transcendental])),
103
+ Q.antihermitian: (set([Q.antihermitian]), set([])),
104
+ Q.commutative: (set([Q.commutative]), set([])),
105
+ Q.complex: (set([Q.commutative, Q.complex, Q.finite]),
106
+ set([Q.infinite, Q.negative_infinite, Q.positive_infinite])),
107
+ Q.complex_elements: (set([Q.complex_elements]), set([])),
108
+ Q.composite: (set([Q.algebraic, Q.commutative, Q.complex, Q.composite,
109
+ Q.extended_nonnegative, Q.extended_nonzero,
110
+ Q.extended_positive, Q.extended_real, Q.finite, Q.hermitian,
111
+ Q.integer, Q.nonnegative, Q.nonzero, Q.positive, Q.rational,
112
+ Q.real]), set([Q.extended_negative, Q.extended_nonpositive,
113
+ Q.imaginary, Q.infinite, Q.irrational, Q.negative,
114
+ Q.negative_infinite, Q.nonpositive, Q.positive_infinite,
115
+ Q.prime, Q.transcendental, Q.zero])),
116
+ Q.diagonal: (set([Q.diagonal, Q.lower_triangular, Q.normal, Q.square,
117
+ Q.symmetric, Q.triangular, Q.upper_triangular]), set([])),
118
+ Q.even: (set([Q.algebraic, Q.commutative, Q.complex, Q.even,
119
+ Q.extended_real, Q.finite, Q.hermitian, Q.integer, Q.rational,
120
+ Q.real]), set([Q.imaginary, Q.infinite, Q.irrational,
121
+ Q.negative_infinite, Q.odd, Q.positive_infinite,
122
+ Q.transcendental])),
123
+ Q.extended_negative: (set([Q.commutative, Q.extended_negative,
124
+ Q.extended_nonpositive, Q.extended_nonzero, Q.extended_real]),
125
+ set([Q.composite, Q.extended_nonnegative, Q.extended_positive,
126
+ Q.imaginary, Q.nonnegative, Q.positive, Q.positive_infinite,
127
+ Q.prime, Q.zero])),
128
+ Q.extended_nonnegative: (set([Q.commutative, Q.extended_nonnegative,
129
+ Q.extended_real]), set([Q.extended_negative, Q.imaginary,
130
+ Q.negative, Q.negative_infinite])),
131
+ Q.extended_nonpositive: (set([Q.commutative, Q.extended_nonpositive,
132
+ Q.extended_real]), set([Q.composite, Q.extended_positive,
133
+ Q.imaginary, Q.positive, Q.positive_infinite, Q.prime])),
134
+ Q.extended_nonzero: (set([Q.commutative, Q.extended_nonzero,
135
+ Q.extended_real]), set([Q.imaginary, Q.zero])),
136
+ Q.extended_positive: (set([Q.commutative, Q.extended_nonnegative,
137
+ Q.extended_nonzero, Q.extended_positive, Q.extended_real]),
138
+ set([Q.extended_negative, Q.extended_nonpositive, Q.imaginary,
139
+ Q.negative, Q.negative_infinite, Q.nonpositive, Q.zero])),
140
+ Q.extended_real: (set([Q.commutative, Q.extended_real]),
141
+ set([Q.imaginary])),
142
+ Q.finite: (set([Q.commutative, Q.finite]), set([Q.infinite,
143
+ Q.negative_infinite, Q.positive_infinite])),
144
+ Q.fullrank: (set([Q.fullrank]), set([])),
145
+ Q.hermitian: (set([Q.hermitian]), set([])),
146
+ Q.imaginary: (set([Q.antihermitian, Q.commutative, Q.complex,
147
+ Q.finite, Q.imaginary]), set([Q.composite, Q.even,
148
+ Q.extended_negative, Q.extended_nonnegative,
149
+ Q.extended_nonpositive, Q.extended_nonzero,
150
+ Q.extended_positive, Q.extended_real, Q.infinite, Q.integer,
151
+ Q.irrational, Q.negative, Q.negative_infinite, Q.nonnegative,
152
+ Q.nonpositive, Q.nonzero, Q.odd, Q.positive,
153
+ Q.positive_infinite, Q.prime, Q.rational, Q.real, Q.zero])),
154
+ Q.infinite: (set([Q.commutative, Q.infinite]), set([Q.algebraic,
155
+ Q.complex, Q.composite, Q.even, Q.finite, Q.imaginary,
156
+ Q.integer, Q.irrational, Q.negative, Q.nonnegative,
157
+ Q.nonpositive, Q.nonzero, Q.odd, Q.positive, Q.prime,
158
+ Q.rational, Q.real, Q.transcendental, Q.zero])),
159
+ Q.integer: (set([Q.algebraic, Q.commutative, Q.complex,
160
+ Q.extended_real, Q.finite, Q.hermitian, Q.integer, Q.rational,
161
+ Q.real]), set([Q.imaginary, Q.infinite, Q.irrational,
162
+ Q.negative_infinite, Q.positive_infinite, Q.transcendental])),
163
+ Q.integer_elements: (set([Q.complex_elements, Q.integer_elements,
164
+ Q.real_elements]), set([])),
165
+ Q.invertible: (set([Q.fullrank, Q.invertible, Q.square]),
166
+ set([Q.singular])),
167
+ Q.irrational: (set([Q.commutative, Q.complex, Q.extended_nonzero,
168
+ Q.extended_real, Q.finite, Q.hermitian, Q.irrational,
169
+ Q.nonzero, Q.real]), set([Q.composite, Q.even, Q.imaginary,
170
+ Q.infinite, Q.integer, Q.negative_infinite, Q.odd,
171
+ Q.positive_infinite, Q.prime, Q.rational, Q.zero])),
172
+ Q.is_true: (set([Q.is_true]), set([])),
173
+ Q.lower_triangular: (set([Q.lower_triangular, Q.triangular]), set([])),
174
+ Q.negative: (set([Q.commutative, Q.complex, Q.extended_negative,
175
+ Q.extended_nonpositive, Q.extended_nonzero, Q.extended_real,
176
+ Q.finite, Q.hermitian, Q.negative, Q.nonpositive, Q.nonzero,
177
+ Q.real]), set([Q.composite, Q.extended_nonnegative,
178
+ Q.extended_positive, Q.imaginary, Q.infinite,
179
+ Q.negative_infinite, Q.nonnegative, Q.positive,
180
+ Q.positive_infinite, Q.prime, Q.zero])),
181
+ Q.negative_infinite: (set([Q.commutative, Q.extended_negative,
182
+ Q.extended_nonpositive, Q.extended_nonzero, Q.extended_real,
183
+ Q.infinite, Q.negative_infinite]), set([Q.algebraic,
184
+ Q.complex, Q.composite, Q.even, Q.extended_nonnegative,
185
+ Q.extended_positive, Q.finite, Q.imaginary, Q.integer,
186
+ Q.irrational, Q.negative, Q.nonnegative, Q.nonpositive,
187
+ Q.nonzero, Q.odd, Q.positive, Q.positive_infinite, Q.prime,
188
+ Q.rational, Q.real, Q.transcendental, Q.zero])),
189
+ Q.nonnegative: (set([Q.commutative, Q.complex, Q.extended_nonnegative,
190
+ Q.extended_real, Q.finite, Q.hermitian, Q.nonnegative,
191
+ Q.real]), set([Q.extended_negative, Q.imaginary, Q.infinite,
192
+ Q.negative, Q.negative_infinite, Q.positive_infinite])),
193
+ Q.nonpositive: (set([Q.commutative, Q.complex, Q.extended_nonpositive,
194
+ Q.extended_real, Q.finite, Q.hermitian, Q.nonpositive,
195
+ Q.real]), set([Q.composite, Q.extended_positive, Q.imaginary,
196
+ Q.infinite, Q.negative_infinite, Q.positive,
197
+ Q.positive_infinite, Q.prime])),
198
+ Q.nonzero: (set([Q.commutative, Q.complex, Q.extended_nonzero,
199
+ Q.extended_real, Q.finite, Q.hermitian, Q.nonzero, Q.real]),
200
+ set([Q.imaginary, Q.infinite, Q.negative_infinite,
201
+ Q.positive_infinite, Q.zero])),
202
+ Q.normal: (set([Q.normal, Q.square]), set([])),
203
+ Q.odd: (set([Q.algebraic, Q.commutative, Q.complex,
204
+ Q.extended_nonzero, Q.extended_real, Q.finite, Q.hermitian,
205
+ Q.integer, Q.nonzero, Q.odd, Q.rational, Q.real]),
206
+ set([Q.even, Q.imaginary, Q.infinite, Q.irrational,
207
+ Q.negative_infinite, Q.positive_infinite, Q.transcendental,
208
+ Q.zero])),
209
+ Q.orthogonal: (set([Q.fullrank, Q.invertible, Q.normal, Q.orthogonal,
210
+ Q.positive_definite, Q.square, Q.unitary]), set([Q.singular])),
211
+ Q.positive: (set([Q.commutative, Q.complex, Q.extended_nonnegative,
212
+ Q.extended_nonzero, Q.extended_positive, Q.extended_real,
213
+ Q.finite, Q.hermitian, Q.nonnegative, Q.nonzero, Q.positive,
214
+ Q.real]), set([Q.extended_negative, Q.extended_nonpositive,
215
+ Q.imaginary, Q.infinite, Q.negative, Q.negative_infinite,
216
+ Q.nonpositive, Q.positive_infinite, Q.zero])),
217
+ Q.positive_definite: (set([Q.fullrank, Q.invertible,
218
+ Q.positive_definite, Q.square]), set([Q.singular])),
219
+ Q.positive_infinite: (set([Q.commutative, Q.extended_nonnegative,
220
+ Q.extended_nonzero, Q.extended_positive, Q.extended_real,
221
+ Q.infinite, Q.positive_infinite]), set([Q.algebraic,
222
+ Q.complex, Q.composite, Q.even, Q.extended_negative,
223
+ Q.extended_nonpositive, Q.finite, Q.imaginary, Q.integer,
224
+ Q.irrational, Q.negative, Q.negative_infinite, Q.nonnegative,
225
+ Q.nonpositive, Q.nonzero, Q.odd, Q.positive, Q.prime,
226
+ Q.rational, Q.real, Q.transcendental, Q.zero])),
227
+ Q.prime: (set([Q.algebraic, Q.commutative, Q.complex,
228
+ Q.extended_nonnegative, Q.extended_nonzero,
229
+ Q.extended_positive, Q.extended_real, Q.finite, Q.hermitian,
230
+ Q.integer, Q.nonnegative, Q.nonzero, Q.positive, Q.prime,
231
+ Q.rational, Q.real]), set([Q.composite, Q.extended_negative,
232
+ Q.extended_nonpositive, Q.imaginary, Q.infinite, Q.irrational,
233
+ Q.negative, Q.negative_infinite, Q.nonpositive,
234
+ Q.positive_infinite, Q.transcendental, Q.zero])),
235
+ Q.rational: (set([Q.algebraic, Q.commutative, Q.complex,
236
+ Q.extended_real, Q.finite, Q.hermitian, Q.rational, Q.real]),
237
+ set([Q.imaginary, Q.infinite, Q.irrational,
238
+ Q.negative_infinite, Q.positive_infinite, Q.transcendental])),
239
+ Q.real: (set([Q.commutative, Q.complex, Q.extended_real, Q.finite,
240
+ Q.hermitian, Q.real]), set([Q.imaginary, Q.infinite,
241
+ Q.negative_infinite, Q.positive_infinite])),
242
+ Q.real_elements: (set([Q.complex_elements, Q.real_elements]), set([])),
243
+ Q.singular: (set([Q.singular]), set([Q.invertible, Q.orthogonal,
244
+ Q.positive_definite, Q.unitary])),
245
+ Q.square: (set([Q.square]), set([])),
246
+ Q.symmetric: (set([Q.square, Q.symmetric]), set([])),
247
+ Q.transcendental: (set([Q.commutative, Q.complex, Q.finite,
248
+ Q.transcendental]), set([Q.algebraic, Q.composite, Q.even,
249
+ Q.infinite, Q.integer, Q.negative_infinite, Q.odd,
250
+ Q.positive_infinite, Q.prime, Q.rational, Q.zero])),
251
+ Q.triangular: (set([Q.triangular]), set([])),
252
+ Q.unit_triangular: (set([Q.triangular, Q.unit_triangular]), set([])),
253
+ Q.unitary: (set([Q.fullrank, Q.invertible, Q.normal, Q.square,
254
+ Q.unitary]), set([Q.singular])),
255
+ Q.upper_triangular: (set([Q.triangular, Q.upper_triangular]), set([])),
256
+ Q.zero: (set([Q.algebraic, Q.commutative, Q.complex, Q.even,
257
+ Q.extended_nonnegative, Q.extended_nonpositive,
258
+ Q.extended_real, Q.finite, Q.hermitian, Q.integer,
259
+ Q.nonnegative, Q.nonpositive, Q.rational, Q.real, Q.zero]),
260
+ set([Q.composite, Q.extended_negative, Q.extended_nonzero,
261
+ Q.extended_positive, Q.imaginary, Q.infinite, Q.irrational,
262
+ Q.negative, Q.negative_infinite, Q.nonzero, Q.odd, Q.positive,
263
+ Q.positive_infinite, Q.prime, Q.transcendental])),
264
+ }
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/assume.py ADDED
@@ -0,0 +1,485 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """A module which implements predicates and assumption context."""
2
+
3
+ from contextlib import contextmanager
4
+ import inspect
5
+ from sympy.core.symbol import Str
6
+ from sympy.core.sympify import _sympify
7
+ from sympy.logic.boolalg import Boolean, false, true
8
+ from sympy.multipledispatch.dispatcher import Dispatcher, str_signature
9
+ from sympy.utilities.exceptions import sympy_deprecation_warning
10
+ from sympy.utilities.iterables import is_sequence
11
+ from sympy.utilities.source import get_class
12
+
13
+
14
+ class AssumptionsContext(set):
15
+ """
16
+ Set containing default assumptions which are applied to the ``ask()``
17
+ function.
18
+
19
+ Explanation
20
+ ===========
21
+
22
+ This is used to represent global assumptions, but you can also use this
23
+ class to create your own local assumptions contexts. It is basically a thin
24
+ wrapper to Python's set, so see its documentation for advanced usage.
25
+
26
+ Examples
27
+ ========
28
+
29
+ The default assumption context is ``global_assumptions``, which is initially empty:
30
+
31
+ >>> from sympy import ask, Q
32
+ >>> from sympy.assumptions import global_assumptions
33
+ >>> global_assumptions
34
+ AssumptionsContext()
35
+
36
+ You can add default assumptions:
37
+
38
+ >>> from sympy.abc import x
39
+ >>> global_assumptions.add(Q.real(x))
40
+ >>> global_assumptions
41
+ AssumptionsContext({Q.real(x)})
42
+ >>> ask(Q.real(x))
43
+ True
44
+
45
+ And remove them:
46
+
47
+ >>> global_assumptions.remove(Q.real(x))
48
+ >>> print(ask(Q.real(x)))
49
+ None
50
+
51
+ The ``clear()`` method removes every assumption:
52
+
53
+ >>> global_assumptions.add(Q.positive(x))
54
+ >>> global_assumptions
55
+ AssumptionsContext({Q.positive(x)})
56
+ >>> global_assumptions.clear()
57
+ >>> global_assumptions
58
+ AssumptionsContext()
59
+
60
+ See Also
61
+ ========
62
+
63
+ assuming
64
+
65
+ """
66
+
67
+ def add(self, *assumptions):
68
+ """Add assumptions."""
69
+ for a in assumptions:
70
+ super().add(a)
71
+
72
+ def _sympystr(self, printer):
73
+ if not self:
74
+ return "%s()" % self.__class__.__name__
75
+ return "{}({})".format(self.__class__.__name__, printer._print_set(self))
76
+
77
+ global_assumptions = AssumptionsContext()
78
+
79
+
80
+ class AppliedPredicate(Boolean):
81
+ """
82
+ The class of expressions resulting from applying ``Predicate`` to
83
+ the arguments. ``AppliedPredicate`` merely wraps its argument and
84
+ remain unevaluated. To evaluate it, use the ``ask()`` function.
85
+
86
+ Examples
87
+ ========
88
+
89
+ >>> from sympy import Q, ask
90
+ >>> Q.integer(1)
91
+ Q.integer(1)
92
+
93
+ The ``function`` attribute returns the predicate, and the ``arguments``
94
+ attribute returns the tuple of arguments.
95
+
96
+ >>> type(Q.integer(1))
97
+ <class 'sympy.assumptions.assume.AppliedPredicate'>
98
+ >>> Q.integer(1).function
99
+ Q.integer
100
+ >>> Q.integer(1).arguments
101
+ (1,)
102
+
103
+ Applied predicates can be evaluated to a boolean value with ``ask``:
104
+
105
+ >>> ask(Q.integer(1))
106
+ True
107
+
108
+ """
109
+ __slots__ = ()
110
+
111
+ def __new__(cls, predicate, *args):
112
+ if not isinstance(predicate, Predicate):
113
+ raise TypeError("%s is not a Predicate." % predicate)
114
+ args = map(_sympify, args)
115
+ return super().__new__(cls, predicate, *args)
116
+
117
+ @property
118
+ def arg(self):
119
+ """
120
+ Return the expression used by this assumption.
121
+
122
+ Examples
123
+ ========
124
+
125
+ >>> from sympy import Q, Symbol
126
+ >>> x = Symbol('x')
127
+ >>> a = Q.integer(x + 1)
128
+ >>> a.arg
129
+ x + 1
130
+
131
+ """
132
+ # Will be deprecated
133
+ args = self._args
134
+ if len(args) == 2:
135
+ # backwards compatibility
136
+ return args[1]
137
+ raise TypeError("'arg' property is allowed only for unary predicates.")
138
+
139
+ @property
140
+ def function(self):
141
+ """
142
+ Return the predicate.
143
+ """
144
+ # Will be changed to self.args[0] after args overriding is removed
145
+ return self._args[0]
146
+
147
+ @property
148
+ def arguments(self):
149
+ """
150
+ Return the arguments which are applied to the predicate.
151
+ """
152
+ # Will be changed to self.args[1:] after args overriding is removed
153
+ return self._args[1:]
154
+
155
+ def _eval_ask(self, assumptions):
156
+ return self.function.eval(self.arguments, assumptions)
157
+
158
+ @property
159
+ def binary_symbols(self):
160
+ from .ask import Q
161
+ if self.function == Q.is_true:
162
+ i = self.arguments[0]
163
+ if i.is_Boolean or i.is_Symbol:
164
+ return i.binary_symbols
165
+ if self.function in (Q.eq, Q.ne):
166
+ if true in self.arguments or false in self.arguments:
167
+ if self.arguments[0].is_Symbol:
168
+ return {self.arguments[0]}
169
+ elif self.arguments[1].is_Symbol:
170
+ return {self.arguments[1]}
171
+ return set()
172
+
173
+
174
+ class PredicateMeta(type):
175
+ def __new__(cls, clsname, bases, dct):
176
+ # If handler is not defined, assign empty dispatcher.
177
+ if "handler" not in dct:
178
+ name = f"Ask{clsname.capitalize()}Handler"
179
+ handler = Dispatcher(name, doc="Handler for key %s" % name)
180
+ dct["handler"] = handler
181
+
182
+ dct["_orig_doc"] = dct.get("__doc__", "")
183
+
184
+ return super().__new__(cls, clsname, bases, dct)
185
+
186
+ @property
187
+ def __doc__(cls):
188
+ handler = cls.handler
189
+ doc = cls._orig_doc
190
+ if cls is not Predicate and handler is not None:
191
+ doc += "Handler\n"
192
+ doc += " =======\n\n"
193
+
194
+ # Append the handler's doc without breaking sphinx documentation.
195
+ docs = [" Multiply dispatched method: %s" % handler.name]
196
+ if handler.doc:
197
+ for line in handler.doc.splitlines():
198
+ if not line:
199
+ continue
200
+ docs.append(" %s" % line)
201
+ other = []
202
+ for sig in handler.ordering[::-1]:
203
+ func = handler.funcs[sig]
204
+ if func.__doc__:
205
+ s = ' Inputs: <%s>' % str_signature(sig)
206
+ lines = []
207
+ for line in func.__doc__.splitlines():
208
+ lines.append(" %s" % line)
209
+ s += "\n".join(lines)
210
+ docs.append(s)
211
+ else:
212
+ other.append(str_signature(sig))
213
+ if other:
214
+ othersig = " Other signatures:"
215
+ for line in other:
216
+ othersig += "\n * %s" % line
217
+ docs.append(othersig)
218
+
219
+ doc += '\n\n'.join(docs)
220
+
221
+ return doc
222
+
223
+
224
+ class Predicate(Boolean, metaclass=PredicateMeta):
225
+ """
226
+ Base class for mathematical predicates. It also serves as a
227
+ constructor for undefined predicate objects.
228
+
229
+ Explanation
230
+ ===========
231
+
232
+ Predicate is a function that returns a boolean value [1].
233
+
234
+ Predicate function is object, and it is instance of predicate class.
235
+ When a predicate is applied to arguments, ``AppliedPredicate``
236
+ instance is returned. This merely wraps the argument and remain
237
+ unevaluated. To obtain the truth value of applied predicate, use the
238
+ function ``ask``.
239
+
240
+ Evaluation of predicate is done by multiple dispatching. You can
241
+ register new handler to the predicate to support new types.
242
+
243
+ Every predicate in SymPy can be accessed via the property of ``Q``.
244
+ For example, ``Q.even`` returns the predicate which checks if the
245
+ argument is even number.
246
+
247
+ To define a predicate which can be evaluated, you must subclass this
248
+ class, make an instance of it, and register it to ``Q``. After then,
249
+ dispatch the handler by argument types.
250
+
251
+ If you directly construct predicate using this class, you will get
252
+ ``UndefinedPredicate`` which cannot be dispatched. This is useful
253
+ when you are building boolean expressions which do not need to be
254
+ evaluated.
255
+
256
+ Examples
257
+ ========
258
+
259
+ Applying and evaluating to boolean value:
260
+
261
+ >>> from sympy import Q, ask
262
+ >>> ask(Q.prime(7))
263
+ True
264
+
265
+ You can define a new predicate by subclassing and dispatching. Here,
266
+ we define a predicate for sexy primes [2] as an example.
267
+
268
+ >>> from sympy import Predicate, Integer
269
+ >>> class SexyPrimePredicate(Predicate):
270
+ ... name = "sexyprime"
271
+ >>> Q.sexyprime = SexyPrimePredicate()
272
+ >>> @Q.sexyprime.register(Integer, Integer)
273
+ ... def _(int1, int2, assumptions):
274
+ ... args = sorted([int1, int2])
275
+ ... if not all(ask(Q.prime(a), assumptions) for a in args):
276
+ ... return False
277
+ ... return args[1] - args[0] == 6
278
+ >>> ask(Q.sexyprime(5, 11))
279
+ True
280
+
281
+ Direct constructing returns ``UndefinedPredicate``, which can be
282
+ applied but cannot be dispatched.
283
+
284
+ >>> from sympy import Predicate, Integer
285
+ >>> Q.P = Predicate("P")
286
+ >>> type(Q.P)
287
+ <class 'sympy.assumptions.assume.UndefinedPredicate'>
288
+ >>> Q.P(1)
289
+ Q.P(1)
290
+ >>> Q.P.register(Integer)(lambda expr, assump: True)
291
+ Traceback (most recent call last):
292
+ ...
293
+ TypeError: <class 'sympy.assumptions.assume.UndefinedPredicate'> cannot be dispatched.
294
+
295
+ References
296
+ ==========
297
+
298
+ .. [1] https://en.wikipedia.org/wiki/Predicate_%28mathematical_logic%29
299
+ .. [2] https://en.wikipedia.org/wiki/Sexy_prime
300
+
301
+ """
302
+
303
+ is_Atom = True
304
+
305
+ def __new__(cls, *args, **kwargs):
306
+ if cls is Predicate:
307
+ return UndefinedPredicate(*args, **kwargs)
308
+ obj = super().__new__(cls, *args)
309
+ return obj
310
+
311
+ @property
312
+ def name(self):
313
+ # May be overridden
314
+ return type(self).__name__
315
+
316
+ @classmethod
317
+ def register(cls, *types, **kwargs):
318
+ """
319
+ Register the signature to the handler.
320
+ """
321
+ if cls.handler is None:
322
+ raise TypeError("%s cannot be dispatched." % type(cls))
323
+ return cls.handler.register(*types, **kwargs)
324
+
325
+ @classmethod
326
+ def register_many(cls, *types, **kwargs):
327
+ """
328
+ Register multiple signatures to same handler.
329
+ """
330
+ def _(func):
331
+ for t in types:
332
+ if not is_sequence(t):
333
+ t = (t,) # for convenience, allow passing `type` to mean `(type,)`
334
+ cls.register(*t, **kwargs)(func)
335
+ return _
336
+
337
+ def __call__(self, *args):
338
+ return AppliedPredicate(self, *args)
339
+
340
+ def eval(self, args, assumptions=True):
341
+ """
342
+ Evaluate ``self(*args)`` under the given assumptions.
343
+
344
+ This uses only direct resolution methods, not logical inference.
345
+ """
346
+ result = None
347
+ try:
348
+ result = self.handler(*args, assumptions=assumptions)
349
+ except NotImplementedError:
350
+ pass
351
+ return result
352
+
353
+ def _eval_refine(self, assumptions):
354
+ # When Predicate is no longer Boolean, delete this method
355
+ return self
356
+
357
+
358
+ class UndefinedPredicate(Predicate):
359
+ """
360
+ Predicate without handler.
361
+
362
+ Explanation
363
+ ===========
364
+
365
+ This predicate is generated by using ``Predicate`` directly for
366
+ construction. It does not have a handler, and evaluating this with
367
+ arguments is done by SAT solver.
368
+
369
+ Examples
370
+ ========
371
+
372
+ >>> from sympy import Predicate, Q
373
+ >>> Q.P = Predicate('P')
374
+ >>> Q.P.func
375
+ <class 'sympy.assumptions.assume.UndefinedPredicate'>
376
+ >>> Q.P.name
377
+ Str('P')
378
+
379
+ """
380
+
381
+ handler = None
382
+
383
+ def __new__(cls, name, handlers=None):
384
+ # "handlers" parameter supports old design
385
+ if not isinstance(name, Str):
386
+ name = Str(name)
387
+ obj = super(Boolean, cls).__new__(cls, name)
388
+ obj.handlers = handlers or []
389
+ return obj
390
+
391
+ @property
392
+ def name(self):
393
+ return self.args[0]
394
+
395
+ def _hashable_content(self):
396
+ return (self.name,)
397
+
398
+ def __getnewargs__(self):
399
+ return (self.name,)
400
+
401
+ def __call__(self, expr):
402
+ return AppliedPredicate(self, expr)
403
+
404
+ def add_handler(self, handler):
405
+ sympy_deprecation_warning(
406
+ """
407
+ The AskHandler system is deprecated. Predicate.add_handler()
408
+ should be replaced with the multipledispatch handler of Predicate.
409
+ """,
410
+ deprecated_since_version="1.8",
411
+ active_deprecations_target='deprecated-askhandler',
412
+ )
413
+ self.handlers.append(handler)
414
+
415
+ def remove_handler(self, handler):
416
+ sympy_deprecation_warning(
417
+ """
418
+ The AskHandler system is deprecated. Predicate.remove_handler()
419
+ should be replaced with the multipledispatch handler of Predicate.
420
+ """,
421
+ deprecated_since_version="1.8",
422
+ active_deprecations_target='deprecated-askhandler',
423
+ )
424
+ self.handlers.remove(handler)
425
+
426
+ def eval(self, args, assumptions=True):
427
+ # Support for deprecated design
428
+ # When old design is removed, this will always return None
429
+ sympy_deprecation_warning(
430
+ """
431
+ The AskHandler system is deprecated. Evaluating UndefinedPredicate
432
+ objects should be replaced with the multipledispatch handler of
433
+ Predicate.
434
+ """,
435
+ deprecated_since_version="1.8",
436
+ active_deprecations_target='deprecated-askhandler',
437
+ stacklevel=5,
438
+ )
439
+ expr, = args
440
+ res, _res = None, None
441
+ mro = inspect.getmro(type(expr))
442
+ for handler in self.handlers:
443
+ cls = get_class(handler)
444
+ for subclass in mro:
445
+ eval_ = getattr(cls, subclass.__name__, None)
446
+ if eval_ is None:
447
+ continue
448
+ res = eval_(expr, assumptions)
449
+ # Do not stop if value returned is None
450
+ # Try to check for higher classes
451
+ if res is None:
452
+ continue
453
+ if _res is None:
454
+ _res = res
455
+ else:
456
+ # only check consistency if both resolutors have concluded
457
+ if _res != res:
458
+ raise ValueError('incompatible resolutors')
459
+ break
460
+ return res
461
+
462
+
463
+ @contextmanager
464
+ def assuming(*assumptions):
465
+ """
466
+ Context manager for assumptions.
467
+
468
+ Examples
469
+ ========
470
+
471
+ >>> from sympy import assuming, Q, ask
472
+ >>> from sympy.abc import x, y
473
+ >>> print(ask(Q.integer(x + y)))
474
+ None
475
+ >>> with assuming(Q.integer(x), Q.integer(y)):
476
+ ... print(ask(Q.integer(x + y)))
477
+ True
478
+ """
479
+ old_global_assumptions = global_assumptions.copy()
480
+ global_assumptions.update(assumptions)
481
+ try:
482
+ yield
483
+ finally:
484
+ global_assumptions.clear()
485
+ global_assumptions.update(old_global_assumptions)
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/facts.py ADDED
@@ -0,0 +1,220 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Known facts in assumptions module.
3
+
4
+ This module defines the facts between unary predicates in ``get_known_facts()``,
5
+ and supports functions to generate the contents in
6
+ ``sympy.assumptions.ask_generated`` file.
7
+ """
8
+
9
+ from sympy.assumptions.ask import Q
10
+ from sympy.assumptions.assume import AppliedPredicate
11
+ from sympy.core.cache import cacheit
12
+ from sympy.core.symbol import Symbol
13
+ from sympy.logic.boolalg import (to_cnf, And, Not, Implies, Equivalent,
14
+ Exclusive,)
15
+ from sympy.logic.inference import satisfiable
16
+
17
+
18
+ @cacheit
19
+ def get_composite_predicates():
20
+ # To reduce the complexity of sat solver, these predicates are
21
+ # transformed into the combination of primitive predicates.
22
+ return {
23
+ Q.real : Q.negative | Q.zero | Q.positive,
24
+ Q.integer : Q.even | Q.odd,
25
+ Q.nonpositive : Q.negative | Q.zero,
26
+ Q.nonzero : Q.negative | Q.positive,
27
+ Q.nonnegative : Q.zero | Q.positive,
28
+ Q.extended_real : Q.negative_infinite | Q.negative | Q.zero | Q.positive | Q.positive_infinite,
29
+ Q.extended_positive: Q.positive | Q.positive_infinite,
30
+ Q.extended_negative: Q.negative | Q.negative_infinite,
31
+ Q.extended_nonzero: Q.negative_infinite | Q.negative | Q.positive | Q.positive_infinite,
32
+ Q.extended_nonpositive: Q.negative_infinite | Q.negative | Q.zero,
33
+ Q.extended_nonnegative: Q.zero | Q.positive | Q.positive_infinite,
34
+ Q.complex : Q.algebraic | Q.transcendental
35
+ }
36
+
37
+
38
+ @cacheit
39
+ def get_known_facts(x=None):
40
+ """
41
+ Facts between unary predicates.
42
+
43
+ Parameters
44
+ ==========
45
+
46
+ x : Symbol, optional
47
+ Placeholder symbol for unary facts. Default is ``Symbol('x')``.
48
+
49
+ Returns
50
+ =======
51
+
52
+ fact : Known facts in conjugated normal form.
53
+
54
+ """
55
+ if x is None:
56
+ x = Symbol('x')
57
+
58
+ fact = And(
59
+ # primitive predicates for extended real exclude each other.
60
+ Exclusive(Q.negative_infinite(x), Q.negative(x), Q.zero(x),
61
+ Q.positive(x), Q.positive_infinite(x)),
62
+
63
+ # build complex plane
64
+ Exclusive(Q.real(x), Q.imaginary(x)),
65
+ Implies(Q.real(x) | Q.imaginary(x), Q.complex(x)),
66
+
67
+ # other subsets of complex
68
+ Exclusive(Q.transcendental(x), Q.algebraic(x)),
69
+ Equivalent(Q.real(x), Q.rational(x) | Q.irrational(x)),
70
+ Exclusive(Q.irrational(x), Q.rational(x)),
71
+ Implies(Q.rational(x), Q.algebraic(x)),
72
+
73
+ # integers
74
+ Exclusive(Q.even(x), Q.odd(x)),
75
+ Implies(Q.integer(x), Q.rational(x)),
76
+ Implies(Q.zero(x), Q.even(x)),
77
+ Exclusive(Q.composite(x), Q.prime(x)),
78
+ Implies(Q.composite(x) | Q.prime(x), Q.integer(x) & Q.positive(x)),
79
+ Implies(Q.even(x) & Q.positive(x) & ~Q.prime(x), Q.composite(x)),
80
+
81
+ # hermitian and antihermitian
82
+ Implies(Q.real(x), Q.hermitian(x)),
83
+ Implies(Q.imaginary(x), Q.antihermitian(x)),
84
+ Implies(Q.zero(x), Q.hermitian(x) | Q.antihermitian(x)),
85
+
86
+ # define finity and infinity, and build extended real line
87
+ Exclusive(Q.infinite(x), Q.finite(x)),
88
+ Implies(Q.complex(x), Q.finite(x)),
89
+ Implies(Q.negative_infinite(x) | Q.positive_infinite(x), Q.infinite(x)),
90
+
91
+ # commutativity
92
+ Implies(Q.finite(x) | Q.infinite(x), Q.commutative(x)),
93
+
94
+ # matrices
95
+ Implies(Q.orthogonal(x), Q.positive_definite(x)),
96
+ Implies(Q.orthogonal(x), Q.unitary(x)),
97
+ Implies(Q.unitary(x) & Q.real_elements(x), Q.orthogonal(x)),
98
+ Implies(Q.unitary(x), Q.normal(x)),
99
+ Implies(Q.unitary(x), Q.invertible(x)),
100
+ Implies(Q.normal(x), Q.square(x)),
101
+ Implies(Q.diagonal(x), Q.normal(x)),
102
+ Implies(Q.positive_definite(x), Q.invertible(x)),
103
+ Implies(Q.diagonal(x), Q.upper_triangular(x)),
104
+ Implies(Q.diagonal(x), Q.lower_triangular(x)),
105
+ Implies(Q.lower_triangular(x), Q.triangular(x)),
106
+ Implies(Q.upper_triangular(x), Q.triangular(x)),
107
+ Implies(Q.triangular(x), Q.upper_triangular(x) | Q.lower_triangular(x)),
108
+ Implies(Q.upper_triangular(x) & Q.lower_triangular(x), Q.diagonal(x)),
109
+ Implies(Q.diagonal(x), Q.symmetric(x)),
110
+ Implies(Q.unit_triangular(x), Q.triangular(x)),
111
+ Implies(Q.invertible(x), Q.fullrank(x)),
112
+ Implies(Q.invertible(x), Q.square(x)),
113
+ Implies(Q.symmetric(x), Q.square(x)),
114
+ Implies(Q.fullrank(x) & Q.square(x), Q.invertible(x)),
115
+ Equivalent(Q.invertible(x), ~Q.singular(x)),
116
+ Implies(Q.integer_elements(x), Q.real_elements(x)),
117
+ Implies(Q.real_elements(x), Q.complex_elements(x)),
118
+ )
119
+ return fact
120
+
121
+
122
+ def generate_known_facts_dict(keys, fact):
123
+ """
124
+ Computes and returns a dictionary which contains the relations between
125
+ unary predicates.
126
+
127
+ Each key is a predicate, and item is two groups of predicates.
128
+ First group contains the predicates which are implied by the key, and
129
+ second group contains the predicates which are rejected by the key.
130
+
131
+ All predicates in *keys* and *fact* must be unary and have same placeholder
132
+ symbol.
133
+
134
+ Parameters
135
+ ==========
136
+
137
+ keys : list of AppliedPredicate instances.
138
+
139
+ fact : Fact between predicates in conjugated normal form.
140
+
141
+ Examples
142
+ ========
143
+
144
+ >>> from sympy import Q, And, Implies
145
+ >>> from sympy.assumptions.facts import generate_known_facts_dict
146
+ >>> from sympy.abc import x
147
+ >>> keys = [Q.even(x), Q.odd(x), Q.zero(x)]
148
+ >>> fact = And(Implies(Q.even(x), ~Q.odd(x)),
149
+ ... Implies(Q.zero(x), Q.even(x)))
150
+ >>> generate_known_facts_dict(keys, fact)
151
+ {Q.even: ({Q.even}, {Q.odd}),
152
+ Q.odd: ({Q.odd}, {Q.even, Q.zero}),
153
+ Q.zero: ({Q.even, Q.zero}, {Q.odd})}
154
+ """
155
+ fact_cnf = to_cnf(fact)
156
+ mapping = single_fact_lookup(keys, fact_cnf)
157
+
158
+ ret = {}
159
+ for key, value in mapping.items():
160
+ implied = set()
161
+ rejected = set()
162
+ for expr in value:
163
+ if isinstance(expr, AppliedPredicate):
164
+ implied.add(expr.function)
165
+ elif isinstance(expr, Not):
166
+ pred = expr.args[0]
167
+ rejected.add(pred.function)
168
+ ret[key.function] = (implied, rejected)
169
+ return ret
170
+
171
+
172
+ @cacheit
173
+ def get_known_facts_keys():
174
+ """
175
+ Return every unary predicates registered to ``Q``.
176
+
177
+ This function is used to generate the keys for
178
+ ``generate_known_facts_dict``.
179
+
180
+ """
181
+ exclude = set()
182
+ for pred in [Q.eq, Q.ne, Q.gt, Q.lt, Q.ge, Q.le]:
183
+ # exclude polyadic predicates
184
+ exclude.add(pred)
185
+
186
+ result = []
187
+ for attr in Q.__class__.__dict__:
188
+ if attr.startswith('__'):
189
+ continue
190
+ pred = getattr(Q, attr)
191
+ if pred in exclude:
192
+ continue
193
+ result.append(pred)
194
+ return result
195
+
196
+
197
+ def single_fact_lookup(known_facts_keys, known_facts_cnf):
198
+ # Return the dictionary for quick lookup of single fact
199
+ mapping = {}
200
+ for key in known_facts_keys:
201
+ mapping[key] = {key}
202
+ for other_key in known_facts_keys:
203
+ if other_key != key:
204
+ if ask_full_inference(other_key, key, known_facts_cnf):
205
+ mapping[key].add(other_key)
206
+ if ask_full_inference(~other_key, key, known_facts_cnf):
207
+ mapping[key].add(~other_key)
208
+ return mapping
209
+
210
+
211
+ def ask_full_inference(proposition, assumptions, known_facts_cnf):
212
+ """
213
+ Method for inferring properties about objects.
214
+
215
+ """
216
+ if not satisfiable(And(known_facts_cnf, assumptions, proposition)):
217
+ return False
218
+ if not satisfiable(And(known_facts_cnf, assumptions, Not(proposition))):
219
+ return True
220
+ return None
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__init__.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Multipledispatch handlers for ``Predicate`` are implemented here.
3
+ Handlers in this module are not directly imported to other modules in
4
+ order to avoid circular import problem.
5
+ """
6
+
7
+ from .common import (AskHandler, CommonHandler,
8
+ test_closed_group)
9
+
10
+ __all__ = [
11
+ 'AskHandler', 'CommonHandler',
12
+ 'test_closed_group'
13
+ ]
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (505 Bytes). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/calculus.cpython-310.pyc ADDED
Binary file (6.38 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/common.cpython-310.pyc ADDED
Binary file (4.64 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/matrices.cpython-310.pyc ADDED
Binary file (18.5 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/ntheory.cpython-310.pyc ADDED
Binary file (6.02 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/order.cpython-310.pyc ADDED
Binary file (10.3 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/__pycache__/sets.cpython-310.pyc ADDED
Binary file (17.9 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/calculus.py ADDED
@@ -0,0 +1,258 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This module contains query handlers responsible for calculus queries:
3
+ infinitesimal, finite, etc.
4
+ """
5
+
6
+ from sympy.assumptions import Q, ask
7
+ from sympy.core import Add, Mul, Pow, Symbol
8
+ from sympy.core.numbers import (NegativeInfinity, GoldenRatio,
9
+ Infinity, Exp1, ComplexInfinity, ImaginaryUnit, NaN, Number, Pi, E,
10
+ TribonacciConstant)
11
+ from sympy.functions import cos, exp, log, sign, sin
12
+ from sympy.logic.boolalg import conjuncts
13
+
14
+ from ..predicates.calculus import (FinitePredicate, InfinitePredicate,
15
+ PositiveInfinitePredicate, NegativeInfinitePredicate)
16
+
17
+
18
+ # FinitePredicate
19
+
20
+
21
+ @FinitePredicate.register(Symbol)
22
+ def _(expr, assumptions):
23
+ """
24
+ Handles Symbol.
25
+ """
26
+ if expr.is_finite is not None:
27
+ return expr.is_finite
28
+ if Q.finite(expr) in conjuncts(assumptions):
29
+ return True
30
+ return None
31
+
32
+ @FinitePredicate.register(Add)
33
+ def _(expr, assumptions):
34
+ """
35
+ Return True if expr is bounded, False if not and None if unknown.
36
+
37
+ Truth Table:
38
+
39
+ +-------+-----+-----------+-----------+
40
+ | | | | |
41
+ | | B | U | ? |
42
+ | | | | |
43
+ +-------+-----+---+---+---+---+---+---+
44
+ | | | | | | | | |
45
+ | | |'+'|'-'|'x'|'+'|'-'|'x'|
46
+ | | | | | | | | |
47
+ +-------+-----+---+---+---+---+---+---+
48
+ | | | | |
49
+ | B | B | U | ? |
50
+ | | | | |
51
+ +---+---+-----+---+---+---+---+---+---+
52
+ | | | | | | | | | |
53
+ | |'+'| | U | ? | ? | U | ? | ? |
54
+ | | | | | | | | | |
55
+ | +---+-----+---+---+---+---+---+---+
56
+ | | | | | | | | | |
57
+ | U |'-'| | ? | U | ? | ? | U | ? |
58
+ | | | | | | | | | |
59
+ | +---+-----+---+---+---+---+---+---+
60
+ | | | | | |
61
+ | |'x'| | ? | ? |
62
+ | | | | | |
63
+ +---+---+-----+---+---+---+---+---+---+
64
+ | | | | |
65
+ | ? | | | ? |
66
+ | | | | |
67
+ +-------+-----+-----------+---+---+---+
68
+
69
+ * 'B' = Bounded
70
+
71
+ * 'U' = Unbounded
72
+
73
+ * '?' = unknown boundedness
74
+
75
+ * '+' = positive sign
76
+
77
+ * '-' = negative sign
78
+
79
+ * 'x' = sign unknown
80
+
81
+ * All Bounded -> True
82
+
83
+ * 1 Unbounded and the rest Bounded -> False
84
+
85
+ * >1 Unbounded, all with same known sign -> False
86
+
87
+ * Any Unknown and unknown sign -> None
88
+
89
+ * Else -> None
90
+
91
+ When the signs are not the same you can have an undefined
92
+ result as in oo - oo, hence 'bounded' is also undefined.
93
+ """
94
+ sign = -1 # sign of unknown or infinite
95
+ result = True
96
+ for arg in expr.args:
97
+ _bounded = ask(Q.finite(arg), assumptions)
98
+ if _bounded:
99
+ continue
100
+ s = ask(Q.extended_positive(arg), assumptions)
101
+ # if there has been more than one sign or if the sign of this arg
102
+ # is None and Bounded is None or there was already
103
+ # an unknown sign, return None
104
+ if sign != -1 and s != sign or \
105
+ s is None and None in (_bounded, sign):
106
+ return None
107
+ else:
108
+ sign = s
109
+ # once False, do not change
110
+ if result is not False:
111
+ result = _bounded
112
+ return result
113
+
114
+ @FinitePredicate.register(Mul)
115
+ def _(expr, assumptions):
116
+ """
117
+ Return True if expr is bounded, False if not and None if unknown.
118
+
119
+ Truth Table:
120
+
121
+ +---+---+---+--------+
122
+ | | | | |
123
+ | | B | U | ? |
124
+ | | | | |
125
+ +---+---+---+---+----+
126
+ | | | | | |
127
+ | | | | s | /s |
128
+ | | | | | |
129
+ +---+---+---+---+----+
130
+ | | | | |
131
+ | B | B | U | ? |
132
+ | | | | |
133
+ +---+---+---+---+----+
134
+ | | | | | |
135
+ | U | | U | U | ? |
136
+ | | | | | |
137
+ +---+---+---+---+----+
138
+ | | | | |
139
+ | ? | | | ? |
140
+ | | | | |
141
+ +---+---+---+---+----+
142
+
143
+ * B = Bounded
144
+
145
+ * U = Unbounded
146
+
147
+ * ? = unknown boundedness
148
+
149
+ * s = signed (hence nonzero)
150
+
151
+ * /s = not signed
152
+ """
153
+ result = True
154
+ for arg in expr.args:
155
+ _bounded = ask(Q.finite(arg), assumptions)
156
+ if _bounded:
157
+ continue
158
+ elif _bounded is None:
159
+ if result is None:
160
+ return None
161
+ if ask(Q.extended_nonzero(arg), assumptions) is None:
162
+ return None
163
+ if result is not False:
164
+ result = None
165
+ else:
166
+ result = False
167
+ return result
168
+
169
+ @FinitePredicate.register(Pow)
170
+ def _(expr, assumptions):
171
+ """
172
+ * Unbounded ** NonZero -> Unbounded
173
+
174
+ * Bounded ** Bounded -> Bounded
175
+
176
+ * Abs()<=1 ** Positive -> Bounded
177
+
178
+ * Abs()>=1 ** Negative -> Bounded
179
+
180
+ * Otherwise unknown
181
+ """
182
+ if expr.base == E:
183
+ return ask(Q.finite(expr.exp), assumptions)
184
+
185
+ base_bounded = ask(Q.finite(expr.base), assumptions)
186
+ exp_bounded = ask(Q.finite(expr.exp), assumptions)
187
+ if base_bounded is None and exp_bounded is None: # Common Case
188
+ return None
189
+ if base_bounded is False and ask(Q.extended_nonzero(expr.exp), assumptions):
190
+ return False
191
+ if base_bounded and exp_bounded:
192
+ return True
193
+ if (abs(expr.base) <= 1) == True and ask(Q.extended_positive(expr.exp), assumptions):
194
+ return True
195
+ if (abs(expr.base) >= 1) == True and ask(Q.extended_negative(expr.exp), assumptions):
196
+ return True
197
+ if (abs(expr.base) >= 1) == True and exp_bounded is False:
198
+ return False
199
+ return None
200
+
201
+ @FinitePredicate.register(exp)
202
+ def _(expr, assumptions):
203
+ return ask(Q.finite(expr.exp), assumptions)
204
+
205
+ @FinitePredicate.register(log)
206
+ def _(expr, assumptions):
207
+ # After complex -> finite fact is registered to new assumption system,
208
+ # querying Q.infinite may be removed.
209
+ if ask(Q.infinite(expr.args[0]), assumptions):
210
+ return False
211
+ return ask(~Q.zero(expr.args[0]), assumptions)
212
+
213
+ @FinitePredicate.register_many(cos, sin, Number, Pi, Exp1, GoldenRatio,
214
+ TribonacciConstant, ImaginaryUnit, sign)
215
+ def _(expr, assumptions):
216
+ return True
217
+
218
+ @FinitePredicate.register_many(ComplexInfinity, Infinity, NegativeInfinity)
219
+ def _(expr, assumptions):
220
+ return False
221
+
222
+ @FinitePredicate.register(NaN)
223
+ def _(expr, assumptions):
224
+ return None
225
+
226
+
227
+ # InfinitePredicate
228
+
229
+
230
+ @InfinitePredicate.register_many(ComplexInfinity, Infinity, NegativeInfinity)
231
+ def _(expr, assumptions):
232
+ return True
233
+
234
+
235
+ # PositiveInfinitePredicate
236
+
237
+
238
+ @PositiveInfinitePredicate.register(Infinity)
239
+ def _(expr, assumptions):
240
+ return True
241
+
242
+
243
+ @PositiveInfinitePredicate.register_many(NegativeInfinity, ComplexInfinity)
244
+ def _(expr, assumptions):
245
+ return False
246
+
247
+
248
+ # NegativeInfinitePredicate
249
+
250
+
251
+ @NegativeInfinitePredicate.register(NegativeInfinity)
252
+ def _(expr, assumptions):
253
+ return True
254
+
255
+
256
+ @NegativeInfinitePredicate.register_many(Infinity, ComplexInfinity)
257
+ def _(expr, assumptions):
258
+ return False
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/common.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This module defines base class for handlers and some core handlers:
3
+ ``Q.commutative`` and ``Q.is_true``.
4
+ """
5
+
6
+ from sympy.assumptions import Q, ask, AppliedPredicate
7
+ from sympy.core import Basic, Symbol
8
+ from sympy.core.logic import _fuzzy_group
9
+ from sympy.core.numbers import NaN, Number
10
+ from sympy.logic.boolalg import (And, BooleanTrue, BooleanFalse, conjuncts,
11
+ Equivalent, Implies, Not, Or)
12
+ from sympy.utilities.exceptions import sympy_deprecation_warning
13
+
14
+ from ..predicates.common import CommutativePredicate, IsTruePredicate
15
+
16
+
17
+ class AskHandler:
18
+ """Base class that all Ask Handlers must inherit."""
19
+ def __new__(cls, *args, **kwargs):
20
+ sympy_deprecation_warning(
21
+ """
22
+ The AskHandler system is deprecated. The AskHandler class should
23
+ be replaced with the multipledispatch handler of Predicate
24
+ """,
25
+ deprecated_since_version="1.8",
26
+ active_deprecations_target='deprecated-askhandler',
27
+ )
28
+ return super().__new__(cls, *args, **kwargs)
29
+
30
+
31
+ class CommonHandler(AskHandler):
32
+ # Deprecated
33
+ """Defines some useful methods common to most Handlers. """
34
+
35
+ @staticmethod
36
+ def AlwaysTrue(expr, assumptions):
37
+ return True
38
+
39
+ @staticmethod
40
+ def AlwaysFalse(expr, assumptions):
41
+ return False
42
+
43
+ @staticmethod
44
+ def AlwaysNone(expr, assumptions):
45
+ return None
46
+
47
+ NaN = AlwaysFalse
48
+
49
+
50
+ # CommutativePredicate
51
+
52
+ @CommutativePredicate.register(Symbol)
53
+ def _(expr, assumptions):
54
+ """Objects are expected to be commutative unless otherwise stated"""
55
+ assumps = conjuncts(assumptions)
56
+ if expr.is_commutative is not None:
57
+ return expr.is_commutative and not ~Q.commutative(expr) in assumps
58
+ if Q.commutative(expr) in assumps:
59
+ return True
60
+ elif ~Q.commutative(expr) in assumps:
61
+ return False
62
+ return True
63
+
64
+ @CommutativePredicate.register(Basic)
65
+ def _(expr, assumptions):
66
+ for arg in expr.args:
67
+ if not ask(Q.commutative(arg), assumptions):
68
+ return False
69
+ return True
70
+
71
+ @CommutativePredicate.register(Number)
72
+ def _(expr, assumptions):
73
+ return True
74
+
75
+ @CommutativePredicate.register(NaN)
76
+ def _(expr, assumptions):
77
+ return True
78
+
79
+
80
+ # IsTruePredicate
81
+
82
+ @IsTruePredicate.register(bool)
83
+ def _(expr, assumptions):
84
+ return expr
85
+
86
+ @IsTruePredicate.register(BooleanTrue)
87
+ def _(expr, assumptions):
88
+ return True
89
+
90
+ @IsTruePredicate.register(BooleanFalse)
91
+ def _(expr, assumptions):
92
+ return False
93
+
94
+ @IsTruePredicate.register(AppliedPredicate)
95
+ def _(expr, assumptions):
96
+ return ask(expr, assumptions)
97
+
98
+ @IsTruePredicate.register(Not)
99
+ def _(expr, assumptions):
100
+ arg = expr.args[0]
101
+ if arg.is_Symbol:
102
+ # symbol used as abstract boolean object
103
+ return None
104
+ value = ask(arg, assumptions=assumptions)
105
+ if value in (True, False):
106
+ return not value
107
+ else:
108
+ return None
109
+
110
+ @IsTruePredicate.register(Or)
111
+ def _(expr, assumptions):
112
+ result = False
113
+ for arg in expr.args:
114
+ p = ask(arg, assumptions=assumptions)
115
+ if p is True:
116
+ return True
117
+ if p is None:
118
+ result = None
119
+ return result
120
+
121
+ @IsTruePredicate.register(And)
122
+ def _(expr, assumptions):
123
+ result = True
124
+ for arg in expr.args:
125
+ p = ask(arg, assumptions=assumptions)
126
+ if p is False:
127
+ return False
128
+ if p is None:
129
+ result = None
130
+ return result
131
+
132
+ @IsTruePredicate.register(Implies)
133
+ def _(expr, assumptions):
134
+ p, q = expr.args
135
+ return ask(~p | q, assumptions=assumptions)
136
+
137
+ @IsTruePredicate.register(Equivalent)
138
+ def _(expr, assumptions):
139
+ p, q = expr.args
140
+ pt = ask(p, assumptions=assumptions)
141
+ if pt is None:
142
+ return None
143
+ qt = ask(q, assumptions=assumptions)
144
+ if qt is None:
145
+ return None
146
+ return pt == qt
147
+
148
+
149
+ #### Helper methods
150
+ def test_closed_group(expr, assumptions, key):
151
+ """
152
+ Test for membership in a group with respect
153
+ to the current operation.
154
+ """
155
+ return _fuzzy_group(
156
+ (ask(key(a), assumptions) for a in expr.args), quick_exit=True)
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/matrices.py ADDED
@@ -0,0 +1,716 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This module contains query handlers responsible for Matrices queries:
3
+ Square, Symmetric, Invertible etc.
4
+ """
5
+
6
+ from sympy.logic.boolalg import conjuncts
7
+ from sympy.assumptions import Q, ask
8
+ from sympy.assumptions.handlers import test_closed_group
9
+ from sympy.matrices import MatrixBase
10
+ from sympy.matrices.expressions import (BlockMatrix, BlockDiagMatrix, Determinant,
11
+ DiagMatrix, DiagonalMatrix, HadamardProduct, Identity, Inverse, MatAdd, MatMul,
12
+ MatPow, MatrixExpr, MatrixSlice, MatrixSymbol, OneMatrix, Trace, Transpose,
13
+ ZeroMatrix)
14
+ from sympy.matrices.expressions.blockmatrix import reblock_2x2
15
+ from sympy.matrices.expressions.factorizations import Factorization
16
+ from sympy.matrices.expressions.fourier import DFT
17
+ from sympy.core.logic import fuzzy_and
18
+ from sympy.utilities.iterables import sift
19
+ from sympy.core import Basic
20
+
21
+ from ..predicates.matrices import (SquarePredicate, SymmetricPredicate,
22
+ InvertiblePredicate, OrthogonalPredicate, UnitaryPredicate,
23
+ FullRankPredicate, PositiveDefinitePredicate, UpperTriangularPredicate,
24
+ LowerTriangularPredicate, DiagonalPredicate, IntegerElementsPredicate,
25
+ RealElementsPredicate, ComplexElementsPredicate)
26
+
27
+
28
+ def _Factorization(predicate, expr, assumptions):
29
+ if predicate in expr.predicates:
30
+ return True
31
+
32
+
33
+ # SquarePredicate
34
+
35
+ @SquarePredicate.register(MatrixExpr)
36
+ def _(expr, assumptions):
37
+ return expr.shape[0] == expr.shape[1]
38
+
39
+
40
+ # SymmetricPredicate
41
+
42
+ @SymmetricPredicate.register(MatMul)
43
+ def _(expr, assumptions):
44
+ factor, mmul = expr.as_coeff_mmul()
45
+ if all(ask(Q.symmetric(arg), assumptions) for arg in mmul.args):
46
+ return True
47
+ # TODO: implement sathandlers system for the matrices.
48
+ # Now it duplicates the general fact: Implies(Q.diagonal, Q.symmetric).
49
+ if ask(Q.diagonal(expr), assumptions):
50
+ return True
51
+ if len(mmul.args) >= 2 and mmul.args[0] == mmul.args[-1].T:
52
+ if len(mmul.args) == 2:
53
+ return True
54
+ return ask(Q.symmetric(MatMul(*mmul.args[1:-1])), assumptions)
55
+
56
+ @SymmetricPredicate.register(MatPow)
57
+ def _(expr, assumptions):
58
+ # only for integer powers
59
+ base, exp = expr.args
60
+ int_exp = ask(Q.integer(exp), assumptions)
61
+ if not int_exp:
62
+ return None
63
+ non_negative = ask(~Q.negative(exp), assumptions)
64
+ if (non_negative or non_negative == False
65
+ and ask(Q.invertible(base), assumptions)):
66
+ return ask(Q.symmetric(base), assumptions)
67
+ return None
68
+
69
+ @SymmetricPredicate.register(MatAdd)
70
+ def _(expr, assumptions):
71
+ return all(ask(Q.symmetric(arg), assumptions) for arg in expr.args)
72
+
73
+ @SymmetricPredicate.register(MatrixSymbol)
74
+ def _(expr, assumptions):
75
+ if not expr.is_square:
76
+ return False
77
+ # TODO: implement sathandlers system for the matrices.
78
+ # Now it duplicates the general fact: Implies(Q.diagonal, Q.symmetric).
79
+ if ask(Q.diagonal(expr), assumptions):
80
+ return True
81
+ if Q.symmetric(expr) in conjuncts(assumptions):
82
+ return True
83
+
84
+ @SymmetricPredicate.register_many(OneMatrix, ZeroMatrix)
85
+ def _(expr, assumptions):
86
+ return ask(Q.square(expr), assumptions)
87
+
88
+ @SymmetricPredicate.register_many(Inverse, Transpose)
89
+ def _(expr, assumptions):
90
+ return ask(Q.symmetric(expr.arg), assumptions)
91
+
92
+ @SymmetricPredicate.register(MatrixSlice)
93
+ def _(expr, assumptions):
94
+ # TODO: implement sathandlers system for the matrices.
95
+ # Now it duplicates the general fact: Implies(Q.diagonal, Q.symmetric).
96
+ if ask(Q.diagonal(expr), assumptions):
97
+ return True
98
+ if not expr.on_diag:
99
+ return None
100
+ else:
101
+ return ask(Q.symmetric(expr.parent), assumptions)
102
+
103
+ @SymmetricPredicate.register(Identity)
104
+ def _(expr, assumptions):
105
+ return True
106
+
107
+
108
+ # InvertiblePredicate
109
+
110
+ @InvertiblePredicate.register(MatMul)
111
+ def _(expr, assumptions):
112
+ factor, mmul = expr.as_coeff_mmul()
113
+ if all(ask(Q.invertible(arg), assumptions) for arg in mmul.args):
114
+ return True
115
+ if any(ask(Q.invertible(arg), assumptions) is False
116
+ for arg in mmul.args):
117
+ return False
118
+
119
+ @InvertiblePredicate.register(MatPow)
120
+ def _(expr, assumptions):
121
+ # only for integer powers
122
+ base, exp = expr.args
123
+ int_exp = ask(Q.integer(exp), assumptions)
124
+ if not int_exp:
125
+ return None
126
+ if exp.is_negative == False:
127
+ return ask(Q.invertible(base), assumptions)
128
+ return None
129
+
130
+ @InvertiblePredicate.register(MatAdd)
131
+ def _(expr, assumptions):
132
+ return None
133
+
134
+ @InvertiblePredicate.register(MatrixSymbol)
135
+ def _(expr, assumptions):
136
+ if not expr.is_square:
137
+ return False
138
+ if Q.invertible(expr) in conjuncts(assumptions):
139
+ return True
140
+
141
+ @InvertiblePredicate.register_many(Identity, Inverse)
142
+ def _(expr, assumptions):
143
+ return True
144
+
145
+ @InvertiblePredicate.register(ZeroMatrix)
146
+ def _(expr, assumptions):
147
+ return False
148
+
149
+ @InvertiblePredicate.register(OneMatrix)
150
+ def _(expr, assumptions):
151
+ return expr.shape[0] == 1 and expr.shape[1] == 1
152
+
153
+ @InvertiblePredicate.register(Transpose)
154
+ def _(expr, assumptions):
155
+ return ask(Q.invertible(expr.arg), assumptions)
156
+
157
+ @InvertiblePredicate.register(MatrixSlice)
158
+ def _(expr, assumptions):
159
+ if not expr.on_diag:
160
+ return None
161
+ else:
162
+ return ask(Q.invertible(expr.parent), assumptions)
163
+
164
+ @InvertiblePredicate.register(MatrixBase)
165
+ def _(expr, assumptions):
166
+ if not expr.is_square:
167
+ return False
168
+ return expr.rank() == expr.rows
169
+
170
+ @InvertiblePredicate.register(MatrixExpr)
171
+ def _(expr, assumptions):
172
+ if not expr.is_square:
173
+ return False
174
+ return None
175
+
176
+ @InvertiblePredicate.register(BlockMatrix)
177
+ def _(expr, assumptions):
178
+ if not expr.is_square:
179
+ return False
180
+ if expr.blockshape == (1, 1):
181
+ return ask(Q.invertible(expr.blocks[0, 0]), assumptions)
182
+ expr = reblock_2x2(expr)
183
+ if expr.blockshape == (2, 2):
184
+ [[A, B], [C, D]] = expr.blocks.tolist()
185
+ if ask(Q.invertible(A), assumptions) == True:
186
+ invertible = ask(Q.invertible(D - C * A.I * B), assumptions)
187
+ if invertible is not None:
188
+ return invertible
189
+ if ask(Q.invertible(B), assumptions) == True:
190
+ invertible = ask(Q.invertible(C - D * B.I * A), assumptions)
191
+ if invertible is not None:
192
+ return invertible
193
+ if ask(Q.invertible(C), assumptions) == True:
194
+ invertible = ask(Q.invertible(B - A * C.I * D), assumptions)
195
+ if invertible is not None:
196
+ return invertible
197
+ if ask(Q.invertible(D), assumptions) == True:
198
+ invertible = ask(Q.invertible(A - B * D.I * C), assumptions)
199
+ if invertible is not None:
200
+ return invertible
201
+ return None
202
+
203
+ @InvertiblePredicate.register(BlockDiagMatrix)
204
+ def _(expr, assumptions):
205
+ if expr.rowblocksizes != expr.colblocksizes:
206
+ return None
207
+ return fuzzy_and([ask(Q.invertible(a), assumptions) for a in expr.diag])
208
+
209
+
210
+ # OrthogonalPredicate
211
+
212
+ @OrthogonalPredicate.register(MatMul)
213
+ def _(expr, assumptions):
214
+ factor, mmul = expr.as_coeff_mmul()
215
+ if (all(ask(Q.orthogonal(arg), assumptions) for arg in mmul.args) and
216
+ factor == 1):
217
+ return True
218
+ if any(ask(Q.invertible(arg), assumptions) is False
219
+ for arg in mmul.args):
220
+ return False
221
+
222
+ @OrthogonalPredicate.register(MatPow)
223
+ def _(expr, assumptions):
224
+ # only for integer powers
225
+ base, exp = expr.args
226
+ int_exp = ask(Q.integer(exp), assumptions)
227
+ if int_exp:
228
+ return ask(Q.orthogonal(base), assumptions)
229
+ return None
230
+
231
+ @OrthogonalPredicate.register(MatAdd)
232
+ def _(expr, assumptions):
233
+ if (len(expr.args) == 1 and
234
+ ask(Q.orthogonal(expr.args[0]), assumptions)):
235
+ return True
236
+
237
+ @OrthogonalPredicate.register(MatrixSymbol)
238
+ def _(expr, assumptions):
239
+ if (not expr.is_square or
240
+ ask(Q.invertible(expr), assumptions) is False):
241
+ return False
242
+ if Q.orthogonal(expr) in conjuncts(assumptions):
243
+ return True
244
+
245
+ @OrthogonalPredicate.register(Identity)
246
+ def _(expr, assumptions):
247
+ return True
248
+
249
+ @OrthogonalPredicate.register(ZeroMatrix)
250
+ def _(expr, assumptions):
251
+ return False
252
+
253
+ @OrthogonalPredicate.register_many(Inverse, Transpose)
254
+ def _(expr, assumptions):
255
+ return ask(Q.orthogonal(expr.arg), assumptions)
256
+
257
+ @OrthogonalPredicate.register(MatrixSlice)
258
+ def _(expr, assumptions):
259
+ if not expr.on_diag:
260
+ return None
261
+ else:
262
+ return ask(Q.orthogonal(expr.parent), assumptions)
263
+
264
+ @OrthogonalPredicate.register(Factorization)
265
+ def _(expr, assumptions):
266
+ return _Factorization(Q.orthogonal, expr, assumptions)
267
+
268
+
269
+ # UnitaryPredicate
270
+
271
+ @UnitaryPredicate.register(MatMul)
272
+ def _(expr, assumptions):
273
+ factor, mmul = expr.as_coeff_mmul()
274
+ if (all(ask(Q.unitary(arg), assumptions) for arg in mmul.args) and
275
+ abs(factor) == 1):
276
+ return True
277
+ if any(ask(Q.invertible(arg), assumptions) is False
278
+ for arg in mmul.args):
279
+ return False
280
+
281
+ @UnitaryPredicate.register(MatPow)
282
+ def _(expr, assumptions):
283
+ # only for integer powers
284
+ base, exp = expr.args
285
+ int_exp = ask(Q.integer(exp), assumptions)
286
+ if int_exp:
287
+ return ask(Q.unitary(base), assumptions)
288
+ return None
289
+
290
+ @UnitaryPredicate.register(MatrixSymbol)
291
+ def _(expr, assumptions):
292
+ if (not expr.is_square or
293
+ ask(Q.invertible(expr), assumptions) is False):
294
+ return False
295
+ if Q.unitary(expr) in conjuncts(assumptions):
296
+ return True
297
+
298
+ @UnitaryPredicate.register_many(Inverse, Transpose)
299
+ def _(expr, assumptions):
300
+ return ask(Q.unitary(expr.arg), assumptions)
301
+
302
+ @UnitaryPredicate.register(MatrixSlice)
303
+ def _(expr, assumptions):
304
+ if not expr.on_diag:
305
+ return None
306
+ else:
307
+ return ask(Q.unitary(expr.parent), assumptions)
308
+
309
+ @UnitaryPredicate.register_many(DFT, Identity)
310
+ def _(expr, assumptions):
311
+ return True
312
+
313
+ @UnitaryPredicate.register(ZeroMatrix)
314
+ def _(expr, assumptions):
315
+ return False
316
+
317
+ @UnitaryPredicate.register(Factorization)
318
+ def _(expr, assumptions):
319
+ return _Factorization(Q.unitary, expr, assumptions)
320
+
321
+
322
+ # FullRankPredicate
323
+
324
+ @FullRankPredicate.register(MatMul)
325
+ def _(expr, assumptions):
326
+ if all(ask(Q.fullrank(arg), assumptions) for arg in expr.args):
327
+ return True
328
+
329
+ @FullRankPredicate.register(MatPow)
330
+ def _(expr, assumptions):
331
+ # only for integer powers
332
+ base, exp = expr.args
333
+ int_exp = ask(Q.integer(exp), assumptions)
334
+ if int_exp and ask(~Q.negative(exp), assumptions):
335
+ return ask(Q.fullrank(base), assumptions)
336
+ return None
337
+
338
+ @FullRankPredicate.register(Identity)
339
+ def _(expr, assumptions):
340
+ return True
341
+
342
+ @FullRankPredicate.register(ZeroMatrix)
343
+ def _(expr, assumptions):
344
+ return False
345
+
346
+ @FullRankPredicate.register(OneMatrix)
347
+ def _(expr, assumptions):
348
+ return expr.shape[0] == 1 and expr.shape[1] == 1
349
+
350
+ @FullRankPredicate.register_many(Inverse, Transpose)
351
+ def _(expr, assumptions):
352
+ return ask(Q.fullrank(expr.arg), assumptions)
353
+
354
+ @FullRankPredicate.register(MatrixSlice)
355
+ def _(expr, assumptions):
356
+ if ask(Q.orthogonal(expr.parent), assumptions):
357
+ return True
358
+
359
+
360
+ # PositiveDefinitePredicate
361
+
362
+ @PositiveDefinitePredicate.register(MatMul)
363
+ def _(expr, assumptions):
364
+ factor, mmul = expr.as_coeff_mmul()
365
+ if (all(ask(Q.positive_definite(arg), assumptions)
366
+ for arg in mmul.args) and factor > 0):
367
+ return True
368
+ if (len(mmul.args) >= 2
369
+ and mmul.args[0] == mmul.args[-1].T
370
+ and ask(Q.fullrank(mmul.args[0]), assumptions)):
371
+ return ask(Q.positive_definite(
372
+ MatMul(*mmul.args[1:-1])), assumptions)
373
+
374
+ @PositiveDefinitePredicate.register(MatPow)
375
+ def _(expr, assumptions):
376
+ # a power of a positive definite matrix is positive definite
377
+ if ask(Q.positive_definite(expr.args[0]), assumptions):
378
+ return True
379
+
380
+ @PositiveDefinitePredicate.register(MatAdd)
381
+ def _(expr, assumptions):
382
+ if all(ask(Q.positive_definite(arg), assumptions)
383
+ for arg in expr.args):
384
+ return True
385
+
386
+ @PositiveDefinitePredicate.register(MatrixSymbol)
387
+ def _(expr, assumptions):
388
+ if not expr.is_square:
389
+ return False
390
+ if Q.positive_definite(expr) in conjuncts(assumptions):
391
+ return True
392
+
393
+ @PositiveDefinitePredicate.register(Identity)
394
+ def _(expr, assumptions):
395
+ return True
396
+
397
+ @PositiveDefinitePredicate.register(ZeroMatrix)
398
+ def _(expr, assumptions):
399
+ return False
400
+
401
+ @PositiveDefinitePredicate.register(OneMatrix)
402
+ def _(expr, assumptions):
403
+ return expr.shape[0] == 1 and expr.shape[1] == 1
404
+
405
+ @PositiveDefinitePredicate.register_many(Inverse, Transpose)
406
+ def _(expr, assumptions):
407
+ return ask(Q.positive_definite(expr.arg), assumptions)
408
+
409
+ @PositiveDefinitePredicate.register(MatrixSlice)
410
+ def _(expr, assumptions):
411
+ if not expr.on_diag:
412
+ return None
413
+ else:
414
+ return ask(Q.positive_definite(expr.parent), assumptions)
415
+
416
+
417
+ # UpperTriangularPredicate
418
+
419
+ @UpperTriangularPredicate.register(MatMul)
420
+ def _(expr, assumptions):
421
+ factor, matrices = expr.as_coeff_matrices()
422
+ if all(ask(Q.upper_triangular(m), assumptions) for m in matrices):
423
+ return True
424
+
425
+ @UpperTriangularPredicate.register(MatAdd)
426
+ def _(expr, assumptions):
427
+ if all(ask(Q.upper_triangular(arg), assumptions) for arg in expr.args):
428
+ return True
429
+
430
+ @UpperTriangularPredicate.register(MatPow)
431
+ def _(expr, assumptions):
432
+ # only for integer powers
433
+ base, exp = expr.args
434
+ int_exp = ask(Q.integer(exp), assumptions)
435
+ if not int_exp:
436
+ return None
437
+ non_negative = ask(~Q.negative(exp), assumptions)
438
+ if (non_negative or non_negative == False
439
+ and ask(Q.invertible(base), assumptions)):
440
+ return ask(Q.upper_triangular(base), assumptions)
441
+ return None
442
+
443
+ @UpperTriangularPredicate.register(MatrixSymbol)
444
+ def _(expr, assumptions):
445
+ if Q.upper_triangular(expr) in conjuncts(assumptions):
446
+ return True
447
+
448
+ @UpperTriangularPredicate.register_many(Identity, ZeroMatrix)
449
+ def _(expr, assumptions):
450
+ return True
451
+
452
+ @UpperTriangularPredicate.register(OneMatrix)
453
+ def _(expr, assumptions):
454
+ return expr.shape[0] == 1 and expr.shape[1] == 1
455
+
456
+ @UpperTriangularPredicate.register(Transpose)
457
+ def _(expr, assumptions):
458
+ return ask(Q.lower_triangular(expr.arg), assumptions)
459
+
460
+ @UpperTriangularPredicate.register(Inverse)
461
+ def _(expr, assumptions):
462
+ return ask(Q.upper_triangular(expr.arg), assumptions)
463
+
464
+ @UpperTriangularPredicate.register(MatrixSlice)
465
+ def _(expr, assumptions):
466
+ if not expr.on_diag:
467
+ return None
468
+ else:
469
+ return ask(Q.upper_triangular(expr.parent), assumptions)
470
+
471
+ @UpperTriangularPredicate.register(Factorization)
472
+ def _(expr, assumptions):
473
+ return _Factorization(Q.upper_triangular, expr, assumptions)
474
+
475
+ # LowerTriangularPredicate
476
+
477
+ @LowerTriangularPredicate.register(MatMul)
478
+ def _(expr, assumptions):
479
+ factor, matrices = expr.as_coeff_matrices()
480
+ if all(ask(Q.lower_triangular(m), assumptions) for m in matrices):
481
+ return True
482
+
483
+ @LowerTriangularPredicate.register(MatAdd)
484
+ def _(expr, assumptions):
485
+ if all(ask(Q.lower_triangular(arg), assumptions) for arg in expr.args):
486
+ return True
487
+
488
+ @LowerTriangularPredicate.register(MatPow)
489
+ def _(expr, assumptions):
490
+ # only for integer powers
491
+ base, exp = expr.args
492
+ int_exp = ask(Q.integer(exp), assumptions)
493
+ if not int_exp:
494
+ return None
495
+ non_negative = ask(~Q.negative(exp), assumptions)
496
+ if (non_negative or non_negative == False
497
+ and ask(Q.invertible(base), assumptions)):
498
+ return ask(Q.lower_triangular(base), assumptions)
499
+ return None
500
+
501
+ @LowerTriangularPredicate.register(MatrixSymbol)
502
+ def _(expr, assumptions):
503
+ if Q.lower_triangular(expr) in conjuncts(assumptions):
504
+ return True
505
+
506
+ @LowerTriangularPredicate.register_many(Identity, ZeroMatrix)
507
+ def _(expr, assumptions):
508
+ return True
509
+
510
+ @LowerTriangularPredicate.register(OneMatrix)
511
+ def _(expr, assumptions):
512
+ return expr.shape[0] == 1 and expr.shape[1] == 1
513
+
514
+ @LowerTriangularPredicate.register(Transpose)
515
+ def _(expr, assumptions):
516
+ return ask(Q.upper_triangular(expr.arg), assumptions)
517
+
518
+ @LowerTriangularPredicate.register(Inverse)
519
+ def _(expr, assumptions):
520
+ return ask(Q.lower_triangular(expr.arg), assumptions)
521
+
522
+ @LowerTriangularPredicate.register(MatrixSlice)
523
+ def _(expr, assumptions):
524
+ if not expr.on_diag:
525
+ return None
526
+ else:
527
+ return ask(Q.lower_triangular(expr.parent), assumptions)
528
+
529
+ @LowerTriangularPredicate.register(Factorization)
530
+ def _(expr, assumptions):
531
+ return _Factorization(Q.lower_triangular, expr, assumptions)
532
+
533
+
534
+ # DiagonalPredicate
535
+
536
+ def _is_empty_or_1x1(expr):
537
+ return expr.shape in ((0, 0), (1, 1))
538
+
539
+ @DiagonalPredicate.register(MatMul)
540
+ def _(expr, assumptions):
541
+ if _is_empty_or_1x1(expr):
542
+ return True
543
+ factor, matrices = expr.as_coeff_matrices()
544
+ if all(ask(Q.diagonal(m), assumptions) for m in matrices):
545
+ return True
546
+
547
+ @DiagonalPredicate.register(MatPow)
548
+ def _(expr, assumptions):
549
+ # only for integer powers
550
+ base, exp = expr.args
551
+ int_exp = ask(Q.integer(exp), assumptions)
552
+ if not int_exp:
553
+ return None
554
+ non_negative = ask(~Q.negative(exp), assumptions)
555
+ if (non_negative or non_negative == False
556
+ and ask(Q.invertible(base), assumptions)):
557
+ return ask(Q.diagonal(base), assumptions)
558
+ return None
559
+
560
+ @DiagonalPredicate.register(MatAdd)
561
+ def _(expr, assumptions):
562
+ if all(ask(Q.diagonal(arg), assumptions) for arg in expr.args):
563
+ return True
564
+
565
+ @DiagonalPredicate.register(MatrixSymbol)
566
+ def _(expr, assumptions):
567
+ if _is_empty_or_1x1(expr):
568
+ return True
569
+ if Q.diagonal(expr) in conjuncts(assumptions):
570
+ return True
571
+
572
+ @DiagonalPredicate.register(OneMatrix)
573
+ def _(expr, assumptions):
574
+ return expr.shape[0] == 1 and expr.shape[1] == 1
575
+
576
+ @DiagonalPredicate.register_many(Inverse, Transpose)
577
+ def _(expr, assumptions):
578
+ return ask(Q.diagonal(expr.arg), assumptions)
579
+
580
+ @DiagonalPredicate.register(MatrixSlice)
581
+ def _(expr, assumptions):
582
+ if _is_empty_or_1x1(expr):
583
+ return True
584
+ if not expr.on_diag:
585
+ return None
586
+ else:
587
+ return ask(Q.diagonal(expr.parent), assumptions)
588
+
589
+ @DiagonalPredicate.register_many(DiagonalMatrix, DiagMatrix, Identity, ZeroMatrix)
590
+ def _(expr, assumptions):
591
+ return True
592
+
593
+ @DiagonalPredicate.register(Factorization)
594
+ def _(expr, assumptions):
595
+ return _Factorization(Q.diagonal, expr, assumptions)
596
+
597
+
598
+ # IntegerElementsPredicate
599
+
600
+ def BM_elements(predicate, expr, assumptions):
601
+ """ Block Matrix elements. """
602
+ return all(ask(predicate(b), assumptions) for b in expr.blocks)
603
+
604
+ def MS_elements(predicate, expr, assumptions):
605
+ """ Matrix Slice elements. """
606
+ return ask(predicate(expr.parent), assumptions)
607
+
608
+ def MatMul_elements(matrix_predicate, scalar_predicate, expr, assumptions):
609
+ d = sift(expr.args, lambda x: isinstance(x, MatrixExpr))
610
+ factors, matrices = d[False], d[True]
611
+ return fuzzy_and([
612
+ test_closed_group(Basic(*factors), assumptions, scalar_predicate),
613
+ test_closed_group(Basic(*matrices), assumptions, matrix_predicate)])
614
+
615
+
616
+ @IntegerElementsPredicate.register_many(Determinant, HadamardProduct, MatAdd,
617
+ Trace, Transpose)
618
+ def _(expr, assumptions):
619
+ return test_closed_group(expr, assumptions, Q.integer_elements)
620
+
621
+ @IntegerElementsPredicate.register(MatPow)
622
+ def _(expr, assumptions):
623
+ # only for integer powers
624
+ base, exp = expr.args
625
+ int_exp = ask(Q.integer(exp), assumptions)
626
+ if not int_exp:
627
+ return None
628
+ if exp.is_negative == False:
629
+ return ask(Q.integer_elements(base), assumptions)
630
+ return None
631
+
632
+ @IntegerElementsPredicate.register_many(Identity, OneMatrix, ZeroMatrix)
633
+ def _(expr, assumptions):
634
+ return True
635
+
636
+ @IntegerElementsPredicate.register(MatMul)
637
+ def _(expr, assumptions):
638
+ return MatMul_elements(Q.integer_elements, Q.integer, expr, assumptions)
639
+
640
+ @IntegerElementsPredicate.register(MatrixSlice)
641
+ def _(expr, assumptions):
642
+ return MS_elements(Q.integer_elements, expr, assumptions)
643
+
644
+ @IntegerElementsPredicate.register(BlockMatrix)
645
+ def _(expr, assumptions):
646
+ return BM_elements(Q.integer_elements, expr, assumptions)
647
+
648
+
649
+ # RealElementsPredicate
650
+
651
+ @RealElementsPredicate.register_many(Determinant, Factorization, HadamardProduct,
652
+ MatAdd, Trace, Transpose)
653
+ def _(expr, assumptions):
654
+ return test_closed_group(expr, assumptions, Q.real_elements)
655
+
656
+ @RealElementsPredicate.register(MatPow)
657
+ def _(expr, assumptions):
658
+ # only for integer powers
659
+ base, exp = expr.args
660
+ int_exp = ask(Q.integer(exp), assumptions)
661
+ if not int_exp:
662
+ return None
663
+ non_negative = ask(~Q.negative(exp), assumptions)
664
+ if (non_negative or non_negative == False
665
+ and ask(Q.invertible(base), assumptions)):
666
+ return ask(Q.real_elements(base), assumptions)
667
+ return None
668
+
669
+ @RealElementsPredicate.register(MatMul)
670
+ def _(expr, assumptions):
671
+ return MatMul_elements(Q.real_elements, Q.real, expr, assumptions)
672
+
673
+ @RealElementsPredicate.register(MatrixSlice)
674
+ def _(expr, assumptions):
675
+ return MS_elements(Q.real_elements, expr, assumptions)
676
+
677
+ @RealElementsPredicate.register(BlockMatrix)
678
+ def _(expr, assumptions):
679
+ return BM_elements(Q.real_elements, expr, assumptions)
680
+
681
+
682
+ # ComplexElementsPredicate
683
+
684
+ @ComplexElementsPredicate.register_many(Determinant, Factorization, HadamardProduct,
685
+ Inverse, MatAdd, Trace, Transpose)
686
+ def _(expr, assumptions):
687
+ return test_closed_group(expr, assumptions, Q.complex_elements)
688
+
689
+ @ComplexElementsPredicate.register(MatPow)
690
+ def _(expr, assumptions):
691
+ # only for integer powers
692
+ base, exp = expr.args
693
+ int_exp = ask(Q.integer(exp), assumptions)
694
+ if not int_exp:
695
+ return None
696
+ non_negative = ask(~Q.negative(exp), assumptions)
697
+ if (non_negative or non_negative == False
698
+ and ask(Q.invertible(base), assumptions)):
699
+ return ask(Q.complex_elements(base), assumptions)
700
+ return None
701
+
702
+ @ComplexElementsPredicate.register(MatMul)
703
+ def _(expr, assumptions):
704
+ return MatMul_elements(Q.complex_elements, Q.complex, expr, assumptions)
705
+
706
+ @ComplexElementsPredicate.register(MatrixSlice)
707
+ def _(expr, assumptions):
708
+ return MS_elements(Q.complex_elements, expr, assumptions)
709
+
710
+ @ComplexElementsPredicate.register(BlockMatrix)
711
+ def _(expr, assumptions):
712
+ return BM_elements(Q.complex_elements, expr, assumptions)
713
+
714
+ @ComplexElementsPredicate.register(DFT)
715
+ def _(expr, assumptions):
716
+ return True
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/ntheory.py ADDED
@@ -0,0 +1,267 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Handlers for keys related to number theory: prime, even, odd, etc.
3
+ """
4
+
5
+ from sympy.assumptions import Q, ask
6
+ from sympy.core import Add, Basic, Expr, Float, Mul, Pow, S
7
+ from sympy.core.numbers import (ImaginaryUnit, Infinity, Integer, NaN,
8
+ NegativeInfinity, NumberSymbol, Rational)
9
+ from sympy.functions import Abs, im, re
10
+ from sympy.ntheory import isprime
11
+
12
+ from sympy.multipledispatch import MDNotImplementedError
13
+
14
+ from ..predicates.ntheory import (PrimePredicate, CompositePredicate,
15
+ EvenPredicate, OddPredicate)
16
+
17
+
18
+ # PrimePredicate
19
+
20
+ def _PrimePredicate_number(expr, assumptions):
21
+ # helper method
22
+ exact = not expr.atoms(Float)
23
+ try:
24
+ i = int(expr.round())
25
+ if (expr - i).equals(0) is False:
26
+ raise TypeError
27
+ except TypeError:
28
+ return False
29
+ if exact:
30
+ return isprime(i)
31
+ # when not exact, we won't give a True or False
32
+ # since the number represents an approximate value
33
+
34
+ @PrimePredicate.register(Expr)
35
+ def _(expr, assumptions):
36
+ ret = expr.is_prime
37
+ if ret is None:
38
+ raise MDNotImplementedError
39
+ return ret
40
+
41
+ @PrimePredicate.register(Basic)
42
+ def _(expr, assumptions):
43
+ if expr.is_number:
44
+ return _PrimePredicate_number(expr, assumptions)
45
+
46
+ @PrimePredicate.register(Mul)
47
+ def _(expr, assumptions):
48
+ if expr.is_number:
49
+ return _PrimePredicate_number(expr, assumptions)
50
+ for arg in expr.args:
51
+ if not ask(Q.integer(arg), assumptions):
52
+ return None
53
+ for arg in expr.args:
54
+ if arg.is_number and arg.is_composite:
55
+ return False
56
+
57
+ @PrimePredicate.register(Pow)
58
+ def _(expr, assumptions):
59
+ """
60
+ Integer**Integer -> !Prime
61
+ """
62
+ if expr.is_number:
63
+ return _PrimePredicate_number(expr, assumptions)
64
+ if ask(Q.integer(expr.exp), assumptions) and \
65
+ ask(Q.integer(expr.base), assumptions):
66
+ return False
67
+
68
+ @PrimePredicate.register(Integer)
69
+ def _(expr, assumptions):
70
+ return isprime(expr)
71
+
72
+ @PrimePredicate.register_many(Rational, Infinity, NegativeInfinity, ImaginaryUnit)
73
+ def _(expr, assumptions):
74
+ return False
75
+
76
+ @PrimePredicate.register(Float)
77
+ def _(expr, assumptions):
78
+ return _PrimePredicate_number(expr, assumptions)
79
+
80
+ @PrimePredicate.register(NumberSymbol)
81
+ def _(expr, assumptions):
82
+ return _PrimePredicate_number(expr, assumptions)
83
+
84
+ @PrimePredicate.register(NaN)
85
+ def _(expr, assumptions):
86
+ return None
87
+
88
+
89
+ # CompositePredicate
90
+
91
+ @CompositePredicate.register(Expr)
92
+ def _(expr, assumptions):
93
+ ret = expr.is_composite
94
+ if ret is None:
95
+ raise MDNotImplementedError
96
+ return ret
97
+
98
+ @CompositePredicate.register(Basic)
99
+ def _(expr, assumptions):
100
+ _positive = ask(Q.positive(expr), assumptions)
101
+ if _positive:
102
+ _integer = ask(Q.integer(expr), assumptions)
103
+ if _integer:
104
+ _prime = ask(Q.prime(expr), assumptions)
105
+ if _prime is None:
106
+ return
107
+ # Positive integer which is not prime is not
108
+ # necessarily composite
109
+ if expr.equals(1):
110
+ return False
111
+ return not _prime
112
+ else:
113
+ return _integer
114
+ else:
115
+ return _positive
116
+
117
+
118
+ # EvenPredicate
119
+
120
+ def _EvenPredicate_number(expr, assumptions):
121
+ # helper method
122
+ try:
123
+ i = int(expr.round())
124
+ if not (expr - i).equals(0):
125
+ raise TypeError
126
+ except TypeError:
127
+ return False
128
+ if isinstance(expr, (float, Float)):
129
+ return False
130
+ return i % 2 == 0
131
+
132
+ @EvenPredicate.register(Expr)
133
+ def _(expr, assumptions):
134
+ ret = expr.is_even
135
+ if ret is None:
136
+ raise MDNotImplementedError
137
+ return ret
138
+
139
+ @EvenPredicate.register(Basic)
140
+ def _(expr, assumptions):
141
+ if expr.is_number:
142
+ return _EvenPredicate_number(expr, assumptions)
143
+
144
+ @EvenPredicate.register(Mul)
145
+ def _(expr, assumptions):
146
+ """
147
+ Even * Integer -> Even
148
+ Even * Odd -> Even
149
+ Integer * Odd -> ?
150
+ Odd * Odd -> Odd
151
+ Even * Even -> Even
152
+ Integer * Integer -> Even if Integer + Integer = Odd
153
+ otherwise -> ?
154
+ """
155
+ if expr.is_number:
156
+ return _EvenPredicate_number(expr, assumptions)
157
+ even, odd, irrational, acc = False, 0, False, 1
158
+ for arg in expr.args:
159
+ # check for all integers and at least one even
160
+ if ask(Q.integer(arg), assumptions):
161
+ if ask(Q.even(arg), assumptions):
162
+ even = True
163
+ elif ask(Q.odd(arg), assumptions):
164
+ odd += 1
165
+ elif not even and acc != 1:
166
+ if ask(Q.odd(acc + arg), assumptions):
167
+ even = True
168
+ elif ask(Q.irrational(arg), assumptions):
169
+ # one irrational makes the result False
170
+ # two makes it undefined
171
+ if irrational:
172
+ break
173
+ irrational = True
174
+ else:
175
+ break
176
+ acc = arg
177
+ else:
178
+ if irrational:
179
+ return False
180
+ if even:
181
+ return True
182
+ if odd == len(expr.args):
183
+ return False
184
+
185
+ @EvenPredicate.register(Add)
186
+ def _(expr, assumptions):
187
+ """
188
+ Even + Odd -> Odd
189
+ Even + Even -> Even
190
+ Odd + Odd -> Even
191
+
192
+ """
193
+ if expr.is_number:
194
+ return _EvenPredicate_number(expr, assumptions)
195
+ _result = True
196
+ for arg in expr.args:
197
+ if ask(Q.even(arg), assumptions):
198
+ pass
199
+ elif ask(Q.odd(arg), assumptions):
200
+ _result = not _result
201
+ else:
202
+ break
203
+ else:
204
+ return _result
205
+
206
+ @EvenPredicate.register(Pow)
207
+ def _(expr, assumptions):
208
+ if expr.is_number:
209
+ return _EvenPredicate_number(expr, assumptions)
210
+ if ask(Q.integer(expr.exp), assumptions):
211
+ if ask(Q.positive(expr.exp), assumptions):
212
+ return ask(Q.even(expr.base), assumptions)
213
+ elif ask(~Q.negative(expr.exp) & Q.odd(expr.base), assumptions):
214
+ return False
215
+ elif expr.base is S.NegativeOne:
216
+ return False
217
+
218
+ @EvenPredicate.register(Integer)
219
+ def _(expr, assumptions):
220
+ return not bool(expr.p & 1)
221
+
222
+ @EvenPredicate.register_many(Rational, Infinity, NegativeInfinity, ImaginaryUnit)
223
+ def _(expr, assumptions):
224
+ return False
225
+
226
+ @EvenPredicate.register(NumberSymbol)
227
+ def _(expr, assumptions):
228
+ return _EvenPredicate_number(expr, assumptions)
229
+
230
+ @EvenPredicate.register(Abs)
231
+ def _(expr, assumptions):
232
+ if ask(Q.real(expr.args[0]), assumptions):
233
+ return ask(Q.even(expr.args[0]), assumptions)
234
+
235
+ @EvenPredicate.register(re)
236
+ def _(expr, assumptions):
237
+ if ask(Q.real(expr.args[0]), assumptions):
238
+ return ask(Q.even(expr.args[0]), assumptions)
239
+
240
+ @EvenPredicate.register(im)
241
+ def _(expr, assumptions):
242
+ if ask(Q.real(expr.args[0]), assumptions):
243
+ return True
244
+
245
+ @EvenPredicate.register(NaN)
246
+ def _(expr, assumptions):
247
+ return None
248
+
249
+
250
+ # OddPredicate
251
+
252
+ @OddPredicate.register(Expr)
253
+ def _(expr, assumptions):
254
+ ret = expr.is_odd
255
+ if ret is None:
256
+ raise MDNotImplementedError
257
+ return ret
258
+
259
+ @OddPredicate.register(Basic)
260
+ def _(expr, assumptions):
261
+ _integer = ask(Q.integer(expr), assumptions)
262
+ if _integer:
263
+ _even = ask(Q.even(expr), assumptions)
264
+ if _even is None:
265
+ return None
266
+ return not _even
267
+ return _integer
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/order.py ADDED
@@ -0,0 +1,436 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Handlers related to order relations: positive, negative, etc.
3
+ """
4
+
5
+ from sympy.assumptions import Q, ask
6
+ from sympy.core import Add, Basic, Expr, Mul, Pow
7
+ from sympy.core.logic import fuzzy_not, fuzzy_and, fuzzy_or
8
+ from sympy.core.numbers import E, ImaginaryUnit, NaN, I, pi
9
+ from sympy.functions import Abs, acos, acot, asin, atan, exp, factorial, log
10
+ from sympy.matrices import Determinant, Trace
11
+ from sympy.matrices.expressions.matexpr import MatrixElement
12
+
13
+ from sympy.multipledispatch import MDNotImplementedError
14
+
15
+ from ..predicates.order import (NegativePredicate, NonNegativePredicate,
16
+ NonZeroPredicate, ZeroPredicate, NonPositivePredicate, PositivePredicate,
17
+ ExtendedNegativePredicate, ExtendedNonNegativePredicate,
18
+ ExtendedNonPositivePredicate, ExtendedNonZeroPredicate,
19
+ ExtendedPositivePredicate,)
20
+
21
+
22
+ # NegativePredicate
23
+
24
+ def _NegativePredicate_number(expr, assumptions):
25
+ r, i = expr.as_real_imag()
26
+ # If the imaginary part can symbolically be shown to be zero then
27
+ # we just evaluate the real part; otherwise we evaluate the imaginary
28
+ # part to see if it actually evaluates to zero and if it does then
29
+ # we make the comparison between the real part and zero.
30
+ if not i:
31
+ r = r.evalf(2)
32
+ if r._prec != 1:
33
+ return r < 0
34
+ else:
35
+ i = i.evalf(2)
36
+ if i._prec != 1:
37
+ if i != 0:
38
+ return False
39
+ r = r.evalf(2)
40
+ if r._prec != 1:
41
+ return r < 0
42
+
43
+ @NegativePredicate.register(Basic)
44
+ def _(expr, assumptions):
45
+ if expr.is_number:
46
+ return _NegativePredicate_number(expr, assumptions)
47
+
48
+ @NegativePredicate.register(Expr)
49
+ def _(expr, assumptions):
50
+ ret = expr.is_negative
51
+ if ret is None:
52
+ raise MDNotImplementedError
53
+ return ret
54
+
55
+ @NegativePredicate.register(Add)
56
+ def _(expr, assumptions):
57
+ """
58
+ Positive + Positive -> Positive,
59
+ Negative + Negative -> Negative
60
+ """
61
+ if expr.is_number:
62
+ return _NegativePredicate_number(expr, assumptions)
63
+
64
+ r = ask(Q.real(expr), assumptions)
65
+ if r is not True:
66
+ return r
67
+
68
+ nonpos = 0
69
+ for arg in expr.args:
70
+ if ask(Q.negative(arg), assumptions) is not True:
71
+ if ask(Q.positive(arg), assumptions) is False:
72
+ nonpos += 1
73
+ else:
74
+ break
75
+ else:
76
+ if nonpos < len(expr.args):
77
+ return True
78
+
79
+ @NegativePredicate.register(Mul)
80
+ def _(expr, assumptions):
81
+ if expr.is_number:
82
+ return _NegativePredicate_number(expr, assumptions)
83
+ result = None
84
+ for arg in expr.args:
85
+ if result is None:
86
+ result = False
87
+ if ask(Q.negative(arg), assumptions):
88
+ result = not result
89
+ elif ask(Q.positive(arg), assumptions):
90
+ pass
91
+ else:
92
+ return
93
+ return result
94
+
95
+ @NegativePredicate.register(Pow)
96
+ def _(expr, assumptions):
97
+ """
98
+ Real ** Even -> NonNegative
99
+ Real ** Odd -> same_as_base
100
+ NonNegative ** Positive -> NonNegative
101
+ """
102
+ if expr.base == E:
103
+ # Exponential is always positive:
104
+ if ask(Q.real(expr.exp), assumptions):
105
+ return False
106
+ return
107
+
108
+ if expr.is_number:
109
+ return _NegativePredicate_number(expr, assumptions)
110
+ if ask(Q.real(expr.base), assumptions):
111
+ if ask(Q.positive(expr.base), assumptions):
112
+ if ask(Q.real(expr.exp), assumptions):
113
+ return False
114
+ if ask(Q.even(expr.exp), assumptions):
115
+ return False
116
+ if ask(Q.odd(expr.exp), assumptions):
117
+ return ask(Q.negative(expr.base), assumptions)
118
+
119
+ @NegativePredicate.register_many(Abs, ImaginaryUnit)
120
+ def _(expr, assumptions):
121
+ return False
122
+
123
+ @NegativePredicate.register(exp)
124
+ def _(expr, assumptions):
125
+ if ask(Q.real(expr.exp), assumptions):
126
+ return False
127
+ raise MDNotImplementedError
128
+
129
+
130
+ # NonNegativePredicate
131
+
132
+ @NonNegativePredicate.register(Basic)
133
+ def _(expr, assumptions):
134
+ if expr.is_number:
135
+ notnegative = fuzzy_not(_NegativePredicate_number(expr, assumptions))
136
+ if notnegative:
137
+ return ask(Q.real(expr), assumptions)
138
+ else:
139
+ return notnegative
140
+
141
+ @NonNegativePredicate.register(Expr)
142
+ def _(expr, assumptions):
143
+ ret = expr.is_nonnegative
144
+ if ret is None:
145
+ raise MDNotImplementedError
146
+ return ret
147
+
148
+
149
+ # NonZeroPredicate
150
+
151
+ @NonZeroPredicate.register(Expr)
152
+ def _(expr, assumptions):
153
+ ret = expr.is_nonzero
154
+ if ret is None:
155
+ raise MDNotImplementedError
156
+ return ret
157
+
158
+ @NonZeroPredicate.register(Basic)
159
+ def _(expr, assumptions):
160
+ if ask(Q.real(expr)) is False:
161
+ return False
162
+ if expr.is_number:
163
+ # if there are no symbols just evalf
164
+ i = expr.evalf(2)
165
+ def nonz(i):
166
+ if i._prec != 1:
167
+ return i != 0
168
+ return fuzzy_or(nonz(i) for i in i.as_real_imag())
169
+
170
+ @NonZeroPredicate.register(Add)
171
+ def _(expr, assumptions):
172
+ if all(ask(Q.positive(x), assumptions) for x in expr.args) \
173
+ or all(ask(Q.negative(x), assumptions) for x in expr.args):
174
+ return True
175
+
176
+ @NonZeroPredicate.register(Mul)
177
+ def _(expr, assumptions):
178
+ for arg in expr.args:
179
+ result = ask(Q.nonzero(arg), assumptions)
180
+ if result:
181
+ continue
182
+ return result
183
+ return True
184
+
185
+ @NonZeroPredicate.register(Pow)
186
+ def _(expr, assumptions):
187
+ return ask(Q.nonzero(expr.base), assumptions)
188
+
189
+ @NonZeroPredicate.register(Abs)
190
+ def _(expr, assumptions):
191
+ return ask(Q.nonzero(expr.args[0]), assumptions)
192
+
193
+ @NonZeroPredicate.register(NaN)
194
+ def _(expr, assumptions):
195
+ return None
196
+
197
+
198
+ # ZeroPredicate
199
+
200
+ @ZeroPredicate.register(Expr)
201
+ def _(expr, assumptions):
202
+ ret = expr.is_zero
203
+ if ret is None:
204
+ raise MDNotImplementedError
205
+ return ret
206
+
207
+ @ZeroPredicate.register(Basic)
208
+ def _(expr, assumptions):
209
+ return fuzzy_and([fuzzy_not(ask(Q.nonzero(expr), assumptions)),
210
+ ask(Q.real(expr), assumptions)])
211
+
212
+ @ZeroPredicate.register(Mul)
213
+ def _(expr, assumptions):
214
+ # TODO: This should be deducible from the nonzero handler
215
+ return fuzzy_or(ask(Q.zero(arg), assumptions) for arg in expr.args)
216
+
217
+
218
+ # NonPositivePredicate
219
+
220
+ @NonPositivePredicate.register(Expr)
221
+ def _(expr, assumptions):
222
+ ret = expr.is_nonpositive
223
+ if ret is None:
224
+ raise MDNotImplementedError
225
+ return ret
226
+
227
+ @NonPositivePredicate.register(Basic)
228
+ def _(expr, assumptions):
229
+ if expr.is_number:
230
+ notpositive = fuzzy_not(_PositivePredicate_number(expr, assumptions))
231
+ if notpositive:
232
+ return ask(Q.real(expr), assumptions)
233
+ else:
234
+ return notpositive
235
+
236
+
237
+ # PositivePredicate
238
+
239
+ def _PositivePredicate_number(expr, assumptions):
240
+ r, i = expr.as_real_imag()
241
+ # If the imaginary part can symbolically be shown to be zero then
242
+ # we just evaluate the real part; otherwise we evaluate the imaginary
243
+ # part to see if it actually evaluates to zero and if it does then
244
+ # we make the comparison between the real part and zero.
245
+ if not i:
246
+ r = r.evalf(2)
247
+ if r._prec != 1:
248
+ return r > 0
249
+ else:
250
+ i = i.evalf(2)
251
+ if i._prec != 1:
252
+ if i != 0:
253
+ return False
254
+ r = r.evalf(2)
255
+ if r._prec != 1:
256
+ return r > 0
257
+
258
+ @PositivePredicate.register(Expr)
259
+ def _(expr, assumptions):
260
+ ret = expr.is_positive
261
+ if ret is None:
262
+ raise MDNotImplementedError
263
+ return ret
264
+
265
+ @PositivePredicate.register(Basic)
266
+ def _(expr, assumptions):
267
+ if expr.is_number:
268
+ return _PositivePredicate_number(expr, assumptions)
269
+
270
+ @PositivePredicate.register(Mul)
271
+ def _(expr, assumptions):
272
+ if expr.is_number:
273
+ return _PositivePredicate_number(expr, assumptions)
274
+ result = True
275
+ for arg in expr.args:
276
+ if ask(Q.positive(arg), assumptions):
277
+ continue
278
+ elif ask(Q.negative(arg), assumptions):
279
+ result = result ^ True
280
+ else:
281
+ return
282
+ return result
283
+
284
+ @PositivePredicate.register(Add)
285
+ def _(expr, assumptions):
286
+ if expr.is_number:
287
+ return _PositivePredicate_number(expr, assumptions)
288
+
289
+ r = ask(Q.real(expr), assumptions)
290
+ if r is not True:
291
+ return r
292
+
293
+ nonneg = 0
294
+ for arg in expr.args:
295
+ if ask(Q.positive(arg), assumptions) is not True:
296
+ if ask(Q.negative(arg), assumptions) is False:
297
+ nonneg += 1
298
+ else:
299
+ break
300
+ else:
301
+ if nonneg < len(expr.args):
302
+ return True
303
+
304
+ @PositivePredicate.register(Pow)
305
+ def _(expr, assumptions):
306
+ if expr.base == E:
307
+ if ask(Q.real(expr.exp), assumptions):
308
+ return True
309
+ if ask(Q.imaginary(expr.exp), assumptions):
310
+ return ask(Q.even(expr.exp/(I*pi)), assumptions)
311
+ return
312
+
313
+ if expr.is_number:
314
+ return _PositivePredicate_number(expr, assumptions)
315
+ if ask(Q.positive(expr.base), assumptions):
316
+ if ask(Q.real(expr.exp), assumptions):
317
+ return True
318
+ if ask(Q.negative(expr.base), assumptions):
319
+ if ask(Q.even(expr.exp), assumptions):
320
+ return True
321
+ if ask(Q.odd(expr.exp), assumptions):
322
+ return False
323
+
324
+ @PositivePredicate.register(exp)
325
+ def _(expr, assumptions):
326
+ if ask(Q.real(expr.exp), assumptions):
327
+ return True
328
+ if ask(Q.imaginary(expr.exp), assumptions):
329
+ return ask(Q.even(expr.exp/(I*pi)), assumptions)
330
+
331
+ @PositivePredicate.register(log)
332
+ def _(expr, assumptions):
333
+ r = ask(Q.real(expr.args[0]), assumptions)
334
+ if r is not True:
335
+ return r
336
+ if ask(Q.positive(expr.args[0] - 1), assumptions):
337
+ return True
338
+ if ask(Q.negative(expr.args[0] - 1), assumptions):
339
+ return False
340
+
341
+ @PositivePredicate.register(factorial)
342
+ def _(expr, assumptions):
343
+ x = expr.args[0]
344
+ if ask(Q.integer(x) & Q.positive(x), assumptions):
345
+ return True
346
+
347
+ @PositivePredicate.register(ImaginaryUnit)
348
+ def _(expr, assumptions):
349
+ return False
350
+
351
+ @PositivePredicate.register(Abs)
352
+ def _(expr, assumptions):
353
+ return ask(Q.nonzero(expr), assumptions)
354
+
355
+ @PositivePredicate.register(Trace)
356
+ def _(expr, assumptions):
357
+ if ask(Q.positive_definite(expr.arg), assumptions):
358
+ return True
359
+
360
+ @PositivePredicate.register(Determinant)
361
+ def _(expr, assumptions):
362
+ if ask(Q.positive_definite(expr.arg), assumptions):
363
+ return True
364
+
365
+ @PositivePredicate.register(MatrixElement)
366
+ def _(expr, assumptions):
367
+ if (expr.i == expr.j
368
+ and ask(Q.positive_definite(expr.parent), assumptions)):
369
+ return True
370
+
371
+ @PositivePredicate.register(atan)
372
+ def _(expr, assumptions):
373
+ return ask(Q.positive(expr.args[0]), assumptions)
374
+
375
+ @PositivePredicate.register(asin)
376
+ def _(expr, assumptions):
377
+ x = expr.args[0]
378
+ if ask(Q.positive(x) & Q.nonpositive(x - 1), assumptions):
379
+ return True
380
+ if ask(Q.negative(x) & Q.nonnegative(x + 1), assumptions):
381
+ return False
382
+
383
+ @PositivePredicate.register(acos)
384
+ def _(expr, assumptions):
385
+ x = expr.args[0]
386
+ if ask(Q.nonpositive(x - 1) & Q.nonnegative(x + 1), assumptions):
387
+ return True
388
+
389
+ @PositivePredicate.register(acot)
390
+ def _(expr, assumptions):
391
+ return ask(Q.real(expr.args[0]), assumptions)
392
+
393
+ @PositivePredicate.register(NaN)
394
+ def _(expr, assumptions):
395
+ return None
396
+
397
+
398
+ # ExtendedNegativePredicate
399
+
400
+ @ExtendedNegativePredicate.register(object)
401
+ def _(expr, assumptions):
402
+ return ask(Q.negative(expr) | Q.negative_infinite(expr), assumptions)
403
+
404
+
405
+ # ExtendedPositivePredicate
406
+
407
+ @ExtendedPositivePredicate.register(object)
408
+ def _(expr, assumptions):
409
+ return ask(Q.positive(expr) | Q.positive_infinite(expr), assumptions)
410
+
411
+
412
+ # ExtendedNonZeroPredicate
413
+
414
+ @ExtendedNonZeroPredicate.register(object)
415
+ def _(expr, assumptions):
416
+ return ask(
417
+ Q.negative_infinite(expr) | Q.negative(expr) | Q.positive(expr) | Q.positive_infinite(expr),
418
+ assumptions)
419
+
420
+
421
+ # ExtendedNonPositivePredicate
422
+
423
+ @ExtendedNonPositivePredicate.register(object)
424
+ def _(expr, assumptions):
425
+ return ask(
426
+ Q.negative_infinite(expr) | Q.negative(expr) | Q.zero(expr),
427
+ assumptions)
428
+
429
+
430
+ # ExtendedNonNegativePredicate
431
+
432
+ @ExtendedNonNegativePredicate.register(object)
433
+ def _(expr, assumptions):
434
+ return ask(
435
+ Q.zero(expr) | Q.positive(expr) | Q.positive_infinite(expr),
436
+ assumptions)
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/handlers/sets.py ADDED
@@ -0,0 +1,772 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Handlers for predicates related to set membership: integer, rational, etc.
3
+ """
4
+
5
+ from sympy.assumptions import Q, ask
6
+ from sympy.core import Add, Basic, Expr, Mul, Pow, S
7
+ from sympy.core.numbers import (AlgebraicNumber, ComplexInfinity, Exp1, Float,
8
+ GoldenRatio, ImaginaryUnit, Infinity, Integer, NaN, NegativeInfinity,
9
+ Number, NumberSymbol, Pi, pi, Rational, TribonacciConstant, E)
10
+ from sympy.core.logic import fuzzy_bool
11
+ from sympy.functions import (Abs, acos, acot, asin, atan, cos, cot, exp, im,
12
+ log, re, sin, tan)
13
+ from sympy.core.numbers import I
14
+ from sympy.core.relational import Eq
15
+ from sympy.functions.elementary.complexes import conjugate
16
+ from sympy.matrices import Determinant, MatrixBase, Trace
17
+ from sympy.matrices.expressions.matexpr import MatrixElement
18
+
19
+ from sympy.multipledispatch import MDNotImplementedError
20
+
21
+ from .common import test_closed_group
22
+ from ..predicates.sets import (IntegerPredicate, RationalPredicate,
23
+ IrrationalPredicate, RealPredicate, ExtendedRealPredicate,
24
+ HermitianPredicate, ComplexPredicate, ImaginaryPredicate,
25
+ AntihermitianPredicate, AlgebraicPredicate)
26
+
27
+
28
+ # IntegerPredicate
29
+
30
+ def _IntegerPredicate_number(expr, assumptions):
31
+ # helper function
32
+ try:
33
+ i = int(expr.round())
34
+ if not (expr - i).equals(0):
35
+ raise TypeError
36
+ return True
37
+ except TypeError:
38
+ return False
39
+
40
+ @IntegerPredicate.register_many(int, Integer) # type:ignore
41
+ def _(expr, assumptions):
42
+ return True
43
+
44
+ @IntegerPredicate.register_many(Exp1, GoldenRatio, ImaginaryUnit, Infinity,
45
+ NegativeInfinity, Pi, Rational, TribonacciConstant)
46
+ def _(expr, assumptions):
47
+ return False
48
+
49
+ @IntegerPredicate.register(Expr)
50
+ def _(expr, assumptions):
51
+ ret = expr.is_integer
52
+ if ret is None:
53
+ raise MDNotImplementedError
54
+ return ret
55
+
56
+ @IntegerPredicate.register_many(Add, Pow)
57
+ def _(expr, assumptions):
58
+ """
59
+ * Integer + Integer -> Integer
60
+ * Integer + !Integer -> !Integer
61
+ * !Integer + !Integer -> ?
62
+ """
63
+ if expr.is_number:
64
+ return _IntegerPredicate_number(expr, assumptions)
65
+ return test_closed_group(expr, assumptions, Q.integer)
66
+
67
+ @IntegerPredicate.register(Mul)
68
+ def _(expr, assumptions):
69
+ """
70
+ * Integer*Integer -> Integer
71
+ * Integer*Irrational -> !Integer
72
+ * Odd/Even -> !Integer
73
+ * Integer*Rational -> ?
74
+ """
75
+ if expr.is_number:
76
+ return _IntegerPredicate_number(expr, assumptions)
77
+ _output = True
78
+ for arg in expr.args:
79
+ if not ask(Q.integer(arg), assumptions):
80
+ if arg.is_Rational:
81
+ if arg.q == 2:
82
+ return ask(Q.even(2*expr), assumptions)
83
+ if ~(arg.q & 1):
84
+ return None
85
+ elif ask(Q.irrational(arg), assumptions):
86
+ if _output:
87
+ _output = False
88
+ else:
89
+ return
90
+ else:
91
+ return
92
+
93
+ return _output
94
+
95
+ @IntegerPredicate.register(Abs)
96
+ def _(expr, assumptions):
97
+ return ask(Q.integer(expr.args[0]), assumptions)
98
+
99
+ @IntegerPredicate.register_many(Determinant, MatrixElement, Trace)
100
+ def _(expr, assumptions):
101
+ return ask(Q.integer_elements(expr.args[0]), assumptions)
102
+
103
+
104
+ # RationalPredicate
105
+
106
+ @RationalPredicate.register(Rational)
107
+ def _(expr, assumptions):
108
+ return True
109
+
110
+ @RationalPredicate.register(Float)
111
+ def _(expr, assumptions):
112
+ return None
113
+
114
+ @RationalPredicate.register_many(Exp1, GoldenRatio, ImaginaryUnit, Infinity,
115
+ NegativeInfinity, Pi, TribonacciConstant)
116
+ def _(expr, assumptions):
117
+ return False
118
+
119
+ @RationalPredicate.register(Expr)
120
+ def _(expr, assumptions):
121
+ ret = expr.is_rational
122
+ if ret is None:
123
+ raise MDNotImplementedError
124
+ return ret
125
+
126
+ @RationalPredicate.register_many(Add, Mul)
127
+ def _(expr, assumptions):
128
+ """
129
+ * Rational + Rational -> Rational
130
+ * Rational + !Rational -> !Rational
131
+ * !Rational + !Rational -> ?
132
+ """
133
+ if expr.is_number:
134
+ if expr.as_real_imag()[1]:
135
+ return False
136
+ return test_closed_group(expr, assumptions, Q.rational)
137
+
138
+ @RationalPredicate.register(Pow)
139
+ def _(expr, assumptions):
140
+ """
141
+ * Rational ** Integer -> Rational
142
+ * Irrational ** Rational -> Irrational
143
+ * Rational ** Irrational -> ?
144
+ """
145
+ if expr.base == E:
146
+ x = expr.exp
147
+ if ask(Q.rational(x), assumptions):
148
+ return ask(~Q.nonzero(x), assumptions)
149
+ return
150
+
151
+ if ask(Q.integer(expr.exp), assumptions):
152
+ return ask(Q.rational(expr.base), assumptions)
153
+ elif ask(Q.rational(expr.exp), assumptions):
154
+ if ask(Q.prime(expr.base), assumptions):
155
+ return False
156
+
157
+ @RationalPredicate.register_many(asin, atan, cos, sin, tan)
158
+ def _(expr, assumptions):
159
+ x = expr.args[0]
160
+ if ask(Q.rational(x), assumptions):
161
+ return ask(~Q.nonzero(x), assumptions)
162
+
163
+ @RationalPredicate.register(exp)
164
+ def _(expr, assumptions):
165
+ x = expr.exp
166
+ if ask(Q.rational(x), assumptions):
167
+ return ask(~Q.nonzero(x), assumptions)
168
+
169
+ @RationalPredicate.register_many(acot, cot)
170
+ def _(expr, assumptions):
171
+ x = expr.args[0]
172
+ if ask(Q.rational(x), assumptions):
173
+ return False
174
+
175
+ @RationalPredicate.register_many(acos, log)
176
+ def _(expr, assumptions):
177
+ x = expr.args[0]
178
+ if ask(Q.rational(x), assumptions):
179
+ return ask(~Q.nonzero(x - 1), assumptions)
180
+
181
+
182
+ # IrrationalPredicate
183
+
184
+ @IrrationalPredicate.register(Expr)
185
+ def _(expr, assumptions):
186
+ ret = expr.is_irrational
187
+ if ret is None:
188
+ raise MDNotImplementedError
189
+ return ret
190
+
191
+ @IrrationalPredicate.register(Basic)
192
+ def _(expr, assumptions):
193
+ _real = ask(Q.real(expr), assumptions)
194
+ if _real:
195
+ _rational = ask(Q.rational(expr), assumptions)
196
+ if _rational is None:
197
+ return None
198
+ return not _rational
199
+ else:
200
+ return _real
201
+
202
+
203
+ # RealPredicate
204
+
205
+ def _RealPredicate_number(expr, assumptions):
206
+ # let as_real_imag() work first since the expression may
207
+ # be simpler to evaluate
208
+ i = expr.as_real_imag()[1].evalf(2)
209
+ if i._prec != 1:
210
+ return not i
211
+ # allow None to be returned if we couldn't show for sure
212
+ # that i was 0
213
+
214
+ @RealPredicate.register_many(Abs, Exp1, Float, GoldenRatio, im, Pi, Rational,
215
+ re, TribonacciConstant)
216
+ def _(expr, assumptions):
217
+ return True
218
+
219
+ @RealPredicate.register_many(ImaginaryUnit, Infinity, NegativeInfinity)
220
+ def _(expr, assumptions):
221
+ return False
222
+
223
+ @RealPredicate.register(Expr)
224
+ def _(expr, assumptions):
225
+ ret = expr.is_real
226
+ if ret is None:
227
+ raise MDNotImplementedError
228
+ return ret
229
+
230
+ @RealPredicate.register(Add)
231
+ def _(expr, assumptions):
232
+ """
233
+ * Real + Real -> Real
234
+ * Real + (Complex & !Real) -> !Real
235
+ """
236
+ if expr.is_number:
237
+ return _RealPredicate_number(expr, assumptions)
238
+ return test_closed_group(expr, assumptions, Q.real)
239
+
240
+ @RealPredicate.register(Mul)
241
+ def _(expr, assumptions):
242
+ """
243
+ * Real*Real -> Real
244
+ * Real*Imaginary -> !Real
245
+ * Imaginary*Imaginary -> Real
246
+ """
247
+ if expr.is_number:
248
+ return _RealPredicate_number(expr, assumptions)
249
+ result = True
250
+ for arg in expr.args:
251
+ if ask(Q.real(arg), assumptions):
252
+ pass
253
+ elif ask(Q.imaginary(arg), assumptions):
254
+ result = result ^ True
255
+ else:
256
+ break
257
+ else:
258
+ return result
259
+
260
+ @RealPredicate.register(Pow)
261
+ def _(expr, assumptions):
262
+ """
263
+ * Real**Integer -> Real
264
+ * Positive**Real -> Real
265
+ * Real**(Integer/Even) -> Real if base is nonnegative
266
+ * Real**(Integer/Odd) -> Real
267
+ * Imaginary**(Integer/Even) -> Real
268
+ * Imaginary**(Integer/Odd) -> not Real
269
+ * Imaginary**Real -> ? since Real could be 0 (giving real)
270
+ or 1 (giving imaginary)
271
+ * b**Imaginary -> Real if log(b) is imaginary and b != 0
272
+ and exponent != integer multiple of
273
+ I*pi/log(b)
274
+ * Real**Real -> ? e.g. sqrt(-1) is imaginary and
275
+ sqrt(2) is not
276
+ """
277
+ if expr.is_number:
278
+ return _RealPredicate_number(expr, assumptions)
279
+
280
+ if expr.base == E:
281
+ return ask(
282
+ Q.integer(expr.exp/I/pi) | Q.real(expr.exp), assumptions
283
+ )
284
+
285
+ if expr.base.func == exp or (expr.base.is_Pow and expr.base.base == E):
286
+ if ask(Q.imaginary(expr.base.exp), assumptions):
287
+ if ask(Q.imaginary(expr.exp), assumptions):
288
+ return True
289
+ # If the i = (exp's arg)/(I*pi) is an integer or half-integer
290
+ # multiple of I*pi then 2*i will be an integer. In addition,
291
+ # exp(i*I*pi) = (-1)**i so the overall realness of the expr
292
+ # can be determined by replacing exp(i*I*pi) with (-1)**i.
293
+ i = expr.base.exp/I/pi
294
+ if ask(Q.integer(2*i), assumptions):
295
+ return ask(Q.real((S.NegativeOne**i)**expr.exp), assumptions)
296
+ return
297
+
298
+ if ask(Q.imaginary(expr.base), assumptions):
299
+ if ask(Q.integer(expr.exp), assumptions):
300
+ odd = ask(Q.odd(expr.exp), assumptions)
301
+ if odd is not None:
302
+ return not odd
303
+ return
304
+
305
+ if ask(Q.imaginary(expr.exp), assumptions):
306
+ imlog = ask(Q.imaginary(log(expr.base)), assumptions)
307
+ if imlog is not None:
308
+ # I**i -> real, log(I) is imag;
309
+ # (2*I)**i -> complex, log(2*I) is not imag
310
+ return imlog
311
+
312
+ if ask(Q.real(expr.base), assumptions):
313
+ if ask(Q.real(expr.exp), assumptions):
314
+ if expr.exp.is_Rational and \
315
+ ask(Q.even(expr.exp.q), assumptions):
316
+ return ask(Q.positive(expr.base), assumptions)
317
+ elif ask(Q.integer(expr.exp), assumptions):
318
+ return True
319
+ elif ask(Q.positive(expr.base), assumptions):
320
+ return True
321
+ elif ask(Q.negative(expr.base), assumptions):
322
+ return False
323
+
324
+ @RealPredicate.register_many(cos, sin)
325
+ def _(expr, assumptions):
326
+ if ask(Q.real(expr.args[0]), assumptions):
327
+ return True
328
+
329
+ @RealPredicate.register(exp)
330
+ def _(expr, assumptions):
331
+ return ask(
332
+ Q.integer(expr.exp/I/pi) | Q.real(expr.exp), assumptions
333
+ )
334
+
335
+ @RealPredicate.register(log)
336
+ def _(expr, assumptions):
337
+ return ask(Q.positive(expr.args[0]), assumptions)
338
+
339
+ @RealPredicate.register_many(Determinant, MatrixElement, Trace)
340
+ def _(expr, assumptions):
341
+ return ask(Q.real_elements(expr.args[0]), assumptions)
342
+
343
+
344
+ # ExtendedRealPredicate
345
+
346
+ @ExtendedRealPredicate.register(object)
347
+ def _(expr, assumptions):
348
+ return ask(Q.negative_infinite(expr)
349
+ | Q.negative(expr)
350
+ | Q.zero(expr)
351
+ | Q.positive(expr)
352
+ | Q.positive_infinite(expr),
353
+ assumptions)
354
+
355
+ @ExtendedRealPredicate.register_many(Infinity, NegativeInfinity)
356
+ def _(expr, assumptions):
357
+ return True
358
+
359
+ @ExtendedRealPredicate.register_many(Add, Mul, Pow) # type:ignore
360
+ def _(expr, assumptions):
361
+ return test_closed_group(expr, assumptions, Q.extended_real)
362
+
363
+
364
+ # HermitianPredicate
365
+
366
+ @HermitianPredicate.register(object) # type:ignore
367
+ def _(expr, assumptions):
368
+ if isinstance(expr, MatrixBase):
369
+ return None
370
+ return ask(Q.real(expr), assumptions)
371
+
372
+ @HermitianPredicate.register(Add) # type:ignore
373
+ def _(expr, assumptions):
374
+ """
375
+ * Hermitian + Hermitian -> Hermitian
376
+ * Hermitian + !Hermitian -> !Hermitian
377
+ """
378
+ if expr.is_number:
379
+ raise MDNotImplementedError
380
+ return test_closed_group(expr, assumptions, Q.hermitian)
381
+
382
+ @HermitianPredicate.register(Mul) # type:ignore
383
+ def _(expr, assumptions):
384
+ """
385
+ As long as there is at most only one noncommutative term:
386
+
387
+ * Hermitian*Hermitian -> Hermitian
388
+ * Hermitian*Antihermitian -> !Hermitian
389
+ * Antihermitian*Antihermitian -> Hermitian
390
+ """
391
+ if expr.is_number:
392
+ raise MDNotImplementedError
393
+ nccount = 0
394
+ result = True
395
+ for arg in expr.args:
396
+ if ask(Q.antihermitian(arg), assumptions):
397
+ result = result ^ True
398
+ elif not ask(Q.hermitian(arg), assumptions):
399
+ break
400
+ if ask(~Q.commutative(arg), assumptions):
401
+ nccount += 1
402
+ if nccount > 1:
403
+ break
404
+ else:
405
+ return result
406
+
407
+ @HermitianPredicate.register(Pow) # type:ignore
408
+ def _(expr, assumptions):
409
+ """
410
+ * Hermitian**Integer -> Hermitian
411
+ """
412
+ if expr.is_number:
413
+ raise MDNotImplementedError
414
+ if expr.base == E:
415
+ if ask(Q.hermitian(expr.exp), assumptions):
416
+ return True
417
+ raise MDNotImplementedError
418
+ if ask(Q.hermitian(expr.base), assumptions):
419
+ if ask(Q.integer(expr.exp), assumptions):
420
+ return True
421
+ raise MDNotImplementedError
422
+
423
+ @HermitianPredicate.register_many(cos, sin) # type:ignore
424
+ def _(expr, assumptions):
425
+ if ask(Q.hermitian(expr.args[0]), assumptions):
426
+ return True
427
+ raise MDNotImplementedError
428
+
429
+ @HermitianPredicate.register(exp) # type:ignore
430
+ def _(expr, assumptions):
431
+ if ask(Q.hermitian(expr.exp), assumptions):
432
+ return True
433
+ raise MDNotImplementedError
434
+
435
+ @HermitianPredicate.register(MatrixBase) # type:ignore
436
+ def _(mat, assumptions):
437
+ rows, cols = mat.shape
438
+ ret_val = True
439
+ for i in range(rows):
440
+ for j in range(i, cols):
441
+ cond = fuzzy_bool(Eq(mat[i, j], conjugate(mat[j, i])))
442
+ if cond is None:
443
+ ret_val = None
444
+ if cond == False:
445
+ return False
446
+ if ret_val is None:
447
+ raise MDNotImplementedError
448
+ return ret_val
449
+
450
+
451
+ # ComplexPredicate
452
+
453
+ @ComplexPredicate.register_many(Abs, cos, exp, im, ImaginaryUnit, log, Number, # type:ignore
454
+ NumberSymbol, re, sin)
455
+ def _(expr, assumptions):
456
+ return True
457
+
458
+ @ComplexPredicate.register_many(Infinity, NegativeInfinity) # type:ignore
459
+ def _(expr, assumptions):
460
+ return False
461
+
462
+ @ComplexPredicate.register(Expr) # type:ignore
463
+ def _(expr, assumptions):
464
+ ret = expr.is_complex
465
+ if ret is None:
466
+ raise MDNotImplementedError
467
+ return ret
468
+
469
+ @ComplexPredicate.register_many(Add, Mul) # type:ignore
470
+ def _(expr, assumptions):
471
+ return test_closed_group(expr, assumptions, Q.complex)
472
+
473
+ @ComplexPredicate.register(Pow) # type:ignore
474
+ def _(expr, assumptions):
475
+ if expr.base == E:
476
+ return True
477
+ return test_closed_group(expr, assumptions, Q.complex)
478
+
479
+ @ComplexPredicate.register_many(Determinant, MatrixElement, Trace) # type:ignore
480
+ def _(expr, assumptions):
481
+ return ask(Q.complex_elements(expr.args[0]), assumptions)
482
+
483
+ @ComplexPredicate.register(NaN) # type:ignore
484
+ def _(expr, assumptions):
485
+ return None
486
+
487
+
488
+ # ImaginaryPredicate
489
+
490
+ def _Imaginary_number(expr, assumptions):
491
+ # let as_real_imag() work first since the expression may
492
+ # be simpler to evaluate
493
+ r = expr.as_real_imag()[0].evalf(2)
494
+ if r._prec != 1:
495
+ return not r
496
+ # allow None to be returned if we couldn't show for sure
497
+ # that r was 0
498
+
499
+ @ImaginaryPredicate.register(ImaginaryUnit) # type:ignore
500
+ def _(expr, assumptions):
501
+ return True
502
+
503
+ @ImaginaryPredicate.register(Expr) # type:ignore
504
+ def _(expr, assumptions):
505
+ ret = expr.is_imaginary
506
+ if ret is None:
507
+ raise MDNotImplementedError
508
+ return ret
509
+
510
+ @ImaginaryPredicate.register(Add) # type:ignore
511
+ def _(expr, assumptions):
512
+ """
513
+ * Imaginary + Imaginary -> Imaginary
514
+ * Imaginary + Complex -> ?
515
+ * Imaginary + Real -> !Imaginary
516
+ """
517
+ if expr.is_number:
518
+ return _Imaginary_number(expr, assumptions)
519
+
520
+ reals = 0
521
+ for arg in expr.args:
522
+ if ask(Q.imaginary(arg), assumptions):
523
+ pass
524
+ elif ask(Q.real(arg), assumptions):
525
+ reals += 1
526
+ else:
527
+ break
528
+ else:
529
+ if reals == 0:
530
+ return True
531
+ if reals in (1, len(expr.args)):
532
+ # two reals could sum 0 thus giving an imaginary
533
+ return False
534
+
535
+ @ImaginaryPredicate.register(Mul) # type:ignore
536
+ def _(expr, assumptions):
537
+ """
538
+ * Real*Imaginary -> Imaginary
539
+ * Imaginary*Imaginary -> Real
540
+ """
541
+ if expr.is_number:
542
+ return _Imaginary_number(expr, assumptions)
543
+ result = False
544
+ reals = 0
545
+ for arg in expr.args:
546
+ if ask(Q.imaginary(arg), assumptions):
547
+ result = result ^ True
548
+ elif not ask(Q.real(arg), assumptions):
549
+ break
550
+ else:
551
+ if reals == len(expr.args):
552
+ return False
553
+ return result
554
+
555
+ @ImaginaryPredicate.register(Pow) # type:ignore
556
+ def _(expr, assumptions):
557
+ """
558
+ * Imaginary**Odd -> Imaginary
559
+ * Imaginary**Even -> Real
560
+ * b**Imaginary -> !Imaginary if exponent is an integer
561
+ multiple of I*pi/log(b)
562
+ * Imaginary**Real -> ?
563
+ * Positive**Real -> Real
564
+ * Negative**Integer -> Real
565
+ * Negative**(Integer/2) -> Imaginary
566
+ * Negative**Real -> not Imaginary if exponent is not Rational
567
+ """
568
+ if expr.is_number:
569
+ return _Imaginary_number(expr, assumptions)
570
+
571
+ if expr.base == E:
572
+ a = expr.exp/I/pi
573
+ return ask(Q.integer(2*a) & ~Q.integer(a), assumptions)
574
+
575
+ if expr.base.func == exp or (expr.base.is_Pow and expr.base.base == E):
576
+ if ask(Q.imaginary(expr.base.exp), assumptions):
577
+ if ask(Q.imaginary(expr.exp), assumptions):
578
+ return False
579
+ i = expr.base.exp/I/pi
580
+ if ask(Q.integer(2*i), assumptions):
581
+ return ask(Q.imaginary((S.NegativeOne**i)**expr.exp), assumptions)
582
+
583
+ if ask(Q.imaginary(expr.base), assumptions):
584
+ if ask(Q.integer(expr.exp), assumptions):
585
+ odd = ask(Q.odd(expr.exp), assumptions)
586
+ if odd is not None:
587
+ return odd
588
+ return
589
+
590
+ if ask(Q.imaginary(expr.exp), assumptions):
591
+ imlog = ask(Q.imaginary(log(expr.base)), assumptions)
592
+ if imlog is not None:
593
+ # I**i -> real; (2*I)**i -> complex ==> not imaginary
594
+ return False
595
+
596
+ if ask(Q.real(expr.base) & Q.real(expr.exp), assumptions):
597
+ if ask(Q.positive(expr.base), assumptions):
598
+ return False
599
+ else:
600
+ rat = ask(Q.rational(expr.exp), assumptions)
601
+ if not rat:
602
+ return rat
603
+ if ask(Q.integer(expr.exp), assumptions):
604
+ return False
605
+ else:
606
+ half = ask(Q.integer(2*expr.exp), assumptions)
607
+ if half:
608
+ return ask(Q.negative(expr.base), assumptions)
609
+ return half
610
+
611
+ @ImaginaryPredicate.register(log) # type:ignore
612
+ def _(expr, assumptions):
613
+ if ask(Q.real(expr.args[0]), assumptions):
614
+ if ask(Q.positive(expr.args[0]), assumptions):
615
+ return False
616
+ return
617
+ # XXX it should be enough to do
618
+ # return ask(Q.nonpositive(expr.args[0]), assumptions)
619
+ # but ask(Q.nonpositive(exp(x)), Q.imaginary(x)) -> None;
620
+ # it should return True since exp(x) will be either 0 or complex
621
+ if expr.args[0].func == exp or (expr.args[0].is_Pow and expr.args[0].base == E):
622
+ if expr.args[0].exp in [I, -I]:
623
+ return True
624
+ im = ask(Q.imaginary(expr.args[0]), assumptions)
625
+ if im is False:
626
+ return False
627
+
628
+ @ImaginaryPredicate.register(exp) # type:ignore
629
+ def _(expr, assumptions):
630
+ a = expr.exp/I/pi
631
+ return ask(Q.integer(2*a) & ~Q.integer(a), assumptions)
632
+
633
+ @ImaginaryPredicate.register_many(Number, NumberSymbol) # type:ignore
634
+ def _(expr, assumptions):
635
+ return not (expr.as_real_imag()[1] == 0)
636
+
637
+ @ImaginaryPredicate.register(NaN) # type:ignore
638
+ def _(expr, assumptions):
639
+ return None
640
+
641
+
642
+ # AntihermitianPredicate
643
+
644
+ @AntihermitianPredicate.register(object) # type:ignore
645
+ def _(expr, assumptions):
646
+ if isinstance(expr, MatrixBase):
647
+ return None
648
+ if ask(Q.zero(expr), assumptions):
649
+ return True
650
+ return ask(Q.imaginary(expr), assumptions)
651
+
652
+ @AntihermitianPredicate.register(Add) # type:ignore
653
+ def _(expr, assumptions):
654
+ """
655
+ * Antihermitian + Antihermitian -> Antihermitian
656
+ * Antihermitian + !Antihermitian -> !Antihermitian
657
+ """
658
+ if expr.is_number:
659
+ raise MDNotImplementedError
660
+ return test_closed_group(expr, assumptions, Q.antihermitian)
661
+
662
+ @AntihermitianPredicate.register(Mul) # type:ignore
663
+ def _(expr, assumptions):
664
+ """
665
+ As long as there is at most only one noncommutative term:
666
+
667
+ * Hermitian*Hermitian -> !Antihermitian
668
+ * Hermitian*Antihermitian -> Antihermitian
669
+ * Antihermitian*Antihermitian -> !Antihermitian
670
+ """
671
+ if expr.is_number:
672
+ raise MDNotImplementedError
673
+ nccount = 0
674
+ result = False
675
+ for arg in expr.args:
676
+ if ask(Q.antihermitian(arg), assumptions):
677
+ result = result ^ True
678
+ elif not ask(Q.hermitian(arg), assumptions):
679
+ break
680
+ if ask(~Q.commutative(arg), assumptions):
681
+ nccount += 1
682
+ if nccount > 1:
683
+ break
684
+ else:
685
+ return result
686
+
687
+ @AntihermitianPredicate.register(Pow) # type:ignore
688
+ def _(expr, assumptions):
689
+ """
690
+ * Hermitian**Integer -> !Antihermitian
691
+ * Antihermitian**Even -> !Antihermitian
692
+ * Antihermitian**Odd -> Antihermitian
693
+ """
694
+ if expr.is_number:
695
+ raise MDNotImplementedError
696
+ if ask(Q.hermitian(expr.base), assumptions):
697
+ if ask(Q.integer(expr.exp), assumptions):
698
+ return False
699
+ elif ask(Q.antihermitian(expr.base), assumptions):
700
+ if ask(Q.even(expr.exp), assumptions):
701
+ return False
702
+ elif ask(Q.odd(expr.exp), assumptions):
703
+ return True
704
+ raise MDNotImplementedError
705
+
706
+ @AntihermitianPredicate.register(MatrixBase) # type:ignore
707
+ def _(mat, assumptions):
708
+ rows, cols = mat.shape
709
+ ret_val = True
710
+ for i in range(rows):
711
+ for j in range(i, cols):
712
+ cond = fuzzy_bool(Eq(mat[i, j], -conjugate(mat[j, i])))
713
+ if cond is None:
714
+ ret_val = None
715
+ if cond == False:
716
+ return False
717
+ if ret_val is None:
718
+ raise MDNotImplementedError
719
+ return ret_val
720
+
721
+
722
+ # AlgebraicPredicate
723
+
724
+ @AlgebraicPredicate.register_many(AlgebraicNumber, Float, GoldenRatio, # type:ignore
725
+ ImaginaryUnit, TribonacciConstant)
726
+ def _(expr, assumptions):
727
+ return True
728
+
729
+ @AlgebraicPredicate.register_many(ComplexInfinity, Exp1, Infinity, # type:ignore
730
+ NegativeInfinity, Pi)
731
+ def _(expr, assumptions):
732
+ return False
733
+
734
+ @AlgebraicPredicate.register_many(Add, Mul) # type:ignore
735
+ def _(expr, assumptions):
736
+ return test_closed_group(expr, assumptions, Q.algebraic)
737
+
738
+ @AlgebraicPredicate.register(Pow) # type:ignore
739
+ def _(expr, assumptions):
740
+ if expr.base == E:
741
+ if ask(Q.algebraic(expr.exp), assumptions):
742
+ return ask(~Q.nonzero(expr.exp), assumptions)
743
+ return
744
+ return expr.exp.is_Rational and ask(Q.algebraic(expr.base), assumptions)
745
+
746
+ @AlgebraicPredicate.register(Rational) # type:ignore
747
+ def _(expr, assumptions):
748
+ return expr.q != 0
749
+
750
+ @AlgebraicPredicate.register_many(asin, atan, cos, sin, tan) # type:ignore
751
+ def _(expr, assumptions):
752
+ x = expr.args[0]
753
+ if ask(Q.algebraic(x), assumptions):
754
+ return ask(~Q.nonzero(x), assumptions)
755
+
756
+ @AlgebraicPredicate.register(exp) # type:ignore
757
+ def _(expr, assumptions):
758
+ x = expr.exp
759
+ if ask(Q.algebraic(x), assumptions):
760
+ return ask(~Q.nonzero(x), assumptions)
761
+
762
+ @AlgebraicPredicate.register_many(acot, cot) # type:ignore
763
+ def _(expr, assumptions):
764
+ x = expr.args[0]
765
+ if ask(Q.algebraic(x), assumptions):
766
+ return False
767
+
768
+ @AlgebraicPredicate.register_many(acos, log) # type:ignore
769
+ def _(expr, assumptions):
770
+ x = expr.args[0]
771
+ if ask(Q.algebraic(x), assumptions):
772
+ return ask(~Q.nonzero(x - 1), assumptions)
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__init__.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ """
2
+ Module to implement predicate classes.
3
+
4
+ Class of every predicate registered to ``Q`` is defined here.
5
+ """
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (316 Bytes). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/calculus.cpython-310.pyc ADDED
Binary file (2.32 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/common.cpython-310.pyc ADDED
Binary file (2.71 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/matrices.cpython-310.pyc ADDED
Binary file (13 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/ntheory.cpython-310.pyc ADDED
Binary file (2.95 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/order.cpython-310.pyc ADDED
Binary file (10.2 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/sets.cpython-310.pyc ADDED
Binary file (9.27 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/calculus.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.assumptions import Predicate
2
+ from sympy.multipledispatch import Dispatcher
3
+
4
+ class FinitePredicate(Predicate):
5
+ """
6
+ Finite number predicate.
7
+
8
+ Explanation
9
+ ===========
10
+
11
+ ``Q.finite(x)`` is true if ``x`` is a number but neither an infinity
12
+ nor a ``NaN``. In other words, ``ask(Q.finite(x))`` is true for all
13
+ numerical ``x`` having a bounded absolute value.
14
+
15
+ Examples
16
+ ========
17
+
18
+ >>> from sympy import Q, ask, S, oo, I, zoo
19
+ >>> from sympy.abc import x
20
+ >>> ask(Q.finite(oo))
21
+ False
22
+ >>> ask(Q.finite(-oo))
23
+ False
24
+ >>> ask(Q.finite(zoo))
25
+ False
26
+ >>> ask(Q.finite(1))
27
+ True
28
+ >>> ask(Q.finite(2 + 3*I))
29
+ True
30
+ >>> ask(Q.finite(x), Q.positive(x))
31
+ True
32
+ >>> print(ask(Q.finite(S.NaN)))
33
+ None
34
+
35
+ References
36
+ ==========
37
+
38
+ .. [1] https://en.wikipedia.org/wiki/Finite
39
+
40
+ """
41
+ name = 'finite'
42
+ handler = Dispatcher(
43
+ "FiniteHandler",
44
+ doc=("Handler for Q.finite. Test that an expression is bounded respect"
45
+ " to all its variables.")
46
+ )
47
+
48
+
49
+ class InfinitePredicate(Predicate):
50
+ """
51
+ Infinite number predicate.
52
+
53
+ ``Q.infinite(x)`` is true iff the absolute value of ``x`` is
54
+ infinity.
55
+
56
+ """
57
+ # TODO: Add examples
58
+ name = 'infinite'
59
+ handler = Dispatcher(
60
+ "InfiniteHandler",
61
+ doc="""Handler for Q.infinite key."""
62
+ )
63
+
64
+
65
+ class PositiveInfinitePredicate(Predicate):
66
+ """
67
+ Positive infinity predicate.
68
+
69
+ ``Q.positive_infinite(x)`` is true iff ``x`` is positive infinity ``oo``.
70
+ """
71
+ name = 'positive_infinite'
72
+ handler = Dispatcher("PositiveInfiniteHandler")
73
+
74
+
75
+ class NegativeInfinitePredicate(Predicate):
76
+ """
77
+ Negative infinity predicate.
78
+
79
+ ``Q.negative_infinite(x)`` is true iff ``x`` is negative infinity ``-oo``.
80
+ """
81
+ name = 'negative_infinite'
82
+ handler = Dispatcher("NegativeInfiniteHandler")
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/common.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.assumptions import Predicate, AppliedPredicate, Q
2
+ from sympy.core.relational import Eq, Ne, Gt, Lt, Ge, Le
3
+ from sympy.multipledispatch import Dispatcher
4
+
5
+
6
+ class CommutativePredicate(Predicate):
7
+ """
8
+ Commutative predicate.
9
+
10
+ Explanation
11
+ ===========
12
+
13
+ ``ask(Q.commutative(x))`` is true iff ``x`` commutes with any other
14
+ object with respect to multiplication operation.
15
+
16
+ """
17
+ # TODO: Add examples
18
+ name = 'commutative'
19
+ handler = Dispatcher("CommutativeHandler", doc="Handler for key 'commutative'.")
20
+
21
+
22
+ binrelpreds = {Eq: Q.eq, Ne: Q.ne, Gt: Q.gt, Lt: Q.lt, Ge: Q.ge, Le: Q.le}
23
+
24
+ class IsTruePredicate(Predicate):
25
+ """
26
+ Generic predicate.
27
+
28
+ Explanation
29
+ ===========
30
+
31
+ ``ask(Q.is_true(x))`` is true iff ``x`` is true. This only makes
32
+ sense if ``x`` is a boolean object.
33
+
34
+ Examples
35
+ ========
36
+
37
+ >>> from sympy import ask, Q
38
+ >>> from sympy.abc import x, y
39
+ >>> ask(Q.is_true(True))
40
+ True
41
+
42
+ Wrapping another applied predicate just returns the applied predicate.
43
+
44
+ >>> Q.is_true(Q.even(x))
45
+ Q.even(x)
46
+
47
+ Wrapping binary relation classes in SymPy core returns applied binary
48
+ relational predicates.
49
+
50
+ >>> from sympy import Eq, Gt
51
+ >>> Q.is_true(Eq(x, y))
52
+ Q.eq(x, y)
53
+ >>> Q.is_true(Gt(x, y))
54
+ Q.gt(x, y)
55
+
56
+ Notes
57
+ =====
58
+
59
+ This class is designed to wrap the boolean objects so that they can
60
+ behave as if they are applied predicates. Consequently, wrapping another
61
+ applied predicate is unnecessary and thus it just returns the argument.
62
+ Also, binary relation classes in SymPy core have binary predicates to
63
+ represent themselves and thus wrapping them with ``Q.is_true`` converts them
64
+ to these applied predicates.
65
+
66
+ """
67
+ name = 'is_true'
68
+ handler = Dispatcher(
69
+ "IsTrueHandler",
70
+ doc="Wrapper allowing to query the truth value of a boolean expression."
71
+ )
72
+
73
+ def __call__(self, arg):
74
+ # No need to wrap another predicate
75
+ if isinstance(arg, AppliedPredicate):
76
+ return arg
77
+ # Convert relational predicates instead of wrapping them
78
+ if getattr(arg, "is_Relational", False):
79
+ pred = binrelpreds[type(arg)]
80
+ return pred(*arg.args)
81
+ return super().__call__(arg)
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/matrices.py ADDED
@@ -0,0 +1,511 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.assumptions import Predicate
2
+ from sympy.multipledispatch import Dispatcher
3
+
4
+ class SquarePredicate(Predicate):
5
+ """
6
+ Square matrix predicate.
7
+
8
+ Explanation
9
+ ===========
10
+
11
+ ``Q.square(x)`` is true iff ``x`` is a square matrix. A square matrix
12
+ is a matrix with the same number of rows and columns.
13
+
14
+ Examples
15
+ ========
16
+
17
+ >>> from sympy import Q, ask, MatrixSymbol, ZeroMatrix, Identity
18
+ >>> X = MatrixSymbol('X', 2, 2)
19
+ >>> Y = MatrixSymbol('X', 2, 3)
20
+ >>> ask(Q.square(X))
21
+ True
22
+ >>> ask(Q.square(Y))
23
+ False
24
+ >>> ask(Q.square(ZeroMatrix(3, 3)))
25
+ True
26
+ >>> ask(Q.square(Identity(3)))
27
+ True
28
+
29
+ References
30
+ ==========
31
+
32
+ .. [1] https://en.wikipedia.org/wiki/Square_matrix
33
+
34
+ """
35
+ name = 'square'
36
+ handler = Dispatcher("SquareHandler", doc="Handler for Q.square.")
37
+
38
+
39
+ class SymmetricPredicate(Predicate):
40
+ """
41
+ Symmetric matrix predicate.
42
+
43
+ Explanation
44
+ ===========
45
+
46
+ ``Q.symmetric(x)`` is true iff ``x`` is a square matrix and is equal to
47
+ its transpose. Every square diagonal matrix is a symmetric matrix.
48
+
49
+ Examples
50
+ ========
51
+
52
+ >>> from sympy import Q, ask, MatrixSymbol
53
+ >>> X = MatrixSymbol('X', 2, 2)
54
+ >>> Y = MatrixSymbol('Y', 2, 3)
55
+ >>> Z = MatrixSymbol('Z', 2, 2)
56
+ >>> ask(Q.symmetric(X*Z), Q.symmetric(X) & Q.symmetric(Z))
57
+ True
58
+ >>> ask(Q.symmetric(X + Z), Q.symmetric(X) & Q.symmetric(Z))
59
+ True
60
+ >>> ask(Q.symmetric(Y))
61
+ False
62
+
63
+
64
+ References
65
+ ==========
66
+
67
+ .. [1] https://en.wikipedia.org/wiki/Symmetric_matrix
68
+
69
+ """
70
+ # TODO: Add handlers to make these keys work with
71
+ # actual matrices and add more examples in the docstring.
72
+ name = 'symmetric'
73
+ handler = Dispatcher("SymmetricHandler", doc="Handler for Q.symmetric.")
74
+
75
+
76
+ class InvertiblePredicate(Predicate):
77
+ """
78
+ Invertible matrix predicate.
79
+
80
+ Explanation
81
+ ===========
82
+
83
+ ``Q.invertible(x)`` is true iff ``x`` is an invertible matrix.
84
+ A square matrix is called invertible only if its determinant is 0.
85
+
86
+ Examples
87
+ ========
88
+
89
+ >>> from sympy import Q, ask, MatrixSymbol
90
+ >>> X = MatrixSymbol('X', 2, 2)
91
+ >>> Y = MatrixSymbol('Y', 2, 3)
92
+ >>> Z = MatrixSymbol('Z', 2, 2)
93
+ >>> ask(Q.invertible(X*Y), Q.invertible(X))
94
+ False
95
+ >>> ask(Q.invertible(X*Z), Q.invertible(X) & Q.invertible(Z))
96
+ True
97
+ >>> ask(Q.invertible(X), Q.fullrank(X) & Q.square(X))
98
+ True
99
+
100
+ References
101
+ ==========
102
+
103
+ .. [1] https://en.wikipedia.org/wiki/Invertible_matrix
104
+
105
+ """
106
+ name = 'invertible'
107
+ handler = Dispatcher("InvertibleHandler", doc="Handler for Q.invertible.")
108
+
109
+
110
+ class OrthogonalPredicate(Predicate):
111
+ """
112
+ Orthogonal matrix predicate.
113
+
114
+ Explanation
115
+ ===========
116
+
117
+ ``Q.orthogonal(x)`` is true iff ``x`` is an orthogonal matrix.
118
+ A square matrix ``M`` is an orthogonal matrix if it satisfies
119
+ ``M^TM = MM^T = I`` where ``M^T`` is the transpose matrix of
120
+ ``M`` and ``I`` is an identity matrix. Note that an orthogonal
121
+ matrix is necessarily invertible.
122
+
123
+ Examples
124
+ ========
125
+
126
+ >>> from sympy import Q, ask, MatrixSymbol, Identity
127
+ >>> X = MatrixSymbol('X', 2, 2)
128
+ >>> Y = MatrixSymbol('Y', 2, 3)
129
+ >>> Z = MatrixSymbol('Z', 2, 2)
130
+ >>> ask(Q.orthogonal(Y))
131
+ False
132
+ >>> ask(Q.orthogonal(X*Z*X), Q.orthogonal(X) & Q.orthogonal(Z))
133
+ True
134
+ >>> ask(Q.orthogonal(Identity(3)))
135
+ True
136
+ >>> ask(Q.invertible(X), Q.orthogonal(X))
137
+ True
138
+
139
+ References
140
+ ==========
141
+
142
+ .. [1] https://en.wikipedia.org/wiki/Orthogonal_matrix
143
+
144
+ """
145
+ name = 'orthogonal'
146
+ handler = Dispatcher("OrthogonalHandler", doc="Handler for key 'orthogonal'.")
147
+
148
+
149
+ class UnitaryPredicate(Predicate):
150
+ """
151
+ Unitary matrix predicate.
152
+
153
+ Explanation
154
+ ===========
155
+
156
+ ``Q.unitary(x)`` is true iff ``x`` is a unitary matrix.
157
+ Unitary matrix is an analogue to orthogonal matrix. A square
158
+ matrix ``M`` with complex elements is unitary if :math:``M^TM = MM^T= I``
159
+ where :math:``M^T`` is the conjugate transpose matrix of ``M``.
160
+
161
+ Examples
162
+ ========
163
+
164
+ >>> from sympy import Q, ask, MatrixSymbol, Identity
165
+ >>> X = MatrixSymbol('X', 2, 2)
166
+ >>> Y = MatrixSymbol('Y', 2, 3)
167
+ >>> Z = MatrixSymbol('Z', 2, 2)
168
+ >>> ask(Q.unitary(Y))
169
+ False
170
+ >>> ask(Q.unitary(X*Z*X), Q.unitary(X) & Q.unitary(Z))
171
+ True
172
+ >>> ask(Q.unitary(Identity(3)))
173
+ True
174
+
175
+ References
176
+ ==========
177
+
178
+ .. [1] https://en.wikipedia.org/wiki/Unitary_matrix
179
+
180
+ """
181
+ name = 'unitary'
182
+ handler = Dispatcher("UnitaryHandler", doc="Handler for key 'unitary'.")
183
+
184
+
185
+ class FullRankPredicate(Predicate):
186
+ """
187
+ Fullrank matrix predicate.
188
+
189
+ Explanation
190
+ ===========
191
+
192
+ ``Q.fullrank(x)`` is true iff ``x`` is a full rank matrix.
193
+ A matrix is full rank if all rows and columns of the matrix
194
+ are linearly independent. A square matrix is full rank iff
195
+ its determinant is nonzero.
196
+
197
+ Examples
198
+ ========
199
+
200
+ >>> from sympy import Q, ask, MatrixSymbol, ZeroMatrix, Identity
201
+ >>> X = MatrixSymbol('X', 2, 2)
202
+ >>> ask(Q.fullrank(X.T), Q.fullrank(X))
203
+ True
204
+ >>> ask(Q.fullrank(ZeroMatrix(3, 3)))
205
+ False
206
+ >>> ask(Q.fullrank(Identity(3)))
207
+ True
208
+
209
+ """
210
+ name = 'fullrank'
211
+ handler = Dispatcher("FullRankHandler", doc="Handler for key 'fullrank'.")
212
+
213
+
214
+ class PositiveDefinitePredicate(Predicate):
215
+ r"""
216
+ Positive definite matrix predicate.
217
+
218
+ Explanation
219
+ ===========
220
+
221
+ If $M$ is a :math:`n \times n` symmetric real matrix, it is said
222
+ to be positive definite if :math:`Z^TMZ` is positive for
223
+ every non-zero column vector $Z$ of $n$ real numbers.
224
+
225
+ Examples
226
+ ========
227
+
228
+ >>> from sympy import Q, ask, MatrixSymbol, Identity
229
+ >>> X = MatrixSymbol('X', 2, 2)
230
+ >>> Y = MatrixSymbol('Y', 2, 3)
231
+ >>> Z = MatrixSymbol('Z', 2, 2)
232
+ >>> ask(Q.positive_definite(Y))
233
+ False
234
+ >>> ask(Q.positive_definite(Identity(3)))
235
+ True
236
+ >>> ask(Q.positive_definite(X + Z), Q.positive_definite(X) &
237
+ ... Q.positive_definite(Z))
238
+ True
239
+
240
+ References
241
+ ==========
242
+
243
+ .. [1] https://en.wikipedia.org/wiki/Positive-definite_matrix
244
+
245
+ """
246
+ name = "positive_definite"
247
+ handler = Dispatcher("PositiveDefiniteHandler", doc="Handler for key 'positive_definite'.")
248
+
249
+
250
+ class UpperTriangularPredicate(Predicate):
251
+ """
252
+ Upper triangular matrix predicate.
253
+
254
+ Explanation
255
+ ===========
256
+
257
+ A matrix $M$ is called upper triangular matrix if :math:`M_{ij}=0`
258
+ for :math:`i<j`.
259
+
260
+ Examples
261
+ ========
262
+
263
+ >>> from sympy import Q, ask, ZeroMatrix, Identity
264
+ >>> ask(Q.upper_triangular(Identity(3)))
265
+ True
266
+ >>> ask(Q.upper_triangular(ZeroMatrix(3, 3)))
267
+ True
268
+
269
+ References
270
+ ==========
271
+
272
+ .. [1] https://mathworld.wolfram.com/UpperTriangularMatrix.html
273
+
274
+ """
275
+ name = "upper_triangular"
276
+ handler = Dispatcher("UpperTriangularHandler", doc="Handler for key 'upper_triangular'.")
277
+
278
+
279
+ class LowerTriangularPredicate(Predicate):
280
+ """
281
+ Lower triangular matrix predicate.
282
+
283
+ Explanation
284
+ ===========
285
+
286
+ A matrix $M$ is called lower triangular matrix if :math:`M_{ij}=0`
287
+ for :math:`i>j`.
288
+
289
+ Examples
290
+ ========
291
+
292
+ >>> from sympy import Q, ask, ZeroMatrix, Identity
293
+ >>> ask(Q.lower_triangular(Identity(3)))
294
+ True
295
+ >>> ask(Q.lower_triangular(ZeroMatrix(3, 3)))
296
+ True
297
+
298
+ References
299
+ ==========
300
+
301
+ .. [1] https://mathworld.wolfram.com/LowerTriangularMatrix.html
302
+
303
+ """
304
+ name = "lower_triangular"
305
+ handler = Dispatcher("LowerTriangularHandler", doc="Handler for key 'lower_triangular'.")
306
+
307
+
308
+ class DiagonalPredicate(Predicate):
309
+ """
310
+ Diagonal matrix predicate.
311
+
312
+ Explanation
313
+ ===========
314
+
315
+ ``Q.diagonal(x)`` is true iff ``x`` is a diagonal matrix. A diagonal
316
+ matrix is a matrix in which the entries outside the main diagonal
317
+ are all zero.
318
+
319
+ Examples
320
+ ========
321
+
322
+ >>> from sympy import Q, ask, MatrixSymbol, ZeroMatrix
323
+ >>> X = MatrixSymbol('X', 2, 2)
324
+ >>> ask(Q.diagonal(ZeroMatrix(3, 3)))
325
+ True
326
+ >>> ask(Q.diagonal(X), Q.lower_triangular(X) &
327
+ ... Q.upper_triangular(X))
328
+ True
329
+
330
+ References
331
+ ==========
332
+
333
+ .. [1] https://en.wikipedia.org/wiki/Diagonal_matrix
334
+
335
+ """
336
+ name = "diagonal"
337
+ handler = Dispatcher("DiagonalHandler", doc="Handler for key 'diagonal'.")
338
+
339
+
340
+ class IntegerElementsPredicate(Predicate):
341
+ """
342
+ Integer elements matrix predicate.
343
+
344
+ Explanation
345
+ ===========
346
+
347
+ ``Q.integer_elements(x)`` is true iff all the elements of ``x``
348
+ are integers.
349
+
350
+ Examples
351
+ ========
352
+
353
+ >>> from sympy import Q, ask, MatrixSymbol
354
+ >>> X = MatrixSymbol('X', 4, 4)
355
+ >>> ask(Q.integer(X[1, 2]), Q.integer_elements(X))
356
+ True
357
+
358
+ """
359
+ name = "integer_elements"
360
+ handler = Dispatcher("IntegerElementsHandler", doc="Handler for key 'integer_elements'.")
361
+
362
+
363
+ class RealElementsPredicate(Predicate):
364
+ """
365
+ Real elements matrix predicate.
366
+
367
+ Explanation
368
+ ===========
369
+
370
+ ``Q.real_elements(x)`` is true iff all the elements of ``x``
371
+ are real numbers.
372
+
373
+ Examples
374
+ ========
375
+
376
+ >>> from sympy import Q, ask, MatrixSymbol
377
+ >>> X = MatrixSymbol('X', 4, 4)
378
+ >>> ask(Q.real(X[1, 2]), Q.real_elements(X))
379
+ True
380
+
381
+ """
382
+ name = "real_elements"
383
+ handler = Dispatcher("RealElementsHandler", doc="Handler for key 'real_elements'.")
384
+
385
+
386
+ class ComplexElementsPredicate(Predicate):
387
+ """
388
+ Complex elements matrix predicate.
389
+
390
+ Explanation
391
+ ===========
392
+
393
+ ``Q.complex_elements(x)`` is true iff all the elements of ``x``
394
+ are complex numbers.
395
+
396
+ Examples
397
+ ========
398
+
399
+ >>> from sympy import Q, ask, MatrixSymbol
400
+ >>> X = MatrixSymbol('X', 4, 4)
401
+ >>> ask(Q.complex(X[1, 2]), Q.complex_elements(X))
402
+ True
403
+ >>> ask(Q.complex_elements(X), Q.integer_elements(X))
404
+ True
405
+
406
+ """
407
+ name = "complex_elements"
408
+ handler = Dispatcher("ComplexElementsHandler", doc="Handler for key 'complex_elements'.")
409
+
410
+
411
+ class SingularPredicate(Predicate):
412
+ """
413
+ Singular matrix predicate.
414
+
415
+ A matrix is singular iff the value of its determinant is 0.
416
+
417
+ Examples
418
+ ========
419
+
420
+ >>> from sympy import Q, ask, MatrixSymbol
421
+ >>> X = MatrixSymbol('X', 4, 4)
422
+ >>> ask(Q.singular(X), Q.invertible(X))
423
+ False
424
+ >>> ask(Q.singular(X), ~Q.invertible(X))
425
+ True
426
+
427
+ References
428
+ ==========
429
+
430
+ .. [1] https://mathworld.wolfram.com/SingularMatrix.html
431
+
432
+ """
433
+ name = "singular"
434
+ handler = Dispatcher("SingularHandler", doc="Predicate fore key 'singular'.")
435
+
436
+
437
+ class NormalPredicate(Predicate):
438
+ """
439
+ Normal matrix predicate.
440
+
441
+ A matrix is normal if it commutes with its conjugate transpose.
442
+
443
+ Examples
444
+ ========
445
+
446
+ >>> from sympy import Q, ask, MatrixSymbol
447
+ >>> X = MatrixSymbol('X', 4, 4)
448
+ >>> ask(Q.normal(X), Q.unitary(X))
449
+ True
450
+
451
+ References
452
+ ==========
453
+
454
+ .. [1] https://en.wikipedia.org/wiki/Normal_matrix
455
+
456
+ """
457
+ name = "normal"
458
+ handler = Dispatcher("NormalHandler", doc="Predicate fore key 'normal'.")
459
+
460
+
461
+ class TriangularPredicate(Predicate):
462
+ """
463
+ Triangular matrix predicate.
464
+
465
+ Explanation
466
+ ===========
467
+
468
+ ``Q.triangular(X)`` is true if ``X`` is one that is either lower
469
+ triangular or upper triangular.
470
+
471
+ Examples
472
+ ========
473
+
474
+ >>> from sympy import Q, ask, MatrixSymbol
475
+ >>> X = MatrixSymbol('X', 4, 4)
476
+ >>> ask(Q.triangular(X), Q.upper_triangular(X))
477
+ True
478
+ >>> ask(Q.triangular(X), Q.lower_triangular(X))
479
+ True
480
+
481
+ References
482
+ ==========
483
+
484
+ .. [1] https://en.wikipedia.org/wiki/Triangular_matrix
485
+
486
+ """
487
+ name = "triangular"
488
+ handler = Dispatcher("TriangularHandler", doc="Predicate fore key 'triangular'.")
489
+
490
+
491
+ class UnitTriangularPredicate(Predicate):
492
+ """
493
+ Unit triangular matrix predicate.
494
+
495
+ Explanation
496
+ ===========
497
+
498
+ A unit triangular matrix is a triangular matrix with 1s
499
+ on the diagonal.
500
+
501
+ Examples
502
+ ========
503
+
504
+ >>> from sympy import Q, ask, MatrixSymbol
505
+ >>> X = MatrixSymbol('X', 4, 4)
506
+ >>> ask(Q.triangular(X), Q.unit_triangular(X))
507
+ True
508
+
509
+ """
510
+ name = "unit_triangular"
511
+ handler = Dispatcher("UnitTriangularHandler", doc="Predicate fore key 'unit_triangular'.")
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/ntheory.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.assumptions import Predicate
2
+ from sympy.multipledispatch import Dispatcher
3
+
4
+
5
+ class PrimePredicate(Predicate):
6
+ """
7
+ Prime number predicate.
8
+
9
+ Explanation
10
+ ===========
11
+
12
+ ``ask(Q.prime(x))`` is true iff ``x`` is a natural number greater
13
+ than 1 that has no positive divisors other than ``1`` and the
14
+ number itself.
15
+
16
+ Examples
17
+ ========
18
+
19
+ >>> from sympy import Q, ask
20
+ >>> ask(Q.prime(0))
21
+ False
22
+ >>> ask(Q.prime(1))
23
+ False
24
+ >>> ask(Q.prime(2))
25
+ True
26
+ >>> ask(Q.prime(20))
27
+ False
28
+ >>> ask(Q.prime(-3))
29
+ False
30
+
31
+ """
32
+ name = 'prime'
33
+ handler = Dispatcher(
34
+ "PrimeHandler",
35
+ doc=("Handler for key 'prime'. Test that an expression represents a prime"
36
+ " number. When the expression is an exact number, the result (when True)"
37
+ " is subject to the limitations of isprime() which is used to return the "
38
+ "result.")
39
+ )
40
+
41
+
42
+ class CompositePredicate(Predicate):
43
+ """
44
+ Composite number predicate.
45
+
46
+ Explanation
47
+ ===========
48
+
49
+ ``ask(Q.composite(x))`` is true iff ``x`` is a positive integer and has
50
+ at least one positive divisor other than ``1`` and the number itself.
51
+
52
+ Examples
53
+ ========
54
+
55
+ >>> from sympy import Q, ask
56
+ >>> ask(Q.composite(0))
57
+ False
58
+ >>> ask(Q.composite(1))
59
+ False
60
+ >>> ask(Q.composite(2))
61
+ False
62
+ >>> ask(Q.composite(20))
63
+ True
64
+
65
+ """
66
+ name = 'composite'
67
+ handler = Dispatcher("CompositeHandler", doc="Handler for key 'composite'.")
68
+
69
+
70
+ class EvenPredicate(Predicate):
71
+ """
72
+ Even number predicate.
73
+
74
+ Explanation
75
+ ===========
76
+
77
+ ``ask(Q.even(x))`` is true iff ``x`` belongs to the set of even
78
+ integers.
79
+
80
+ Examples
81
+ ========
82
+
83
+ >>> from sympy import Q, ask, pi
84
+ >>> ask(Q.even(0))
85
+ True
86
+ >>> ask(Q.even(2))
87
+ True
88
+ >>> ask(Q.even(3))
89
+ False
90
+ >>> ask(Q.even(pi))
91
+ False
92
+
93
+ """
94
+ name = 'even'
95
+ handler = Dispatcher("EvenHandler", doc="Handler for key 'even'.")
96
+
97
+
98
+ class OddPredicate(Predicate):
99
+ """
100
+ Odd number predicate.
101
+
102
+ Explanation
103
+ ===========
104
+
105
+ ``ask(Q.odd(x))`` is true iff ``x`` belongs to the set of odd numbers.
106
+
107
+ Examples
108
+ ========
109
+
110
+ >>> from sympy import Q, ask, pi
111
+ >>> ask(Q.odd(0))
112
+ False
113
+ >>> ask(Q.odd(2))
114
+ False
115
+ >>> ask(Q.odd(3))
116
+ True
117
+ >>> ask(Q.odd(pi))
118
+ False
119
+
120
+ """
121
+ name = 'odd'
122
+ handler = Dispatcher(
123
+ "OddHandler",
124
+ doc=("Handler for key 'odd'. Test that an expression represents an odd"
125
+ " number.")
126
+ )
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/order.py ADDED
@@ -0,0 +1,390 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.assumptions import Predicate
2
+ from sympy.multipledispatch import Dispatcher
3
+
4
+
5
+ class NegativePredicate(Predicate):
6
+ r"""
7
+ Negative number predicate.
8
+
9
+ Explanation
10
+ ===========
11
+
12
+ ``Q.negative(x)`` is true iff ``x`` is a real number and :math:`x < 0`, that is,
13
+ it is in the interval :math:`(-\infty, 0)`. Note in particular that negative
14
+ infinity is not negative.
15
+
16
+ A few important facts about negative numbers:
17
+
18
+ - Note that ``Q.nonnegative`` and ``~Q.negative`` are *not* the same
19
+ thing. ``~Q.negative(x)`` simply means that ``x`` is not negative,
20
+ whereas ``Q.nonnegative(x)`` means that ``x`` is real and not
21
+ negative, i.e., ``Q.nonnegative(x)`` is logically equivalent to
22
+ ``Q.zero(x) | Q.positive(x)``. So for example, ``~Q.negative(I)`` is
23
+ true, whereas ``Q.nonnegative(I)`` is false.
24
+
25
+ - See the documentation of ``Q.real`` for more information about
26
+ related facts.
27
+
28
+ Examples
29
+ ========
30
+
31
+ >>> from sympy import Q, ask, symbols, I
32
+ >>> x = symbols('x')
33
+ >>> ask(Q.negative(x), Q.real(x) & ~Q.positive(x) & ~Q.zero(x))
34
+ True
35
+ >>> ask(Q.negative(-1))
36
+ True
37
+ >>> ask(Q.nonnegative(I))
38
+ False
39
+ >>> ask(~Q.negative(I))
40
+ True
41
+
42
+ """
43
+ name = 'negative'
44
+ handler = Dispatcher(
45
+ "NegativeHandler",
46
+ doc=("Handler for Q.negative. Test that an expression is strictly less"
47
+ " than zero.")
48
+ )
49
+
50
+
51
+ class NonNegativePredicate(Predicate):
52
+ """
53
+ Nonnegative real number predicate.
54
+
55
+ Explanation
56
+ ===========
57
+
58
+ ``ask(Q.nonnegative(x))`` is true iff ``x`` belongs to the set of
59
+ positive numbers including zero.
60
+
61
+ - Note that ``Q.nonnegative`` and ``~Q.negative`` are *not* the same
62
+ thing. ``~Q.negative(x)`` simply means that ``x`` is not negative,
63
+ whereas ``Q.nonnegative(x)`` means that ``x`` is real and not
64
+ negative, i.e., ``Q.nonnegative(x)`` is logically equivalent to
65
+ ``Q.zero(x) | Q.positive(x)``. So for example, ``~Q.negative(I)`` is
66
+ true, whereas ``Q.nonnegative(I)`` is false.
67
+
68
+ Examples
69
+ ========
70
+
71
+ >>> from sympy import Q, ask, I
72
+ >>> ask(Q.nonnegative(1))
73
+ True
74
+ >>> ask(Q.nonnegative(0))
75
+ True
76
+ >>> ask(Q.nonnegative(-1))
77
+ False
78
+ >>> ask(Q.nonnegative(I))
79
+ False
80
+ >>> ask(Q.nonnegative(-I))
81
+ False
82
+
83
+ """
84
+ name = 'nonnegative'
85
+ handler = Dispatcher(
86
+ "NonNegativeHandler",
87
+ doc=("Handler for Q.nonnegative.")
88
+ )
89
+
90
+
91
+ class NonZeroPredicate(Predicate):
92
+ """
93
+ Nonzero real number predicate.
94
+
95
+ Explanation
96
+ ===========
97
+
98
+ ``ask(Q.nonzero(x))`` is true iff ``x`` is real and ``x`` is not zero. Note in
99
+ particular that ``Q.nonzero(x)`` is false if ``x`` is not real. Use
100
+ ``~Q.zero(x)`` if you want the negation of being zero without any real
101
+ assumptions.
102
+
103
+ A few important facts about nonzero numbers:
104
+
105
+ - ``Q.nonzero`` is logically equivalent to ``Q.positive | Q.negative``.
106
+
107
+ - See the documentation of ``Q.real`` for more information about
108
+ related facts.
109
+
110
+ Examples
111
+ ========
112
+
113
+ >>> from sympy import Q, ask, symbols, I, oo
114
+ >>> x = symbols('x')
115
+ >>> print(ask(Q.nonzero(x), ~Q.zero(x)))
116
+ None
117
+ >>> ask(Q.nonzero(x), Q.positive(x))
118
+ True
119
+ >>> ask(Q.nonzero(x), Q.zero(x))
120
+ False
121
+ >>> ask(Q.nonzero(0))
122
+ False
123
+ >>> ask(Q.nonzero(I))
124
+ False
125
+ >>> ask(~Q.zero(I))
126
+ True
127
+ >>> ask(Q.nonzero(oo))
128
+ False
129
+
130
+ """
131
+ name = 'nonzero'
132
+ handler = Dispatcher(
133
+ "NonZeroHandler",
134
+ doc=("Handler for key 'zero'. Test that an expression is not identically"
135
+ " zero.")
136
+ )
137
+
138
+
139
+ class ZeroPredicate(Predicate):
140
+ """
141
+ Zero number predicate.
142
+
143
+ Explanation
144
+ ===========
145
+
146
+ ``ask(Q.zero(x))`` is true iff the value of ``x`` is zero.
147
+
148
+ Examples
149
+ ========
150
+
151
+ >>> from sympy import ask, Q, oo, symbols
152
+ >>> x, y = symbols('x, y')
153
+ >>> ask(Q.zero(0))
154
+ True
155
+ >>> ask(Q.zero(1/oo))
156
+ True
157
+ >>> print(ask(Q.zero(0*oo)))
158
+ None
159
+ >>> ask(Q.zero(1))
160
+ False
161
+ >>> ask(Q.zero(x*y), Q.zero(x) | Q.zero(y))
162
+ True
163
+
164
+ """
165
+ name = 'zero'
166
+ handler = Dispatcher(
167
+ "ZeroHandler",
168
+ doc="Handler for key 'zero'."
169
+ )
170
+
171
+
172
+ class NonPositivePredicate(Predicate):
173
+ """
174
+ Nonpositive real number predicate.
175
+
176
+ Explanation
177
+ ===========
178
+
179
+ ``ask(Q.nonpositive(x))`` is true iff ``x`` belongs to the set of
180
+ negative numbers including zero.
181
+
182
+ - Note that ``Q.nonpositive`` and ``~Q.positive`` are *not* the same
183
+ thing. ``~Q.positive(x)`` simply means that ``x`` is not positive,
184
+ whereas ``Q.nonpositive(x)`` means that ``x`` is real and not
185
+ positive, i.e., ``Q.nonpositive(x)`` is logically equivalent to
186
+ `Q.negative(x) | Q.zero(x)``. So for example, ``~Q.positive(I)`` is
187
+ true, whereas ``Q.nonpositive(I)`` is false.
188
+
189
+ Examples
190
+ ========
191
+
192
+ >>> from sympy import Q, ask, I
193
+
194
+ >>> ask(Q.nonpositive(-1))
195
+ True
196
+ >>> ask(Q.nonpositive(0))
197
+ True
198
+ >>> ask(Q.nonpositive(1))
199
+ False
200
+ >>> ask(Q.nonpositive(I))
201
+ False
202
+ >>> ask(Q.nonpositive(-I))
203
+ False
204
+
205
+ """
206
+ name = 'nonpositive'
207
+ handler = Dispatcher(
208
+ "NonPositiveHandler",
209
+ doc="Handler for key 'nonpositive'."
210
+ )
211
+
212
+
213
+ class PositivePredicate(Predicate):
214
+ r"""
215
+ Positive real number predicate.
216
+
217
+ Explanation
218
+ ===========
219
+
220
+ ``Q.positive(x)`` is true iff ``x`` is real and `x > 0`, that is if ``x``
221
+ is in the interval `(0, \infty)`. In particular, infinity is not
222
+ positive.
223
+
224
+ A few important facts about positive numbers:
225
+
226
+ - Note that ``Q.nonpositive`` and ``~Q.positive`` are *not* the same
227
+ thing. ``~Q.positive(x)`` simply means that ``x`` is not positive,
228
+ whereas ``Q.nonpositive(x)`` means that ``x`` is real and not
229
+ positive, i.e., ``Q.nonpositive(x)`` is logically equivalent to
230
+ `Q.negative(x) | Q.zero(x)``. So for example, ``~Q.positive(I)`` is
231
+ true, whereas ``Q.nonpositive(I)`` is false.
232
+
233
+ - See the documentation of ``Q.real`` for more information about
234
+ related facts.
235
+
236
+ Examples
237
+ ========
238
+
239
+ >>> from sympy import Q, ask, symbols, I
240
+ >>> x = symbols('x')
241
+ >>> ask(Q.positive(x), Q.real(x) & ~Q.negative(x) & ~Q.zero(x))
242
+ True
243
+ >>> ask(Q.positive(1))
244
+ True
245
+ >>> ask(Q.nonpositive(I))
246
+ False
247
+ >>> ask(~Q.positive(I))
248
+ True
249
+
250
+ """
251
+ name = 'positive'
252
+ handler = Dispatcher(
253
+ "PositiveHandler",
254
+ doc=("Handler for key 'positive'. Test that an expression is strictly"
255
+ " greater than zero.")
256
+ )
257
+
258
+
259
+ class ExtendedPositivePredicate(Predicate):
260
+ r"""
261
+ Positive extended real number predicate.
262
+
263
+ Explanation
264
+ ===========
265
+
266
+ ``Q.extended_positive(x)`` is true iff ``x`` is extended real and
267
+ `x > 0`, that is if ``x`` is in the interval `(0, \infty]`.
268
+
269
+ Examples
270
+ ========
271
+
272
+ >>> from sympy import ask, I, oo, Q
273
+ >>> ask(Q.extended_positive(1))
274
+ True
275
+ >>> ask(Q.extended_positive(oo))
276
+ True
277
+ >>> ask(Q.extended_positive(I))
278
+ False
279
+
280
+ """
281
+ name = 'extended_positive'
282
+ handler = Dispatcher("ExtendedPositiveHandler")
283
+
284
+
285
+ class ExtendedNegativePredicate(Predicate):
286
+ r"""
287
+ Negative extended real number predicate.
288
+
289
+ Explanation
290
+ ===========
291
+
292
+ ``Q.extended_negative(x)`` is true iff ``x`` is extended real and
293
+ `x < 0`, that is if ``x`` is in the interval `[-\infty, 0)`.
294
+
295
+ Examples
296
+ ========
297
+
298
+ >>> from sympy import ask, I, oo, Q
299
+ >>> ask(Q.extended_negative(-1))
300
+ True
301
+ >>> ask(Q.extended_negative(-oo))
302
+ True
303
+ >>> ask(Q.extended_negative(-I))
304
+ False
305
+
306
+ """
307
+ name = 'extended_negative'
308
+ handler = Dispatcher("ExtendedNegativeHandler")
309
+
310
+
311
+ class ExtendedNonZeroPredicate(Predicate):
312
+ """
313
+ Nonzero extended real number predicate.
314
+
315
+ Explanation
316
+ ===========
317
+
318
+ ``ask(Q.extended_nonzero(x))`` is true iff ``x`` is extended real and
319
+ ``x`` is not zero.
320
+
321
+ Examples
322
+ ========
323
+
324
+ >>> from sympy import ask, I, oo, Q
325
+ >>> ask(Q.extended_nonzero(-1))
326
+ True
327
+ >>> ask(Q.extended_nonzero(oo))
328
+ True
329
+ >>> ask(Q.extended_nonzero(I))
330
+ False
331
+
332
+ """
333
+ name = 'extended_nonzero'
334
+ handler = Dispatcher("ExtendedNonZeroHandler")
335
+
336
+
337
+ class ExtendedNonPositivePredicate(Predicate):
338
+ """
339
+ Nonpositive extended real number predicate.
340
+
341
+ Explanation
342
+ ===========
343
+
344
+ ``ask(Q.extended_nonpositive(x))`` is true iff ``x`` is extended real and
345
+ ``x`` is not positive.
346
+
347
+ Examples
348
+ ========
349
+
350
+ >>> from sympy import ask, I, oo, Q
351
+ >>> ask(Q.extended_nonpositive(-1))
352
+ True
353
+ >>> ask(Q.extended_nonpositive(oo))
354
+ False
355
+ >>> ask(Q.extended_nonpositive(0))
356
+ True
357
+ >>> ask(Q.extended_nonpositive(I))
358
+ False
359
+
360
+ """
361
+ name = 'extended_nonpositive'
362
+ handler = Dispatcher("ExtendedNonPositiveHandler")
363
+
364
+
365
+ class ExtendedNonNegativePredicate(Predicate):
366
+ """
367
+ Nonnegative extended real number predicate.
368
+
369
+ Explanation
370
+ ===========
371
+
372
+ ``ask(Q.extended_nonnegative(x))`` is true iff ``x`` is extended real and
373
+ ``x`` is not negative.
374
+
375
+ Examples
376
+ ========
377
+
378
+ >>> from sympy import ask, I, oo, Q
379
+ >>> ask(Q.extended_nonnegative(-1))
380
+ False
381
+ >>> ask(Q.extended_nonnegative(oo))
382
+ True
383
+ >>> ask(Q.extended_nonnegative(0))
384
+ True
385
+ >>> ask(Q.extended_nonnegative(I))
386
+ False
387
+
388
+ """
389
+ name = 'extended_nonnegative'
390
+ handler = Dispatcher("ExtendedNonNegativeHandler")
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/predicates/sets.py ADDED
@@ -0,0 +1,387 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.assumptions import Predicate
2
+ from sympy.multipledispatch import Dispatcher
3
+
4
+
5
+ class IntegerPredicate(Predicate):
6
+ """
7
+ Integer predicate.
8
+
9
+ Explanation
10
+ ===========
11
+
12
+ ``Q.integer(x)`` is true iff ``x`` belongs to the set of integer
13
+ numbers.
14
+
15
+ Examples
16
+ ========
17
+
18
+ >>> from sympy import Q, ask, S
19
+ >>> ask(Q.integer(5))
20
+ True
21
+ >>> ask(Q.integer(S(1)/2))
22
+ False
23
+
24
+ References
25
+ ==========
26
+
27
+ .. [1] https://en.wikipedia.org/wiki/Integer
28
+
29
+ """
30
+ name = 'integer'
31
+ handler = Dispatcher(
32
+ "IntegerHandler",
33
+ doc=("Handler for Q.integer.\n\n"
34
+ "Test that an expression belongs to the field of integer numbers.")
35
+ )
36
+
37
+
38
+ class RationalPredicate(Predicate):
39
+ """
40
+ Rational number predicate.
41
+
42
+ Explanation
43
+ ===========
44
+
45
+ ``Q.rational(x)`` is true iff ``x`` belongs to the set of
46
+ rational numbers.
47
+
48
+ Examples
49
+ ========
50
+
51
+ >>> from sympy import ask, Q, pi, S
52
+ >>> ask(Q.rational(0))
53
+ True
54
+ >>> ask(Q.rational(S(1)/2))
55
+ True
56
+ >>> ask(Q.rational(pi))
57
+ False
58
+
59
+ References
60
+ ==========
61
+
62
+ .. [1] https://en.wikipedia.org/wiki/Rational_number
63
+
64
+ """
65
+ name = 'rational'
66
+ handler = Dispatcher(
67
+ "RationalHandler",
68
+ doc=("Handler for Q.rational.\n\n"
69
+ "Test that an expression belongs to the field of rational numbers.")
70
+ )
71
+
72
+
73
+ class IrrationalPredicate(Predicate):
74
+ """
75
+ Irrational number predicate.
76
+
77
+ Explanation
78
+ ===========
79
+
80
+ ``Q.irrational(x)`` is true iff ``x`` is any real number that
81
+ cannot be expressed as a ratio of integers.
82
+
83
+ Examples
84
+ ========
85
+
86
+ >>> from sympy import ask, Q, pi, S, I
87
+ >>> ask(Q.irrational(0))
88
+ False
89
+ >>> ask(Q.irrational(S(1)/2))
90
+ False
91
+ >>> ask(Q.irrational(pi))
92
+ True
93
+ >>> ask(Q.irrational(I))
94
+ False
95
+
96
+ References
97
+ ==========
98
+
99
+ .. [1] https://en.wikipedia.org/wiki/Irrational_number
100
+
101
+ """
102
+ name = 'irrational'
103
+ handler = Dispatcher(
104
+ "IrrationalHandler",
105
+ doc=("Handler for Q.irrational.\n\n"
106
+ "Test that an expression is irrational numbers.")
107
+ )
108
+
109
+
110
+ class RealPredicate(Predicate):
111
+ r"""
112
+ Real number predicate.
113
+
114
+ Explanation
115
+ ===========
116
+
117
+ ``Q.real(x)`` is true iff ``x`` is a real number, i.e., it is in the
118
+ interval `(-\infty, \infty)`. Note that, in particular the
119
+ infinities are not real. Use ``Q.extended_real`` if you want to
120
+ consider those as well.
121
+
122
+ A few important facts about reals:
123
+
124
+ - Every real number is positive, negative, or zero. Furthermore,
125
+ because these sets are pairwise disjoint, each real number is
126
+ exactly one of those three.
127
+
128
+ - Every real number is also complex.
129
+
130
+ - Every real number is finite.
131
+
132
+ - Every real number is either rational or irrational.
133
+
134
+ - Every real number is either algebraic or transcendental.
135
+
136
+ - The facts ``Q.negative``, ``Q.zero``, ``Q.positive``,
137
+ ``Q.nonnegative``, ``Q.nonpositive``, ``Q.nonzero``,
138
+ ``Q.integer``, ``Q.rational``, and ``Q.irrational`` all imply
139
+ ``Q.real``, as do all facts that imply those facts.
140
+
141
+ - The facts ``Q.algebraic``, and ``Q.transcendental`` do not imply
142
+ ``Q.real``; they imply ``Q.complex``. An algebraic or
143
+ transcendental number may or may not be real.
144
+
145
+ - The "non" facts (i.e., ``Q.nonnegative``, ``Q.nonzero``,
146
+ ``Q.nonpositive`` and ``Q.noninteger``) are not equivalent to
147
+ not the fact, but rather, not the fact *and* ``Q.real``.
148
+ For example, ``Q.nonnegative`` means ``~Q.negative & Q.real``.
149
+ So for example, ``I`` is not nonnegative, nonzero, or
150
+ nonpositive.
151
+
152
+ Examples
153
+ ========
154
+
155
+ >>> from sympy import Q, ask, symbols
156
+ >>> x = symbols('x')
157
+ >>> ask(Q.real(x), Q.positive(x))
158
+ True
159
+ >>> ask(Q.real(0))
160
+ True
161
+
162
+ References
163
+ ==========
164
+
165
+ .. [1] https://en.wikipedia.org/wiki/Real_number
166
+
167
+ """
168
+ name = 'real'
169
+ handler = Dispatcher(
170
+ "RealHandler",
171
+ doc=("Handler for Q.real.\n\n"
172
+ "Test that an expression belongs to the field of real numbers.")
173
+ )
174
+
175
+
176
+ class ExtendedRealPredicate(Predicate):
177
+ r"""
178
+ Extended real predicate.
179
+
180
+ Explanation
181
+ ===========
182
+
183
+ ``Q.extended_real(x)`` is true iff ``x`` is a real number or
184
+ `\{-\infty, \infty\}`.
185
+
186
+ See documentation of ``Q.real`` for more information about related
187
+ facts.
188
+
189
+ Examples
190
+ ========
191
+
192
+ >>> from sympy import ask, Q, oo, I
193
+ >>> ask(Q.extended_real(1))
194
+ True
195
+ >>> ask(Q.extended_real(I))
196
+ False
197
+ >>> ask(Q.extended_real(oo))
198
+ True
199
+
200
+ """
201
+ name = 'extended_real'
202
+ handler = Dispatcher(
203
+ "ExtendedRealHandler",
204
+ doc=("Handler for Q.extended_real.\n\n"
205
+ "Test that an expression belongs to the field of extended real\n"
206
+ "numbers, that is real numbers union {Infinity, -Infinity}.")
207
+ )
208
+
209
+
210
+ class HermitianPredicate(Predicate):
211
+ """
212
+ Hermitian predicate.
213
+
214
+ Explanation
215
+ ===========
216
+
217
+ ``ask(Q.hermitian(x))`` is true iff ``x`` belongs to the set of
218
+ Hermitian operators.
219
+
220
+ References
221
+ ==========
222
+
223
+ .. [1] https://mathworld.wolfram.com/HermitianOperator.html
224
+
225
+ """
226
+ # TODO: Add examples
227
+ name = 'hermitian'
228
+ handler = Dispatcher(
229
+ "HermitianHandler",
230
+ doc=("Handler for Q.hermitian.\n\n"
231
+ "Test that an expression belongs to the field of Hermitian operators.")
232
+ )
233
+
234
+
235
+ class ComplexPredicate(Predicate):
236
+ """
237
+ Complex number predicate.
238
+
239
+ Explanation
240
+ ===========
241
+
242
+ ``Q.complex(x)`` is true iff ``x`` belongs to the set of complex
243
+ numbers. Note that every complex number is finite.
244
+
245
+ Examples
246
+ ========
247
+
248
+ >>> from sympy import Q, Symbol, ask, I, oo
249
+ >>> x = Symbol('x')
250
+ >>> ask(Q.complex(0))
251
+ True
252
+ >>> ask(Q.complex(2 + 3*I))
253
+ True
254
+ >>> ask(Q.complex(oo))
255
+ False
256
+
257
+ References
258
+ ==========
259
+
260
+ .. [1] https://en.wikipedia.org/wiki/Complex_number
261
+
262
+ """
263
+ name = 'complex'
264
+ handler = Dispatcher(
265
+ "ComplexHandler",
266
+ doc=("Handler for Q.complex.\n\n"
267
+ "Test that an expression belongs to the field of complex numbers.")
268
+ )
269
+
270
+
271
+ class ImaginaryPredicate(Predicate):
272
+ """
273
+ Imaginary number predicate.
274
+
275
+ Explanation
276
+ ===========
277
+
278
+ ``Q.imaginary(x)`` is true iff ``x`` can be written as a real
279
+ number multiplied by the imaginary unit ``I``. Please note that ``0``
280
+ is not considered to be an imaginary number.
281
+
282
+ Examples
283
+ ========
284
+
285
+ >>> from sympy import Q, ask, I
286
+ >>> ask(Q.imaginary(3*I))
287
+ True
288
+ >>> ask(Q.imaginary(2 + 3*I))
289
+ False
290
+ >>> ask(Q.imaginary(0))
291
+ False
292
+
293
+ References
294
+ ==========
295
+
296
+ .. [1] https://en.wikipedia.org/wiki/Imaginary_number
297
+
298
+ """
299
+ name = 'imaginary'
300
+ handler = Dispatcher(
301
+ "ImaginaryHandler",
302
+ doc=("Handler for Q.imaginary.\n\n"
303
+ "Test that an expression belongs to the field of imaginary numbers,\n"
304
+ "that is, numbers in the form x*I, where x is real.")
305
+ )
306
+
307
+
308
+ class AntihermitianPredicate(Predicate):
309
+ """
310
+ Antihermitian predicate.
311
+
312
+ Explanation
313
+ ===========
314
+
315
+ ``Q.antihermitian(x)`` is true iff ``x`` belongs to the field of
316
+ antihermitian operators, i.e., operators in the form ``x*I``, where
317
+ ``x`` is Hermitian.
318
+
319
+ References
320
+ ==========
321
+
322
+ .. [1] https://mathworld.wolfram.com/HermitianOperator.html
323
+
324
+ """
325
+ # TODO: Add examples
326
+ name = 'antihermitian'
327
+ handler = Dispatcher(
328
+ "AntiHermitianHandler",
329
+ doc=("Handler for Q.antihermitian.\n\n"
330
+ "Test that an expression belongs to the field of anti-Hermitian\n"
331
+ "operators, that is, operators in the form x*I, where x is Hermitian.")
332
+ )
333
+
334
+
335
+ class AlgebraicPredicate(Predicate):
336
+ r"""
337
+ Algebraic number predicate.
338
+
339
+ Explanation
340
+ ===========
341
+
342
+ ``Q.algebraic(x)`` is true iff ``x`` belongs to the set of
343
+ algebraic numbers. ``x`` is algebraic if there is some polynomial
344
+ in ``p(x)\in \mathbb\{Q\}[x]`` such that ``p(x) = 0``.
345
+
346
+ Examples
347
+ ========
348
+
349
+ >>> from sympy import ask, Q, sqrt, I, pi
350
+ >>> ask(Q.algebraic(sqrt(2)))
351
+ True
352
+ >>> ask(Q.algebraic(I))
353
+ True
354
+ >>> ask(Q.algebraic(pi))
355
+ False
356
+
357
+ References
358
+ ==========
359
+
360
+ .. [1] https://en.wikipedia.org/wiki/Algebraic_number
361
+
362
+ """
363
+ name = 'algebraic'
364
+ AlgebraicHandler = Dispatcher(
365
+ "AlgebraicHandler",
366
+ doc="""Handler for Q.algebraic key."""
367
+ )
368
+
369
+
370
+ class TranscendentalPredicate(Predicate):
371
+ """
372
+ Transcedental number predicate.
373
+
374
+ Explanation
375
+ ===========
376
+
377
+ ``Q.transcendental(x)`` is true iff ``x`` belongs to the set of
378
+ transcendental numbers. A transcendental number is a real
379
+ or complex number that is not algebraic.
380
+
381
+ """
382
+ # TODO: Add examples
383
+ name = 'transcendental'
384
+ handler = Dispatcher(
385
+ "Transcendental",
386
+ doc="""Handler for Q.transcendental key."""
387
+ )
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/refine.py ADDED
@@ -0,0 +1,405 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+ from typing import Callable
3
+
4
+ from sympy.core import S, Add, Expr, Basic, Mul, Pow, Rational
5
+ from sympy.core.logic import fuzzy_not
6
+ from sympy.logic.boolalg import Boolean
7
+
8
+ from sympy.assumptions import ask, Q # type: ignore
9
+
10
+
11
+ def refine(expr, assumptions=True):
12
+ """
13
+ Simplify an expression using assumptions.
14
+
15
+ Explanation
16
+ ===========
17
+
18
+ Unlike :func:`~.simplify()` which performs structural simplification
19
+ without any assumption, this function transforms the expression into
20
+ the form which is only valid under certain assumptions. Note that
21
+ ``simplify()`` is generally not done in refining process.
22
+
23
+ Refining boolean expression involves reducing it to ``S.true`` or
24
+ ``S.false``. Unlike :func:`~.ask()`, the expression will not be reduced
25
+ if the truth value cannot be determined.
26
+
27
+ Examples
28
+ ========
29
+
30
+ >>> from sympy import refine, sqrt, Q
31
+ >>> from sympy.abc import x
32
+ >>> refine(sqrt(x**2), Q.real(x))
33
+ Abs(x)
34
+ >>> refine(sqrt(x**2), Q.positive(x))
35
+ x
36
+
37
+ >>> refine(Q.real(x), Q.positive(x))
38
+ True
39
+ >>> refine(Q.positive(x), Q.real(x))
40
+ Q.positive(x)
41
+
42
+ See Also
43
+ ========
44
+
45
+ sympy.simplify.simplify.simplify : Structural simplification without assumptions.
46
+ sympy.assumptions.ask.ask : Query for boolean expressions using assumptions.
47
+ """
48
+ if not isinstance(expr, Basic):
49
+ return expr
50
+
51
+ if not expr.is_Atom:
52
+ args = [refine(arg, assumptions) for arg in expr.args]
53
+ # TODO: this will probably not work with Integral or Polynomial
54
+ expr = expr.func(*args)
55
+ if hasattr(expr, '_eval_refine'):
56
+ ref_expr = expr._eval_refine(assumptions)
57
+ if ref_expr is not None:
58
+ return ref_expr
59
+ name = expr.__class__.__name__
60
+ handler = handlers_dict.get(name, None)
61
+ if handler is None:
62
+ return expr
63
+ new_expr = handler(expr, assumptions)
64
+ if (new_expr is None) or (expr == new_expr):
65
+ return expr
66
+ if not isinstance(new_expr, Expr):
67
+ return new_expr
68
+ return refine(new_expr, assumptions)
69
+
70
+
71
+ def refine_abs(expr, assumptions):
72
+ """
73
+ Handler for the absolute value.
74
+
75
+ Examples
76
+ ========
77
+
78
+ >>> from sympy import Q, Abs
79
+ >>> from sympy.assumptions.refine import refine_abs
80
+ >>> from sympy.abc import x
81
+ >>> refine_abs(Abs(x), Q.real(x))
82
+ >>> refine_abs(Abs(x), Q.positive(x))
83
+ x
84
+ >>> refine_abs(Abs(x), Q.negative(x))
85
+ -x
86
+
87
+ """
88
+ from sympy.functions.elementary.complexes import Abs
89
+ arg = expr.args[0]
90
+ if ask(Q.real(arg), assumptions) and \
91
+ fuzzy_not(ask(Q.negative(arg), assumptions)):
92
+ # if it's nonnegative
93
+ return arg
94
+ if ask(Q.negative(arg), assumptions):
95
+ return -arg
96
+ # arg is Mul
97
+ if isinstance(arg, Mul):
98
+ r = [refine(abs(a), assumptions) for a in arg.args]
99
+ non_abs = []
100
+ in_abs = []
101
+ for i in r:
102
+ if isinstance(i, Abs):
103
+ in_abs.append(i.args[0])
104
+ else:
105
+ non_abs.append(i)
106
+ return Mul(*non_abs) * Abs(Mul(*in_abs))
107
+
108
+
109
+ def refine_Pow(expr, assumptions):
110
+ """
111
+ Handler for instances of Pow.
112
+
113
+ Examples
114
+ ========
115
+
116
+ >>> from sympy import Q
117
+ >>> from sympy.assumptions.refine import refine_Pow
118
+ >>> from sympy.abc import x,y,z
119
+ >>> refine_Pow((-1)**x, Q.real(x))
120
+ >>> refine_Pow((-1)**x, Q.even(x))
121
+ 1
122
+ >>> refine_Pow((-1)**x, Q.odd(x))
123
+ -1
124
+
125
+ For powers of -1, even parts of the exponent can be simplified:
126
+
127
+ >>> refine_Pow((-1)**(x+y), Q.even(x))
128
+ (-1)**y
129
+ >>> refine_Pow((-1)**(x+y+z), Q.odd(x) & Q.odd(z))
130
+ (-1)**y
131
+ >>> refine_Pow((-1)**(x+y+2), Q.odd(x))
132
+ (-1)**(y + 1)
133
+ >>> refine_Pow((-1)**(x+3), True)
134
+ (-1)**(x + 1)
135
+
136
+ """
137
+ from sympy.functions.elementary.complexes import Abs
138
+ from sympy.functions import sign
139
+ if isinstance(expr.base, Abs):
140
+ if ask(Q.real(expr.base.args[0]), assumptions) and \
141
+ ask(Q.even(expr.exp), assumptions):
142
+ return expr.base.args[0] ** expr.exp
143
+ if ask(Q.real(expr.base), assumptions):
144
+ if expr.base.is_number:
145
+ if ask(Q.even(expr.exp), assumptions):
146
+ return abs(expr.base) ** expr.exp
147
+ if ask(Q.odd(expr.exp), assumptions):
148
+ return sign(expr.base) * abs(expr.base) ** expr.exp
149
+ if isinstance(expr.exp, Rational):
150
+ if isinstance(expr.base, Pow):
151
+ return abs(expr.base.base) ** (expr.base.exp * expr.exp)
152
+
153
+ if expr.base is S.NegativeOne:
154
+ if expr.exp.is_Add:
155
+
156
+ old = expr
157
+
158
+ # For powers of (-1) we can remove
159
+ # - even terms
160
+ # - pairs of odd terms
161
+ # - a single odd term + 1
162
+ # - A numerical constant N can be replaced with mod(N,2)
163
+
164
+ coeff, terms = expr.exp.as_coeff_add()
165
+ terms = set(terms)
166
+ even_terms = set()
167
+ odd_terms = set()
168
+ initial_number_of_terms = len(terms)
169
+
170
+ for t in terms:
171
+ if ask(Q.even(t), assumptions):
172
+ even_terms.add(t)
173
+ elif ask(Q.odd(t), assumptions):
174
+ odd_terms.add(t)
175
+
176
+ terms -= even_terms
177
+ if len(odd_terms) % 2:
178
+ terms -= odd_terms
179
+ new_coeff = (coeff + S.One) % 2
180
+ else:
181
+ terms -= odd_terms
182
+ new_coeff = coeff % 2
183
+
184
+ if new_coeff != coeff or len(terms) < initial_number_of_terms:
185
+ terms.add(new_coeff)
186
+ expr = expr.base**(Add(*terms))
187
+
188
+ # Handle (-1)**((-1)**n/2 + m/2)
189
+ e2 = 2*expr.exp
190
+ if ask(Q.even(e2), assumptions):
191
+ if e2.could_extract_minus_sign():
192
+ e2 *= expr.base
193
+ if e2.is_Add:
194
+ i, p = e2.as_two_terms()
195
+ if p.is_Pow and p.base is S.NegativeOne:
196
+ if ask(Q.integer(p.exp), assumptions):
197
+ i = (i + 1)/2
198
+ if ask(Q.even(i), assumptions):
199
+ return expr.base**p.exp
200
+ elif ask(Q.odd(i), assumptions):
201
+ return expr.base**(p.exp + 1)
202
+ else:
203
+ return expr.base**(p.exp + i)
204
+
205
+ if old != expr:
206
+ return expr
207
+
208
+
209
+ def refine_atan2(expr, assumptions):
210
+ """
211
+ Handler for the atan2 function.
212
+
213
+ Examples
214
+ ========
215
+
216
+ >>> from sympy import Q, atan2
217
+ >>> from sympy.assumptions.refine import refine_atan2
218
+ >>> from sympy.abc import x, y
219
+ >>> refine_atan2(atan2(y,x), Q.real(y) & Q.positive(x))
220
+ atan(y/x)
221
+ >>> refine_atan2(atan2(y,x), Q.negative(y) & Q.negative(x))
222
+ atan(y/x) - pi
223
+ >>> refine_atan2(atan2(y,x), Q.positive(y) & Q.negative(x))
224
+ atan(y/x) + pi
225
+ >>> refine_atan2(atan2(y,x), Q.zero(y) & Q.negative(x))
226
+ pi
227
+ >>> refine_atan2(atan2(y,x), Q.positive(y) & Q.zero(x))
228
+ pi/2
229
+ >>> refine_atan2(atan2(y,x), Q.negative(y) & Q.zero(x))
230
+ -pi/2
231
+ >>> refine_atan2(atan2(y,x), Q.zero(y) & Q.zero(x))
232
+ nan
233
+ """
234
+ from sympy.functions.elementary.trigonometric import atan
235
+ y, x = expr.args
236
+ if ask(Q.real(y) & Q.positive(x), assumptions):
237
+ return atan(y / x)
238
+ elif ask(Q.negative(y) & Q.negative(x), assumptions):
239
+ return atan(y / x) - S.Pi
240
+ elif ask(Q.positive(y) & Q.negative(x), assumptions):
241
+ return atan(y / x) + S.Pi
242
+ elif ask(Q.zero(y) & Q.negative(x), assumptions):
243
+ return S.Pi
244
+ elif ask(Q.positive(y) & Q.zero(x), assumptions):
245
+ return S.Pi/2
246
+ elif ask(Q.negative(y) & Q.zero(x), assumptions):
247
+ return -S.Pi/2
248
+ elif ask(Q.zero(y) & Q.zero(x), assumptions):
249
+ return S.NaN
250
+ else:
251
+ return expr
252
+
253
+
254
+ def refine_re(expr, assumptions):
255
+ """
256
+ Handler for real part.
257
+
258
+ Examples
259
+ ========
260
+
261
+ >>> from sympy.assumptions.refine import refine_re
262
+ >>> from sympy import Q, re
263
+ >>> from sympy.abc import x
264
+ >>> refine_re(re(x), Q.real(x))
265
+ x
266
+ >>> refine_re(re(x), Q.imaginary(x))
267
+ 0
268
+ """
269
+ arg = expr.args[0]
270
+ if ask(Q.real(arg), assumptions):
271
+ return arg
272
+ if ask(Q.imaginary(arg), assumptions):
273
+ return S.Zero
274
+ return _refine_reim(expr, assumptions)
275
+
276
+
277
+ def refine_im(expr, assumptions):
278
+ """
279
+ Handler for imaginary part.
280
+
281
+ Explanation
282
+ ===========
283
+
284
+ >>> from sympy.assumptions.refine import refine_im
285
+ >>> from sympy import Q, im
286
+ >>> from sympy.abc import x
287
+ >>> refine_im(im(x), Q.real(x))
288
+ 0
289
+ >>> refine_im(im(x), Q.imaginary(x))
290
+ -I*x
291
+ """
292
+ arg = expr.args[0]
293
+ if ask(Q.real(arg), assumptions):
294
+ return S.Zero
295
+ if ask(Q.imaginary(arg), assumptions):
296
+ return - S.ImaginaryUnit * arg
297
+ return _refine_reim(expr, assumptions)
298
+
299
+ def refine_arg(expr, assumptions):
300
+ """
301
+ Handler for complex argument
302
+
303
+ Explanation
304
+ ===========
305
+
306
+ >>> from sympy.assumptions.refine import refine_arg
307
+ >>> from sympy import Q, arg
308
+ >>> from sympy.abc import x
309
+ >>> refine_arg(arg(x), Q.positive(x))
310
+ 0
311
+ >>> refine_arg(arg(x), Q.negative(x))
312
+ pi
313
+ """
314
+ rg = expr.args[0]
315
+ if ask(Q.positive(rg), assumptions):
316
+ return S.Zero
317
+ if ask(Q.negative(rg), assumptions):
318
+ return S.Pi
319
+ return None
320
+
321
+
322
+ def _refine_reim(expr, assumptions):
323
+ # Helper function for refine_re & refine_im
324
+ expanded = expr.expand(complex = True)
325
+ if expanded != expr:
326
+ refined = refine(expanded, assumptions)
327
+ if refined != expanded:
328
+ return refined
329
+ # Best to leave the expression as is
330
+ return None
331
+
332
+
333
+ def refine_sign(expr, assumptions):
334
+ """
335
+ Handler for sign.
336
+
337
+ Examples
338
+ ========
339
+
340
+ >>> from sympy.assumptions.refine import refine_sign
341
+ >>> from sympy import Symbol, Q, sign, im
342
+ >>> x = Symbol('x', real = True)
343
+ >>> expr = sign(x)
344
+ >>> refine_sign(expr, Q.positive(x) & Q.nonzero(x))
345
+ 1
346
+ >>> refine_sign(expr, Q.negative(x) & Q.nonzero(x))
347
+ -1
348
+ >>> refine_sign(expr, Q.zero(x))
349
+ 0
350
+ >>> y = Symbol('y', imaginary = True)
351
+ >>> expr = sign(y)
352
+ >>> refine_sign(expr, Q.positive(im(y)))
353
+ I
354
+ >>> refine_sign(expr, Q.negative(im(y)))
355
+ -I
356
+ """
357
+ arg = expr.args[0]
358
+ if ask(Q.zero(arg), assumptions):
359
+ return S.Zero
360
+ if ask(Q.real(arg)):
361
+ if ask(Q.positive(arg), assumptions):
362
+ return S.One
363
+ if ask(Q.negative(arg), assumptions):
364
+ return S.NegativeOne
365
+ if ask(Q.imaginary(arg)):
366
+ arg_re, arg_im = arg.as_real_imag()
367
+ if ask(Q.positive(arg_im), assumptions):
368
+ return S.ImaginaryUnit
369
+ if ask(Q.negative(arg_im), assumptions):
370
+ return -S.ImaginaryUnit
371
+ return expr
372
+
373
+
374
+ def refine_matrixelement(expr, assumptions):
375
+ """
376
+ Handler for symmetric part.
377
+
378
+ Examples
379
+ ========
380
+
381
+ >>> from sympy.assumptions.refine import refine_matrixelement
382
+ >>> from sympy import MatrixSymbol, Q
383
+ >>> X = MatrixSymbol('X', 3, 3)
384
+ >>> refine_matrixelement(X[0, 1], Q.symmetric(X))
385
+ X[0, 1]
386
+ >>> refine_matrixelement(X[1, 0], Q.symmetric(X))
387
+ X[0, 1]
388
+ """
389
+ from sympy.matrices.expressions.matexpr import MatrixElement
390
+ matrix, i, j = expr.args
391
+ if ask(Q.symmetric(matrix), assumptions):
392
+ if (i - j).could_extract_minus_sign():
393
+ return expr
394
+ return MatrixElement(matrix, j, i)
395
+
396
+ handlers_dict: dict[str, Callable[[Expr, Boolean], Expr]] = {
397
+ 'Abs': refine_abs,
398
+ 'Pow': refine_Pow,
399
+ 'atan2': refine_atan2,
400
+ 're': refine_re,
401
+ 'im': refine_im,
402
+ 'arg': refine_arg,
403
+ 'sign': refine_sign,
404
+ 'MatrixElement': refine_matrixelement
405
+ }
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/__init__.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ A module to implement finitary relations [1] as predicate.
3
+
4
+ References
5
+ ==========
6
+
7
+ .. [1] https://en.wikipedia.org/wiki/Finitary_relation
8
+
9
+ """
10
+
11
+ __all__ = ['BinaryRelation', 'AppliedBinaryRelation']
12
+
13
+ from .binrel import BinaryRelation, AppliedBinaryRelation
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (462 Bytes). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/binrel.cpython-310.pyc ADDED
Binary file (6.52 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/equality.cpython-310.pyc ADDED
Binary file (7.7 kB). View file
 
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/binrel.py ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ General binary relations.
3
+ """
4
+ from typing import Optional
5
+
6
+ from sympy.core.singleton import S
7
+ from sympy.assumptions import AppliedPredicate, ask, Predicate, Q # type: ignore
8
+ from sympy.core.kind import BooleanKind
9
+ from sympy.core.relational import Eq, Ne, Gt, Lt, Ge, Le
10
+ from sympy.logic.boolalg import conjuncts, Not
11
+
12
+ __all__ = ["BinaryRelation", "AppliedBinaryRelation"]
13
+
14
+
15
+ class BinaryRelation(Predicate):
16
+ """
17
+ Base class for all binary relational predicates.
18
+
19
+ Explanation
20
+ ===========
21
+
22
+ Binary relation takes two arguments and returns ``AppliedBinaryRelation``
23
+ instance. To evaluate it to boolean value, use :obj:`~.ask()` or
24
+ :obj:`~.refine()` function.
25
+
26
+ You can add support for new types by registering the handler to dispatcher.
27
+ See :obj:`~.Predicate()` for more information about predicate dispatching.
28
+
29
+ Examples
30
+ ========
31
+
32
+ Applying and evaluating to boolean value:
33
+
34
+ >>> from sympy import Q, ask, sin, cos
35
+ >>> from sympy.abc import x
36
+ >>> Q.eq(sin(x)**2+cos(x)**2, 1)
37
+ Q.eq(sin(x)**2 + cos(x)**2, 1)
38
+ >>> ask(_)
39
+ True
40
+
41
+ You can define a new binary relation by subclassing and dispatching.
42
+ Here, we define a relation $R$ such that $x R y$ returns true if
43
+ $x = y + 1$.
44
+
45
+ >>> from sympy import ask, Number, Q
46
+ >>> from sympy.assumptions import BinaryRelation
47
+ >>> class MyRel(BinaryRelation):
48
+ ... name = "R"
49
+ ... is_reflexive = False
50
+ >>> Q.R = MyRel()
51
+ >>> @Q.R.register(Number, Number)
52
+ ... def _(n1, n2, assumptions):
53
+ ... return ask(Q.zero(n1 - n2 - 1), assumptions)
54
+ >>> Q.R(2, 1)
55
+ Q.R(2, 1)
56
+
57
+ Now, we can use ``ask()`` to evaluate it to boolean value.
58
+
59
+ >>> ask(Q.R(2, 1))
60
+ True
61
+ >>> ask(Q.R(1, 2))
62
+ False
63
+
64
+ ``Q.R`` returns ``False`` with minimum cost if two arguments have same
65
+ structure because it is antireflexive relation [1] by
66
+ ``is_reflexive = False``.
67
+
68
+ >>> ask(Q.R(x, x))
69
+ False
70
+
71
+ References
72
+ ==========
73
+
74
+ .. [1] https://en.wikipedia.org/wiki/Reflexive_relation
75
+ """
76
+
77
+ is_reflexive: Optional[bool] = None
78
+ is_symmetric: Optional[bool] = None
79
+
80
+ def __call__(self, *args):
81
+ if not len(args) == 2:
82
+ raise ValueError("Binary relation takes two arguments, but got %s." % len(args))
83
+ return AppliedBinaryRelation(self, *args)
84
+
85
+ @property
86
+ def reversed(self):
87
+ if self.is_symmetric:
88
+ return self
89
+ return None
90
+
91
+ @property
92
+ def negated(self):
93
+ return None
94
+
95
+ def _compare_reflexive(self, lhs, rhs):
96
+ # quick exit for structurally same arguments
97
+ # do not check != here because it cannot catch the
98
+ # equivalent arguments with different structures.
99
+
100
+ # reflexivity does not hold to NaN
101
+ if lhs is S.NaN or rhs is S.NaN:
102
+ return None
103
+
104
+ reflexive = self.is_reflexive
105
+ if reflexive is None:
106
+ pass
107
+ elif reflexive and (lhs == rhs):
108
+ return True
109
+ elif not reflexive and (lhs == rhs):
110
+ return False
111
+ return None
112
+
113
+ def eval(self, args, assumptions=True):
114
+ # quick exit for structurally same arguments
115
+ ret = self._compare_reflexive(*args)
116
+ if ret is not None:
117
+ return ret
118
+
119
+ # don't perform simplify on args here. (done by AppliedBinaryRelation._eval_ask)
120
+ # evaluate by multipledispatch
121
+ lhs, rhs = args
122
+ ret = self.handler(lhs, rhs, assumptions=assumptions)
123
+ if ret is not None:
124
+ return ret
125
+
126
+ # check reversed order if the relation is reflexive
127
+ if self.is_reflexive:
128
+ types = (type(lhs), type(rhs))
129
+ if self.handler.dispatch(*types) is not self.handler.dispatch(*reversed(types)):
130
+ ret = self.handler(rhs, lhs, assumptions=assumptions)
131
+
132
+ return ret
133
+
134
+
135
+ class AppliedBinaryRelation(AppliedPredicate):
136
+ """
137
+ The class of expressions resulting from applying ``BinaryRelation``
138
+ to the arguments.
139
+
140
+ """
141
+
142
+ @property
143
+ def lhs(self):
144
+ """The left-hand side of the relation."""
145
+ return self.arguments[0]
146
+
147
+ @property
148
+ def rhs(self):
149
+ """The right-hand side of the relation."""
150
+ return self.arguments[1]
151
+
152
+ @property
153
+ def reversed(self):
154
+ """
155
+ Try to return the relationship with sides reversed.
156
+ """
157
+ revfunc = self.function.reversed
158
+ if revfunc is None:
159
+ return self
160
+ return revfunc(self.rhs, self.lhs)
161
+
162
+ @property
163
+ def reversedsign(self):
164
+ """
165
+ Try to return the relationship with signs reversed.
166
+ """
167
+ revfunc = self.function.reversed
168
+ if revfunc is None:
169
+ return self
170
+ if not any(side.kind is BooleanKind for side in self.arguments):
171
+ return revfunc(-self.lhs, -self.rhs)
172
+ return self
173
+
174
+ @property
175
+ def negated(self):
176
+ neg_rel = self.function.negated
177
+ if neg_rel is None:
178
+ return Not(self, evaluate=False)
179
+ return neg_rel(*self.arguments)
180
+
181
+ def _eval_ask(self, assumptions):
182
+ conj_assumps = set()
183
+ binrelpreds = {Eq: Q.eq, Ne: Q.ne, Gt: Q.gt, Lt: Q.lt, Ge: Q.ge, Le: Q.le}
184
+ for a in conjuncts(assumptions):
185
+ if a.func in binrelpreds:
186
+ conj_assumps.add(binrelpreds[type(a)](*a.args))
187
+ else:
188
+ conj_assumps.add(a)
189
+
190
+ # After CNF in assumptions module is modified to take polyadic
191
+ # predicate, this will be removed
192
+ if any(rel in conj_assumps for rel in (self, self.reversed)):
193
+ return True
194
+ neg_rels = (self.negated, self.reversed.negated, Not(self, evaluate=False),
195
+ Not(self.reversed, evaluate=False))
196
+ if any(rel in conj_assumps for rel in neg_rels):
197
+ return False
198
+
199
+ # evaluation using multipledispatching
200
+ ret = self.function.eval(self.arguments, assumptions)
201
+ if ret is not None:
202
+ return ret
203
+
204
+ # simplify the args and try again
205
+ args = tuple(a.simplify() for a in self.arguments)
206
+ return self.function.eval(args, assumptions)
207
+
208
+ def __bool__(self):
209
+ ret = ask(self)
210
+ if ret is None:
211
+ raise TypeError("Cannot determine truth value of %s" % self)
212
+ return ret
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/relation/equality.py ADDED
@@ -0,0 +1,302 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Module for mathematical equality [1] and inequalities [2].
3
+
4
+ The purpose of this module is to provide the instances which represent the
5
+ binary predicates in order to combine the relationals into logical inference
6
+ system. Objects such as ``Q.eq``, ``Q.lt`` should remain internal to
7
+ assumptions module, and user must use the classes such as :obj:`~.Eq()`,
8
+ :obj:`~.Lt()` instead to construct the relational expressions.
9
+
10
+ References
11
+ ==========
12
+
13
+ .. [1] https://en.wikipedia.org/wiki/Equality_(mathematics)
14
+ .. [2] https://en.wikipedia.org/wiki/Inequality_(mathematics)
15
+ """
16
+ from sympy.assumptions import Q
17
+ from sympy.core.relational import is_eq, is_neq, is_gt, is_ge, is_lt, is_le
18
+
19
+ from .binrel import BinaryRelation
20
+
21
+ __all__ = ['EqualityPredicate', 'UnequalityPredicate', 'StrictGreaterThanPredicate',
22
+ 'GreaterThanPredicate', 'StrictLessThanPredicate', 'LessThanPredicate']
23
+
24
+
25
+ class EqualityPredicate(BinaryRelation):
26
+ """
27
+ Binary predicate for $=$.
28
+
29
+ The purpose of this class is to provide the instance which represent
30
+ the equality predicate in order to allow the logical inference.
31
+ This class must remain internal to assumptions module and user must
32
+ use :obj:`~.Eq()` instead to construct the equality expression.
33
+
34
+ Evaluating this predicate to ``True`` or ``False`` is done by
35
+ :func:`~.core.relational.is_eq()`
36
+
37
+ Examples
38
+ ========
39
+
40
+ >>> from sympy import ask, Q
41
+ >>> Q.eq(0, 0)
42
+ Q.eq(0, 0)
43
+ >>> ask(_)
44
+ True
45
+
46
+ See Also
47
+ ========
48
+
49
+ sympy.core.relational.Eq
50
+
51
+ """
52
+ is_reflexive = True
53
+ is_symmetric = True
54
+
55
+ name = 'eq'
56
+ handler = None # Do not allow dispatching by this predicate
57
+
58
+ @property
59
+ def negated(self):
60
+ return Q.ne
61
+
62
+ def eval(self, args, assumptions=True):
63
+ if assumptions == True:
64
+ # default assumptions for is_eq is None
65
+ assumptions = None
66
+ return is_eq(*args, assumptions)
67
+
68
+
69
+ class UnequalityPredicate(BinaryRelation):
70
+ r"""
71
+ Binary predicate for $\neq$.
72
+
73
+ The purpose of this class is to provide the instance which represent
74
+ the inequation predicate in order to allow the logical inference.
75
+ This class must remain internal to assumptions module and user must
76
+ use :obj:`~.Ne()` instead to construct the inequation expression.
77
+
78
+ Evaluating this predicate to ``True`` or ``False`` is done by
79
+ :func:`~.core.relational.is_neq()`
80
+
81
+ Examples
82
+ ========
83
+
84
+ >>> from sympy import ask, Q
85
+ >>> Q.ne(0, 0)
86
+ Q.ne(0, 0)
87
+ >>> ask(_)
88
+ False
89
+
90
+ See Also
91
+ ========
92
+
93
+ sympy.core.relational.Ne
94
+
95
+ """
96
+ is_reflexive = False
97
+ is_symmetric = True
98
+
99
+ name = 'ne'
100
+ handler = None
101
+
102
+ @property
103
+ def negated(self):
104
+ return Q.eq
105
+
106
+ def eval(self, args, assumptions=True):
107
+ if assumptions == True:
108
+ # default assumptions for is_neq is None
109
+ assumptions = None
110
+ return is_neq(*args, assumptions)
111
+
112
+
113
+ class StrictGreaterThanPredicate(BinaryRelation):
114
+ """
115
+ Binary predicate for $>$.
116
+
117
+ The purpose of this class is to provide the instance which represent
118
+ the ">" predicate in order to allow the logical inference.
119
+ This class must remain internal to assumptions module and user must
120
+ use :obj:`~.Gt()` instead to construct the equality expression.
121
+
122
+ Evaluating this predicate to ``True`` or ``False`` is done by
123
+ :func:`~.core.relational.is_gt()`
124
+
125
+ Examples
126
+ ========
127
+
128
+ >>> from sympy import ask, Q
129
+ >>> Q.gt(0, 0)
130
+ Q.gt(0, 0)
131
+ >>> ask(_)
132
+ False
133
+
134
+ See Also
135
+ ========
136
+
137
+ sympy.core.relational.Gt
138
+
139
+ """
140
+ is_reflexive = False
141
+ is_symmetric = False
142
+
143
+ name = 'gt'
144
+ handler = None
145
+
146
+ @property
147
+ def reversed(self):
148
+ return Q.lt
149
+
150
+ @property
151
+ def negated(self):
152
+ return Q.le
153
+
154
+ def eval(self, args, assumptions=True):
155
+ if assumptions == True:
156
+ # default assumptions for is_gt is None
157
+ assumptions = None
158
+ return is_gt(*args, assumptions)
159
+
160
+
161
+ class GreaterThanPredicate(BinaryRelation):
162
+ """
163
+ Binary predicate for $>=$.
164
+
165
+ The purpose of this class is to provide the instance which represent
166
+ the ">=" predicate in order to allow the logical inference.
167
+ This class must remain internal to assumptions module and user must
168
+ use :obj:`~.Ge()` instead to construct the equality expression.
169
+
170
+ Evaluating this predicate to ``True`` or ``False`` is done by
171
+ :func:`~.core.relational.is_ge()`
172
+
173
+ Examples
174
+ ========
175
+
176
+ >>> from sympy import ask, Q
177
+ >>> Q.ge(0, 0)
178
+ Q.ge(0, 0)
179
+ >>> ask(_)
180
+ True
181
+
182
+ See Also
183
+ ========
184
+
185
+ sympy.core.relational.Ge
186
+
187
+ """
188
+ is_reflexive = True
189
+ is_symmetric = False
190
+
191
+ name = 'ge'
192
+ handler = None
193
+
194
+ @property
195
+ def reversed(self):
196
+ return Q.le
197
+
198
+ @property
199
+ def negated(self):
200
+ return Q.lt
201
+
202
+ def eval(self, args, assumptions=True):
203
+ if assumptions == True:
204
+ # default assumptions for is_ge is None
205
+ assumptions = None
206
+ return is_ge(*args, assumptions)
207
+
208
+
209
+ class StrictLessThanPredicate(BinaryRelation):
210
+ """
211
+ Binary predicate for $<$.
212
+
213
+ The purpose of this class is to provide the instance which represent
214
+ the "<" predicate in order to allow the logical inference.
215
+ This class must remain internal to assumptions module and user must
216
+ use :obj:`~.Lt()` instead to construct the equality expression.
217
+
218
+ Evaluating this predicate to ``True`` or ``False`` is done by
219
+ :func:`~.core.relational.is_lt()`
220
+
221
+ Examples
222
+ ========
223
+
224
+ >>> from sympy import ask, Q
225
+ >>> Q.lt(0, 0)
226
+ Q.lt(0, 0)
227
+ >>> ask(_)
228
+ False
229
+
230
+ See Also
231
+ ========
232
+
233
+ sympy.core.relational.Lt
234
+
235
+ """
236
+ is_reflexive = False
237
+ is_symmetric = False
238
+
239
+ name = 'lt'
240
+ handler = None
241
+
242
+ @property
243
+ def reversed(self):
244
+ return Q.gt
245
+
246
+ @property
247
+ def negated(self):
248
+ return Q.ge
249
+
250
+ def eval(self, args, assumptions=True):
251
+ if assumptions == True:
252
+ # default assumptions for is_lt is None
253
+ assumptions = None
254
+ return is_lt(*args, assumptions)
255
+
256
+
257
+ class LessThanPredicate(BinaryRelation):
258
+ """
259
+ Binary predicate for $<=$.
260
+
261
+ The purpose of this class is to provide the instance which represent
262
+ the "<=" predicate in order to allow the logical inference.
263
+ This class must remain internal to assumptions module and user must
264
+ use :obj:`~.Le()` instead to construct the equality expression.
265
+
266
+ Evaluating this predicate to ``True`` or ``False`` is done by
267
+ :func:`~.core.relational.is_le()`
268
+
269
+ Examples
270
+ ========
271
+
272
+ >>> from sympy import ask, Q
273
+ >>> Q.le(0, 0)
274
+ Q.le(0, 0)
275
+ >>> ask(_)
276
+ True
277
+
278
+ See Also
279
+ ========
280
+
281
+ sympy.core.relational.Le
282
+
283
+ """
284
+ is_reflexive = True
285
+ is_symmetric = False
286
+
287
+ name = 'le'
288
+ handler = None
289
+
290
+ @property
291
+ def reversed(self):
292
+ return Q.ge
293
+
294
+ @property
295
+ def negated(self):
296
+ return Q.gt
297
+
298
+ def eval(self, args, assumptions=True):
299
+ if assumptions == True:
300
+ # default assumptions for is_le is None
301
+ assumptions = None
302
+ return is_le(*args, assumptions)
llmeval-env/lib/python3.10/site-packages/sympy/assumptions/satask.py ADDED
@@ -0,0 +1,361 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Module to evaluate the proposition with assumptions using SAT algorithm.
3
+ """
4
+
5
+ from sympy.core.singleton import S
6
+ from sympy.core.symbol import Symbol
7
+ from sympy.assumptions.ask_generated import get_all_known_facts
8
+ from sympy.assumptions.assume import global_assumptions, AppliedPredicate
9
+ from sympy.assumptions.sathandlers import class_fact_registry
10
+ from sympy.core import oo
11
+ from sympy.logic.inference import satisfiable
12
+ from sympy.assumptions.cnf import CNF, EncodedCNF
13
+
14
+
15
+ def satask(proposition, assumptions=True, context=global_assumptions,
16
+ use_known_facts=True, iterations=oo):
17
+ """
18
+ Function to evaluate the proposition with assumptions using SAT algorithm.
19
+
20
+ This function extracts every fact relevant to the expressions composing
21
+ proposition and assumptions. For example, if a predicate containing
22
+ ``Abs(x)`` is proposed, then ``Q.zero(Abs(x)) | Q.positive(Abs(x))``
23
+ will be found and passed to SAT solver because ``Q.nonnegative`` is
24
+ registered as a fact for ``Abs``.
25
+
26
+ Proposition is evaluated to ``True`` or ``False`` if the truth value can be
27
+ determined. If not, ``None`` is returned.
28
+
29
+ Parameters
30
+ ==========
31
+
32
+ proposition : Any boolean expression.
33
+ Proposition which will be evaluated to boolean value.
34
+
35
+ assumptions : Any boolean expression, optional.
36
+ Local assumptions to evaluate the *proposition*.
37
+
38
+ context : AssumptionsContext, optional.
39
+ Default assumptions to evaluate the *proposition*. By default,
40
+ this is ``sympy.assumptions.global_assumptions`` variable.
41
+
42
+ use_known_facts : bool, optional.
43
+ If ``True``, facts from ``sympy.assumptions.ask_generated``
44
+ module are passed to SAT solver as well.
45
+
46
+ iterations : int, optional.
47
+ Number of times that relevant facts are recursively extracted.
48
+ Default is infinite times until no new fact is found.
49
+
50
+ Returns
51
+ =======
52
+
53
+ ``True``, ``False``, or ``None``
54
+
55
+ Examples
56
+ ========
57
+
58
+ >>> from sympy import Abs, Q
59
+ >>> from sympy.assumptions.satask import satask
60
+ >>> from sympy.abc import x
61
+ >>> satask(Q.zero(Abs(x)), Q.zero(x))
62
+ True
63
+
64
+ """
65
+ props = CNF.from_prop(proposition)
66
+ _props = CNF.from_prop(~proposition)
67
+
68
+ assumptions = CNF.from_prop(assumptions)
69
+
70
+ context_cnf = CNF()
71
+ if context:
72
+ context_cnf = context_cnf.extend(context)
73
+
74
+ sat = get_all_relevant_facts(props, assumptions, context_cnf,
75
+ use_known_facts=use_known_facts, iterations=iterations)
76
+ sat.add_from_cnf(assumptions)
77
+ if context:
78
+ sat.add_from_cnf(context_cnf)
79
+
80
+ return check_satisfiability(props, _props, sat)
81
+
82
+
83
+ def check_satisfiability(prop, _prop, factbase):
84
+ sat_true = factbase.copy()
85
+ sat_false = factbase.copy()
86
+ sat_true.add_from_cnf(prop)
87
+ sat_false.add_from_cnf(_prop)
88
+ can_be_true = satisfiable(sat_true)
89
+ can_be_false = satisfiable(sat_false)
90
+
91
+ if can_be_true and can_be_false:
92
+ return None
93
+
94
+ if can_be_true and not can_be_false:
95
+ return True
96
+
97
+ if not can_be_true and can_be_false:
98
+ return False
99
+
100
+ if not can_be_true and not can_be_false:
101
+ # TODO: Run additional checks to see which combination of the
102
+ # assumptions, global_assumptions, and relevant_facts are
103
+ # inconsistent.
104
+ raise ValueError("Inconsistent assumptions")
105
+
106
+
107
+ def extract_predargs(proposition, assumptions=None, context=None):
108
+ """
109
+ Extract every expression in the argument of predicates from *proposition*,
110
+ *assumptions* and *context*.
111
+
112
+ Parameters
113
+ ==========
114
+
115
+ proposition : sympy.assumptions.cnf.CNF
116
+
117
+ assumptions : sympy.assumptions.cnf.CNF, optional.
118
+
119
+ context : sympy.assumptions.cnf.CNF, optional.
120
+ CNF generated from assumptions context.
121
+
122
+ Examples
123
+ ========
124
+
125
+ >>> from sympy import Q, Abs
126
+ >>> from sympy.assumptions.cnf import CNF
127
+ >>> from sympy.assumptions.satask import extract_predargs
128
+ >>> from sympy.abc import x, y
129
+ >>> props = CNF.from_prop(Q.zero(Abs(x*y)))
130
+ >>> assump = CNF.from_prop(Q.zero(x) & Q.zero(y))
131
+ >>> extract_predargs(props, assump)
132
+ {x, y, Abs(x*y)}
133
+
134
+ """
135
+ req_keys = find_symbols(proposition)
136
+ keys = proposition.all_predicates()
137
+ # XXX: We need this since True/False are not Basic
138
+ lkeys = set()
139
+ if assumptions:
140
+ lkeys |= assumptions.all_predicates()
141
+ if context:
142
+ lkeys |= context.all_predicates()
143
+
144
+ lkeys = lkeys - {S.true, S.false}
145
+ tmp_keys = None
146
+ while tmp_keys != set():
147
+ tmp = set()
148
+ for l in lkeys:
149
+ syms = find_symbols(l)
150
+ if (syms & req_keys) != set():
151
+ tmp |= syms
152
+ tmp_keys = tmp - req_keys
153
+ req_keys |= tmp_keys
154
+ keys |= {l for l in lkeys if find_symbols(l) & req_keys != set()}
155
+
156
+ exprs = set()
157
+ for key in keys:
158
+ if isinstance(key, AppliedPredicate):
159
+ exprs |= set(key.arguments)
160
+ else:
161
+ exprs.add(key)
162
+ return exprs
163
+
164
+ def find_symbols(pred):
165
+ """
166
+ Find every :obj:`~.Symbol` in *pred*.
167
+
168
+ Parameters
169
+ ==========
170
+
171
+ pred : sympy.assumptions.cnf.CNF, or any Expr.
172
+
173
+ """
174
+ if isinstance(pred, CNF):
175
+ symbols = set()
176
+ for a in pred.all_predicates():
177
+ symbols |= find_symbols(a)
178
+ return symbols
179
+ return pred.atoms(Symbol)
180
+
181
+
182
+ def get_relevant_clsfacts(exprs, relevant_facts=None):
183
+ """
184
+ Extract relevant facts from the items in *exprs*. Facts are defined in
185
+ ``assumptions.sathandlers`` module.
186
+
187
+ This function is recursively called by ``get_all_relevant_facts()``.
188
+
189
+ Parameters
190
+ ==========
191
+
192
+ exprs : set
193
+ Expressions whose relevant facts are searched.
194
+
195
+ relevant_facts : sympy.assumptions.cnf.CNF, optional.
196
+ Pre-discovered relevant facts.
197
+
198
+ Returns
199
+ =======
200
+
201
+ exprs : set
202
+ Candidates for next relevant fact searching.
203
+
204
+ relevant_facts : sympy.assumptions.cnf.CNF
205
+ Updated relevant facts.
206
+
207
+ Examples
208
+ ========
209
+
210
+ Here, we will see how facts relevant to ``Abs(x*y)`` are recursively
211
+ extracted. On the first run, set containing the expression is passed
212
+ without pre-discovered relevant facts. The result is a set containing
213
+ candidates for next run, and ``CNF()`` instance containing facts
214
+ which are relevant to ``Abs`` and its argument.
215
+
216
+ >>> from sympy import Abs
217
+ >>> from sympy.assumptions.satask import get_relevant_clsfacts
218
+ >>> from sympy.abc import x, y
219
+ >>> exprs = {Abs(x*y)}
220
+ >>> exprs, facts = get_relevant_clsfacts(exprs)
221
+ >>> exprs
222
+ {x*y}
223
+ >>> facts.clauses #doctest: +SKIP
224
+ {frozenset({Literal(Q.odd(Abs(x*y)), False), Literal(Q.odd(x*y), True)}),
225
+ frozenset({Literal(Q.zero(Abs(x*y)), False), Literal(Q.zero(x*y), True)}),
226
+ frozenset({Literal(Q.even(Abs(x*y)), False), Literal(Q.even(x*y), True)}),
227
+ frozenset({Literal(Q.zero(Abs(x*y)), True), Literal(Q.zero(x*y), False)}),
228
+ frozenset({Literal(Q.even(Abs(x*y)), False),
229
+ Literal(Q.odd(Abs(x*y)), False),
230
+ Literal(Q.odd(x*y), True)}),
231
+ frozenset({Literal(Q.even(Abs(x*y)), False),
232
+ Literal(Q.even(x*y), True),
233
+ Literal(Q.odd(Abs(x*y)), False)}),
234
+ frozenset({Literal(Q.positive(Abs(x*y)), False),
235
+ Literal(Q.zero(Abs(x*y)), False)})}
236
+
237
+ We pass the first run's results to the second run, and get the expressions
238
+ for next run and updated facts.
239
+
240
+ >>> exprs, facts = get_relevant_clsfacts(exprs, relevant_facts=facts)
241
+ >>> exprs
242
+ {x, y}
243
+
244
+ On final run, no more candidate is returned thus we know that all
245
+ relevant facts are successfully retrieved.
246
+
247
+ >>> exprs, facts = get_relevant_clsfacts(exprs, relevant_facts=facts)
248
+ >>> exprs
249
+ set()
250
+
251
+ """
252
+ if not relevant_facts:
253
+ relevant_facts = CNF()
254
+
255
+ newexprs = set()
256
+ for expr in exprs:
257
+ for fact in class_fact_registry(expr):
258
+ newfact = CNF.to_CNF(fact)
259
+ relevant_facts = relevant_facts._and(newfact)
260
+ for key in newfact.all_predicates():
261
+ if isinstance(key, AppliedPredicate):
262
+ newexprs |= set(key.arguments)
263
+
264
+ return newexprs - exprs, relevant_facts
265
+
266
+
267
+ def get_all_relevant_facts(proposition, assumptions, context,
268
+ use_known_facts=True, iterations=oo):
269
+ """
270
+ Extract all relevant facts from *proposition* and *assumptions*.
271
+
272
+ This function extracts the facts by recursively calling
273
+ ``get_relevant_clsfacts()``. Extracted facts are converted to
274
+ ``EncodedCNF`` and returned.
275
+
276
+ Parameters
277
+ ==========
278
+
279
+ proposition : sympy.assumptions.cnf.CNF
280
+ CNF generated from proposition expression.
281
+
282
+ assumptions : sympy.assumptions.cnf.CNF
283
+ CNF generated from assumption expression.
284
+
285
+ context : sympy.assumptions.cnf.CNF
286
+ CNF generated from assumptions context.
287
+
288
+ use_known_facts : bool, optional.
289
+ If ``True``, facts from ``sympy.assumptions.ask_generated``
290
+ module are encoded as well.
291
+
292
+ iterations : int, optional.
293
+ Number of times that relevant facts are recursively extracted.
294
+ Default is infinite times until no new fact is found.
295
+
296
+ Returns
297
+ =======
298
+
299
+ sympy.assumptions.cnf.EncodedCNF
300
+
301
+ Examples
302
+ ========
303
+
304
+ >>> from sympy import Q
305
+ >>> from sympy.assumptions.cnf import CNF
306
+ >>> from sympy.assumptions.satask import get_all_relevant_facts
307
+ >>> from sympy.abc import x, y
308
+ >>> props = CNF.from_prop(Q.nonzero(x*y))
309
+ >>> assump = CNF.from_prop(Q.nonzero(x))
310
+ >>> context = CNF.from_prop(Q.nonzero(y))
311
+ >>> get_all_relevant_facts(props, assump, context) #doctest: +SKIP
312
+ <sympy.assumptions.cnf.EncodedCNF at 0x7f09faa6ccd0>
313
+
314
+ """
315
+ # The relevant facts might introduce new keys, e.g., Q.zero(x*y) will
316
+ # introduce the keys Q.zero(x) and Q.zero(y), so we need to run it until
317
+ # we stop getting new things. Hopefully this strategy won't lead to an
318
+ # infinite loop in the future.
319
+ i = 0
320
+ relevant_facts = CNF()
321
+ all_exprs = set()
322
+ while True:
323
+ if i == 0:
324
+ exprs = extract_predargs(proposition, assumptions, context)
325
+ all_exprs |= exprs
326
+ exprs, relevant_facts = get_relevant_clsfacts(exprs, relevant_facts)
327
+ i += 1
328
+ if i >= iterations:
329
+ break
330
+ if not exprs:
331
+ break
332
+
333
+ if use_known_facts:
334
+ known_facts_CNF = CNF()
335
+ known_facts_CNF.add_clauses(get_all_known_facts())
336
+ kf_encoded = EncodedCNF()
337
+ kf_encoded.from_cnf(known_facts_CNF)
338
+
339
+ def translate_literal(lit, delta):
340
+ if lit > 0:
341
+ return lit + delta
342
+ else:
343
+ return lit - delta
344
+
345
+ def translate_data(data, delta):
346
+ return [{translate_literal(i, delta) for i in clause} for clause in data]
347
+ data = []
348
+ symbols = []
349
+ n_lit = len(kf_encoded.symbols)
350
+ for i, expr in enumerate(all_exprs):
351
+ symbols += [pred(expr) for pred in kf_encoded.symbols]
352
+ data += translate_data(kf_encoded.data, i * n_lit)
353
+
354
+ encoding = dict(list(zip(symbols, range(1, len(symbols)+1))))
355
+ ctx = EncodedCNF(data, encoding)
356
+ else:
357
+ ctx = EncodedCNF()
358
+
359
+ ctx.add_from_cnf(relevant_facts)
360
+
361
+ return ctx