Datasets:
Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/__pycache__/__init__.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/__pycache__/release.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/__init__.py +3 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/__pycache__/__init__.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/__pycache__/quaternion.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/quaternion.py +1666 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/tests/__init__.py +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/tests/test_quaternion.py +437 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__init__.py +18 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/__init__.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask_generated.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/assume.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/cnf.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/refine.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/ask.py +651 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/ask_generated.py +352 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/assume.py +485 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/cnf.py +445 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/facts.py +270 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/__init__.py +13 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/calculus.py +273 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/common.py +164 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/matrices.py +716 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/ntheory.py +279 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/order.py +440 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/sets.py +816 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/lra_satask.py +286 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/predicates/__init__.py +5 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/predicates/calculus.py +82 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/predicates/common.py +81 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/predicates/matrices.py +511 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/predicates/ntheory.py +126 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/predicates/order.py +390 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/predicates/sets.py +399 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/refine.py +405 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/relation/__init__.py +13 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/__init__.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/binrel.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/equality.cpython-310.pyc +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/relation/binrel.py +212 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/relation/equality.py +302 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/satask.py +369 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/sathandlers.py +322 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/__init__.py +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/test_assumptions_2.py +35 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/test_context.py +39 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/test_matrices.py +283 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/test_query.py +0 -0
- Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/test_refine.py +227 -0
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (28.4 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/__pycache__/release.cpython-310.pyc
ADDED
|
Binary file (237 Bytes). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .quaternion import Quaternion
|
| 2 |
+
|
| 3 |
+
__all__ = ["Quaternion",]
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (292 Bytes). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/__pycache__/quaternion.cpython-310.pyc
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core.numbers import Rational
|
| 2 |
+
from sympy.core.singleton import S
|
| 3 |
+
from sympy.core.relational import is_eq
|
| 4 |
+
from sympy.functions.elementary.complexes import (conjugate, im, re, sign)
|
| 5 |
+
from sympy.functions.elementary.exponential import (exp, log as ln)
|
| 6 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 7 |
+
from sympy.functions.elementary.trigonometric import (acos, asin, atan2)
|
| 8 |
+
from sympy.functions.elementary.trigonometric import (cos, sin)
|
| 9 |
+
from sympy.simplify.trigsimp import trigsimp
|
| 10 |
+
from sympy.integrals.integrals import integrate
|
| 11 |
+
from sympy.matrices.dense import MutableDenseMatrix as Matrix
|
| 12 |
+
from sympy.core.sympify import sympify, _sympify
|
| 13 |
+
from sympy.core.expr import Expr
|
| 14 |
+
from sympy.core.logic import fuzzy_not, fuzzy_or
|
| 15 |
+
from sympy.utilities.misc import as_int
|
| 16 |
+
|
| 17 |
+
from mpmath.libmp.libmpf import prec_to_dps
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def _check_norm(elements, norm):
|
| 21 |
+
"""validate if input norm is consistent"""
|
| 22 |
+
if norm is not None and norm.is_number:
|
| 23 |
+
if norm.is_positive is False:
|
| 24 |
+
raise ValueError("Input norm must be positive.")
|
| 25 |
+
|
| 26 |
+
numerical = all(i.is_number and i.is_real is True for i in elements)
|
| 27 |
+
if numerical and is_eq(norm**2, sum(i**2 for i in elements)) is False:
|
| 28 |
+
raise ValueError("Incompatible value for norm.")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def _is_extrinsic(seq):
|
| 32 |
+
"""validate seq and return True if seq is lowercase and False if uppercase"""
|
| 33 |
+
if type(seq) != str:
|
| 34 |
+
raise ValueError('Expected seq to be a string.')
|
| 35 |
+
if len(seq) != 3:
|
| 36 |
+
raise ValueError("Expected 3 axes, got `{}`.".format(seq))
|
| 37 |
+
|
| 38 |
+
intrinsic = seq.isupper()
|
| 39 |
+
extrinsic = seq.islower()
|
| 40 |
+
if not (intrinsic or extrinsic):
|
| 41 |
+
raise ValueError("seq must either be fully uppercase (for extrinsic "
|
| 42 |
+
"rotations), or fully lowercase, for intrinsic "
|
| 43 |
+
"rotations).")
|
| 44 |
+
|
| 45 |
+
i, j, k = seq.lower()
|
| 46 |
+
if (i == j) or (j == k):
|
| 47 |
+
raise ValueError("Consecutive axes must be different")
|
| 48 |
+
|
| 49 |
+
bad = set(seq) - set('xyzXYZ')
|
| 50 |
+
if bad:
|
| 51 |
+
raise ValueError("Expected axes from `seq` to be from "
|
| 52 |
+
"['x', 'y', 'z'] or ['X', 'Y', 'Z'], "
|
| 53 |
+
"got {}".format(''.join(bad)))
|
| 54 |
+
|
| 55 |
+
return extrinsic
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class Quaternion(Expr):
|
| 59 |
+
"""Provides basic quaternion operations.
|
| 60 |
+
Quaternion objects can be instantiated as ``Quaternion(a, b, c, d)``
|
| 61 |
+
as in $q = a + bi + cj + dk$.
|
| 62 |
+
|
| 63 |
+
Parameters
|
| 64 |
+
==========
|
| 65 |
+
|
| 66 |
+
norm : None or number
|
| 67 |
+
Pre-defined quaternion norm. If a value is given, Quaternion.norm
|
| 68 |
+
returns this pre-defined value instead of calculating the norm
|
| 69 |
+
|
| 70 |
+
Examples
|
| 71 |
+
========
|
| 72 |
+
|
| 73 |
+
>>> from sympy import Quaternion
|
| 74 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
| 75 |
+
>>> q
|
| 76 |
+
1 + 2*i + 3*j + 4*k
|
| 77 |
+
|
| 78 |
+
Quaternions over complex fields can be defined as:
|
| 79 |
+
|
| 80 |
+
>>> from sympy import Quaternion
|
| 81 |
+
>>> from sympy import symbols, I
|
| 82 |
+
>>> x = symbols('x')
|
| 83 |
+
>>> q1 = Quaternion(x, x**3, x, x**2, real_field = False)
|
| 84 |
+
>>> q2 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
| 85 |
+
>>> q1
|
| 86 |
+
x + x**3*i + x*j + x**2*k
|
| 87 |
+
>>> q2
|
| 88 |
+
(3 + 4*I) + (2 + 5*I)*i + 0*j + (7 + 8*I)*k
|
| 89 |
+
|
| 90 |
+
Defining symbolic unit quaternions:
|
| 91 |
+
|
| 92 |
+
>>> from sympy import Quaternion
|
| 93 |
+
>>> from sympy.abc import w, x, y, z
|
| 94 |
+
>>> q = Quaternion(w, x, y, z, norm=1)
|
| 95 |
+
>>> q
|
| 96 |
+
w + x*i + y*j + z*k
|
| 97 |
+
>>> q.norm()
|
| 98 |
+
1
|
| 99 |
+
|
| 100 |
+
References
|
| 101 |
+
==========
|
| 102 |
+
|
| 103 |
+
.. [1] https://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/
|
| 104 |
+
.. [2] https://en.wikipedia.org/wiki/Quaternion
|
| 105 |
+
|
| 106 |
+
"""
|
| 107 |
+
_op_priority = 11.0
|
| 108 |
+
|
| 109 |
+
is_commutative = False
|
| 110 |
+
|
| 111 |
+
def __new__(cls, a=0, b=0, c=0, d=0, real_field=True, norm=None):
|
| 112 |
+
a, b, c, d = map(sympify, (a, b, c, d))
|
| 113 |
+
|
| 114 |
+
if any(i.is_commutative is False for i in [a, b, c, d]):
|
| 115 |
+
raise ValueError("arguments have to be commutative")
|
| 116 |
+
obj = super().__new__(cls, a, b, c, d)
|
| 117 |
+
obj._real_field = real_field
|
| 118 |
+
obj.set_norm(norm)
|
| 119 |
+
return obj
|
| 120 |
+
|
| 121 |
+
def set_norm(self, norm):
|
| 122 |
+
"""Sets norm of an already instantiated quaternion.
|
| 123 |
+
|
| 124 |
+
Parameters
|
| 125 |
+
==========
|
| 126 |
+
|
| 127 |
+
norm : None or number
|
| 128 |
+
Pre-defined quaternion norm. If a value is given, Quaternion.norm
|
| 129 |
+
returns this pre-defined value instead of calculating the norm
|
| 130 |
+
|
| 131 |
+
Examples
|
| 132 |
+
========
|
| 133 |
+
|
| 134 |
+
>>> from sympy import Quaternion
|
| 135 |
+
>>> from sympy.abc import a, b, c, d
|
| 136 |
+
>>> q = Quaternion(a, b, c, d)
|
| 137 |
+
>>> q.norm()
|
| 138 |
+
sqrt(a**2 + b**2 + c**2 + d**2)
|
| 139 |
+
|
| 140 |
+
Setting the norm:
|
| 141 |
+
|
| 142 |
+
>>> q.set_norm(1)
|
| 143 |
+
>>> q.norm()
|
| 144 |
+
1
|
| 145 |
+
|
| 146 |
+
Removing set norm:
|
| 147 |
+
|
| 148 |
+
>>> q.set_norm(None)
|
| 149 |
+
>>> q.norm()
|
| 150 |
+
sqrt(a**2 + b**2 + c**2 + d**2)
|
| 151 |
+
|
| 152 |
+
"""
|
| 153 |
+
norm = sympify(norm)
|
| 154 |
+
_check_norm(self.args, norm)
|
| 155 |
+
self._norm = norm
|
| 156 |
+
|
| 157 |
+
@property
|
| 158 |
+
def a(self):
|
| 159 |
+
return self.args[0]
|
| 160 |
+
|
| 161 |
+
@property
|
| 162 |
+
def b(self):
|
| 163 |
+
return self.args[1]
|
| 164 |
+
|
| 165 |
+
@property
|
| 166 |
+
def c(self):
|
| 167 |
+
return self.args[2]
|
| 168 |
+
|
| 169 |
+
@property
|
| 170 |
+
def d(self):
|
| 171 |
+
return self.args[3]
|
| 172 |
+
|
| 173 |
+
@property
|
| 174 |
+
def real_field(self):
|
| 175 |
+
return self._real_field
|
| 176 |
+
|
| 177 |
+
@property
|
| 178 |
+
def product_matrix_left(self):
|
| 179 |
+
r"""Returns 4 x 4 Matrix equivalent to a Hamilton product from the
|
| 180 |
+
left. This can be useful when treating quaternion elements as column
|
| 181 |
+
vectors. Given a quaternion $q = a + bi + cj + dk$ where a, b, c and d
|
| 182 |
+
are real numbers, the product matrix from the left is:
|
| 183 |
+
|
| 184 |
+
.. math::
|
| 185 |
+
|
| 186 |
+
M = \begin{bmatrix} a &-b &-c &-d \\
|
| 187 |
+
b & a &-d & c \\
|
| 188 |
+
c & d & a &-b \\
|
| 189 |
+
d &-c & b & a \end{bmatrix}
|
| 190 |
+
|
| 191 |
+
Examples
|
| 192 |
+
========
|
| 193 |
+
|
| 194 |
+
>>> from sympy import Quaternion
|
| 195 |
+
>>> from sympy.abc import a, b, c, d
|
| 196 |
+
>>> q1 = Quaternion(1, 0, 0, 1)
|
| 197 |
+
>>> q2 = Quaternion(a, b, c, d)
|
| 198 |
+
>>> q1.product_matrix_left
|
| 199 |
+
Matrix([
|
| 200 |
+
[1, 0, 0, -1],
|
| 201 |
+
[0, 1, -1, 0],
|
| 202 |
+
[0, 1, 1, 0],
|
| 203 |
+
[1, 0, 0, 1]])
|
| 204 |
+
|
| 205 |
+
>>> q1.product_matrix_left * q2.to_Matrix()
|
| 206 |
+
Matrix([
|
| 207 |
+
[a - d],
|
| 208 |
+
[b - c],
|
| 209 |
+
[b + c],
|
| 210 |
+
[a + d]])
|
| 211 |
+
|
| 212 |
+
This is equivalent to:
|
| 213 |
+
|
| 214 |
+
>>> (q1 * q2).to_Matrix()
|
| 215 |
+
Matrix([
|
| 216 |
+
[a - d],
|
| 217 |
+
[b - c],
|
| 218 |
+
[b + c],
|
| 219 |
+
[a + d]])
|
| 220 |
+
"""
|
| 221 |
+
return Matrix([
|
| 222 |
+
[self.a, -self.b, -self.c, -self.d],
|
| 223 |
+
[self.b, self.a, -self.d, self.c],
|
| 224 |
+
[self.c, self.d, self.a, -self.b],
|
| 225 |
+
[self.d, -self.c, self.b, self.a]])
|
| 226 |
+
|
| 227 |
+
@property
|
| 228 |
+
def product_matrix_right(self):
|
| 229 |
+
r"""Returns 4 x 4 Matrix equivalent to a Hamilton product from the
|
| 230 |
+
right. This can be useful when treating quaternion elements as column
|
| 231 |
+
vectors. Given a quaternion $q = a + bi + cj + dk$ where a, b, c and d
|
| 232 |
+
are real numbers, the product matrix from the left is:
|
| 233 |
+
|
| 234 |
+
.. math::
|
| 235 |
+
|
| 236 |
+
M = \begin{bmatrix} a &-b &-c &-d \\
|
| 237 |
+
b & a & d &-c \\
|
| 238 |
+
c &-d & a & b \\
|
| 239 |
+
d & c &-b & a \end{bmatrix}
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
Examples
|
| 243 |
+
========
|
| 244 |
+
|
| 245 |
+
>>> from sympy import Quaternion
|
| 246 |
+
>>> from sympy.abc import a, b, c, d
|
| 247 |
+
>>> q1 = Quaternion(a, b, c, d)
|
| 248 |
+
>>> q2 = Quaternion(1, 0, 0, 1)
|
| 249 |
+
>>> q2.product_matrix_right
|
| 250 |
+
Matrix([
|
| 251 |
+
[1, 0, 0, -1],
|
| 252 |
+
[0, 1, 1, 0],
|
| 253 |
+
[0, -1, 1, 0],
|
| 254 |
+
[1, 0, 0, 1]])
|
| 255 |
+
|
| 256 |
+
Note the switched arguments: the matrix represents the quaternion on
|
| 257 |
+
the right, but is still considered as a matrix multiplication from the
|
| 258 |
+
left.
|
| 259 |
+
|
| 260 |
+
>>> q2.product_matrix_right * q1.to_Matrix()
|
| 261 |
+
Matrix([
|
| 262 |
+
[ a - d],
|
| 263 |
+
[ b + c],
|
| 264 |
+
[-b + c],
|
| 265 |
+
[ a + d]])
|
| 266 |
+
|
| 267 |
+
This is equivalent to:
|
| 268 |
+
|
| 269 |
+
>>> (q1 * q2).to_Matrix()
|
| 270 |
+
Matrix([
|
| 271 |
+
[ a - d],
|
| 272 |
+
[ b + c],
|
| 273 |
+
[-b + c],
|
| 274 |
+
[ a + d]])
|
| 275 |
+
"""
|
| 276 |
+
return Matrix([
|
| 277 |
+
[self.a, -self.b, -self.c, -self.d],
|
| 278 |
+
[self.b, self.a, self.d, -self.c],
|
| 279 |
+
[self.c, -self.d, self.a, self.b],
|
| 280 |
+
[self.d, self.c, -self.b, self.a]])
|
| 281 |
+
|
| 282 |
+
def to_Matrix(self, vector_only=False):
|
| 283 |
+
"""Returns elements of quaternion as a column vector.
|
| 284 |
+
By default, a ``Matrix`` of length 4 is returned, with the real part as the
|
| 285 |
+
first element.
|
| 286 |
+
If ``vector_only`` is ``True``, returns only imaginary part as a Matrix of
|
| 287 |
+
length 3.
|
| 288 |
+
|
| 289 |
+
Parameters
|
| 290 |
+
==========
|
| 291 |
+
|
| 292 |
+
vector_only : bool
|
| 293 |
+
If True, only imaginary part is returned.
|
| 294 |
+
Default value: False
|
| 295 |
+
|
| 296 |
+
Returns
|
| 297 |
+
=======
|
| 298 |
+
|
| 299 |
+
Matrix
|
| 300 |
+
A column vector constructed by the elements of the quaternion.
|
| 301 |
+
|
| 302 |
+
Examples
|
| 303 |
+
========
|
| 304 |
+
|
| 305 |
+
>>> from sympy import Quaternion
|
| 306 |
+
>>> from sympy.abc import a, b, c, d
|
| 307 |
+
>>> q = Quaternion(a, b, c, d)
|
| 308 |
+
>>> q
|
| 309 |
+
a + b*i + c*j + d*k
|
| 310 |
+
|
| 311 |
+
>>> q.to_Matrix()
|
| 312 |
+
Matrix([
|
| 313 |
+
[a],
|
| 314 |
+
[b],
|
| 315 |
+
[c],
|
| 316 |
+
[d]])
|
| 317 |
+
|
| 318 |
+
|
| 319 |
+
>>> q.to_Matrix(vector_only=True)
|
| 320 |
+
Matrix([
|
| 321 |
+
[b],
|
| 322 |
+
[c],
|
| 323 |
+
[d]])
|
| 324 |
+
|
| 325 |
+
"""
|
| 326 |
+
if vector_only:
|
| 327 |
+
return Matrix(self.args[1:])
|
| 328 |
+
else:
|
| 329 |
+
return Matrix(self.args)
|
| 330 |
+
|
| 331 |
+
@classmethod
|
| 332 |
+
def from_Matrix(cls, elements):
|
| 333 |
+
"""Returns quaternion from elements of a column vector`.
|
| 334 |
+
If vector_only is True, returns only imaginary part as a Matrix of
|
| 335 |
+
length 3.
|
| 336 |
+
|
| 337 |
+
Parameters
|
| 338 |
+
==========
|
| 339 |
+
|
| 340 |
+
elements : Matrix, list or tuple of length 3 or 4. If length is 3,
|
| 341 |
+
assume real part is zero.
|
| 342 |
+
Default value: False
|
| 343 |
+
|
| 344 |
+
Returns
|
| 345 |
+
=======
|
| 346 |
+
|
| 347 |
+
Quaternion
|
| 348 |
+
A quaternion created from the input elements.
|
| 349 |
+
|
| 350 |
+
Examples
|
| 351 |
+
========
|
| 352 |
+
|
| 353 |
+
>>> from sympy import Quaternion
|
| 354 |
+
>>> from sympy.abc import a, b, c, d
|
| 355 |
+
>>> q = Quaternion.from_Matrix([a, b, c, d])
|
| 356 |
+
>>> q
|
| 357 |
+
a + b*i + c*j + d*k
|
| 358 |
+
|
| 359 |
+
>>> q = Quaternion.from_Matrix([b, c, d])
|
| 360 |
+
>>> q
|
| 361 |
+
0 + b*i + c*j + d*k
|
| 362 |
+
|
| 363 |
+
"""
|
| 364 |
+
length = len(elements)
|
| 365 |
+
if length != 3 and length != 4:
|
| 366 |
+
raise ValueError("Input elements must have length 3 or 4, got {} "
|
| 367 |
+
"elements".format(length))
|
| 368 |
+
|
| 369 |
+
if length == 3:
|
| 370 |
+
return Quaternion(0, *elements)
|
| 371 |
+
else:
|
| 372 |
+
return Quaternion(*elements)
|
| 373 |
+
|
| 374 |
+
@classmethod
|
| 375 |
+
def from_euler(cls, angles, seq):
|
| 376 |
+
"""Returns quaternion equivalent to rotation represented by the Euler
|
| 377 |
+
angles, in the sequence defined by ``seq``.
|
| 378 |
+
|
| 379 |
+
Parameters
|
| 380 |
+
==========
|
| 381 |
+
|
| 382 |
+
angles : list, tuple or Matrix of 3 numbers
|
| 383 |
+
The Euler angles (in radians).
|
| 384 |
+
seq : string of length 3
|
| 385 |
+
Represents the sequence of rotations.
|
| 386 |
+
For extrinsic rotations, seq must be all lowercase and its elements
|
| 387 |
+
must be from the set ``{'x', 'y', 'z'}``
|
| 388 |
+
For intrinsic rotations, seq must be all uppercase and its elements
|
| 389 |
+
must be from the set ``{'X', 'Y', 'Z'}``
|
| 390 |
+
|
| 391 |
+
Returns
|
| 392 |
+
=======
|
| 393 |
+
|
| 394 |
+
Quaternion
|
| 395 |
+
The normalized rotation quaternion calculated from the Euler angles
|
| 396 |
+
in the given sequence.
|
| 397 |
+
|
| 398 |
+
Examples
|
| 399 |
+
========
|
| 400 |
+
|
| 401 |
+
>>> from sympy import Quaternion
|
| 402 |
+
>>> from sympy import pi
|
| 403 |
+
>>> q = Quaternion.from_euler([pi/2, 0, 0], 'xyz')
|
| 404 |
+
>>> q
|
| 405 |
+
sqrt(2)/2 + sqrt(2)/2*i + 0*j + 0*k
|
| 406 |
+
|
| 407 |
+
>>> q = Quaternion.from_euler([0, pi/2, pi] , 'zyz')
|
| 408 |
+
>>> q
|
| 409 |
+
0 + (-sqrt(2)/2)*i + 0*j + sqrt(2)/2*k
|
| 410 |
+
|
| 411 |
+
>>> q = Quaternion.from_euler([0, pi/2, pi] , 'ZYZ')
|
| 412 |
+
>>> q
|
| 413 |
+
0 + sqrt(2)/2*i + 0*j + sqrt(2)/2*k
|
| 414 |
+
|
| 415 |
+
"""
|
| 416 |
+
|
| 417 |
+
if len(angles) != 3:
|
| 418 |
+
raise ValueError("3 angles must be given.")
|
| 419 |
+
|
| 420 |
+
extrinsic = _is_extrinsic(seq)
|
| 421 |
+
i, j, k = seq.lower()
|
| 422 |
+
|
| 423 |
+
# get elementary basis vectors
|
| 424 |
+
ei = [1 if n == i else 0 for n in 'xyz']
|
| 425 |
+
ej = [1 if n == j else 0 for n in 'xyz']
|
| 426 |
+
ek = [1 if n == k else 0 for n in 'xyz']
|
| 427 |
+
|
| 428 |
+
# calculate distinct quaternions
|
| 429 |
+
qi = cls.from_axis_angle(ei, angles[0])
|
| 430 |
+
qj = cls.from_axis_angle(ej, angles[1])
|
| 431 |
+
qk = cls.from_axis_angle(ek, angles[2])
|
| 432 |
+
|
| 433 |
+
if extrinsic:
|
| 434 |
+
return trigsimp(qk * qj * qi)
|
| 435 |
+
else:
|
| 436 |
+
return trigsimp(qi * qj * qk)
|
| 437 |
+
|
| 438 |
+
def to_euler(self, seq, angle_addition=True, avoid_square_root=False):
|
| 439 |
+
r"""Returns Euler angles representing same rotation as the quaternion,
|
| 440 |
+
in the sequence given by ``seq``. This implements the method described
|
| 441 |
+
in [1]_.
|
| 442 |
+
|
| 443 |
+
For degenerate cases (gymbal lock cases), the third angle is
|
| 444 |
+
set to zero.
|
| 445 |
+
|
| 446 |
+
Parameters
|
| 447 |
+
==========
|
| 448 |
+
|
| 449 |
+
seq : string of length 3
|
| 450 |
+
Represents the sequence of rotations.
|
| 451 |
+
For extrinsic rotations, seq must be all lowercase and its elements
|
| 452 |
+
must be from the set ``{'x', 'y', 'z'}``
|
| 453 |
+
For intrinsic rotations, seq must be all uppercase and its elements
|
| 454 |
+
must be from the set ``{'X', 'Y', 'Z'}``
|
| 455 |
+
|
| 456 |
+
angle_addition : bool
|
| 457 |
+
When True, first and third angles are given as an addition and
|
| 458 |
+
subtraction of two simpler ``atan2`` expressions. When False, the
|
| 459 |
+
first and third angles are each given by a single more complicated
|
| 460 |
+
``atan2`` expression. This equivalent expression is given by:
|
| 461 |
+
|
| 462 |
+
.. math::
|
| 463 |
+
|
| 464 |
+
\operatorname{atan_2} (b,a) \pm \operatorname{atan_2} (d,c) =
|
| 465 |
+
\operatorname{atan_2} (bc\pm ad, ac\mp bd)
|
| 466 |
+
|
| 467 |
+
Default value: True
|
| 468 |
+
|
| 469 |
+
avoid_square_root : bool
|
| 470 |
+
When True, the second angle is calculated with an expression based
|
| 471 |
+
on ``acos``, which is slightly more complicated but avoids a square
|
| 472 |
+
root. When False, second angle is calculated with ``atan2``, which
|
| 473 |
+
is simpler and can be better for numerical reasons (some
|
| 474 |
+
numerical implementations of ``acos`` have problems near zero).
|
| 475 |
+
Default value: False
|
| 476 |
+
|
| 477 |
+
|
| 478 |
+
Returns
|
| 479 |
+
=======
|
| 480 |
+
|
| 481 |
+
Tuple
|
| 482 |
+
The Euler angles calculated from the quaternion
|
| 483 |
+
|
| 484 |
+
Examples
|
| 485 |
+
========
|
| 486 |
+
|
| 487 |
+
>>> from sympy import Quaternion
|
| 488 |
+
>>> from sympy.abc import a, b, c, d
|
| 489 |
+
>>> euler = Quaternion(a, b, c, d).to_euler('zyz')
|
| 490 |
+
>>> euler
|
| 491 |
+
(-atan2(-b, c) + atan2(d, a),
|
| 492 |
+
2*atan2(sqrt(b**2 + c**2), sqrt(a**2 + d**2)),
|
| 493 |
+
atan2(-b, c) + atan2(d, a))
|
| 494 |
+
|
| 495 |
+
|
| 496 |
+
References
|
| 497 |
+
==========
|
| 498 |
+
|
| 499 |
+
.. [1] https://doi.org/10.1371/journal.pone.0276302
|
| 500 |
+
|
| 501 |
+
"""
|
| 502 |
+
if self.is_zero_quaternion():
|
| 503 |
+
raise ValueError('Cannot convert a quaternion with norm 0.')
|
| 504 |
+
|
| 505 |
+
angles = [0, 0, 0]
|
| 506 |
+
|
| 507 |
+
extrinsic = _is_extrinsic(seq)
|
| 508 |
+
i, j, k = seq.lower()
|
| 509 |
+
|
| 510 |
+
# get index corresponding to elementary basis vectors
|
| 511 |
+
i = 'xyz'.index(i) + 1
|
| 512 |
+
j = 'xyz'.index(j) + 1
|
| 513 |
+
k = 'xyz'.index(k) + 1
|
| 514 |
+
|
| 515 |
+
if not extrinsic:
|
| 516 |
+
i, k = k, i
|
| 517 |
+
|
| 518 |
+
# check if sequence is symmetric
|
| 519 |
+
symmetric = i == k
|
| 520 |
+
if symmetric:
|
| 521 |
+
k = 6 - i - j
|
| 522 |
+
|
| 523 |
+
# parity of the permutation
|
| 524 |
+
sign = (i - j) * (j - k) * (k - i) // 2
|
| 525 |
+
|
| 526 |
+
# permutate elements
|
| 527 |
+
elements = [self.a, self.b, self.c, self.d]
|
| 528 |
+
a = elements[0]
|
| 529 |
+
b = elements[i]
|
| 530 |
+
c = elements[j]
|
| 531 |
+
d = elements[k] * sign
|
| 532 |
+
|
| 533 |
+
if not symmetric:
|
| 534 |
+
a, b, c, d = a - c, b + d, c + a, d - b
|
| 535 |
+
|
| 536 |
+
if avoid_square_root:
|
| 537 |
+
if symmetric:
|
| 538 |
+
n2 = self.norm()**2
|
| 539 |
+
angles[1] = acos((a * a + b * b - c * c - d * d) / n2)
|
| 540 |
+
else:
|
| 541 |
+
n2 = 2 * self.norm()**2
|
| 542 |
+
angles[1] = asin((c * c + d * d - a * a - b * b) / n2)
|
| 543 |
+
else:
|
| 544 |
+
angles[1] = 2 * atan2(sqrt(c * c + d * d), sqrt(a * a + b * b))
|
| 545 |
+
if not symmetric:
|
| 546 |
+
angles[1] -= S.Pi / 2
|
| 547 |
+
|
| 548 |
+
# Check for singularities in numerical cases
|
| 549 |
+
case = 0
|
| 550 |
+
if is_eq(c, S.Zero) and is_eq(d, S.Zero):
|
| 551 |
+
case = 1
|
| 552 |
+
if is_eq(a, S.Zero) and is_eq(b, S.Zero):
|
| 553 |
+
case = 2
|
| 554 |
+
|
| 555 |
+
if case == 0:
|
| 556 |
+
if angle_addition:
|
| 557 |
+
angles[0] = atan2(b, a) + atan2(d, c)
|
| 558 |
+
angles[2] = atan2(b, a) - atan2(d, c)
|
| 559 |
+
else:
|
| 560 |
+
angles[0] = atan2(b*c + a*d, a*c - b*d)
|
| 561 |
+
angles[2] = atan2(b*c - a*d, a*c + b*d)
|
| 562 |
+
|
| 563 |
+
else: # any degenerate case
|
| 564 |
+
angles[2 * (not extrinsic)] = S.Zero
|
| 565 |
+
if case == 1:
|
| 566 |
+
angles[2 * extrinsic] = 2 * atan2(b, a)
|
| 567 |
+
else:
|
| 568 |
+
angles[2 * extrinsic] = 2 * atan2(d, c)
|
| 569 |
+
angles[2 * extrinsic] *= (-1 if extrinsic else 1)
|
| 570 |
+
|
| 571 |
+
# for Tait-Bryan angles
|
| 572 |
+
if not symmetric:
|
| 573 |
+
angles[0] *= sign
|
| 574 |
+
|
| 575 |
+
if extrinsic:
|
| 576 |
+
return tuple(angles[::-1])
|
| 577 |
+
else:
|
| 578 |
+
return tuple(angles)
|
| 579 |
+
|
| 580 |
+
@classmethod
|
| 581 |
+
def from_axis_angle(cls, vector, angle):
|
| 582 |
+
"""Returns a rotation quaternion given the axis and the angle of rotation.
|
| 583 |
+
|
| 584 |
+
Parameters
|
| 585 |
+
==========
|
| 586 |
+
|
| 587 |
+
vector : tuple of three numbers
|
| 588 |
+
The vector representation of the given axis.
|
| 589 |
+
angle : number
|
| 590 |
+
The angle by which axis is rotated (in radians).
|
| 591 |
+
|
| 592 |
+
Returns
|
| 593 |
+
=======
|
| 594 |
+
|
| 595 |
+
Quaternion
|
| 596 |
+
The normalized rotation quaternion calculated from the given axis and the angle of rotation.
|
| 597 |
+
|
| 598 |
+
Examples
|
| 599 |
+
========
|
| 600 |
+
|
| 601 |
+
>>> from sympy import Quaternion
|
| 602 |
+
>>> from sympy import pi, sqrt
|
| 603 |
+
>>> q = Quaternion.from_axis_angle((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3), 2*pi/3)
|
| 604 |
+
>>> q
|
| 605 |
+
1/2 + 1/2*i + 1/2*j + 1/2*k
|
| 606 |
+
|
| 607 |
+
"""
|
| 608 |
+
(x, y, z) = vector
|
| 609 |
+
norm = sqrt(x**2 + y**2 + z**2)
|
| 610 |
+
(x, y, z) = (x / norm, y / norm, z / norm)
|
| 611 |
+
s = sin(angle * S.Half)
|
| 612 |
+
a = cos(angle * S.Half)
|
| 613 |
+
b = x * s
|
| 614 |
+
c = y * s
|
| 615 |
+
d = z * s
|
| 616 |
+
|
| 617 |
+
# note that this quaternion is already normalized by construction:
|
| 618 |
+
# c^2 + (s*x)^2 + (s*y)^2 + (s*z)^2 = c^2 + s^2*(x^2 + y^2 + z^2) = c^2 + s^2 * 1 = c^2 + s^2 = 1
|
| 619 |
+
# so, what we return is a normalized quaternion
|
| 620 |
+
|
| 621 |
+
return cls(a, b, c, d)
|
| 622 |
+
|
| 623 |
+
@classmethod
|
| 624 |
+
def from_rotation_matrix(cls, M):
|
| 625 |
+
"""Returns the equivalent quaternion of a matrix. The quaternion will be normalized
|
| 626 |
+
only if the matrix is special orthogonal (orthogonal and det(M) = 1).
|
| 627 |
+
|
| 628 |
+
Parameters
|
| 629 |
+
==========
|
| 630 |
+
|
| 631 |
+
M : Matrix
|
| 632 |
+
Input matrix to be converted to equivalent quaternion. M must be special
|
| 633 |
+
orthogonal (orthogonal and det(M) = 1) for the quaternion to be normalized.
|
| 634 |
+
|
| 635 |
+
Returns
|
| 636 |
+
=======
|
| 637 |
+
|
| 638 |
+
Quaternion
|
| 639 |
+
The quaternion equivalent to given matrix.
|
| 640 |
+
|
| 641 |
+
Examples
|
| 642 |
+
========
|
| 643 |
+
|
| 644 |
+
>>> from sympy import Quaternion
|
| 645 |
+
>>> from sympy import Matrix, symbols, cos, sin, trigsimp
|
| 646 |
+
>>> x = symbols('x')
|
| 647 |
+
>>> M = Matrix([[cos(x), -sin(x), 0], [sin(x), cos(x), 0], [0, 0, 1]])
|
| 648 |
+
>>> q = trigsimp(Quaternion.from_rotation_matrix(M))
|
| 649 |
+
>>> q
|
| 650 |
+
sqrt(2)*sqrt(cos(x) + 1)/2 + 0*i + 0*j + sqrt(2 - 2*cos(x))*sign(sin(x))/2*k
|
| 651 |
+
|
| 652 |
+
"""
|
| 653 |
+
|
| 654 |
+
absQ = M.det()**Rational(1, 3)
|
| 655 |
+
|
| 656 |
+
a = sqrt(absQ + M[0, 0] + M[1, 1] + M[2, 2]) / 2
|
| 657 |
+
b = sqrt(absQ + M[0, 0] - M[1, 1] - M[2, 2]) / 2
|
| 658 |
+
c = sqrt(absQ - M[0, 0] + M[1, 1] - M[2, 2]) / 2
|
| 659 |
+
d = sqrt(absQ - M[0, 0] - M[1, 1] + M[2, 2]) / 2
|
| 660 |
+
|
| 661 |
+
b = b * sign(M[2, 1] - M[1, 2])
|
| 662 |
+
c = c * sign(M[0, 2] - M[2, 0])
|
| 663 |
+
d = d * sign(M[1, 0] - M[0, 1])
|
| 664 |
+
|
| 665 |
+
return Quaternion(a, b, c, d)
|
| 666 |
+
|
| 667 |
+
def __add__(self, other):
|
| 668 |
+
return self.add(other)
|
| 669 |
+
|
| 670 |
+
def __radd__(self, other):
|
| 671 |
+
return self.add(other)
|
| 672 |
+
|
| 673 |
+
def __sub__(self, other):
|
| 674 |
+
return self.add(other*-1)
|
| 675 |
+
|
| 676 |
+
def __mul__(self, other):
|
| 677 |
+
return self._generic_mul(self, _sympify(other))
|
| 678 |
+
|
| 679 |
+
def __rmul__(self, other):
|
| 680 |
+
return self._generic_mul(_sympify(other), self)
|
| 681 |
+
|
| 682 |
+
def __pow__(self, p):
|
| 683 |
+
return self.pow(p)
|
| 684 |
+
|
| 685 |
+
def __neg__(self):
|
| 686 |
+
return Quaternion(-self.a, -self.b, -self.c, -self.d)
|
| 687 |
+
|
| 688 |
+
def __truediv__(self, other):
|
| 689 |
+
return self * sympify(other)**-1
|
| 690 |
+
|
| 691 |
+
def __rtruediv__(self, other):
|
| 692 |
+
return sympify(other) * self**-1
|
| 693 |
+
|
| 694 |
+
def _eval_Integral(self, *args):
|
| 695 |
+
return self.integrate(*args)
|
| 696 |
+
|
| 697 |
+
def diff(self, *symbols, **kwargs):
|
| 698 |
+
kwargs.setdefault('evaluate', True)
|
| 699 |
+
return self.func(*[a.diff(*symbols, **kwargs) for a in self.args])
|
| 700 |
+
|
| 701 |
+
def add(self, other):
|
| 702 |
+
"""Adds quaternions.
|
| 703 |
+
|
| 704 |
+
Parameters
|
| 705 |
+
==========
|
| 706 |
+
|
| 707 |
+
other : Quaternion
|
| 708 |
+
The quaternion to add to current (self) quaternion.
|
| 709 |
+
|
| 710 |
+
Returns
|
| 711 |
+
=======
|
| 712 |
+
|
| 713 |
+
Quaternion
|
| 714 |
+
The resultant quaternion after adding self to other
|
| 715 |
+
|
| 716 |
+
Examples
|
| 717 |
+
========
|
| 718 |
+
|
| 719 |
+
>>> from sympy import Quaternion
|
| 720 |
+
>>> from sympy import symbols
|
| 721 |
+
>>> q1 = Quaternion(1, 2, 3, 4)
|
| 722 |
+
>>> q2 = Quaternion(5, 6, 7, 8)
|
| 723 |
+
>>> q1.add(q2)
|
| 724 |
+
6 + 8*i + 10*j + 12*k
|
| 725 |
+
>>> q1 + 5
|
| 726 |
+
6 + 2*i + 3*j + 4*k
|
| 727 |
+
>>> x = symbols('x', real = True)
|
| 728 |
+
>>> q1.add(x)
|
| 729 |
+
(x + 1) + 2*i + 3*j + 4*k
|
| 730 |
+
|
| 731 |
+
Quaternions over complex fields :
|
| 732 |
+
|
| 733 |
+
>>> from sympy import Quaternion
|
| 734 |
+
>>> from sympy import I
|
| 735 |
+
>>> q3 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
| 736 |
+
>>> q3.add(2 + 3*I)
|
| 737 |
+
(5 + 7*I) + (2 + 5*I)*i + 0*j + (7 + 8*I)*k
|
| 738 |
+
|
| 739 |
+
"""
|
| 740 |
+
q1 = self
|
| 741 |
+
q2 = sympify(other)
|
| 742 |
+
|
| 743 |
+
# If q2 is a number or a SymPy expression instead of a quaternion
|
| 744 |
+
if not isinstance(q2, Quaternion):
|
| 745 |
+
if q1.real_field and q2.is_complex:
|
| 746 |
+
return Quaternion(re(q2) + q1.a, im(q2) + q1.b, q1.c, q1.d)
|
| 747 |
+
elif q2.is_commutative:
|
| 748 |
+
return Quaternion(q1.a + q2, q1.b, q1.c, q1.d)
|
| 749 |
+
else:
|
| 750 |
+
raise ValueError("Only commutative expressions can be added with a Quaternion.")
|
| 751 |
+
|
| 752 |
+
return Quaternion(q1.a + q2.a, q1.b + q2.b, q1.c + q2.c, q1.d
|
| 753 |
+
+ q2.d)
|
| 754 |
+
|
| 755 |
+
def mul(self, other):
|
| 756 |
+
"""Multiplies quaternions.
|
| 757 |
+
|
| 758 |
+
Parameters
|
| 759 |
+
==========
|
| 760 |
+
|
| 761 |
+
other : Quaternion or symbol
|
| 762 |
+
The quaternion to multiply to current (self) quaternion.
|
| 763 |
+
|
| 764 |
+
Returns
|
| 765 |
+
=======
|
| 766 |
+
|
| 767 |
+
Quaternion
|
| 768 |
+
The resultant quaternion after multiplying self with other
|
| 769 |
+
|
| 770 |
+
Examples
|
| 771 |
+
========
|
| 772 |
+
|
| 773 |
+
>>> from sympy import Quaternion
|
| 774 |
+
>>> from sympy import symbols
|
| 775 |
+
>>> q1 = Quaternion(1, 2, 3, 4)
|
| 776 |
+
>>> q2 = Quaternion(5, 6, 7, 8)
|
| 777 |
+
>>> q1.mul(q2)
|
| 778 |
+
(-60) + 12*i + 30*j + 24*k
|
| 779 |
+
>>> q1.mul(2)
|
| 780 |
+
2 + 4*i + 6*j + 8*k
|
| 781 |
+
>>> x = symbols('x', real = True)
|
| 782 |
+
>>> q1.mul(x)
|
| 783 |
+
x + 2*x*i + 3*x*j + 4*x*k
|
| 784 |
+
|
| 785 |
+
Quaternions over complex fields :
|
| 786 |
+
|
| 787 |
+
>>> from sympy import Quaternion
|
| 788 |
+
>>> from sympy import I
|
| 789 |
+
>>> q3 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
| 790 |
+
>>> q3.mul(2 + 3*I)
|
| 791 |
+
(2 + 3*I)*(3 + 4*I) + (2 + 3*I)*(2 + 5*I)*i + 0*j + (2 + 3*I)*(7 + 8*I)*k
|
| 792 |
+
|
| 793 |
+
"""
|
| 794 |
+
return self._generic_mul(self, _sympify(other))
|
| 795 |
+
|
| 796 |
+
@staticmethod
|
| 797 |
+
def _generic_mul(q1, q2):
|
| 798 |
+
"""Generic multiplication.
|
| 799 |
+
|
| 800 |
+
Parameters
|
| 801 |
+
==========
|
| 802 |
+
|
| 803 |
+
q1 : Quaternion or symbol
|
| 804 |
+
q2 : Quaternion or symbol
|
| 805 |
+
|
| 806 |
+
It is important to note that if neither q1 nor q2 is a Quaternion,
|
| 807 |
+
this function simply returns q1 * q2.
|
| 808 |
+
|
| 809 |
+
Returns
|
| 810 |
+
=======
|
| 811 |
+
|
| 812 |
+
Quaternion
|
| 813 |
+
The resultant quaternion after multiplying q1 and q2
|
| 814 |
+
|
| 815 |
+
Examples
|
| 816 |
+
========
|
| 817 |
+
|
| 818 |
+
>>> from sympy import Quaternion
|
| 819 |
+
>>> from sympy import Symbol, S
|
| 820 |
+
>>> q1 = Quaternion(1, 2, 3, 4)
|
| 821 |
+
>>> q2 = Quaternion(5, 6, 7, 8)
|
| 822 |
+
>>> Quaternion._generic_mul(q1, q2)
|
| 823 |
+
(-60) + 12*i + 30*j + 24*k
|
| 824 |
+
>>> Quaternion._generic_mul(q1, S(2))
|
| 825 |
+
2 + 4*i + 6*j + 8*k
|
| 826 |
+
>>> x = Symbol('x', real = True)
|
| 827 |
+
>>> Quaternion._generic_mul(q1, x)
|
| 828 |
+
x + 2*x*i + 3*x*j + 4*x*k
|
| 829 |
+
|
| 830 |
+
Quaternions over complex fields :
|
| 831 |
+
|
| 832 |
+
>>> from sympy import I
|
| 833 |
+
>>> q3 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
| 834 |
+
>>> Quaternion._generic_mul(q3, 2 + 3*I)
|
| 835 |
+
(2 + 3*I)*(3 + 4*I) + (2 + 3*I)*(2 + 5*I)*i + 0*j + (2 + 3*I)*(7 + 8*I)*k
|
| 836 |
+
|
| 837 |
+
"""
|
| 838 |
+
# None is a Quaternion:
|
| 839 |
+
if not isinstance(q1, Quaternion) and not isinstance(q2, Quaternion):
|
| 840 |
+
return q1 * q2
|
| 841 |
+
|
| 842 |
+
# If q1 is a number or a SymPy expression instead of a quaternion
|
| 843 |
+
if not isinstance(q1, Quaternion):
|
| 844 |
+
if q2.real_field and q1.is_complex:
|
| 845 |
+
return Quaternion(re(q1), im(q1), 0, 0) * q2
|
| 846 |
+
elif q1.is_commutative:
|
| 847 |
+
return Quaternion(q1 * q2.a, q1 * q2.b, q1 * q2.c, q1 * q2.d)
|
| 848 |
+
else:
|
| 849 |
+
raise ValueError("Only commutative expressions can be multiplied with a Quaternion.")
|
| 850 |
+
|
| 851 |
+
# If q2 is a number or a SymPy expression instead of a quaternion
|
| 852 |
+
if not isinstance(q2, Quaternion):
|
| 853 |
+
if q1.real_field and q2.is_complex:
|
| 854 |
+
return q1 * Quaternion(re(q2), im(q2), 0, 0)
|
| 855 |
+
elif q2.is_commutative:
|
| 856 |
+
return Quaternion(q2 * q1.a, q2 * q1.b, q2 * q1.c, q2 * q1.d)
|
| 857 |
+
else:
|
| 858 |
+
raise ValueError("Only commutative expressions can be multiplied with a Quaternion.")
|
| 859 |
+
|
| 860 |
+
# If any of the quaternions has a fixed norm, pre-compute norm
|
| 861 |
+
if q1._norm is None and q2._norm is None:
|
| 862 |
+
norm = None
|
| 863 |
+
else:
|
| 864 |
+
norm = q1.norm() * q2.norm()
|
| 865 |
+
|
| 866 |
+
return Quaternion(-q1.b*q2.b - q1.c*q2.c - q1.d*q2.d + q1.a*q2.a,
|
| 867 |
+
q1.b*q2.a + q1.c*q2.d - q1.d*q2.c + q1.a*q2.b,
|
| 868 |
+
-q1.b*q2.d + q1.c*q2.a + q1.d*q2.b + q1.a*q2.c,
|
| 869 |
+
q1.b*q2.c - q1.c*q2.b + q1.d*q2.a + q1.a * q2.d,
|
| 870 |
+
norm=norm)
|
| 871 |
+
|
| 872 |
+
def _eval_conjugate(self):
|
| 873 |
+
"""Returns the conjugate of the quaternion."""
|
| 874 |
+
q = self
|
| 875 |
+
return Quaternion(q.a, -q.b, -q.c, -q.d, norm=q._norm)
|
| 876 |
+
|
| 877 |
+
def norm(self):
|
| 878 |
+
"""Returns the norm of the quaternion."""
|
| 879 |
+
if self._norm is None: # check if norm is pre-defined
|
| 880 |
+
q = self
|
| 881 |
+
# trigsimp is used to simplify sin(x)^2 + cos(x)^2 (these terms
|
| 882 |
+
# arise when from_axis_angle is used).
|
| 883 |
+
return sqrt(trigsimp(q.a**2 + q.b**2 + q.c**2 + q.d**2))
|
| 884 |
+
|
| 885 |
+
return self._norm
|
| 886 |
+
|
| 887 |
+
def normalize(self):
|
| 888 |
+
"""Returns the normalized form of the quaternion."""
|
| 889 |
+
q = self
|
| 890 |
+
return q * (1/q.norm())
|
| 891 |
+
|
| 892 |
+
def inverse(self):
|
| 893 |
+
"""Returns the inverse of the quaternion."""
|
| 894 |
+
q = self
|
| 895 |
+
if not q.norm():
|
| 896 |
+
raise ValueError("Cannot compute inverse for a quaternion with zero norm")
|
| 897 |
+
return conjugate(q) * (1/q.norm()**2)
|
| 898 |
+
|
| 899 |
+
def pow(self, p):
|
| 900 |
+
"""Finds the pth power of the quaternion.
|
| 901 |
+
|
| 902 |
+
Parameters
|
| 903 |
+
==========
|
| 904 |
+
|
| 905 |
+
p : int
|
| 906 |
+
Power to be applied on quaternion.
|
| 907 |
+
|
| 908 |
+
Returns
|
| 909 |
+
=======
|
| 910 |
+
|
| 911 |
+
Quaternion
|
| 912 |
+
Returns the p-th power of the current quaternion.
|
| 913 |
+
Returns the inverse if p = -1.
|
| 914 |
+
|
| 915 |
+
Examples
|
| 916 |
+
========
|
| 917 |
+
|
| 918 |
+
>>> from sympy import Quaternion
|
| 919 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
| 920 |
+
>>> q.pow(4)
|
| 921 |
+
668 + (-224)*i + (-336)*j + (-448)*k
|
| 922 |
+
|
| 923 |
+
"""
|
| 924 |
+
try:
|
| 925 |
+
q, p = self, as_int(p)
|
| 926 |
+
except ValueError:
|
| 927 |
+
return NotImplemented
|
| 928 |
+
|
| 929 |
+
if p < 0:
|
| 930 |
+
q, p = q.inverse(), -p
|
| 931 |
+
|
| 932 |
+
if p == 1:
|
| 933 |
+
return q
|
| 934 |
+
|
| 935 |
+
res = Quaternion(1, 0, 0, 0)
|
| 936 |
+
while p > 0:
|
| 937 |
+
if p & 1:
|
| 938 |
+
res *= q
|
| 939 |
+
q *= q
|
| 940 |
+
p >>= 1
|
| 941 |
+
|
| 942 |
+
return res
|
| 943 |
+
|
| 944 |
+
def exp(self):
|
| 945 |
+
"""Returns the exponential of $q$, given by $e^q$.
|
| 946 |
+
|
| 947 |
+
Returns
|
| 948 |
+
=======
|
| 949 |
+
|
| 950 |
+
Quaternion
|
| 951 |
+
The exponential of the quaternion.
|
| 952 |
+
|
| 953 |
+
Examples
|
| 954 |
+
========
|
| 955 |
+
|
| 956 |
+
>>> from sympy import Quaternion
|
| 957 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
| 958 |
+
>>> q.exp()
|
| 959 |
+
E*cos(sqrt(29))
|
| 960 |
+
+ 2*sqrt(29)*E*sin(sqrt(29))/29*i
|
| 961 |
+
+ 3*sqrt(29)*E*sin(sqrt(29))/29*j
|
| 962 |
+
+ 4*sqrt(29)*E*sin(sqrt(29))/29*k
|
| 963 |
+
|
| 964 |
+
"""
|
| 965 |
+
# exp(q) = e^a(cos||v|| + v/||v||*sin||v||)
|
| 966 |
+
q = self
|
| 967 |
+
vector_norm = sqrt(q.b**2 + q.c**2 + q.d**2)
|
| 968 |
+
a = exp(q.a) * cos(vector_norm)
|
| 969 |
+
b = exp(q.a) * sin(vector_norm) * q.b / vector_norm
|
| 970 |
+
c = exp(q.a) * sin(vector_norm) * q.c / vector_norm
|
| 971 |
+
d = exp(q.a) * sin(vector_norm) * q.d / vector_norm
|
| 972 |
+
|
| 973 |
+
return Quaternion(a, b, c, d)
|
| 974 |
+
|
| 975 |
+
def log(self):
|
| 976 |
+
r"""Returns the logarithm of the quaternion, given by $\log q$.
|
| 977 |
+
|
| 978 |
+
Examples
|
| 979 |
+
========
|
| 980 |
+
|
| 981 |
+
>>> from sympy import Quaternion
|
| 982 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
| 983 |
+
>>> q.log()
|
| 984 |
+
log(sqrt(30))
|
| 985 |
+
+ 2*sqrt(29)*acos(sqrt(30)/30)/29*i
|
| 986 |
+
+ 3*sqrt(29)*acos(sqrt(30)/30)/29*j
|
| 987 |
+
+ 4*sqrt(29)*acos(sqrt(30)/30)/29*k
|
| 988 |
+
|
| 989 |
+
"""
|
| 990 |
+
# log(q) = log||q|| + v/||v||*arccos(a/||q||)
|
| 991 |
+
q = self
|
| 992 |
+
vector_norm = sqrt(q.b**2 + q.c**2 + q.d**2)
|
| 993 |
+
q_norm = q.norm()
|
| 994 |
+
a = ln(q_norm)
|
| 995 |
+
b = q.b * acos(q.a / q_norm) / vector_norm
|
| 996 |
+
c = q.c * acos(q.a / q_norm) / vector_norm
|
| 997 |
+
d = q.d * acos(q.a / q_norm) / vector_norm
|
| 998 |
+
|
| 999 |
+
return Quaternion(a, b, c, d)
|
| 1000 |
+
|
| 1001 |
+
def _eval_subs(self, *args):
|
| 1002 |
+
elements = [i.subs(*args) for i in self.args]
|
| 1003 |
+
norm = self._norm
|
| 1004 |
+
if norm is not None:
|
| 1005 |
+
norm = norm.subs(*args)
|
| 1006 |
+
_check_norm(elements, norm)
|
| 1007 |
+
return Quaternion(*elements, norm=norm)
|
| 1008 |
+
|
| 1009 |
+
def _eval_evalf(self, prec):
|
| 1010 |
+
"""Returns the floating point approximations (decimal numbers) of the quaternion.
|
| 1011 |
+
|
| 1012 |
+
Returns
|
| 1013 |
+
=======
|
| 1014 |
+
|
| 1015 |
+
Quaternion
|
| 1016 |
+
Floating point approximations of quaternion(self)
|
| 1017 |
+
|
| 1018 |
+
Examples
|
| 1019 |
+
========
|
| 1020 |
+
|
| 1021 |
+
>>> from sympy import Quaternion
|
| 1022 |
+
>>> from sympy import sqrt
|
| 1023 |
+
>>> q = Quaternion(1/sqrt(1), 1/sqrt(2), 1/sqrt(3), 1/sqrt(4))
|
| 1024 |
+
>>> q.evalf()
|
| 1025 |
+
1.00000000000000
|
| 1026 |
+
+ 0.707106781186547*i
|
| 1027 |
+
+ 0.577350269189626*j
|
| 1028 |
+
+ 0.500000000000000*k
|
| 1029 |
+
|
| 1030 |
+
"""
|
| 1031 |
+
nprec = prec_to_dps(prec)
|
| 1032 |
+
return Quaternion(*[arg.evalf(n=nprec) for arg in self.args])
|
| 1033 |
+
|
| 1034 |
+
def pow_cos_sin(self, p):
|
| 1035 |
+
"""Computes the pth power in the cos-sin form.
|
| 1036 |
+
|
| 1037 |
+
Parameters
|
| 1038 |
+
==========
|
| 1039 |
+
|
| 1040 |
+
p : int
|
| 1041 |
+
Power to be applied on quaternion.
|
| 1042 |
+
|
| 1043 |
+
Returns
|
| 1044 |
+
=======
|
| 1045 |
+
|
| 1046 |
+
Quaternion
|
| 1047 |
+
The p-th power in the cos-sin form.
|
| 1048 |
+
|
| 1049 |
+
Examples
|
| 1050 |
+
========
|
| 1051 |
+
|
| 1052 |
+
>>> from sympy import Quaternion
|
| 1053 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
| 1054 |
+
>>> q.pow_cos_sin(4)
|
| 1055 |
+
900*cos(4*acos(sqrt(30)/30))
|
| 1056 |
+
+ 1800*sqrt(29)*sin(4*acos(sqrt(30)/30))/29*i
|
| 1057 |
+
+ 2700*sqrt(29)*sin(4*acos(sqrt(30)/30))/29*j
|
| 1058 |
+
+ 3600*sqrt(29)*sin(4*acos(sqrt(30)/30))/29*k
|
| 1059 |
+
|
| 1060 |
+
"""
|
| 1061 |
+
# q = ||q||*(cos(a) + u*sin(a))
|
| 1062 |
+
# q^p = ||q||^p * (cos(p*a) + u*sin(p*a))
|
| 1063 |
+
|
| 1064 |
+
q = self
|
| 1065 |
+
(v, angle) = q.to_axis_angle()
|
| 1066 |
+
q2 = Quaternion.from_axis_angle(v, p * angle)
|
| 1067 |
+
return q2 * (q.norm()**p)
|
| 1068 |
+
|
| 1069 |
+
def integrate(self, *args):
|
| 1070 |
+
"""Computes integration of quaternion.
|
| 1071 |
+
|
| 1072 |
+
Returns
|
| 1073 |
+
=======
|
| 1074 |
+
|
| 1075 |
+
Quaternion
|
| 1076 |
+
Integration of the quaternion(self) with the given variable.
|
| 1077 |
+
|
| 1078 |
+
Examples
|
| 1079 |
+
========
|
| 1080 |
+
|
| 1081 |
+
Indefinite Integral of quaternion :
|
| 1082 |
+
|
| 1083 |
+
>>> from sympy import Quaternion
|
| 1084 |
+
>>> from sympy.abc import x
|
| 1085 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
| 1086 |
+
>>> q.integrate(x)
|
| 1087 |
+
x + 2*x*i + 3*x*j + 4*x*k
|
| 1088 |
+
|
| 1089 |
+
Definite integral of quaternion :
|
| 1090 |
+
|
| 1091 |
+
>>> from sympy import Quaternion
|
| 1092 |
+
>>> from sympy.abc import x
|
| 1093 |
+
>>> q = Quaternion(1, 2, 3, 4)
|
| 1094 |
+
>>> q.integrate((x, 1, 5))
|
| 1095 |
+
4 + 8*i + 12*j + 16*k
|
| 1096 |
+
|
| 1097 |
+
"""
|
| 1098 |
+
return Quaternion(integrate(self.a, *args), integrate(self.b, *args),
|
| 1099 |
+
integrate(self.c, *args), integrate(self.d, *args))
|
| 1100 |
+
|
| 1101 |
+
@staticmethod
|
| 1102 |
+
def rotate_point(pin, r):
|
| 1103 |
+
"""Returns the coordinates of the point pin (a 3 tuple) after rotation.
|
| 1104 |
+
|
| 1105 |
+
Parameters
|
| 1106 |
+
==========
|
| 1107 |
+
|
| 1108 |
+
pin : tuple
|
| 1109 |
+
A 3-element tuple of coordinates of a point which needs to be
|
| 1110 |
+
rotated.
|
| 1111 |
+
r : Quaternion or tuple
|
| 1112 |
+
Axis and angle of rotation.
|
| 1113 |
+
|
| 1114 |
+
It's important to note that when r is a tuple, it must be of the form
|
| 1115 |
+
(axis, angle)
|
| 1116 |
+
|
| 1117 |
+
Returns
|
| 1118 |
+
=======
|
| 1119 |
+
|
| 1120 |
+
tuple
|
| 1121 |
+
The coordinates of the point after rotation.
|
| 1122 |
+
|
| 1123 |
+
Examples
|
| 1124 |
+
========
|
| 1125 |
+
|
| 1126 |
+
>>> from sympy import Quaternion
|
| 1127 |
+
>>> from sympy import symbols, trigsimp, cos, sin
|
| 1128 |
+
>>> x = symbols('x')
|
| 1129 |
+
>>> q = Quaternion(cos(x/2), 0, 0, sin(x/2))
|
| 1130 |
+
>>> trigsimp(Quaternion.rotate_point((1, 1, 1), q))
|
| 1131 |
+
(sqrt(2)*cos(x + pi/4), sqrt(2)*sin(x + pi/4), 1)
|
| 1132 |
+
>>> (axis, angle) = q.to_axis_angle()
|
| 1133 |
+
>>> trigsimp(Quaternion.rotate_point((1, 1, 1), (axis, angle)))
|
| 1134 |
+
(sqrt(2)*cos(x + pi/4), sqrt(2)*sin(x + pi/4), 1)
|
| 1135 |
+
|
| 1136 |
+
"""
|
| 1137 |
+
if isinstance(r, tuple):
|
| 1138 |
+
# if r is of the form (vector, angle)
|
| 1139 |
+
q = Quaternion.from_axis_angle(r[0], r[1])
|
| 1140 |
+
else:
|
| 1141 |
+
# if r is a quaternion
|
| 1142 |
+
q = r.normalize()
|
| 1143 |
+
pout = q * Quaternion(0, pin[0], pin[1], pin[2]) * conjugate(q)
|
| 1144 |
+
return (pout.b, pout.c, pout.d)
|
| 1145 |
+
|
| 1146 |
+
def to_axis_angle(self):
|
| 1147 |
+
"""Returns the axis and angle of rotation of a quaternion.
|
| 1148 |
+
|
| 1149 |
+
Returns
|
| 1150 |
+
=======
|
| 1151 |
+
|
| 1152 |
+
tuple
|
| 1153 |
+
Tuple of (axis, angle)
|
| 1154 |
+
|
| 1155 |
+
Examples
|
| 1156 |
+
========
|
| 1157 |
+
|
| 1158 |
+
>>> from sympy import Quaternion
|
| 1159 |
+
>>> q = Quaternion(1, 1, 1, 1)
|
| 1160 |
+
>>> (axis, angle) = q.to_axis_angle()
|
| 1161 |
+
>>> axis
|
| 1162 |
+
(sqrt(3)/3, sqrt(3)/3, sqrt(3)/3)
|
| 1163 |
+
>>> angle
|
| 1164 |
+
2*pi/3
|
| 1165 |
+
|
| 1166 |
+
"""
|
| 1167 |
+
q = self
|
| 1168 |
+
if q.a.is_negative:
|
| 1169 |
+
q = q * -1
|
| 1170 |
+
|
| 1171 |
+
q = q.normalize()
|
| 1172 |
+
angle = trigsimp(2 * acos(q.a))
|
| 1173 |
+
|
| 1174 |
+
# Since quaternion is normalised, q.a is less than 1.
|
| 1175 |
+
s = sqrt(1 - q.a*q.a)
|
| 1176 |
+
|
| 1177 |
+
x = trigsimp(q.b / s)
|
| 1178 |
+
y = trigsimp(q.c / s)
|
| 1179 |
+
z = trigsimp(q.d / s)
|
| 1180 |
+
|
| 1181 |
+
v = (x, y, z)
|
| 1182 |
+
t = (v, angle)
|
| 1183 |
+
|
| 1184 |
+
return t
|
| 1185 |
+
|
| 1186 |
+
def to_rotation_matrix(self, v=None, homogeneous=True):
|
| 1187 |
+
"""Returns the equivalent rotation transformation matrix of the quaternion
|
| 1188 |
+
which represents rotation about the origin if ``v`` is not passed.
|
| 1189 |
+
|
| 1190 |
+
Parameters
|
| 1191 |
+
==========
|
| 1192 |
+
|
| 1193 |
+
v : tuple or None
|
| 1194 |
+
Default value: None
|
| 1195 |
+
homogeneous : bool
|
| 1196 |
+
When True, gives an expression that may be more efficient for
|
| 1197 |
+
symbolic calculations but less so for direct evaluation. Both
|
| 1198 |
+
formulas are mathematically equivalent.
|
| 1199 |
+
Default value: True
|
| 1200 |
+
|
| 1201 |
+
Returns
|
| 1202 |
+
=======
|
| 1203 |
+
|
| 1204 |
+
tuple
|
| 1205 |
+
Returns the equivalent rotation transformation matrix of the quaternion
|
| 1206 |
+
which represents rotation about the origin if v is not passed.
|
| 1207 |
+
|
| 1208 |
+
Examples
|
| 1209 |
+
========
|
| 1210 |
+
|
| 1211 |
+
>>> from sympy import Quaternion
|
| 1212 |
+
>>> from sympy import symbols, trigsimp, cos, sin
|
| 1213 |
+
>>> x = symbols('x')
|
| 1214 |
+
>>> q = Quaternion(cos(x/2), 0, 0, sin(x/2))
|
| 1215 |
+
>>> trigsimp(q.to_rotation_matrix())
|
| 1216 |
+
Matrix([
|
| 1217 |
+
[cos(x), -sin(x), 0],
|
| 1218 |
+
[sin(x), cos(x), 0],
|
| 1219 |
+
[ 0, 0, 1]])
|
| 1220 |
+
|
| 1221 |
+
Generates a 4x4 transformation matrix (used for rotation about a point
|
| 1222 |
+
other than the origin) if the point(v) is passed as an argument.
|
| 1223 |
+
"""
|
| 1224 |
+
|
| 1225 |
+
q = self
|
| 1226 |
+
s = q.norm()**-2
|
| 1227 |
+
|
| 1228 |
+
# diagonal elements are different according to parameter normal
|
| 1229 |
+
if homogeneous:
|
| 1230 |
+
m00 = s*(q.a**2 + q.b**2 - q.c**2 - q.d**2)
|
| 1231 |
+
m11 = s*(q.a**2 - q.b**2 + q.c**2 - q.d**2)
|
| 1232 |
+
m22 = s*(q.a**2 - q.b**2 - q.c**2 + q.d**2)
|
| 1233 |
+
else:
|
| 1234 |
+
m00 = 1 - 2*s*(q.c**2 + q.d**2)
|
| 1235 |
+
m11 = 1 - 2*s*(q.b**2 + q.d**2)
|
| 1236 |
+
m22 = 1 - 2*s*(q.b**2 + q.c**2)
|
| 1237 |
+
|
| 1238 |
+
m01 = 2*s*(q.b*q.c - q.d*q.a)
|
| 1239 |
+
m02 = 2*s*(q.b*q.d + q.c*q.a)
|
| 1240 |
+
|
| 1241 |
+
m10 = 2*s*(q.b*q.c + q.d*q.a)
|
| 1242 |
+
m12 = 2*s*(q.c*q.d - q.b*q.a)
|
| 1243 |
+
|
| 1244 |
+
m20 = 2*s*(q.b*q.d - q.c*q.a)
|
| 1245 |
+
m21 = 2*s*(q.c*q.d + q.b*q.a)
|
| 1246 |
+
|
| 1247 |
+
if not v:
|
| 1248 |
+
return Matrix([[m00, m01, m02], [m10, m11, m12], [m20, m21, m22]])
|
| 1249 |
+
|
| 1250 |
+
else:
|
| 1251 |
+
(x, y, z) = v
|
| 1252 |
+
|
| 1253 |
+
m03 = x - x*m00 - y*m01 - z*m02
|
| 1254 |
+
m13 = y - x*m10 - y*m11 - z*m12
|
| 1255 |
+
m23 = z - x*m20 - y*m21 - z*m22
|
| 1256 |
+
m30 = m31 = m32 = 0
|
| 1257 |
+
m33 = 1
|
| 1258 |
+
|
| 1259 |
+
return Matrix([[m00, m01, m02, m03], [m10, m11, m12, m13],
|
| 1260 |
+
[m20, m21, m22, m23], [m30, m31, m32, m33]])
|
| 1261 |
+
|
| 1262 |
+
def scalar_part(self):
|
| 1263 |
+
r"""Returns scalar part($\mathbf{S}(q)$) of the quaternion q.
|
| 1264 |
+
|
| 1265 |
+
Explanation
|
| 1266 |
+
===========
|
| 1267 |
+
|
| 1268 |
+
Given a quaternion $q = a + bi + cj + dk$, returns $\mathbf{S}(q) = a$.
|
| 1269 |
+
|
| 1270 |
+
Examples
|
| 1271 |
+
========
|
| 1272 |
+
|
| 1273 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1274 |
+
>>> q = Quaternion(4, 8, 13, 12)
|
| 1275 |
+
>>> q.scalar_part()
|
| 1276 |
+
4
|
| 1277 |
+
|
| 1278 |
+
"""
|
| 1279 |
+
|
| 1280 |
+
return self.a
|
| 1281 |
+
|
| 1282 |
+
def vector_part(self):
|
| 1283 |
+
r"""
|
| 1284 |
+
Returns $\mathbf{V}(q)$, the vector part of the quaternion $q$.
|
| 1285 |
+
|
| 1286 |
+
Explanation
|
| 1287 |
+
===========
|
| 1288 |
+
|
| 1289 |
+
Given a quaternion $q = a + bi + cj + dk$, returns $\mathbf{V}(q) = bi + cj + dk$.
|
| 1290 |
+
|
| 1291 |
+
Examples
|
| 1292 |
+
========
|
| 1293 |
+
|
| 1294 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1295 |
+
>>> q = Quaternion(1, 1, 1, 1)
|
| 1296 |
+
>>> q.vector_part()
|
| 1297 |
+
0 + 1*i + 1*j + 1*k
|
| 1298 |
+
|
| 1299 |
+
>>> q = Quaternion(4, 8, 13, 12)
|
| 1300 |
+
>>> q.vector_part()
|
| 1301 |
+
0 + 8*i + 13*j + 12*k
|
| 1302 |
+
|
| 1303 |
+
"""
|
| 1304 |
+
|
| 1305 |
+
return Quaternion(0, self.b, self.c, self.d)
|
| 1306 |
+
|
| 1307 |
+
def axis(self):
|
| 1308 |
+
r"""
|
| 1309 |
+
Returns $\mathbf{Ax}(q)$, the axis of the quaternion $q$.
|
| 1310 |
+
|
| 1311 |
+
Explanation
|
| 1312 |
+
===========
|
| 1313 |
+
|
| 1314 |
+
Given a quaternion $q = a + bi + cj + dk$, returns $\mathbf{Ax}(q)$ i.e., the versor of the vector part of that quaternion
|
| 1315 |
+
equal to $\mathbf{U}[\mathbf{V}(q)]$.
|
| 1316 |
+
The axis is always an imaginary unit with square equal to $-1 + 0i + 0j + 0k$.
|
| 1317 |
+
|
| 1318 |
+
Examples
|
| 1319 |
+
========
|
| 1320 |
+
|
| 1321 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1322 |
+
>>> q = Quaternion(1, 1, 1, 1)
|
| 1323 |
+
>>> q.axis()
|
| 1324 |
+
0 + sqrt(3)/3*i + sqrt(3)/3*j + sqrt(3)/3*k
|
| 1325 |
+
|
| 1326 |
+
See Also
|
| 1327 |
+
========
|
| 1328 |
+
|
| 1329 |
+
vector_part
|
| 1330 |
+
|
| 1331 |
+
"""
|
| 1332 |
+
axis = self.vector_part().normalize()
|
| 1333 |
+
|
| 1334 |
+
return Quaternion(0, axis.b, axis.c, axis.d)
|
| 1335 |
+
|
| 1336 |
+
def is_pure(self):
|
| 1337 |
+
"""
|
| 1338 |
+
Returns true if the quaternion is pure, false if the quaternion is not pure
|
| 1339 |
+
or returns none if it is unknown.
|
| 1340 |
+
|
| 1341 |
+
Explanation
|
| 1342 |
+
===========
|
| 1343 |
+
|
| 1344 |
+
A pure quaternion (also a vector quaternion) is a quaternion with scalar
|
| 1345 |
+
part equal to 0.
|
| 1346 |
+
|
| 1347 |
+
Examples
|
| 1348 |
+
========
|
| 1349 |
+
|
| 1350 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1351 |
+
>>> q = Quaternion(0, 8, 13, 12)
|
| 1352 |
+
>>> q.is_pure()
|
| 1353 |
+
True
|
| 1354 |
+
|
| 1355 |
+
See Also
|
| 1356 |
+
========
|
| 1357 |
+
scalar_part
|
| 1358 |
+
|
| 1359 |
+
"""
|
| 1360 |
+
|
| 1361 |
+
return self.a.is_zero
|
| 1362 |
+
|
| 1363 |
+
def is_zero_quaternion(self):
|
| 1364 |
+
"""
|
| 1365 |
+
Returns true if the quaternion is a zero quaternion or false if it is not a zero quaternion
|
| 1366 |
+
and None if the value is unknown.
|
| 1367 |
+
|
| 1368 |
+
Explanation
|
| 1369 |
+
===========
|
| 1370 |
+
|
| 1371 |
+
A zero quaternion is a quaternion with both scalar part and
|
| 1372 |
+
vector part equal to 0.
|
| 1373 |
+
|
| 1374 |
+
Examples
|
| 1375 |
+
========
|
| 1376 |
+
|
| 1377 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1378 |
+
>>> q = Quaternion(1, 0, 0, 0)
|
| 1379 |
+
>>> q.is_zero_quaternion()
|
| 1380 |
+
False
|
| 1381 |
+
|
| 1382 |
+
>>> q = Quaternion(0, 0, 0, 0)
|
| 1383 |
+
>>> q.is_zero_quaternion()
|
| 1384 |
+
True
|
| 1385 |
+
|
| 1386 |
+
See Also
|
| 1387 |
+
========
|
| 1388 |
+
scalar_part
|
| 1389 |
+
vector_part
|
| 1390 |
+
|
| 1391 |
+
"""
|
| 1392 |
+
|
| 1393 |
+
return self.norm().is_zero
|
| 1394 |
+
|
| 1395 |
+
def angle(self):
|
| 1396 |
+
r"""
|
| 1397 |
+
Returns the angle of the quaternion measured in the real-axis plane.
|
| 1398 |
+
|
| 1399 |
+
Explanation
|
| 1400 |
+
===========
|
| 1401 |
+
|
| 1402 |
+
Given a quaternion $q = a + bi + cj + dk$ where $a$, $b$, $c$ and $d$
|
| 1403 |
+
are real numbers, returns the angle of the quaternion given by
|
| 1404 |
+
|
| 1405 |
+
.. math::
|
| 1406 |
+
\theta := 2 \operatorname{atan_2}\left(\sqrt{b^2 + c^2 + d^2}, {a}\right)
|
| 1407 |
+
|
| 1408 |
+
Examples
|
| 1409 |
+
========
|
| 1410 |
+
|
| 1411 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1412 |
+
>>> q = Quaternion(1, 4, 4, 4)
|
| 1413 |
+
>>> q.angle()
|
| 1414 |
+
2*atan(4*sqrt(3))
|
| 1415 |
+
|
| 1416 |
+
"""
|
| 1417 |
+
|
| 1418 |
+
return 2 * atan2(self.vector_part().norm(), self.scalar_part())
|
| 1419 |
+
|
| 1420 |
+
|
| 1421 |
+
def arc_coplanar(self, other):
|
| 1422 |
+
"""
|
| 1423 |
+
Returns True if the transformation arcs represented by the input quaternions happen in the same plane.
|
| 1424 |
+
|
| 1425 |
+
Explanation
|
| 1426 |
+
===========
|
| 1427 |
+
|
| 1428 |
+
Two quaternions are said to be coplanar (in this arc sense) when their axes are parallel.
|
| 1429 |
+
The plane of a quaternion is the one normal to its axis.
|
| 1430 |
+
|
| 1431 |
+
Parameters
|
| 1432 |
+
==========
|
| 1433 |
+
|
| 1434 |
+
other : a Quaternion
|
| 1435 |
+
|
| 1436 |
+
Returns
|
| 1437 |
+
=======
|
| 1438 |
+
|
| 1439 |
+
True : if the planes of the two quaternions are the same, apart from its orientation/sign.
|
| 1440 |
+
False : if the planes of the two quaternions are not the same, apart from its orientation/sign.
|
| 1441 |
+
None : if plane of either of the quaternion is unknown.
|
| 1442 |
+
|
| 1443 |
+
Examples
|
| 1444 |
+
========
|
| 1445 |
+
|
| 1446 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1447 |
+
>>> q1 = Quaternion(1, 4, 4, 4)
|
| 1448 |
+
>>> q2 = Quaternion(3, 8, 8, 8)
|
| 1449 |
+
>>> Quaternion.arc_coplanar(q1, q2)
|
| 1450 |
+
True
|
| 1451 |
+
|
| 1452 |
+
>>> q1 = Quaternion(2, 8, 13, 12)
|
| 1453 |
+
>>> Quaternion.arc_coplanar(q1, q2)
|
| 1454 |
+
False
|
| 1455 |
+
|
| 1456 |
+
See Also
|
| 1457 |
+
========
|
| 1458 |
+
|
| 1459 |
+
vector_coplanar
|
| 1460 |
+
is_pure
|
| 1461 |
+
|
| 1462 |
+
"""
|
| 1463 |
+
if (self.is_zero_quaternion()) or (other.is_zero_quaternion()):
|
| 1464 |
+
raise ValueError('Neither of the given quaternions can be 0')
|
| 1465 |
+
|
| 1466 |
+
return fuzzy_or([(self.axis() - other.axis()).is_zero_quaternion(), (self.axis() + other.axis()).is_zero_quaternion()])
|
| 1467 |
+
|
| 1468 |
+
@classmethod
|
| 1469 |
+
def vector_coplanar(cls, q1, q2, q3):
|
| 1470 |
+
r"""
|
| 1471 |
+
Returns True if the axis of the pure quaternions seen as 3D vectors
|
| 1472 |
+
``q1``, ``q2``, and ``q3`` are coplanar.
|
| 1473 |
+
|
| 1474 |
+
Explanation
|
| 1475 |
+
===========
|
| 1476 |
+
|
| 1477 |
+
Three pure quaternions are vector coplanar if the quaternions seen as 3D vectors are coplanar.
|
| 1478 |
+
|
| 1479 |
+
Parameters
|
| 1480 |
+
==========
|
| 1481 |
+
|
| 1482 |
+
q1
|
| 1483 |
+
A pure Quaternion.
|
| 1484 |
+
q2
|
| 1485 |
+
A pure Quaternion.
|
| 1486 |
+
q3
|
| 1487 |
+
A pure Quaternion.
|
| 1488 |
+
|
| 1489 |
+
Returns
|
| 1490 |
+
=======
|
| 1491 |
+
|
| 1492 |
+
True : if the axis of the pure quaternions seen as 3D vectors
|
| 1493 |
+
q1, q2, and q3 are coplanar.
|
| 1494 |
+
False : if the axis of the pure quaternions seen as 3D vectors
|
| 1495 |
+
q1, q2, and q3 are not coplanar.
|
| 1496 |
+
None : if the axis of the pure quaternions seen as 3D vectors
|
| 1497 |
+
q1, q2, and q3 are coplanar is unknown.
|
| 1498 |
+
|
| 1499 |
+
Examples
|
| 1500 |
+
========
|
| 1501 |
+
|
| 1502 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1503 |
+
>>> q1 = Quaternion(0, 4, 4, 4)
|
| 1504 |
+
>>> q2 = Quaternion(0, 8, 8, 8)
|
| 1505 |
+
>>> q3 = Quaternion(0, 24, 24, 24)
|
| 1506 |
+
>>> Quaternion.vector_coplanar(q1, q2, q3)
|
| 1507 |
+
True
|
| 1508 |
+
|
| 1509 |
+
>>> q1 = Quaternion(0, 8, 16, 8)
|
| 1510 |
+
>>> q2 = Quaternion(0, 8, 3, 12)
|
| 1511 |
+
>>> Quaternion.vector_coplanar(q1, q2, q3)
|
| 1512 |
+
False
|
| 1513 |
+
|
| 1514 |
+
See Also
|
| 1515 |
+
========
|
| 1516 |
+
|
| 1517 |
+
axis
|
| 1518 |
+
is_pure
|
| 1519 |
+
|
| 1520 |
+
"""
|
| 1521 |
+
|
| 1522 |
+
if fuzzy_not(q1.is_pure()) or fuzzy_not(q2.is_pure()) or fuzzy_not(q3.is_pure()):
|
| 1523 |
+
raise ValueError('The given quaternions must be pure')
|
| 1524 |
+
|
| 1525 |
+
M = Matrix([[q1.b, q1.c, q1.d], [q2.b, q2.c, q2.d], [q3.b, q3.c, q3.d]]).det()
|
| 1526 |
+
return M.is_zero
|
| 1527 |
+
|
| 1528 |
+
def parallel(self, other):
|
| 1529 |
+
"""
|
| 1530 |
+
Returns True if the two pure quaternions seen as 3D vectors are parallel.
|
| 1531 |
+
|
| 1532 |
+
Explanation
|
| 1533 |
+
===========
|
| 1534 |
+
|
| 1535 |
+
Two pure quaternions are called parallel when their vector product is commutative which
|
| 1536 |
+
implies that the quaternions seen as 3D vectors have same direction.
|
| 1537 |
+
|
| 1538 |
+
Parameters
|
| 1539 |
+
==========
|
| 1540 |
+
|
| 1541 |
+
other : a Quaternion
|
| 1542 |
+
|
| 1543 |
+
Returns
|
| 1544 |
+
=======
|
| 1545 |
+
|
| 1546 |
+
True : if the two pure quaternions seen as 3D vectors are parallel.
|
| 1547 |
+
False : if the two pure quaternions seen as 3D vectors are not parallel.
|
| 1548 |
+
None : if the two pure quaternions seen as 3D vectors are parallel is unknown.
|
| 1549 |
+
|
| 1550 |
+
Examples
|
| 1551 |
+
========
|
| 1552 |
+
|
| 1553 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1554 |
+
>>> q = Quaternion(0, 4, 4, 4)
|
| 1555 |
+
>>> q1 = Quaternion(0, 8, 8, 8)
|
| 1556 |
+
>>> q.parallel(q1)
|
| 1557 |
+
True
|
| 1558 |
+
|
| 1559 |
+
>>> q1 = Quaternion(0, 8, 13, 12)
|
| 1560 |
+
>>> q.parallel(q1)
|
| 1561 |
+
False
|
| 1562 |
+
|
| 1563 |
+
"""
|
| 1564 |
+
|
| 1565 |
+
if fuzzy_not(self.is_pure()) or fuzzy_not(other.is_pure()):
|
| 1566 |
+
raise ValueError('The provided quaternions must be pure')
|
| 1567 |
+
|
| 1568 |
+
return (self*other - other*self).is_zero_quaternion()
|
| 1569 |
+
|
| 1570 |
+
def orthogonal(self, other):
|
| 1571 |
+
"""
|
| 1572 |
+
Returns the orthogonality of two quaternions.
|
| 1573 |
+
|
| 1574 |
+
Explanation
|
| 1575 |
+
===========
|
| 1576 |
+
|
| 1577 |
+
Two pure quaternions are called orthogonal when their product is anti-commutative.
|
| 1578 |
+
|
| 1579 |
+
Parameters
|
| 1580 |
+
==========
|
| 1581 |
+
|
| 1582 |
+
other : a Quaternion
|
| 1583 |
+
|
| 1584 |
+
Returns
|
| 1585 |
+
=======
|
| 1586 |
+
|
| 1587 |
+
True : if the two pure quaternions seen as 3D vectors are orthogonal.
|
| 1588 |
+
False : if the two pure quaternions seen as 3D vectors are not orthogonal.
|
| 1589 |
+
None : if the two pure quaternions seen as 3D vectors are orthogonal is unknown.
|
| 1590 |
+
|
| 1591 |
+
Examples
|
| 1592 |
+
========
|
| 1593 |
+
|
| 1594 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1595 |
+
>>> q = Quaternion(0, 4, 4, 4)
|
| 1596 |
+
>>> q1 = Quaternion(0, 8, 8, 8)
|
| 1597 |
+
>>> q.orthogonal(q1)
|
| 1598 |
+
False
|
| 1599 |
+
|
| 1600 |
+
>>> q1 = Quaternion(0, 2, 2, 0)
|
| 1601 |
+
>>> q = Quaternion(0, 2, -2, 0)
|
| 1602 |
+
>>> q.orthogonal(q1)
|
| 1603 |
+
True
|
| 1604 |
+
|
| 1605 |
+
"""
|
| 1606 |
+
|
| 1607 |
+
if fuzzy_not(self.is_pure()) or fuzzy_not(other.is_pure()):
|
| 1608 |
+
raise ValueError('The given quaternions must be pure')
|
| 1609 |
+
|
| 1610 |
+
return (self*other + other*self).is_zero_quaternion()
|
| 1611 |
+
|
| 1612 |
+
def index_vector(self):
|
| 1613 |
+
r"""
|
| 1614 |
+
Returns the index vector of the quaternion.
|
| 1615 |
+
|
| 1616 |
+
Explanation
|
| 1617 |
+
===========
|
| 1618 |
+
|
| 1619 |
+
The index vector is given by $\mathbf{T}(q)$, the norm (or magnitude) of
|
| 1620 |
+
the quaternion $q$, multiplied by $\mathbf{Ax}(q)$, the axis of $q$.
|
| 1621 |
+
|
| 1622 |
+
Returns
|
| 1623 |
+
=======
|
| 1624 |
+
|
| 1625 |
+
Quaternion: representing index vector of the provided quaternion.
|
| 1626 |
+
|
| 1627 |
+
Examples
|
| 1628 |
+
========
|
| 1629 |
+
|
| 1630 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1631 |
+
>>> q = Quaternion(2, 4, 2, 4)
|
| 1632 |
+
>>> q.index_vector()
|
| 1633 |
+
0 + 4*sqrt(10)/3*i + 2*sqrt(10)/3*j + 4*sqrt(10)/3*k
|
| 1634 |
+
|
| 1635 |
+
See Also
|
| 1636 |
+
========
|
| 1637 |
+
|
| 1638 |
+
axis
|
| 1639 |
+
norm
|
| 1640 |
+
|
| 1641 |
+
"""
|
| 1642 |
+
|
| 1643 |
+
return self.norm() * self.axis()
|
| 1644 |
+
|
| 1645 |
+
def mensor(self):
|
| 1646 |
+
"""
|
| 1647 |
+
Returns the natural logarithm of the norm(magnitude) of the quaternion.
|
| 1648 |
+
|
| 1649 |
+
Examples
|
| 1650 |
+
========
|
| 1651 |
+
|
| 1652 |
+
>>> from sympy.algebras.quaternion import Quaternion
|
| 1653 |
+
>>> q = Quaternion(2, 4, 2, 4)
|
| 1654 |
+
>>> q.mensor()
|
| 1655 |
+
log(2*sqrt(10))
|
| 1656 |
+
>>> q.norm()
|
| 1657 |
+
2*sqrt(10)
|
| 1658 |
+
|
| 1659 |
+
See Also
|
| 1660 |
+
========
|
| 1661 |
+
|
| 1662 |
+
norm
|
| 1663 |
+
|
| 1664 |
+
"""
|
| 1665 |
+
|
| 1666 |
+
return ln(self.norm())
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/tests/__init__.py
ADDED
|
File without changes
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/algebras/tests/test_quaternion.py
ADDED
|
@@ -0,0 +1,437 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.testing.pytest import slow
|
| 2 |
+
from sympy.core.function import diff
|
| 3 |
+
from sympy.core.function import expand
|
| 4 |
+
from sympy.core.numbers import (E, I, Rational, pi)
|
| 5 |
+
from sympy.core.singleton import S
|
| 6 |
+
from sympy.core.symbol import (Symbol, symbols)
|
| 7 |
+
from sympy.functions.elementary.complexes import (Abs, conjugate, im, re, sign)
|
| 8 |
+
from sympy.functions.elementary.exponential import log
|
| 9 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 10 |
+
from sympy.functions.elementary.trigonometric import (acos, asin, cos, sin, atan2, atan)
|
| 11 |
+
from sympy.integrals.integrals import integrate
|
| 12 |
+
from sympy.matrices.dense import Matrix
|
| 13 |
+
from sympy.simplify import simplify
|
| 14 |
+
from sympy.simplify.trigsimp import trigsimp
|
| 15 |
+
from sympy.algebras.quaternion import Quaternion
|
| 16 |
+
from sympy.testing.pytest import raises
|
| 17 |
+
import math
|
| 18 |
+
from itertools import permutations, product
|
| 19 |
+
|
| 20 |
+
w, x, y, z = symbols('w:z')
|
| 21 |
+
phi = symbols('phi')
|
| 22 |
+
|
| 23 |
+
def test_quaternion_construction():
|
| 24 |
+
q = Quaternion(w, x, y, z)
|
| 25 |
+
assert q + q == Quaternion(2*w, 2*x, 2*y, 2*z)
|
| 26 |
+
|
| 27 |
+
q2 = Quaternion.from_axis_angle((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3),
|
| 28 |
+
pi*Rational(2, 3))
|
| 29 |
+
assert q2 == Quaternion(S.Half, S.Half,
|
| 30 |
+
S.Half, S.Half)
|
| 31 |
+
|
| 32 |
+
M = Matrix([[cos(phi), -sin(phi), 0], [sin(phi), cos(phi), 0], [0, 0, 1]])
|
| 33 |
+
q3 = trigsimp(Quaternion.from_rotation_matrix(M))
|
| 34 |
+
assert q3 == Quaternion(
|
| 35 |
+
sqrt(2)*sqrt(cos(phi) + 1)/2, 0, 0, sqrt(2 - 2*cos(phi))*sign(sin(phi))/2)
|
| 36 |
+
|
| 37 |
+
nc = Symbol('nc', commutative=False)
|
| 38 |
+
raises(ValueError, lambda: Quaternion(w, x, nc, z))
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def test_quaternion_construction_norm():
|
| 42 |
+
q1 = Quaternion(*symbols('a:d'))
|
| 43 |
+
|
| 44 |
+
q2 = Quaternion(w, x, y, z)
|
| 45 |
+
assert expand((q1*q2).norm()**2 - (q1.norm()**2 * q2.norm()**2)) == 0
|
| 46 |
+
|
| 47 |
+
q3 = Quaternion(w, x, y, z, norm=1)
|
| 48 |
+
assert (q1 * q3).norm() == q1.norm()
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def test_issue_25254():
|
| 52 |
+
# calculating the inverse cached the norm which caused problems
|
| 53 |
+
# when multiplying
|
| 54 |
+
p = Quaternion(1, 0, 0, 0)
|
| 55 |
+
q = Quaternion.from_axis_angle((1, 1, 1), 3 * math.pi/4)
|
| 56 |
+
qi = q.inverse() # this operation cached the norm
|
| 57 |
+
test = q * p * qi
|
| 58 |
+
assert ((test - p).norm() < 1E-10)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def test_to_and_from_Matrix():
|
| 62 |
+
q = Quaternion(w, x, y, z)
|
| 63 |
+
q_full = Quaternion.from_Matrix(q.to_Matrix())
|
| 64 |
+
q_vect = Quaternion.from_Matrix(q.to_Matrix(True))
|
| 65 |
+
assert (q - q_full).is_zero_quaternion()
|
| 66 |
+
assert (q.vector_part() - q_vect).is_zero_quaternion()
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_product_matrices():
|
| 70 |
+
q1 = Quaternion(w, x, y, z)
|
| 71 |
+
q2 = Quaternion(*(symbols("a:d")))
|
| 72 |
+
assert (q1 * q2).to_Matrix() == q1.product_matrix_left * q2.to_Matrix()
|
| 73 |
+
assert (q1 * q2).to_Matrix() == q2.product_matrix_right * q1.to_Matrix()
|
| 74 |
+
|
| 75 |
+
R1 = (q1.product_matrix_left * q1.product_matrix_right.T)[1:, 1:]
|
| 76 |
+
R2 = simplify(q1.to_rotation_matrix()*q1.norm()**2)
|
| 77 |
+
assert R1 == R2
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def test_quaternion_axis_angle():
|
| 81 |
+
|
| 82 |
+
test_data = [ # axis, angle, expected_quaternion
|
| 83 |
+
((1, 0, 0), 0, (1, 0, 0, 0)),
|
| 84 |
+
((1, 0, 0), pi/2, (sqrt(2)/2, sqrt(2)/2, 0, 0)),
|
| 85 |
+
((0, 1, 0), pi/2, (sqrt(2)/2, 0, sqrt(2)/2, 0)),
|
| 86 |
+
((0, 0, 1), pi/2, (sqrt(2)/2, 0, 0, sqrt(2)/2)),
|
| 87 |
+
((1, 0, 0), pi, (0, 1, 0, 0)),
|
| 88 |
+
((0, 1, 0), pi, (0, 0, 1, 0)),
|
| 89 |
+
((0, 0, 1), pi, (0, 0, 0, 1)),
|
| 90 |
+
((1, 1, 1), pi, (0, 1/sqrt(3),1/sqrt(3),1/sqrt(3))),
|
| 91 |
+
((sqrt(3)/3, sqrt(3)/3, sqrt(3)/3), pi*2/3, (S.Half, S.Half, S.Half, S.Half))
|
| 92 |
+
]
|
| 93 |
+
|
| 94 |
+
for axis, angle, expected in test_data:
|
| 95 |
+
assert Quaternion.from_axis_angle(axis, angle) == Quaternion(*expected)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def test_quaternion_axis_angle_simplification():
|
| 99 |
+
result = Quaternion.from_axis_angle((1, 2, 3), asin(4))
|
| 100 |
+
assert result.a == cos(asin(4)/2)
|
| 101 |
+
assert result.b == sqrt(14)*sin(asin(4)/2)/14
|
| 102 |
+
assert result.c == sqrt(14)*sin(asin(4)/2)/7
|
| 103 |
+
assert result.d == 3*sqrt(14)*sin(asin(4)/2)/14
|
| 104 |
+
|
| 105 |
+
def test_quaternion_complex_real_addition():
|
| 106 |
+
a = symbols("a", complex=True)
|
| 107 |
+
b = symbols("b", real=True)
|
| 108 |
+
# This symbol is not complex:
|
| 109 |
+
c = symbols("c", commutative=False)
|
| 110 |
+
|
| 111 |
+
q = Quaternion(w, x, y, z)
|
| 112 |
+
assert a + q == Quaternion(w + re(a), x + im(a), y, z)
|
| 113 |
+
assert 1 + q == Quaternion(1 + w, x, y, z)
|
| 114 |
+
assert I + q == Quaternion(w, 1 + x, y, z)
|
| 115 |
+
assert b + q == Quaternion(w + b, x, y, z)
|
| 116 |
+
raises(ValueError, lambda: c + q)
|
| 117 |
+
raises(ValueError, lambda: q * c)
|
| 118 |
+
raises(ValueError, lambda: c * q)
|
| 119 |
+
|
| 120 |
+
assert -q == Quaternion(-w, -x, -y, -z)
|
| 121 |
+
|
| 122 |
+
q1 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
| 123 |
+
q2 = Quaternion(1, 4, 7, 8)
|
| 124 |
+
|
| 125 |
+
assert q1 + (2 + 3*I) == Quaternion(5 + 7*I, 2 + 5*I, 0, 7 + 8*I)
|
| 126 |
+
assert q2 + (2 + 3*I) == Quaternion(3, 7, 7, 8)
|
| 127 |
+
assert q1 * (2 + 3*I) == \
|
| 128 |
+
Quaternion((2 + 3*I)*(3 + 4*I), (2 + 3*I)*(2 + 5*I), 0, (2 + 3*I)*(7 + 8*I))
|
| 129 |
+
assert q2 * (2 + 3*I) == Quaternion(-10, 11, 38, -5)
|
| 130 |
+
|
| 131 |
+
q1 = Quaternion(1, 2, 3, 4)
|
| 132 |
+
q0 = Quaternion(0, 0, 0, 0)
|
| 133 |
+
assert q1 + q0 == q1
|
| 134 |
+
assert q1 - q0 == q1
|
| 135 |
+
assert q1 - q1 == q0
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
def test_quaternion_subs():
|
| 139 |
+
q = Quaternion.from_axis_angle((0, 0, 1), phi)
|
| 140 |
+
assert q.subs(phi, 0) == Quaternion(1, 0, 0, 0)
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def test_quaternion_evalf():
|
| 144 |
+
assert (Quaternion(sqrt(2), 0, 0, sqrt(3)).evalf() ==
|
| 145 |
+
Quaternion(sqrt(2).evalf(), 0, 0, sqrt(3).evalf()))
|
| 146 |
+
assert (Quaternion(1/sqrt(2), 0, 0, 1/sqrt(2)).evalf() ==
|
| 147 |
+
Quaternion((1/sqrt(2)).evalf(), 0, 0, (1/sqrt(2)).evalf()))
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def test_quaternion_functions():
|
| 151 |
+
q = Quaternion(w, x, y, z)
|
| 152 |
+
q1 = Quaternion(1, 2, 3, 4)
|
| 153 |
+
q0 = Quaternion(0, 0, 0, 0)
|
| 154 |
+
|
| 155 |
+
assert conjugate(q) == Quaternion(w, -x, -y, -z)
|
| 156 |
+
assert q.norm() == sqrt(w**2 + x**2 + y**2 + z**2)
|
| 157 |
+
assert q.normalize() == Quaternion(w, x, y, z) / sqrt(w**2 + x**2 + y**2 + z**2)
|
| 158 |
+
assert q.inverse() == Quaternion(w, -x, -y, -z) / (w**2 + x**2 + y**2 + z**2)
|
| 159 |
+
assert q.inverse() == q.pow(-1)
|
| 160 |
+
raises(ValueError, lambda: q0.inverse())
|
| 161 |
+
assert q.pow(2) == Quaternion(w**2 - x**2 - y**2 - z**2, 2*w*x, 2*w*y, 2*w*z)
|
| 162 |
+
assert q**(2) == Quaternion(w**2 - x**2 - y**2 - z**2, 2*w*x, 2*w*y, 2*w*z)
|
| 163 |
+
assert q1.pow(-2) == Quaternion(
|
| 164 |
+
Rational(-7, 225), Rational(-1, 225), Rational(-1, 150), Rational(-2, 225))
|
| 165 |
+
assert q1**(-2) == Quaternion(
|
| 166 |
+
Rational(-7, 225), Rational(-1, 225), Rational(-1, 150), Rational(-2, 225))
|
| 167 |
+
assert q1.pow(-0.5) == NotImplemented
|
| 168 |
+
raises(TypeError, lambda: q1**(-0.5))
|
| 169 |
+
|
| 170 |
+
assert q1.exp() == \
|
| 171 |
+
Quaternion(E * cos(sqrt(29)),
|
| 172 |
+
2 * sqrt(29) * E * sin(sqrt(29)) / 29,
|
| 173 |
+
3 * sqrt(29) * E * sin(sqrt(29)) / 29,
|
| 174 |
+
4 * sqrt(29) * E * sin(sqrt(29)) / 29)
|
| 175 |
+
assert q1.log() == \
|
| 176 |
+
Quaternion(log(sqrt(30)),
|
| 177 |
+
2 * sqrt(29) * acos(sqrt(30)/30) / 29,
|
| 178 |
+
3 * sqrt(29) * acos(sqrt(30)/30) / 29,
|
| 179 |
+
4 * sqrt(29) * acos(sqrt(30)/30) / 29)
|
| 180 |
+
|
| 181 |
+
assert q1.pow_cos_sin(2) == \
|
| 182 |
+
Quaternion(30 * cos(2 * acos(sqrt(30)/30)),
|
| 183 |
+
60 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29,
|
| 184 |
+
90 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29,
|
| 185 |
+
120 * sqrt(29) * sin(2 * acos(sqrt(30)/30)) / 29)
|
| 186 |
+
|
| 187 |
+
assert diff(Quaternion(x, x, x, x), x) == Quaternion(1, 1, 1, 1)
|
| 188 |
+
|
| 189 |
+
assert integrate(Quaternion(x, x, x, x), x) == \
|
| 190 |
+
Quaternion(x**2 / 2, x**2 / 2, x**2 / 2, x**2 / 2)
|
| 191 |
+
|
| 192 |
+
assert Quaternion(1, x, x**2, x**3).integrate(x) == \
|
| 193 |
+
Quaternion(x, x**2/2, x**3/3, x**4/4)
|
| 194 |
+
|
| 195 |
+
assert Quaternion(sin(x), cos(x), sin(2*x), cos(2*x)).integrate(x) == \
|
| 196 |
+
Quaternion(-cos(x), sin(x), -cos(2*x)/2, sin(2*x)/2)
|
| 197 |
+
|
| 198 |
+
assert Quaternion(x**2, y**2, z**2, x*y*z).integrate(x, y) == \
|
| 199 |
+
Quaternion(x**3*y/3, x*y**3/3, x*y*z**2, x**2*y**2*z/4)
|
| 200 |
+
|
| 201 |
+
assert Quaternion.rotate_point((1, 1, 1), q1) == (S.One / 5, 1, S(7) / 5)
|
| 202 |
+
n = Symbol('n')
|
| 203 |
+
raises(TypeError, lambda: q1**n)
|
| 204 |
+
n = Symbol('n', integer=True)
|
| 205 |
+
raises(TypeError, lambda: q1**n)
|
| 206 |
+
|
| 207 |
+
assert Quaternion(22, 23, 55, 8).scalar_part() == 22
|
| 208 |
+
assert Quaternion(w, x, y, z).scalar_part() == w
|
| 209 |
+
|
| 210 |
+
assert Quaternion(22, 23, 55, 8).vector_part() == Quaternion(0, 23, 55, 8)
|
| 211 |
+
assert Quaternion(w, x, y, z).vector_part() == Quaternion(0, x, y, z)
|
| 212 |
+
|
| 213 |
+
assert q1.axis() == Quaternion(0, 2*sqrt(29)/29, 3*sqrt(29)/29, 4*sqrt(29)/29)
|
| 214 |
+
assert q1.axis().pow(2) == Quaternion(-1, 0, 0, 0)
|
| 215 |
+
assert q0.axis().scalar_part() == 0
|
| 216 |
+
assert (q.axis() == Quaternion(0,
|
| 217 |
+
x/sqrt(x**2 + y**2 + z**2),
|
| 218 |
+
y/sqrt(x**2 + y**2 + z**2),
|
| 219 |
+
z/sqrt(x**2 + y**2 + z**2)))
|
| 220 |
+
|
| 221 |
+
assert q0.is_pure() is True
|
| 222 |
+
assert q1.is_pure() is False
|
| 223 |
+
assert Quaternion(0, 0, 0, 3).is_pure() is True
|
| 224 |
+
assert Quaternion(0, 2, 10, 3).is_pure() is True
|
| 225 |
+
assert Quaternion(w, 2, 10, 3).is_pure() is None
|
| 226 |
+
|
| 227 |
+
assert q1.angle() == 2*atan(sqrt(29))
|
| 228 |
+
assert q.angle() == 2*atan2(sqrt(x**2 + y**2 + z**2), w)
|
| 229 |
+
|
| 230 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(2, 4, 6, 8)) is True
|
| 231 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(1, -2, -3, -4)) is True
|
| 232 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(1, 8, 12, 16)) is True
|
| 233 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(1, 2, 3, 4)) is True
|
| 234 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(w, 4, 6, 8)) is True
|
| 235 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(2, 7, 4, 1)) is False
|
| 236 |
+
assert Quaternion.arc_coplanar(q1, Quaternion(w, x, y, z)) is None
|
| 237 |
+
raises(ValueError, lambda: Quaternion.arc_coplanar(q1, q0))
|
| 238 |
+
|
| 239 |
+
assert Quaternion.vector_coplanar(
|
| 240 |
+
Quaternion(0, 8, 12, 16),
|
| 241 |
+
Quaternion(0, 4, 6, 8),
|
| 242 |
+
Quaternion(0, 2, 3, 4)) is True
|
| 243 |
+
assert Quaternion.vector_coplanar(
|
| 244 |
+
Quaternion(0, 0, 0, 0), Quaternion(0, 4, 6, 8), Quaternion(0, 2, 3, 4)) is True
|
| 245 |
+
assert Quaternion.vector_coplanar(
|
| 246 |
+
Quaternion(0, 8, 2, 6), Quaternion(0, 1, 6, 6), Quaternion(0, 0, 3, 4)) is False
|
| 247 |
+
assert Quaternion.vector_coplanar(
|
| 248 |
+
Quaternion(0, 1, 3, 4),
|
| 249 |
+
Quaternion(0, 4, w, 6),
|
| 250 |
+
Quaternion(0, 6, 8, 1)) is None
|
| 251 |
+
raises(ValueError, lambda:
|
| 252 |
+
Quaternion.vector_coplanar(q0, Quaternion(0, 4, 6, 8), q1))
|
| 253 |
+
|
| 254 |
+
assert Quaternion(0, 1, 2, 3).parallel(Quaternion(0, 2, 4, 6)) is True
|
| 255 |
+
assert Quaternion(0, 1, 2, 3).parallel(Quaternion(0, 2, 2, 6)) is False
|
| 256 |
+
assert Quaternion(0, 1, 2, 3).parallel(Quaternion(w, x, y, 6)) is None
|
| 257 |
+
raises(ValueError, lambda: q0.parallel(q1))
|
| 258 |
+
|
| 259 |
+
assert Quaternion(0, 1, 2, 3).orthogonal(Quaternion(0, -2, 1, 0)) is True
|
| 260 |
+
assert Quaternion(0, 2, 4, 7).orthogonal(Quaternion(0, 2, 2, 6)) is False
|
| 261 |
+
assert Quaternion(0, 2, 4, 7).orthogonal(Quaternion(w, x, y, 6)) is None
|
| 262 |
+
raises(ValueError, lambda: q0.orthogonal(q1))
|
| 263 |
+
|
| 264 |
+
assert q1.index_vector() == Quaternion(
|
| 265 |
+
0, 2*sqrt(870)/29,
|
| 266 |
+
3*sqrt(870)/29,
|
| 267 |
+
4*sqrt(870)/29)
|
| 268 |
+
assert Quaternion(0, 3, 9, 4).index_vector() == Quaternion(0, 3, 9, 4)
|
| 269 |
+
|
| 270 |
+
assert Quaternion(4, 3, 9, 4).mensor() == log(sqrt(122))
|
| 271 |
+
assert Quaternion(3, 3, 0, 2).mensor() == log(sqrt(22))
|
| 272 |
+
|
| 273 |
+
assert q0.is_zero_quaternion() is True
|
| 274 |
+
assert q1.is_zero_quaternion() is False
|
| 275 |
+
assert Quaternion(w, 0, 0, 0).is_zero_quaternion() is None
|
| 276 |
+
|
| 277 |
+
def test_quaternion_conversions():
|
| 278 |
+
q1 = Quaternion(1, 2, 3, 4)
|
| 279 |
+
|
| 280 |
+
assert q1.to_axis_angle() == ((2 * sqrt(29)/29,
|
| 281 |
+
3 * sqrt(29)/29,
|
| 282 |
+
4 * sqrt(29)/29),
|
| 283 |
+
2 * acos(sqrt(30)/30))
|
| 284 |
+
|
| 285 |
+
assert (q1.to_rotation_matrix() ==
|
| 286 |
+
Matrix([[Rational(-2, 3), Rational(2, 15), Rational(11, 15)],
|
| 287 |
+
[Rational(2, 3), Rational(-1, 3), Rational(2, 3)],
|
| 288 |
+
[Rational(1, 3), Rational(14, 15), Rational(2, 15)]]))
|
| 289 |
+
|
| 290 |
+
assert (q1.to_rotation_matrix((1, 1, 1)) ==
|
| 291 |
+
Matrix([
|
| 292 |
+
[Rational(-2, 3), Rational(2, 15), Rational(11, 15), Rational(4, 5)],
|
| 293 |
+
[Rational(2, 3), Rational(-1, 3), Rational(2, 3), S.Zero],
|
| 294 |
+
[Rational(1, 3), Rational(14, 15), Rational(2, 15), Rational(-2, 5)],
|
| 295 |
+
[S.Zero, S.Zero, S.Zero, S.One]]))
|
| 296 |
+
|
| 297 |
+
theta = symbols("theta", real=True)
|
| 298 |
+
q2 = Quaternion(cos(theta/2), 0, 0, sin(theta/2))
|
| 299 |
+
|
| 300 |
+
assert trigsimp(q2.to_rotation_matrix()) == Matrix([
|
| 301 |
+
[cos(theta), -sin(theta), 0],
|
| 302 |
+
[sin(theta), cos(theta), 0],
|
| 303 |
+
[0, 0, 1]])
|
| 304 |
+
|
| 305 |
+
assert q2.to_axis_angle() == ((0, 0, sin(theta/2)/Abs(sin(theta/2))),
|
| 306 |
+
2*acos(cos(theta/2)))
|
| 307 |
+
|
| 308 |
+
assert trigsimp(q2.to_rotation_matrix((1, 1, 1))) == Matrix([
|
| 309 |
+
[cos(theta), -sin(theta), 0, sin(theta) - cos(theta) + 1],
|
| 310 |
+
[sin(theta), cos(theta), 0, -sin(theta) - cos(theta) + 1],
|
| 311 |
+
[0, 0, 1, 0],
|
| 312 |
+
[0, 0, 0, 1]])
|
| 313 |
+
|
| 314 |
+
|
| 315 |
+
def test_rotation_matrix_homogeneous():
|
| 316 |
+
q = Quaternion(w, x, y, z)
|
| 317 |
+
R1 = q.to_rotation_matrix(homogeneous=True) * q.norm()**2
|
| 318 |
+
R2 = simplify(q.to_rotation_matrix(homogeneous=False) * q.norm()**2)
|
| 319 |
+
assert R1 == R2
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
def test_quaternion_rotation_iss1593():
|
| 323 |
+
"""
|
| 324 |
+
There was a sign mistake in the definition,
|
| 325 |
+
of the rotation matrix. This tests that particular sign mistake.
|
| 326 |
+
See issue 1593 for reference.
|
| 327 |
+
See wikipedia
|
| 328 |
+
https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation#Quaternion-derived_rotation_matrix
|
| 329 |
+
for the correct definition
|
| 330 |
+
"""
|
| 331 |
+
q = Quaternion(cos(phi/2), sin(phi/2), 0, 0)
|
| 332 |
+
assert(trigsimp(q.to_rotation_matrix()) == Matrix([
|
| 333 |
+
[1, 0, 0],
|
| 334 |
+
[0, cos(phi), -sin(phi)],
|
| 335 |
+
[0, sin(phi), cos(phi)]]))
|
| 336 |
+
|
| 337 |
+
|
| 338 |
+
def test_quaternion_multiplication():
|
| 339 |
+
q1 = Quaternion(3 + 4*I, 2 + 5*I, 0, 7 + 8*I, real_field = False)
|
| 340 |
+
q2 = Quaternion(1, 2, 3, 5)
|
| 341 |
+
q3 = Quaternion(1, 1, 1, y)
|
| 342 |
+
|
| 343 |
+
assert Quaternion._generic_mul(S(4), S.One) == 4
|
| 344 |
+
assert (Quaternion._generic_mul(S(4), q1) ==
|
| 345 |
+
Quaternion(12 + 16*I, 8 + 20*I, 0, 28 + 32*I))
|
| 346 |
+
assert q2.mul(2) == Quaternion(2, 4, 6, 10)
|
| 347 |
+
assert q2.mul(q3) == Quaternion(-5*y - 4, 3*y - 2, 9 - 2*y, y + 4)
|
| 348 |
+
assert q2.mul(q3) == q2*q3
|
| 349 |
+
|
| 350 |
+
z = symbols('z', complex=True)
|
| 351 |
+
z_quat = Quaternion(re(z), im(z), 0, 0)
|
| 352 |
+
q = Quaternion(*symbols('q:4', real=True))
|
| 353 |
+
|
| 354 |
+
assert z * q == z_quat * q
|
| 355 |
+
assert q * z == q * z_quat
|
| 356 |
+
|
| 357 |
+
|
| 358 |
+
def test_issue_16318():
|
| 359 |
+
#for rtruediv
|
| 360 |
+
q0 = Quaternion(0, 0, 0, 0)
|
| 361 |
+
raises(ValueError, lambda: 1/q0)
|
| 362 |
+
#for rotate_point
|
| 363 |
+
q = Quaternion(1, 2, 3, 4)
|
| 364 |
+
(axis, angle) = q.to_axis_angle()
|
| 365 |
+
assert Quaternion.rotate_point((1, 1, 1), (axis, angle)) == (S.One / 5, 1, S(7) / 5)
|
| 366 |
+
#test for to_axis_angle
|
| 367 |
+
q = Quaternion(-1, 1, 1, 1)
|
| 368 |
+
axis = (-sqrt(3)/3, -sqrt(3)/3, -sqrt(3)/3)
|
| 369 |
+
angle = 2*pi/3
|
| 370 |
+
assert (axis, angle) == q.to_axis_angle()
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
@slow
|
| 374 |
+
def test_to_euler():
|
| 375 |
+
q = Quaternion(w, x, y, z)
|
| 376 |
+
q_normalized = q.normalize()
|
| 377 |
+
|
| 378 |
+
seqs = ['zxy', 'zyx', 'zyz', 'zxz']
|
| 379 |
+
seqs += [seq.upper() for seq in seqs]
|
| 380 |
+
|
| 381 |
+
for seq in seqs:
|
| 382 |
+
euler_from_q = q.to_euler(seq)
|
| 383 |
+
q_back = simplify(Quaternion.from_euler(euler_from_q, seq))
|
| 384 |
+
assert q_back == q_normalized
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
def test_to_euler_iss24504():
|
| 388 |
+
"""
|
| 389 |
+
There was a mistake in the degenerate case testing
|
| 390 |
+
See issue 24504 for reference.
|
| 391 |
+
"""
|
| 392 |
+
q = Quaternion.from_euler((phi, 0, 0), 'zyz')
|
| 393 |
+
assert trigsimp(q.to_euler('zyz'), inverse=True) == (phi, 0, 0)
|
| 394 |
+
|
| 395 |
+
|
| 396 |
+
def test_to_euler_numerical_singilarities():
|
| 397 |
+
|
| 398 |
+
def test_one_case(angles, seq):
|
| 399 |
+
q = Quaternion.from_euler(angles, seq)
|
| 400 |
+
assert q.to_euler(seq) == angles
|
| 401 |
+
|
| 402 |
+
# symmetric
|
| 403 |
+
test_one_case((pi/2, 0, 0), 'zyz')
|
| 404 |
+
test_one_case((pi/2, 0, 0), 'ZYZ')
|
| 405 |
+
test_one_case((pi/2, pi, 0), 'zyz')
|
| 406 |
+
test_one_case((pi/2, pi, 0), 'ZYZ')
|
| 407 |
+
|
| 408 |
+
# asymmetric
|
| 409 |
+
test_one_case((pi/2, pi/2, 0), 'zyx')
|
| 410 |
+
test_one_case((pi/2, -pi/2, 0), 'zyx')
|
| 411 |
+
test_one_case((pi/2, pi/2, 0), 'ZYX')
|
| 412 |
+
test_one_case((pi/2, -pi/2, 0), 'ZYX')
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
@slow
|
| 416 |
+
def test_to_euler_options():
|
| 417 |
+
def test_one_case(q):
|
| 418 |
+
angles1 = Matrix(q.to_euler(seq, True, True))
|
| 419 |
+
angles2 = Matrix(q.to_euler(seq, False, False))
|
| 420 |
+
angle_errors = simplify(angles1-angles2).evalf()
|
| 421 |
+
for angle_error in angle_errors:
|
| 422 |
+
# forcing angles to set {-pi, pi}
|
| 423 |
+
angle_error = (angle_error + pi) % (2 * pi) - pi
|
| 424 |
+
assert angle_error < 10e-7
|
| 425 |
+
|
| 426 |
+
for xyz in ('xyz', 'XYZ'):
|
| 427 |
+
for seq_tuple in permutations(xyz):
|
| 428 |
+
for symmetric in (True, False):
|
| 429 |
+
if symmetric:
|
| 430 |
+
seq = ''.join([seq_tuple[0], seq_tuple[1], seq_tuple[0]])
|
| 431 |
+
else:
|
| 432 |
+
seq = ''.join(seq_tuple)
|
| 433 |
+
|
| 434 |
+
for elements in product([-1, 0, 1], repeat=4):
|
| 435 |
+
q = Quaternion(*elements)
|
| 436 |
+
if not q.is_zero_quaternion():
|
| 437 |
+
test_one_case(q)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/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 |
+
]
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (733 Bytes). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask.cpython-310.pyc
ADDED
|
Binary file (18.8 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/ask_generated.cpython-310.pyc
ADDED
|
Binary file (11.1 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/assume.cpython-310.pyc
ADDED
|
Binary file (14.5 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/cnf.cpython-310.pyc
ADDED
|
Binary file (16.7 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/__pycache__/refine.cpython-310.pyc
ADDED
|
Binary file (9.97 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/ask.py
ADDED
|
@@ -0,0 +1,651 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 noninteger(self):
|
| 79 |
+
from .predicates.sets import NonIntegerPredicate
|
| 80 |
+
return NonIntegerPredicate()
|
| 81 |
+
|
| 82 |
+
@memoize_property
|
| 83 |
+
def rational(self):
|
| 84 |
+
from .handlers.sets import RationalPredicate
|
| 85 |
+
return RationalPredicate()
|
| 86 |
+
|
| 87 |
+
@memoize_property
|
| 88 |
+
def irrational(self):
|
| 89 |
+
from .handlers.sets import IrrationalPredicate
|
| 90 |
+
return IrrationalPredicate()
|
| 91 |
+
|
| 92 |
+
@memoize_property
|
| 93 |
+
def finite(self):
|
| 94 |
+
from .handlers.calculus import FinitePredicate
|
| 95 |
+
return FinitePredicate()
|
| 96 |
+
|
| 97 |
+
@memoize_property
|
| 98 |
+
def infinite(self):
|
| 99 |
+
from .handlers.calculus import InfinitePredicate
|
| 100 |
+
return InfinitePredicate()
|
| 101 |
+
|
| 102 |
+
@memoize_property
|
| 103 |
+
def positive_infinite(self):
|
| 104 |
+
from .handlers.calculus import PositiveInfinitePredicate
|
| 105 |
+
return PositiveInfinitePredicate()
|
| 106 |
+
|
| 107 |
+
@memoize_property
|
| 108 |
+
def negative_infinite(self):
|
| 109 |
+
from .handlers.calculus import NegativeInfinitePredicate
|
| 110 |
+
return NegativeInfinitePredicate()
|
| 111 |
+
|
| 112 |
+
@memoize_property
|
| 113 |
+
def positive(self):
|
| 114 |
+
from .handlers.order import PositivePredicate
|
| 115 |
+
return PositivePredicate()
|
| 116 |
+
|
| 117 |
+
@memoize_property
|
| 118 |
+
def negative(self):
|
| 119 |
+
from .handlers.order import NegativePredicate
|
| 120 |
+
return NegativePredicate()
|
| 121 |
+
|
| 122 |
+
@memoize_property
|
| 123 |
+
def zero(self):
|
| 124 |
+
from .handlers.order import ZeroPredicate
|
| 125 |
+
return ZeroPredicate()
|
| 126 |
+
|
| 127 |
+
@memoize_property
|
| 128 |
+
def extended_positive(self):
|
| 129 |
+
from .handlers.order import ExtendedPositivePredicate
|
| 130 |
+
return ExtendedPositivePredicate()
|
| 131 |
+
|
| 132 |
+
@memoize_property
|
| 133 |
+
def extended_negative(self):
|
| 134 |
+
from .handlers.order import ExtendedNegativePredicate
|
| 135 |
+
return ExtendedNegativePredicate()
|
| 136 |
+
|
| 137 |
+
@memoize_property
|
| 138 |
+
def nonzero(self):
|
| 139 |
+
from .handlers.order import NonZeroPredicate
|
| 140 |
+
return NonZeroPredicate()
|
| 141 |
+
|
| 142 |
+
@memoize_property
|
| 143 |
+
def nonpositive(self):
|
| 144 |
+
from .handlers.order import NonPositivePredicate
|
| 145 |
+
return NonPositivePredicate()
|
| 146 |
+
|
| 147 |
+
@memoize_property
|
| 148 |
+
def nonnegative(self):
|
| 149 |
+
from .handlers.order import NonNegativePredicate
|
| 150 |
+
return NonNegativePredicate()
|
| 151 |
+
|
| 152 |
+
@memoize_property
|
| 153 |
+
def extended_nonzero(self):
|
| 154 |
+
from .handlers.order import ExtendedNonZeroPredicate
|
| 155 |
+
return ExtendedNonZeroPredicate()
|
| 156 |
+
|
| 157 |
+
@memoize_property
|
| 158 |
+
def extended_nonpositive(self):
|
| 159 |
+
from .handlers.order import ExtendedNonPositivePredicate
|
| 160 |
+
return ExtendedNonPositivePredicate()
|
| 161 |
+
|
| 162 |
+
@memoize_property
|
| 163 |
+
def extended_nonnegative(self):
|
| 164 |
+
from .handlers.order import ExtendedNonNegativePredicate
|
| 165 |
+
return ExtendedNonNegativePredicate()
|
| 166 |
+
|
| 167 |
+
@memoize_property
|
| 168 |
+
def even(self):
|
| 169 |
+
from .handlers.ntheory import EvenPredicate
|
| 170 |
+
return EvenPredicate()
|
| 171 |
+
|
| 172 |
+
@memoize_property
|
| 173 |
+
def odd(self):
|
| 174 |
+
from .handlers.ntheory import OddPredicate
|
| 175 |
+
return OddPredicate()
|
| 176 |
+
|
| 177 |
+
@memoize_property
|
| 178 |
+
def prime(self):
|
| 179 |
+
from .handlers.ntheory import PrimePredicate
|
| 180 |
+
return PrimePredicate()
|
| 181 |
+
|
| 182 |
+
@memoize_property
|
| 183 |
+
def composite(self):
|
| 184 |
+
from .handlers.ntheory import CompositePredicate
|
| 185 |
+
return CompositePredicate()
|
| 186 |
+
|
| 187 |
+
@memoize_property
|
| 188 |
+
def commutative(self):
|
| 189 |
+
from .handlers.common import CommutativePredicate
|
| 190 |
+
return CommutativePredicate()
|
| 191 |
+
|
| 192 |
+
@memoize_property
|
| 193 |
+
def is_true(self):
|
| 194 |
+
from .handlers.common import IsTruePredicate
|
| 195 |
+
return IsTruePredicate()
|
| 196 |
+
|
| 197 |
+
@memoize_property
|
| 198 |
+
def symmetric(self):
|
| 199 |
+
from .handlers.matrices import SymmetricPredicate
|
| 200 |
+
return SymmetricPredicate()
|
| 201 |
+
|
| 202 |
+
@memoize_property
|
| 203 |
+
def invertible(self):
|
| 204 |
+
from .handlers.matrices import InvertiblePredicate
|
| 205 |
+
return InvertiblePredicate()
|
| 206 |
+
|
| 207 |
+
@memoize_property
|
| 208 |
+
def orthogonal(self):
|
| 209 |
+
from .handlers.matrices import OrthogonalPredicate
|
| 210 |
+
return OrthogonalPredicate()
|
| 211 |
+
|
| 212 |
+
@memoize_property
|
| 213 |
+
def unitary(self):
|
| 214 |
+
from .handlers.matrices import UnitaryPredicate
|
| 215 |
+
return UnitaryPredicate()
|
| 216 |
+
|
| 217 |
+
@memoize_property
|
| 218 |
+
def positive_definite(self):
|
| 219 |
+
from .handlers.matrices import PositiveDefinitePredicate
|
| 220 |
+
return PositiveDefinitePredicate()
|
| 221 |
+
|
| 222 |
+
@memoize_property
|
| 223 |
+
def upper_triangular(self):
|
| 224 |
+
from .handlers.matrices import UpperTriangularPredicate
|
| 225 |
+
return UpperTriangularPredicate()
|
| 226 |
+
|
| 227 |
+
@memoize_property
|
| 228 |
+
def lower_triangular(self):
|
| 229 |
+
from .handlers.matrices import LowerTriangularPredicate
|
| 230 |
+
return LowerTriangularPredicate()
|
| 231 |
+
|
| 232 |
+
@memoize_property
|
| 233 |
+
def diagonal(self):
|
| 234 |
+
from .handlers.matrices import DiagonalPredicate
|
| 235 |
+
return DiagonalPredicate()
|
| 236 |
+
|
| 237 |
+
@memoize_property
|
| 238 |
+
def fullrank(self):
|
| 239 |
+
from .handlers.matrices import FullRankPredicate
|
| 240 |
+
return FullRankPredicate()
|
| 241 |
+
|
| 242 |
+
@memoize_property
|
| 243 |
+
def square(self):
|
| 244 |
+
from .handlers.matrices import SquarePredicate
|
| 245 |
+
return SquarePredicate()
|
| 246 |
+
|
| 247 |
+
@memoize_property
|
| 248 |
+
def integer_elements(self):
|
| 249 |
+
from .handlers.matrices import IntegerElementsPredicate
|
| 250 |
+
return IntegerElementsPredicate()
|
| 251 |
+
|
| 252 |
+
@memoize_property
|
| 253 |
+
def real_elements(self):
|
| 254 |
+
from .handlers.matrices import RealElementsPredicate
|
| 255 |
+
return RealElementsPredicate()
|
| 256 |
+
|
| 257 |
+
@memoize_property
|
| 258 |
+
def complex_elements(self):
|
| 259 |
+
from .handlers.matrices import ComplexElementsPredicate
|
| 260 |
+
return ComplexElementsPredicate()
|
| 261 |
+
|
| 262 |
+
@memoize_property
|
| 263 |
+
def singular(self):
|
| 264 |
+
from .predicates.matrices import SingularPredicate
|
| 265 |
+
return SingularPredicate()
|
| 266 |
+
|
| 267 |
+
@memoize_property
|
| 268 |
+
def normal(self):
|
| 269 |
+
from .predicates.matrices import NormalPredicate
|
| 270 |
+
return NormalPredicate()
|
| 271 |
+
|
| 272 |
+
@memoize_property
|
| 273 |
+
def triangular(self):
|
| 274 |
+
from .predicates.matrices import TriangularPredicate
|
| 275 |
+
return TriangularPredicate()
|
| 276 |
+
|
| 277 |
+
@memoize_property
|
| 278 |
+
def unit_triangular(self):
|
| 279 |
+
from .predicates.matrices import UnitTriangularPredicate
|
| 280 |
+
return UnitTriangularPredicate()
|
| 281 |
+
|
| 282 |
+
@memoize_property
|
| 283 |
+
def eq(self):
|
| 284 |
+
from .relation.equality import EqualityPredicate
|
| 285 |
+
return EqualityPredicate()
|
| 286 |
+
|
| 287 |
+
@memoize_property
|
| 288 |
+
def ne(self):
|
| 289 |
+
from .relation.equality import UnequalityPredicate
|
| 290 |
+
return UnequalityPredicate()
|
| 291 |
+
|
| 292 |
+
@memoize_property
|
| 293 |
+
def gt(self):
|
| 294 |
+
from .relation.equality import StrictGreaterThanPredicate
|
| 295 |
+
return StrictGreaterThanPredicate()
|
| 296 |
+
|
| 297 |
+
@memoize_property
|
| 298 |
+
def ge(self):
|
| 299 |
+
from .relation.equality import GreaterThanPredicate
|
| 300 |
+
return GreaterThanPredicate()
|
| 301 |
+
|
| 302 |
+
@memoize_property
|
| 303 |
+
def lt(self):
|
| 304 |
+
from .relation.equality import StrictLessThanPredicate
|
| 305 |
+
return StrictLessThanPredicate()
|
| 306 |
+
|
| 307 |
+
@memoize_property
|
| 308 |
+
def le(self):
|
| 309 |
+
from .relation.equality import LessThanPredicate
|
| 310 |
+
return LessThanPredicate()
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
Q = AssumptionKeys()
|
| 314 |
+
|
| 315 |
+
def _extract_all_facts(assump, exprs):
|
| 316 |
+
"""
|
| 317 |
+
Extract all relevant assumptions from *assump* with respect to given *exprs*.
|
| 318 |
+
|
| 319 |
+
Parameters
|
| 320 |
+
==========
|
| 321 |
+
|
| 322 |
+
assump : sympy.assumptions.cnf.CNF
|
| 323 |
+
|
| 324 |
+
exprs : tuple of expressions
|
| 325 |
+
|
| 326 |
+
Returns
|
| 327 |
+
=======
|
| 328 |
+
|
| 329 |
+
sympy.assumptions.cnf.CNF
|
| 330 |
+
|
| 331 |
+
Examples
|
| 332 |
+
========
|
| 333 |
+
|
| 334 |
+
>>> from sympy import Q
|
| 335 |
+
>>> from sympy.assumptions.cnf import CNF
|
| 336 |
+
>>> from sympy.assumptions.ask import _extract_all_facts
|
| 337 |
+
>>> from sympy.abc import x, y
|
| 338 |
+
>>> assump = CNF.from_prop(Q.positive(x) & Q.integer(y))
|
| 339 |
+
>>> exprs = (x,)
|
| 340 |
+
>>> cnf = _extract_all_facts(assump, exprs)
|
| 341 |
+
>>> cnf.clauses
|
| 342 |
+
{frozenset({Literal(Q.positive, False)})}
|
| 343 |
+
|
| 344 |
+
"""
|
| 345 |
+
facts = set()
|
| 346 |
+
|
| 347 |
+
for clause in assump.clauses:
|
| 348 |
+
args = []
|
| 349 |
+
for literal in clause:
|
| 350 |
+
if isinstance(literal.lit, AppliedPredicate) and len(literal.lit.arguments) == 1:
|
| 351 |
+
if literal.lit.arg in exprs:
|
| 352 |
+
# Add literal if it has matching in it
|
| 353 |
+
args.append(Literal(literal.lit.function, literal.is_Not))
|
| 354 |
+
else:
|
| 355 |
+
# If any of the literals doesn't have matching expr don't add the whole clause.
|
| 356 |
+
break
|
| 357 |
+
else:
|
| 358 |
+
# If any of the literals aren't unary predicate don't add the whole clause.
|
| 359 |
+
break
|
| 360 |
+
|
| 361 |
+
else:
|
| 362 |
+
if args:
|
| 363 |
+
facts.add(frozenset(args))
|
| 364 |
+
return CNF(facts)
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
def ask(proposition, assumptions=True, context=global_assumptions):
|
| 368 |
+
"""
|
| 369 |
+
Function to evaluate the proposition with assumptions.
|
| 370 |
+
|
| 371 |
+
Explanation
|
| 372 |
+
===========
|
| 373 |
+
|
| 374 |
+
This function evaluates the proposition to ``True`` or ``False`` if
|
| 375 |
+
the truth value can be determined. If not, it returns ``None``.
|
| 376 |
+
|
| 377 |
+
It should be discerned from :func:`~.refine` which, when applied to a
|
| 378 |
+
proposition, simplifies the argument to symbolic ``Boolean`` instead of
|
| 379 |
+
Python built-in ``True``, ``False`` or ``None``.
|
| 380 |
+
|
| 381 |
+
**Syntax**
|
| 382 |
+
|
| 383 |
+
* ask(proposition)
|
| 384 |
+
Evaluate the *proposition* in global assumption context.
|
| 385 |
+
|
| 386 |
+
* ask(proposition, assumptions)
|
| 387 |
+
Evaluate the *proposition* with respect to *assumptions* in
|
| 388 |
+
global assumption context.
|
| 389 |
+
|
| 390 |
+
Parameters
|
| 391 |
+
==========
|
| 392 |
+
|
| 393 |
+
proposition : Boolean
|
| 394 |
+
Proposition which will be evaluated to boolean value. If this is
|
| 395 |
+
not ``AppliedPredicate``, it will be wrapped by ``Q.is_true``.
|
| 396 |
+
|
| 397 |
+
assumptions : Boolean, optional
|
| 398 |
+
Local assumptions to evaluate the *proposition*.
|
| 399 |
+
|
| 400 |
+
context : AssumptionsContext, optional
|
| 401 |
+
Default assumptions to evaluate the *proposition*. By default,
|
| 402 |
+
this is ``sympy.assumptions.global_assumptions`` variable.
|
| 403 |
+
|
| 404 |
+
Returns
|
| 405 |
+
=======
|
| 406 |
+
|
| 407 |
+
``True``, ``False``, or ``None``
|
| 408 |
+
|
| 409 |
+
Raises
|
| 410 |
+
======
|
| 411 |
+
|
| 412 |
+
TypeError : *proposition* or *assumptions* is not valid logical expression.
|
| 413 |
+
|
| 414 |
+
ValueError : assumptions are inconsistent.
|
| 415 |
+
|
| 416 |
+
Examples
|
| 417 |
+
========
|
| 418 |
+
|
| 419 |
+
>>> from sympy import ask, Q, pi
|
| 420 |
+
>>> from sympy.abc import x, y
|
| 421 |
+
>>> ask(Q.rational(pi))
|
| 422 |
+
False
|
| 423 |
+
>>> ask(Q.even(x*y), Q.even(x) & Q.integer(y))
|
| 424 |
+
True
|
| 425 |
+
>>> ask(Q.prime(4*x), Q.integer(x))
|
| 426 |
+
False
|
| 427 |
+
|
| 428 |
+
If the truth value cannot be determined, ``None`` will be returned.
|
| 429 |
+
|
| 430 |
+
>>> print(ask(Q.odd(3*x))) # cannot determine unless we know x
|
| 431 |
+
None
|
| 432 |
+
|
| 433 |
+
``ValueError`` is raised if assumptions are inconsistent.
|
| 434 |
+
|
| 435 |
+
>>> ask(Q.integer(x), Q.even(x) & Q.odd(x))
|
| 436 |
+
Traceback (most recent call last):
|
| 437 |
+
...
|
| 438 |
+
ValueError: inconsistent assumptions Q.even(x) & Q.odd(x)
|
| 439 |
+
|
| 440 |
+
Notes
|
| 441 |
+
=====
|
| 442 |
+
|
| 443 |
+
Relations in assumptions are not implemented (yet), so the following
|
| 444 |
+
will not give a meaningful result.
|
| 445 |
+
|
| 446 |
+
>>> ask(Q.positive(x), x > 0)
|
| 447 |
+
|
| 448 |
+
It is however a work in progress.
|
| 449 |
+
|
| 450 |
+
See Also
|
| 451 |
+
========
|
| 452 |
+
|
| 453 |
+
sympy.assumptions.refine.refine : Simplification using assumptions.
|
| 454 |
+
Proposition is not reduced to ``None`` if the truth value cannot
|
| 455 |
+
be determined.
|
| 456 |
+
"""
|
| 457 |
+
from sympy.assumptions.satask import satask
|
| 458 |
+
from sympy.assumptions.lra_satask import lra_satask
|
| 459 |
+
from sympy.logic.algorithms.lra_theory import UnhandledInput
|
| 460 |
+
|
| 461 |
+
proposition = sympify(proposition)
|
| 462 |
+
assumptions = sympify(assumptions)
|
| 463 |
+
|
| 464 |
+
if isinstance(proposition, Predicate) or proposition.kind is not BooleanKind:
|
| 465 |
+
raise TypeError("proposition must be a valid logical expression")
|
| 466 |
+
|
| 467 |
+
if isinstance(assumptions, Predicate) or assumptions.kind is not BooleanKind:
|
| 468 |
+
raise TypeError("assumptions must be a valid logical expression")
|
| 469 |
+
|
| 470 |
+
binrelpreds = {Eq: Q.eq, Ne: Q.ne, Gt: Q.gt, Lt: Q.lt, Ge: Q.ge, Le: Q.le}
|
| 471 |
+
if isinstance(proposition, AppliedPredicate):
|
| 472 |
+
key, args = proposition.function, proposition.arguments
|
| 473 |
+
elif proposition.func in binrelpreds:
|
| 474 |
+
key, args = binrelpreds[type(proposition)], proposition.args
|
| 475 |
+
else:
|
| 476 |
+
key, args = Q.is_true, (proposition,)
|
| 477 |
+
|
| 478 |
+
# convert local and global assumptions to CNF
|
| 479 |
+
assump_cnf = CNF.from_prop(assumptions)
|
| 480 |
+
assump_cnf.extend(context)
|
| 481 |
+
|
| 482 |
+
# extract the relevant facts from assumptions with respect to args
|
| 483 |
+
local_facts = _extract_all_facts(assump_cnf, args)
|
| 484 |
+
|
| 485 |
+
# convert default facts and assumed facts to encoded CNF
|
| 486 |
+
known_facts_cnf = get_all_known_facts()
|
| 487 |
+
enc_cnf = EncodedCNF()
|
| 488 |
+
enc_cnf.from_cnf(CNF(known_facts_cnf))
|
| 489 |
+
enc_cnf.add_from_cnf(local_facts)
|
| 490 |
+
|
| 491 |
+
# check the satisfiability of given assumptions
|
| 492 |
+
if local_facts.clauses and satisfiable(enc_cnf) is False:
|
| 493 |
+
raise ValueError("inconsistent assumptions %s" % assumptions)
|
| 494 |
+
|
| 495 |
+
# quick computation for single fact
|
| 496 |
+
res = _ask_single_fact(key, local_facts)
|
| 497 |
+
if res is not None:
|
| 498 |
+
return res
|
| 499 |
+
|
| 500 |
+
# direct resolution method, no logic
|
| 501 |
+
res = key(*args)._eval_ask(assumptions)
|
| 502 |
+
if res is not None:
|
| 503 |
+
return bool(res)
|
| 504 |
+
|
| 505 |
+
# using satask (still costly)
|
| 506 |
+
res = satask(proposition, assumptions=assumptions, context=context)
|
| 507 |
+
if res is not None:
|
| 508 |
+
return res
|
| 509 |
+
|
| 510 |
+
try:
|
| 511 |
+
res = lra_satask(proposition, assumptions=assumptions, context=context)
|
| 512 |
+
except UnhandledInput:
|
| 513 |
+
return None
|
| 514 |
+
|
| 515 |
+
return res
|
| 516 |
+
|
| 517 |
+
|
| 518 |
+
def _ask_single_fact(key, local_facts):
|
| 519 |
+
"""
|
| 520 |
+
Compute the truth value of single predicate using assumptions.
|
| 521 |
+
|
| 522 |
+
Parameters
|
| 523 |
+
==========
|
| 524 |
+
|
| 525 |
+
key : sympy.assumptions.assume.Predicate
|
| 526 |
+
Proposition predicate.
|
| 527 |
+
|
| 528 |
+
local_facts : sympy.assumptions.cnf.CNF
|
| 529 |
+
Local assumption in CNF form.
|
| 530 |
+
|
| 531 |
+
Returns
|
| 532 |
+
=======
|
| 533 |
+
|
| 534 |
+
``True``, ``False`` or ``None``
|
| 535 |
+
|
| 536 |
+
Examples
|
| 537 |
+
========
|
| 538 |
+
|
| 539 |
+
>>> from sympy import Q
|
| 540 |
+
>>> from sympy.assumptions.cnf import CNF
|
| 541 |
+
>>> from sympy.assumptions.ask import _ask_single_fact
|
| 542 |
+
|
| 543 |
+
If prerequisite of proposition is rejected by the assumption,
|
| 544 |
+
return ``False``.
|
| 545 |
+
|
| 546 |
+
>>> key, assump = Q.zero, ~Q.zero
|
| 547 |
+
>>> local_facts = CNF.from_prop(assump)
|
| 548 |
+
>>> _ask_single_fact(key, local_facts)
|
| 549 |
+
False
|
| 550 |
+
>>> key, assump = Q.zero, ~Q.even
|
| 551 |
+
>>> local_facts = CNF.from_prop(assump)
|
| 552 |
+
>>> _ask_single_fact(key, local_facts)
|
| 553 |
+
False
|
| 554 |
+
|
| 555 |
+
If assumption implies the proposition, return ``True``.
|
| 556 |
+
|
| 557 |
+
>>> key, assump = Q.even, Q.zero
|
| 558 |
+
>>> local_facts = CNF.from_prop(assump)
|
| 559 |
+
>>> _ask_single_fact(key, local_facts)
|
| 560 |
+
True
|
| 561 |
+
|
| 562 |
+
If proposition rejects the assumption, return ``False``.
|
| 563 |
+
|
| 564 |
+
>>> key, assump = Q.even, Q.odd
|
| 565 |
+
>>> local_facts = CNF.from_prop(assump)
|
| 566 |
+
>>> _ask_single_fact(key, local_facts)
|
| 567 |
+
False
|
| 568 |
+
"""
|
| 569 |
+
if local_facts.clauses:
|
| 570 |
+
|
| 571 |
+
known_facts_dict = get_known_facts_dict()
|
| 572 |
+
|
| 573 |
+
if len(local_facts.clauses) == 1:
|
| 574 |
+
cl, = local_facts.clauses
|
| 575 |
+
if len(cl) == 1:
|
| 576 |
+
f, = cl
|
| 577 |
+
prop_facts = known_facts_dict.get(key, None)
|
| 578 |
+
prop_req = prop_facts[0] if prop_facts is not None else set()
|
| 579 |
+
if f.is_Not and f.arg in prop_req:
|
| 580 |
+
# the prerequisite of proposition is rejected
|
| 581 |
+
return False
|
| 582 |
+
|
| 583 |
+
for clause in local_facts.clauses:
|
| 584 |
+
if len(clause) == 1:
|
| 585 |
+
f, = clause
|
| 586 |
+
prop_facts = known_facts_dict.get(f.arg, None) if not f.is_Not else None
|
| 587 |
+
if prop_facts is None:
|
| 588 |
+
continue
|
| 589 |
+
|
| 590 |
+
prop_req, prop_rej = prop_facts
|
| 591 |
+
if key in prop_req:
|
| 592 |
+
# assumption implies the proposition
|
| 593 |
+
return True
|
| 594 |
+
elif key in prop_rej:
|
| 595 |
+
# proposition rejects the assumption
|
| 596 |
+
return False
|
| 597 |
+
|
| 598 |
+
return None
|
| 599 |
+
|
| 600 |
+
|
| 601 |
+
def register_handler(key, handler):
|
| 602 |
+
"""
|
| 603 |
+
Register a handler in the ask system. key must be a string and handler a
|
| 604 |
+
class inheriting from AskHandler.
|
| 605 |
+
|
| 606 |
+
.. deprecated:: 1.8.
|
| 607 |
+
Use multipledispatch handler instead. See :obj:`~.Predicate`.
|
| 608 |
+
|
| 609 |
+
"""
|
| 610 |
+
sympy_deprecation_warning(
|
| 611 |
+
"""
|
| 612 |
+
The AskHandler system is deprecated. The register_handler() function
|
| 613 |
+
should be replaced with the multipledispatch handler of Predicate.
|
| 614 |
+
""",
|
| 615 |
+
deprecated_since_version="1.8",
|
| 616 |
+
active_deprecations_target='deprecated-askhandler',
|
| 617 |
+
)
|
| 618 |
+
if isinstance(key, Predicate):
|
| 619 |
+
key = key.name.name
|
| 620 |
+
Qkey = getattr(Q, key, None)
|
| 621 |
+
if Qkey is not None:
|
| 622 |
+
Qkey.add_handler(handler)
|
| 623 |
+
else:
|
| 624 |
+
setattr(Q, key, Predicate(key, handlers=[handler]))
|
| 625 |
+
|
| 626 |
+
|
| 627 |
+
def remove_handler(key, handler):
|
| 628 |
+
"""
|
| 629 |
+
Removes a handler from the ask system.
|
| 630 |
+
|
| 631 |
+
.. deprecated:: 1.8.
|
| 632 |
+
Use multipledispatch handler instead. See :obj:`~.Predicate`.
|
| 633 |
+
|
| 634 |
+
"""
|
| 635 |
+
sympy_deprecation_warning(
|
| 636 |
+
"""
|
| 637 |
+
The AskHandler system is deprecated. The remove_handler() function
|
| 638 |
+
should be replaced with the multipledispatch handler of Predicate.
|
| 639 |
+
""",
|
| 640 |
+
deprecated_since_version="1.8",
|
| 641 |
+
active_deprecations_target='deprecated-askhandler',
|
| 642 |
+
)
|
| 643 |
+
if isinstance(key, Predicate):
|
| 644 |
+
key = key.name.name
|
| 645 |
+
# Don't show the same warning again recursively
|
| 646 |
+
with ignore_warnings(SymPyDeprecationWarning):
|
| 647 |
+
getattr(Q, key).remove_handler(handler)
|
| 648 |
+
|
| 649 |
+
|
| 650 |
+
from sympy.ass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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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_all_known_matrix_facts():
|
| 91 |
+
"""
|
| 92 |
+
Known facts between unary predicates for matrices as CNF clauses.
|
| 93 |
+
"""
|
| 94 |
+
return {
|
| 95 |
+
frozenset((Literal(Q.complex_elements, False), Literal(Q.real_elements, True))),
|
| 96 |
+
frozenset((Literal(Q.diagonal, False), Literal(Q.lower_triangular, True), Literal(Q.upper_triangular, True))),
|
| 97 |
+
frozenset((Literal(Q.diagonal, True), Literal(Q.lower_triangular, False))),
|
| 98 |
+
frozenset((Literal(Q.diagonal, True), Literal(Q.normal, False))),
|
| 99 |
+
frozenset((Literal(Q.diagonal, True), Literal(Q.symmetric, False))),
|
| 100 |
+
frozenset((Literal(Q.diagonal, True), Literal(Q.upper_triangular, False))),
|
| 101 |
+
frozenset((Literal(Q.fullrank, False), Literal(Q.invertible, True))),
|
| 102 |
+
frozenset((Literal(Q.fullrank, True), Literal(Q.invertible, False), Literal(Q.square, True))),
|
| 103 |
+
frozenset((Literal(Q.integer_elements, True), Literal(Q.real_elements, False))),
|
| 104 |
+
frozenset((Literal(Q.invertible, False), Literal(Q.positive_definite, True))),
|
| 105 |
+
frozenset((Literal(Q.invertible, False), Literal(Q.singular, False))),
|
| 106 |
+
frozenset((Literal(Q.invertible, False), Literal(Q.unitary, True))),
|
| 107 |
+
frozenset((Literal(Q.invertible, True), Literal(Q.singular, True))),
|
| 108 |
+
frozenset((Literal(Q.invertible, True), Literal(Q.square, False))),
|
| 109 |
+
frozenset((Literal(Q.lower_triangular, False), Literal(Q.triangular, True), Literal(Q.upper_triangular, False))),
|
| 110 |
+
frozenset((Literal(Q.lower_triangular, True), Literal(Q.triangular, False))),
|
| 111 |
+
frozenset((Literal(Q.normal, False), Literal(Q.unitary, True))),
|
| 112 |
+
frozenset((Literal(Q.normal, True), Literal(Q.square, False))),
|
| 113 |
+
frozenset((Literal(Q.orthogonal, False), Literal(Q.real_elements, True), Literal(Q.unitary, True))),
|
| 114 |
+
frozenset((Literal(Q.orthogonal, True), Literal(Q.positive_definite, False))),
|
| 115 |
+
frozenset((Literal(Q.orthogonal, True), Literal(Q.unitary, False))),
|
| 116 |
+
frozenset((Literal(Q.square, False), Literal(Q.symmetric, True))),
|
| 117 |
+
frozenset((Literal(Q.triangular, False), Literal(Q.unit_triangular, True))),
|
| 118 |
+
frozenset((Literal(Q.triangular, False), Literal(Q.upper_triangular, True)))
|
| 119 |
+
}
|
| 120 |
+
|
| 121 |
+
@cacheit
|
| 122 |
+
def get_all_known_number_facts():
|
| 123 |
+
"""
|
| 124 |
+
Known facts between unary predicates for numbers as CNF clauses.
|
| 125 |
+
"""
|
| 126 |
+
return {
|
| 127 |
+
frozenset((Literal(Q.algebraic, False), Literal(Q.imaginary, True), Literal(Q.transcendental, False))),
|
| 128 |
+
frozenset((Literal(Q.algebraic, False), Literal(Q.negative, True), Literal(Q.transcendental, False))),
|
| 129 |
+
frozenset((Literal(Q.algebraic, False), Literal(Q.positive, True), Literal(Q.transcendental, False))),
|
| 130 |
+
frozenset((Literal(Q.algebraic, False), Literal(Q.rational, True))),
|
| 131 |
+
frozenset((Literal(Q.algebraic, False), Literal(Q.transcendental, False), Literal(Q.zero, True))),
|
| 132 |
+
frozenset((Literal(Q.algebraic, True), Literal(Q.finite, False))),
|
| 133 |
+
frozenset((Literal(Q.algebraic, True), Literal(Q.transcendental, True))),
|
| 134 |
+
frozenset((Literal(Q.antihermitian, False), Literal(Q.hermitian, False), Literal(Q.zero, True))),
|
| 135 |
+
frozenset((Literal(Q.antihermitian, False), Literal(Q.imaginary, True))),
|
| 136 |
+
frozenset((Literal(Q.commutative, False), Literal(Q.finite, True))),
|
| 137 |
+
frozenset((Literal(Q.commutative, False), Literal(Q.infinite, True))),
|
| 138 |
+
frozenset((Literal(Q.composite, False), Literal(Q.even, True), Literal(Q.positive, True), Literal(Q.prime, False))),
|
| 139 |
+
frozenset((Literal(Q.composite, True), Literal(Q.even, False), Literal(Q.odd, False))),
|
| 140 |
+
frozenset((Literal(Q.composite, True), Literal(Q.positive, False))),
|
| 141 |
+
frozenset((Literal(Q.composite, True), Literal(Q.prime, True))),
|
| 142 |
+
frozenset((Literal(Q.even, False), Literal(Q.odd, False), Literal(Q.prime, True))),
|
| 143 |
+
frozenset((Literal(Q.even, False), Literal(Q.zero, True))),
|
| 144 |
+
frozenset((Literal(Q.even, True), Literal(Q.odd, True))),
|
| 145 |
+
frozenset((Literal(Q.even, True), Literal(Q.rational, False))),
|
| 146 |
+
frozenset((Literal(Q.finite, False), Literal(Q.transcendental, True))),
|
| 147 |
+
frozenset((Literal(Q.finite, True), Literal(Q.infinite, True))),
|
| 148 |
+
frozenset((Literal(Q.hermitian, False), Literal(Q.negative, True))),
|
| 149 |
+
frozenset((Literal(Q.hermitian, False), Literal(Q.positive, True))),
|
| 150 |
+
frozenset((Literal(Q.hermitian, False), Literal(Q.zero, True))),
|
| 151 |
+
frozenset((Literal(Q.imaginary, True), Literal(Q.negative, True))),
|
| 152 |
+
frozenset((Literal(Q.imaginary, True), Literal(Q.positive, True))),
|
| 153 |
+
frozenset((Literal(Q.imaginary, True), Literal(Q.zero, True))),
|
| 154 |
+
frozenset((Literal(Q.infinite, False), Literal(Q.negative_infinite, True))),
|
| 155 |
+
frozenset((Literal(Q.infinite, False), Literal(Q.positive_infinite, True))),
|
| 156 |
+
frozenset((Literal(Q.irrational, False), Literal(Q.negative, True), Literal(Q.rational, False))),
|
| 157 |
+
frozenset((Literal(Q.irrational, False), Literal(Q.positive, True), Literal(Q.rational, False))),
|
| 158 |
+
frozenset((Literal(Q.irrational, False), Literal(Q.rational, False), Literal(Q.zero, True))),
|
| 159 |
+
frozenset((Literal(Q.irrational, True), Literal(Q.negative, False), Literal(Q.positive, False), Literal(Q.zero, False))),
|
| 160 |
+
frozenset((Literal(Q.irrational, True), Literal(Q.rational, True))),
|
| 161 |
+
frozenset((Literal(Q.negative, False), Literal(Q.positive, False), Literal(Q.rational, True), Literal(Q.zero, False))),
|
| 162 |
+
frozenset((Literal(Q.negative, True), Literal(Q.negative_infinite, True))),
|
| 163 |
+
frozenset((Literal(Q.negative, True), Literal(Q.positive, True))),
|
| 164 |
+
frozenset((Literal(Q.negative, True), Literal(Q.positive_infinite, True))),
|
| 165 |
+
frozenset((Literal(Q.negative, True), Literal(Q.zero, True))),
|
| 166 |
+
frozenset((Literal(Q.negative_infinite, True), Literal(Q.positive, True))),
|
| 167 |
+
frozenset((Literal(Q.negative_infinite, True), Literal(Q.positive_infinite, True))),
|
| 168 |
+
frozenset((Literal(Q.negative_infinite, True), Literal(Q.zero, True))),
|
| 169 |
+
frozenset((Literal(Q.odd, True), Literal(Q.rational, False))),
|
| 170 |
+
frozenset((Literal(Q.positive, False), Literal(Q.prime, True))),
|
| 171 |
+
frozenset((Literal(Q.positive, True), Literal(Q.positive_infinite, True))),
|
| 172 |
+
frozenset((Literal(Q.positive, True), Literal(Q.zero, True))),
|
| 173 |
+
frozenset((Literal(Q.positive_infinite, True), Literal(Q.zero, True)))
|
| 174 |
+
}
|
| 175 |
+
|
| 176 |
+
@cacheit
|
| 177 |
+
def get_known_facts_dict():
|
| 178 |
+
"""
|
| 179 |
+
Logical relations between unary predicates as dictionary.
|
| 180 |
+
|
| 181 |
+
Each key is a predicate, and item is two groups of predicates.
|
| 182 |
+
First group contains the predicates which are implied by the key, and
|
| 183 |
+
second group contains the predicates which are rejected by the key.
|
| 184 |
+
|
| 185 |
+
"""
|
| 186 |
+
return {
|
| 187 |
+
Q.algebraic: (set([Q.algebraic, Q.commutative, Q.complex, Q.finite]),
|
| 188 |
+
set([Q.infinite, Q.negative_infinite, Q.positive_infinite,
|
| 189 |
+
Q.transcendental])),
|
| 190 |
+
Q.antihermitian: (set([Q.antihermitian]), set([])),
|
| 191 |
+
Q.commutative: (set([Q.commutative]), set([])),
|
| 192 |
+
Q.complex: (set([Q.commutative, Q.complex, Q.finite]),
|
| 193 |
+
set([Q.infinite, Q.negative_infinite, Q.positive_infinite])),
|
| 194 |
+
Q.complex_elements: (set([Q.complex_elements]), set([])),
|
| 195 |
+
Q.composite: (set([Q.algebraic, Q.commutative, Q.complex, Q.composite,
|
| 196 |
+
Q.extended_nonnegative, Q.extended_nonzero,
|
| 197 |
+
Q.extended_positive, Q.extended_real, Q.finite, Q.hermitian,
|
| 198 |
+
Q.integer, Q.nonnegative, Q.nonzero, Q.positive, Q.rational,
|
| 199 |
+
Q.real]), set([Q.extended_negative, Q.extended_nonpositive,
|
| 200 |
+
Q.imaginary, Q.infinite, Q.irrational, Q.negative,
|
| 201 |
+
Q.negative_infinite, Q.nonpositive, Q.positive_infinite,
|
| 202 |
+
Q.prime, Q.transcendental, Q.zero])),
|
| 203 |
+
Q.diagonal: (set([Q.diagonal, Q.lower_triangular, Q.normal, Q.square,
|
| 204 |
+
Q.symmetric, Q.triangular, Q.upper_triangular]), set([])),
|
| 205 |
+
Q.even: (set([Q.algebraic, Q.commutative, Q.complex, Q.even,
|
| 206 |
+
Q.extended_real, Q.finite, Q.hermitian, Q.integer, Q.rational,
|
| 207 |
+
Q.real]), set([Q.imaginary, Q.infinite, Q.irrational,
|
| 208 |
+
Q.negative_infinite, Q.odd, Q.positive_infinite,
|
| 209 |
+
Q.transcendental])),
|
| 210 |
+
Q.extended_negative: (set([Q.commutative, Q.extended_negative,
|
| 211 |
+
Q.extended_nonpositive, Q.extended_nonzero, Q.extended_real]),
|
| 212 |
+
set([Q.composite, Q.extended_nonnegative, Q.extended_positive,
|
| 213 |
+
Q.imaginary, Q.nonnegative, Q.positive, Q.positive_infinite,
|
| 214 |
+
Q.prime, Q.zero])),
|
| 215 |
+
Q.extended_nonnegative: (set([Q.commutative, Q.extended_nonnegative,
|
| 216 |
+
Q.extended_real]), set([Q.extended_negative, Q.imaginary,
|
| 217 |
+
Q.negative, Q.negative_infinite])),
|
| 218 |
+
Q.extended_nonpositive: (set([Q.commutative, Q.extended_nonpositive,
|
| 219 |
+
Q.extended_real]), set([Q.composite, Q.extended_positive,
|
| 220 |
+
Q.imaginary, Q.positive, Q.positive_infinite, Q.prime])),
|
| 221 |
+
Q.extended_nonzero: (set([Q.commutative, Q.extended_nonzero,
|
| 222 |
+
Q.extended_real]), set([Q.imaginary, Q.zero])),
|
| 223 |
+
Q.extended_positive: (set([Q.commutative, Q.extended_nonnegative,
|
| 224 |
+
Q.extended_nonzero, Q.extended_positive, Q.extended_real]),
|
| 225 |
+
set([Q.extended_negative, Q.extended_nonpositive, Q.imaginary,
|
| 226 |
+
Q.negative, Q.negative_infinite, Q.nonpositive, Q.zero])),
|
| 227 |
+
Q.extended_real: (set([Q.commutative, Q.extended_real]),
|
| 228 |
+
set([Q.imaginary])),
|
| 229 |
+
Q.finite: (set([Q.commutative, Q.finite]), set([Q.infinite,
|
| 230 |
+
Q.negative_infinite, Q.positive_infinite])),
|
| 231 |
+
Q.fullrank: (set([Q.fullrank]), set([])),
|
| 232 |
+
Q.hermitian: (set([Q.hermitian]), set([])),
|
| 233 |
+
Q.imaginary: (set([Q.antihermitian, Q.commutative, Q.complex,
|
| 234 |
+
Q.finite, Q.imaginary]), set([Q.composite, Q.even,
|
| 235 |
+
Q.extended_negative, Q.extended_nonnegative,
|
| 236 |
+
Q.extended_nonpositive, Q.extended_nonzero,
|
| 237 |
+
Q.extended_positive, Q.extended_real, Q.infinite, Q.integer,
|
| 238 |
+
Q.irrational, Q.negative, Q.negative_infinite, Q.nonnegative,
|
| 239 |
+
Q.nonpositive, Q.nonzero, Q.odd, Q.positive,
|
| 240 |
+
Q.positive_infinite, Q.prime, Q.rational, Q.real, Q.zero])),
|
| 241 |
+
Q.infinite: (set([Q.commutative, Q.infinite]), set([Q.algebraic,
|
| 242 |
+
Q.complex, Q.composite, Q.even, Q.finite, Q.imaginary,
|
| 243 |
+
Q.integer, Q.irrational, Q.negative, Q.nonnegative,
|
| 244 |
+
Q.nonpositive, Q.nonzero, Q.odd, Q.positive, Q.prime,
|
| 245 |
+
Q.rational, Q.real, Q.transcendental, Q.zero])),
|
| 246 |
+
Q.integer: (set([Q.algebraic, Q.commutative, Q.complex,
|
| 247 |
+
Q.extended_real, Q.finite, Q.hermitian, Q.integer, Q.rational,
|
| 248 |
+
Q.real]), set([Q.imaginary, Q.infinite, Q.irrational,
|
| 249 |
+
Q.negative_infinite, Q.positive_infinite, Q.transcendental])),
|
| 250 |
+
Q.integer_elements: (set([Q.complex_elements, Q.integer_elements,
|
| 251 |
+
Q.real_elements]), set([])),
|
| 252 |
+
Q.invertible: (set([Q.fullrank, Q.invertible, Q.square]),
|
| 253 |
+
set([Q.singular])),
|
| 254 |
+
Q.irrational: (set([Q.commutative, Q.complex, Q.extended_nonzero,
|
| 255 |
+
Q.extended_real, Q.finite, Q.hermitian, Q.irrational,
|
| 256 |
+
Q.nonzero, Q.real]), set([Q.composite, Q.even, Q.imaginary,
|
| 257 |
+
Q.infinite, Q.integer, Q.negative_infinite, Q.odd,
|
| 258 |
+
Q.positive_infinite, Q.prime, Q.rational, Q.zero])),
|
| 259 |
+
Q.is_true: (set([Q.is_true]), set([])),
|
| 260 |
+
Q.lower_triangular: (set([Q.lower_triangular, Q.triangular]), set([])),
|
| 261 |
+
Q.negative: (set([Q.commutative, Q.complex, Q.extended_negative,
|
| 262 |
+
Q.extended_nonpositive, Q.extended_nonzero, Q.extended_real,
|
| 263 |
+
Q.finite, Q.hermitian, Q.negative, Q.nonpositive, Q.nonzero,
|
| 264 |
+
Q.real]), set([Q.composite, Q.extended_nonnegative,
|
| 265 |
+
Q.extended_positive, Q.imaginary, Q.infinite,
|
| 266 |
+
Q.negative_infinite, Q.nonnegative, Q.positive,
|
| 267 |
+
Q.positive_infinite, Q.prime, Q.zero])),
|
| 268 |
+
Q.negative_infinite: (set([Q.commutative, Q.extended_negative,
|
| 269 |
+
Q.extended_nonpositive, Q.extended_nonzero, Q.extended_real,
|
| 270 |
+
Q.infinite, Q.negative_infinite]), set([Q.algebraic,
|
| 271 |
+
Q.complex, Q.composite, Q.even, Q.extended_nonnegative,
|
| 272 |
+
Q.extended_positive, Q.finite, Q.imaginary, Q.integer,
|
| 273 |
+
Q.irrational, Q.negative, Q.nonnegative, Q.nonpositive,
|
| 274 |
+
Q.nonzero, Q.odd, Q.positive, Q.positive_infinite, Q.prime,
|
| 275 |
+
Q.rational, Q.real, Q.transcendental, Q.zero])),
|
| 276 |
+
Q.noninteger: (set([Q.noninteger]), set([])),
|
| 277 |
+
Q.nonnegative: (set([Q.commutative, Q.complex, Q.extended_nonnegative,
|
| 278 |
+
Q.extended_real, Q.finite, Q.hermitian, Q.nonnegative,
|
| 279 |
+
Q.real]), set([Q.extended_negative, Q.imaginary, Q.infinite,
|
| 280 |
+
Q.negative, Q.negative_infinite, Q.positive_infinite])),
|
| 281 |
+
Q.nonpositive: (set([Q.commutative, Q.complex, Q.extended_nonpositive,
|
| 282 |
+
Q.extended_real, Q.finite, Q.hermitian, Q.nonpositive,
|
| 283 |
+
Q.real]), set([Q.composite, Q.extended_positive, Q.imaginary,
|
| 284 |
+
Q.infinite, Q.negative_infinite, Q.positive,
|
| 285 |
+
Q.positive_infinite, Q.prime])),
|
| 286 |
+
Q.nonzero: (set([Q.commutative, Q.complex, Q.extended_nonzero,
|
| 287 |
+
Q.extended_real, Q.finite, Q.hermitian, Q.nonzero, Q.real]),
|
| 288 |
+
set([Q.imaginary, Q.infinite, Q.negative_infinite,
|
| 289 |
+
Q.positive_infinite, Q.zero])),
|
| 290 |
+
Q.normal: (set([Q.normal, Q.square]), set([])),
|
| 291 |
+
Q.odd: (set([Q.algebraic, Q.commutative, Q.complex,
|
| 292 |
+
Q.extended_nonzero, Q.extended_real, Q.finite, Q.hermitian,
|
| 293 |
+
Q.integer, Q.nonzero, Q.odd, Q.rational, Q.real]),
|
| 294 |
+
set([Q.even, Q.imaginary, Q.infinite, Q.irrational,
|
| 295 |
+
Q.negative_infinite, Q.positive_infinite, Q.transcendental,
|
| 296 |
+
Q.zero])),
|
| 297 |
+
Q.orthogonal: (set([Q.fullrank, Q.invertible, Q.normal, Q.orthogonal,
|
| 298 |
+
Q.positive_definite, Q.square, Q.unitary]), set([Q.singular])),
|
| 299 |
+
Q.positive: (set([Q.commutative, Q.complex, Q.extended_nonnegative,
|
| 300 |
+
Q.extended_nonzero, Q.extended_positive, Q.extended_real,
|
| 301 |
+
Q.finite, Q.hermitian, Q.nonnegative, Q.nonzero, Q.positive,
|
| 302 |
+
Q.real]), set([Q.extended_negative, Q.extended_nonpositive,
|
| 303 |
+
Q.imaginary, Q.infinite, Q.negative, Q.negative_infinite,
|
| 304 |
+
Q.nonpositive, Q.positive_infinite, Q.zero])),
|
| 305 |
+
Q.positive_definite: (set([Q.fullrank, Q.invertible,
|
| 306 |
+
Q.positive_definite, Q.square]), set([Q.singular])),
|
| 307 |
+
Q.positive_infinite: (set([Q.commutative, Q.extended_nonnegative,
|
| 308 |
+
Q.extended_nonzero, Q.extended_positive, Q.extended_real,
|
| 309 |
+
Q.infinite, Q.positive_infinite]), set([Q.algebraic,
|
| 310 |
+
Q.complex, Q.composite, Q.even, Q.extended_negative,
|
| 311 |
+
Q.extended_nonpositive, Q.finite, Q.imaginary, Q.integer,
|
| 312 |
+
Q.irrational, Q.negative, Q.negative_infinite, Q.nonnegative,
|
| 313 |
+
Q.nonpositive, Q.nonzero, Q.odd, Q.positive, Q.prime,
|
| 314 |
+
Q.rational, Q.real, Q.transcendental, Q.zero])),
|
| 315 |
+
Q.prime: (set([Q.algebraic, Q.commutative, Q.complex,
|
| 316 |
+
Q.extended_nonnegative, Q.extended_nonzero,
|
| 317 |
+
Q.extended_positive, Q.extended_real, Q.finite, Q.hermitian,
|
| 318 |
+
Q.integer, Q.nonnegative, Q.nonzero, Q.positive, Q.prime,
|
| 319 |
+
Q.rational, Q.real]), set([Q.composite, Q.extended_negative,
|
| 320 |
+
Q.extended_nonpositive, Q.imaginary, Q.infinite, Q.irrational,
|
| 321 |
+
Q.negative, Q.negative_infinite, Q.nonpositive,
|
| 322 |
+
Q.positive_infinite, Q.transcendental, Q.zero])),
|
| 323 |
+
Q.rational: (set([Q.algebraic, Q.commutative, Q.complex,
|
| 324 |
+
Q.extended_real, Q.finite, Q.hermitian, Q.rational, Q.real]),
|
| 325 |
+
set([Q.imaginary, Q.infinite, Q.irrational,
|
| 326 |
+
Q.negative_infinite, Q.positive_infinite, Q.transcendental])),
|
| 327 |
+
Q.real: (set([Q.commutative, Q.complex, Q.extended_real, Q.finite,
|
| 328 |
+
Q.hermitian, Q.real]), set([Q.imaginary, Q.infinite,
|
| 329 |
+
Q.negative_infinite, Q.positive_infinite])),
|
| 330 |
+
Q.real_elements: (set([Q.complex_elements, Q.real_elements]), set([])),
|
| 331 |
+
Q.singular: (set([Q.singular]), set([Q.invertible, Q.orthogonal,
|
| 332 |
+
Q.positive_definite, Q.unitary])),
|
| 333 |
+
Q.square: (set([Q.square]), set([])),
|
| 334 |
+
Q.symmetric: (set([Q.square, Q.symmetric]), set([])),
|
| 335 |
+
Q.transcendental: (set([Q.commutative, Q.complex, Q.finite,
|
| 336 |
+
Q.transcendental]), set([Q.algebraic, Q.composite, Q.even,
|
| 337 |
+
Q.infinite, Q.integer, Q.negative_infinite, Q.odd,
|
| 338 |
+
Q.positive_infinite, Q.prime, Q.rational, Q.zero])),
|
| 339 |
+
Q.triangular: (set([Q.triangular]), set([])),
|
| 340 |
+
Q.unit_triangular: (set([Q.triangular, Q.unit_triangular]), set([])),
|
| 341 |
+
Q.unitary: (set([Q.fullrank, Q.invertible, Q.normal, Q.square,
|
| 342 |
+
Q.unitary]), set([Q.singular])),
|
| 343 |
+
Q.upper_triangular: (set([Q.triangular, Q.upper_triangular]), set([])),
|
| 344 |
+
Q.zero: (set([Q.algebraic, Q.commutative, Q.complex, Q.even,
|
| 345 |
+
Q.extended_nonnegative, Q.extended_nonpositive,
|
| 346 |
+
Q.extended_real, Q.finite, Q.hermitian, Q.integer,
|
| 347 |
+
Q.nonnegative, Q.nonpositive, Q.rational, Q.real, Q.zero]),
|
| 348 |
+
set([Q.composite, Q.extended_negative, Q.extended_nonzero,
|
| 349 |
+
Q.extended_positive, Q.imaginary, Q.infinite, Q.irrational,
|
| 350 |
+
Q.negative, Q.negative_infinite, Q.nonzero, Q.odd, Q.positive,
|
| 351 |
+
Q.positive_infinite, Q.prime, Q.transcendental])),
|
| 352 |
+
}
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/assume.py
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/cnf.py
ADDED
|
@@ -0,0 +1,445 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
+
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 |
+
lit = self.lit.apply(expr)
|
| 58 |
+
return type(self)(lit, self.is_Not)
|
| 59 |
+
|
| 60 |
+
def __invert__(self):
|
| 61 |
+
is_Not = not self.is_Not
|
| 62 |
+
return Literal(self.lit, is_Not)
|
| 63 |
+
|
| 64 |
+
def __str__(self):
|
| 65 |
+
return '{}({}, {})'.format(type(self).__name__, self.lit, self.is_Not)
|
| 66 |
+
|
| 67 |
+
__repr__ = __str__
|
| 68 |
+
|
| 69 |
+
def __eq__(self, other):
|
| 70 |
+
return self.arg == other.arg and self.is_Not == other.is_Not
|
| 71 |
+
|
| 72 |
+
def __hash__(self):
|
| 73 |
+
h = hash((type(self).__name__, self.arg, self.is_Not))
|
| 74 |
+
return h
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
class OR:
|
| 78 |
+
"""
|
| 79 |
+
A low-level implementation for Or
|
| 80 |
+
"""
|
| 81 |
+
def __init__(self, *args):
|
| 82 |
+
self._args = args
|
| 83 |
+
|
| 84 |
+
@property
|
| 85 |
+
def args(self):
|
| 86 |
+
return sorted(self._args, key=str)
|
| 87 |
+
|
| 88 |
+
def rcall(self, expr):
|
| 89 |
+
return type(self)(*[arg.rcall(expr)
|
| 90 |
+
for arg in self._args
|
| 91 |
+
])
|
| 92 |
+
|
| 93 |
+
def __invert__(self):
|
| 94 |
+
return AND(*[~arg for arg in self._args])
|
| 95 |
+
|
| 96 |
+
def __hash__(self):
|
| 97 |
+
return hash((type(self).__name__,) + tuple(self.args))
|
| 98 |
+
|
| 99 |
+
def __eq__(self, other):
|
| 100 |
+
return self.args == other.args
|
| 101 |
+
|
| 102 |
+
def __str__(self):
|
| 103 |
+
s = '(' + ' | '.join([str(arg) for arg in self.args]) + ')'
|
| 104 |
+
return s
|
| 105 |
+
|
| 106 |
+
__repr__ = __str__
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class AND:
|
| 110 |
+
"""
|
| 111 |
+
A low-level implementation for And
|
| 112 |
+
"""
|
| 113 |
+
def __init__(self, *args):
|
| 114 |
+
self._args = args
|
| 115 |
+
|
| 116 |
+
def __invert__(self):
|
| 117 |
+
return OR(*[~arg for arg in self._args])
|
| 118 |
+
|
| 119 |
+
@property
|
| 120 |
+
def args(self):
|
| 121 |
+
return sorted(self._args, key=str)
|
| 122 |
+
|
| 123 |
+
def rcall(self, expr):
|
| 124 |
+
return type(self)(*[arg.rcall(expr)
|
| 125 |
+
for arg in self._args
|
| 126 |
+
])
|
| 127 |
+
|
| 128 |
+
def __hash__(self):
|
| 129 |
+
return hash((type(self).__name__,) + tuple(self.args))
|
| 130 |
+
|
| 131 |
+
def __eq__(self, other):
|
| 132 |
+
return self.args == other.args
|
| 133 |
+
|
| 134 |
+
def __str__(self):
|
| 135 |
+
s = '('+' & '.join([str(arg) for arg in self.args])+')'
|
| 136 |
+
return s
|
| 137 |
+
|
| 138 |
+
__repr__ = __str__
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def to_NNF(expr, composite_map=None):
|
| 142 |
+
"""
|
| 143 |
+
Generates the Negation Normal Form of any boolean expression in terms
|
| 144 |
+
of AND, OR, and Literal objects.
|
| 145 |
+
|
| 146 |
+
Examples
|
| 147 |
+
========
|
| 148 |
+
|
| 149 |
+
>>> from sympy import Q, Eq
|
| 150 |
+
>>> from sympy.assumptions.cnf import to_NNF
|
| 151 |
+
>>> from sympy.abc import x, y
|
| 152 |
+
>>> expr = Q.even(x) & ~Q.positive(x)
|
| 153 |
+
>>> to_NNF(expr)
|
| 154 |
+
(Literal(Q.even(x), False) & Literal(Q.positive(x), True))
|
| 155 |
+
|
| 156 |
+
Supported boolean objects are converted to corresponding predicates.
|
| 157 |
+
|
| 158 |
+
>>> to_NNF(Eq(x, y))
|
| 159 |
+
Literal(Q.eq(x, y), False)
|
| 160 |
+
|
| 161 |
+
If ``composite_map`` argument is given, ``to_NNF`` decomposes the
|
| 162 |
+
specified predicate into a combination of primitive predicates.
|
| 163 |
+
|
| 164 |
+
>>> cmap = {Q.nonpositive: Q.negative | Q.zero}
|
| 165 |
+
>>> to_NNF(Q.nonpositive, cmap)
|
| 166 |
+
(Literal(Q.negative, False) | Literal(Q.zero, False))
|
| 167 |
+
>>> to_NNF(Q.nonpositive(x), cmap)
|
| 168 |
+
(Literal(Q.negative(x), False) | Literal(Q.zero(x), False))
|
| 169 |
+
"""
|
| 170 |
+
from sympy.assumptions.ask import Q
|
| 171 |
+
|
| 172 |
+
if composite_map is None:
|
| 173 |
+
composite_map = {}
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
binrelpreds = {Eq: Q.eq, Ne: Q.ne, Gt: Q.gt, Lt: Q.lt, Ge: Q.ge, Le: Q.le}
|
| 177 |
+
if type(expr) in binrelpreds:
|
| 178 |
+
pred = binrelpreds[type(expr)]
|
| 179 |
+
expr = pred(*expr.args)
|
| 180 |
+
|
| 181 |
+
if isinstance(expr, Not):
|
| 182 |
+
arg = expr.args[0]
|
| 183 |
+
tmp = to_NNF(arg, composite_map) # Strategy: negate the NNF of expr
|
| 184 |
+
return ~tmp
|
| 185 |
+
|
| 186 |
+
if isinstance(expr, Or):
|
| 187 |
+
return OR(*[to_NNF(x, composite_map) for x in Or.make_args(expr)])
|
| 188 |
+
|
| 189 |
+
if isinstance(expr, And):
|
| 190 |
+
return AND(*[to_NNF(x, composite_map) for x in And.make_args(expr)])
|
| 191 |
+
|
| 192 |
+
if isinstance(expr, Nand):
|
| 193 |
+
tmp = AND(*[to_NNF(x, composite_map) for x in expr.args])
|
| 194 |
+
return ~tmp
|
| 195 |
+
|
| 196 |
+
if isinstance(expr, Nor):
|
| 197 |
+
tmp = OR(*[to_NNF(x, composite_map) for x in expr.args])
|
| 198 |
+
return ~tmp
|
| 199 |
+
|
| 200 |
+
if isinstance(expr, Xor):
|
| 201 |
+
cnfs = []
|
| 202 |
+
for i in range(0, len(expr.args) + 1, 2):
|
| 203 |
+
for neg in combinations(expr.args, i):
|
| 204 |
+
clause = [~to_NNF(s, composite_map) if s in neg else to_NNF(s, composite_map)
|
| 205 |
+
for s in expr.args]
|
| 206 |
+
cnfs.append(OR(*clause))
|
| 207 |
+
return AND(*cnfs)
|
| 208 |
+
|
| 209 |
+
if isinstance(expr, Xnor):
|
| 210 |
+
cnfs = []
|
| 211 |
+
for i in range(0, len(expr.args) + 1, 2):
|
| 212 |
+
for neg in combinations(expr.args, i):
|
| 213 |
+
clause = [~to_NNF(s, composite_map) if s in neg else to_NNF(s, composite_map)
|
| 214 |
+
for s in expr.args]
|
| 215 |
+
cnfs.append(OR(*clause))
|
| 216 |
+
return ~AND(*cnfs)
|
| 217 |
+
|
| 218 |
+
if isinstance(expr, Implies):
|
| 219 |
+
L, R = to_NNF(expr.args[0], composite_map), to_NNF(expr.args[1], composite_map)
|
| 220 |
+
return OR(~L, R)
|
| 221 |
+
|
| 222 |
+
if isinstance(expr, Equivalent):
|
| 223 |
+
cnfs = []
|
| 224 |
+
for a, b in zip_longest(expr.args, expr.args[1:], fillvalue=expr.args[0]):
|
| 225 |
+
a = to_NNF(a, composite_map)
|
| 226 |
+
b = to_NNF(b, composite_map)
|
| 227 |
+
cnfs.append(OR(~a, b))
|
| 228 |
+
return AND(*cnfs)
|
| 229 |
+
|
| 230 |
+
if isinstance(expr, ITE):
|
| 231 |
+
L = to_NNF(expr.args[0], composite_map)
|
| 232 |
+
M = to_NNF(expr.args[1], composite_map)
|
| 233 |
+
R = to_NNF(expr.args[2], composite_map)
|
| 234 |
+
return AND(OR(~L, M), OR(L, R))
|
| 235 |
+
|
| 236 |
+
if isinstance(expr, AppliedPredicate):
|
| 237 |
+
pred, args = expr.function, expr.arguments
|
| 238 |
+
newpred = composite_map.get(pred, None)
|
| 239 |
+
if newpred is not None:
|
| 240 |
+
return to_NNF(newpred.rcall(*args), composite_map)
|
| 241 |
+
|
| 242 |
+
if isinstance(expr, Predicate):
|
| 243 |
+
newpred = composite_map.get(expr, None)
|
| 244 |
+
if newpred is not None:
|
| 245 |
+
return to_NNF(newpred, composite_map)
|
| 246 |
+
|
| 247 |
+
return Literal(expr)
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
def distribute_AND_over_OR(expr):
|
| 251 |
+
"""
|
| 252 |
+
Distributes AND over OR in the NNF expression.
|
| 253 |
+
Returns the result( Conjunctive Normal Form of expression)
|
| 254 |
+
as a CNF object.
|
| 255 |
+
"""
|
| 256 |
+
if not isinstance(expr, (AND, OR)):
|
| 257 |
+
tmp = set()
|
| 258 |
+
tmp.add(frozenset((expr,)))
|
| 259 |
+
return CNF(tmp)
|
| 260 |
+
|
| 261 |
+
if isinstance(expr, OR):
|
| 262 |
+
return CNF.all_or(*[distribute_AND_over_OR(arg)
|
| 263 |
+
for arg in expr._args])
|
| 264 |
+
|
| 265 |
+
if isinstance(expr, AND):
|
| 266 |
+
return CNF.all_and(*[distribute_AND_over_OR(arg)
|
| 267 |
+
for arg in expr._args])
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
class CNF:
|
| 271 |
+
"""
|
| 272 |
+
Class to represent CNF of a Boolean expression.
|
| 273 |
+
Consists of set of clauses, which themselves are stored as
|
| 274 |
+
frozenset of Literal objects.
|
| 275 |
+
|
| 276 |
+
Examples
|
| 277 |
+
========
|
| 278 |
+
|
| 279 |
+
>>> from sympy import Q
|
| 280 |
+
>>> from sympy.assumptions.cnf import CNF
|
| 281 |
+
>>> from sympy.abc import x
|
| 282 |
+
>>> cnf = CNF.from_prop(Q.real(x) & ~Q.zero(x))
|
| 283 |
+
>>> cnf.clauses
|
| 284 |
+
{frozenset({Literal(Q.zero(x), True)}),
|
| 285 |
+
frozenset({Literal(Q.negative(x), False),
|
| 286 |
+
Literal(Q.positive(x), False), Literal(Q.zero(x), False)})}
|
| 287 |
+
"""
|
| 288 |
+
def __init__(self, clauses=None):
|
| 289 |
+
if not clauses:
|
| 290 |
+
clauses = set()
|
| 291 |
+
self.clauses = clauses
|
| 292 |
+
|
| 293 |
+
def add(self, prop):
|
| 294 |
+
clauses = CNF.to_CNF(prop).clauses
|
| 295 |
+
self.add_clauses(clauses)
|
| 296 |
+
|
| 297 |
+
def __str__(self):
|
| 298 |
+
s = ' & '.join(
|
| 299 |
+
['(' + ' | '.join([str(lit) for lit in clause]) +')'
|
| 300 |
+
for clause in self.clauses]
|
| 301 |
+
)
|
| 302 |
+
return s
|
| 303 |
+
|
| 304 |
+
def extend(self, props):
|
| 305 |
+
for p in props:
|
| 306 |
+
self.add(p)
|
| 307 |
+
return self
|
| 308 |
+
|
| 309 |
+
def copy(self):
|
| 310 |
+
return CNF(set(self.clauses))
|
| 311 |
+
|
| 312 |
+
def add_clauses(self, clauses):
|
| 313 |
+
self.clauses |= clauses
|
| 314 |
+
|
| 315 |
+
@classmethod
|
| 316 |
+
def from_prop(cls, prop):
|
| 317 |
+
res = cls()
|
| 318 |
+
res.add(prop)
|
| 319 |
+
return res
|
| 320 |
+
|
| 321 |
+
def __iand__(self, other):
|
| 322 |
+
self.add_clauses(other.clauses)
|
| 323 |
+
return self
|
| 324 |
+
|
| 325 |
+
def all_predicates(self):
|
| 326 |
+
predicates = set()
|
| 327 |
+
for c in self.clauses:
|
| 328 |
+
predicates |= {arg.lit for arg in c}
|
| 329 |
+
return predicates
|
| 330 |
+
|
| 331 |
+
def _or(self, cnf):
|
| 332 |
+
clauses = set()
|
| 333 |
+
for a, b in product(self.clauses, cnf.clauses):
|
| 334 |
+
tmp = set(a)
|
| 335 |
+
tmp.update(b)
|
| 336 |
+
clauses.add(frozenset(tmp))
|
| 337 |
+
return CNF(clauses)
|
| 338 |
+
|
| 339 |
+
def _and(self, cnf):
|
| 340 |
+
clauses = self.clauses.union(cnf.clauses)
|
| 341 |
+
return CNF(clauses)
|
| 342 |
+
|
| 343 |
+
def _not(self):
|
| 344 |
+
clss = list(self.clauses)
|
| 345 |
+
ll = {frozenset((~x,)) for x in clss[-1]}
|
| 346 |
+
ll = CNF(ll)
|
| 347 |
+
|
| 348 |
+
for rest in clss[:-1]:
|
| 349 |
+
p = {frozenset((~x,)) for x in rest}
|
| 350 |
+
ll = ll._or(CNF(p))
|
| 351 |
+
return ll
|
| 352 |
+
|
| 353 |
+
def rcall(self, expr):
|
| 354 |
+
clause_list = []
|
| 355 |
+
for clause in self.clauses:
|
| 356 |
+
lits = [arg.rcall(expr) for arg in clause]
|
| 357 |
+
clause_list.append(OR(*lits))
|
| 358 |
+
expr = AND(*clause_list)
|
| 359 |
+
return distribute_AND_over_OR(expr)
|
| 360 |
+
|
| 361 |
+
@classmethod
|
| 362 |
+
def all_or(cls, *cnfs):
|
| 363 |
+
b = cnfs[0].copy()
|
| 364 |
+
for rest in cnfs[1:]:
|
| 365 |
+
b = b._or(rest)
|
| 366 |
+
return b
|
| 367 |
+
|
| 368 |
+
@classmethod
|
| 369 |
+
def all_and(cls, *cnfs):
|
| 370 |
+
b = cnfs[0].copy()
|
| 371 |
+
for rest in cnfs[1:]:
|
| 372 |
+
b = b._and(rest)
|
| 373 |
+
return b
|
| 374 |
+
|
| 375 |
+
@classmethod
|
| 376 |
+
def to_CNF(cls, expr):
|
| 377 |
+
from sympy.assumptions.facts import get_composite_predicates
|
| 378 |
+
expr = to_NNF(expr, get_composite_predicates())
|
| 379 |
+
expr = distribute_AND_over_OR(expr)
|
| 380 |
+
return expr
|
| 381 |
+
|
| 382 |
+
@classmethod
|
| 383 |
+
def CNF_to_cnf(cls, cnf):
|
| 384 |
+
"""
|
| 385 |
+
Converts CNF object to SymPy's boolean expression
|
| 386 |
+
retaining the form of expression.
|
| 387 |
+
"""
|
| 388 |
+
def remove_literal(arg):
|
| 389 |
+
return Not(arg.lit) if arg.is_Not else arg.lit
|
| 390 |
+
|
| 391 |
+
return And(*(Or(*(remove_literal(arg) for arg in clause)) for clause in cnf.clauses))
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
class EncodedCNF:
|
| 395 |
+
"""
|
| 396 |
+
Class for encoding the CNF expression.
|
| 397 |
+
"""
|
| 398 |
+
def __init__(self, data=None, encoding=None):
|
| 399 |
+
if not data and not encoding:
|
| 400 |
+
data = []
|
| 401 |
+
encoding = {}
|
| 402 |
+
self.data = data
|
| 403 |
+
self.encoding = encoding
|
| 404 |
+
self._symbols = list(encoding.keys())
|
| 405 |
+
|
| 406 |
+
def from_cnf(self, cnf):
|
| 407 |
+
self._symbols = list(cnf.all_predicates())
|
| 408 |
+
n = len(self._symbols)
|
| 409 |
+
self.encoding = dict(zip(self._symbols, range(1, n + 1)))
|
| 410 |
+
self.data = [self.encode(clause) for clause in cnf.clauses]
|
| 411 |
+
|
| 412 |
+
@property
|
| 413 |
+
def symbols(self):
|
| 414 |
+
return self._symbols
|
| 415 |
+
|
| 416 |
+
@property
|
| 417 |
+
def variables(self):
|
| 418 |
+
return range(1, len(self._symbols) + 1)
|
| 419 |
+
|
| 420 |
+
def copy(self):
|
| 421 |
+
new_data = [set(clause) for clause in self.data]
|
| 422 |
+
return EncodedCNF(new_data, dict(self.encoding))
|
| 423 |
+
|
| 424 |
+
def add_prop(self, prop):
|
| 425 |
+
cnf = CNF.from_prop(prop)
|
| 426 |
+
self.add_from_cnf(cnf)
|
| 427 |
+
|
| 428 |
+
def add_from_cnf(self, cnf):
|
| 429 |
+
clauses = [self.encode(clause) for clause in cnf.clauses]
|
| 430 |
+
self.data += clauses
|
| 431 |
+
|
| 432 |
+
def encode_arg(self, arg):
|
| 433 |
+
literal = arg.lit
|
| 434 |
+
value = self.encoding.get(literal, None)
|
| 435 |
+
if value is None:
|
| 436 |
+
n = len(self._symbols)
|
| 437 |
+
self._symbols.append(literal)
|
| 438 |
+
value = self.encoding[literal] = n + 1
|
| 439 |
+
if arg.is_Not:
|
| 440 |
+
return -value
|
| 441 |
+
else:
|
| 442 |
+
return value
|
| 443 |
+
|
| 444 |
+
def encode(self, clause):
|
| 445 |
+
return {self.encode_arg(arg) if not arg.lit == S.false else 0 for arg in clause}
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
get_number_facts(x),
|
| 60 |
+
get_matrix_facts(x)
|
| 61 |
+
)
|
| 62 |
+
return fact
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
@cacheit
|
| 66 |
+
def get_number_facts(x = None):
|
| 67 |
+
"""
|
| 68 |
+
Facts between unary number predicates.
|
| 69 |
+
|
| 70 |
+
Parameters
|
| 71 |
+
==========
|
| 72 |
+
|
| 73 |
+
x : Symbol, optional
|
| 74 |
+
Placeholder symbol for unary facts. Default is ``Symbol('x')``.
|
| 75 |
+
|
| 76 |
+
Returns
|
| 77 |
+
=======
|
| 78 |
+
|
| 79 |
+
fact : Known facts in conjugated normal form.
|
| 80 |
+
|
| 81 |
+
"""
|
| 82 |
+
if x is None:
|
| 83 |
+
x = Symbol('x')
|
| 84 |
+
|
| 85 |
+
fact = And(
|
| 86 |
+
# primitive predicates for extended real exclude each other.
|
| 87 |
+
Exclusive(Q.negative_infinite(x), Q.negative(x), Q.zero(x),
|
| 88 |
+
Q.positive(x), Q.positive_infinite(x)),
|
| 89 |
+
|
| 90 |
+
# build complex plane
|
| 91 |
+
Exclusive(Q.real(x), Q.imaginary(x)),
|
| 92 |
+
Implies(Q.real(x) | Q.imaginary(x), Q.complex(x)),
|
| 93 |
+
|
| 94 |
+
# other subsets of complex
|
| 95 |
+
Exclusive(Q.transcendental(x), Q.algebraic(x)),
|
| 96 |
+
Equivalent(Q.real(x), Q.rational(x) | Q.irrational(x)),
|
| 97 |
+
Exclusive(Q.irrational(x), Q.rational(x)),
|
| 98 |
+
Implies(Q.rational(x), Q.algebraic(x)),
|
| 99 |
+
|
| 100 |
+
# integers
|
| 101 |
+
Exclusive(Q.even(x), Q.odd(x)),
|
| 102 |
+
Implies(Q.integer(x), Q.rational(x)),
|
| 103 |
+
Implies(Q.zero(x), Q.even(x)),
|
| 104 |
+
Exclusive(Q.composite(x), Q.prime(x)),
|
| 105 |
+
Implies(Q.composite(x) | Q.prime(x), Q.integer(x) & Q.positive(x)),
|
| 106 |
+
Implies(Q.even(x) & Q.positive(x) & ~Q.prime(x), Q.composite(x)),
|
| 107 |
+
|
| 108 |
+
# hermitian and antihermitian
|
| 109 |
+
Implies(Q.real(x), Q.hermitian(x)),
|
| 110 |
+
Implies(Q.imaginary(x), Q.antihermitian(x)),
|
| 111 |
+
Implies(Q.zero(x), Q.hermitian(x) | Q.antihermitian(x)),
|
| 112 |
+
|
| 113 |
+
# define finity and infinity, and build extended real line
|
| 114 |
+
Exclusive(Q.infinite(x), Q.finite(x)),
|
| 115 |
+
Implies(Q.complex(x), Q.finite(x)),
|
| 116 |
+
Implies(Q.negative_infinite(x) | Q.positive_infinite(x), Q.infinite(x)),
|
| 117 |
+
|
| 118 |
+
# commutativity
|
| 119 |
+
Implies(Q.finite(x) | Q.infinite(x), Q.commutative(x)),
|
| 120 |
+
)
|
| 121 |
+
return fact
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
@cacheit
|
| 125 |
+
def get_matrix_facts(x = None):
|
| 126 |
+
"""
|
| 127 |
+
Facts between unary matrix predicates.
|
| 128 |
+
|
| 129 |
+
Parameters
|
| 130 |
+
==========
|
| 131 |
+
|
| 132 |
+
x : Symbol, optional
|
| 133 |
+
Placeholder symbol for unary facts. Default is ``Symbol('x')``.
|
| 134 |
+
|
| 135 |
+
Returns
|
| 136 |
+
=======
|
| 137 |
+
|
| 138 |
+
fact : Known facts in conjugated normal form.
|
| 139 |
+
|
| 140 |
+
"""
|
| 141 |
+
if x is None:
|
| 142 |
+
x = Symbol('x')
|
| 143 |
+
|
| 144 |
+
fact = And(
|
| 145 |
+
# matrices
|
| 146 |
+
Implies(Q.orthogonal(x), Q.positive_definite(x)),
|
| 147 |
+
Implies(Q.orthogonal(x), Q.unitary(x)),
|
| 148 |
+
Implies(Q.unitary(x) & Q.real_elements(x), Q.orthogonal(x)),
|
| 149 |
+
Implies(Q.unitary(x), Q.normal(x)),
|
| 150 |
+
Implies(Q.unitary(x), Q.invertible(x)),
|
| 151 |
+
Implies(Q.normal(x), Q.square(x)),
|
| 152 |
+
Implies(Q.diagonal(x), Q.normal(x)),
|
| 153 |
+
Implies(Q.positive_definite(x), Q.invertible(x)),
|
| 154 |
+
Implies(Q.diagonal(x), Q.upper_triangular(x)),
|
| 155 |
+
Implies(Q.diagonal(x), Q.lower_triangular(x)),
|
| 156 |
+
Implies(Q.lower_triangular(x), Q.triangular(x)),
|
| 157 |
+
Implies(Q.upper_triangular(x), Q.triangular(x)),
|
| 158 |
+
Implies(Q.triangular(x), Q.upper_triangular(x) | Q.lower_triangular(x)),
|
| 159 |
+
Implies(Q.upper_triangular(x) & Q.lower_triangular(x), Q.diagonal(x)),
|
| 160 |
+
Implies(Q.diagonal(x), Q.symmetric(x)),
|
| 161 |
+
Implies(Q.unit_triangular(x), Q.triangular(x)),
|
| 162 |
+
Implies(Q.invertible(x), Q.fullrank(x)),
|
| 163 |
+
Implies(Q.invertible(x), Q.square(x)),
|
| 164 |
+
Implies(Q.symmetric(x), Q.square(x)),
|
| 165 |
+
Implies(Q.fullrank(x) & Q.square(x), Q.invertible(x)),
|
| 166 |
+
Equivalent(Q.invertible(x), ~Q.singular(x)),
|
| 167 |
+
Implies(Q.integer_elements(x), Q.real_elements(x)),
|
| 168 |
+
Implies(Q.real_elements(x), Q.complex_elements(x)),
|
| 169 |
+
)
|
| 170 |
+
return fact
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def generate_known_facts_dict(keys, fact):
|
| 175 |
+
"""
|
| 176 |
+
Computes and returns a dictionary which contains the relations between
|
| 177 |
+
unary predicates.
|
| 178 |
+
|
| 179 |
+
Each key is a predicate, and item is two groups of predicates.
|
| 180 |
+
First group contains the predicates which are implied by the key, and
|
| 181 |
+
second group contains the predicates which are rejected by the key.
|
| 182 |
+
|
| 183 |
+
All predicates in *keys* and *fact* must be unary and have same placeholder
|
| 184 |
+
symbol.
|
| 185 |
+
|
| 186 |
+
Parameters
|
| 187 |
+
==========
|
| 188 |
+
|
| 189 |
+
keys : list of AppliedPredicate instances.
|
| 190 |
+
|
| 191 |
+
fact : Fact between predicates in conjugated normal form.
|
| 192 |
+
|
| 193 |
+
Examples
|
| 194 |
+
========
|
| 195 |
+
|
| 196 |
+
>>> from sympy import Q, And, Implies
|
| 197 |
+
>>> from sympy.assumptions.facts import generate_known_facts_dict
|
| 198 |
+
>>> from sympy.abc import x
|
| 199 |
+
>>> keys = [Q.even(x), Q.odd(x), Q.zero(x)]
|
| 200 |
+
>>> fact = And(Implies(Q.even(x), ~Q.odd(x)),
|
| 201 |
+
... Implies(Q.zero(x), Q.even(x)))
|
| 202 |
+
>>> generate_known_facts_dict(keys, fact)
|
| 203 |
+
{Q.even: ({Q.even}, {Q.odd}),
|
| 204 |
+
Q.odd: ({Q.odd}, {Q.even, Q.zero}),
|
| 205 |
+
Q.zero: ({Q.even, Q.zero}, {Q.odd})}
|
| 206 |
+
"""
|
| 207 |
+
fact_cnf = to_cnf(fact)
|
| 208 |
+
mapping = single_fact_lookup(keys, fact_cnf)
|
| 209 |
+
|
| 210 |
+
ret = {}
|
| 211 |
+
for key, value in mapping.items():
|
| 212 |
+
implied = set()
|
| 213 |
+
rejected = set()
|
| 214 |
+
for expr in value:
|
| 215 |
+
if isinstance(expr, AppliedPredicate):
|
| 216 |
+
implied.add(expr.function)
|
| 217 |
+
elif isinstance(expr, Not):
|
| 218 |
+
pred = expr.args[0]
|
| 219 |
+
rejected.add(pred.function)
|
| 220 |
+
ret[key.function] = (implied, rejected)
|
| 221 |
+
return ret
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
@cacheit
|
| 225 |
+
def get_known_facts_keys():
|
| 226 |
+
"""
|
| 227 |
+
Return every unary predicates registered to ``Q``.
|
| 228 |
+
|
| 229 |
+
This function is used to generate the keys for
|
| 230 |
+
``generate_known_facts_dict``.
|
| 231 |
+
|
| 232 |
+
"""
|
| 233 |
+
# exclude polyadic predicates
|
| 234 |
+
exclude = {Q.eq, Q.ne, Q.gt, Q.lt, Q.ge, Q.le}
|
| 235 |
+
|
| 236 |
+
result = []
|
| 237 |
+
for attr in Q.__class__.__dict__:
|
| 238 |
+
if attr.startswith('__'):
|
| 239 |
+
continue
|
| 240 |
+
pred = getattr(Q, attr)
|
| 241 |
+
if pred in exclude:
|
| 242 |
+
continue
|
| 243 |
+
result.append(pred)
|
| 244 |
+
return result
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def single_fact_lookup(known_facts_keys, known_facts_cnf):
|
| 248 |
+
# Return the dictionary for quick lookup of single fact
|
| 249 |
+
mapping = {}
|
| 250 |
+
for key in known_facts_keys:
|
| 251 |
+
mapping[key] = {key}
|
| 252 |
+
for other_key in known_facts_keys:
|
| 253 |
+
if other_key != key:
|
| 254 |
+
if ask_full_inference(other_key, key, known_facts_cnf):
|
| 255 |
+
mapping[key].add(other_key)
|
| 256 |
+
if ask_full_inference(~other_key, key, known_facts_cnf):
|
| 257 |
+
mapping[key].add(~other_key)
|
| 258 |
+
return mapping
|
| 259 |
+
|
| 260 |
+
|
| 261 |
+
def ask_full_inference(proposition, assumptions, known_facts_cnf):
|
| 262 |
+
"""
|
| 263 |
+
Method for inferring properties about objects.
|
| 264 |
+
|
| 265 |
+
"""
|
| 266 |
+
if not satisfiable(And(known_facts_cnf, assumptions, proposition)):
|
| 267 |
+
return False
|
| 268 |
+
if not satisfiable(And(known_facts_cnf, assumptions, Not(proposition))):
|
| 269 |
+
return True
|
| 270 |
+
return None
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/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__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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 Expr, 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 |
+
possible_zero = False
|
| 155 |
+
for arg in expr.args:
|
| 156 |
+
_bounded = ask(Q.finite(arg), assumptions)
|
| 157 |
+
if _bounded:
|
| 158 |
+
if ask(Q.zero(arg), assumptions) is not False:
|
| 159 |
+
if result is False:
|
| 160 |
+
return None
|
| 161 |
+
possible_zero = True
|
| 162 |
+
elif _bounded is None:
|
| 163 |
+
if result is None:
|
| 164 |
+
return None
|
| 165 |
+
if ask(Q.extended_nonzero(arg), assumptions) is None:
|
| 166 |
+
return None
|
| 167 |
+
if result is not False:
|
| 168 |
+
result = None
|
| 169 |
+
else:
|
| 170 |
+
if possible_zero:
|
| 171 |
+
return None
|
| 172 |
+
result = False
|
| 173 |
+
return result
|
| 174 |
+
|
| 175 |
+
@FinitePredicate.register(Pow)
|
| 176 |
+
def _(expr, assumptions):
|
| 177 |
+
"""
|
| 178 |
+
* Unbounded ** NonZero -> Unbounded
|
| 179 |
+
|
| 180 |
+
* Bounded ** Bounded -> Bounded
|
| 181 |
+
|
| 182 |
+
* Abs()<=1 ** Positive -> Bounded
|
| 183 |
+
|
| 184 |
+
* Abs()>=1 ** Negative -> Bounded
|
| 185 |
+
|
| 186 |
+
* Otherwise unknown
|
| 187 |
+
"""
|
| 188 |
+
if expr.base == E:
|
| 189 |
+
return ask(Q.finite(expr.exp), assumptions)
|
| 190 |
+
|
| 191 |
+
base_bounded = ask(Q.finite(expr.base), assumptions)
|
| 192 |
+
exp_bounded = ask(Q.finite(expr.exp), assumptions)
|
| 193 |
+
if base_bounded is None and exp_bounded is None: # Common Case
|
| 194 |
+
return None
|
| 195 |
+
if base_bounded is False and ask(Q.extended_nonzero(expr.exp), assumptions):
|
| 196 |
+
return False
|
| 197 |
+
if base_bounded and exp_bounded:
|
| 198 |
+
is_base_zero = ask(Q.zero(expr.base),assumptions)
|
| 199 |
+
is_exp_negative = ask(Q.negative(expr.exp),assumptions)
|
| 200 |
+
if is_base_zero is True and is_exp_negative is True:
|
| 201 |
+
return False
|
| 202 |
+
if is_base_zero is not False and is_exp_negative is not False:
|
| 203 |
+
return None
|
| 204 |
+
return True
|
| 205 |
+
if (abs(expr.base) <= 1) == True and ask(Q.extended_positive(expr.exp), assumptions):
|
| 206 |
+
return True
|
| 207 |
+
if (abs(expr.base) >= 1) == True and ask(Q.extended_negative(expr.exp), assumptions):
|
| 208 |
+
return True
|
| 209 |
+
if (abs(expr.base) >= 1) == True and exp_bounded is False:
|
| 210 |
+
return False
|
| 211 |
+
return None
|
| 212 |
+
|
| 213 |
+
@FinitePredicate.register(exp)
|
| 214 |
+
def _(expr, assumptions):
|
| 215 |
+
return ask(Q.finite(expr.exp), assumptions)
|
| 216 |
+
|
| 217 |
+
@FinitePredicate.register(log)
|
| 218 |
+
def _(expr, assumptions):
|
| 219 |
+
# After complex -> finite fact is registered to new assumption system,
|
| 220 |
+
# querying Q.infinite may be removed.
|
| 221 |
+
if ask(Q.infinite(expr.args[0]), assumptions):
|
| 222 |
+
return False
|
| 223 |
+
return ask(~Q.zero(expr.args[0]), assumptions)
|
| 224 |
+
|
| 225 |
+
@FinitePredicate.register_many(cos, sin, Number, Pi, Exp1, GoldenRatio,
|
| 226 |
+
TribonacciConstant, ImaginaryUnit, sign)
|
| 227 |
+
def _(expr, assumptions):
|
| 228 |
+
return True
|
| 229 |
+
|
| 230 |
+
@FinitePredicate.register_many(ComplexInfinity, Infinity, NegativeInfinity)
|
| 231 |
+
def _(expr, assumptions):
|
| 232 |
+
return False
|
| 233 |
+
|
| 234 |
+
@FinitePredicate.register(NaN)
|
| 235 |
+
def _(expr, assumptions):
|
| 236 |
+
return None
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
# InfinitePredicate
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
@InfinitePredicate.register(Expr)
|
| 243 |
+
def _(expr, assumptions):
|
| 244 |
+
is_finite = Q.finite(expr)._eval_ask(assumptions)
|
| 245 |
+
if is_finite is None:
|
| 246 |
+
return None
|
| 247 |
+
return not is_finite
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
# PositiveInfinitePredicate
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
@PositiveInfinitePredicate.register(Infinity)
|
| 254 |
+
def _(expr, assumptions):
|
| 255 |
+
return True
|
| 256 |
+
|
| 257 |
+
|
| 258 |
+
@PositiveInfinitePredicate.register_many(NegativeInfinity, ComplexInfinity)
|
| 259 |
+
def _(expr, assumptions):
|
| 260 |
+
return False
|
| 261 |
+
|
| 262 |
+
|
| 263 |
+
# NegativeInfinitePredicate
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
@NegativeInfinitePredicate.register(NegativeInfinity)
|
| 267 |
+
def _(expr, assumptions):
|
| 268 |
+
return True
|
| 269 |
+
|
| 270 |
+
|
| 271 |
+
@NegativeInfinitePredicate.register_many(Infinity, ComplexInfinity)
|
| 272 |
+
def _(expr, assumptions):
|
| 273 |
+
return False
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/common.py
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
This module defines base class for handlers and some core handlers:
|
| 3 |
+
``Q.commutative`` and ``Q.is_true``.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from sympy.assumptions import Q, ask, AppliedPredicate
|
| 7 |
+
from sympy.core import Basic, Symbol
|
| 8 |
+
from sympy.core.logic import _fuzzy_group, fuzzy_and, fuzzy_or
|
| 9 |
+
from sympy.core.numbers import NaN, Number
|
| 10 |
+
from sympy.logic.boolalg import (And, BooleanTrue, BooleanFalse, conjuncts,
|
| 11 |
+
Equivalent, Implies, Not, Or)
|
| 12 |
+
from sympy.utilities.exceptions import sympy_deprecation_warning
|
| 13 |
+
|
| 14 |
+
from ..predicates.common import CommutativePredicate, IsTruePredicate
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class AskHandler:
|
| 18 |
+
"""Base class that all Ask Handlers must inherit."""
|
| 19 |
+
def __new__(cls, *args, **kwargs):
|
| 20 |
+
sympy_deprecation_warning(
|
| 21 |
+
"""
|
| 22 |
+
The AskHandler system is deprecated. The AskHandler class should
|
| 23 |
+
be replaced with the multipledispatch handler of Predicate
|
| 24 |
+
""",
|
| 25 |
+
deprecated_since_version="1.8",
|
| 26 |
+
active_deprecations_target='deprecated-askhandler',
|
| 27 |
+
)
|
| 28 |
+
return super().__new__(cls, *args, **kwargs)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class CommonHandler(AskHandler):
|
| 32 |
+
# Deprecated
|
| 33 |
+
"""Defines some useful methods common to most Handlers. """
|
| 34 |
+
|
| 35 |
+
@staticmethod
|
| 36 |
+
def AlwaysTrue(expr, assumptions):
|
| 37 |
+
return True
|
| 38 |
+
|
| 39 |
+
@staticmethod
|
| 40 |
+
def AlwaysFalse(expr, assumptions):
|
| 41 |
+
return False
|
| 42 |
+
|
| 43 |
+
@staticmethod
|
| 44 |
+
def AlwaysNone(expr, assumptions):
|
| 45 |
+
return None
|
| 46 |
+
|
| 47 |
+
NaN = AlwaysFalse
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# CommutativePredicate
|
| 51 |
+
|
| 52 |
+
@CommutativePredicate.register(Symbol)
|
| 53 |
+
def _(expr, assumptions):
|
| 54 |
+
"""Objects are expected to be commutative unless otherwise stated"""
|
| 55 |
+
assumps = conjuncts(assumptions)
|
| 56 |
+
if expr.is_commutative is not None:
|
| 57 |
+
return expr.is_commutative and not ~Q.commutative(expr) in assumps
|
| 58 |
+
if Q.commutative(expr) in assumps:
|
| 59 |
+
return True
|
| 60 |
+
elif ~Q.commutative(expr) in assumps:
|
| 61 |
+
return False
|
| 62 |
+
return True
|
| 63 |
+
|
| 64 |
+
@CommutativePredicate.register(Basic)
|
| 65 |
+
def _(expr, assumptions):
|
| 66 |
+
for arg in expr.args:
|
| 67 |
+
if not ask(Q.commutative(arg), assumptions):
|
| 68 |
+
return False
|
| 69 |
+
return True
|
| 70 |
+
|
| 71 |
+
@CommutativePredicate.register(Number)
|
| 72 |
+
def _(expr, assumptions):
|
| 73 |
+
return True
|
| 74 |
+
|
| 75 |
+
@CommutativePredicate.register(NaN)
|
| 76 |
+
def _(expr, assumptions):
|
| 77 |
+
return True
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# IsTruePredicate
|
| 81 |
+
|
| 82 |
+
@IsTruePredicate.register(bool)
|
| 83 |
+
def _(expr, assumptions):
|
| 84 |
+
return expr
|
| 85 |
+
|
| 86 |
+
@IsTruePredicate.register(BooleanTrue)
|
| 87 |
+
def _(expr, assumptions):
|
| 88 |
+
return True
|
| 89 |
+
|
| 90 |
+
@IsTruePredicate.register(BooleanFalse)
|
| 91 |
+
def _(expr, assumptions):
|
| 92 |
+
return False
|
| 93 |
+
|
| 94 |
+
@IsTruePredicate.register(AppliedPredicate)
|
| 95 |
+
def _(expr, assumptions):
|
| 96 |
+
return ask(expr, assumptions)
|
| 97 |
+
|
| 98 |
+
@IsTruePredicate.register(Not)
|
| 99 |
+
def _(expr, assumptions):
|
| 100 |
+
arg = expr.args[0]
|
| 101 |
+
if arg.is_Symbol:
|
| 102 |
+
# symbol used as abstract boolean object
|
| 103 |
+
return None
|
| 104 |
+
value = ask(arg, assumptions=assumptions)
|
| 105 |
+
if value in (True, False):
|
| 106 |
+
return not value
|
| 107 |
+
else:
|
| 108 |
+
return None
|
| 109 |
+
|
| 110 |
+
@IsTruePredicate.register(Or)
|
| 111 |
+
def _(expr, assumptions):
|
| 112 |
+
result = False
|
| 113 |
+
for arg in expr.args:
|
| 114 |
+
p = ask(arg, assumptions=assumptions)
|
| 115 |
+
if p is True:
|
| 116 |
+
return True
|
| 117 |
+
if p is None:
|
| 118 |
+
result = None
|
| 119 |
+
return result
|
| 120 |
+
|
| 121 |
+
@IsTruePredicate.register(And)
|
| 122 |
+
def _(expr, assumptions):
|
| 123 |
+
result = True
|
| 124 |
+
for arg in expr.args:
|
| 125 |
+
p = ask(arg, assumptions=assumptions)
|
| 126 |
+
if p is False:
|
| 127 |
+
return False
|
| 128 |
+
if p is None:
|
| 129 |
+
result = None
|
| 130 |
+
return result
|
| 131 |
+
|
| 132 |
+
@IsTruePredicate.register(Implies)
|
| 133 |
+
def _(expr, assumptions):
|
| 134 |
+
p, q = expr.args
|
| 135 |
+
return ask(~p | q, assumptions=assumptions)
|
| 136 |
+
|
| 137 |
+
@IsTruePredicate.register(Equivalent)
|
| 138 |
+
def _(expr, assumptions):
|
| 139 |
+
p, q = expr.args
|
| 140 |
+
pt = ask(p, assumptions=assumptions)
|
| 141 |
+
if pt is None:
|
| 142 |
+
return None
|
| 143 |
+
qt = ask(q, assumptions=assumptions)
|
| 144 |
+
if qt is None:
|
| 145 |
+
return None
|
| 146 |
+
return pt == qt
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
#### Helper methods
|
| 150 |
+
def test_closed_group(expr, assumptions, key):
|
| 151 |
+
"""
|
| 152 |
+
Test for membership in a group with respect
|
| 153 |
+
to the current operation.
|
| 154 |
+
"""
|
| 155 |
+
return _fuzzy_group(
|
| 156 |
+
(ask(key(a), assumptions) for a in expr.args), quick_exit=True)
|
| 157 |
+
|
| 158 |
+
def ask_all(*queries, assumptions):
|
| 159 |
+
return fuzzy_and(
|
| 160 |
+
(ask(query, assumptions) for query in queries))
|
| 161 |
+
|
| 162 |
+
def ask_any(*queries, assumptions):
|
| 163 |
+
return fuzzy_or(
|
| 164 |
+
(ask(query, assumptions) for query in queries))
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/matrices.py
ADDED
|
@@ -0,0 +1,716 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/ntheory.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, int_valued)
|
| 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 |
+
prime_base = ask(Q.prime(expr.base), assumptions)
|
| 67 |
+
if prime_base is False:
|
| 68 |
+
return False
|
| 69 |
+
is_exp_one = ask(Q.eq(expr.exp, 1), assumptions)
|
| 70 |
+
if is_exp_one is False:
|
| 71 |
+
return False
|
| 72 |
+
if prime_base is True and is_exp_one is True:
|
| 73 |
+
return True
|
| 74 |
+
|
| 75 |
+
@PrimePredicate.register(Integer)
|
| 76 |
+
def _(expr, assumptions):
|
| 77 |
+
return isprime(expr)
|
| 78 |
+
|
| 79 |
+
@PrimePredicate.register_many(Rational, Infinity, NegativeInfinity, ImaginaryUnit)
|
| 80 |
+
def _(expr, assumptions):
|
| 81 |
+
return False
|
| 82 |
+
|
| 83 |
+
@PrimePredicate.register(Float)
|
| 84 |
+
def _(expr, assumptions):
|
| 85 |
+
return _PrimePredicate_number(expr, assumptions)
|
| 86 |
+
|
| 87 |
+
@PrimePredicate.register(NumberSymbol)
|
| 88 |
+
def _(expr, assumptions):
|
| 89 |
+
return _PrimePredicate_number(expr, assumptions)
|
| 90 |
+
|
| 91 |
+
@PrimePredicate.register(NaN)
|
| 92 |
+
def _(expr, assumptions):
|
| 93 |
+
return None
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
# CompositePredicate
|
| 97 |
+
|
| 98 |
+
@CompositePredicate.register(Expr)
|
| 99 |
+
def _(expr, assumptions):
|
| 100 |
+
ret = expr.is_composite
|
| 101 |
+
if ret is None:
|
| 102 |
+
raise MDNotImplementedError
|
| 103 |
+
return ret
|
| 104 |
+
|
| 105 |
+
@CompositePredicate.register(Basic)
|
| 106 |
+
def _(expr, assumptions):
|
| 107 |
+
_positive = ask(Q.positive(expr), assumptions)
|
| 108 |
+
if _positive:
|
| 109 |
+
_integer = ask(Q.integer(expr), assumptions)
|
| 110 |
+
if _integer:
|
| 111 |
+
_prime = ask(Q.prime(expr), assumptions)
|
| 112 |
+
if _prime is None:
|
| 113 |
+
return
|
| 114 |
+
# Positive integer which is not prime is not
|
| 115 |
+
# necessarily composite
|
| 116 |
+
_is_one = ask(Q.eq(expr, 1), assumptions)
|
| 117 |
+
if _is_one:
|
| 118 |
+
return False
|
| 119 |
+
if _is_one is None:
|
| 120 |
+
return None
|
| 121 |
+
return not _prime
|
| 122 |
+
else:
|
| 123 |
+
return _integer
|
| 124 |
+
else:
|
| 125 |
+
return _positive
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
# EvenPredicate
|
| 129 |
+
|
| 130 |
+
def _EvenPredicate_number(expr, assumptions):
|
| 131 |
+
# helper method
|
| 132 |
+
if isinstance(expr, (float, Float)):
|
| 133 |
+
if int_valued(expr):
|
| 134 |
+
return None
|
| 135 |
+
return False
|
| 136 |
+
try:
|
| 137 |
+
i = int(expr.round())
|
| 138 |
+
except TypeError:
|
| 139 |
+
return False
|
| 140 |
+
if not (expr - i).equals(0):
|
| 141 |
+
return False
|
| 142 |
+
return i % 2 == 0
|
| 143 |
+
|
| 144 |
+
@EvenPredicate.register(Expr)
|
| 145 |
+
def _(expr, assumptions):
|
| 146 |
+
ret = expr.is_even
|
| 147 |
+
if ret is None:
|
| 148 |
+
raise MDNotImplementedError
|
| 149 |
+
return ret
|
| 150 |
+
|
| 151 |
+
@EvenPredicate.register(Basic)
|
| 152 |
+
def _(expr, assumptions):
|
| 153 |
+
if expr.is_number:
|
| 154 |
+
return _EvenPredicate_number(expr, assumptions)
|
| 155 |
+
|
| 156 |
+
@EvenPredicate.register(Mul)
|
| 157 |
+
def _(expr, assumptions):
|
| 158 |
+
"""
|
| 159 |
+
Even * Integer -> Even
|
| 160 |
+
Even * Odd -> Even
|
| 161 |
+
Integer * Odd -> ?
|
| 162 |
+
Odd * Odd -> Odd
|
| 163 |
+
Even * Even -> Even
|
| 164 |
+
Integer * Integer -> Even if Integer + Integer = Odd
|
| 165 |
+
otherwise -> ?
|
| 166 |
+
"""
|
| 167 |
+
if expr.is_number:
|
| 168 |
+
return _EvenPredicate_number(expr, assumptions)
|
| 169 |
+
even, odd, irrational, acc = False, 0, False, 1
|
| 170 |
+
for arg in expr.args:
|
| 171 |
+
# check for all integers and at least one even
|
| 172 |
+
if ask(Q.integer(arg), assumptions):
|
| 173 |
+
if ask(Q.even(arg), assumptions):
|
| 174 |
+
even = True
|
| 175 |
+
elif ask(Q.odd(arg), assumptions):
|
| 176 |
+
odd += 1
|
| 177 |
+
elif not even and acc != 1:
|
| 178 |
+
if ask(Q.odd(acc + arg), assumptions):
|
| 179 |
+
even = True
|
| 180 |
+
elif ask(Q.irrational(arg), assumptions):
|
| 181 |
+
# one irrational makes the result False
|
| 182 |
+
# two makes it undefined
|
| 183 |
+
if irrational:
|
| 184 |
+
break
|
| 185 |
+
irrational = True
|
| 186 |
+
else:
|
| 187 |
+
break
|
| 188 |
+
acc = arg
|
| 189 |
+
else:
|
| 190 |
+
if irrational:
|
| 191 |
+
return False
|
| 192 |
+
if even:
|
| 193 |
+
return True
|
| 194 |
+
if odd == len(expr.args):
|
| 195 |
+
return False
|
| 196 |
+
|
| 197 |
+
@EvenPredicate.register(Add)
|
| 198 |
+
def _(expr, assumptions):
|
| 199 |
+
"""
|
| 200 |
+
Even + Odd -> Odd
|
| 201 |
+
Even + Even -> Even
|
| 202 |
+
Odd + Odd -> Even
|
| 203 |
+
|
| 204 |
+
"""
|
| 205 |
+
if expr.is_number:
|
| 206 |
+
return _EvenPredicate_number(expr, assumptions)
|
| 207 |
+
_result = True
|
| 208 |
+
for arg in expr.args:
|
| 209 |
+
if ask(Q.even(arg), assumptions):
|
| 210 |
+
pass
|
| 211 |
+
elif ask(Q.odd(arg), assumptions):
|
| 212 |
+
_result = not _result
|
| 213 |
+
else:
|
| 214 |
+
break
|
| 215 |
+
else:
|
| 216 |
+
return _result
|
| 217 |
+
|
| 218 |
+
@EvenPredicate.register(Pow)
|
| 219 |
+
def _(expr, assumptions):
|
| 220 |
+
if expr.is_number:
|
| 221 |
+
return _EvenPredicate_number(expr, assumptions)
|
| 222 |
+
if ask(Q.integer(expr.exp), assumptions):
|
| 223 |
+
if ask(Q.positive(expr.exp), assumptions):
|
| 224 |
+
return ask(Q.even(expr.base), assumptions)
|
| 225 |
+
elif ask(~Q.negative(expr.exp) & Q.odd(expr.base), assumptions):
|
| 226 |
+
return False
|
| 227 |
+
elif expr.base is S.NegativeOne:
|
| 228 |
+
return False
|
| 229 |
+
|
| 230 |
+
@EvenPredicate.register(Integer)
|
| 231 |
+
def _(expr, assumptions):
|
| 232 |
+
return not bool(expr.p & 1)
|
| 233 |
+
|
| 234 |
+
@EvenPredicate.register_many(Rational, Infinity, NegativeInfinity, ImaginaryUnit)
|
| 235 |
+
def _(expr, assumptions):
|
| 236 |
+
return False
|
| 237 |
+
|
| 238 |
+
@EvenPredicate.register(NumberSymbol)
|
| 239 |
+
def _(expr, assumptions):
|
| 240 |
+
return _EvenPredicate_number(expr, assumptions)
|
| 241 |
+
|
| 242 |
+
@EvenPredicate.register(Abs)
|
| 243 |
+
def _(expr, assumptions):
|
| 244 |
+
if ask(Q.real(expr.args[0]), assumptions):
|
| 245 |
+
return ask(Q.even(expr.args[0]), assumptions)
|
| 246 |
+
|
| 247 |
+
@EvenPredicate.register(re)
|
| 248 |
+
def _(expr, assumptions):
|
| 249 |
+
if ask(Q.real(expr.args[0]), assumptions):
|
| 250 |
+
return ask(Q.even(expr.args[0]), assumptions)
|
| 251 |
+
|
| 252 |
+
@EvenPredicate.register(im)
|
| 253 |
+
def _(expr, assumptions):
|
| 254 |
+
if ask(Q.real(expr.args[0]), assumptions):
|
| 255 |
+
return True
|
| 256 |
+
|
| 257 |
+
@EvenPredicate.register(NaN)
|
| 258 |
+
def _(expr, assumptions):
|
| 259 |
+
return None
|
| 260 |
+
|
| 261 |
+
|
| 262 |
+
# OddPredicate
|
| 263 |
+
|
| 264 |
+
@OddPredicate.register(Expr)
|
| 265 |
+
def _(expr, assumptions):
|
| 266 |
+
ret = expr.is_odd
|
| 267 |
+
if ret is None:
|
| 268 |
+
raise MDNotImplementedError
|
| 269 |
+
return ret
|
| 270 |
+
|
| 271 |
+
@OddPredicate.register(Basic)
|
| 272 |
+
def _(expr, assumptions):
|
| 273 |
+
_integer = ask(Q.integer(expr), assumptions)
|
| 274 |
+
if _integer:
|
| 275 |
+
_even = ask(Q.even(expr), assumptions)
|
| 276 |
+
if _even is None:
|
| 277 |
+
return None
|
| 278 |
+
return not _even
|
| 279 |
+
return _integer
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/order.py
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, S
|
| 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 |
+
|
| 27 |
+
if r == S.NaN or i == S.NaN:
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
# If the imaginary part can symbolically be shown to be zero then
|
| 31 |
+
# we just evaluate the real part; otherwise we evaluate the imaginary
|
| 32 |
+
# part to see if it actually evaluates to zero and if it does then
|
| 33 |
+
# we make the comparison between the real part and zero.
|
| 34 |
+
if not i:
|
| 35 |
+
r = r.evalf(2)
|
| 36 |
+
if r._prec != 1:
|
| 37 |
+
return r < 0
|
| 38 |
+
else:
|
| 39 |
+
i = i.evalf(2)
|
| 40 |
+
if i._prec != 1:
|
| 41 |
+
if i != 0:
|
| 42 |
+
return False
|
| 43 |
+
r = r.evalf(2)
|
| 44 |
+
if r._prec != 1:
|
| 45 |
+
return r < 0
|
| 46 |
+
|
| 47 |
+
@NegativePredicate.register(Basic)
|
| 48 |
+
def _(expr, assumptions):
|
| 49 |
+
if expr.is_number:
|
| 50 |
+
return _NegativePredicate_number(expr, assumptions)
|
| 51 |
+
|
| 52 |
+
@NegativePredicate.register(Expr)
|
| 53 |
+
def _(expr, assumptions):
|
| 54 |
+
ret = expr.is_negative
|
| 55 |
+
if ret is None:
|
| 56 |
+
raise MDNotImplementedError
|
| 57 |
+
return ret
|
| 58 |
+
|
| 59 |
+
@NegativePredicate.register(Add)
|
| 60 |
+
def _(expr, assumptions):
|
| 61 |
+
"""
|
| 62 |
+
Positive + Positive -> Positive,
|
| 63 |
+
Negative + Negative -> Negative
|
| 64 |
+
"""
|
| 65 |
+
if expr.is_number:
|
| 66 |
+
return _NegativePredicate_number(expr, assumptions)
|
| 67 |
+
|
| 68 |
+
r = ask(Q.real(expr), assumptions)
|
| 69 |
+
if r is not True:
|
| 70 |
+
return r
|
| 71 |
+
|
| 72 |
+
nonpos = 0
|
| 73 |
+
for arg in expr.args:
|
| 74 |
+
if ask(Q.negative(arg), assumptions) is not True:
|
| 75 |
+
if ask(Q.positive(arg), assumptions) is False:
|
| 76 |
+
nonpos += 1
|
| 77 |
+
else:
|
| 78 |
+
break
|
| 79 |
+
else:
|
| 80 |
+
if nonpos < len(expr.args):
|
| 81 |
+
return True
|
| 82 |
+
|
| 83 |
+
@NegativePredicate.register(Mul)
|
| 84 |
+
def _(expr, assumptions):
|
| 85 |
+
if expr.is_number:
|
| 86 |
+
return _NegativePredicate_number(expr, assumptions)
|
| 87 |
+
result = None
|
| 88 |
+
for arg in expr.args:
|
| 89 |
+
if result is None:
|
| 90 |
+
result = False
|
| 91 |
+
if ask(Q.negative(arg), assumptions):
|
| 92 |
+
result = not result
|
| 93 |
+
elif ask(Q.positive(arg), assumptions):
|
| 94 |
+
pass
|
| 95 |
+
else:
|
| 96 |
+
return
|
| 97 |
+
return result
|
| 98 |
+
|
| 99 |
+
@NegativePredicate.register(Pow)
|
| 100 |
+
def _(expr, assumptions):
|
| 101 |
+
"""
|
| 102 |
+
Real ** Even -> NonNegative
|
| 103 |
+
Real ** Odd -> same_as_base
|
| 104 |
+
NonNegative ** Positive -> NonNegative
|
| 105 |
+
"""
|
| 106 |
+
if expr.base == E:
|
| 107 |
+
# Exponential is always positive:
|
| 108 |
+
if ask(Q.real(expr.exp), assumptions):
|
| 109 |
+
return False
|
| 110 |
+
return
|
| 111 |
+
|
| 112 |
+
if expr.is_number:
|
| 113 |
+
return _NegativePredicate_number(expr, assumptions)
|
| 114 |
+
if ask(Q.real(expr.base), assumptions):
|
| 115 |
+
if ask(Q.positive(expr.base), assumptions):
|
| 116 |
+
if ask(Q.real(expr.exp), assumptions):
|
| 117 |
+
return False
|
| 118 |
+
if ask(Q.even(expr.exp), assumptions):
|
| 119 |
+
return False
|
| 120 |
+
if ask(Q.odd(expr.exp), assumptions):
|
| 121 |
+
return ask(Q.negative(expr.base), assumptions)
|
| 122 |
+
|
| 123 |
+
@NegativePredicate.register_many(Abs, ImaginaryUnit)
|
| 124 |
+
def _(expr, assumptions):
|
| 125 |
+
return False
|
| 126 |
+
|
| 127 |
+
@NegativePredicate.register(exp)
|
| 128 |
+
def _(expr, assumptions):
|
| 129 |
+
if ask(Q.real(expr.exp), assumptions):
|
| 130 |
+
return False
|
| 131 |
+
raise MDNotImplementedError
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
# NonNegativePredicate
|
| 135 |
+
|
| 136 |
+
@NonNegativePredicate.register(Basic)
|
| 137 |
+
def _(expr, assumptions):
|
| 138 |
+
if expr.is_number:
|
| 139 |
+
notnegative = fuzzy_not(_NegativePredicate_number(expr, assumptions))
|
| 140 |
+
if notnegative:
|
| 141 |
+
return ask(Q.real(expr), assumptions)
|
| 142 |
+
else:
|
| 143 |
+
return notnegative
|
| 144 |
+
|
| 145 |
+
@NonNegativePredicate.register(Expr)
|
| 146 |
+
def _(expr, assumptions):
|
| 147 |
+
ret = expr.is_nonnegative
|
| 148 |
+
if ret is None:
|
| 149 |
+
raise MDNotImplementedError
|
| 150 |
+
return ret
|
| 151 |
+
|
| 152 |
+
|
| 153 |
+
# NonZeroPredicate
|
| 154 |
+
|
| 155 |
+
@NonZeroPredicate.register(Expr)
|
| 156 |
+
def _(expr, assumptions):
|
| 157 |
+
ret = expr.is_nonzero
|
| 158 |
+
if ret is None:
|
| 159 |
+
raise MDNotImplementedError
|
| 160 |
+
return ret
|
| 161 |
+
|
| 162 |
+
@NonZeroPredicate.register(Basic)
|
| 163 |
+
def _(expr, assumptions):
|
| 164 |
+
if ask(Q.real(expr)) is False:
|
| 165 |
+
return False
|
| 166 |
+
if expr.is_number:
|
| 167 |
+
# if there are no symbols just evalf
|
| 168 |
+
i = expr.evalf(2)
|
| 169 |
+
def nonz(i):
|
| 170 |
+
if i._prec != 1:
|
| 171 |
+
return i != 0
|
| 172 |
+
return fuzzy_or(nonz(i) for i in i.as_real_imag())
|
| 173 |
+
|
| 174 |
+
@NonZeroPredicate.register(Add)
|
| 175 |
+
def _(expr, assumptions):
|
| 176 |
+
if all(ask(Q.positive(x), assumptions) for x in expr.args) \
|
| 177 |
+
or all(ask(Q.negative(x), assumptions) for x in expr.args):
|
| 178 |
+
return True
|
| 179 |
+
|
| 180 |
+
@NonZeroPredicate.register(Mul)
|
| 181 |
+
def _(expr, assumptions):
|
| 182 |
+
for arg in expr.args:
|
| 183 |
+
result = ask(Q.nonzero(arg), assumptions)
|
| 184 |
+
if result:
|
| 185 |
+
continue
|
| 186 |
+
return result
|
| 187 |
+
return True
|
| 188 |
+
|
| 189 |
+
@NonZeroPredicate.register(Pow)
|
| 190 |
+
def _(expr, assumptions):
|
| 191 |
+
return ask(Q.nonzero(expr.base), assumptions)
|
| 192 |
+
|
| 193 |
+
@NonZeroPredicate.register(Abs)
|
| 194 |
+
def _(expr, assumptions):
|
| 195 |
+
return ask(Q.nonzero(expr.args[0]), assumptions)
|
| 196 |
+
|
| 197 |
+
@NonZeroPredicate.register(NaN)
|
| 198 |
+
def _(expr, assumptions):
|
| 199 |
+
return None
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
# ZeroPredicate
|
| 203 |
+
|
| 204 |
+
@ZeroPredicate.register(Expr)
|
| 205 |
+
def _(expr, assumptions):
|
| 206 |
+
ret = expr.is_zero
|
| 207 |
+
if ret is None:
|
| 208 |
+
raise MDNotImplementedError
|
| 209 |
+
return ret
|
| 210 |
+
|
| 211 |
+
@ZeroPredicate.register(Basic)
|
| 212 |
+
def _(expr, assumptions):
|
| 213 |
+
return fuzzy_and([fuzzy_not(ask(Q.nonzero(expr), assumptions)),
|
| 214 |
+
ask(Q.real(expr), assumptions)])
|
| 215 |
+
|
| 216 |
+
@ZeroPredicate.register(Mul)
|
| 217 |
+
def _(expr, assumptions):
|
| 218 |
+
# TODO: This should be deducible from the nonzero handler
|
| 219 |
+
return fuzzy_or(ask(Q.zero(arg), assumptions) for arg in expr.args)
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
# NonPositivePredicate
|
| 223 |
+
|
| 224 |
+
@NonPositivePredicate.register(Expr)
|
| 225 |
+
def _(expr, assumptions):
|
| 226 |
+
ret = expr.is_nonpositive
|
| 227 |
+
if ret is None:
|
| 228 |
+
raise MDNotImplementedError
|
| 229 |
+
return ret
|
| 230 |
+
|
| 231 |
+
@NonPositivePredicate.register(Basic)
|
| 232 |
+
def _(expr, assumptions):
|
| 233 |
+
if expr.is_number:
|
| 234 |
+
notpositive = fuzzy_not(_PositivePredicate_number(expr, assumptions))
|
| 235 |
+
if notpositive:
|
| 236 |
+
return ask(Q.real(expr), assumptions)
|
| 237 |
+
else:
|
| 238 |
+
return notpositive
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
# PositivePredicate
|
| 242 |
+
|
| 243 |
+
def _PositivePredicate_number(expr, assumptions):
|
| 244 |
+
r, i = expr.as_real_imag()
|
| 245 |
+
# If the imaginary part can symbolically be shown to be zero then
|
| 246 |
+
# we just evaluate the real part; otherwise we evaluate the imaginary
|
| 247 |
+
# part to see if it actually evaluates to zero and if it does then
|
| 248 |
+
# we make the comparison between the real part and zero.
|
| 249 |
+
if not i:
|
| 250 |
+
r = r.evalf(2)
|
| 251 |
+
if r._prec != 1:
|
| 252 |
+
return r > 0
|
| 253 |
+
else:
|
| 254 |
+
i = i.evalf(2)
|
| 255 |
+
if i._prec != 1:
|
| 256 |
+
if i != 0:
|
| 257 |
+
return False
|
| 258 |
+
r = r.evalf(2)
|
| 259 |
+
if r._prec != 1:
|
| 260 |
+
return r > 0
|
| 261 |
+
|
| 262 |
+
@PositivePredicate.register(Expr)
|
| 263 |
+
def _(expr, assumptions):
|
| 264 |
+
ret = expr.is_positive
|
| 265 |
+
if ret is None:
|
| 266 |
+
raise MDNotImplementedError
|
| 267 |
+
return ret
|
| 268 |
+
|
| 269 |
+
@PositivePredicate.register(Basic)
|
| 270 |
+
def _(expr, assumptions):
|
| 271 |
+
if expr.is_number:
|
| 272 |
+
return _PositivePredicate_number(expr, assumptions)
|
| 273 |
+
|
| 274 |
+
@PositivePredicate.register(Mul)
|
| 275 |
+
def _(expr, assumptions):
|
| 276 |
+
if expr.is_number:
|
| 277 |
+
return _PositivePredicate_number(expr, assumptions)
|
| 278 |
+
result = True
|
| 279 |
+
for arg in expr.args:
|
| 280 |
+
if ask(Q.positive(arg), assumptions):
|
| 281 |
+
continue
|
| 282 |
+
elif ask(Q.negative(arg), assumptions):
|
| 283 |
+
result = result ^ True
|
| 284 |
+
else:
|
| 285 |
+
return
|
| 286 |
+
return result
|
| 287 |
+
|
| 288 |
+
@PositivePredicate.register(Add)
|
| 289 |
+
def _(expr, assumptions):
|
| 290 |
+
if expr.is_number:
|
| 291 |
+
return _PositivePredicate_number(expr, assumptions)
|
| 292 |
+
|
| 293 |
+
r = ask(Q.real(expr), assumptions)
|
| 294 |
+
if r is not True:
|
| 295 |
+
return r
|
| 296 |
+
|
| 297 |
+
nonneg = 0
|
| 298 |
+
for arg in expr.args:
|
| 299 |
+
if ask(Q.positive(arg), assumptions) is not True:
|
| 300 |
+
if ask(Q.negative(arg), assumptions) is False:
|
| 301 |
+
nonneg += 1
|
| 302 |
+
else:
|
| 303 |
+
break
|
| 304 |
+
else:
|
| 305 |
+
if nonneg < len(expr.args):
|
| 306 |
+
return True
|
| 307 |
+
|
| 308 |
+
@PositivePredicate.register(Pow)
|
| 309 |
+
def _(expr, assumptions):
|
| 310 |
+
if expr.base == E:
|
| 311 |
+
if ask(Q.real(expr.exp), assumptions):
|
| 312 |
+
return True
|
| 313 |
+
if ask(Q.imaginary(expr.exp), assumptions):
|
| 314 |
+
return ask(Q.even(expr.exp/(I*pi)), assumptions)
|
| 315 |
+
return
|
| 316 |
+
|
| 317 |
+
if expr.is_number:
|
| 318 |
+
return _PositivePredicate_number(expr, assumptions)
|
| 319 |
+
if ask(Q.positive(expr.base), assumptions):
|
| 320 |
+
if ask(Q.real(expr.exp), assumptions):
|
| 321 |
+
return True
|
| 322 |
+
if ask(Q.negative(expr.base), assumptions):
|
| 323 |
+
if ask(Q.even(expr.exp), assumptions):
|
| 324 |
+
return True
|
| 325 |
+
if ask(Q.odd(expr.exp), assumptions):
|
| 326 |
+
return False
|
| 327 |
+
|
| 328 |
+
@PositivePredicate.register(exp)
|
| 329 |
+
def _(expr, assumptions):
|
| 330 |
+
if ask(Q.real(expr.exp), assumptions):
|
| 331 |
+
return True
|
| 332 |
+
if ask(Q.imaginary(expr.exp), assumptions):
|
| 333 |
+
return ask(Q.even(expr.exp/(I*pi)), assumptions)
|
| 334 |
+
|
| 335 |
+
@PositivePredicate.register(log)
|
| 336 |
+
def _(expr, assumptions):
|
| 337 |
+
r = ask(Q.real(expr.args[0]), assumptions)
|
| 338 |
+
if r is not True:
|
| 339 |
+
return r
|
| 340 |
+
if ask(Q.positive(expr.args[0] - 1), assumptions):
|
| 341 |
+
return True
|
| 342 |
+
if ask(Q.negative(expr.args[0] - 1), assumptions):
|
| 343 |
+
return False
|
| 344 |
+
|
| 345 |
+
@PositivePredicate.register(factorial)
|
| 346 |
+
def _(expr, assumptions):
|
| 347 |
+
x = expr.args[0]
|
| 348 |
+
if ask(Q.integer(x) & Q.positive(x), assumptions):
|
| 349 |
+
return True
|
| 350 |
+
|
| 351 |
+
@PositivePredicate.register(ImaginaryUnit)
|
| 352 |
+
def _(expr, assumptions):
|
| 353 |
+
return False
|
| 354 |
+
|
| 355 |
+
@PositivePredicate.register(Abs)
|
| 356 |
+
def _(expr, assumptions):
|
| 357 |
+
return ask(Q.nonzero(expr), assumptions)
|
| 358 |
+
|
| 359 |
+
@PositivePredicate.register(Trace)
|
| 360 |
+
def _(expr, assumptions):
|
| 361 |
+
if ask(Q.positive_definite(expr.arg), assumptions):
|
| 362 |
+
return True
|
| 363 |
+
|
| 364 |
+
@PositivePredicate.register(Determinant)
|
| 365 |
+
def _(expr, assumptions):
|
| 366 |
+
if ask(Q.positive_definite(expr.arg), assumptions):
|
| 367 |
+
return True
|
| 368 |
+
|
| 369 |
+
@PositivePredicate.register(MatrixElement)
|
| 370 |
+
def _(expr, assumptions):
|
| 371 |
+
if (expr.i == expr.j
|
| 372 |
+
and ask(Q.positive_definite(expr.parent), assumptions)):
|
| 373 |
+
return True
|
| 374 |
+
|
| 375 |
+
@PositivePredicate.register(atan)
|
| 376 |
+
def _(expr, assumptions):
|
| 377 |
+
return ask(Q.positive(expr.args[0]), assumptions)
|
| 378 |
+
|
| 379 |
+
@PositivePredicate.register(asin)
|
| 380 |
+
def _(expr, assumptions):
|
| 381 |
+
x = expr.args[0]
|
| 382 |
+
if ask(Q.positive(x) & Q.nonpositive(x - 1), assumptions):
|
| 383 |
+
return True
|
| 384 |
+
if ask(Q.negative(x) & Q.nonnegative(x + 1), assumptions):
|
| 385 |
+
return False
|
| 386 |
+
|
| 387 |
+
@PositivePredicate.register(acos)
|
| 388 |
+
def _(expr, assumptions):
|
| 389 |
+
x = expr.args[0]
|
| 390 |
+
if ask(Q.nonpositive(x - 1) & Q.nonnegative(x + 1), assumptions):
|
| 391 |
+
return True
|
| 392 |
+
|
| 393 |
+
@PositivePredicate.register(acot)
|
| 394 |
+
def _(expr, assumptions):
|
| 395 |
+
return ask(Q.real(expr.args[0]), assumptions)
|
| 396 |
+
|
| 397 |
+
@PositivePredicate.register(NaN)
|
| 398 |
+
def _(expr, assumptions):
|
| 399 |
+
return None
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
# ExtendedNegativePredicate
|
| 403 |
+
|
| 404 |
+
@ExtendedNegativePredicate.register(object)
|
| 405 |
+
def _(expr, assumptions):
|
| 406 |
+
return ask(Q.negative(expr) | Q.negative_infinite(expr), assumptions)
|
| 407 |
+
|
| 408 |
+
|
| 409 |
+
# ExtendedPositivePredicate
|
| 410 |
+
|
| 411 |
+
@ExtendedPositivePredicate.register(object)
|
| 412 |
+
def _(expr, assumptions):
|
| 413 |
+
return ask(Q.positive(expr) | Q.positive_infinite(expr), assumptions)
|
| 414 |
+
|
| 415 |
+
|
| 416 |
+
# ExtendedNonZeroPredicate
|
| 417 |
+
|
| 418 |
+
@ExtendedNonZeroPredicate.register(object)
|
| 419 |
+
def _(expr, assumptions):
|
| 420 |
+
return ask(
|
| 421 |
+
Q.negative_infinite(expr) | Q.negative(expr) | Q.positive(expr) | Q.positive_infinite(expr),
|
| 422 |
+
assumptions)
|
| 423 |
+
|
| 424 |
+
|
| 425 |
+
# ExtendedNonPositivePredicate
|
| 426 |
+
|
| 427 |
+
@ExtendedNonPositivePredicate.register(object)
|
| 428 |
+
def _(expr, assumptions):
|
| 429 |
+
return ask(
|
| 430 |
+
Q.negative_infinite(expr) | Q.negative(expr) | Q.zero(expr),
|
| 431 |
+
assumptions)
|
| 432 |
+
|
| 433 |
+
|
| 434 |
+
# ExtendedNonNegativePredicate
|
| 435 |
+
|
| 436 |
+
@ExtendedNonNegativePredicate.register(object)
|
| 437 |
+
def _(expr, assumptions):
|
| 438 |
+
return ask(
|
| 439 |
+
Q.zero(expr) | Q.positive(expr) | Q.positive_infinite(expr),
|
| 440 |
+
assumptions)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/handlers/sets.py
ADDED
|
@@ -0,0 +1,816 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, ask_all, ask_any
|
| 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(Add)
|
| 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(Pow)
|
| 68 |
+
def _(expr,assumptions):
|
| 69 |
+
if expr.is_number:
|
| 70 |
+
return _IntegerPredicate_number(expr, assumptions)
|
| 71 |
+
if ask_all(~Q.zero(expr.base), Q.finite(expr.base), Q.zero(expr.exp), assumptions=assumptions):
|
| 72 |
+
return True
|
| 73 |
+
if ask_all(Q.integer(expr.base), Q.integer(expr.exp), assumptions=assumptions):
|
| 74 |
+
if ask_any(Q.positive(expr.exp), Q.nonnegative(expr.exp) & ~Q.zero(expr.base), Q.zero(expr.base-1), Q.zero(expr.base+1), assumptions=assumptions):
|
| 75 |
+
return True
|
| 76 |
+
|
| 77 |
+
@IntegerPredicate.register(Mul)
|
| 78 |
+
def _(expr, assumptions):
|
| 79 |
+
"""
|
| 80 |
+
* Integer*Integer -> Integer
|
| 81 |
+
* Integer*Irrational -> !Integer
|
| 82 |
+
* Odd/Even -> !Integer
|
| 83 |
+
* Integer*Rational -> ?
|
| 84 |
+
"""
|
| 85 |
+
if expr.is_number:
|
| 86 |
+
return _IntegerPredicate_number(expr, assumptions)
|
| 87 |
+
_output = True
|
| 88 |
+
for arg in expr.args:
|
| 89 |
+
if not ask(Q.integer(arg), assumptions):
|
| 90 |
+
if arg.is_Rational:
|
| 91 |
+
if arg.q == 2:
|
| 92 |
+
return ask(Q.even(2*expr), assumptions)
|
| 93 |
+
if ~(arg.q & 1):
|
| 94 |
+
return None
|
| 95 |
+
elif ask(Q.irrational(arg), assumptions):
|
| 96 |
+
if _output:
|
| 97 |
+
_output = False
|
| 98 |
+
else:
|
| 99 |
+
return
|
| 100 |
+
else:
|
| 101 |
+
return
|
| 102 |
+
|
| 103 |
+
return _output
|
| 104 |
+
|
| 105 |
+
@IntegerPredicate.register(Abs)
|
| 106 |
+
def _(expr, assumptions):
|
| 107 |
+
if ask(Q.integer(expr.args[0]), assumptions):
|
| 108 |
+
return True
|
| 109 |
+
|
| 110 |
+
@IntegerPredicate.register_many(Determinant, MatrixElement, Trace)
|
| 111 |
+
def _(expr, assumptions):
|
| 112 |
+
return ask(Q.integer_elements(expr.args[0]), assumptions)
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
# RationalPredicate
|
| 116 |
+
|
| 117 |
+
@RationalPredicate.register(Rational)
|
| 118 |
+
def _(expr, assumptions):
|
| 119 |
+
return True
|
| 120 |
+
|
| 121 |
+
@RationalPredicate.register(Float)
|
| 122 |
+
def _(expr, assumptions):
|
| 123 |
+
return None
|
| 124 |
+
|
| 125 |
+
@RationalPredicate.register_many(Exp1, GoldenRatio, ImaginaryUnit, Infinity,
|
| 126 |
+
NegativeInfinity, Pi, TribonacciConstant)
|
| 127 |
+
def _(expr, assumptions):
|
| 128 |
+
return False
|
| 129 |
+
|
| 130 |
+
@RationalPredicate.register(Expr)
|
| 131 |
+
def _(expr, assumptions):
|
| 132 |
+
ret = expr.is_rational
|
| 133 |
+
if ret is None:
|
| 134 |
+
raise MDNotImplementedError
|
| 135 |
+
return ret
|
| 136 |
+
|
| 137 |
+
@RationalPredicate.register_many(Add, Mul)
|
| 138 |
+
def _(expr, assumptions):
|
| 139 |
+
"""
|
| 140 |
+
* Rational + Rational -> Rational
|
| 141 |
+
* Rational + !Rational -> !Rational
|
| 142 |
+
* !Rational + !Rational -> ?
|
| 143 |
+
"""
|
| 144 |
+
if expr.is_number:
|
| 145 |
+
if expr.as_real_imag()[1]:
|
| 146 |
+
return False
|
| 147 |
+
return test_closed_group(expr, assumptions, Q.rational)
|
| 148 |
+
|
| 149 |
+
@RationalPredicate.register(Pow)
|
| 150 |
+
def _(expr, assumptions):
|
| 151 |
+
"""
|
| 152 |
+
* Rational ** Integer -> Rational
|
| 153 |
+
* Irrational ** Rational -> Irrational
|
| 154 |
+
* Rational ** Irrational -> ?
|
| 155 |
+
"""
|
| 156 |
+
if expr.base == E:
|
| 157 |
+
x = expr.exp
|
| 158 |
+
if ask(Q.rational(x), assumptions):
|
| 159 |
+
return ask(Q.zero(x), assumptions)
|
| 160 |
+
return
|
| 161 |
+
|
| 162 |
+
is_exp_integer = ask(Q.integer(expr.exp), assumptions)
|
| 163 |
+
if is_exp_integer:
|
| 164 |
+
is_base_rational = ask(Q.rational(expr.base),assumptions)
|
| 165 |
+
if is_base_rational:
|
| 166 |
+
is_base_zero = ask(Q.zero(expr.base),assumptions)
|
| 167 |
+
if is_base_zero is False:
|
| 168 |
+
return True
|
| 169 |
+
if is_base_zero and ask(Q.positive(expr.exp)):
|
| 170 |
+
return True
|
| 171 |
+
if ask(Q.algebraic(expr.base),assumptions) is False:
|
| 172 |
+
return ask(Q.zero(expr.exp), assumptions)
|
| 173 |
+
if ask(Q.irrational(expr.base),assumptions) and ask(Q.eq(expr.exp,-1)):
|
| 174 |
+
return False
|
| 175 |
+
return
|
| 176 |
+
elif ask(Q.rational(expr.exp), assumptions):
|
| 177 |
+
if ask(Q.prime(expr.base), assumptions) and is_exp_integer is False:
|
| 178 |
+
return False
|
| 179 |
+
if ask(Q.zero(expr.base)) and ask(Q.positive(expr.exp)):
|
| 180 |
+
return True
|
| 181 |
+
if ask(Q.eq(expr.base,1)):
|
| 182 |
+
return True
|
| 183 |
+
|
| 184 |
+
@RationalPredicate.register_many(asin, atan, cos, sin, tan)
|
| 185 |
+
def _(expr, assumptions):
|
| 186 |
+
x = expr.args[0]
|
| 187 |
+
if ask(Q.rational(x), assumptions):
|
| 188 |
+
return ask(~Q.nonzero(x), assumptions)
|
| 189 |
+
|
| 190 |
+
@RationalPredicate.register(exp)
|
| 191 |
+
def _(expr, assumptions):
|
| 192 |
+
x = expr.exp
|
| 193 |
+
if ask(Q.rational(x), assumptions):
|
| 194 |
+
return ask(~Q.nonzero(x), assumptions)
|
| 195 |
+
|
| 196 |
+
@RationalPredicate.register_many(acot, cot)
|
| 197 |
+
def _(expr, assumptions):
|
| 198 |
+
x = expr.args[0]
|
| 199 |
+
if ask(Q.rational(x), assumptions):
|
| 200 |
+
return False
|
| 201 |
+
|
| 202 |
+
@RationalPredicate.register_many(acos, log)
|
| 203 |
+
def _(expr, assumptions):
|
| 204 |
+
x = expr.args[0]
|
| 205 |
+
if ask(Q.rational(x), assumptions):
|
| 206 |
+
return ask(~Q.nonzero(x - 1), assumptions)
|
| 207 |
+
|
| 208 |
+
|
| 209 |
+
# IrrationalPredicate
|
| 210 |
+
|
| 211 |
+
@IrrationalPredicate.register(Expr)
|
| 212 |
+
def _(expr, assumptions):
|
| 213 |
+
ret = expr.is_irrational
|
| 214 |
+
if ret is None:
|
| 215 |
+
raise MDNotImplementedError
|
| 216 |
+
return ret
|
| 217 |
+
|
| 218 |
+
@IrrationalPredicate.register(Basic)
|
| 219 |
+
def _(expr, assumptions):
|
| 220 |
+
_real = ask(Q.real(expr), assumptions)
|
| 221 |
+
if _real:
|
| 222 |
+
_rational = ask(Q.rational(expr), assumptions)
|
| 223 |
+
if _rational is None:
|
| 224 |
+
return None
|
| 225 |
+
return not _rational
|
| 226 |
+
else:
|
| 227 |
+
return _real
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
# RealPredicate
|
| 231 |
+
|
| 232 |
+
def _RealPredicate_number(expr, assumptions):
|
| 233 |
+
# let as_real_imag() work first since the expression may
|
| 234 |
+
# be simpler to evaluate
|
| 235 |
+
i = expr.as_real_imag()[1].evalf(2)
|
| 236 |
+
if i._prec != 1:
|
| 237 |
+
return not i
|
| 238 |
+
# allow None to be returned if we couldn't show for sure
|
| 239 |
+
# that i was 0
|
| 240 |
+
|
| 241 |
+
@RealPredicate.register_many(Abs, Exp1, Float, GoldenRatio, im, Pi, Rational,
|
| 242 |
+
re, TribonacciConstant)
|
| 243 |
+
def _(expr, assumptions):
|
| 244 |
+
return True
|
| 245 |
+
|
| 246 |
+
@RealPredicate.register_many(ImaginaryUnit, Infinity, NegativeInfinity)
|
| 247 |
+
def _(expr, assumptions):
|
| 248 |
+
return False
|
| 249 |
+
|
| 250 |
+
@RealPredicate.register(Expr)
|
| 251 |
+
def _(expr, assumptions):
|
| 252 |
+
ret = expr.is_real
|
| 253 |
+
if ret is None:
|
| 254 |
+
raise MDNotImplementedError
|
| 255 |
+
return ret
|
| 256 |
+
|
| 257 |
+
@RealPredicate.register(Add)
|
| 258 |
+
def _(expr, assumptions):
|
| 259 |
+
"""
|
| 260 |
+
* Real + Real -> Real
|
| 261 |
+
* Real + (Complex & !Real) -> !Real
|
| 262 |
+
"""
|
| 263 |
+
if expr.is_number:
|
| 264 |
+
return _RealPredicate_number(expr, assumptions)
|
| 265 |
+
return test_closed_group(expr, assumptions, Q.real)
|
| 266 |
+
|
| 267 |
+
@RealPredicate.register(Mul)
|
| 268 |
+
def _(expr, assumptions):
|
| 269 |
+
"""
|
| 270 |
+
* Real*Real -> Real
|
| 271 |
+
* Real*Imaginary -> !Real
|
| 272 |
+
* Imaginary*Imaginary -> Real
|
| 273 |
+
"""
|
| 274 |
+
if expr.is_number:
|
| 275 |
+
return _RealPredicate_number(expr, assumptions)
|
| 276 |
+
result = True
|
| 277 |
+
for arg in expr.args:
|
| 278 |
+
if ask(Q.real(arg), assumptions):
|
| 279 |
+
pass
|
| 280 |
+
elif ask(Q.imaginary(arg), assumptions):
|
| 281 |
+
result = result ^ True
|
| 282 |
+
else:
|
| 283 |
+
break
|
| 284 |
+
else:
|
| 285 |
+
return result
|
| 286 |
+
|
| 287 |
+
@RealPredicate.register(Pow)
|
| 288 |
+
def _(expr, assumptions):
|
| 289 |
+
"""
|
| 290 |
+
* Real**Integer -> Real
|
| 291 |
+
* Positive**Real -> Real
|
| 292 |
+
* Negative**Real -> ?
|
| 293 |
+
* Real**(Integer/Even) -> Real if base is nonnegative
|
| 294 |
+
* Real**(Integer/Odd) -> Real
|
| 295 |
+
* Imaginary**(Integer/Even) -> Real
|
| 296 |
+
* Imaginary**(Integer/Odd) -> not Real
|
| 297 |
+
* Imaginary**Real -> ? since Real could be 0 (giving real)
|
| 298 |
+
or 1 (giving imaginary)
|
| 299 |
+
* b**Imaginary -> Real if log(b) is imaginary and b != 0
|
| 300 |
+
and exponent != integer multiple of
|
| 301 |
+
I*pi/log(b)
|
| 302 |
+
* Real**Real -> ? e.g. sqrt(-1) is imaginary and
|
| 303 |
+
sqrt(2) is not
|
| 304 |
+
"""
|
| 305 |
+
if expr.is_number:
|
| 306 |
+
return _RealPredicate_number(expr, assumptions)
|
| 307 |
+
|
| 308 |
+
if expr.base == E:
|
| 309 |
+
return ask(
|
| 310 |
+
Q.integer(expr.exp/I/pi) | Q.real(expr.exp), assumptions
|
| 311 |
+
)
|
| 312 |
+
|
| 313 |
+
if expr.base.func == exp or (expr.base.is_Pow and expr.base.base == E):
|
| 314 |
+
if ask(Q.imaginary(expr.base.exp), assumptions):
|
| 315 |
+
if ask(Q.imaginary(expr.exp), assumptions):
|
| 316 |
+
return True
|
| 317 |
+
# If the i = (exp's arg)/(I*pi) is an integer or half-integer
|
| 318 |
+
# multiple of I*pi then 2*i will be an integer. In addition,
|
| 319 |
+
# exp(i*I*pi) = (-1)**i so the overall realness of the expr
|
| 320 |
+
# can be determined by replacing exp(i*I*pi) with (-1)**i.
|
| 321 |
+
i = expr.base.exp/I/pi
|
| 322 |
+
if ask(Q.integer(2*i), assumptions):
|
| 323 |
+
return ask(Q.real((S.NegativeOne**i)**expr.exp), assumptions)
|
| 324 |
+
return
|
| 325 |
+
|
| 326 |
+
if ask(Q.imaginary(expr.base), assumptions):
|
| 327 |
+
if ask(Q.integer(expr.exp), assumptions):
|
| 328 |
+
odd = ask(Q.odd(expr.exp), assumptions)
|
| 329 |
+
if odd is not None:
|
| 330 |
+
return not odd
|
| 331 |
+
return
|
| 332 |
+
|
| 333 |
+
if ask(Q.imaginary(expr.exp), assumptions):
|
| 334 |
+
imlog = ask(Q.imaginary(log(expr.base)), assumptions)
|
| 335 |
+
if imlog is not None:
|
| 336 |
+
# I**i -> real, log(I) is imag;
|
| 337 |
+
# (2*I)**i -> complex, log(2*I) is not imag
|
| 338 |
+
return imlog
|
| 339 |
+
|
| 340 |
+
if ask(Q.real(expr.base), assumptions):
|
| 341 |
+
if ask(Q.real(expr.exp), assumptions):
|
| 342 |
+
if ask(Q.zero(expr.base), assumptions) is not False:
|
| 343 |
+
if ask(Q.positive(expr.exp), assumptions):
|
| 344 |
+
return True
|
| 345 |
+
return
|
| 346 |
+
if expr.exp.is_Rational and \
|
| 347 |
+
ask(Q.even(expr.exp.q), assumptions):
|
| 348 |
+
return ask(Q.positive(expr.base), assumptions)
|
| 349 |
+
elif ask(Q.integer(expr.exp), assumptions):
|
| 350 |
+
return True
|
| 351 |
+
elif ask(Q.positive(expr.base), assumptions):
|
| 352 |
+
return True
|
| 353 |
+
|
| 354 |
+
@RealPredicate.register_many(cos, sin)
|
| 355 |
+
def _(expr, assumptions):
|
| 356 |
+
if ask(Q.real(expr.args[0]), assumptions):
|
| 357 |
+
return True
|
| 358 |
+
|
| 359 |
+
@RealPredicate.register(exp)
|
| 360 |
+
def _(expr, assumptions):
|
| 361 |
+
return ask(
|
| 362 |
+
Q.integer(expr.exp/I/pi) | Q.real(expr.exp), assumptions
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
@RealPredicate.register(log)
|
| 366 |
+
def _(expr, assumptions):
|
| 367 |
+
return ask(Q.positive(expr.args[0]), assumptions)
|
| 368 |
+
|
| 369 |
+
@RealPredicate.register_many(Determinant, MatrixElement, Trace)
|
| 370 |
+
def _(expr, assumptions):
|
| 371 |
+
return ask(Q.real_elements(expr.args[0]), assumptions)
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
# ExtendedRealPredicate
|
| 375 |
+
|
| 376 |
+
@ExtendedRealPredicate.register(object)
|
| 377 |
+
def _(expr, assumptions):
|
| 378 |
+
return ask(Q.negative_infinite(expr)
|
| 379 |
+
| Q.negative(expr)
|
| 380 |
+
| Q.zero(expr)
|
| 381 |
+
| Q.positive(expr)
|
| 382 |
+
| Q.positive_infinite(expr),
|
| 383 |
+
assumptions)
|
| 384 |
+
|
| 385 |
+
@ExtendedRealPredicate.register_many(Infinity, NegativeInfinity)
|
| 386 |
+
def _(expr, assumptions):
|
| 387 |
+
return True
|
| 388 |
+
|
| 389 |
+
@ExtendedRealPredicate.register_many(Add, Mul, Pow) # type:ignore
|
| 390 |
+
def _(expr, assumptions):
|
| 391 |
+
return test_closed_group(expr, assumptions, Q.extended_real)
|
| 392 |
+
|
| 393 |
+
|
| 394 |
+
# HermitianPredicate
|
| 395 |
+
|
| 396 |
+
@HermitianPredicate.register(object) # type:ignore
|
| 397 |
+
def _(expr, assumptions):
|
| 398 |
+
if isinstance(expr, MatrixBase):
|
| 399 |
+
return None
|
| 400 |
+
return ask(Q.real(expr), assumptions)
|
| 401 |
+
|
| 402 |
+
@HermitianPredicate.register(Add) # type:ignore
|
| 403 |
+
def _(expr, assumptions):
|
| 404 |
+
"""
|
| 405 |
+
* Hermitian + Hermitian -> Hermitian
|
| 406 |
+
* Hermitian + !Hermitian -> !Hermitian
|
| 407 |
+
"""
|
| 408 |
+
if expr.is_number:
|
| 409 |
+
raise MDNotImplementedError
|
| 410 |
+
return test_closed_group(expr, assumptions, Q.hermitian)
|
| 411 |
+
|
| 412 |
+
@HermitianPredicate.register(Mul) # type:ignore
|
| 413 |
+
def _(expr, assumptions):
|
| 414 |
+
"""
|
| 415 |
+
As long as there is at most only one noncommutative term:
|
| 416 |
+
|
| 417 |
+
* Hermitian*Hermitian -> Hermitian
|
| 418 |
+
* Hermitian*Antihermitian -> !Hermitian
|
| 419 |
+
* Antihermitian*Antihermitian -> Hermitian
|
| 420 |
+
"""
|
| 421 |
+
if expr.is_number:
|
| 422 |
+
raise MDNotImplementedError
|
| 423 |
+
nccount = 0
|
| 424 |
+
result = True
|
| 425 |
+
for arg in expr.args:
|
| 426 |
+
if ask(Q.antihermitian(arg), assumptions):
|
| 427 |
+
result = result ^ True
|
| 428 |
+
elif not ask(Q.hermitian(arg), assumptions):
|
| 429 |
+
break
|
| 430 |
+
if ask(~Q.commutative(arg), assumptions):
|
| 431 |
+
nccount += 1
|
| 432 |
+
if nccount > 1:
|
| 433 |
+
break
|
| 434 |
+
else:
|
| 435 |
+
return result
|
| 436 |
+
|
| 437 |
+
@HermitianPredicate.register(Pow) # type:ignore
|
| 438 |
+
def _(expr, assumptions):
|
| 439 |
+
"""
|
| 440 |
+
* Hermitian**Integer -> Hermitian
|
| 441 |
+
"""
|
| 442 |
+
if expr.is_number:
|
| 443 |
+
raise MDNotImplementedError
|
| 444 |
+
if expr.base == E:
|
| 445 |
+
if ask(Q.hermitian(expr.exp), assumptions):
|
| 446 |
+
return True
|
| 447 |
+
raise MDNotImplementedError
|
| 448 |
+
if ask(Q.hermitian(expr.base), assumptions):
|
| 449 |
+
if ask(Q.integer(expr.exp), assumptions):
|
| 450 |
+
return True
|
| 451 |
+
raise MDNotImplementedError
|
| 452 |
+
|
| 453 |
+
@HermitianPredicate.register_many(cos, sin) # type:ignore
|
| 454 |
+
def _(expr, assumptions):
|
| 455 |
+
if ask(Q.hermitian(expr.args[0]), assumptions):
|
| 456 |
+
return True
|
| 457 |
+
raise MDNotImplementedError
|
| 458 |
+
|
| 459 |
+
@HermitianPredicate.register(exp) # type:ignore
|
| 460 |
+
def _(expr, assumptions):
|
| 461 |
+
if ask(Q.hermitian(expr.exp), assumptions):
|
| 462 |
+
return True
|
| 463 |
+
raise MDNotImplementedError
|
| 464 |
+
|
| 465 |
+
@HermitianPredicate.register(MatrixBase) # type:ignore
|
| 466 |
+
def _(mat, assumptions):
|
| 467 |
+
rows, cols = mat.shape
|
| 468 |
+
ret_val = True
|
| 469 |
+
for i in range(rows):
|
| 470 |
+
for j in range(i, cols):
|
| 471 |
+
cond = fuzzy_bool(Eq(mat[i, j], conjugate(mat[j, i])))
|
| 472 |
+
if cond is None:
|
| 473 |
+
ret_val = None
|
| 474 |
+
if cond == False:
|
| 475 |
+
return False
|
| 476 |
+
if ret_val is None:
|
| 477 |
+
raise MDNotImplementedError
|
| 478 |
+
return ret_val
|
| 479 |
+
|
| 480 |
+
|
| 481 |
+
# ComplexPredicate
|
| 482 |
+
|
| 483 |
+
@ComplexPredicate.register_many(Abs, cos, exp, im, ImaginaryUnit, log, Number, # type:ignore
|
| 484 |
+
NumberSymbol, re, sin)
|
| 485 |
+
def _(expr, assumptions):
|
| 486 |
+
return True
|
| 487 |
+
|
| 488 |
+
@ComplexPredicate.register_many(Infinity, NegativeInfinity) # type:ignore
|
| 489 |
+
def _(expr, assumptions):
|
| 490 |
+
return False
|
| 491 |
+
|
| 492 |
+
@ComplexPredicate.register(Expr) # type:ignore
|
| 493 |
+
def _(expr, assumptions):
|
| 494 |
+
ret = expr.is_complex
|
| 495 |
+
if ret is None:
|
| 496 |
+
raise MDNotImplementedError
|
| 497 |
+
return ret
|
| 498 |
+
|
| 499 |
+
@ComplexPredicate.register_many(Add, Mul) # type:ignore
|
| 500 |
+
def _(expr, assumptions):
|
| 501 |
+
return test_closed_group(expr, assumptions, Q.complex)
|
| 502 |
+
|
| 503 |
+
@ComplexPredicate.register(Pow) # type:ignore
|
| 504 |
+
def _(expr, assumptions):
|
| 505 |
+
if expr.base == E:
|
| 506 |
+
return True
|
| 507 |
+
return test_closed_group(expr, assumptions, Q.complex)
|
| 508 |
+
|
| 509 |
+
@ComplexPredicate.register_many(Determinant, MatrixElement, Trace) # type:ignore
|
| 510 |
+
def _(expr, assumptions):
|
| 511 |
+
return ask(Q.complex_elements(expr.args[0]), assumptions)
|
| 512 |
+
|
| 513 |
+
@ComplexPredicate.register(NaN) # type:ignore
|
| 514 |
+
def _(expr, assumptions):
|
| 515 |
+
return None
|
| 516 |
+
|
| 517 |
+
|
| 518 |
+
# ImaginaryPredicate
|
| 519 |
+
|
| 520 |
+
def _Imaginary_number(expr, assumptions):
|
| 521 |
+
# let as_real_imag() work first since the expression may
|
| 522 |
+
# be simpler to evaluate
|
| 523 |
+
r = expr.as_real_imag()[0].evalf(2)
|
| 524 |
+
if r._prec != 1:
|
| 525 |
+
return not r
|
| 526 |
+
# allow None to be returned if we couldn't show for sure
|
| 527 |
+
# that r was 0
|
| 528 |
+
|
| 529 |
+
@ImaginaryPredicate.register(ImaginaryUnit) # type:ignore
|
| 530 |
+
def _(expr, assumptions):
|
| 531 |
+
return True
|
| 532 |
+
|
| 533 |
+
@ImaginaryPredicate.register(Expr) # type:ignore
|
| 534 |
+
def _(expr, assumptions):
|
| 535 |
+
ret = expr.is_imaginary
|
| 536 |
+
if ret is None:
|
| 537 |
+
raise MDNotImplementedError
|
| 538 |
+
return ret
|
| 539 |
+
|
| 540 |
+
@ImaginaryPredicate.register(Add) # type:ignore
|
| 541 |
+
def _(expr, assumptions):
|
| 542 |
+
"""
|
| 543 |
+
* Imaginary + Imaginary -> Imaginary
|
| 544 |
+
* Imaginary + Complex -> ?
|
| 545 |
+
* Imaginary + Real -> !Imaginary
|
| 546 |
+
"""
|
| 547 |
+
if expr.is_number:
|
| 548 |
+
return _Imaginary_number(expr, assumptions)
|
| 549 |
+
|
| 550 |
+
reals = 0
|
| 551 |
+
for arg in expr.args:
|
| 552 |
+
if ask(Q.imaginary(arg), assumptions):
|
| 553 |
+
pass
|
| 554 |
+
elif ask(Q.real(arg), assumptions):
|
| 555 |
+
reals += 1
|
| 556 |
+
else:
|
| 557 |
+
break
|
| 558 |
+
else:
|
| 559 |
+
if reals == 0:
|
| 560 |
+
return True
|
| 561 |
+
if reals in (1, len(expr.args)):
|
| 562 |
+
# two reals could sum 0 thus giving an imaginary
|
| 563 |
+
return False
|
| 564 |
+
|
| 565 |
+
@ImaginaryPredicate.register(Mul) # type:ignore
|
| 566 |
+
def _(expr, assumptions):
|
| 567 |
+
"""
|
| 568 |
+
* Real*Imaginary -> Imaginary
|
| 569 |
+
* Imaginary*Imaginary -> Real
|
| 570 |
+
"""
|
| 571 |
+
if expr.is_number:
|
| 572 |
+
return _Imaginary_number(expr, assumptions)
|
| 573 |
+
result = False
|
| 574 |
+
reals = 0
|
| 575 |
+
for arg in expr.args:
|
| 576 |
+
if ask(Q.imaginary(arg), assumptions):
|
| 577 |
+
result = result ^ True
|
| 578 |
+
elif not ask(Q.real(arg), assumptions):
|
| 579 |
+
break
|
| 580 |
+
else:
|
| 581 |
+
if reals == len(expr.args):
|
| 582 |
+
return False
|
| 583 |
+
return result
|
| 584 |
+
|
| 585 |
+
@ImaginaryPredicate.register(Pow) # type:ignore
|
| 586 |
+
def _(expr, assumptions):
|
| 587 |
+
"""
|
| 588 |
+
* Imaginary**Odd -> Imaginary
|
| 589 |
+
* Imaginary**Even -> Real
|
| 590 |
+
* b**Imaginary -> !Imaginary if exponent is an integer
|
| 591 |
+
multiple of I*pi/log(b)
|
| 592 |
+
* Imaginary**Real -> ?
|
| 593 |
+
* Positive**Real -> Real
|
| 594 |
+
* Negative**Integer -> Real
|
| 595 |
+
* Negative**(Integer/2) -> Imaginary
|
| 596 |
+
* Negative**Real -> not Imaginary if exponent is not Rational
|
| 597 |
+
"""
|
| 598 |
+
if expr.is_number:
|
| 599 |
+
return _Imaginary_number(expr, assumptions)
|
| 600 |
+
|
| 601 |
+
if expr.base == E:
|
| 602 |
+
a = expr.exp/I/pi
|
| 603 |
+
return ask(Q.integer(2*a) & ~Q.integer(a), assumptions)
|
| 604 |
+
|
| 605 |
+
if expr.base.func == exp or (expr.base.is_Pow and expr.base.base == E):
|
| 606 |
+
if ask(Q.imaginary(expr.base.exp), assumptions):
|
| 607 |
+
if ask(Q.imaginary(expr.exp), assumptions):
|
| 608 |
+
return False
|
| 609 |
+
i = expr.base.exp/I/pi
|
| 610 |
+
if ask(Q.integer(2*i), assumptions):
|
| 611 |
+
return ask(Q.imaginary((S.NegativeOne**i)**expr.exp), assumptions)
|
| 612 |
+
|
| 613 |
+
if ask(Q.imaginary(expr.base), assumptions):
|
| 614 |
+
if ask(Q.integer(expr.exp), assumptions):
|
| 615 |
+
odd = ask(Q.odd(expr.exp), assumptions)
|
| 616 |
+
if odd is not None:
|
| 617 |
+
return odd
|
| 618 |
+
return
|
| 619 |
+
|
| 620 |
+
if ask(Q.imaginary(expr.exp), assumptions):
|
| 621 |
+
imlog = ask(Q.imaginary(log(expr.base)), assumptions)
|
| 622 |
+
if imlog is not None:
|
| 623 |
+
# I**i -> real; (2*I)**i -> complex ==> not imaginary
|
| 624 |
+
return False
|
| 625 |
+
|
| 626 |
+
if ask(Q.real(expr.base) & Q.real(expr.exp), assumptions):
|
| 627 |
+
if ask(Q.positive(expr.base), assumptions):
|
| 628 |
+
return False
|
| 629 |
+
else:
|
| 630 |
+
rat = ask(Q.rational(expr.exp), assumptions)
|
| 631 |
+
if not rat:
|
| 632 |
+
return rat
|
| 633 |
+
if ask(Q.integer(expr.exp), assumptions):
|
| 634 |
+
return False
|
| 635 |
+
else:
|
| 636 |
+
half = ask(Q.integer(2*expr.exp), assumptions)
|
| 637 |
+
if half:
|
| 638 |
+
return ask(Q.negative(expr.base), assumptions)
|
| 639 |
+
return half
|
| 640 |
+
|
| 641 |
+
@ImaginaryPredicate.register(log) # type:ignore
|
| 642 |
+
def _(expr, assumptions):
|
| 643 |
+
if ask(Q.real(expr.args[0]), assumptions):
|
| 644 |
+
if ask(Q.positive(expr.args[0]), assumptions):
|
| 645 |
+
return False
|
| 646 |
+
return
|
| 647 |
+
# XXX it should be enough to do
|
| 648 |
+
# return ask(Q.nonpositive(expr.args[0]), assumptions)
|
| 649 |
+
# but ask(Q.nonpositive(exp(x)), Q.imaginary(x)) -> None;
|
| 650 |
+
# it should return True since exp(x) will be either 0 or complex
|
| 651 |
+
if expr.args[0].func == exp or (expr.args[0].is_Pow and expr.args[0].base == E):
|
| 652 |
+
if expr.args[0].exp in [I, -I]:
|
| 653 |
+
return True
|
| 654 |
+
im = ask(Q.imaginary(expr.args[0]), assumptions)
|
| 655 |
+
if im is False:
|
| 656 |
+
return False
|
| 657 |
+
|
| 658 |
+
@ImaginaryPredicate.register(exp) # type:ignore
|
| 659 |
+
def _(expr, assumptions):
|
| 660 |
+
a = expr.exp/I/pi
|
| 661 |
+
return ask(Q.integer(2*a) & ~Q.integer(a), assumptions)
|
| 662 |
+
|
| 663 |
+
@ImaginaryPredicate.register_many(Number, NumberSymbol) # type:ignore
|
| 664 |
+
def _(expr, assumptions):
|
| 665 |
+
return not (expr.as_real_imag()[1] == 0)
|
| 666 |
+
|
| 667 |
+
@ImaginaryPredicate.register(NaN) # type:ignore
|
| 668 |
+
def _(expr, assumptions):
|
| 669 |
+
return None
|
| 670 |
+
|
| 671 |
+
|
| 672 |
+
# AntihermitianPredicate
|
| 673 |
+
|
| 674 |
+
@AntihermitianPredicate.register(object) # type:ignore
|
| 675 |
+
def _(expr, assumptions):
|
| 676 |
+
if isinstance(expr, MatrixBase):
|
| 677 |
+
return None
|
| 678 |
+
if ask(Q.zero(expr), assumptions):
|
| 679 |
+
return True
|
| 680 |
+
return ask(Q.imaginary(expr), assumptions)
|
| 681 |
+
|
| 682 |
+
@AntihermitianPredicate.register(Add) # type:ignore
|
| 683 |
+
def _(expr, assumptions):
|
| 684 |
+
"""
|
| 685 |
+
* Antihermitian + Antihermitian -> Antihermitian
|
| 686 |
+
* Antihermitian + !Antihermitian -> !Antihermitian
|
| 687 |
+
"""
|
| 688 |
+
if expr.is_number:
|
| 689 |
+
raise MDNotImplementedError
|
| 690 |
+
return test_closed_group(expr, assumptions, Q.antihermitian)
|
| 691 |
+
|
| 692 |
+
@AntihermitianPredicate.register(Mul) # type:ignore
|
| 693 |
+
def _(expr, assumptions):
|
| 694 |
+
"""
|
| 695 |
+
As long as there is at most only one noncommutative term:
|
| 696 |
+
|
| 697 |
+
* Hermitian*Hermitian -> !Antihermitian
|
| 698 |
+
* Hermitian*Antihermitian -> Antihermitian
|
| 699 |
+
* Antihermitian*Antihermitian -> !Antihermitian
|
| 700 |
+
"""
|
| 701 |
+
if expr.is_number:
|
| 702 |
+
raise MDNotImplementedError
|
| 703 |
+
nccount = 0
|
| 704 |
+
result = False
|
| 705 |
+
for arg in expr.args:
|
| 706 |
+
if ask(Q.antihermitian(arg), assumptions):
|
| 707 |
+
result = result ^ True
|
| 708 |
+
elif not ask(Q.hermitian(arg), assumptions):
|
| 709 |
+
break
|
| 710 |
+
if ask(~Q.commutative(arg), assumptions):
|
| 711 |
+
nccount += 1
|
| 712 |
+
if nccount > 1:
|
| 713 |
+
break
|
| 714 |
+
else:
|
| 715 |
+
return result
|
| 716 |
+
|
| 717 |
+
@AntihermitianPredicate.register(Pow) # type:ignore
|
| 718 |
+
def _(expr, assumptions):
|
| 719 |
+
"""
|
| 720 |
+
* Hermitian**Integer -> !Antihermitian
|
| 721 |
+
* Antihermitian**Even -> !Antihermitian
|
| 722 |
+
* Antihermitian**Odd -> Antihermitian
|
| 723 |
+
"""
|
| 724 |
+
if expr.is_number:
|
| 725 |
+
raise MDNotImplementedError
|
| 726 |
+
if ask(Q.hermitian(expr.base), assumptions):
|
| 727 |
+
if ask(Q.integer(expr.exp), assumptions):
|
| 728 |
+
return False
|
| 729 |
+
elif ask(Q.antihermitian(expr.base), assumptions):
|
| 730 |
+
if ask(Q.even(expr.exp), assumptions):
|
| 731 |
+
return False
|
| 732 |
+
elif ask(Q.odd(expr.exp), assumptions):
|
| 733 |
+
return True
|
| 734 |
+
raise MDNotImplementedError
|
| 735 |
+
|
| 736 |
+
@AntihermitianPredicate.register(MatrixBase) # type:ignore
|
| 737 |
+
def _(mat, assumptions):
|
| 738 |
+
rows, cols = mat.shape
|
| 739 |
+
ret_val = True
|
| 740 |
+
for i in range(rows):
|
| 741 |
+
for j in range(i, cols):
|
| 742 |
+
cond = fuzzy_bool(Eq(mat[i, j], -conjugate(mat[j, i])))
|
| 743 |
+
if cond is None:
|
| 744 |
+
ret_val = None
|
| 745 |
+
if cond == False:
|
| 746 |
+
return False
|
| 747 |
+
if ret_val is None:
|
| 748 |
+
raise MDNotImplementedError
|
| 749 |
+
return ret_val
|
| 750 |
+
|
| 751 |
+
|
| 752 |
+
# AlgebraicPredicate
|
| 753 |
+
|
| 754 |
+
@AlgebraicPredicate.register_many(AlgebraicNumber, Float, GoldenRatio, # type:ignore
|
| 755 |
+
ImaginaryUnit, TribonacciConstant)
|
| 756 |
+
def _(expr, assumptions):
|
| 757 |
+
return True
|
| 758 |
+
|
| 759 |
+
@AlgebraicPredicate.register_many(ComplexInfinity, Exp1, Infinity, # type:ignore
|
| 760 |
+
NegativeInfinity, Pi)
|
| 761 |
+
def _(expr, assumptions):
|
| 762 |
+
return False
|
| 763 |
+
|
| 764 |
+
@AlgebraicPredicate.register_many(Add, Mul) # type:ignore
|
| 765 |
+
def _(expr, assumptions):
|
| 766 |
+
return test_closed_group(expr, assumptions, Q.algebraic)
|
| 767 |
+
|
| 768 |
+
@AlgebraicPredicate.register(Pow) # type:ignore
|
| 769 |
+
def _(expr, assumptions):
|
| 770 |
+
if expr.base == E:
|
| 771 |
+
if ask(Q.algebraic(expr.exp), assumptions):
|
| 772 |
+
return ask(~Q.nonzero(expr.exp), assumptions)
|
| 773 |
+
return
|
| 774 |
+
if expr.base == pi:
|
| 775 |
+
if ask(Q.integer(expr.exp), assumptions) and ask(Q.positive(expr.exp), assumptions):
|
| 776 |
+
return False
|
| 777 |
+
return
|
| 778 |
+
exp_rational = ask(Q.rational(expr.exp), assumptions)
|
| 779 |
+
base_algebraic = ask(Q.algebraic(expr.base), assumptions)
|
| 780 |
+
exp_algebraic = ask(Q.algebraic(expr.exp),assumptions)
|
| 781 |
+
if base_algebraic and exp_algebraic:
|
| 782 |
+
if exp_rational:
|
| 783 |
+
return True
|
| 784 |
+
# Check based on the Gelfond-Schneider theorem:
|
| 785 |
+
# If the base is algebraic and not equal to 0 or 1, and the exponent
|
| 786 |
+
# is irrational,then the result is transcendental.
|
| 787 |
+
if ask(Q.ne(expr.base,0) & Q.ne(expr.base,1)) and exp_rational is False:
|
| 788 |
+
return False
|
| 789 |
+
|
| 790 |
+
@AlgebraicPredicate.register(Rational) # type:ignore
|
| 791 |
+
def _(expr, assumptions):
|
| 792 |
+
return expr.q != 0
|
| 793 |
+
|
| 794 |
+
@AlgebraicPredicate.register_many(asin, atan, cos, sin, tan) # type:ignore
|
| 795 |
+
def _(expr, assumptions):
|
| 796 |
+
x = expr.args[0]
|
| 797 |
+
if ask(Q.algebraic(x), assumptions):
|
| 798 |
+
return ask(~Q.nonzero(x), assumptions)
|
| 799 |
+
|
| 800 |
+
@AlgebraicPredicate.register(exp) # type:ignore
|
| 801 |
+
def _(expr, assumptions):
|
| 802 |
+
x = expr.exp
|
| 803 |
+
if ask(Q.algebraic(x), assumptions):
|
| 804 |
+
return ask(~Q.nonzero(x), assumptions)
|
| 805 |
+
|
| 806 |
+
@AlgebraicPredicate.register_many(acot, cot) # type:ignore
|
| 807 |
+
def _(expr, assumptions):
|
| 808 |
+
x = expr.args[0]
|
| 809 |
+
if ask(Q.algebraic(x), assumptions):
|
| 810 |
+
return False
|
| 811 |
+
|
| 812 |
+
@AlgebraicPredicate.register_many(acos, log) # type:ignore
|
| 813 |
+
def _(expr, assumptions):
|
| 814 |
+
x = expr.args[0]
|
| 815 |
+
if ask(Q.algebraic(x), assumptions):
|
| 816 |
+
return ask(~Q.nonzero(x - 1), assumptions)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/lra_satask.py
ADDED
|
@@ -0,0 +1,286 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.assumptions.assume import global_assumptions
|
| 2 |
+
from sympy.assumptions.cnf import CNF, EncodedCNF
|
| 3 |
+
from sympy.assumptions.ask import Q
|
| 4 |
+
from sympy.logic.inference import satisfiable
|
| 5 |
+
from sympy.logic.algorithms.lra_theory import UnhandledInput, ALLOWED_PRED
|
| 6 |
+
from sympy.matrices.kind import MatrixKind
|
| 7 |
+
from sympy.core.kind import NumberKind
|
| 8 |
+
from sympy.assumptions.assume import AppliedPredicate
|
| 9 |
+
from sympy.core.mul import Mul
|
| 10 |
+
from sympy.core.singleton import S
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def lra_satask(proposition, assumptions=True, context=global_assumptions):
|
| 14 |
+
"""
|
| 15 |
+
Function to evaluate the proposition with assumptions using SAT algorithm
|
| 16 |
+
in conjunction with an Linear Real Arithmetic theory solver.
|
| 17 |
+
|
| 18 |
+
Used to handle inequalities. Should eventually be depreciated and combined
|
| 19 |
+
into satask, but infinity handling and other things need to be implemented
|
| 20 |
+
before that can happen.
|
| 21 |
+
"""
|
| 22 |
+
props = CNF.from_prop(proposition)
|
| 23 |
+
_props = CNF.from_prop(~proposition)
|
| 24 |
+
|
| 25 |
+
cnf = CNF.from_prop(assumptions)
|
| 26 |
+
assumptions = EncodedCNF()
|
| 27 |
+
assumptions.from_cnf(cnf)
|
| 28 |
+
|
| 29 |
+
context_cnf = CNF()
|
| 30 |
+
if context:
|
| 31 |
+
context_cnf = context_cnf.extend(context)
|
| 32 |
+
|
| 33 |
+
assumptions.add_from_cnf(context_cnf)
|
| 34 |
+
|
| 35 |
+
return check_satisfiability(props, _props, assumptions)
|
| 36 |
+
|
| 37 |
+
# Some predicates such as Q.prime can't be handled by lra_satask.
|
| 38 |
+
# For example, (x > 0) & (x < 1) & Q.prime(x) is unsat but lra_satask would think it was sat.
|
| 39 |
+
# WHITE_LIST is a list of predicates that can always be handled.
|
| 40 |
+
WHITE_LIST = ALLOWED_PRED | {Q.positive, Q.negative, Q.zero, Q.nonzero, Q.nonpositive, Q.nonnegative,
|
| 41 |
+
Q.extended_positive, Q.extended_negative, Q.extended_nonpositive,
|
| 42 |
+
Q.extended_negative, Q.extended_nonzero, Q.negative_infinite,
|
| 43 |
+
Q.positive_infinite}
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def check_satisfiability(prop, _prop, factbase):
|
| 47 |
+
sat_true = factbase.copy()
|
| 48 |
+
sat_false = factbase.copy()
|
| 49 |
+
sat_true.add_from_cnf(prop)
|
| 50 |
+
sat_false.add_from_cnf(_prop)
|
| 51 |
+
|
| 52 |
+
all_pred, all_exprs = get_all_pred_and_expr_from_enc_cnf(sat_true)
|
| 53 |
+
|
| 54 |
+
for pred in all_pred:
|
| 55 |
+
if pred.function not in WHITE_LIST and pred.function != Q.ne:
|
| 56 |
+
raise UnhandledInput(f"LRASolver: {pred} is an unhandled predicate")
|
| 57 |
+
for expr in all_exprs:
|
| 58 |
+
if expr.kind == MatrixKind(NumberKind):
|
| 59 |
+
raise UnhandledInput(f"LRASolver: {expr} is of MatrixKind")
|
| 60 |
+
if expr == S.NaN:
|
| 61 |
+
raise UnhandledInput("LRASolver: nan")
|
| 62 |
+
|
| 63 |
+
# convert old assumptions into predicates and add them to sat_true and sat_false
|
| 64 |
+
# also check for unhandled predicates
|
| 65 |
+
for assm in extract_pred_from_old_assum(all_exprs):
|
| 66 |
+
n = len(sat_true.encoding)
|
| 67 |
+
if assm not in sat_true.encoding:
|
| 68 |
+
sat_true.encoding[assm] = n+1
|
| 69 |
+
sat_true.data.append([sat_true.encoding[assm]])
|
| 70 |
+
|
| 71 |
+
n = len(sat_false.encoding)
|
| 72 |
+
if assm not in sat_false.encoding:
|
| 73 |
+
sat_false.encoding[assm] = n+1
|
| 74 |
+
sat_false.data.append([sat_false.encoding[assm]])
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
sat_true = _preprocess(sat_true)
|
| 78 |
+
sat_false = _preprocess(sat_false)
|
| 79 |
+
|
| 80 |
+
can_be_true = satisfiable(sat_true, use_lra_theory=True) is not False
|
| 81 |
+
can_be_false = satisfiable(sat_false, use_lra_theory=True) is not False
|
| 82 |
+
|
| 83 |
+
if can_be_true and can_be_false:
|
| 84 |
+
return None
|
| 85 |
+
|
| 86 |
+
if can_be_true and not can_be_false:
|
| 87 |
+
return True
|
| 88 |
+
|
| 89 |
+
if not can_be_true and can_be_false:
|
| 90 |
+
return False
|
| 91 |
+
|
| 92 |
+
if not can_be_true and not can_be_false:
|
| 93 |
+
raise ValueError("Inconsistent assumptions")
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def _preprocess(enc_cnf):
|
| 97 |
+
"""
|
| 98 |
+
Returns an encoded cnf with only Q.eq, Q.gt, Q.lt,
|
| 99 |
+
Q.ge, and Q.le predicate.
|
| 100 |
+
|
| 101 |
+
Converts every unequality into a disjunction of strict
|
| 102 |
+
inequalities. For example, x != 3 would become
|
| 103 |
+
x < 3 OR x > 3.
|
| 104 |
+
|
| 105 |
+
Also converts all negated Q.ne predicates into
|
| 106 |
+
equalities.
|
| 107 |
+
"""
|
| 108 |
+
|
| 109 |
+
# loops through each literal in each clause
|
| 110 |
+
# to construct a new, preprocessed encodedCNF
|
| 111 |
+
|
| 112 |
+
enc_cnf = enc_cnf.copy()
|
| 113 |
+
cur_enc = 1
|
| 114 |
+
rev_encoding = {value: key for key, value in enc_cnf.encoding.items()}
|
| 115 |
+
|
| 116 |
+
new_encoding = {}
|
| 117 |
+
new_data = []
|
| 118 |
+
for clause in enc_cnf.data:
|
| 119 |
+
new_clause = []
|
| 120 |
+
for lit in clause:
|
| 121 |
+
if lit == 0:
|
| 122 |
+
new_clause.append(lit)
|
| 123 |
+
new_encoding[lit] = False
|
| 124 |
+
continue
|
| 125 |
+
prop = rev_encoding[abs(lit)]
|
| 126 |
+
negated = lit < 0
|
| 127 |
+
sign = (lit > 0) - (lit < 0)
|
| 128 |
+
|
| 129 |
+
prop = _pred_to_binrel(prop)
|
| 130 |
+
|
| 131 |
+
if not isinstance(prop, AppliedPredicate):
|
| 132 |
+
if prop not in new_encoding:
|
| 133 |
+
new_encoding[prop] = cur_enc
|
| 134 |
+
cur_enc += 1
|
| 135 |
+
lit = new_encoding[prop]
|
| 136 |
+
new_clause.append(sign*lit)
|
| 137 |
+
continue
|
| 138 |
+
|
| 139 |
+
|
| 140 |
+
if negated and prop.function == Q.eq:
|
| 141 |
+
negated = False
|
| 142 |
+
prop = Q.ne(*prop.arguments)
|
| 143 |
+
|
| 144 |
+
if prop.function == Q.ne:
|
| 145 |
+
arg1, arg2 = prop.arguments
|
| 146 |
+
if negated:
|
| 147 |
+
new_prop = Q.eq(arg1, arg2)
|
| 148 |
+
if new_prop not in new_encoding:
|
| 149 |
+
new_encoding[new_prop] = cur_enc
|
| 150 |
+
cur_enc += 1
|
| 151 |
+
|
| 152 |
+
new_enc = new_encoding[new_prop]
|
| 153 |
+
new_clause.append(new_enc)
|
| 154 |
+
continue
|
| 155 |
+
else:
|
| 156 |
+
new_props = (Q.gt(arg1, arg2), Q.lt(arg1, arg2))
|
| 157 |
+
for new_prop in new_props:
|
| 158 |
+
if new_prop not in new_encoding:
|
| 159 |
+
new_encoding[new_prop] = cur_enc
|
| 160 |
+
cur_enc += 1
|
| 161 |
+
|
| 162 |
+
new_enc = new_encoding[new_prop]
|
| 163 |
+
new_clause.append(new_enc)
|
| 164 |
+
continue
|
| 165 |
+
|
| 166 |
+
if prop.function == Q.eq and negated:
|
| 167 |
+
assert False
|
| 168 |
+
|
| 169 |
+
if prop not in new_encoding:
|
| 170 |
+
new_encoding[prop] = cur_enc
|
| 171 |
+
cur_enc += 1
|
| 172 |
+
new_clause.append(new_encoding[prop]*sign)
|
| 173 |
+
new_data.append(new_clause)
|
| 174 |
+
|
| 175 |
+
assert len(new_encoding) >= cur_enc - 1
|
| 176 |
+
|
| 177 |
+
enc_cnf = EncodedCNF(new_data, new_encoding)
|
| 178 |
+
return enc_cnf
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def _pred_to_binrel(pred):
|
| 182 |
+
if not isinstance(pred, AppliedPredicate):
|
| 183 |
+
return pred
|
| 184 |
+
|
| 185 |
+
if pred.function in pred_to_pos_neg_zero:
|
| 186 |
+
f = pred_to_pos_neg_zero[pred.function]
|
| 187 |
+
if f is False:
|
| 188 |
+
return False
|
| 189 |
+
pred = f(pred.arguments[0])
|
| 190 |
+
|
| 191 |
+
if pred.function == Q.positive:
|
| 192 |
+
pred = Q.gt(pred.arguments[0], 0)
|
| 193 |
+
elif pred.function == Q.negative:
|
| 194 |
+
pred = Q.lt(pred.arguments[0], 0)
|
| 195 |
+
elif pred.function == Q.zero:
|
| 196 |
+
pred = Q.eq(pred.arguments[0], 0)
|
| 197 |
+
elif pred.function == Q.nonpositive:
|
| 198 |
+
pred = Q.le(pred.arguments[0], 0)
|
| 199 |
+
elif pred.function == Q.nonnegative:
|
| 200 |
+
pred = Q.ge(pred.arguments[0], 0)
|
| 201 |
+
elif pred.function == Q.nonzero:
|
| 202 |
+
pred = Q.ne(pred.arguments[0], 0)
|
| 203 |
+
|
| 204 |
+
return pred
|
| 205 |
+
|
| 206 |
+
pred_to_pos_neg_zero = {
|
| 207 |
+
Q.extended_positive: Q.positive,
|
| 208 |
+
Q.extended_negative: Q.negative,
|
| 209 |
+
Q.extended_nonpositive: Q.nonpositive,
|
| 210 |
+
Q.extended_negative: Q.negative,
|
| 211 |
+
Q.extended_nonzero: Q.nonzero,
|
| 212 |
+
Q.negative_infinite: False,
|
| 213 |
+
Q.positive_infinite: False
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
def get_all_pred_and_expr_from_enc_cnf(enc_cnf):
|
| 217 |
+
all_exprs = set()
|
| 218 |
+
all_pred = set()
|
| 219 |
+
for pred in enc_cnf.encoding.keys():
|
| 220 |
+
if isinstance(pred, AppliedPredicate):
|
| 221 |
+
all_pred.add(pred)
|
| 222 |
+
all_exprs.update(pred.arguments)
|
| 223 |
+
|
| 224 |
+
return all_pred, all_exprs
|
| 225 |
+
|
| 226 |
+
def extract_pred_from_old_assum(all_exprs):
|
| 227 |
+
"""
|
| 228 |
+
Returns a list of relevant new assumption predicate
|
| 229 |
+
based on any old assumptions.
|
| 230 |
+
|
| 231 |
+
Raises an UnhandledInput exception if any of the assumptions are
|
| 232 |
+
unhandled.
|
| 233 |
+
|
| 234 |
+
Ignored predicate:
|
| 235 |
+
- commutative
|
| 236 |
+
- complex
|
| 237 |
+
- algebraic
|
| 238 |
+
- transcendental
|
| 239 |
+
- extended_real
|
| 240 |
+
- real
|
| 241 |
+
- all matrix predicate
|
| 242 |
+
- rational
|
| 243 |
+
- irrational
|
| 244 |
+
|
| 245 |
+
Example
|
| 246 |
+
=======
|
| 247 |
+
>>> from sympy.assumptions.lra_satask import extract_pred_from_old_assum
|
| 248 |
+
>>> from sympy import symbols
|
| 249 |
+
>>> x, y = symbols("x y", positive=True)
|
| 250 |
+
>>> extract_pred_from_old_assum([x, y, 2])
|
| 251 |
+
[Q.positive(x), Q.positive(y)]
|
| 252 |
+
"""
|
| 253 |
+
ret = []
|
| 254 |
+
for expr in all_exprs:
|
| 255 |
+
if not hasattr(expr, "free_symbols"):
|
| 256 |
+
continue
|
| 257 |
+
if len(expr.free_symbols) == 0:
|
| 258 |
+
continue
|
| 259 |
+
|
| 260 |
+
if expr.is_real is not True:
|
| 261 |
+
raise UnhandledInput(f"LRASolver: {expr} must be real")
|
| 262 |
+
# test for I times imaginary variable; such expressions are considered real
|
| 263 |
+
if isinstance(expr, Mul) and any(arg.is_real is not True for arg in expr.args):
|
| 264 |
+
raise UnhandledInput(f"LRASolver: {expr} must be real")
|
| 265 |
+
|
| 266 |
+
if expr.is_integer == True and expr.is_zero != True:
|
| 267 |
+
raise UnhandledInput(f"LRASolver: {expr} is an integer")
|
| 268 |
+
if expr.is_integer == False:
|
| 269 |
+
raise UnhandledInput(f"LRASolver: {expr} can't be an integer")
|
| 270 |
+
if expr.is_rational == False:
|
| 271 |
+
raise UnhandledInput(f"LRASolver: {expr} is irational")
|
| 272 |
+
|
| 273 |
+
if expr.is_zero:
|
| 274 |
+
ret.append(Q.zero(expr))
|
| 275 |
+
elif expr.is_positive:
|
| 276 |
+
ret.append(Q.positive(expr))
|
| 277 |
+
elif expr.is_negative:
|
| 278 |
+
ret.append(Q.negative(expr))
|
| 279 |
+
elif expr.is_nonzero:
|
| 280 |
+
ret.append(Q.nonzero(expr))
|
| 281 |
+
elif expr.is_nonpositive:
|
| 282 |
+
ret.append(Q.nonpositive(expr))
|
| 283 |
+
elif expr.is_nonnegative:
|
| 284 |
+
ret.append(Q.nonnegative(expr))
|
| 285 |
+
|
| 286 |
+
return ret
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/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 |
+
"""
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/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")
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/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)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/predicates/matrices.py
ADDED
|
@@ -0,0 +1,511 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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'.")
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/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 |
+
)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/predicates/order.py
ADDED
|
@@ -0,0 +1,390 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 'nonzero'. 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")
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/predicates/sets.py
ADDED
|
@@ -0,0 +1,399 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 NonIntegerPredicate(Predicate):
|
| 39 |
+
"""
|
| 40 |
+
Non-integer extended real predicate.
|
| 41 |
+
"""
|
| 42 |
+
name = 'noninteger'
|
| 43 |
+
handler = Dispatcher(
|
| 44 |
+
"NonIntegerHandler",
|
| 45 |
+
doc=("Handler for Q.noninteger.\n\n"
|
| 46 |
+
"Test that an expression is a non-integer extended real number.")
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
class RationalPredicate(Predicate):
|
| 51 |
+
"""
|
| 52 |
+
Rational number predicate.
|
| 53 |
+
|
| 54 |
+
Explanation
|
| 55 |
+
===========
|
| 56 |
+
|
| 57 |
+
``Q.rational(x)`` is true iff ``x`` belongs to the set of
|
| 58 |
+
rational numbers.
|
| 59 |
+
|
| 60 |
+
Examples
|
| 61 |
+
========
|
| 62 |
+
|
| 63 |
+
>>> from sympy import ask, Q, pi, S
|
| 64 |
+
>>> ask(Q.rational(0))
|
| 65 |
+
True
|
| 66 |
+
>>> ask(Q.rational(S(1)/2))
|
| 67 |
+
True
|
| 68 |
+
>>> ask(Q.rational(pi))
|
| 69 |
+
False
|
| 70 |
+
|
| 71 |
+
References
|
| 72 |
+
==========
|
| 73 |
+
|
| 74 |
+
.. [1] https://en.wikipedia.org/wiki/Rational_number
|
| 75 |
+
|
| 76 |
+
"""
|
| 77 |
+
name = 'rational'
|
| 78 |
+
handler = Dispatcher(
|
| 79 |
+
"RationalHandler",
|
| 80 |
+
doc=("Handler for Q.rational.\n\n"
|
| 81 |
+
"Test that an expression belongs to the field of rational numbers.")
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
class IrrationalPredicate(Predicate):
|
| 86 |
+
"""
|
| 87 |
+
Irrational number predicate.
|
| 88 |
+
|
| 89 |
+
Explanation
|
| 90 |
+
===========
|
| 91 |
+
|
| 92 |
+
``Q.irrational(x)`` is true iff ``x`` is any real number that
|
| 93 |
+
cannot be expressed as a ratio of integers.
|
| 94 |
+
|
| 95 |
+
Examples
|
| 96 |
+
========
|
| 97 |
+
|
| 98 |
+
>>> from sympy import ask, Q, pi, S, I
|
| 99 |
+
>>> ask(Q.irrational(0))
|
| 100 |
+
False
|
| 101 |
+
>>> ask(Q.irrational(S(1)/2))
|
| 102 |
+
False
|
| 103 |
+
>>> ask(Q.irrational(pi))
|
| 104 |
+
True
|
| 105 |
+
>>> ask(Q.irrational(I))
|
| 106 |
+
False
|
| 107 |
+
|
| 108 |
+
References
|
| 109 |
+
==========
|
| 110 |
+
|
| 111 |
+
.. [1] https://en.wikipedia.org/wiki/Irrational_number
|
| 112 |
+
|
| 113 |
+
"""
|
| 114 |
+
name = 'irrational'
|
| 115 |
+
handler = Dispatcher(
|
| 116 |
+
"IrrationalHandler",
|
| 117 |
+
doc=("Handler for Q.irrational.\n\n"
|
| 118 |
+
"Test that an expression is irrational numbers.")
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
class RealPredicate(Predicate):
|
| 123 |
+
r"""
|
| 124 |
+
Real number predicate.
|
| 125 |
+
|
| 126 |
+
Explanation
|
| 127 |
+
===========
|
| 128 |
+
|
| 129 |
+
``Q.real(x)`` is true iff ``x`` is a real number, i.e., it is in the
|
| 130 |
+
interval `(-\infty, \infty)`. Note that, in particular the
|
| 131 |
+
infinities are not real. Use ``Q.extended_real`` if you want to
|
| 132 |
+
consider those as well.
|
| 133 |
+
|
| 134 |
+
A few important facts about reals:
|
| 135 |
+
|
| 136 |
+
- Every real number is positive, negative, or zero. Furthermore,
|
| 137 |
+
because these sets are pairwise disjoint, each real number is
|
| 138 |
+
exactly one of those three.
|
| 139 |
+
|
| 140 |
+
- Every real number is also complex.
|
| 141 |
+
|
| 142 |
+
- Every real number is finite.
|
| 143 |
+
|
| 144 |
+
- Every real number is either rational or irrational.
|
| 145 |
+
|
| 146 |
+
- Every real number is either algebraic or transcendental.
|
| 147 |
+
|
| 148 |
+
- The facts ``Q.negative``, ``Q.zero``, ``Q.positive``,
|
| 149 |
+
``Q.nonnegative``, ``Q.nonpositive``, ``Q.nonzero``,
|
| 150 |
+
``Q.integer``, ``Q.rational``, and ``Q.irrational`` all imply
|
| 151 |
+
``Q.real``, as do all facts that imply those facts.
|
| 152 |
+
|
| 153 |
+
- The facts ``Q.algebraic``, and ``Q.transcendental`` do not imply
|
| 154 |
+
``Q.real``; they imply ``Q.complex``. An algebraic or
|
| 155 |
+
transcendental number may or may not be real.
|
| 156 |
+
|
| 157 |
+
- The "non" facts (i.e., ``Q.nonnegative``, ``Q.nonzero``,
|
| 158 |
+
``Q.nonpositive`` and ``Q.noninteger``) are not equivalent to
|
| 159 |
+
not the fact, but rather, not the fact *and* ``Q.real``.
|
| 160 |
+
For example, ``Q.nonnegative`` means ``~Q.negative & Q.real``.
|
| 161 |
+
So for example, ``I`` is not nonnegative, nonzero, or
|
| 162 |
+
nonpositive.
|
| 163 |
+
|
| 164 |
+
Examples
|
| 165 |
+
========
|
| 166 |
+
|
| 167 |
+
>>> from sympy import Q, ask, symbols
|
| 168 |
+
>>> x = symbols('x')
|
| 169 |
+
>>> ask(Q.real(x), Q.positive(x))
|
| 170 |
+
True
|
| 171 |
+
>>> ask(Q.real(0))
|
| 172 |
+
True
|
| 173 |
+
|
| 174 |
+
References
|
| 175 |
+
==========
|
| 176 |
+
|
| 177 |
+
.. [1] https://en.wikipedia.org/wiki/Real_number
|
| 178 |
+
|
| 179 |
+
"""
|
| 180 |
+
name = 'real'
|
| 181 |
+
handler = Dispatcher(
|
| 182 |
+
"RealHandler",
|
| 183 |
+
doc=("Handler for Q.real.\n\n"
|
| 184 |
+
"Test that an expression belongs to the field of real numbers.")
|
| 185 |
+
)
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
class ExtendedRealPredicate(Predicate):
|
| 189 |
+
r"""
|
| 190 |
+
Extended real predicate.
|
| 191 |
+
|
| 192 |
+
Explanation
|
| 193 |
+
===========
|
| 194 |
+
|
| 195 |
+
``Q.extended_real(x)`` is true iff ``x`` is a real number or
|
| 196 |
+
`\{-\infty, \infty\}`.
|
| 197 |
+
|
| 198 |
+
See documentation of ``Q.real`` for more information about related
|
| 199 |
+
facts.
|
| 200 |
+
|
| 201 |
+
Examples
|
| 202 |
+
========
|
| 203 |
+
|
| 204 |
+
>>> from sympy import ask, Q, oo, I
|
| 205 |
+
>>> ask(Q.extended_real(1))
|
| 206 |
+
True
|
| 207 |
+
>>> ask(Q.extended_real(I))
|
| 208 |
+
False
|
| 209 |
+
>>> ask(Q.extended_real(oo))
|
| 210 |
+
True
|
| 211 |
+
|
| 212 |
+
"""
|
| 213 |
+
name = 'extended_real'
|
| 214 |
+
handler = Dispatcher(
|
| 215 |
+
"ExtendedRealHandler",
|
| 216 |
+
doc=("Handler for Q.extended_real.\n\n"
|
| 217 |
+
"Test that an expression belongs to the field of extended real\n"
|
| 218 |
+
"numbers, that is real numbers union {Infinity, -Infinity}.")
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
class HermitianPredicate(Predicate):
|
| 223 |
+
"""
|
| 224 |
+
Hermitian predicate.
|
| 225 |
+
|
| 226 |
+
Explanation
|
| 227 |
+
===========
|
| 228 |
+
|
| 229 |
+
``ask(Q.hermitian(x))`` is true iff ``x`` belongs to the set of
|
| 230 |
+
Hermitian operators.
|
| 231 |
+
|
| 232 |
+
References
|
| 233 |
+
==========
|
| 234 |
+
|
| 235 |
+
.. [1] https://mathworld.wolfram.com/HermitianOperator.html
|
| 236 |
+
|
| 237 |
+
"""
|
| 238 |
+
# TODO: Add examples
|
| 239 |
+
name = 'hermitian'
|
| 240 |
+
handler = Dispatcher(
|
| 241 |
+
"HermitianHandler",
|
| 242 |
+
doc=("Handler for Q.hermitian.\n\n"
|
| 243 |
+
"Test that an expression belongs to the field of Hermitian operators.")
|
| 244 |
+
)
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
class ComplexPredicate(Predicate):
|
| 248 |
+
"""
|
| 249 |
+
Complex number predicate.
|
| 250 |
+
|
| 251 |
+
Explanation
|
| 252 |
+
===========
|
| 253 |
+
|
| 254 |
+
``Q.complex(x)`` is true iff ``x`` belongs to the set of complex
|
| 255 |
+
numbers. Note that every complex number is finite.
|
| 256 |
+
|
| 257 |
+
Examples
|
| 258 |
+
========
|
| 259 |
+
|
| 260 |
+
>>> from sympy import Q, Symbol, ask, I, oo
|
| 261 |
+
>>> x = Symbol('x')
|
| 262 |
+
>>> ask(Q.complex(0))
|
| 263 |
+
True
|
| 264 |
+
>>> ask(Q.complex(2 + 3*I))
|
| 265 |
+
True
|
| 266 |
+
>>> ask(Q.complex(oo))
|
| 267 |
+
False
|
| 268 |
+
|
| 269 |
+
References
|
| 270 |
+
==========
|
| 271 |
+
|
| 272 |
+
.. [1] https://en.wikipedia.org/wiki/Complex_number
|
| 273 |
+
|
| 274 |
+
"""
|
| 275 |
+
name = 'complex'
|
| 276 |
+
handler = Dispatcher(
|
| 277 |
+
"ComplexHandler",
|
| 278 |
+
doc=("Handler for Q.complex.\n\n"
|
| 279 |
+
"Test that an expression belongs to the field of complex numbers.")
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
class ImaginaryPredicate(Predicate):
|
| 284 |
+
"""
|
| 285 |
+
Imaginary number predicate.
|
| 286 |
+
|
| 287 |
+
Explanation
|
| 288 |
+
===========
|
| 289 |
+
|
| 290 |
+
``Q.imaginary(x)`` is true iff ``x`` can be written as a real
|
| 291 |
+
number multiplied by the imaginary unit ``I``. Please note that ``0``
|
| 292 |
+
is not considered to be an imaginary number.
|
| 293 |
+
|
| 294 |
+
Examples
|
| 295 |
+
========
|
| 296 |
+
|
| 297 |
+
>>> from sympy import Q, ask, I
|
| 298 |
+
>>> ask(Q.imaginary(3*I))
|
| 299 |
+
True
|
| 300 |
+
>>> ask(Q.imaginary(2 + 3*I))
|
| 301 |
+
False
|
| 302 |
+
>>> ask(Q.imaginary(0))
|
| 303 |
+
False
|
| 304 |
+
|
| 305 |
+
References
|
| 306 |
+
==========
|
| 307 |
+
|
| 308 |
+
.. [1] https://en.wikipedia.org/wiki/Imaginary_number
|
| 309 |
+
|
| 310 |
+
"""
|
| 311 |
+
name = 'imaginary'
|
| 312 |
+
handler = Dispatcher(
|
| 313 |
+
"ImaginaryHandler",
|
| 314 |
+
doc=("Handler for Q.imaginary.\n\n"
|
| 315 |
+
"Test that an expression belongs to the field of imaginary numbers,\n"
|
| 316 |
+
"that is, numbers in the form x*I, where x is real.")
|
| 317 |
+
)
|
| 318 |
+
|
| 319 |
+
|
| 320 |
+
class AntihermitianPredicate(Predicate):
|
| 321 |
+
"""
|
| 322 |
+
Antihermitian predicate.
|
| 323 |
+
|
| 324 |
+
Explanation
|
| 325 |
+
===========
|
| 326 |
+
|
| 327 |
+
``Q.antihermitian(x)`` is true iff ``x`` belongs to the field of
|
| 328 |
+
antihermitian operators, i.e., operators in the form ``x*I``, where
|
| 329 |
+
``x`` is Hermitian.
|
| 330 |
+
|
| 331 |
+
References
|
| 332 |
+
==========
|
| 333 |
+
|
| 334 |
+
.. [1] https://mathworld.wolfram.com/HermitianOperator.html
|
| 335 |
+
|
| 336 |
+
"""
|
| 337 |
+
# TODO: Add examples
|
| 338 |
+
name = 'antihermitian'
|
| 339 |
+
handler = Dispatcher(
|
| 340 |
+
"AntiHermitianHandler",
|
| 341 |
+
doc=("Handler for Q.antihermitian.\n\n"
|
| 342 |
+
"Test that an expression belongs to the field of anti-Hermitian\n"
|
| 343 |
+
"operators, that is, operators in the form x*I, where x is Hermitian.")
|
| 344 |
+
)
|
| 345 |
+
|
| 346 |
+
|
| 347 |
+
class AlgebraicPredicate(Predicate):
|
| 348 |
+
r"""
|
| 349 |
+
Algebraic number predicate.
|
| 350 |
+
|
| 351 |
+
Explanation
|
| 352 |
+
===========
|
| 353 |
+
|
| 354 |
+
``Q.algebraic(x)`` is true iff ``x`` belongs to the set of
|
| 355 |
+
algebraic numbers. ``x`` is algebraic if there is some polynomial
|
| 356 |
+
in ``p(x)\in \mathbb\{Q\}[x]`` such that ``p(x) = 0``.
|
| 357 |
+
|
| 358 |
+
Examples
|
| 359 |
+
========
|
| 360 |
+
|
| 361 |
+
>>> from sympy import ask, Q, sqrt, I, pi
|
| 362 |
+
>>> ask(Q.algebraic(sqrt(2)))
|
| 363 |
+
True
|
| 364 |
+
>>> ask(Q.algebraic(I))
|
| 365 |
+
True
|
| 366 |
+
>>> ask(Q.algebraic(pi))
|
| 367 |
+
False
|
| 368 |
+
|
| 369 |
+
References
|
| 370 |
+
==========
|
| 371 |
+
|
| 372 |
+
.. [1] https://en.wikipedia.org/wiki/Algebraic_number
|
| 373 |
+
|
| 374 |
+
"""
|
| 375 |
+
name = 'algebraic'
|
| 376 |
+
AlgebraicHandler = Dispatcher(
|
| 377 |
+
"AlgebraicHandler",
|
| 378 |
+
doc="""Handler for Q.algebraic key."""
|
| 379 |
+
)
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
class TranscendentalPredicate(Predicate):
|
| 383 |
+
"""
|
| 384 |
+
Transcedental number predicate.
|
| 385 |
+
|
| 386 |
+
Explanation
|
| 387 |
+
===========
|
| 388 |
+
|
| 389 |
+
``Q.transcendental(x)`` is true iff ``x`` belongs to the set of
|
| 390 |
+
transcendental numbers. A transcendental number is a real
|
| 391 |
+
or complex number that is not algebraic.
|
| 392 |
+
|
| 393 |
+
"""
|
| 394 |
+
# TODO: Add examples
|
| 395 |
+
name = 'transcendental'
|
| 396 |
+
handler = Dispatcher(
|
| 397 |
+
"Transcendental",
|
| 398 |
+
doc="""Handler for Q.transcendental key."""
|
| 399 |
+
)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/refine.py
ADDED
|
@@ -0,0 +1,405 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
}
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/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
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (500 Bytes). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/binrel.cpython-310.pyc
ADDED
|
Binary file (6.56 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/relation/__pycache__/equality.cpython-310.pyc
ADDED
|
Binary file (7.73 kB). View file
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/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 |
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/satask.py
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.core.kind import NumberKind, UndefinedKind
|
| 8 |
+
from sympy.assumptions.ask_generated import get_all_known_matrix_facts, get_all_known_number_facts
|
| 9 |
+
from sympy.assumptions.assume import global_assumptions, AppliedPredicate
|
| 10 |
+
from sympy.assumptions.sathandlers import class_fact_registry
|
| 11 |
+
from sympy.core import oo
|
| 12 |
+
from sympy.logic.inference import satisfiable
|
| 13 |
+
from sympy.assumptions.cnf import CNF, EncodedCNF
|
| 14 |
+
from sympy.matrices.kind import MatrixKind
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def satask(proposition, assumptions=True, context=global_assumptions,
|
| 18 |
+
use_known_facts=True, iterations=oo):
|
| 19 |
+
"""
|
| 20 |
+
Function to evaluate the proposition with assumptions using SAT algorithm.
|
| 21 |
+
|
| 22 |
+
This function extracts every fact relevant to the expressions composing
|
| 23 |
+
proposition and assumptions. For example, if a predicate containing
|
| 24 |
+
``Abs(x)`` is proposed, then ``Q.zero(Abs(x)) | Q.positive(Abs(x))``
|
| 25 |
+
will be found and passed to SAT solver because ``Q.nonnegative`` is
|
| 26 |
+
registered as a fact for ``Abs``.
|
| 27 |
+
|
| 28 |
+
Proposition is evaluated to ``True`` or ``False`` if the truth value can be
|
| 29 |
+
determined. If not, ``None`` is returned.
|
| 30 |
+
|
| 31 |
+
Parameters
|
| 32 |
+
==========
|
| 33 |
+
|
| 34 |
+
proposition : Any boolean expression.
|
| 35 |
+
Proposition which will be evaluated to boolean value.
|
| 36 |
+
|
| 37 |
+
assumptions : Any boolean expression, optional.
|
| 38 |
+
Local assumptions to evaluate the *proposition*.
|
| 39 |
+
|
| 40 |
+
context : AssumptionsContext, optional.
|
| 41 |
+
Default assumptions to evaluate the *proposition*. By default,
|
| 42 |
+
this is ``sympy.assumptions.global_assumptions`` variable.
|
| 43 |
+
|
| 44 |
+
use_known_facts : bool, optional.
|
| 45 |
+
If ``True``, facts from ``sympy.assumptions.ask_generated``
|
| 46 |
+
module are passed to SAT solver as well.
|
| 47 |
+
|
| 48 |
+
iterations : int, optional.
|
| 49 |
+
Number of times that relevant facts are recursively extracted.
|
| 50 |
+
Default is infinite times until no new fact is found.
|
| 51 |
+
|
| 52 |
+
Returns
|
| 53 |
+
=======
|
| 54 |
+
|
| 55 |
+
``True``, ``False``, or ``None``
|
| 56 |
+
|
| 57 |
+
Examples
|
| 58 |
+
========
|
| 59 |
+
|
| 60 |
+
>>> from sympy import Abs, Q
|
| 61 |
+
>>> from sympy.assumptions.satask import satask
|
| 62 |
+
>>> from sympy.abc import x
|
| 63 |
+
>>> satask(Q.zero(Abs(x)), Q.zero(x))
|
| 64 |
+
True
|
| 65 |
+
|
| 66 |
+
"""
|
| 67 |
+
props = CNF.from_prop(proposition)
|
| 68 |
+
_props = CNF.from_prop(~proposition)
|
| 69 |
+
|
| 70 |
+
assumptions = CNF.from_prop(assumptions)
|
| 71 |
+
|
| 72 |
+
context_cnf = CNF()
|
| 73 |
+
if context:
|
| 74 |
+
context_cnf = context_cnf.extend(context)
|
| 75 |
+
|
| 76 |
+
sat = get_all_relevant_facts(props, assumptions, context_cnf,
|
| 77 |
+
use_known_facts=use_known_facts, iterations=iterations)
|
| 78 |
+
sat.add_from_cnf(assumptions)
|
| 79 |
+
if context:
|
| 80 |
+
sat.add_from_cnf(context_cnf)
|
| 81 |
+
|
| 82 |
+
return check_satisfiability(props, _props, sat)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def check_satisfiability(prop, _prop, factbase):
|
| 86 |
+
sat_true = factbase.copy()
|
| 87 |
+
sat_false = factbase.copy()
|
| 88 |
+
sat_true.add_from_cnf(prop)
|
| 89 |
+
sat_false.add_from_cnf(_prop)
|
| 90 |
+
can_be_true = satisfiable(sat_true)
|
| 91 |
+
can_be_false = satisfiable(sat_false)
|
| 92 |
+
|
| 93 |
+
if can_be_true and can_be_false:
|
| 94 |
+
return None
|
| 95 |
+
|
| 96 |
+
if can_be_true and not can_be_false:
|
| 97 |
+
return True
|
| 98 |
+
|
| 99 |
+
if not can_be_true and can_be_false:
|
| 100 |
+
return False
|
| 101 |
+
|
| 102 |
+
if not can_be_true and not can_be_false:
|
| 103 |
+
# TODO: Run additional checks to see which combination of the
|
| 104 |
+
# assumptions, global_assumptions, and relevant_facts are
|
| 105 |
+
# inconsistent.
|
| 106 |
+
raise ValueError("Inconsistent assumptions")
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
def extract_predargs(proposition, assumptions=None, context=None):
|
| 110 |
+
"""
|
| 111 |
+
Extract every expression in the argument of predicates from *proposition*,
|
| 112 |
+
*assumptions* and *context*.
|
| 113 |
+
|
| 114 |
+
Parameters
|
| 115 |
+
==========
|
| 116 |
+
|
| 117 |
+
proposition : sympy.assumptions.cnf.CNF
|
| 118 |
+
|
| 119 |
+
assumptions : sympy.assumptions.cnf.CNF, optional.
|
| 120 |
+
|
| 121 |
+
context : sympy.assumptions.cnf.CNF, optional.
|
| 122 |
+
CNF generated from assumptions context.
|
| 123 |
+
|
| 124 |
+
Examples
|
| 125 |
+
========
|
| 126 |
+
|
| 127 |
+
>>> from sympy import Q, Abs
|
| 128 |
+
>>> from sympy.assumptions.cnf import CNF
|
| 129 |
+
>>> from sympy.assumptions.satask import extract_predargs
|
| 130 |
+
>>> from sympy.abc import x, y
|
| 131 |
+
>>> props = CNF.from_prop(Q.zero(Abs(x*y)))
|
| 132 |
+
>>> assump = CNF.from_prop(Q.zero(x) & Q.zero(y))
|
| 133 |
+
>>> extract_predargs(props, assump)
|
| 134 |
+
{x, y, Abs(x*y)}
|
| 135 |
+
|
| 136 |
+
"""
|
| 137 |
+
req_keys = find_symbols(proposition)
|
| 138 |
+
keys = proposition.all_predicates()
|
| 139 |
+
# XXX: We need this since True/False are not Basic
|
| 140 |
+
lkeys = set()
|
| 141 |
+
if assumptions:
|
| 142 |
+
lkeys |= assumptions.all_predicates()
|
| 143 |
+
if context:
|
| 144 |
+
lkeys |= context.all_predicates()
|
| 145 |
+
|
| 146 |
+
lkeys = lkeys - {S.true, S.false}
|
| 147 |
+
tmp_keys = None
|
| 148 |
+
while tmp_keys != set():
|
| 149 |
+
tmp = set()
|
| 150 |
+
for l in lkeys:
|
| 151 |
+
syms = find_symbols(l)
|
| 152 |
+
if (syms & req_keys) != set():
|
| 153 |
+
tmp |= syms
|
| 154 |
+
tmp_keys = tmp - req_keys
|
| 155 |
+
req_keys |= tmp_keys
|
| 156 |
+
keys |= {l for l in lkeys if find_symbols(l) & req_keys != set()}
|
| 157 |
+
|
| 158 |
+
exprs = set()
|
| 159 |
+
for key in keys:
|
| 160 |
+
if isinstance(key, AppliedPredicate):
|
| 161 |
+
exprs |= set(key.arguments)
|
| 162 |
+
else:
|
| 163 |
+
exprs.add(key)
|
| 164 |
+
return exprs
|
| 165 |
+
|
| 166 |
+
def find_symbols(pred):
|
| 167 |
+
"""
|
| 168 |
+
Find every :obj:`~.Symbol` in *pred*.
|
| 169 |
+
|
| 170 |
+
Parameters
|
| 171 |
+
==========
|
| 172 |
+
|
| 173 |
+
pred : sympy.assumptions.cnf.CNF, or any Expr.
|
| 174 |
+
|
| 175 |
+
"""
|
| 176 |
+
if isinstance(pred, CNF):
|
| 177 |
+
symbols = set()
|
| 178 |
+
for a in pred.all_predicates():
|
| 179 |
+
symbols |= find_symbols(a)
|
| 180 |
+
return symbols
|
| 181 |
+
return pred.atoms(Symbol)
|
| 182 |
+
|
| 183 |
+
|
| 184 |
+
def get_relevant_clsfacts(exprs, relevant_facts=None):
|
| 185 |
+
"""
|
| 186 |
+
Extract relevant facts from the items in *exprs*. Facts are defined in
|
| 187 |
+
``assumptions.sathandlers`` module.
|
| 188 |
+
|
| 189 |
+
This function is recursively called by ``get_all_relevant_facts()``.
|
| 190 |
+
|
| 191 |
+
Parameters
|
| 192 |
+
==========
|
| 193 |
+
|
| 194 |
+
exprs : set
|
| 195 |
+
Expressions whose relevant facts are searched.
|
| 196 |
+
|
| 197 |
+
relevant_facts : sympy.assumptions.cnf.CNF, optional.
|
| 198 |
+
Pre-discovered relevant facts.
|
| 199 |
+
|
| 200 |
+
Returns
|
| 201 |
+
=======
|
| 202 |
+
|
| 203 |
+
exprs : set
|
| 204 |
+
Candidates for next relevant fact searching.
|
| 205 |
+
|
| 206 |
+
relevant_facts : sympy.assumptions.cnf.CNF
|
| 207 |
+
Updated relevant facts.
|
| 208 |
+
|
| 209 |
+
Examples
|
| 210 |
+
========
|
| 211 |
+
|
| 212 |
+
Here, we will see how facts relevant to ``Abs(x*y)`` are recursively
|
| 213 |
+
extracted. On the first run, set containing the expression is passed
|
| 214 |
+
without pre-discovered relevant facts. The result is a set containing
|
| 215 |
+
candidates for next run, and ``CNF()`` instance containing facts
|
| 216 |
+
which are relevant to ``Abs`` and its argument.
|
| 217 |
+
|
| 218 |
+
>>> from sympy import Abs
|
| 219 |
+
>>> from sympy.assumptions.satask import get_relevant_clsfacts
|
| 220 |
+
>>> from sympy.abc import x, y
|
| 221 |
+
>>> exprs = {Abs(x*y)}
|
| 222 |
+
>>> exprs, facts = get_relevant_clsfacts(exprs)
|
| 223 |
+
>>> exprs
|
| 224 |
+
{x*y}
|
| 225 |
+
>>> facts.clauses #doctest: +SKIP
|
| 226 |
+
{frozenset({Literal(Q.odd(Abs(x*y)), False), Literal(Q.odd(x*y), True)}),
|
| 227 |
+
frozenset({Literal(Q.zero(Abs(x*y)), False), Literal(Q.zero(x*y), True)}),
|
| 228 |
+
frozenset({Literal(Q.even(Abs(x*y)), False), Literal(Q.even(x*y), True)}),
|
| 229 |
+
frozenset({Literal(Q.zero(Abs(x*y)), True), Literal(Q.zero(x*y), False)}),
|
| 230 |
+
frozenset({Literal(Q.even(Abs(x*y)), False),
|
| 231 |
+
Literal(Q.odd(Abs(x*y)), False),
|
| 232 |
+
Literal(Q.odd(x*y), True)}),
|
| 233 |
+
frozenset({Literal(Q.even(Abs(x*y)), False),
|
| 234 |
+
Literal(Q.even(x*y), True),
|
| 235 |
+
Literal(Q.odd(Abs(x*y)), False)}),
|
| 236 |
+
frozenset({Literal(Q.positive(Abs(x*y)), False),
|
| 237 |
+
Literal(Q.zero(Abs(x*y)), False)})}
|
| 238 |
+
|
| 239 |
+
We pass the first run's results to the second run, and get the expressions
|
| 240 |
+
for next run and updated facts.
|
| 241 |
+
|
| 242 |
+
>>> exprs, facts = get_relevant_clsfacts(exprs, relevant_facts=facts)
|
| 243 |
+
>>> exprs
|
| 244 |
+
{x, y}
|
| 245 |
+
|
| 246 |
+
On final run, no more candidate is returned thus we know that all
|
| 247 |
+
relevant facts are successfully retrieved.
|
| 248 |
+
|
| 249 |
+
>>> exprs, facts = get_relevant_clsfacts(exprs, relevant_facts=facts)
|
| 250 |
+
>>> exprs
|
| 251 |
+
set()
|
| 252 |
+
|
| 253 |
+
"""
|
| 254 |
+
if not relevant_facts:
|
| 255 |
+
relevant_facts = CNF()
|
| 256 |
+
|
| 257 |
+
newexprs = set()
|
| 258 |
+
for expr in exprs:
|
| 259 |
+
for fact in class_fact_registry(expr):
|
| 260 |
+
newfact = CNF.to_CNF(fact)
|
| 261 |
+
relevant_facts = relevant_facts._and(newfact)
|
| 262 |
+
for key in newfact.all_predicates():
|
| 263 |
+
if isinstance(key, AppliedPredicate):
|
| 264 |
+
newexprs |= set(key.arguments)
|
| 265 |
+
|
| 266 |
+
return newexprs - exprs, relevant_facts
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
def get_all_relevant_facts(proposition, assumptions, context,
|
| 270 |
+
use_known_facts=True, iterations=oo):
|
| 271 |
+
"""
|
| 272 |
+
Extract all relevant facts from *proposition* and *assumptions*.
|
| 273 |
+
|
| 274 |
+
This function extracts the facts by recursively calling
|
| 275 |
+
``get_relevant_clsfacts()``. Extracted facts are converted to
|
| 276 |
+
``EncodedCNF`` and returned.
|
| 277 |
+
|
| 278 |
+
Parameters
|
| 279 |
+
==========
|
| 280 |
+
|
| 281 |
+
proposition : sympy.assumptions.cnf.CNF
|
| 282 |
+
CNF generated from proposition expression.
|
| 283 |
+
|
| 284 |
+
assumptions : sympy.assumptions.cnf.CNF
|
| 285 |
+
CNF generated from assumption expression.
|
| 286 |
+
|
| 287 |
+
context : sympy.assumptions.cnf.CNF
|
| 288 |
+
CNF generated from assumptions context.
|
| 289 |
+
|
| 290 |
+
use_known_facts : bool, optional.
|
| 291 |
+
If ``True``, facts from ``sympy.assumptions.ask_generated``
|
| 292 |
+
module are encoded as well.
|
| 293 |
+
|
| 294 |
+
iterations : int, optional.
|
| 295 |
+
Number of times that relevant facts are recursively extracted.
|
| 296 |
+
Default is infinite times until no new fact is found.
|
| 297 |
+
|
| 298 |
+
Returns
|
| 299 |
+
=======
|
| 300 |
+
|
| 301 |
+
sympy.assumptions.cnf.EncodedCNF
|
| 302 |
+
|
| 303 |
+
Examples
|
| 304 |
+
========
|
| 305 |
+
|
| 306 |
+
>>> from sympy import Q
|
| 307 |
+
>>> from sympy.assumptions.cnf import CNF
|
| 308 |
+
>>> from sympy.assumptions.satask import get_all_relevant_facts
|
| 309 |
+
>>> from sympy.abc import x, y
|
| 310 |
+
>>> props = CNF.from_prop(Q.nonzero(x*y))
|
| 311 |
+
>>> assump = CNF.from_prop(Q.nonzero(x))
|
| 312 |
+
>>> context = CNF.from_prop(Q.nonzero(y))
|
| 313 |
+
>>> get_all_relevant_facts(props, assump, context) #doctest: +SKIP
|
| 314 |
+
<sympy.assumptions.cnf.EncodedCNF at 0x7f09faa6ccd0>
|
| 315 |
+
|
| 316 |
+
"""
|
| 317 |
+
# The relevant facts might introduce new keys, e.g., Q.zero(x*y) will
|
| 318 |
+
# introduce the keys Q.zero(x) and Q.zero(y), so we need to run it until
|
| 319 |
+
# we stop getting new things. Hopefully this strategy won't lead to an
|
| 320 |
+
# infinite loop in the future.
|
| 321 |
+
i = 0
|
| 322 |
+
relevant_facts = CNF()
|
| 323 |
+
all_exprs = set()
|
| 324 |
+
while True:
|
| 325 |
+
if i == 0:
|
| 326 |
+
exprs = extract_predargs(proposition, assumptions, context)
|
| 327 |
+
all_exprs |= exprs
|
| 328 |
+
exprs, relevant_facts = get_relevant_clsfacts(exprs, relevant_facts)
|
| 329 |
+
i += 1
|
| 330 |
+
if i >= iterations:
|
| 331 |
+
break
|
| 332 |
+
if not exprs:
|
| 333 |
+
break
|
| 334 |
+
|
| 335 |
+
if use_known_facts:
|
| 336 |
+
known_facts_CNF = CNF()
|
| 337 |
+
|
| 338 |
+
if any(expr.kind == MatrixKind(NumberKind) for expr in all_exprs):
|
| 339 |
+
known_facts_CNF.add_clauses(get_all_known_matrix_facts())
|
| 340 |
+
# check for undefinedKind since kind system isn't fully implemented
|
| 341 |
+
if any(((expr.kind == NumberKind) or (expr.kind == UndefinedKind)) for expr in all_exprs):
|
| 342 |
+
known_facts_CNF.add_clauses(get_all_known_number_facts())
|
| 343 |
+
|
| 344 |
+
kf_encoded = EncodedCNF()
|
| 345 |
+
kf_encoded.from_cnf(known_facts_CNF)
|
| 346 |
+
|
| 347 |
+
def translate_literal(lit, delta):
|
| 348 |
+
if lit > 0:
|
| 349 |
+
return lit + delta
|
| 350 |
+
else:
|
| 351 |
+
return lit - delta
|
| 352 |
+
|
| 353 |
+
def translate_data(data, delta):
|
| 354 |
+
return [{translate_literal(i, delta) for i in clause} for clause in data]
|
| 355 |
+
data = []
|
| 356 |
+
symbols = []
|
| 357 |
+
n_lit = len(kf_encoded.symbols)
|
| 358 |
+
for i, expr in enumerate(all_exprs):
|
| 359 |
+
symbols += [pred(expr) for pred in kf_encoded.symbols]
|
| 360 |
+
data += translate_data(kf_encoded.data, i * n_lit)
|
| 361 |
+
|
| 362 |
+
encoding = dict(list(zip(symbols, range(1, len(symbols)+1))))
|
| 363 |
+
ctx = EncodedCNF(data, encoding)
|
| 364 |
+
else:
|
| 365 |
+
ctx = EncodedCNF()
|
| 366 |
+
|
| 367 |
+
ctx.add_from_cnf(relevant_facts)
|
| 368 |
+
|
| 369 |
+
return ctx
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/sathandlers.py
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
ret.update(h(expr) for h in handlers1)
|
| 188 |
+
for h in handlers2:
|
| 189 |
+
ret.update(h(expr))
|
| 190 |
+
return ret
|
| 191 |
+
|
| 192 |
+
class_fact_registry = ClassFactRegistry()
|
| 193 |
+
|
| 194 |
+
|
| 195 |
+
|
| 196 |
+
### Class fact registration ###
|
| 197 |
+
|
| 198 |
+
x = Symbol('x')
|
| 199 |
+
|
| 200 |
+
## Abs ##
|
| 201 |
+
|
| 202 |
+
@class_fact_registry.multiregister(Abs)
|
| 203 |
+
def _(expr):
|
| 204 |
+
arg = expr.args[0]
|
| 205 |
+
return [Q.nonnegative(expr),
|
| 206 |
+
Equivalent(~Q.zero(arg), ~Q.zero(expr)),
|
| 207 |
+
Q.even(arg) >> Q.even(expr),
|
| 208 |
+
Q.odd(arg) >> Q.odd(expr),
|
| 209 |
+
Q.integer(arg) >> Q.integer(expr),
|
| 210 |
+
]
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
### Add ##
|
| 214 |
+
|
| 215 |
+
@class_fact_registry.multiregister(Add)
|
| 216 |
+
def _(expr):
|
| 217 |
+
return [allargs(x, Q.positive(x), expr) >> Q.positive(expr),
|
| 218 |
+
allargs(x, Q.negative(x), expr) >> Q.negative(expr),
|
| 219 |
+
allargs(x, Q.real(x), expr) >> Q.real(expr),
|
| 220 |
+
allargs(x, Q.rational(x), expr) >> Q.rational(expr),
|
| 221 |
+
allargs(x, Q.integer(x), expr) >> Q.integer(expr),
|
| 222 |
+
exactlyonearg(x, ~Q.integer(x), expr) >> ~Q.integer(expr),
|
| 223 |
+
]
|
| 224 |
+
|
| 225 |
+
@class_fact_registry.register(Add)
|
| 226 |
+
def _(expr):
|
| 227 |
+
allargs_real = allargs(x, Q.real(x), expr)
|
| 228 |
+
onearg_irrational = exactlyonearg(x, Q.irrational(x), expr)
|
| 229 |
+
return Implies(allargs_real, Implies(onearg_irrational, Q.irrational(expr)))
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
### Mul ###
|
| 233 |
+
|
| 234 |
+
@class_fact_registry.multiregister(Mul)
|
| 235 |
+
def _(expr):
|
| 236 |
+
return [Equivalent(Q.zero(expr), anyarg(x, Q.zero(x), expr)),
|
| 237 |
+
allargs(x, Q.positive(x), expr) >> Q.positive(expr),
|
| 238 |
+
allargs(x, Q.real(x), expr) >> Q.real(expr),
|
| 239 |
+
allargs(x, Q.rational(x), expr) >> Q.rational(expr),
|
| 240 |
+
allargs(x, Q.integer(x), expr) >> Q.integer(expr),
|
| 241 |
+
exactlyonearg(x, ~Q.rational(x), expr) >> ~Q.integer(expr),
|
| 242 |
+
allargs(x, Q.commutative(x), expr) >> Q.commutative(expr),
|
| 243 |
+
]
|
| 244 |
+
|
| 245 |
+
@class_fact_registry.register(Mul)
|
| 246 |
+
def _(expr):
|
| 247 |
+
# Implicitly assumes Mul has more than one arg
|
| 248 |
+
# Would be allargs(x, Q.prime(x) | Q.composite(x)) except 1 is composite
|
| 249 |
+
# More advanced prime assumptions will require inequalities, as 1 provides
|
| 250 |
+
# a corner case.
|
| 251 |
+
allargs_prime = allargs(x, Q.prime(x), expr)
|
| 252 |
+
return Implies(allargs_prime, ~Q.prime(expr))
|
| 253 |
+
|
| 254 |
+
@class_fact_registry.register(Mul)
|
| 255 |
+
def _(expr):
|
| 256 |
+
# General Case: Odd number of imaginary args implies mul is imaginary(To be implemented)
|
| 257 |
+
allargs_imag_or_real = allargs(x, Q.imaginary(x) | Q.real(x), expr)
|
| 258 |
+
onearg_imaginary = exactlyonearg(x, Q.imaginary(x), expr)
|
| 259 |
+
return Implies(allargs_imag_or_real, Implies(onearg_imaginary, Q.imaginary(expr)))
|
| 260 |
+
|
| 261 |
+
@class_fact_registry.register(Mul)
|
| 262 |
+
def _(expr):
|
| 263 |
+
allargs_real = allargs(x, Q.real(x), expr)
|
| 264 |
+
onearg_irrational = exactlyonearg(x, Q.irrational(x), expr)
|
| 265 |
+
return Implies(allargs_real, Implies(onearg_irrational, Q.irrational(expr)))
|
| 266 |
+
|
| 267 |
+
@class_fact_registry.register(Mul)
|
| 268 |
+
def _(expr):
|
| 269 |
+
# Including the integer qualification means we don't need to add any facts
|
| 270 |
+
# for odd, since the assumptions already know that every integer is
|
| 271 |
+
# exactly one of even or odd.
|
| 272 |
+
allargs_integer = allargs(x, Q.integer(x), expr)
|
| 273 |
+
anyarg_even = anyarg(x, Q.even(x), expr)
|
| 274 |
+
return Implies(allargs_integer, Equivalent(anyarg_even, Q.even(expr)))
|
| 275 |
+
|
| 276 |
+
|
| 277 |
+
### MatMul ###
|
| 278 |
+
|
| 279 |
+
@class_fact_registry.register(MatMul)
|
| 280 |
+
def _(expr):
|
| 281 |
+
allargs_square = allargs(x, Q.square(x), expr)
|
| 282 |
+
allargs_invertible = allargs(x, Q.invertible(x), expr)
|
| 283 |
+
return Implies(allargs_square, Equivalent(Q.invertible(expr), allargs_invertible))
|
| 284 |
+
|
| 285 |
+
|
| 286 |
+
### Pow ###
|
| 287 |
+
|
| 288 |
+
@class_fact_registry.multiregister(Pow)
|
| 289 |
+
def _(expr):
|
| 290 |
+
base, exp = expr.base, expr.exp
|
| 291 |
+
return [
|
| 292 |
+
(Q.real(base) & Q.even(exp) & Q.nonnegative(exp)) >> Q.nonnegative(expr),
|
| 293 |
+
(Q.nonnegative(base) & Q.odd(exp) & Q.nonnegative(exp)) >> Q.nonnegative(expr),
|
| 294 |
+
(Q.nonpositive(base) & Q.odd(exp) & Q.nonnegative(exp)) >> Q.nonpositive(expr),
|
| 295 |
+
Equivalent(Q.zero(expr), Q.zero(base) & Q.positive(exp))
|
| 296 |
+
]
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
### Numbers ###
|
| 300 |
+
|
| 301 |
+
_old_assump_getters = {
|
| 302 |
+
Q.positive: lambda o: o.is_positive,
|
| 303 |
+
Q.zero: lambda o: o.is_zero,
|
| 304 |
+
Q.negative: lambda o: o.is_negative,
|
| 305 |
+
Q.rational: lambda o: o.is_rational,
|
| 306 |
+
Q.irrational: lambda o: o.is_irrational,
|
| 307 |
+
Q.even: lambda o: o.is_even,
|
| 308 |
+
Q.odd: lambda o: o.is_odd,
|
| 309 |
+
Q.imaginary: lambda o: o.is_imaginary,
|
| 310 |
+
Q.prime: lambda o: o.is_prime,
|
| 311 |
+
Q.composite: lambda o: o.is_composite,
|
| 312 |
+
}
|
| 313 |
+
|
| 314 |
+
@class_fact_registry.multiregister(Number, NumberSymbol, ImaginaryUnit)
|
| 315 |
+
def _(expr):
|
| 316 |
+
ret = []
|
| 317 |
+
for p, getter in _old_assump_getters.items():
|
| 318 |
+
pred = p(expr)
|
| 319 |
+
prop = getter(expr)
|
| 320 |
+
if prop is not None:
|
| 321 |
+
ret.append(Equivalent(pred, prop))
|
| 322 |
+
return ret
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/__init__.py
ADDED
|
File without changes
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/test_assumptions_2.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
rename this to test_assumptions.py when the old assumptions system is deleted
|
| 3 |
+
"""
|
| 4 |
+
from sympy.abc import x, y
|
| 5 |
+
from sympy.assumptions.assume import global_assumptions
|
| 6 |
+
from sympy.assumptions.ask import Q
|
| 7 |
+
from sympy.printing import pretty
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def test_equal():
|
| 11 |
+
"""Test for equality"""
|
| 12 |
+
assert Q.positive(x) == Q.positive(x)
|
| 13 |
+
assert Q.positive(x) != ~Q.positive(x)
|
| 14 |
+
assert ~Q.positive(x) == ~Q.positive(x)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def test_pretty():
|
| 18 |
+
assert pretty(Q.positive(x)) == "Q.positive(x)"
|
| 19 |
+
assert pretty(
|
| 20 |
+
{Q.positive, Q.integer}) == "{Q.integer, Q.positive}"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def test_global():
|
| 24 |
+
"""Test for global assumptions"""
|
| 25 |
+
global_assumptions.add(x > 0)
|
| 26 |
+
assert (x > 0) in global_assumptions
|
| 27 |
+
global_assumptions.remove(x > 0)
|
| 28 |
+
assert not (x > 0) in global_assumptions
|
| 29 |
+
# same with multiple of assumptions
|
| 30 |
+
global_assumptions.add(x > 0, y > 0)
|
| 31 |
+
assert (x > 0) in global_assumptions
|
| 32 |
+
assert (y > 0) in global_assumptions
|
| 33 |
+
global_assumptions.clear()
|
| 34 |
+
assert not (x > 0) in global_assumptions
|
| 35 |
+
assert not (y > 0) in global_assumptions
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/test_context.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.assumptions import ask, Q
|
| 2 |
+
from sympy.assumptions.assume import assuming, global_assumptions
|
| 3 |
+
from sympy.abc import x, y
|
| 4 |
+
|
| 5 |
+
def test_assuming():
|
| 6 |
+
with assuming(Q.integer(x)):
|
| 7 |
+
assert ask(Q.integer(x))
|
| 8 |
+
assert not ask(Q.integer(x))
|
| 9 |
+
|
| 10 |
+
def test_assuming_nested():
|
| 11 |
+
assert not ask(Q.integer(x))
|
| 12 |
+
assert not ask(Q.integer(y))
|
| 13 |
+
with assuming(Q.integer(x)):
|
| 14 |
+
assert ask(Q.integer(x))
|
| 15 |
+
assert not ask(Q.integer(y))
|
| 16 |
+
with assuming(Q.integer(y)):
|
| 17 |
+
assert ask(Q.integer(x))
|
| 18 |
+
assert ask(Q.integer(y))
|
| 19 |
+
assert ask(Q.integer(x))
|
| 20 |
+
assert not ask(Q.integer(y))
|
| 21 |
+
assert not ask(Q.integer(x))
|
| 22 |
+
assert not ask(Q.integer(y))
|
| 23 |
+
|
| 24 |
+
def test_finally():
|
| 25 |
+
try:
|
| 26 |
+
with assuming(Q.integer(x)):
|
| 27 |
+
1/0
|
| 28 |
+
except ZeroDivisionError:
|
| 29 |
+
pass
|
| 30 |
+
assert not ask(Q.integer(x))
|
| 31 |
+
|
| 32 |
+
def test_remove_safe():
|
| 33 |
+
global_assumptions.add(Q.integer(x))
|
| 34 |
+
with assuming():
|
| 35 |
+
assert ask(Q.integer(x))
|
| 36 |
+
global_assumptions.remove(Q.integer(x))
|
| 37 |
+
assert not ask(Q.integer(x))
|
| 38 |
+
assert ask(Q.integer(x))
|
| 39 |
+
global_assumptions.clear() # for the benefit of other tests
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/test_matrices.py
ADDED
|
@@ -0,0 +1,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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.assumptions.ask import (Q, ask)
|
| 2 |
+
from sympy.core.symbol import Symbol
|
| 3 |
+
from sympy.matrices.expressions.diagonal import (DiagMatrix, DiagonalMatrix)
|
| 4 |
+
from sympy.matrices.dense import Matrix
|
| 5 |
+
from sympy.matrices.expressions import (MatrixSymbol, Identity, ZeroMatrix,
|
| 6 |
+
OneMatrix, Trace, MatrixSlice, Determinant, BlockMatrix, BlockDiagMatrix)
|
| 7 |
+
from sympy.matrices.expressions.factorizations import LofLU
|
| 8 |
+
from sympy.testing.pytest import XFAIL
|
| 9 |
+
|
| 10 |
+
X = MatrixSymbol('X', 2, 2)
|
| 11 |
+
Y = MatrixSymbol('Y', 2, 3)
|
| 12 |
+
Z = MatrixSymbol('Z', 2, 2)
|
| 13 |
+
A1x1 = MatrixSymbol('A1x1', 1, 1)
|
| 14 |
+
B1x1 = MatrixSymbol('B1x1', 1, 1)
|
| 15 |
+
C0x0 = MatrixSymbol('C0x0', 0, 0)
|
| 16 |
+
V1 = MatrixSymbol('V1', 2, 1)
|
| 17 |
+
V2 = MatrixSymbol('V2', 2, 1)
|
| 18 |
+
|
| 19 |
+
def test_square():
|
| 20 |
+
assert ask(Q.square(X))
|
| 21 |
+
assert not ask(Q.square(Y))
|
| 22 |
+
assert ask(Q.square(Y*Y.T))
|
| 23 |
+
|
| 24 |
+
def test_invertible():
|
| 25 |
+
assert ask(Q.invertible(X), Q.invertible(X))
|
| 26 |
+
assert ask(Q.invertible(Y)) is False
|
| 27 |
+
assert ask(Q.invertible(X*Y), Q.invertible(X)) is False
|
| 28 |
+
assert ask(Q.invertible(X*Z), Q.invertible(X)) is None
|
| 29 |
+
assert ask(Q.invertible(X*Z), Q.invertible(X) & Q.invertible(Z)) is True
|
| 30 |
+
assert ask(Q.invertible(X.T)) is None
|
| 31 |
+
assert ask(Q.invertible(X.T), Q.invertible(X)) is True
|
| 32 |
+
assert ask(Q.invertible(X.I)) is True
|
| 33 |
+
assert ask(Q.invertible(Identity(3))) is True
|
| 34 |
+
assert ask(Q.invertible(ZeroMatrix(3, 3))) is False
|
| 35 |
+
assert ask(Q.invertible(OneMatrix(1, 1))) is True
|
| 36 |
+
assert ask(Q.invertible(OneMatrix(3, 3))) is False
|
| 37 |
+
assert ask(Q.invertible(X), Q.fullrank(X) & Q.square(X))
|
| 38 |
+
|
| 39 |
+
def test_singular():
|
| 40 |
+
assert ask(Q.singular(X)) is None
|
| 41 |
+
assert ask(Q.singular(X), Q.invertible(X)) is False
|
| 42 |
+
assert ask(Q.singular(X), ~Q.invertible(X)) is True
|
| 43 |
+
|
| 44 |
+
@XFAIL
|
| 45 |
+
def test_invertible_fullrank():
|
| 46 |
+
assert ask(Q.invertible(X), Q.fullrank(X)) is True
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_invertible_BlockMatrix():
|
| 50 |
+
assert ask(Q.invertible(BlockMatrix([Identity(3)]))) == True
|
| 51 |
+
assert ask(Q.invertible(BlockMatrix([ZeroMatrix(3, 3)]))) == False
|
| 52 |
+
|
| 53 |
+
X = Matrix([[1, 2, 3], [3, 5, 4]])
|
| 54 |
+
Y = Matrix([[4, 2, 7], [2, 3, 5]])
|
| 55 |
+
# non-invertible A block
|
| 56 |
+
assert ask(Q.invertible(BlockMatrix([
|
| 57 |
+
[Matrix.ones(3, 3), Y.T],
|
| 58 |
+
[X, Matrix.eye(2)],
|
| 59 |
+
]))) == True
|
| 60 |
+
# non-invertible B block
|
| 61 |
+
assert ask(Q.invertible(BlockMatrix([
|
| 62 |
+
[Y.T, Matrix.ones(3, 3)],
|
| 63 |
+
[Matrix.eye(2), X],
|
| 64 |
+
]))) == True
|
| 65 |
+
# non-invertible C block
|
| 66 |
+
assert ask(Q.invertible(BlockMatrix([
|
| 67 |
+
[X, Matrix.eye(2)],
|
| 68 |
+
[Matrix.ones(3, 3), Y.T],
|
| 69 |
+
]))) == True
|
| 70 |
+
# non-invertible D block
|
| 71 |
+
assert ask(Q.invertible(BlockMatrix([
|
| 72 |
+
[Matrix.eye(2), X],
|
| 73 |
+
[Y.T, Matrix.ones(3, 3)],
|
| 74 |
+
]))) == True
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def test_invertible_BlockDiagMatrix():
|
| 78 |
+
assert ask(Q.invertible(BlockDiagMatrix(Identity(3), Identity(5)))) == True
|
| 79 |
+
assert ask(Q.invertible(BlockDiagMatrix(ZeroMatrix(3, 3), Identity(5)))) == False
|
| 80 |
+
assert ask(Q.invertible(BlockDiagMatrix(Identity(3), OneMatrix(5, 5)))) == False
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def test_symmetric():
|
| 84 |
+
assert ask(Q.symmetric(X), Q.symmetric(X))
|
| 85 |
+
assert ask(Q.symmetric(X*Z), Q.symmetric(X)) is None
|
| 86 |
+
assert ask(Q.symmetric(X*Z), Q.symmetric(X) & Q.symmetric(Z)) is True
|
| 87 |
+
assert ask(Q.symmetric(X + Z), Q.symmetric(X) & Q.symmetric(Z)) is True
|
| 88 |
+
assert ask(Q.symmetric(Y)) is False
|
| 89 |
+
assert ask(Q.symmetric(Y*Y.T)) is True
|
| 90 |
+
assert ask(Q.symmetric(Y.T*X*Y)) is None
|
| 91 |
+
assert ask(Q.symmetric(Y.T*X*Y), Q.symmetric(X)) is True
|
| 92 |
+
assert ask(Q.symmetric(X**10), Q.symmetric(X)) is True
|
| 93 |
+
assert ask(Q.symmetric(A1x1)) is True
|
| 94 |
+
assert ask(Q.symmetric(A1x1 + B1x1)) is True
|
| 95 |
+
assert ask(Q.symmetric(A1x1 * B1x1)) is True
|
| 96 |
+
assert ask(Q.symmetric(V1.T*V1)) is True
|
| 97 |
+
assert ask(Q.symmetric(V1.T*(V1 + V2))) is True
|
| 98 |
+
assert ask(Q.symmetric(V1.T*(V1 + V2) + A1x1)) is True
|
| 99 |
+
assert ask(Q.symmetric(MatrixSlice(Y, (0, 1), (1, 2)))) is True
|
| 100 |
+
assert ask(Q.symmetric(Identity(3))) is True
|
| 101 |
+
assert ask(Q.symmetric(ZeroMatrix(3, 3))) is True
|
| 102 |
+
assert ask(Q.symmetric(OneMatrix(3, 3))) is True
|
| 103 |
+
|
| 104 |
+
def _test_orthogonal_unitary(predicate):
|
| 105 |
+
assert ask(predicate(X), predicate(X))
|
| 106 |
+
assert ask(predicate(X.T), predicate(X)) is True
|
| 107 |
+
assert ask(predicate(X.I), predicate(X)) is True
|
| 108 |
+
assert ask(predicate(X**2), predicate(X))
|
| 109 |
+
assert ask(predicate(Y)) is False
|
| 110 |
+
assert ask(predicate(X)) is None
|
| 111 |
+
assert ask(predicate(X), ~Q.invertible(X)) is False
|
| 112 |
+
assert ask(predicate(X*Z*X), predicate(X) & predicate(Z)) is True
|
| 113 |
+
assert ask(predicate(Identity(3))) is True
|
| 114 |
+
assert ask(predicate(ZeroMatrix(3, 3))) is False
|
| 115 |
+
assert ask(Q.invertible(X), predicate(X))
|
| 116 |
+
assert not ask(predicate(X + Z), predicate(X) & predicate(Z))
|
| 117 |
+
|
| 118 |
+
def test_orthogonal():
|
| 119 |
+
_test_orthogonal_unitary(Q.orthogonal)
|
| 120 |
+
|
| 121 |
+
def test_unitary():
|
| 122 |
+
_test_orthogonal_unitary(Q.unitary)
|
| 123 |
+
assert ask(Q.unitary(X), Q.orthogonal(X))
|
| 124 |
+
|
| 125 |
+
def test_fullrank():
|
| 126 |
+
assert ask(Q.fullrank(X), Q.fullrank(X))
|
| 127 |
+
assert ask(Q.fullrank(X**2), Q.fullrank(X))
|
| 128 |
+
assert ask(Q.fullrank(X.T), Q.fullrank(X)) is True
|
| 129 |
+
assert ask(Q.fullrank(X)) is None
|
| 130 |
+
assert ask(Q.fullrank(Y)) is None
|
| 131 |
+
assert ask(Q.fullrank(X*Z), Q.fullrank(X) & Q.fullrank(Z)) is True
|
| 132 |
+
assert ask(Q.fullrank(Identity(3))) is True
|
| 133 |
+
assert ask(Q.fullrank(ZeroMatrix(3, 3))) is False
|
| 134 |
+
assert ask(Q.fullrank(OneMatrix(1, 1))) is True
|
| 135 |
+
assert ask(Q.fullrank(OneMatrix(3, 3))) is False
|
| 136 |
+
assert ask(Q.invertible(X), ~Q.fullrank(X)) == False
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
def test_positive_definite():
|
| 140 |
+
assert ask(Q.positive_definite(X), Q.positive_definite(X))
|
| 141 |
+
assert ask(Q.positive_definite(X.T), Q.positive_definite(X)) is True
|
| 142 |
+
assert ask(Q.positive_definite(X.I), Q.positive_definite(X)) is True
|
| 143 |
+
assert ask(Q.positive_definite(Y)) is False
|
| 144 |
+
assert ask(Q.positive_definite(X)) is None
|
| 145 |
+
assert ask(Q.positive_definite(X**3), Q.positive_definite(X))
|
| 146 |
+
assert ask(Q.positive_definite(X*Z*X),
|
| 147 |
+
Q.positive_definite(X) & Q.positive_definite(Z)) is True
|
| 148 |
+
assert ask(Q.positive_definite(X), Q.orthogonal(X))
|
| 149 |
+
assert ask(Q.positive_definite(Y.T*X*Y),
|
| 150 |
+
Q.positive_definite(X) & Q.fullrank(Y)) is True
|
| 151 |
+
assert not ask(Q.positive_definite(Y.T*X*Y), Q.positive_definite(X))
|
| 152 |
+
assert ask(Q.positive_definite(Identity(3))) is True
|
| 153 |
+
assert ask(Q.positive_definite(ZeroMatrix(3, 3))) is False
|
| 154 |
+
assert ask(Q.positive_definite(OneMatrix(1, 1))) is True
|
| 155 |
+
assert ask(Q.positive_definite(OneMatrix(3, 3))) is False
|
| 156 |
+
assert ask(Q.positive_definite(X + Z), Q.positive_definite(X) &
|
| 157 |
+
Q.positive_definite(Z)) is True
|
| 158 |
+
assert not ask(Q.positive_definite(-X), Q.positive_definite(X))
|
| 159 |
+
assert ask(Q.positive(X[1, 1]), Q.positive_definite(X))
|
| 160 |
+
|
| 161 |
+
def test_triangular():
|
| 162 |
+
assert ask(Q.upper_triangular(X + Z.T + Identity(2)), Q.upper_triangular(X) &
|
| 163 |
+
Q.lower_triangular(Z)) is True
|
| 164 |
+
assert ask(Q.upper_triangular(X*Z.T), Q.upper_triangular(X) &
|
| 165 |
+
Q.lower_triangular(Z)) is True
|
| 166 |
+
assert ask(Q.lower_triangular(Identity(3))) is True
|
| 167 |
+
assert ask(Q.lower_triangular(ZeroMatrix(3, 3))) is True
|
| 168 |
+
assert ask(Q.upper_triangular(ZeroMatrix(3, 3))) is True
|
| 169 |
+
assert ask(Q.lower_triangular(OneMatrix(1, 1))) is True
|
| 170 |
+
assert ask(Q.upper_triangular(OneMatrix(1, 1))) is True
|
| 171 |
+
assert ask(Q.lower_triangular(OneMatrix(3, 3))) is False
|
| 172 |
+
assert ask(Q.upper_triangular(OneMatrix(3, 3))) is False
|
| 173 |
+
assert ask(Q.triangular(X), Q.unit_triangular(X))
|
| 174 |
+
assert ask(Q.upper_triangular(X**3), Q.upper_triangular(X))
|
| 175 |
+
assert ask(Q.lower_triangular(X**3), Q.lower_triangular(X))
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def test_diagonal():
|
| 179 |
+
assert ask(Q.diagonal(X + Z.T + Identity(2)), Q.diagonal(X) &
|
| 180 |
+
Q.diagonal(Z)) is True
|
| 181 |
+
assert ask(Q.diagonal(ZeroMatrix(3, 3)))
|
| 182 |
+
assert ask(Q.diagonal(OneMatrix(1, 1))) is True
|
| 183 |
+
assert ask(Q.diagonal(OneMatrix(3, 3))) is False
|
| 184 |
+
assert ask(Q.lower_triangular(X) & Q.upper_triangular(X), Q.diagonal(X))
|
| 185 |
+
assert ask(Q.diagonal(X), Q.lower_triangular(X) & Q.upper_triangular(X))
|
| 186 |
+
assert ask(Q.symmetric(X), Q.diagonal(X))
|
| 187 |
+
assert ask(Q.triangular(X), Q.diagonal(X))
|
| 188 |
+
assert ask(Q.diagonal(C0x0))
|
| 189 |
+
assert ask(Q.diagonal(A1x1))
|
| 190 |
+
assert ask(Q.diagonal(A1x1 + B1x1))
|
| 191 |
+
assert ask(Q.diagonal(A1x1*B1x1))
|
| 192 |
+
assert ask(Q.diagonal(V1.T*V2))
|
| 193 |
+
assert ask(Q.diagonal(V1.T*(X + Z)*V1))
|
| 194 |
+
assert ask(Q.diagonal(MatrixSlice(Y, (0, 1), (1, 2)))) is True
|
| 195 |
+
assert ask(Q.diagonal(V1.T*(V1 + V2))) is True
|
| 196 |
+
assert ask(Q.diagonal(X**3), Q.diagonal(X))
|
| 197 |
+
assert ask(Q.diagonal(Identity(3)))
|
| 198 |
+
assert ask(Q.diagonal(DiagMatrix(V1)))
|
| 199 |
+
assert ask(Q.diagonal(DiagonalMatrix(X)))
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def test_non_atoms():
|
| 203 |
+
assert ask(Q.real(Trace(X)), Q.positive(Trace(X)))
|
| 204 |
+
|
| 205 |
+
@XFAIL
|
| 206 |
+
def test_non_trivial_implies():
|
| 207 |
+
X = MatrixSymbol('X', 3, 3)
|
| 208 |
+
Y = MatrixSymbol('Y', 3, 3)
|
| 209 |
+
assert ask(Q.lower_triangular(X+Y), Q.lower_triangular(X) &
|
| 210 |
+
Q.lower_triangular(Y)) is True
|
| 211 |
+
assert ask(Q.triangular(X), Q.lower_triangular(X)) is True
|
| 212 |
+
assert ask(Q.triangular(X+Y), Q.lower_triangular(X) &
|
| 213 |
+
Q.lower_triangular(Y)) is True
|
| 214 |
+
|
| 215 |
+
def test_MatrixSlice():
|
| 216 |
+
X = MatrixSymbol('X', 4, 4)
|
| 217 |
+
B = MatrixSlice(X, (1, 3), (1, 3))
|
| 218 |
+
C = MatrixSlice(X, (0, 3), (1, 3))
|
| 219 |
+
assert ask(Q.symmetric(B), Q.symmetric(X))
|
| 220 |
+
assert ask(Q.invertible(B), Q.invertible(X))
|
| 221 |
+
assert ask(Q.diagonal(B), Q.diagonal(X))
|
| 222 |
+
assert ask(Q.orthogonal(B), Q.orthogonal(X))
|
| 223 |
+
assert ask(Q.upper_triangular(B), Q.upper_triangular(X))
|
| 224 |
+
|
| 225 |
+
assert not ask(Q.symmetric(C), Q.symmetric(X))
|
| 226 |
+
assert not ask(Q.invertible(C), Q.invertible(X))
|
| 227 |
+
assert not ask(Q.diagonal(C), Q.diagonal(X))
|
| 228 |
+
assert not ask(Q.orthogonal(C), Q.orthogonal(X))
|
| 229 |
+
assert not ask(Q.upper_triangular(C), Q.upper_triangular(X))
|
| 230 |
+
|
| 231 |
+
def test_det_trace_positive():
|
| 232 |
+
X = MatrixSymbol('X', 4, 4)
|
| 233 |
+
assert ask(Q.positive(Trace(X)), Q.positive_definite(X))
|
| 234 |
+
assert ask(Q.positive(Determinant(X)), Q.positive_definite(X))
|
| 235 |
+
|
| 236 |
+
def test_field_assumptions():
|
| 237 |
+
X = MatrixSymbol('X', 4, 4)
|
| 238 |
+
Y = MatrixSymbol('Y', 4, 4)
|
| 239 |
+
assert ask(Q.real_elements(X), Q.real_elements(X))
|
| 240 |
+
assert not ask(Q.integer_elements(X), Q.real_elements(X))
|
| 241 |
+
assert ask(Q.complex_elements(X), Q.real_elements(X))
|
| 242 |
+
assert ask(Q.complex_elements(X**2), Q.real_elements(X))
|
| 243 |
+
assert ask(Q.real_elements(X**2), Q.integer_elements(X))
|
| 244 |
+
assert ask(Q.real_elements(X+Y), Q.real_elements(X)) is None
|
| 245 |
+
assert ask(Q.real_elements(X+Y), Q.real_elements(X) & Q.real_elements(Y))
|
| 246 |
+
from sympy.matrices.expressions.hadamard import HadamardProduct
|
| 247 |
+
assert ask(Q.real_elements(HadamardProduct(X, Y)),
|
| 248 |
+
Q.real_elements(X) & Q.real_elements(Y))
|
| 249 |
+
assert ask(Q.complex_elements(X+Y), Q.real_elements(X) & Q.complex_elements(Y))
|
| 250 |
+
|
| 251 |
+
assert ask(Q.real_elements(X.T), Q.real_elements(X))
|
| 252 |
+
assert ask(Q.real_elements(X.I), Q.real_elements(X) & Q.invertible(X))
|
| 253 |
+
assert ask(Q.real_elements(Trace(X)), Q.real_elements(X))
|
| 254 |
+
assert ask(Q.integer_elements(Determinant(X)), Q.integer_elements(X))
|
| 255 |
+
assert not ask(Q.integer_elements(X.I), Q.integer_elements(X))
|
| 256 |
+
alpha = Symbol('alpha')
|
| 257 |
+
assert ask(Q.real_elements(alpha*X), Q.real_elements(X) & Q.real(alpha))
|
| 258 |
+
assert ask(Q.real_elements(LofLU(X)), Q.real_elements(X))
|
| 259 |
+
e = Symbol('e', integer=True, negative=True)
|
| 260 |
+
assert ask(Q.real_elements(X**e), Q.real_elements(X) & Q.invertible(X))
|
| 261 |
+
assert ask(Q.real_elements(X**e), Q.real_elements(X)) is None
|
| 262 |
+
|
| 263 |
+
def test_matrix_element_sets():
|
| 264 |
+
X = MatrixSymbol('X', 4, 4)
|
| 265 |
+
assert ask(Q.real(X[1, 2]), Q.real_elements(X))
|
| 266 |
+
assert ask(Q.integer(X[1, 2]), Q.integer_elements(X))
|
| 267 |
+
assert ask(Q.complex(X[1, 2]), Q.complex_elements(X))
|
| 268 |
+
assert ask(Q.integer_elements(Identity(3)))
|
| 269 |
+
assert ask(Q.integer_elements(ZeroMatrix(3, 3)))
|
| 270 |
+
assert ask(Q.integer_elements(OneMatrix(3, 3)))
|
| 271 |
+
from sympy.matrices.expressions.fourier import DFT
|
| 272 |
+
assert ask(Q.complex_elements(DFT(3)))
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def test_matrix_element_sets_slices_blocks():
|
| 276 |
+
X = MatrixSymbol('X', 4, 4)
|
| 277 |
+
assert ask(Q.integer_elements(X[:, 3]), Q.integer_elements(X))
|
| 278 |
+
assert ask(Q.integer_elements(BlockMatrix([[X], [X]])),
|
| 279 |
+
Q.integer_elements(X))
|
| 280 |
+
|
| 281 |
+
def test_matrix_element_sets_determinant_trace():
|
| 282 |
+
assert ask(Q.integer(Determinant(X)), Q.integer_elements(X))
|
| 283 |
+
assert ask(Q.integer(Trace(X)), Q.integer_elements(X))
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/test_query.py
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
Scripts_RSCM_sim_growth_n_climate_to_Yield/.venv/lib/python3.10/site-packages/sympy/assumptions/tests/test_refine.py
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.assumptions.ask import Q
|
| 2 |
+
from sympy.assumptions.refine import refine
|
| 3 |
+
from sympy.core.expr import Expr
|
| 4 |
+
from sympy.core.numbers import (I, Rational, nan, pi)
|
| 5 |
+
from sympy.core.singleton import S
|
| 6 |
+
from sympy.core.symbol import Symbol
|
| 7 |
+
from sympy.functions.elementary.complexes import (Abs, arg, im, re, sign)
|
| 8 |
+
from sympy.functions.elementary.exponential import exp
|
| 9 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 10 |
+
from sympy.functions.elementary.trigonometric import (atan, atan2)
|
| 11 |
+
from sympy.abc import w, x, y, z
|
| 12 |
+
from sympy.core.relational import Eq, Ne
|
| 13 |
+
from sympy.functions.elementary.piecewise import Piecewise
|
| 14 |
+
from sympy.matrices.expressions.matexpr import MatrixSymbol
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def test_Abs():
|
| 18 |
+
assert refine(Abs(x), Q.positive(x)) == x
|
| 19 |
+
assert refine(1 + Abs(x), Q.positive(x)) == 1 + x
|
| 20 |
+
assert refine(Abs(x), Q.negative(x)) == -x
|
| 21 |
+
assert refine(1 + Abs(x), Q.negative(x)) == 1 - x
|
| 22 |
+
|
| 23 |
+
assert refine(Abs(x**2)) != x**2
|
| 24 |
+
assert refine(Abs(x**2), Q.real(x)) == x**2
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def test_pow1():
|
| 28 |
+
assert refine((-1)**x, Q.even(x)) == 1
|
| 29 |
+
assert refine((-1)**x, Q.odd(x)) == -1
|
| 30 |
+
assert refine((-2)**x, Q.even(x)) == 2**x
|
| 31 |
+
|
| 32 |
+
# nested powers
|
| 33 |
+
assert refine(sqrt(x**2)) != Abs(x)
|
| 34 |
+
assert refine(sqrt(x**2), Q.complex(x)) != Abs(x)
|
| 35 |
+
assert refine(sqrt(x**2), Q.real(x)) == Abs(x)
|
| 36 |
+
assert refine(sqrt(x**2), Q.positive(x)) == x
|
| 37 |
+
assert refine((x**3)**Rational(1, 3)) != x
|
| 38 |
+
|
| 39 |
+
assert refine((x**3)**Rational(1, 3), Q.real(x)) != x
|
| 40 |
+
assert refine((x**3)**Rational(1, 3), Q.positive(x)) == x
|
| 41 |
+
|
| 42 |
+
assert refine(sqrt(1/x), Q.real(x)) != 1/sqrt(x)
|
| 43 |
+
assert refine(sqrt(1/x), Q.positive(x)) == 1/sqrt(x)
|
| 44 |
+
|
| 45 |
+
# powers of (-1)
|
| 46 |
+
assert refine((-1)**(x + y), Q.even(x)) == (-1)**y
|
| 47 |
+
assert refine((-1)**(x + y + z), Q.odd(x) & Q.odd(z)) == (-1)**y
|
| 48 |
+
assert refine((-1)**(x + y + 1), Q.odd(x)) == (-1)**y
|
| 49 |
+
assert refine((-1)**(x + y + 2), Q.odd(x)) == (-1)**(y + 1)
|
| 50 |
+
assert refine((-1)**(x + 3)) == (-1)**(x + 1)
|
| 51 |
+
|
| 52 |
+
# continuation
|
| 53 |
+
assert refine((-1)**((-1)**x/2 - S.Half), Q.integer(x)) == (-1)**x
|
| 54 |
+
assert refine((-1)**((-1)**x/2 + S.Half), Q.integer(x)) == (-1)**(x + 1)
|
| 55 |
+
assert refine((-1)**((-1)**x/2 + 5*S.Half), Q.integer(x)) == (-1)**(x + 1)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def test_pow2():
|
| 59 |
+
assert refine((-1)**((-1)**x/2 - 7*S.Half), Q.integer(x)) == (-1)**(x + 1)
|
| 60 |
+
assert refine((-1)**((-1)**x/2 - 9*S.Half), Q.integer(x)) == (-1)**x
|
| 61 |
+
|
| 62 |
+
# powers of Abs
|
| 63 |
+
assert refine(Abs(x)**2, Q.real(x)) == x**2
|
| 64 |
+
assert refine(Abs(x)**3, Q.real(x)) == Abs(x)**3
|
| 65 |
+
assert refine(Abs(x)**2) == Abs(x)**2
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_exp():
|
| 69 |
+
x = Symbol('x', integer=True)
|
| 70 |
+
assert refine(exp(pi*I*2*x)) == 1
|
| 71 |
+
assert refine(exp(pi*I*2*(x + S.Half))) == -1
|
| 72 |
+
assert refine(exp(pi*I*2*(x + Rational(1, 4)))) == I
|
| 73 |
+
assert refine(exp(pi*I*2*(x + Rational(3, 4)))) == -I
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def test_Piecewise():
|
| 77 |
+
assert refine(Piecewise((1, x < 0), (3, True)), (x < 0)) == 1
|
| 78 |
+
assert refine(Piecewise((1, x < 0), (3, True)), ~(x < 0)) == 3
|
| 79 |
+
assert refine(Piecewise((1, x < 0), (3, True)), (y < 0)) == \
|
| 80 |
+
Piecewise((1, x < 0), (3, True))
|
| 81 |
+
assert refine(Piecewise((1, x > 0), (3, True)), (x > 0)) == 1
|
| 82 |
+
assert refine(Piecewise((1, x > 0), (3, True)), ~(x > 0)) == 3
|
| 83 |
+
assert refine(Piecewise((1, x > 0), (3, True)), (y > 0)) == \
|
| 84 |
+
Piecewise((1, x > 0), (3, True))
|
| 85 |
+
assert refine(Piecewise((1, x <= 0), (3, True)), (x <= 0)) == 1
|
| 86 |
+
assert refine(Piecewise((1, x <= 0), (3, True)), ~(x <= 0)) == 3
|
| 87 |
+
assert refine(Piecewise((1, x <= 0), (3, True)), (y <= 0)) == \
|
| 88 |
+
Piecewise((1, x <= 0), (3, True))
|
| 89 |
+
assert refine(Piecewise((1, x >= 0), (3, True)), (x >= 0)) == 1
|
| 90 |
+
assert refine(Piecewise((1, x >= 0), (3, True)), ~(x >= 0)) == 3
|
| 91 |
+
assert refine(Piecewise((1, x >= 0), (3, True)), (y >= 0)) == \
|
| 92 |
+
Piecewise((1, x >= 0), (3, True))
|
| 93 |
+
assert refine(Piecewise((1, Eq(x, 0)), (3, True)), (Eq(x, 0)))\
|
| 94 |
+
== 1
|
| 95 |
+
assert refine(Piecewise((1, Eq(x, 0)), (3, True)), (Eq(0, x)))\
|
| 96 |
+
== 1
|
| 97 |
+
assert refine(Piecewise((1, Eq(x, 0)), (3, True)), ~(Eq(x, 0)))\
|
| 98 |
+
== 3
|
| 99 |
+
assert refine(Piecewise((1, Eq(x, 0)), (3, True)), ~(Eq(0, x)))\
|
| 100 |
+
== 3
|
| 101 |
+
assert refine(Piecewise((1, Eq(x, 0)), (3, True)), (Eq(y, 0)))\
|
| 102 |
+
== Piecewise((1, Eq(x, 0)), (3, True))
|
| 103 |
+
assert refine(Piecewise((1, Ne(x, 0)), (3, True)), (Ne(x, 0)))\
|
| 104 |
+
== 1
|
| 105 |
+
assert refine(Piecewise((1, Ne(x, 0)), (3, True)), ~(Ne(x, 0)))\
|
| 106 |
+
== 3
|
| 107 |
+
assert refine(Piecewise((1, Ne(x, 0)), (3, True)), (Ne(y, 0)))\
|
| 108 |
+
== Piecewise((1, Ne(x, 0)), (3, True))
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def test_atan2():
|
| 112 |
+
assert refine(atan2(y, x), Q.real(y) & Q.positive(x)) == atan(y/x)
|
| 113 |
+
assert refine(atan2(y, x), Q.negative(y) & Q.positive(x)) == atan(y/x)
|
| 114 |
+
assert refine(atan2(y, x), Q.negative(y) & Q.negative(x)) == atan(y/x) - pi
|
| 115 |
+
assert refine(atan2(y, x), Q.positive(y) & Q.negative(x)) == atan(y/x) + pi
|
| 116 |
+
assert refine(atan2(y, x), Q.zero(y) & Q.negative(x)) == pi
|
| 117 |
+
assert refine(atan2(y, x), Q.positive(y) & Q.zero(x)) == pi/2
|
| 118 |
+
assert refine(atan2(y, x), Q.negative(y) & Q.zero(x)) == -pi/2
|
| 119 |
+
assert refine(atan2(y, x), Q.zero(y) & Q.zero(x)) is nan
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def test_re():
|
| 123 |
+
assert refine(re(x), Q.real(x)) == x
|
| 124 |
+
assert refine(re(x), Q.imaginary(x)) is S.Zero
|
| 125 |
+
assert refine(re(x+y), Q.real(x) & Q.real(y)) == x + y
|
| 126 |
+
assert refine(re(x+y), Q.real(x) & Q.imaginary(y)) == x
|
| 127 |
+
assert refine(re(x*y), Q.real(x) & Q.real(y)) == x * y
|
| 128 |
+
assert refine(re(x*y), Q.real(x) & Q.imaginary(y)) == 0
|
| 129 |
+
assert refine(re(x*y*z), Q.real(x) & Q.real(y) & Q.real(z)) == x * y * z
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def test_im():
|
| 133 |
+
assert refine(im(x), Q.imaginary(x)) == -I*x
|
| 134 |
+
assert refine(im(x), Q.real(x)) is S.Zero
|
| 135 |
+
assert refine(im(x+y), Q.imaginary(x) & Q.imaginary(y)) == -I*x - I*y
|
| 136 |
+
assert refine(im(x+y), Q.real(x) & Q.imaginary(y)) == -I*y
|
| 137 |
+
assert refine(im(x*y), Q.imaginary(x) & Q.real(y)) == -I*x*y
|
| 138 |
+
assert refine(im(x*y), Q.imaginary(x) & Q.imaginary(y)) == 0
|
| 139 |
+
assert refine(im(1/x), Q.imaginary(x)) == -I/x
|
| 140 |
+
assert refine(im(x*y*z), Q.imaginary(x) & Q.imaginary(y)
|
| 141 |
+
& Q.imaginary(z)) == -I*x*y*z
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def test_complex():
|
| 145 |
+
assert refine(re(1/(x + I*y)), Q.real(x) & Q.real(y)) == \
|
| 146 |
+
x/(x**2 + y**2)
|
| 147 |
+
assert refine(im(1/(x + I*y)), Q.real(x) & Q.real(y)) == \
|
| 148 |
+
-y/(x**2 + y**2)
|
| 149 |
+
assert refine(re((w + I*x) * (y + I*z)), Q.real(w) & Q.real(x) & Q.real(y)
|
| 150 |
+
& Q.real(z)) == w*y - x*z
|
| 151 |
+
assert refine(im((w + I*x) * (y + I*z)), Q.real(w) & Q.real(x) & Q.real(y)
|
| 152 |
+
& Q.real(z)) == w*z + x*y
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def test_sign():
|
| 156 |
+
x = Symbol('x', real = True)
|
| 157 |
+
assert refine(sign(x), Q.positive(x)) == 1
|
| 158 |
+
assert refine(sign(x), Q.negative(x)) == -1
|
| 159 |
+
assert refine(sign(x), Q.zero(x)) == 0
|
| 160 |
+
assert refine(sign(x), True) == sign(x)
|
| 161 |
+
assert refine(sign(Abs(x)), Q.nonzero(x)) == 1
|
| 162 |
+
|
| 163 |
+
x = Symbol('x', imaginary=True)
|
| 164 |
+
assert refine(sign(x), Q.positive(im(x))) == S.ImaginaryUnit
|
| 165 |
+
assert refine(sign(x), Q.negative(im(x))) == -S.ImaginaryUnit
|
| 166 |
+
assert refine(sign(x), True) == sign(x)
|
| 167 |
+
|
| 168 |
+
x = Symbol('x', complex=True)
|
| 169 |
+
assert refine(sign(x), Q.zero(x)) == 0
|
| 170 |
+
|
| 171 |
+
def test_arg():
|
| 172 |
+
x = Symbol('x', complex = True)
|
| 173 |
+
assert refine(arg(x), Q.positive(x)) == 0
|
| 174 |
+
assert refine(arg(x), Q.negative(x)) == pi
|
| 175 |
+
|
| 176 |
+
def test_func_args():
|
| 177 |
+
class MyClass(Expr):
|
| 178 |
+
# A class with nontrivial .func
|
| 179 |
+
|
| 180 |
+
def __init__(self, *args):
|
| 181 |
+
self.my_member = ""
|
| 182 |
+
|
| 183 |
+
@property
|
| 184 |
+
def func(self):
|
| 185 |
+
def my_func(*args):
|
| 186 |
+
obj = MyClass(*args)
|
| 187 |
+
obj.my_member = self.my_member
|
| 188 |
+
return obj
|
| 189 |
+
return my_func
|
| 190 |
+
|
| 191 |
+
x = MyClass()
|
| 192 |
+
x.my_member = "A very important value"
|
| 193 |
+
assert x.my_member == refine(x).my_member
|
| 194 |
+
|
| 195 |
+
def test_issue_refine_9384():
|
| 196 |
+
assert refine(Piecewise((1, x < 0), (0, True)), Q.positive(x)) == 0
|
| 197 |
+
assert refine(Piecewise((1, x < 0), (0, True)), Q.negative(x)) == 1
|
| 198 |
+
assert refine(Piecewise((1, x > 0), (0, True)), Q.positive(x)) == 1
|
| 199 |
+
assert refine(Piecewise((1, x > 0), (0, True)), Q.negative(x)) == 0
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def test_eval_refine():
|
| 203 |
+
class MockExpr(Expr):
|
| 204 |
+
def _eval_refine(self, assumptions):
|
| 205 |
+
return True
|
| 206 |
+
|
| 207 |
+
mock_obj = MockExpr()
|
| 208 |
+
assert refine(mock_obj)
|
| 209 |
+
|
| 210 |
+
def test_refine_issue_12724():
|
| 211 |
+
expr1 = refine(Abs(x * y), Q.positive(x))
|
| 212 |
+
expr2 = refine(Abs(x * y * z), Q.positive(x))
|
| 213 |
+
assert expr1 == x * Abs(y)
|
| 214 |
+
assert expr2 == x * Abs(y * z)
|
| 215 |
+
y1 = Symbol('y1', real = True)
|
| 216 |
+
expr3 = refine(Abs(x * y1**2 * z), Q.positive(x))
|
| 217 |
+
assert expr3 == x * y1**2 * Abs(z)
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def test_matrixelement():
|
| 221 |
+
x = MatrixSymbol('x', 3, 3)
|
| 222 |
+
i = Symbol('i', positive = True)
|
| 223 |
+
j = Symbol('j', positive = True)
|
| 224 |
+
assert refine(x[0, 1], Q.symmetric(x)) == x[0, 1]
|
| 225 |
+
assert refine(x[1, 0], Q.symmetric(x)) == x[0, 1]
|
| 226 |
+
assert refine(x[i, j], Q.symmetric(x)) == x[j, i]
|
| 227 |
+
assert refine(x[j, i], Q.symmetric(x)) == x[j, i]
|