Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__init__.py +18 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask_generated.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/assume.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/cnf.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/facts.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/refine.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/satask.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/sathandlers.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/wrapper.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/ask.py +632 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/ask_generated.py +264 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/assume.py +485 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/cnf.py +453 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/facts.py +220 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/handlers/__init__.py +13 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/handlers/calculus.py +258 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/handlers/matrices.py +716 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/handlers/ntheory.py +267 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/handlers/order.py +436 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/handlers/sets.py +772 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__init__.py +5 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/calculus.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/common.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/matrices.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/ntheory.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/order.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/sets.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/calculus.py +82 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/common.py +81 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/matrices.py +511 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/ntheory.py +126 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/order.py +390 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/sets.py +387 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/refine.py +405 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/relation/__init__.py +13 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/binrel.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/equality.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/relation/binrel.py +212 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/relation/equality.py +302 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/satask.py +361 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/sathandlers.py +323 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/tests/__init__.py +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/tests/__pycache__/__init__.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/tests/__pycache__/test_assumptions_2.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/tests/__pycache__/test_context.cpython-310.pyc +0 -0
- env-llmeval/lib/python3.10/site-packages/sympy/assumptions/tests/__pycache__/test_matrices.cpython-310.pyc +0 -0
env-llmeval/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 |
+
]
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (687 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask.cpython-310.pyc
ADDED
Binary file (18.4 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask_generated.cpython-310.pyc
ADDED
Binary file (7.95 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/assume.cpython-310.pyc
ADDED
Binary file (14.5 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/cnf.cpython-310.pyc
ADDED
Binary file (16.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/facts.cpython-310.pyc
ADDED
Binary file (6.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/refine.cpython-310.pyc
ADDED
Binary file (9.93 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/satask.cpython-310.pyc
ADDED
Binary file (10.6 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/sathandlers.cpython-310.pyc
ADDED
Binary file (10.2 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/__pycache__/wrapper.cpython-310.pyc
ADDED
Binary file (5.41 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/ask.py
ADDED
@@ -0,0 +1,632 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""Module for querying SymPy objects about assumptions."""
|
2 |
+
|
3 |
+
from sympy.assumptions.assume import (global_assumptions, Predicate,
|
4 |
+
AppliedPredicate)
|
5 |
+
from sympy.assumptions.cnf import CNF, EncodedCNF, Literal
|
6 |
+
from sympy.core import sympify
|
7 |
+
from sympy.core.kind import BooleanKind
|
8 |
+
from sympy.core.relational import Eq, Ne, Gt, Lt, Ge, Le
|
9 |
+
from sympy.logic.inference import satisfiable
|
10 |
+
from sympy.utilities.decorator import memoize_property
|
11 |
+
from sympy.utilities.exceptions import (sympy_deprecation_warning,
|
12 |
+
SymPyDeprecationWarning,
|
13 |
+
ignore_warnings)
|
14 |
+
|
15 |
+
|
16 |
+
# Memoization is necessary for the properties of AssumptionKeys to
|
17 |
+
# ensure that only one object of Predicate objects are created.
|
18 |
+
# This is because assumption handlers are registered on those objects.
|
19 |
+
|
20 |
+
|
21 |
+
class AssumptionKeys:
|
22 |
+
"""
|
23 |
+
This class contains all the supported keys by ``ask``.
|
24 |
+
It should be accessed via the instance ``sympy.Q``.
|
25 |
+
|
26 |
+
"""
|
27 |
+
|
28 |
+
# DO NOT add methods or properties other than predicate keys.
|
29 |
+
# SAT solver checks the properties of Q and use them to compute the
|
30 |
+
# fact system. Non-predicate attributes will break this.
|
31 |
+
|
32 |
+
@memoize_property
|
33 |
+
def hermitian(self):
|
34 |
+
from .handlers.sets import HermitianPredicate
|
35 |
+
return HermitianPredicate()
|
36 |
+
|
37 |
+
@memoize_property
|
38 |
+
def antihermitian(self):
|
39 |
+
from .handlers.sets import AntihermitianPredicate
|
40 |
+
return AntihermitianPredicate()
|
41 |
+
|
42 |
+
@memoize_property
|
43 |
+
def real(self):
|
44 |
+
from .handlers.sets import RealPredicate
|
45 |
+
return RealPredicate()
|
46 |
+
|
47 |
+
@memoize_property
|
48 |
+
def extended_real(self):
|
49 |
+
from .handlers.sets import ExtendedRealPredicate
|
50 |
+
return ExtendedRealPredicate()
|
51 |
+
|
52 |
+
@memoize_property
|
53 |
+
def imaginary(self):
|
54 |
+
from .handlers.sets import ImaginaryPredicate
|
55 |
+
return ImaginaryPredicate()
|
56 |
+
|
57 |
+
@memoize_property
|
58 |
+
def complex(self):
|
59 |
+
from .handlers.sets import ComplexPredicate
|
60 |
+
return ComplexPredicate()
|
61 |
+
|
62 |
+
@memoize_property
|
63 |
+
def algebraic(self):
|
64 |
+
from .handlers.sets import AlgebraicPredicate
|
65 |
+
return AlgebraicPredicate()
|
66 |
+
|
67 |
+
@memoize_property
|
68 |
+
def transcendental(self):
|
69 |
+
from .predicates.sets import TranscendentalPredicate
|
70 |
+
return TranscendentalPredicate()
|
71 |
+
|
72 |
+
@memoize_property
|
73 |
+
def integer(self):
|
74 |
+
from .handlers.sets import IntegerPredicate
|
75 |
+
return IntegerPredicate()
|
76 |
+
|
77 |
+
@memoize_property
|
78 |
+
def rational(self):
|
79 |
+
from .handlers.sets import RationalPredicate
|
80 |
+
return RationalPredicate()
|
81 |
+
|
82 |
+
@memoize_property
|
83 |
+
def irrational(self):
|
84 |
+
from .handlers.sets import IrrationalPredicate
|
85 |
+
return IrrationalPredicate()
|
86 |
+
|
87 |
+
@memoize_property
|
88 |
+
def finite(self):
|
89 |
+
from .handlers.calculus import FinitePredicate
|
90 |
+
return FinitePredicate()
|
91 |
+
|
92 |
+
@memoize_property
|
93 |
+
def infinite(self):
|
94 |
+
from .handlers.calculus import InfinitePredicate
|
95 |
+
return InfinitePredicate()
|
96 |
+
|
97 |
+
@memoize_property
|
98 |
+
def positive_infinite(self):
|
99 |
+
from .handlers.calculus import PositiveInfinitePredicate
|
100 |
+
return PositiveInfinitePredicate()
|
101 |
+
|
102 |
+
@memoize_property
|
103 |
+
def negative_infinite(self):
|
104 |
+
from .handlers.calculus import NegativeInfinitePredicate
|
105 |
+
return NegativeInfinitePredicate()
|
106 |
+
|
107 |
+
@memoize_property
|
108 |
+
def positive(self):
|
109 |
+
from .handlers.order import PositivePredicate
|
110 |
+
return PositivePredicate()
|
111 |
+
|
112 |
+
@memoize_property
|
113 |
+
def negative(self):
|
114 |
+
from .handlers.order import NegativePredicate
|
115 |
+
return NegativePredicate()
|
116 |
+
|
117 |
+
@memoize_property
|
118 |
+
def zero(self):
|
119 |
+
from .handlers.order import ZeroPredicate
|
120 |
+
return ZeroPredicate()
|
121 |
+
|
122 |
+
@memoize_property
|
123 |
+
def extended_positive(self):
|
124 |
+
from .handlers.order import ExtendedPositivePredicate
|
125 |
+
return ExtendedPositivePredicate()
|
126 |
+
|
127 |
+
@memoize_property
|
128 |
+
def extended_negative(self):
|
129 |
+
from .handlers.order import ExtendedNegativePredicate
|
130 |
+
return ExtendedNegativePredicate()
|
131 |
+
|
132 |
+
@memoize_property
|
133 |
+
def nonzero(self):
|
134 |
+
from .handlers.order import NonZeroPredicate
|
135 |
+
return NonZeroPredicate()
|
136 |
+
|
137 |
+
@memoize_property
|
138 |
+
def nonpositive(self):
|
139 |
+
from .handlers.order import NonPositivePredicate
|
140 |
+
return NonPositivePredicate()
|
141 |
+
|
142 |
+
@memoize_property
|
143 |
+
def nonnegative(self):
|
144 |
+
from .handlers.order import NonNegativePredicate
|
145 |
+
return NonNegativePredicate()
|
146 |
+
|
147 |
+
@memoize_property
|
148 |
+
def extended_nonzero(self):
|
149 |
+
from .handlers.order import ExtendedNonZeroPredicate
|
150 |
+
return ExtendedNonZeroPredicate()
|
151 |
+
|
152 |
+
@memoize_property
|
153 |
+
def extended_nonpositive(self):
|
154 |
+
from .handlers.order import ExtendedNonPositivePredicate
|
155 |
+
return ExtendedNonPositivePredicate()
|
156 |
+
|
157 |
+
@memoize_property
|
158 |
+
def extended_nonnegative(self):
|
159 |
+
from .handlers.order import ExtendedNonNegativePredicate
|
160 |
+
return ExtendedNonNegativePredicate()
|
161 |
+
|
162 |
+
@memoize_property
|
163 |
+
def even(self):
|
164 |
+
from .handlers.ntheory import EvenPredicate
|
165 |
+
return EvenPredicate()
|
166 |
+
|
167 |
+
@memoize_property
|
168 |
+
def odd(self):
|
169 |
+
from .handlers.ntheory import OddPredicate
|
170 |
+
return OddPredicate()
|
171 |
+
|
172 |
+
@memoize_property
|
173 |
+
def prime(self):
|
174 |
+
from .handlers.ntheory import PrimePredicate
|
175 |
+
return PrimePredicate()
|
176 |
+
|
177 |
+
@memoize_property
|
178 |
+
def composite(self):
|
179 |
+
from .handlers.ntheory import CompositePredicate
|
180 |
+
return CompositePredicate()
|
181 |
+
|
182 |
+
@memoize_property
|
183 |
+
def commutative(self):
|
184 |
+
from .handlers.common import CommutativePredicate
|
185 |
+
return CommutativePredicate()
|
186 |
+
|
187 |
+
@memoize_property
|
188 |
+
def is_true(self):
|
189 |
+
from .handlers.common import IsTruePredicate
|
190 |
+
return IsTruePredicate()
|
191 |
+
|
192 |
+
@memoize_property
|
193 |
+
def symmetric(self):
|
194 |
+
from .handlers.matrices import SymmetricPredicate
|
195 |
+
return SymmetricPredicate()
|
196 |
+
|
197 |
+
@memoize_property
|
198 |
+
def invertible(self):
|
199 |
+
from .handlers.matrices import InvertiblePredicate
|
200 |
+
return InvertiblePredicate()
|
201 |
+
|
202 |
+
@memoize_property
|
203 |
+
def orthogonal(self):
|
204 |
+
from .handlers.matrices import OrthogonalPredicate
|
205 |
+
return OrthogonalPredicate()
|
206 |
+
|
207 |
+
@memoize_property
|
208 |
+
def unitary(self):
|
209 |
+
from .handlers.matrices import UnitaryPredicate
|
210 |
+
return UnitaryPredicate()
|
211 |
+
|
212 |
+
@memoize_property
|
213 |
+
def positive_definite(self):
|
214 |
+
from .handlers.matrices import PositiveDefinitePredicate
|
215 |
+
return PositiveDefinitePredicate()
|
216 |
+
|
217 |
+
@memoize_property
|
218 |
+
def upper_triangular(self):
|
219 |
+
from .handlers.matrices import UpperTriangularPredicate
|
220 |
+
return UpperTriangularPredicate()
|
221 |
+
|
222 |
+
@memoize_property
|
223 |
+
def lower_triangular(self):
|
224 |
+
from .handlers.matrices import LowerTriangularPredicate
|
225 |
+
return LowerTriangularPredicate()
|
226 |
+
|
227 |
+
@memoize_property
|
228 |
+
def diagonal(self):
|
229 |
+
from .handlers.matrices import DiagonalPredicate
|
230 |
+
return DiagonalPredicate()
|
231 |
+
|
232 |
+
@memoize_property
|
233 |
+
def fullrank(self):
|
234 |
+
from .handlers.matrices import FullRankPredicate
|
235 |
+
return FullRankPredicate()
|
236 |
+
|
237 |
+
@memoize_property
|
238 |
+
def square(self):
|
239 |
+
from .handlers.matrices import SquarePredicate
|
240 |
+
return SquarePredicate()
|
241 |
+
|
242 |
+
@memoize_property
|
243 |
+
def integer_elements(self):
|
244 |
+
from .handlers.matrices import IntegerElementsPredicate
|
245 |
+
return IntegerElementsPredicate()
|
246 |
+
|
247 |
+
@memoize_property
|
248 |
+
def real_elements(self):
|
249 |
+
from .handlers.matrices import RealElementsPredicate
|
250 |
+
return RealElementsPredicate()
|
251 |
+
|
252 |
+
@memoize_property
|
253 |
+
def complex_elements(self):
|
254 |
+
from .handlers.matrices import ComplexElementsPredicate
|
255 |
+
return ComplexElementsPredicate()
|
256 |
+
|
257 |
+
@memoize_property
|
258 |
+
def singular(self):
|
259 |
+
from .predicates.matrices import SingularPredicate
|
260 |
+
return SingularPredicate()
|
261 |
+
|
262 |
+
@memoize_property
|
263 |
+
def normal(self):
|
264 |
+
from .predicates.matrices import NormalPredicate
|
265 |
+
return NormalPredicate()
|
266 |
+
|
267 |
+
@memoize_property
|
268 |
+
def triangular(self):
|
269 |
+
from .predicates.matrices import TriangularPredicate
|
270 |
+
return TriangularPredicate()
|
271 |
+
|
272 |
+
@memoize_property
|
273 |
+
def unit_triangular(self):
|
274 |
+
from .predicates.matrices import UnitTriangularPredicate
|
275 |
+
return UnitTriangularPredicate()
|
276 |
+
|
277 |
+
@memoize_property
|
278 |
+
def eq(self):
|
279 |
+
from .relation.equality import EqualityPredicate
|
280 |
+
return EqualityPredicate()
|
281 |
+
|
282 |
+
@memoize_property
|
283 |
+
def ne(self):
|
284 |
+
from .relation.equality import UnequalityPredicate
|
285 |
+
return UnequalityPredicate()
|
286 |
+
|
287 |
+
@memoize_property
|
288 |
+
def gt(self):
|
289 |
+
from .relation.equality import StrictGreaterThanPredicate
|
290 |
+
return StrictGreaterThanPredicate()
|
291 |
+
|
292 |
+
@memoize_property
|
293 |
+
def ge(self):
|
294 |
+
from .relation.equality import GreaterThanPredicate
|
295 |
+
return GreaterThanPredicate()
|
296 |
+
|
297 |
+
@memoize_property
|
298 |
+
def lt(self):
|
299 |
+
from .relation.equality import StrictLessThanPredicate
|
300 |
+
return StrictLessThanPredicate()
|
301 |
+
|
302 |
+
@memoize_property
|
303 |
+
def le(self):
|
304 |
+
from .relation.equality import LessThanPredicate
|
305 |
+
return LessThanPredicate()
|
306 |
+
|
307 |
+
|
308 |
+
Q = AssumptionKeys()
|
309 |
+
|
310 |
+
def _extract_all_facts(assump, exprs):
|
311 |
+
"""
|
312 |
+
Extract all relevant assumptions from *assump* with respect to given *exprs*.
|
313 |
+
|
314 |
+
Parameters
|
315 |
+
==========
|
316 |
+
|
317 |
+
assump : sympy.assumptions.cnf.CNF
|
318 |
+
|
319 |
+
exprs : tuple of expressions
|
320 |
+
|
321 |
+
Returns
|
322 |
+
=======
|
323 |
+
|
324 |
+
sympy.assumptions.cnf.CNF
|
325 |
+
|
326 |
+
Examples
|
327 |
+
========
|
328 |
+
|
329 |
+
>>> from sympy import Q
|
330 |
+
>>> from sympy.assumptions.cnf import CNF
|
331 |
+
>>> from sympy.assumptions.ask import _extract_all_facts
|
332 |
+
>>> from sympy.abc import x, y
|
333 |
+
>>> assump = CNF.from_prop(Q.positive(x) & Q.integer(y))
|
334 |
+
>>> exprs = (x,)
|
335 |
+
>>> cnf = _extract_all_facts(assump, exprs)
|
336 |
+
>>> cnf.clauses
|
337 |
+
{frozenset({Literal(Q.positive, False)})}
|
338 |
+
|
339 |
+
"""
|
340 |
+
facts = set()
|
341 |
+
|
342 |
+
for clause in assump.clauses:
|
343 |
+
args = []
|
344 |
+
for literal in clause:
|
345 |
+
if isinstance(literal.lit, AppliedPredicate) and len(literal.lit.arguments) == 1:
|
346 |
+
if literal.lit.arg in exprs:
|
347 |
+
# Add literal if it has matching in it
|
348 |
+
args.append(Literal(literal.lit.function, literal.is_Not))
|
349 |
+
else:
|
350 |
+
# If any of the literals doesn't have matching expr don't add the whole clause.
|
351 |
+
break
|
352 |
+
else:
|
353 |
+
if args:
|
354 |
+
facts.add(frozenset(args))
|
355 |
+
return CNF(facts)
|
356 |
+
|
357 |
+
|
358 |
+
def ask(proposition, assumptions=True, context=global_assumptions):
|
359 |
+
"""
|
360 |
+
Function to evaluate the proposition with assumptions.
|
361 |
+
|
362 |
+
Explanation
|
363 |
+
===========
|
364 |
+
|
365 |
+
This function evaluates the proposition to ``True`` or ``False`` if
|
366 |
+
the truth value can be determined. If not, it returns ``None``.
|
367 |
+
|
368 |
+
It should be discerned from :func:`~.refine()` which, when applied to a
|
369 |
+
proposition, simplifies the argument to symbolic ``Boolean`` instead of
|
370 |
+
Python built-in ``True``, ``False`` or ``None``.
|
371 |
+
|
372 |
+
**Syntax**
|
373 |
+
|
374 |
+
* ask(proposition)
|
375 |
+
Evaluate the *proposition* in global assumption context.
|
376 |
+
|
377 |
+
* ask(proposition, assumptions)
|
378 |
+
Evaluate the *proposition* with respect to *assumptions* in
|
379 |
+
global assumption context.
|
380 |
+
|
381 |
+
Parameters
|
382 |
+
==========
|
383 |
+
|
384 |
+
proposition : Boolean
|
385 |
+
Proposition which will be evaluated to boolean value. If this is
|
386 |
+
not ``AppliedPredicate``, it will be wrapped by ``Q.is_true``.
|
387 |
+
|
388 |
+
assumptions : Boolean, optional
|
389 |
+
Local assumptions to evaluate the *proposition*.
|
390 |
+
|
391 |
+
context : AssumptionsContext, optional
|
392 |
+
Default assumptions to evaluate the *proposition*. By default,
|
393 |
+
this is ``sympy.assumptions.global_assumptions`` variable.
|
394 |
+
|
395 |
+
Returns
|
396 |
+
=======
|
397 |
+
|
398 |
+
``True``, ``False``, or ``None``
|
399 |
+
|
400 |
+
Raises
|
401 |
+
======
|
402 |
+
|
403 |
+
TypeError : *proposition* or *assumptions* is not valid logical expression.
|
404 |
+
|
405 |
+
ValueError : assumptions are inconsistent.
|
406 |
+
|
407 |
+
Examples
|
408 |
+
========
|
409 |
+
|
410 |
+
>>> from sympy import ask, Q, pi
|
411 |
+
>>> from sympy.abc import x, y
|
412 |
+
>>> ask(Q.rational(pi))
|
413 |
+
False
|
414 |
+
>>> ask(Q.even(x*y), Q.even(x) & Q.integer(y))
|
415 |
+
True
|
416 |
+
>>> ask(Q.prime(4*x), Q.integer(x))
|
417 |
+
False
|
418 |
+
|
419 |
+
If the truth value cannot be determined, ``None`` will be returned.
|
420 |
+
|
421 |
+
>>> print(ask(Q.odd(3*x))) # cannot determine unless we know x
|
422 |
+
None
|
423 |
+
|
424 |
+
``ValueError`` is raised if assumptions are inconsistent.
|
425 |
+
|
426 |
+
>>> ask(Q.integer(x), Q.even(x) & Q.odd(x))
|
427 |
+
Traceback (most recent call last):
|
428 |
+
...
|
429 |
+
ValueError: inconsistent assumptions Q.even(x) & Q.odd(x)
|
430 |
+
|
431 |
+
Notes
|
432 |
+
=====
|
433 |
+
|
434 |
+
Relations in assumptions are not implemented (yet), so the following
|
435 |
+
will not give a meaningful result.
|
436 |
+
|
437 |
+
>>> ask(Q.positive(x), x > 0)
|
438 |
+
|
439 |
+
It is however a work in progress.
|
440 |
+
|
441 |
+
See Also
|
442 |
+
========
|
443 |
+
|
444 |
+
sympy.assumptions.refine.refine : Simplification using assumptions.
|
445 |
+
Proposition is not reduced to ``None`` if the truth value cannot
|
446 |
+
be determined.
|
447 |
+
"""
|
448 |
+
from sympy.assumptions.satask import satask
|
449 |
+
|
450 |
+
proposition = sympify(proposition)
|
451 |
+
assumptions = sympify(assumptions)
|
452 |
+
|
453 |
+
if isinstance(proposition, Predicate) or proposition.kind is not BooleanKind:
|
454 |
+
raise TypeError("proposition must be a valid logical expression")
|
455 |
+
|
456 |
+
if isinstance(assumptions, Predicate) or assumptions.kind is not BooleanKind:
|
457 |
+
raise TypeError("assumptions must be a valid logical expression")
|
458 |
+
|
459 |
+
binrelpreds = {Eq: Q.eq, Ne: Q.ne, Gt: Q.gt, Lt: Q.lt, Ge: Q.ge, Le: Q.le}
|
460 |
+
if isinstance(proposition, AppliedPredicate):
|
461 |
+
key, args = proposition.function, proposition.arguments
|
462 |
+
elif proposition.func in binrelpreds:
|
463 |
+
key, args = binrelpreds[type(proposition)], proposition.args
|
464 |
+
else:
|
465 |
+
key, args = Q.is_true, (proposition,)
|
466 |
+
|
467 |
+
# convert local and global assumptions to CNF
|
468 |
+
assump_cnf = CNF.from_prop(assumptions)
|
469 |
+
assump_cnf.extend(context)
|
470 |
+
|
471 |
+
# extract the relevant facts from assumptions with respect to args
|
472 |
+
local_facts = _extract_all_facts(assump_cnf, args)
|
473 |
+
|
474 |
+
# convert default facts and assumed facts to encoded CNF
|
475 |
+
known_facts_cnf = get_all_known_facts()
|
476 |
+
enc_cnf = EncodedCNF()
|
477 |
+
enc_cnf.from_cnf(CNF(known_facts_cnf))
|
478 |
+
enc_cnf.add_from_cnf(local_facts)
|
479 |
+
|
480 |
+
# check the satisfiability of given assumptions
|
481 |
+
if local_facts.clauses and satisfiable(enc_cnf) is False:
|
482 |
+
raise ValueError("inconsistent assumptions %s" % assumptions)
|
483 |
+
|
484 |
+
# quick computation for single fact
|
485 |
+
res = _ask_single_fact(key, local_facts)
|
486 |
+
if res is not None:
|
487 |
+
return res
|
488 |
+
|
489 |
+
# direct resolution method, no logic
|
490 |
+
res = key(*args)._eval_ask(assumptions)
|
491 |
+
if res is not None:
|
492 |
+
return bool(res)
|
493 |
+
|
494 |
+
# using satask (still costly)
|
495 |
+
res = satask(proposition, assumptions=assumptions, context=context)
|
496 |
+
return res
|
497 |
+
|
498 |
+
|
499 |
+
def _ask_single_fact(key, local_facts):
|
500 |
+
"""
|
501 |
+
Compute the truth value of single predicate using assumptions.
|
502 |
+
|
503 |
+
Parameters
|
504 |
+
==========
|
505 |
+
|
506 |
+
key : sympy.assumptions.assume.Predicate
|
507 |
+
Proposition predicate.
|
508 |
+
|
509 |
+
local_facts : sympy.assumptions.cnf.CNF
|
510 |
+
Local assumption in CNF form.
|
511 |
+
|
512 |
+
Returns
|
513 |
+
=======
|
514 |
+
|
515 |
+
``True``, ``False`` or ``None``
|
516 |
+
|
517 |
+
Examples
|
518 |
+
========
|
519 |
+
|
520 |
+
>>> from sympy import Q
|
521 |
+
>>> from sympy.assumptions.cnf import CNF
|
522 |
+
>>> from sympy.assumptions.ask import _ask_single_fact
|
523 |
+
|
524 |
+
If prerequisite of proposition is rejected by the assumption,
|
525 |
+
return ``False``.
|
526 |
+
|
527 |
+
>>> key, assump = Q.zero, ~Q.zero
|
528 |
+
>>> local_facts = CNF.from_prop(assump)
|
529 |
+
>>> _ask_single_fact(key, local_facts)
|
530 |
+
False
|
531 |
+
>>> key, assump = Q.zero, ~Q.even
|
532 |
+
>>> local_facts = CNF.from_prop(assump)
|
533 |
+
>>> _ask_single_fact(key, local_facts)
|
534 |
+
False
|
535 |
+
|
536 |
+
If assumption implies the proposition, return ``True``.
|
537 |
+
|
538 |
+
>>> key, assump = Q.even, Q.zero
|
539 |
+
>>> local_facts = CNF.from_prop(assump)
|
540 |
+
>>> _ask_single_fact(key, local_facts)
|
541 |
+
True
|
542 |
+
|
543 |
+
If proposition rejects the assumption, return ``False``.
|
544 |
+
|
545 |
+
>>> key, assump = Q.even, Q.odd
|
546 |
+
>>> local_facts = CNF.from_prop(assump)
|
547 |
+
>>> _ask_single_fact(key, local_facts)
|
548 |
+
False
|
549 |
+
"""
|
550 |
+
if local_facts.clauses:
|
551 |
+
|
552 |
+
known_facts_dict = get_known_facts_dict()
|
553 |
+
|
554 |
+
if len(local_facts.clauses) == 1:
|
555 |
+
cl, = local_facts.clauses
|
556 |
+
if len(cl) == 1:
|
557 |
+
f, = cl
|
558 |
+
prop_facts = known_facts_dict.get(key, None)
|
559 |
+
prop_req = prop_facts[0] if prop_facts is not None else set()
|
560 |
+
if f.is_Not and f.arg in prop_req:
|
561 |
+
# the prerequisite of proposition is rejected
|
562 |
+
return False
|
563 |
+
|
564 |
+
for clause in local_facts.clauses:
|
565 |
+
if len(clause) == 1:
|
566 |
+
f, = clause
|
567 |
+
prop_facts = known_facts_dict.get(f.arg, None) if not f.is_Not else None
|
568 |
+
if prop_facts is None:
|
569 |
+
continue
|
570 |
+
|
571 |
+
prop_req, prop_rej = prop_facts
|
572 |
+
if key in prop_req:
|
573 |
+
# assumption implies the proposition
|
574 |
+
return True
|
575 |
+
elif key in prop_rej:
|
576 |
+
# proposition rejects the assumption
|
577 |
+
return False
|
578 |
+
|
579 |
+
return None
|
580 |
+
|
581 |
+
|
582 |
+
def register_handler(key, handler):
|
583 |
+
"""
|
584 |
+
Register a handler in the ask system. key must be a string and handler a
|
585 |
+
class inheriting from AskHandler.
|
586 |
+
|
587 |
+
.. deprecated:: 1.8.
|
588 |
+
Use multipledispatch handler instead. See :obj:`~.Predicate`.
|
589 |
+
|
590 |
+
"""
|
591 |
+
sympy_deprecation_warning(
|
592 |
+
"""
|
593 |
+
The AskHandler system is deprecated. The register_handler() function
|
594 |
+
should be replaced with the multipledispatch handler of Predicate.
|
595 |
+
""",
|
596 |
+
deprecated_since_version="1.8",
|
597 |
+
active_deprecations_target='deprecated-askhandler',
|
598 |
+
)
|
599 |
+
if isinstance(key, Predicate):
|
600 |
+
key = key.name.name
|
601 |
+
Qkey = getattr(Q, key, None)
|
602 |
+
if Qkey is not None:
|
603 |
+
Qkey.add_handler(handler)
|
604 |
+
else:
|
605 |
+
setattr(Q, key, Predicate(key, handlers=[handler]))
|
606 |
+
|
607 |
+
|
608 |
+
def remove_handler(key, handler):
|
609 |
+
"""
|
610 |
+
Removes a handler from the ask system.
|
611 |
+
|
612 |
+
.. deprecated:: 1.8.
|
613 |
+
Use multipledispatch handler instead. See :obj:`~.Predicate`.
|
614 |
+
|
615 |
+
"""
|
616 |
+
sympy_deprecation_warning(
|
617 |
+
"""
|
618 |
+
The AskHandler system is deprecated. The remove_handler() function
|
619 |
+
should be replaced with the multipledispatch handler of Predicate.
|
620 |
+
""",
|
621 |
+
deprecated_since_version="1.8",
|
622 |
+
active_deprecations_target='deprecated-askhandler',
|
623 |
+
)
|
624 |
+
if isinstance(key, Predicate):
|
625 |
+
key = key.name.name
|
626 |
+
# Don't show the same warning again recursively
|
627 |
+
with ignore_warnings(SymPyDeprecationWarning):
|
628 |
+
getattr(Q, key).remove_handler(handler)
|
629 |
+
|
630 |
+
|
631 |
+
from sympy.assumptions.ask_generated import (get_all_known_facts,
|
632 |
+
get_known_facts_dict)
|
env-llmeval/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 |
+
}
|
env-llmeval/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)
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/cnf.py
ADDED
@@ -0,0 +1,453 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""
|
2 |
+
The classes used here are for the internal use of assumptions system
|
3 |
+
only and should not be used anywhere else as these do not possess the
|
4 |
+
signatures common to SymPy objects. For general use of logic constructs
|
5 |
+
please refer to sympy.logic classes And, Or, Not, etc.
|
6 |
+
"""
|
7 |
+
from itertools import combinations, product, zip_longest
|
8 |
+
from sympy.assumptions.assume import AppliedPredicate, Predicate
|
9 |
+
from sympy.core.relational import Eq, Ne, Gt, Lt, Ge, Le
|
10 |
+
from sympy.core.singleton import S
|
11 |
+
from sympy.logic.boolalg import Or, And, Not, Xnor
|
12 |
+
from sympy.logic.boolalg import (Equivalent, ITE, Implies, Nand, Nor, Xor)
|
13 |
+
|
14 |
+
|
15 |
+
class Literal:
|
16 |
+
"""
|
17 |
+
The smallest element of a CNF object.
|
18 |
+
|
19 |
+
Parameters
|
20 |
+
==========
|
21 |
+
|
22 |
+
lit : Boolean expression
|
23 |
+
|
24 |
+
is_Not : bool
|
25 |
+
|
26 |
+
Examples
|
27 |
+
========
|
28 |
+
|
29 |
+
>>> from sympy import Q
|
30 |
+
>>> from sympy.assumptions.cnf import Literal
|
31 |
+
>>> from sympy.abc import x
|
32 |
+
>>> Literal(Q.even(x))
|
33 |
+
Literal(Q.even(x), False)
|
34 |
+
>>> Literal(~Q.even(x))
|
35 |
+
Literal(Q.even(x), True)
|
36 |
+
"""
|
37 |
+
|
38 |
+
def __new__(cls, lit, is_Not=False):
|
39 |
+
if isinstance(lit, Not):
|
40 |
+
lit = lit.args[0]
|
41 |
+
is_Not = True
|
42 |
+
elif isinstance(lit, (AND, OR, Literal)):
|
43 |
+
return ~lit if is_Not else lit
|
44 |
+
obj = super().__new__(cls)
|
45 |
+
obj.lit = lit
|
46 |
+
obj.is_Not = is_Not
|
47 |
+
return obj
|
48 |
+
|
49 |
+
@property
|
50 |
+
def arg(self):
|
51 |
+
return self.lit
|
52 |
+
|
53 |
+
def rcall(self, expr):
|
54 |
+
if callable(self.lit):
|
55 |
+
lit = self.lit(expr)
|
56 |
+
else:
|
57 |
+
try:
|
58 |
+
lit = self.lit.apply(expr)
|
59 |
+
except AttributeError:
|
60 |
+
lit = self.lit.rcall(expr)
|
61 |
+
return type(self)(lit, self.is_Not)
|
62 |
+
|
63 |
+
def __invert__(self):
|
64 |
+
is_Not = not self.is_Not
|
65 |
+
return Literal(self.lit, is_Not)
|
66 |
+
|
67 |
+
def __str__(self):
|
68 |
+
return '{}({}, {})'.format(type(self).__name__, self.lit, self.is_Not)
|
69 |
+
|
70 |
+
__repr__ = __str__
|
71 |
+
|
72 |
+
def __eq__(self, other):
|
73 |
+
return self.arg == other.arg and self.is_Not == other.is_Not
|
74 |
+
|
75 |
+
def __hash__(self):
|
76 |
+
h = hash((type(self).__name__, self.arg, self.is_Not))
|
77 |
+
return h
|
78 |
+
|
79 |
+
|
80 |
+
class OR:
|
81 |
+
"""
|
82 |
+
A low-level implementation for Or
|
83 |
+
"""
|
84 |
+
def __init__(self, *args):
|
85 |
+
self._args = args
|
86 |
+
|
87 |
+
@property
|
88 |
+
def args(self):
|
89 |
+
return sorted(self._args, key=str)
|
90 |
+
|
91 |
+
def rcall(self, expr):
|
92 |
+
return type(self)(*[arg.rcall(expr)
|
93 |
+
for arg in self._args
|
94 |
+
])
|
95 |
+
|
96 |
+
def __invert__(self):
|
97 |
+
return AND(*[~arg for arg in self._args])
|
98 |
+
|
99 |
+
def __hash__(self):
|
100 |
+
return hash((type(self).__name__,) + tuple(self.args))
|
101 |
+
|
102 |
+
def __eq__(self, other):
|
103 |
+
return self.args == other.args
|
104 |
+
|
105 |
+
def __str__(self):
|
106 |
+
s = '(' + ' | '.join([str(arg) for arg in self.args]) + ')'
|
107 |
+
return s
|
108 |
+
|
109 |
+
__repr__ = __str__
|
110 |
+
|
111 |
+
|
112 |
+
class AND:
|
113 |
+
"""
|
114 |
+
A low-level implementation for And
|
115 |
+
"""
|
116 |
+
def __init__(self, *args):
|
117 |
+
self._args = args
|
118 |
+
|
119 |
+
def __invert__(self):
|
120 |
+
return OR(*[~arg for arg in self._args])
|
121 |
+
|
122 |
+
@property
|
123 |
+
def args(self):
|
124 |
+
return sorted(self._args, key=str)
|
125 |
+
|
126 |
+
def rcall(self, expr):
|
127 |
+
return type(self)(*[arg.rcall(expr)
|
128 |
+
for arg in self._args
|
129 |
+
])
|
130 |
+
|
131 |
+
def __hash__(self):
|
132 |
+
return hash((type(self).__name__,) + tuple(self.args))
|
133 |
+
|
134 |
+
def __eq__(self, other):
|
135 |
+
return self.args == other.args
|
136 |
+
|
137 |
+
def __str__(self):
|
138 |
+
s = '('+' & '.join([str(arg) for arg in self.args])+')'
|
139 |
+
return s
|
140 |
+
|
141 |
+
__repr__ = __str__
|
142 |
+
|
143 |
+
|
144 |
+
def to_NNF(expr, composite_map=None):
|
145 |
+
"""
|
146 |
+
Generates the Negation Normal Form of any boolean expression in terms
|
147 |
+
of AND, OR, and Literal objects.
|
148 |
+
|
149 |
+
Examples
|
150 |
+
========
|
151 |
+
|
152 |
+
>>> from sympy import Q, Eq
|
153 |
+
>>> from sympy.assumptions.cnf import to_NNF
|
154 |
+
>>> from sympy.abc import x, y
|
155 |
+
>>> expr = Q.even(x) & ~Q.positive(x)
|
156 |
+
>>> to_NNF(expr)
|
157 |
+
(Literal(Q.even(x), False) & Literal(Q.positive(x), True))
|
158 |
+
|
159 |
+
Supported boolean objects are converted to corresponding predicates.
|
160 |
+
|
161 |
+
>>> to_NNF(Eq(x, y))
|
162 |
+
Literal(Q.eq(x, y), False)
|
163 |
+
|
164 |
+
If ``composite_map`` argument is given, ``to_NNF`` decomposes the
|
165 |
+
specified predicate into a combination of primitive predicates.
|
166 |
+
|
167 |
+
>>> cmap = {Q.nonpositive: Q.negative | Q.zero}
|
168 |
+
>>> to_NNF(Q.nonpositive, cmap)
|
169 |
+
(Literal(Q.negative, False) | Literal(Q.zero, False))
|
170 |
+
>>> to_NNF(Q.nonpositive(x), cmap)
|
171 |
+
(Literal(Q.negative(x), False) | Literal(Q.zero(x), False))
|
172 |
+
"""
|
173 |
+
from sympy.assumptions.ask import Q
|
174 |
+
|
175 |
+
if composite_map is None:
|
176 |
+
composite_map = {}
|
177 |
+
|
178 |
+
|
179 |
+
binrelpreds = {Eq: Q.eq, Ne: Q.ne, Gt: Q.gt, Lt: Q.lt, Ge: Q.ge, Le: Q.le}
|
180 |
+
if type(expr) in binrelpreds:
|
181 |
+
pred = binrelpreds[type(expr)]
|
182 |
+
expr = pred(*expr.args)
|
183 |
+
|
184 |
+
if isinstance(expr, Not):
|
185 |
+
arg = expr.args[0]
|
186 |
+
tmp = to_NNF(arg, composite_map) # Strategy: negate the NNF of expr
|
187 |
+
return ~tmp
|
188 |
+
|
189 |
+
if isinstance(expr, Or):
|
190 |
+
return OR(*[to_NNF(x, composite_map) for x in Or.make_args(expr)])
|
191 |
+
|
192 |
+
if isinstance(expr, And):
|
193 |
+
return AND(*[to_NNF(x, composite_map) for x in And.make_args(expr)])
|
194 |
+
|
195 |
+
if isinstance(expr, Nand):
|
196 |
+
tmp = AND(*[to_NNF(x, composite_map) for x in expr.args])
|
197 |
+
return ~tmp
|
198 |
+
|
199 |
+
if isinstance(expr, Nor):
|
200 |
+
tmp = OR(*[to_NNF(x, composite_map) for x in expr.args])
|
201 |
+
return ~tmp
|
202 |
+
|
203 |
+
if isinstance(expr, Xor):
|
204 |
+
cnfs = []
|
205 |
+
for i in range(0, len(expr.args) + 1, 2):
|
206 |
+
for neg in combinations(expr.args, i):
|
207 |
+
clause = [~to_NNF(s, composite_map) if s in neg else to_NNF(s, composite_map)
|
208 |
+
for s in expr.args]
|
209 |
+
cnfs.append(OR(*clause))
|
210 |
+
return AND(*cnfs)
|
211 |
+
|
212 |
+
if isinstance(expr, Xnor):
|
213 |
+
cnfs = []
|
214 |
+
for i in range(0, len(expr.args) + 1, 2):
|
215 |
+
for neg in combinations(expr.args, i):
|
216 |
+
clause = [~to_NNF(s, composite_map) if s in neg else to_NNF(s, composite_map)
|
217 |
+
for s in expr.args]
|
218 |
+
cnfs.append(OR(*clause))
|
219 |
+
return ~AND(*cnfs)
|
220 |
+
|
221 |
+
if isinstance(expr, Implies):
|
222 |
+
L, R = to_NNF(expr.args[0], composite_map), to_NNF(expr.args[1], composite_map)
|
223 |
+
return OR(~L, R)
|
224 |
+
|
225 |
+
if isinstance(expr, Equivalent):
|
226 |
+
cnfs = []
|
227 |
+
for a, b in zip_longest(expr.args, expr.args[1:], fillvalue=expr.args[0]):
|
228 |
+
a = to_NNF(a, composite_map)
|
229 |
+
b = to_NNF(b, composite_map)
|
230 |
+
cnfs.append(OR(~a, b))
|
231 |
+
return AND(*cnfs)
|
232 |
+
|
233 |
+
if isinstance(expr, ITE):
|
234 |
+
L = to_NNF(expr.args[0], composite_map)
|
235 |
+
M = to_NNF(expr.args[1], composite_map)
|
236 |
+
R = to_NNF(expr.args[2], composite_map)
|
237 |
+
return AND(OR(~L, M), OR(L, R))
|
238 |
+
|
239 |
+
if isinstance(expr, AppliedPredicate):
|
240 |
+
pred, args = expr.function, expr.arguments
|
241 |
+
newpred = composite_map.get(pred, None)
|
242 |
+
if newpred is not None:
|
243 |
+
return to_NNF(newpred.rcall(*args), composite_map)
|
244 |
+
|
245 |
+
if isinstance(expr, Predicate):
|
246 |
+
newpred = composite_map.get(expr, None)
|
247 |
+
if newpred is not None:
|
248 |
+
return to_NNF(newpred, composite_map)
|
249 |
+
|
250 |
+
return Literal(expr)
|
251 |
+
|
252 |
+
|
253 |
+
def distribute_AND_over_OR(expr):
|
254 |
+
"""
|
255 |
+
Distributes AND over OR in the NNF expression.
|
256 |
+
Returns the result( Conjunctive Normal Form of expression)
|
257 |
+
as a CNF object.
|
258 |
+
"""
|
259 |
+
if not isinstance(expr, (AND, OR)):
|
260 |
+
tmp = set()
|
261 |
+
tmp.add(frozenset((expr,)))
|
262 |
+
return CNF(tmp)
|
263 |
+
|
264 |
+
if isinstance(expr, OR):
|
265 |
+
return CNF.all_or(*[distribute_AND_over_OR(arg)
|
266 |
+
for arg in expr._args])
|
267 |
+
|
268 |
+
if isinstance(expr, AND):
|
269 |
+
return CNF.all_and(*[distribute_AND_over_OR(arg)
|
270 |
+
for arg in expr._args])
|
271 |
+
|
272 |
+
|
273 |
+
class CNF:
|
274 |
+
"""
|
275 |
+
Class to represent CNF of a Boolean expression.
|
276 |
+
Consists of set of clauses, which themselves are stored as
|
277 |
+
frozenset of Literal objects.
|
278 |
+
|
279 |
+
Examples
|
280 |
+
========
|
281 |
+
|
282 |
+
>>> from sympy import Q
|
283 |
+
>>> from sympy.assumptions.cnf import CNF
|
284 |
+
>>> from sympy.abc import x
|
285 |
+
>>> cnf = CNF.from_prop(Q.real(x) & ~Q.zero(x))
|
286 |
+
>>> cnf.clauses
|
287 |
+
{frozenset({Literal(Q.zero(x), True)}),
|
288 |
+
frozenset({Literal(Q.negative(x), False),
|
289 |
+
Literal(Q.positive(x), False), Literal(Q.zero(x), False)})}
|
290 |
+
"""
|
291 |
+
def __init__(self, clauses=None):
|
292 |
+
if not clauses:
|
293 |
+
clauses = set()
|
294 |
+
self.clauses = clauses
|
295 |
+
|
296 |
+
def add(self, prop):
|
297 |
+
clauses = CNF.to_CNF(prop).clauses
|
298 |
+
self.add_clauses(clauses)
|
299 |
+
|
300 |
+
def __str__(self):
|
301 |
+
s = ' & '.join(
|
302 |
+
['(' + ' | '.join([str(lit) for lit in clause]) +')'
|
303 |
+
for clause in self.clauses]
|
304 |
+
)
|
305 |
+
return s
|
306 |
+
|
307 |
+
def extend(self, props):
|
308 |
+
for p in props:
|
309 |
+
self.add(p)
|
310 |
+
return self
|
311 |
+
|
312 |
+
def copy(self):
|
313 |
+
return CNF(set(self.clauses))
|
314 |
+
|
315 |
+
def add_clauses(self, clauses):
|
316 |
+
self.clauses |= clauses
|
317 |
+
|
318 |
+
@classmethod
|
319 |
+
def from_prop(cls, prop):
|
320 |
+
res = cls()
|
321 |
+
res.add(prop)
|
322 |
+
return res
|
323 |
+
|
324 |
+
def __iand__(self, other):
|
325 |
+
self.add_clauses(other.clauses)
|
326 |
+
return self
|
327 |
+
|
328 |
+
def all_predicates(self):
|
329 |
+
predicates = set()
|
330 |
+
for c in self.clauses:
|
331 |
+
predicates |= {arg.lit for arg in c}
|
332 |
+
return predicates
|
333 |
+
|
334 |
+
def _or(self, cnf):
|
335 |
+
clauses = set()
|
336 |
+
for a, b in product(self.clauses, cnf.clauses):
|
337 |
+
tmp = set(a)
|
338 |
+
for t in b:
|
339 |
+
tmp.add(t)
|
340 |
+
clauses.add(frozenset(tmp))
|
341 |
+
return CNF(clauses)
|
342 |
+
|
343 |
+
def _and(self, cnf):
|
344 |
+
clauses = self.clauses.union(cnf.clauses)
|
345 |
+
return CNF(clauses)
|
346 |
+
|
347 |
+
def _not(self):
|
348 |
+
clss = list(self.clauses)
|
349 |
+
ll = set()
|
350 |
+
for x in clss[-1]:
|
351 |
+
ll.add(frozenset((~x,)))
|
352 |
+
ll = CNF(ll)
|
353 |
+
|
354 |
+
for rest in clss[:-1]:
|
355 |
+
p = set()
|
356 |
+
for x in rest:
|
357 |
+
p.add(frozenset((~x,)))
|
358 |
+
ll = ll._or(CNF(p))
|
359 |
+
return ll
|
360 |
+
|
361 |
+
def rcall(self, expr):
|
362 |
+
clause_list = []
|
363 |
+
for clause in self.clauses:
|
364 |
+
lits = [arg.rcall(expr) for arg in clause]
|
365 |
+
clause_list.append(OR(*lits))
|
366 |
+
expr = AND(*clause_list)
|
367 |
+
return distribute_AND_over_OR(expr)
|
368 |
+
|
369 |
+
@classmethod
|
370 |
+
def all_or(cls, *cnfs):
|
371 |
+
b = cnfs[0].copy()
|
372 |
+
for rest in cnfs[1:]:
|
373 |
+
b = b._or(rest)
|
374 |
+
return b
|
375 |
+
|
376 |
+
@classmethod
|
377 |
+
def all_and(cls, *cnfs):
|
378 |
+
b = cnfs[0].copy()
|
379 |
+
for rest in cnfs[1:]:
|
380 |
+
b = b._and(rest)
|
381 |
+
return b
|
382 |
+
|
383 |
+
@classmethod
|
384 |
+
def to_CNF(cls, expr):
|
385 |
+
from sympy.assumptions.facts import get_composite_predicates
|
386 |
+
expr = to_NNF(expr, get_composite_predicates())
|
387 |
+
expr = distribute_AND_over_OR(expr)
|
388 |
+
return expr
|
389 |
+
|
390 |
+
@classmethod
|
391 |
+
def CNF_to_cnf(cls, cnf):
|
392 |
+
"""
|
393 |
+
Converts CNF object to SymPy's boolean expression
|
394 |
+
retaining the form of expression.
|
395 |
+
"""
|
396 |
+
def remove_literal(arg):
|
397 |
+
return Not(arg.lit) if arg.is_Not else arg.lit
|
398 |
+
|
399 |
+
return And(*(Or(*(remove_literal(arg) for arg in clause)) for clause in cnf.clauses))
|
400 |
+
|
401 |
+
|
402 |
+
class EncodedCNF:
|
403 |
+
"""
|
404 |
+
Class for encoding the CNF expression.
|
405 |
+
"""
|
406 |
+
def __init__(self, data=None, encoding=None):
|
407 |
+
if not data and not encoding:
|
408 |
+
data = []
|
409 |
+
encoding = {}
|
410 |
+
self.data = data
|
411 |
+
self.encoding = encoding
|
412 |
+
self._symbols = list(encoding.keys())
|
413 |
+
|
414 |
+
def from_cnf(self, cnf):
|
415 |
+
self._symbols = list(cnf.all_predicates())
|
416 |
+
n = len(self._symbols)
|
417 |
+
self.encoding = dict(zip(self._symbols, range(1, n + 1)))
|
418 |
+
self.data = [self.encode(clause) for clause in cnf.clauses]
|
419 |
+
|
420 |
+
@property
|
421 |
+
def symbols(self):
|
422 |
+
return self._symbols
|
423 |
+
|
424 |
+
@property
|
425 |
+
def variables(self):
|
426 |
+
return range(1, len(self._symbols) + 1)
|
427 |
+
|
428 |
+
def copy(self):
|
429 |
+
new_data = [set(clause) for clause in self.data]
|
430 |
+
return EncodedCNF(new_data, dict(self.encoding))
|
431 |
+
|
432 |
+
def add_prop(self, prop):
|
433 |
+
cnf = CNF.from_prop(prop)
|
434 |
+
self.add_from_cnf(cnf)
|
435 |
+
|
436 |
+
def add_from_cnf(self, cnf):
|
437 |
+
clauses = [self.encode(clause) for clause in cnf.clauses]
|
438 |
+
self.data += clauses
|
439 |
+
|
440 |
+
def encode_arg(self, arg):
|
441 |
+
literal = arg.lit
|
442 |
+
value = self.encoding.get(literal, None)
|
443 |
+
if value is None:
|
444 |
+
n = len(self._symbols)
|
445 |
+
self._symbols.append(literal)
|
446 |
+
value = self.encoding[literal] = n + 1
|
447 |
+
if arg.is_Not:
|
448 |
+
return -value
|
449 |
+
else:
|
450 |
+
return value
|
451 |
+
|
452 |
+
def encode(self, clause):
|
453 |
+
return {self.encode_arg(arg) if not arg.lit == S.false else 0 for arg in clause}
|
env-llmeval/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
|
env-llmeval/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 |
+
]
|
env-llmeval/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
|
env-llmeval/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
|
env-llmeval/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
|
env-llmeval/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)
|
env-llmeval/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)
|
env-llmeval/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 |
+
"""
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (308 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/calculus.cpython-310.pyc
ADDED
Binary file (2.31 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/common.cpython-310.pyc
ADDED
Binary file (2.7 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/matrices.cpython-310.pyc
ADDED
Binary file (13 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/ntheory.cpython-310.pyc
ADDED
Binary file (2.94 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/order.cpython-310.pyc
ADDED
Binary file (10.1 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/predicates/__pycache__/sets.cpython-310.pyc
ADDED
Binary file (9.26 kB). View file
|
|
env-llmeval/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")
|
env-llmeval/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)
|
env-llmeval/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'.")
|
env-llmeval/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 |
+
)
|
env-llmeval/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")
|
env-llmeval/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 |
+
)
|
env-llmeval/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 |
+
}
|
env-llmeval/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
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (454 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/binrel.cpython-310.pyc
ADDED
Binary file (6.51 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/equality.cpython-310.pyc
ADDED
Binary file (7.69 kB). View file
|
|
env-llmeval/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
|
env-llmeval/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)
|
env-llmeval/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
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/sathandlers.py
ADDED
@@ -0,0 +1,323 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from collections import defaultdict
|
2 |
+
|
3 |
+
from sympy.assumptions.ask import Q
|
4 |
+
from sympy.core import (Add, Mul, Pow, Number, NumberSymbol, Symbol)
|
5 |
+
from sympy.core.numbers import ImaginaryUnit
|
6 |
+
from sympy.functions.elementary.complexes import Abs
|
7 |
+
from sympy.logic.boolalg import (Equivalent, And, Or, Implies)
|
8 |
+
from sympy.matrices.expressions import MatMul
|
9 |
+
|
10 |
+
# APIs here may be subject to change
|
11 |
+
|
12 |
+
|
13 |
+
### Helper functions ###
|
14 |
+
|
15 |
+
def allargs(symbol, fact, expr):
|
16 |
+
"""
|
17 |
+
Apply all arguments of the expression to the fact structure.
|
18 |
+
|
19 |
+
Parameters
|
20 |
+
==========
|
21 |
+
|
22 |
+
symbol : Symbol
|
23 |
+
A placeholder symbol.
|
24 |
+
|
25 |
+
fact : Boolean
|
26 |
+
Resulting ``Boolean`` expression.
|
27 |
+
|
28 |
+
expr : Expr
|
29 |
+
|
30 |
+
Examples
|
31 |
+
========
|
32 |
+
|
33 |
+
>>> from sympy import Q
|
34 |
+
>>> from sympy.assumptions.sathandlers import allargs
|
35 |
+
>>> from sympy.abc import x, y
|
36 |
+
>>> allargs(x, Q.negative(x) | Q.positive(x), x*y)
|
37 |
+
(Q.negative(x) | Q.positive(x)) & (Q.negative(y) | Q.positive(y))
|
38 |
+
|
39 |
+
"""
|
40 |
+
return And(*[fact.subs(symbol, arg) for arg in expr.args])
|
41 |
+
|
42 |
+
|
43 |
+
def anyarg(symbol, fact, expr):
|
44 |
+
"""
|
45 |
+
Apply any argument of the expression to the fact structure.
|
46 |
+
|
47 |
+
Parameters
|
48 |
+
==========
|
49 |
+
|
50 |
+
symbol : Symbol
|
51 |
+
A placeholder symbol.
|
52 |
+
|
53 |
+
fact : Boolean
|
54 |
+
Resulting ``Boolean`` expression.
|
55 |
+
|
56 |
+
expr : Expr
|
57 |
+
|
58 |
+
Examples
|
59 |
+
========
|
60 |
+
|
61 |
+
>>> from sympy import Q
|
62 |
+
>>> from sympy.assumptions.sathandlers import anyarg
|
63 |
+
>>> from sympy.abc import x, y
|
64 |
+
>>> anyarg(x, Q.negative(x) & Q.positive(x), x*y)
|
65 |
+
(Q.negative(x) & Q.positive(x)) | (Q.negative(y) & Q.positive(y))
|
66 |
+
|
67 |
+
"""
|
68 |
+
return Or(*[fact.subs(symbol, arg) for arg in expr.args])
|
69 |
+
|
70 |
+
|
71 |
+
def exactlyonearg(symbol, fact, expr):
|
72 |
+
"""
|
73 |
+
Apply exactly one argument of the expression to the fact structure.
|
74 |
+
|
75 |
+
Parameters
|
76 |
+
==========
|
77 |
+
|
78 |
+
symbol : Symbol
|
79 |
+
A placeholder symbol.
|
80 |
+
|
81 |
+
fact : Boolean
|
82 |
+
Resulting ``Boolean`` expression.
|
83 |
+
|
84 |
+
expr : Expr
|
85 |
+
|
86 |
+
Examples
|
87 |
+
========
|
88 |
+
|
89 |
+
>>> from sympy import Q
|
90 |
+
>>> from sympy.assumptions.sathandlers import exactlyonearg
|
91 |
+
>>> from sympy.abc import x, y
|
92 |
+
>>> exactlyonearg(x, Q.positive(x), x*y)
|
93 |
+
(Q.positive(x) & ~Q.positive(y)) | (Q.positive(y) & ~Q.positive(x))
|
94 |
+
|
95 |
+
"""
|
96 |
+
pred_args = [fact.subs(symbol, arg) for arg in expr.args]
|
97 |
+
res = Or(*[And(pred_args[i], *[~lit for lit in pred_args[:i] +
|
98 |
+
pred_args[i+1:]]) for i in range(len(pred_args))])
|
99 |
+
return res
|
100 |
+
|
101 |
+
|
102 |
+
### Fact registry ###
|
103 |
+
|
104 |
+
class ClassFactRegistry:
|
105 |
+
"""
|
106 |
+
Register handlers against classes.
|
107 |
+
|
108 |
+
Explanation
|
109 |
+
===========
|
110 |
+
|
111 |
+
``register`` method registers the handler function for a class. Here,
|
112 |
+
handler function should return a single fact. ``multiregister`` method
|
113 |
+
registers the handler function for multiple classes. Here, handler function
|
114 |
+
should return a container of multiple facts.
|
115 |
+
|
116 |
+
``registry(expr)`` returns a set of facts for *expr*.
|
117 |
+
|
118 |
+
Examples
|
119 |
+
========
|
120 |
+
|
121 |
+
Here, we register the facts for ``Abs``.
|
122 |
+
|
123 |
+
>>> from sympy import Abs, Equivalent, Q
|
124 |
+
>>> from sympy.assumptions.sathandlers import ClassFactRegistry
|
125 |
+
>>> reg = ClassFactRegistry()
|
126 |
+
>>> @reg.register(Abs)
|
127 |
+
... def f1(expr):
|
128 |
+
... return Q.nonnegative(expr)
|
129 |
+
>>> @reg.register(Abs)
|
130 |
+
... def f2(expr):
|
131 |
+
... arg = expr.args[0]
|
132 |
+
... return Equivalent(~Q.zero(arg), ~Q.zero(expr))
|
133 |
+
|
134 |
+
Calling the registry with expression returns the defined facts for the
|
135 |
+
expression.
|
136 |
+
|
137 |
+
>>> from sympy.abc import x
|
138 |
+
>>> reg(Abs(x))
|
139 |
+
{Q.nonnegative(Abs(x)), Equivalent(~Q.zero(x), ~Q.zero(Abs(x)))}
|
140 |
+
|
141 |
+
Multiple facts can be registered at once by ``multiregister`` method.
|
142 |
+
|
143 |
+
>>> reg2 = ClassFactRegistry()
|
144 |
+
>>> @reg2.multiregister(Abs)
|
145 |
+
... def _(expr):
|
146 |
+
... arg = expr.args[0]
|
147 |
+
... return [Q.even(arg) >> Q.even(expr), Q.odd(arg) >> Q.odd(expr)]
|
148 |
+
>>> reg2(Abs(x))
|
149 |
+
{Implies(Q.even(x), Q.even(Abs(x))), Implies(Q.odd(x), Q.odd(Abs(x)))}
|
150 |
+
|
151 |
+
"""
|
152 |
+
def __init__(self):
|
153 |
+
self.singlefacts = defaultdict(frozenset)
|
154 |
+
self.multifacts = defaultdict(frozenset)
|
155 |
+
|
156 |
+
def register(self, cls):
|
157 |
+
def _(func):
|
158 |
+
self.singlefacts[cls] |= {func}
|
159 |
+
return func
|
160 |
+
return _
|
161 |
+
|
162 |
+
def multiregister(self, *classes):
|
163 |
+
def _(func):
|
164 |
+
for cls in classes:
|
165 |
+
self.multifacts[cls] |= {func}
|
166 |
+
return func
|
167 |
+
return _
|
168 |
+
|
169 |
+
def __getitem__(self, key):
|
170 |
+
ret1 = self.singlefacts[key]
|
171 |
+
for k in self.singlefacts:
|
172 |
+
if issubclass(key, k):
|
173 |
+
ret1 |= self.singlefacts[k]
|
174 |
+
|
175 |
+
ret2 = self.multifacts[key]
|
176 |
+
for k in self.multifacts:
|
177 |
+
if issubclass(key, k):
|
178 |
+
ret2 |= self.multifacts[k]
|
179 |
+
|
180 |
+
return ret1, ret2
|
181 |
+
|
182 |
+
def __call__(self, expr):
|
183 |
+
ret = set()
|
184 |
+
|
185 |
+
handlers1, handlers2 = self[type(expr)]
|
186 |
+
|
187 |
+
for h in handlers1:
|
188 |
+
ret.add(h(expr))
|
189 |
+
for h in handlers2:
|
190 |
+
ret.update(h(expr))
|
191 |
+
return ret
|
192 |
+
|
193 |
+
class_fact_registry = ClassFactRegistry()
|
194 |
+
|
195 |
+
|
196 |
+
|
197 |
+
### Class fact registration ###
|
198 |
+
|
199 |
+
x = Symbol('x')
|
200 |
+
|
201 |
+
## Abs ##
|
202 |
+
|
203 |
+
@class_fact_registry.multiregister(Abs)
|
204 |
+
def _(expr):
|
205 |
+
arg = expr.args[0]
|
206 |
+
return [Q.nonnegative(expr),
|
207 |
+
Equivalent(~Q.zero(arg), ~Q.zero(expr)),
|
208 |
+
Q.even(arg) >> Q.even(expr),
|
209 |
+
Q.odd(arg) >> Q.odd(expr),
|
210 |
+
Q.integer(arg) >> Q.integer(expr),
|
211 |
+
]
|
212 |
+
|
213 |
+
|
214 |
+
### Add ##
|
215 |
+
|
216 |
+
@class_fact_registry.multiregister(Add)
|
217 |
+
def _(expr):
|
218 |
+
return [allargs(x, Q.positive(x), expr) >> Q.positive(expr),
|
219 |
+
allargs(x, Q.negative(x), expr) >> Q.negative(expr),
|
220 |
+
allargs(x, Q.real(x), expr) >> Q.real(expr),
|
221 |
+
allargs(x, Q.rational(x), expr) >> Q.rational(expr),
|
222 |
+
allargs(x, Q.integer(x), expr) >> Q.integer(expr),
|
223 |
+
exactlyonearg(x, ~Q.integer(x), expr) >> ~Q.integer(expr),
|
224 |
+
]
|
225 |
+
|
226 |
+
@class_fact_registry.register(Add)
|
227 |
+
def _(expr):
|
228 |
+
allargs_real = allargs(x, Q.real(x), expr)
|
229 |
+
onearg_irrational = exactlyonearg(x, Q.irrational(x), expr)
|
230 |
+
return Implies(allargs_real, Implies(onearg_irrational, Q.irrational(expr)))
|
231 |
+
|
232 |
+
|
233 |
+
### Mul ###
|
234 |
+
|
235 |
+
@class_fact_registry.multiregister(Mul)
|
236 |
+
def _(expr):
|
237 |
+
return [Equivalent(Q.zero(expr), anyarg(x, Q.zero(x), expr)),
|
238 |
+
allargs(x, Q.positive(x), expr) >> Q.positive(expr),
|
239 |
+
allargs(x, Q.real(x), expr) >> Q.real(expr),
|
240 |
+
allargs(x, Q.rational(x), expr) >> Q.rational(expr),
|
241 |
+
allargs(x, Q.integer(x), expr) >> Q.integer(expr),
|
242 |
+
exactlyonearg(x, ~Q.rational(x), expr) >> ~Q.integer(expr),
|
243 |
+
allargs(x, Q.commutative(x), expr) >> Q.commutative(expr),
|
244 |
+
]
|
245 |
+
|
246 |
+
@class_fact_registry.register(Mul)
|
247 |
+
def _(expr):
|
248 |
+
# Implicitly assumes Mul has more than one arg
|
249 |
+
# Would be allargs(x, Q.prime(x) | Q.composite(x)) except 1 is composite
|
250 |
+
# More advanced prime assumptions will require inequalities, as 1 provides
|
251 |
+
# a corner case.
|
252 |
+
allargs_prime = allargs(x, Q.prime(x), expr)
|
253 |
+
return Implies(allargs_prime, ~Q.prime(expr))
|
254 |
+
|
255 |
+
@class_fact_registry.register(Mul)
|
256 |
+
def _(expr):
|
257 |
+
# General Case: Odd number of imaginary args implies mul is imaginary(To be implemented)
|
258 |
+
allargs_imag_or_real = allargs(x, Q.imaginary(x) | Q.real(x), expr)
|
259 |
+
onearg_imaginary = exactlyonearg(x, Q.imaginary(x), expr)
|
260 |
+
return Implies(allargs_imag_or_real, Implies(onearg_imaginary, Q.imaginary(expr)))
|
261 |
+
|
262 |
+
@class_fact_registry.register(Mul)
|
263 |
+
def _(expr):
|
264 |
+
allargs_real = allargs(x, Q.real(x), expr)
|
265 |
+
onearg_irrational = exactlyonearg(x, Q.irrational(x), expr)
|
266 |
+
return Implies(allargs_real, Implies(onearg_irrational, Q.irrational(expr)))
|
267 |
+
|
268 |
+
@class_fact_registry.register(Mul)
|
269 |
+
def _(expr):
|
270 |
+
# Including the integer qualification means we don't need to add any facts
|
271 |
+
# for odd, since the assumptions already know that every integer is
|
272 |
+
# exactly one of even or odd.
|
273 |
+
allargs_integer = allargs(x, Q.integer(x), expr)
|
274 |
+
anyarg_even = anyarg(x, Q.even(x), expr)
|
275 |
+
return Implies(allargs_integer, Equivalent(anyarg_even, Q.even(expr)))
|
276 |
+
|
277 |
+
|
278 |
+
### MatMul ###
|
279 |
+
|
280 |
+
@class_fact_registry.register(MatMul)
|
281 |
+
def _(expr):
|
282 |
+
allargs_square = allargs(x, Q.square(x), expr)
|
283 |
+
allargs_invertible = allargs(x, Q.invertible(x), expr)
|
284 |
+
return Implies(allargs_square, Equivalent(Q.invertible(expr), allargs_invertible))
|
285 |
+
|
286 |
+
|
287 |
+
### Pow ###
|
288 |
+
|
289 |
+
@class_fact_registry.multiregister(Pow)
|
290 |
+
def _(expr):
|
291 |
+
base, exp = expr.base, expr.exp
|
292 |
+
return [
|
293 |
+
(Q.real(base) & Q.even(exp) & Q.nonnegative(exp)) >> Q.nonnegative(expr),
|
294 |
+
(Q.nonnegative(base) & Q.odd(exp) & Q.nonnegative(exp)) >> Q.nonnegative(expr),
|
295 |
+
(Q.nonpositive(base) & Q.odd(exp) & Q.nonnegative(exp)) >> Q.nonpositive(expr),
|
296 |
+
Equivalent(Q.zero(expr), Q.zero(base) & Q.positive(exp))
|
297 |
+
]
|
298 |
+
|
299 |
+
|
300 |
+
### Numbers ###
|
301 |
+
|
302 |
+
_old_assump_getters = {
|
303 |
+
Q.positive: lambda o: o.is_positive,
|
304 |
+
Q.zero: lambda o: o.is_zero,
|
305 |
+
Q.negative: lambda o: o.is_negative,
|
306 |
+
Q.rational: lambda o: o.is_rational,
|
307 |
+
Q.irrational: lambda o: o.is_irrational,
|
308 |
+
Q.even: lambda o: o.is_even,
|
309 |
+
Q.odd: lambda o: o.is_odd,
|
310 |
+
Q.imaginary: lambda o: o.is_imaginary,
|
311 |
+
Q.prime: lambda o: o.is_prime,
|
312 |
+
Q.composite: lambda o: o.is_composite,
|
313 |
+
}
|
314 |
+
|
315 |
+
@class_fact_registry.multiregister(Number, NumberSymbol, ImaginaryUnit)
|
316 |
+
def _(expr):
|
317 |
+
ret = []
|
318 |
+
for p, getter in _old_assump_getters.items():
|
319 |
+
pred = p(expr)
|
320 |
+
prop = getter(expr)
|
321 |
+
if prop is not None:
|
322 |
+
ret.append(Equivalent(pred, prop))
|
323 |
+
return ret
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/tests/__init__.py
ADDED
File without changes
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/tests/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (188 Bytes). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/tests/__pycache__/test_assumptions_2.cpython-310.pyc
ADDED
Binary file (1.29 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/tests/__pycache__/test_context.cpython-310.pyc
ADDED
Binary file (1.62 kB). View file
|
|
env-llmeval/lib/python3.10/site-packages/sympy/assumptions/tests/__pycache__/test_matrices.cpython-310.pyc
ADDED
Binary file (11 kB). View file
|
|