ADAPT-Chase commited on
Commit
c639fe3
·
verified ·
1 Parent(s): 87044a4

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +36 -0
  2. tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/__init__.cpython-312.pyc +0 -0
  3. tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/abc.cpython-312.pyc +0 -0
  4. tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/conftest.cpython-312.pyc +0 -0
  5. tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/galgebra.cpython-312.pyc +0 -0
  6. tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/release.cpython-312.pyc +0 -0
  7. tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/this.cpython-312.pyc +0 -0
  8. tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/__pycache__/__init__.cpython-312.pyc +0 -0
  9. tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/__pycache__/quaternion.cpython-312.pyc +0 -0
  10. tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/tests/__init__.py +0 -0
  11. tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/tests/__pycache__/__init__.cpython-312.pyc +0 -0
  12. tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/tests/__pycache__/test_quaternion.cpython-312.pyc +0 -0
  13. tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/tests/test_quaternion.py +437 -0
  14. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/__init__.cpython-312.pyc +0 -0
  15. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/ask.cpython-312.pyc +0 -0
  16. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/ask_generated.cpython-312.pyc +0 -0
  17. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/assume.cpython-312.pyc +0 -0
  18. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/cnf.cpython-312.pyc +0 -0
  19. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/facts.cpython-312.pyc +0 -0
  20. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/lra_satask.cpython-312.pyc +0 -0
  21. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/refine.cpython-312.pyc +0 -0
  22. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/satask.cpython-312.pyc +0 -0
  23. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/sathandlers.cpython-312.pyc +0 -0
  24. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/wrapper.cpython-312.pyc +0 -0
  25. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__init__.py +13 -0
  26. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/__init__.cpython-312.pyc +0 -0
  27. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/calculus.cpython-312.pyc +0 -0
  28. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/common.cpython-312.pyc +0 -0
  29. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/matrices.cpython-312.pyc +0 -0
  30. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/ntheory.cpython-312.pyc +0 -0
  31. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/order.cpython-312.pyc +0 -0
  32. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/sets.cpython-312.pyc +0 -0
  33. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/calculus.py +273 -0
  34. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/common.py +164 -0
  35. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/matrices.py +716 -0
  36. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/ntheory.py +279 -0
  37. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/order.py +440 -0
  38. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/sets.py +816 -0
  39. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__init__.py +5 -0
  40. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/__init__.cpython-312.pyc +0 -0
  41. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/calculus.cpython-312.pyc +0 -0
  42. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/common.cpython-312.pyc +0 -0
  43. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/matrices.cpython-312.pyc +0 -0
  44. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/ntheory.cpython-312.pyc +0 -0
  45. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/order.cpython-312.pyc +0 -0
  46. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/sets.cpython-312.pyc +0 -0
  47. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/calculus.py +82 -0
  48. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/common.py +81 -0
  49. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/matrices.py +511 -0
  50. tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/ntheory.py +126 -0
.gitattributes CHANGED
@@ -3953,3 +3953,39 @@ tool_server/.venv/lib/python3.12/site-packages/cusparselt/lib/libcusparseLt.so.0
3953
  tool_server/.venv/lib/python3.12/site-packages/mpmath/__pycache__/function_docs.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3954
  tool_server/.venv/lib/python3.12/site-packages/mpmath/tests/__pycache__/test_fp.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3955
  tool_server/.venv/lib/python3.12/site-packages/mpmath/tests/__pycache__/test_functions2.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3953
  tool_server/.venv/lib/python3.12/site-packages/mpmath/__pycache__/function_docs.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3954
  tool_server/.venv/lib/python3.12/site-packages/mpmath/tests/__pycache__/test_fp.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3955
  tool_server/.venv/lib/python3.12/site-packages/mpmath/tests/__pycache__/test_functions2.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3956
+ tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/tests/__pycache__/test_query.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3957
+ tool_server/.venv/lib/python3.12/site-packages/sympy/combinatorics/__pycache__/perm_groups.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3958
+ tool_server/.venv/lib/python3.12/site-packages/sympy/combinatorics/__pycache__/permutations.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3959
+ tool_server/.venv/lib/python3.12/site-packages/sympy/concrete/tests/__pycache__/test_sums_products.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3960
+ tool_server/.venv/lib/python3.12/site-packages/sympy/core/__pycache__/expr.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3961
+ tool_server/.venv/lib/python3.12/site-packages/sympy/core/__pycache__/function.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3962
+ tool_server/.venv/lib/python3.12/site-packages/sympy/core/__pycache__/numbers.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3963
+ tool_server/.venv/lib/python3.12/site-packages/sympy/core/tests/__pycache__/test_args.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3964
+ tool_server/.venv/lib/python3.12/site-packages/sympy/core/tests/__pycache__/test_arit.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3965
+ tool_server/.venv/lib/python3.12/site-packages/sympy/core/tests/__pycache__/test_expr.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3966
+ tool_server/.venv/lib/python3.12/site-packages/sympy/core/tests/__pycache__/test_function.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3967
+ tool_server/.venv/lib/python3.12/site-packages/sympy/core/tests/__pycache__/test_numbers.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3968
+ tool_server/.venv/lib/python3.12/site-packages/sympy/core/tests/__pycache__/test_relational.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3969
+ tool_server/.venv/lib/python3.12/site-packages/sympy/crypto/__pycache__/crypto.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3970
+ tool_server/.venv/lib/python3.12/site-packages/sympy/functions/combinatorial/__pycache__/numbers.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3971
+ tool_server/.venv/lib/python3.12/site-packages/sympy/functions/combinatorial/tests/__pycache__/test_comb_numbers.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3972
+ tool_server/.venv/lib/python3.12/site-packages/sympy/functions/elementary/__pycache__/hyperbolic.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3973
+ tool_server/.venv/lib/python3.12/site-packages/sympy/functions/elementary/__pycache__/trigonometric.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3974
+ tool_server/.venv/lib/python3.12/site-packages/sympy/functions/elementary/tests/__pycache__/test_hyperbolic.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3975
+ tool_server/.venv/lib/python3.12/site-packages/sympy/functions/elementary/tests/__pycache__/test_piecewise.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3976
+ tool_server/.venv/lib/python3.12/site-packages/sympy/functions/elementary/tests/__pycache__/test_trigonometric.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3977
+ tool_server/.venv/lib/python3.12/site-packages/sympy/functions/special/__pycache__/bessel.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3978
+ tool_server/.venv/lib/python3.12/site-packages/sympy/functions/special/__pycache__/error_functions.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3979
+ tool_server/.venv/lib/python3.12/site-packages/sympy/functions/special/tests/__pycache__/test_error_functions.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3980
+ tool_server/.venv/lib/python3.12/site-packages/sympy/geometry/__pycache__/polygon.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3981
+ tool_server/.venv/lib/python3.12/site-packages/sympy/holonomic/__pycache__/holonomic.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3982
+ tool_server/.venv/lib/python3.12/site-packages/sympy/integrals/__pycache__/laplace.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3983
+ tool_server/.venv/lib/python3.12/site-packages/sympy/integrals/__pycache__/manualintegrate.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3984
+ tool_server/.venv/lib/python3.12/site-packages/sympy/integrals/__pycache__/meijerint.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3985
+ tool_server/.venv/lib/python3.12/site-packages/sympy/integrals/tests/__pycache__/test_integrals.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3986
+ tool_server/.venv/lib/python3.12/site-packages/sympy/logic/__pycache__/boolalg.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3987
+ tool_server/.venv/lib/python3.12/site-packages/sympy/logic/tests/__pycache__/test_boolalg.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3988
+ tool_server/.venv/lib/python3.12/site-packages/sympy/matrices/__pycache__/common.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3989
+ tool_server/.venv/lib/python3.12/site-packages/sympy/matrices/__pycache__/matrixbase.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3990
+ tool_server/.venv/lib/python3.12/site-packages/sympy/matrices/tests/__pycache__/test_matrices.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
3991
+ tool_server/.venv/lib/python3.12/site-packages/sympy/matrices/tests/__pycache__/test_matrixbase.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (31.1 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/abc.cpython-312.pyc ADDED
Binary file (4.34 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/conftest.cpython-312.pyc ADDED
Binary file (4.4 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/galgebra.cpython-312.pyc ADDED
Binary file (309 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/release.cpython-312.pyc ADDED
Binary file (201 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/__pycache__/this.cpython-312.pyc ADDED
Binary file (760 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (259 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/__pycache__/quaternion.cpython-312.pyc ADDED
Binary file (62.5 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/tests/__init__.py ADDED
File without changes
tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/tests/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (190 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/tests/__pycache__/test_quaternion.cpython-312.pyc ADDED
Binary file (36.5 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/algebras/tests/test_quaternion.py ADDED
@@ -0,0 +1,437 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (718 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/ask.cpython-312.pyc ADDED
Binary file (24.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/ask_generated.cpython-312.pyc ADDED
Binary file (53.5 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/assume.cpython-312.pyc ADDED
Binary file (18.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/cnf.cpython-312.pyc ADDED
Binary file (23.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/facts.cpython-312.pyc ADDED
Binary file (14.9 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/lra_satask.cpython-312.pyc ADDED
Binary file (12.2 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/refine.cpython-312.pyc ADDED
Binary file (16 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/satask.cpython-312.pyc ADDED
Binary file (13.5 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/sathandlers.cpython-312.pyc ADDED
Binary file (17.1 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/__pycache__/wrapper.cpython-312.pyc ADDED
Binary file (6.54 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__init__.py ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Multipledispatch handlers for ``Predicate`` are implemented here.
3
+ Handlers in this module are not directly imported to other modules in
4
+ order to avoid circular import problem.
5
+ """
6
+
7
+ from .common import (AskHandler, CommonHandler,
8
+ test_closed_group)
9
+
10
+ __all__ = [
11
+ 'AskHandler', 'CommonHandler',
12
+ 'test_closed_group'
13
+ ]
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (520 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/calculus.cpython-312.pyc ADDED
Binary file (9.6 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/common.cpython-312.pyc ADDED
Binary file (7.44 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/matrices.cpython-312.pyc ADDED
Binary file (39.8 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/ntheory.cpython-312.pyc ADDED
Binary file (11.7 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/order.cpython-312.pyc ADDED
Binary file (21.4 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/__pycache__/sets.cpython-312.pyc ADDED
Binary file (39.5 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/calculus.py ADDED
@@ -0,0 +1,273 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
tool_server/.venv/lib/python3.12/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))
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/matrices.py ADDED
@@ -0,0 +1,716 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This module contains query handlers responsible for Matrices queries:
3
+ Square, Symmetric, Invertible etc.
4
+ """
5
+
6
+ from sympy.logic.boolalg import conjuncts
7
+ from sympy.assumptions import Q, ask
8
+ from sympy.assumptions.handlers import test_closed_group
9
+ from sympy.matrices import MatrixBase
10
+ from sympy.matrices.expressions import (BlockMatrix, BlockDiagMatrix, Determinant,
11
+ DiagMatrix, DiagonalMatrix, HadamardProduct, Identity, Inverse, MatAdd, MatMul,
12
+ MatPow, MatrixExpr, MatrixSlice, MatrixSymbol, OneMatrix, Trace, Transpose,
13
+ ZeroMatrix)
14
+ from sympy.matrices.expressions.blockmatrix import reblock_2x2
15
+ from sympy.matrices.expressions.factorizations import Factorization
16
+ from sympy.matrices.expressions.fourier import DFT
17
+ from sympy.core.logic import fuzzy_and
18
+ from sympy.utilities.iterables import sift
19
+ from sympy.core import Basic
20
+
21
+ from ..predicates.matrices import (SquarePredicate, SymmetricPredicate,
22
+ InvertiblePredicate, OrthogonalPredicate, UnitaryPredicate,
23
+ FullRankPredicate, PositiveDefinitePredicate, UpperTriangularPredicate,
24
+ LowerTriangularPredicate, DiagonalPredicate, IntegerElementsPredicate,
25
+ RealElementsPredicate, ComplexElementsPredicate)
26
+
27
+
28
+ def _Factorization(predicate, expr, assumptions):
29
+ if predicate in expr.predicates:
30
+ return True
31
+
32
+
33
+ # SquarePredicate
34
+
35
+ @SquarePredicate.register(MatrixExpr)
36
+ def _(expr, assumptions):
37
+ return expr.shape[0] == expr.shape[1]
38
+
39
+
40
+ # SymmetricPredicate
41
+
42
+ @SymmetricPredicate.register(MatMul)
43
+ def _(expr, assumptions):
44
+ factor, mmul = expr.as_coeff_mmul()
45
+ if all(ask(Q.symmetric(arg), assumptions) for arg in mmul.args):
46
+ return True
47
+ # TODO: implement sathandlers system for the matrices.
48
+ # Now it duplicates the general fact: Implies(Q.diagonal, Q.symmetric).
49
+ if ask(Q.diagonal(expr), assumptions):
50
+ return True
51
+ if len(mmul.args) >= 2 and mmul.args[0] == mmul.args[-1].T:
52
+ if len(mmul.args) == 2:
53
+ return True
54
+ return ask(Q.symmetric(MatMul(*mmul.args[1:-1])), assumptions)
55
+
56
+ @SymmetricPredicate.register(MatPow)
57
+ def _(expr, assumptions):
58
+ # only for integer powers
59
+ base, exp = expr.args
60
+ int_exp = ask(Q.integer(exp), assumptions)
61
+ if not int_exp:
62
+ return None
63
+ non_negative = ask(~Q.negative(exp), assumptions)
64
+ if (non_negative or non_negative == False
65
+ and ask(Q.invertible(base), assumptions)):
66
+ return ask(Q.symmetric(base), assumptions)
67
+ return None
68
+
69
+ @SymmetricPredicate.register(MatAdd)
70
+ def _(expr, assumptions):
71
+ return all(ask(Q.symmetric(arg), assumptions) for arg in expr.args)
72
+
73
+ @SymmetricPredicate.register(MatrixSymbol)
74
+ def _(expr, assumptions):
75
+ if not expr.is_square:
76
+ return False
77
+ # TODO: implement sathandlers system for the matrices.
78
+ # Now it duplicates the general fact: Implies(Q.diagonal, Q.symmetric).
79
+ if ask(Q.diagonal(expr), assumptions):
80
+ return True
81
+ if Q.symmetric(expr) in conjuncts(assumptions):
82
+ return True
83
+
84
+ @SymmetricPredicate.register_many(OneMatrix, ZeroMatrix)
85
+ def _(expr, assumptions):
86
+ return ask(Q.square(expr), assumptions)
87
+
88
+ @SymmetricPredicate.register_many(Inverse, Transpose)
89
+ def _(expr, assumptions):
90
+ return ask(Q.symmetric(expr.arg), assumptions)
91
+
92
+ @SymmetricPredicate.register(MatrixSlice)
93
+ def _(expr, assumptions):
94
+ # TODO: implement sathandlers system for the matrices.
95
+ # Now it duplicates the general fact: Implies(Q.diagonal, Q.symmetric).
96
+ if ask(Q.diagonal(expr), assumptions):
97
+ return True
98
+ if not expr.on_diag:
99
+ return None
100
+ else:
101
+ return ask(Q.symmetric(expr.parent), assumptions)
102
+
103
+ @SymmetricPredicate.register(Identity)
104
+ def _(expr, assumptions):
105
+ return True
106
+
107
+
108
+ # InvertiblePredicate
109
+
110
+ @InvertiblePredicate.register(MatMul)
111
+ def _(expr, assumptions):
112
+ factor, mmul = expr.as_coeff_mmul()
113
+ if all(ask(Q.invertible(arg), assumptions) for arg in mmul.args):
114
+ return True
115
+ if any(ask(Q.invertible(arg), assumptions) is False
116
+ for arg in mmul.args):
117
+ return False
118
+
119
+ @InvertiblePredicate.register(MatPow)
120
+ def _(expr, assumptions):
121
+ # only for integer powers
122
+ base, exp = expr.args
123
+ int_exp = ask(Q.integer(exp), assumptions)
124
+ if not int_exp:
125
+ return None
126
+ if exp.is_negative == False:
127
+ return ask(Q.invertible(base), assumptions)
128
+ return None
129
+
130
+ @InvertiblePredicate.register(MatAdd)
131
+ def _(expr, assumptions):
132
+ return None
133
+
134
+ @InvertiblePredicate.register(MatrixSymbol)
135
+ def _(expr, assumptions):
136
+ if not expr.is_square:
137
+ return False
138
+ if Q.invertible(expr) in conjuncts(assumptions):
139
+ return True
140
+
141
+ @InvertiblePredicate.register_many(Identity, Inverse)
142
+ def _(expr, assumptions):
143
+ return True
144
+
145
+ @InvertiblePredicate.register(ZeroMatrix)
146
+ def _(expr, assumptions):
147
+ return False
148
+
149
+ @InvertiblePredicate.register(OneMatrix)
150
+ def _(expr, assumptions):
151
+ return expr.shape[0] == 1 and expr.shape[1] == 1
152
+
153
+ @InvertiblePredicate.register(Transpose)
154
+ def _(expr, assumptions):
155
+ return ask(Q.invertible(expr.arg), assumptions)
156
+
157
+ @InvertiblePredicate.register(MatrixSlice)
158
+ def _(expr, assumptions):
159
+ if not expr.on_diag:
160
+ return None
161
+ else:
162
+ return ask(Q.invertible(expr.parent), assumptions)
163
+
164
+ @InvertiblePredicate.register(MatrixBase)
165
+ def _(expr, assumptions):
166
+ if not expr.is_square:
167
+ return False
168
+ return expr.rank() == expr.rows
169
+
170
+ @InvertiblePredicate.register(MatrixExpr)
171
+ def _(expr, assumptions):
172
+ if not expr.is_square:
173
+ return False
174
+ return None
175
+
176
+ @InvertiblePredicate.register(BlockMatrix)
177
+ def _(expr, assumptions):
178
+ if not expr.is_square:
179
+ return False
180
+ if expr.blockshape == (1, 1):
181
+ return ask(Q.invertible(expr.blocks[0, 0]), assumptions)
182
+ expr = reblock_2x2(expr)
183
+ if expr.blockshape == (2, 2):
184
+ [[A, B], [C, D]] = expr.blocks.tolist()
185
+ if ask(Q.invertible(A), assumptions) == True:
186
+ invertible = ask(Q.invertible(D - C * A.I * B), assumptions)
187
+ if invertible is not None:
188
+ return invertible
189
+ if ask(Q.invertible(B), assumptions) == True:
190
+ invertible = ask(Q.invertible(C - D * B.I * A), assumptions)
191
+ if invertible is not None:
192
+ return invertible
193
+ if ask(Q.invertible(C), assumptions) == True:
194
+ invertible = ask(Q.invertible(B - A * C.I * D), assumptions)
195
+ if invertible is not None:
196
+ return invertible
197
+ if ask(Q.invertible(D), assumptions) == True:
198
+ invertible = ask(Q.invertible(A - B * D.I * C), assumptions)
199
+ if invertible is not None:
200
+ return invertible
201
+ return None
202
+
203
+ @InvertiblePredicate.register(BlockDiagMatrix)
204
+ def _(expr, assumptions):
205
+ if expr.rowblocksizes != expr.colblocksizes:
206
+ return None
207
+ return fuzzy_and([ask(Q.invertible(a), assumptions) for a in expr.diag])
208
+
209
+
210
+ # OrthogonalPredicate
211
+
212
+ @OrthogonalPredicate.register(MatMul)
213
+ def _(expr, assumptions):
214
+ factor, mmul = expr.as_coeff_mmul()
215
+ if (all(ask(Q.orthogonal(arg), assumptions) for arg in mmul.args) and
216
+ factor == 1):
217
+ return True
218
+ if any(ask(Q.invertible(arg), assumptions) is False
219
+ for arg in mmul.args):
220
+ return False
221
+
222
+ @OrthogonalPredicate.register(MatPow)
223
+ def _(expr, assumptions):
224
+ # only for integer powers
225
+ base, exp = expr.args
226
+ int_exp = ask(Q.integer(exp), assumptions)
227
+ if int_exp:
228
+ return ask(Q.orthogonal(base), assumptions)
229
+ return None
230
+
231
+ @OrthogonalPredicate.register(MatAdd)
232
+ def _(expr, assumptions):
233
+ if (len(expr.args) == 1 and
234
+ ask(Q.orthogonal(expr.args[0]), assumptions)):
235
+ return True
236
+
237
+ @OrthogonalPredicate.register(MatrixSymbol)
238
+ def _(expr, assumptions):
239
+ if (not expr.is_square or
240
+ ask(Q.invertible(expr), assumptions) is False):
241
+ return False
242
+ if Q.orthogonal(expr) in conjuncts(assumptions):
243
+ return True
244
+
245
+ @OrthogonalPredicate.register(Identity)
246
+ def _(expr, assumptions):
247
+ return True
248
+
249
+ @OrthogonalPredicate.register(ZeroMatrix)
250
+ def _(expr, assumptions):
251
+ return False
252
+
253
+ @OrthogonalPredicate.register_many(Inverse, Transpose)
254
+ def _(expr, assumptions):
255
+ return ask(Q.orthogonal(expr.arg), assumptions)
256
+
257
+ @OrthogonalPredicate.register(MatrixSlice)
258
+ def _(expr, assumptions):
259
+ if not expr.on_diag:
260
+ return None
261
+ else:
262
+ return ask(Q.orthogonal(expr.parent), assumptions)
263
+
264
+ @OrthogonalPredicate.register(Factorization)
265
+ def _(expr, assumptions):
266
+ return _Factorization(Q.orthogonal, expr, assumptions)
267
+
268
+
269
+ # UnitaryPredicate
270
+
271
+ @UnitaryPredicate.register(MatMul)
272
+ def _(expr, assumptions):
273
+ factor, mmul = expr.as_coeff_mmul()
274
+ if (all(ask(Q.unitary(arg), assumptions) for arg in mmul.args) and
275
+ abs(factor) == 1):
276
+ return True
277
+ if any(ask(Q.invertible(arg), assumptions) is False
278
+ for arg in mmul.args):
279
+ return False
280
+
281
+ @UnitaryPredicate.register(MatPow)
282
+ def _(expr, assumptions):
283
+ # only for integer powers
284
+ base, exp = expr.args
285
+ int_exp = ask(Q.integer(exp), assumptions)
286
+ if int_exp:
287
+ return ask(Q.unitary(base), assumptions)
288
+ return None
289
+
290
+ @UnitaryPredicate.register(MatrixSymbol)
291
+ def _(expr, assumptions):
292
+ if (not expr.is_square or
293
+ ask(Q.invertible(expr), assumptions) is False):
294
+ return False
295
+ if Q.unitary(expr) in conjuncts(assumptions):
296
+ return True
297
+
298
+ @UnitaryPredicate.register_many(Inverse, Transpose)
299
+ def _(expr, assumptions):
300
+ return ask(Q.unitary(expr.arg), assumptions)
301
+
302
+ @UnitaryPredicate.register(MatrixSlice)
303
+ def _(expr, assumptions):
304
+ if not expr.on_diag:
305
+ return None
306
+ else:
307
+ return ask(Q.unitary(expr.parent), assumptions)
308
+
309
+ @UnitaryPredicate.register_many(DFT, Identity)
310
+ def _(expr, assumptions):
311
+ return True
312
+
313
+ @UnitaryPredicate.register(ZeroMatrix)
314
+ def _(expr, assumptions):
315
+ return False
316
+
317
+ @UnitaryPredicate.register(Factorization)
318
+ def _(expr, assumptions):
319
+ return _Factorization(Q.unitary, expr, assumptions)
320
+
321
+
322
+ # FullRankPredicate
323
+
324
+ @FullRankPredicate.register(MatMul)
325
+ def _(expr, assumptions):
326
+ if all(ask(Q.fullrank(arg), assumptions) for arg in expr.args):
327
+ return True
328
+
329
+ @FullRankPredicate.register(MatPow)
330
+ def _(expr, assumptions):
331
+ # only for integer powers
332
+ base, exp = expr.args
333
+ int_exp = ask(Q.integer(exp), assumptions)
334
+ if int_exp and ask(~Q.negative(exp), assumptions):
335
+ return ask(Q.fullrank(base), assumptions)
336
+ return None
337
+
338
+ @FullRankPredicate.register(Identity)
339
+ def _(expr, assumptions):
340
+ return True
341
+
342
+ @FullRankPredicate.register(ZeroMatrix)
343
+ def _(expr, assumptions):
344
+ return False
345
+
346
+ @FullRankPredicate.register(OneMatrix)
347
+ def _(expr, assumptions):
348
+ return expr.shape[0] == 1 and expr.shape[1] == 1
349
+
350
+ @FullRankPredicate.register_many(Inverse, Transpose)
351
+ def _(expr, assumptions):
352
+ return ask(Q.fullrank(expr.arg), assumptions)
353
+
354
+ @FullRankPredicate.register(MatrixSlice)
355
+ def _(expr, assumptions):
356
+ if ask(Q.orthogonal(expr.parent), assumptions):
357
+ return True
358
+
359
+
360
+ # PositiveDefinitePredicate
361
+
362
+ @PositiveDefinitePredicate.register(MatMul)
363
+ def _(expr, assumptions):
364
+ factor, mmul = expr.as_coeff_mmul()
365
+ if (all(ask(Q.positive_definite(arg), assumptions)
366
+ for arg in mmul.args) and factor > 0):
367
+ return True
368
+ if (len(mmul.args) >= 2
369
+ and mmul.args[0] == mmul.args[-1].T
370
+ and ask(Q.fullrank(mmul.args[0]), assumptions)):
371
+ return ask(Q.positive_definite(
372
+ MatMul(*mmul.args[1:-1])), assumptions)
373
+
374
+ @PositiveDefinitePredicate.register(MatPow)
375
+ def _(expr, assumptions):
376
+ # a power of a positive definite matrix is positive definite
377
+ if ask(Q.positive_definite(expr.args[0]), assumptions):
378
+ return True
379
+
380
+ @PositiveDefinitePredicate.register(MatAdd)
381
+ def _(expr, assumptions):
382
+ if all(ask(Q.positive_definite(arg), assumptions)
383
+ for arg in expr.args):
384
+ return True
385
+
386
+ @PositiveDefinitePredicate.register(MatrixSymbol)
387
+ def _(expr, assumptions):
388
+ if not expr.is_square:
389
+ return False
390
+ if Q.positive_definite(expr) in conjuncts(assumptions):
391
+ return True
392
+
393
+ @PositiveDefinitePredicate.register(Identity)
394
+ def _(expr, assumptions):
395
+ return True
396
+
397
+ @PositiveDefinitePredicate.register(ZeroMatrix)
398
+ def _(expr, assumptions):
399
+ return False
400
+
401
+ @PositiveDefinitePredicate.register(OneMatrix)
402
+ def _(expr, assumptions):
403
+ return expr.shape[0] == 1 and expr.shape[1] == 1
404
+
405
+ @PositiveDefinitePredicate.register_many(Inverse, Transpose)
406
+ def _(expr, assumptions):
407
+ return ask(Q.positive_definite(expr.arg), assumptions)
408
+
409
+ @PositiveDefinitePredicate.register(MatrixSlice)
410
+ def _(expr, assumptions):
411
+ if not expr.on_diag:
412
+ return None
413
+ else:
414
+ return ask(Q.positive_definite(expr.parent), assumptions)
415
+
416
+
417
+ # UpperTriangularPredicate
418
+
419
+ @UpperTriangularPredicate.register(MatMul)
420
+ def _(expr, assumptions):
421
+ factor, matrices = expr.as_coeff_matrices()
422
+ if all(ask(Q.upper_triangular(m), assumptions) for m in matrices):
423
+ return True
424
+
425
+ @UpperTriangularPredicate.register(MatAdd)
426
+ def _(expr, assumptions):
427
+ if all(ask(Q.upper_triangular(arg), assumptions) for arg in expr.args):
428
+ return True
429
+
430
+ @UpperTriangularPredicate.register(MatPow)
431
+ def _(expr, assumptions):
432
+ # only for integer powers
433
+ base, exp = expr.args
434
+ int_exp = ask(Q.integer(exp), assumptions)
435
+ if not int_exp:
436
+ return None
437
+ non_negative = ask(~Q.negative(exp), assumptions)
438
+ if (non_negative or non_negative == False
439
+ and ask(Q.invertible(base), assumptions)):
440
+ return ask(Q.upper_triangular(base), assumptions)
441
+ return None
442
+
443
+ @UpperTriangularPredicate.register(MatrixSymbol)
444
+ def _(expr, assumptions):
445
+ if Q.upper_triangular(expr) in conjuncts(assumptions):
446
+ return True
447
+
448
+ @UpperTriangularPredicate.register_many(Identity, ZeroMatrix)
449
+ def _(expr, assumptions):
450
+ return True
451
+
452
+ @UpperTriangularPredicate.register(OneMatrix)
453
+ def _(expr, assumptions):
454
+ return expr.shape[0] == 1 and expr.shape[1] == 1
455
+
456
+ @UpperTriangularPredicate.register(Transpose)
457
+ def _(expr, assumptions):
458
+ return ask(Q.lower_triangular(expr.arg), assumptions)
459
+
460
+ @UpperTriangularPredicate.register(Inverse)
461
+ def _(expr, assumptions):
462
+ return ask(Q.upper_triangular(expr.arg), assumptions)
463
+
464
+ @UpperTriangularPredicate.register(MatrixSlice)
465
+ def _(expr, assumptions):
466
+ if not expr.on_diag:
467
+ return None
468
+ else:
469
+ return ask(Q.upper_triangular(expr.parent), assumptions)
470
+
471
+ @UpperTriangularPredicate.register(Factorization)
472
+ def _(expr, assumptions):
473
+ return _Factorization(Q.upper_triangular, expr, assumptions)
474
+
475
+ # LowerTriangularPredicate
476
+
477
+ @LowerTriangularPredicate.register(MatMul)
478
+ def _(expr, assumptions):
479
+ factor, matrices = expr.as_coeff_matrices()
480
+ if all(ask(Q.lower_triangular(m), assumptions) for m in matrices):
481
+ return True
482
+
483
+ @LowerTriangularPredicate.register(MatAdd)
484
+ def _(expr, assumptions):
485
+ if all(ask(Q.lower_triangular(arg), assumptions) for arg in expr.args):
486
+ return True
487
+
488
+ @LowerTriangularPredicate.register(MatPow)
489
+ def _(expr, assumptions):
490
+ # only for integer powers
491
+ base, exp = expr.args
492
+ int_exp = ask(Q.integer(exp), assumptions)
493
+ if not int_exp:
494
+ return None
495
+ non_negative = ask(~Q.negative(exp), assumptions)
496
+ if (non_negative or non_negative == False
497
+ and ask(Q.invertible(base), assumptions)):
498
+ return ask(Q.lower_triangular(base), assumptions)
499
+ return None
500
+
501
+ @LowerTriangularPredicate.register(MatrixSymbol)
502
+ def _(expr, assumptions):
503
+ if Q.lower_triangular(expr) in conjuncts(assumptions):
504
+ return True
505
+
506
+ @LowerTriangularPredicate.register_many(Identity, ZeroMatrix)
507
+ def _(expr, assumptions):
508
+ return True
509
+
510
+ @LowerTriangularPredicate.register(OneMatrix)
511
+ def _(expr, assumptions):
512
+ return expr.shape[0] == 1 and expr.shape[1] == 1
513
+
514
+ @LowerTriangularPredicate.register(Transpose)
515
+ def _(expr, assumptions):
516
+ return ask(Q.upper_triangular(expr.arg), assumptions)
517
+
518
+ @LowerTriangularPredicate.register(Inverse)
519
+ def _(expr, assumptions):
520
+ return ask(Q.lower_triangular(expr.arg), assumptions)
521
+
522
+ @LowerTriangularPredicate.register(MatrixSlice)
523
+ def _(expr, assumptions):
524
+ if not expr.on_diag:
525
+ return None
526
+ else:
527
+ return ask(Q.lower_triangular(expr.parent), assumptions)
528
+
529
+ @LowerTriangularPredicate.register(Factorization)
530
+ def _(expr, assumptions):
531
+ return _Factorization(Q.lower_triangular, expr, assumptions)
532
+
533
+
534
+ # DiagonalPredicate
535
+
536
+ def _is_empty_or_1x1(expr):
537
+ return expr.shape in ((0, 0), (1, 1))
538
+
539
+ @DiagonalPredicate.register(MatMul)
540
+ def _(expr, assumptions):
541
+ if _is_empty_or_1x1(expr):
542
+ return True
543
+ factor, matrices = expr.as_coeff_matrices()
544
+ if all(ask(Q.diagonal(m), assumptions) for m in matrices):
545
+ return True
546
+
547
+ @DiagonalPredicate.register(MatPow)
548
+ def _(expr, assumptions):
549
+ # only for integer powers
550
+ base, exp = expr.args
551
+ int_exp = ask(Q.integer(exp), assumptions)
552
+ if not int_exp:
553
+ return None
554
+ non_negative = ask(~Q.negative(exp), assumptions)
555
+ if (non_negative or non_negative == False
556
+ and ask(Q.invertible(base), assumptions)):
557
+ return ask(Q.diagonal(base), assumptions)
558
+ return None
559
+
560
+ @DiagonalPredicate.register(MatAdd)
561
+ def _(expr, assumptions):
562
+ if all(ask(Q.diagonal(arg), assumptions) for arg in expr.args):
563
+ return True
564
+
565
+ @DiagonalPredicate.register(MatrixSymbol)
566
+ def _(expr, assumptions):
567
+ if _is_empty_or_1x1(expr):
568
+ return True
569
+ if Q.diagonal(expr) in conjuncts(assumptions):
570
+ return True
571
+
572
+ @DiagonalPredicate.register(OneMatrix)
573
+ def _(expr, assumptions):
574
+ return expr.shape[0] == 1 and expr.shape[1] == 1
575
+
576
+ @DiagonalPredicate.register_many(Inverse, Transpose)
577
+ def _(expr, assumptions):
578
+ return ask(Q.diagonal(expr.arg), assumptions)
579
+
580
+ @DiagonalPredicate.register(MatrixSlice)
581
+ def _(expr, assumptions):
582
+ if _is_empty_or_1x1(expr):
583
+ return True
584
+ if not expr.on_diag:
585
+ return None
586
+ else:
587
+ return ask(Q.diagonal(expr.parent), assumptions)
588
+
589
+ @DiagonalPredicate.register_many(DiagonalMatrix, DiagMatrix, Identity, ZeroMatrix)
590
+ def _(expr, assumptions):
591
+ return True
592
+
593
+ @DiagonalPredicate.register(Factorization)
594
+ def _(expr, assumptions):
595
+ return _Factorization(Q.diagonal, expr, assumptions)
596
+
597
+
598
+ # IntegerElementsPredicate
599
+
600
+ def BM_elements(predicate, expr, assumptions):
601
+ """ Block Matrix elements. """
602
+ return all(ask(predicate(b), assumptions) for b in expr.blocks)
603
+
604
+ def MS_elements(predicate, expr, assumptions):
605
+ """ Matrix Slice elements. """
606
+ return ask(predicate(expr.parent), assumptions)
607
+
608
+ def MatMul_elements(matrix_predicate, scalar_predicate, expr, assumptions):
609
+ d = sift(expr.args, lambda x: isinstance(x, MatrixExpr))
610
+ factors, matrices = d[False], d[True]
611
+ return fuzzy_and([
612
+ test_closed_group(Basic(*factors), assumptions, scalar_predicate),
613
+ test_closed_group(Basic(*matrices), assumptions, matrix_predicate)])
614
+
615
+
616
+ @IntegerElementsPredicate.register_many(Determinant, HadamardProduct, MatAdd,
617
+ Trace, Transpose)
618
+ def _(expr, assumptions):
619
+ return test_closed_group(expr, assumptions, Q.integer_elements)
620
+
621
+ @IntegerElementsPredicate.register(MatPow)
622
+ def _(expr, assumptions):
623
+ # only for integer powers
624
+ base, exp = expr.args
625
+ int_exp = ask(Q.integer(exp), assumptions)
626
+ if not int_exp:
627
+ return None
628
+ if exp.is_negative == False:
629
+ return ask(Q.integer_elements(base), assumptions)
630
+ return None
631
+
632
+ @IntegerElementsPredicate.register_many(Identity, OneMatrix, ZeroMatrix)
633
+ def _(expr, assumptions):
634
+ return True
635
+
636
+ @IntegerElementsPredicate.register(MatMul)
637
+ def _(expr, assumptions):
638
+ return MatMul_elements(Q.integer_elements, Q.integer, expr, assumptions)
639
+
640
+ @IntegerElementsPredicate.register(MatrixSlice)
641
+ def _(expr, assumptions):
642
+ return MS_elements(Q.integer_elements, expr, assumptions)
643
+
644
+ @IntegerElementsPredicate.register(BlockMatrix)
645
+ def _(expr, assumptions):
646
+ return BM_elements(Q.integer_elements, expr, assumptions)
647
+
648
+
649
+ # RealElementsPredicate
650
+
651
+ @RealElementsPredicate.register_many(Determinant, Factorization, HadamardProduct,
652
+ MatAdd, Trace, Transpose)
653
+ def _(expr, assumptions):
654
+ return test_closed_group(expr, assumptions, Q.real_elements)
655
+
656
+ @RealElementsPredicate.register(MatPow)
657
+ def _(expr, assumptions):
658
+ # only for integer powers
659
+ base, exp = expr.args
660
+ int_exp = ask(Q.integer(exp), assumptions)
661
+ if not int_exp:
662
+ return None
663
+ non_negative = ask(~Q.negative(exp), assumptions)
664
+ if (non_negative or non_negative == False
665
+ and ask(Q.invertible(base), assumptions)):
666
+ return ask(Q.real_elements(base), assumptions)
667
+ return None
668
+
669
+ @RealElementsPredicate.register(MatMul)
670
+ def _(expr, assumptions):
671
+ return MatMul_elements(Q.real_elements, Q.real, expr, assumptions)
672
+
673
+ @RealElementsPredicate.register(MatrixSlice)
674
+ def _(expr, assumptions):
675
+ return MS_elements(Q.real_elements, expr, assumptions)
676
+
677
+ @RealElementsPredicate.register(BlockMatrix)
678
+ def _(expr, assumptions):
679
+ return BM_elements(Q.real_elements, expr, assumptions)
680
+
681
+
682
+ # ComplexElementsPredicate
683
+
684
+ @ComplexElementsPredicate.register_many(Determinant, Factorization, HadamardProduct,
685
+ Inverse, MatAdd, Trace, Transpose)
686
+ def _(expr, assumptions):
687
+ return test_closed_group(expr, assumptions, Q.complex_elements)
688
+
689
+ @ComplexElementsPredicate.register(MatPow)
690
+ def _(expr, assumptions):
691
+ # only for integer powers
692
+ base, exp = expr.args
693
+ int_exp = ask(Q.integer(exp), assumptions)
694
+ if not int_exp:
695
+ return None
696
+ non_negative = ask(~Q.negative(exp), assumptions)
697
+ if (non_negative or non_negative == False
698
+ and ask(Q.invertible(base), assumptions)):
699
+ return ask(Q.complex_elements(base), assumptions)
700
+ return None
701
+
702
+ @ComplexElementsPredicate.register(MatMul)
703
+ def _(expr, assumptions):
704
+ return MatMul_elements(Q.complex_elements, Q.complex, expr, assumptions)
705
+
706
+ @ComplexElementsPredicate.register(MatrixSlice)
707
+ def _(expr, assumptions):
708
+ return MS_elements(Q.complex_elements, expr, assumptions)
709
+
710
+ @ComplexElementsPredicate.register(BlockMatrix)
711
+ def _(expr, assumptions):
712
+ return BM_elements(Q.complex_elements, expr, assumptions)
713
+
714
+ @ComplexElementsPredicate.register(DFT)
715
+ def _(expr, assumptions):
716
+ return True
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/ntheory.py ADDED
@@ -0,0 +1,279 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/order.py ADDED
@@ -0,0 +1,440 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/handlers/sets.py ADDED
@@ -0,0 +1,816 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
tool_server/.venv/lib/python3.12/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
+ """
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (318 Bytes). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/calculus.cpython-312.pyc ADDED
Binary file (2.53 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/common.cpython-312.pyc ADDED
Binary file (3.15 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/matrices.cpython-312.pyc ADDED
Binary file (14.4 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/ntheory.cpython-312.pyc ADDED
Binary file (3.23 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/order.cpython-312.pyc ADDED
Binary file (10.9 kB). View file
 
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/__pycache__/sets.cpython-312.pyc ADDED
Binary file (10.5 kB). View file
 
tool_server/.venv/lib/python3.12/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")
tool_server/.venv/lib/python3.12/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)
tool_server/.venv/lib/python3.12/site-packages/sympy/assumptions/predicates/matrices.py ADDED
@@ -0,0 +1,511 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sympy.assumptions import Predicate
2
+ from sympy.multipledispatch import Dispatcher
3
+
4
+ class SquarePredicate(Predicate):
5
+ """
6
+ Square matrix predicate.
7
+
8
+ Explanation
9
+ ===========
10
+
11
+ ``Q.square(x)`` is true iff ``x`` is a square matrix. A square matrix
12
+ is a matrix with the same number of rows and columns.
13
+
14
+ Examples
15
+ ========
16
+
17
+ >>> from sympy import Q, ask, MatrixSymbol, ZeroMatrix, Identity
18
+ >>> X = MatrixSymbol('X', 2, 2)
19
+ >>> Y = MatrixSymbol('X', 2, 3)
20
+ >>> ask(Q.square(X))
21
+ True
22
+ >>> ask(Q.square(Y))
23
+ False
24
+ >>> ask(Q.square(ZeroMatrix(3, 3)))
25
+ True
26
+ >>> ask(Q.square(Identity(3)))
27
+ True
28
+
29
+ References
30
+ ==========
31
+
32
+ .. [1] https://en.wikipedia.org/wiki/Square_matrix
33
+
34
+ """
35
+ name = 'square'
36
+ handler = Dispatcher("SquareHandler", doc="Handler for Q.square.")
37
+
38
+
39
+ class SymmetricPredicate(Predicate):
40
+ """
41
+ Symmetric matrix predicate.
42
+
43
+ Explanation
44
+ ===========
45
+
46
+ ``Q.symmetric(x)`` is true iff ``x`` is a square matrix and is equal to
47
+ its transpose. Every square diagonal matrix is a symmetric matrix.
48
+
49
+ Examples
50
+ ========
51
+
52
+ >>> from sympy import Q, ask, MatrixSymbol
53
+ >>> X = MatrixSymbol('X', 2, 2)
54
+ >>> Y = MatrixSymbol('Y', 2, 3)
55
+ >>> Z = MatrixSymbol('Z', 2, 2)
56
+ >>> ask(Q.symmetric(X*Z), Q.symmetric(X) & Q.symmetric(Z))
57
+ True
58
+ >>> ask(Q.symmetric(X + Z), Q.symmetric(X) & Q.symmetric(Z))
59
+ True
60
+ >>> ask(Q.symmetric(Y))
61
+ False
62
+
63
+
64
+ References
65
+ ==========
66
+
67
+ .. [1] https://en.wikipedia.org/wiki/Symmetric_matrix
68
+
69
+ """
70
+ # TODO: Add handlers to make these keys work with
71
+ # actual matrices and add more examples in the docstring.
72
+ name = 'symmetric'
73
+ handler = Dispatcher("SymmetricHandler", doc="Handler for Q.symmetric.")
74
+
75
+
76
+ class InvertiblePredicate(Predicate):
77
+ """
78
+ Invertible matrix predicate.
79
+
80
+ Explanation
81
+ ===========
82
+
83
+ ``Q.invertible(x)`` is true iff ``x`` is an invertible matrix.
84
+ A square matrix is called invertible only if its determinant is 0.
85
+
86
+ Examples
87
+ ========
88
+
89
+ >>> from sympy import Q, ask, MatrixSymbol
90
+ >>> X = MatrixSymbol('X', 2, 2)
91
+ >>> Y = MatrixSymbol('Y', 2, 3)
92
+ >>> Z = MatrixSymbol('Z', 2, 2)
93
+ >>> ask(Q.invertible(X*Y), Q.invertible(X))
94
+ False
95
+ >>> ask(Q.invertible(X*Z), Q.invertible(X) & Q.invertible(Z))
96
+ True
97
+ >>> ask(Q.invertible(X), Q.fullrank(X) & Q.square(X))
98
+ True
99
+
100
+ References
101
+ ==========
102
+
103
+ .. [1] https://en.wikipedia.org/wiki/Invertible_matrix
104
+
105
+ """
106
+ name = 'invertible'
107
+ handler = Dispatcher("InvertibleHandler", doc="Handler for Q.invertible.")
108
+
109
+
110
+ class OrthogonalPredicate(Predicate):
111
+ """
112
+ Orthogonal matrix predicate.
113
+
114
+ Explanation
115
+ ===========
116
+
117
+ ``Q.orthogonal(x)`` is true iff ``x`` is an orthogonal matrix.
118
+ A square matrix ``M`` is an orthogonal matrix if it satisfies
119
+ ``M^TM = MM^T = I`` where ``M^T`` is the transpose matrix of
120
+ ``M`` and ``I`` is an identity matrix. Note that an orthogonal
121
+ matrix is necessarily invertible.
122
+
123
+ Examples
124
+ ========
125
+
126
+ >>> from sympy import Q, ask, MatrixSymbol, Identity
127
+ >>> X = MatrixSymbol('X', 2, 2)
128
+ >>> Y = MatrixSymbol('Y', 2, 3)
129
+ >>> Z = MatrixSymbol('Z', 2, 2)
130
+ >>> ask(Q.orthogonal(Y))
131
+ False
132
+ >>> ask(Q.orthogonal(X*Z*X), Q.orthogonal(X) & Q.orthogonal(Z))
133
+ True
134
+ >>> ask(Q.orthogonal(Identity(3)))
135
+ True
136
+ >>> ask(Q.invertible(X), Q.orthogonal(X))
137
+ True
138
+
139
+ References
140
+ ==========
141
+
142
+ .. [1] https://en.wikipedia.org/wiki/Orthogonal_matrix
143
+
144
+ """
145
+ name = 'orthogonal'
146
+ handler = Dispatcher("OrthogonalHandler", doc="Handler for key 'orthogonal'.")
147
+
148
+
149
+ class UnitaryPredicate(Predicate):
150
+ """
151
+ Unitary matrix predicate.
152
+
153
+ Explanation
154
+ ===========
155
+
156
+ ``Q.unitary(x)`` is true iff ``x`` is a unitary matrix.
157
+ Unitary matrix is an analogue to orthogonal matrix. A square
158
+ matrix ``M`` with complex elements is unitary if :math:``M^TM = MM^T= I``
159
+ where :math:``M^T`` is the conjugate transpose matrix of ``M``.
160
+
161
+ Examples
162
+ ========
163
+
164
+ >>> from sympy import Q, ask, MatrixSymbol, Identity
165
+ >>> X = MatrixSymbol('X', 2, 2)
166
+ >>> Y = MatrixSymbol('Y', 2, 3)
167
+ >>> Z = MatrixSymbol('Z', 2, 2)
168
+ >>> ask(Q.unitary(Y))
169
+ False
170
+ >>> ask(Q.unitary(X*Z*X), Q.unitary(X) & Q.unitary(Z))
171
+ True
172
+ >>> ask(Q.unitary(Identity(3)))
173
+ True
174
+
175
+ References
176
+ ==========
177
+
178
+ .. [1] https://en.wikipedia.org/wiki/Unitary_matrix
179
+
180
+ """
181
+ name = 'unitary'
182
+ handler = Dispatcher("UnitaryHandler", doc="Handler for key 'unitary'.")
183
+
184
+
185
+ class FullRankPredicate(Predicate):
186
+ """
187
+ Fullrank matrix predicate.
188
+
189
+ Explanation
190
+ ===========
191
+
192
+ ``Q.fullrank(x)`` is true iff ``x`` is a full rank matrix.
193
+ A matrix is full rank if all rows and columns of the matrix
194
+ are linearly independent. A square matrix is full rank iff
195
+ its determinant is nonzero.
196
+
197
+ Examples
198
+ ========
199
+
200
+ >>> from sympy import Q, ask, MatrixSymbol, ZeroMatrix, Identity
201
+ >>> X = MatrixSymbol('X', 2, 2)
202
+ >>> ask(Q.fullrank(X.T), Q.fullrank(X))
203
+ True
204
+ >>> ask(Q.fullrank(ZeroMatrix(3, 3)))
205
+ False
206
+ >>> ask(Q.fullrank(Identity(3)))
207
+ True
208
+
209
+ """
210
+ name = 'fullrank'
211
+ handler = Dispatcher("FullRankHandler", doc="Handler for key 'fullrank'.")
212
+
213
+
214
+ class PositiveDefinitePredicate(Predicate):
215
+ r"""
216
+ Positive definite matrix predicate.
217
+
218
+ Explanation
219
+ ===========
220
+
221
+ If $M$ is a :math:`n \times n` symmetric real matrix, it is said
222
+ to be positive definite if :math:`Z^TMZ` is positive for
223
+ every non-zero column vector $Z$ of $n$ real numbers.
224
+
225
+ Examples
226
+ ========
227
+
228
+ >>> from sympy import Q, ask, MatrixSymbol, Identity
229
+ >>> X = MatrixSymbol('X', 2, 2)
230
+ >>> Y = MatrixSymbol('Y', 2, 3)
231
+ >>> Z = MatrixSymbol('Z', 2, 2)
232
+ >>> ask(Q.positive_definite(Y))
233
+ False
234
+ >>> ask(Q.positive_definite(Identity(3)))
235
+ True
236
+ >>> ask(Q.positive_definite(X + Z), Q.positive_definite(X) &
237
+ ... Q.positive_definite(Z))
238
+ True
239
+
240
+ References
241
+ ==========
242
+
243
+ .. [1] https://en.wikipedia.org/wiki/Positive-definite_matrix
244
+
245
+ """
246
+ name = "positive_definite"
247
+ handler = Dispatcher("PositiveDefiniteHandler", doc="Handler for key 'positive_definite'.")
248
+
249
+
250
+ class UpperTriangularPredicate(Predicate):
251
+ """
252
+ Upper triangular matrix predicate.
253
+
254
+ Explanation
255
+ ===========
256
+
257
+ A matrix $M$ is called upper triangular matrix if :math:`M_{ij}=0`
258
+ for :math:`i<j`.
259
+
260
+ Examples
261
+ ========
262
+
263
+ >>> from sympy import Q, ask, ZeroMatrix, Identity
264
+ >>> ask(Q.upper_triangular(Identity(3)))
265
+ True
266
+ >>> ask(Q.upper_triangular(ZeroMatrix(3, 3)))
267
+ True
268
+
269
+ References
270
+ ==========
271
+
272
+ .. [1] https://mathworld.wolfram.com/UpperTriangularMatrix.html
273
+
274
+ """
275
+ name = "upper_triangular"
276
+ handler = Dispatcher("UpperTriangularHandler", doc="Handler for key 'upper_triangular'.")
277
+
278
+
279
+ class LowerTriangularPredicate(Predicate):
280
+ """
281
+ Lower triangular matrix predicate.
282
+
283
+ Explanation
284
+ ===========
285
+
286
+ A matrix $M$ is called lower triangular matrix if :math:`M_{ij}=0`
287
+ for :math:`i>j`.
288
+
289
+ Examples
290
+ ========
291
+
292
+ >>> from sympy import Q, ask, ZeroMatrix, Identity
293
+ >>> ask(Q.lower_triangular(Identity(3)))
294
+ True
295
+ >>> ask(Q.lower_triangular(ZeroMatrix(3, 3)))
296
+ True
297
+
298
+ References
299
+ ==========
300
+
301
+ .. [1] https://mathworld.wolfram.com/LowerTriangularMatrix.html
302
+
303
+ """
304
+ name = "lower_triangular"
305
+ handler = Dispatcher("LowerTriangularHandler", doc="Handler for key 'lower_triangular'.")
306
+
307
+
308
+ class DiagonalPredicate(Predicate):
309
+ """
310
+ Diagonal matrix predicate.
311
+
312
+ Explanation
313
+ ===========
314
+
315
+ ``Q.diagonal(x)`` is true iff ``x`` is a diagonal matrix. A diagonal
316
+ matrix is a matrix in which the entries outside the main diagonal
317
+ are all zero.
318
+
319
+ Examples
320
+ ========
321
+
322
+ >>> from sympy import Q, ask, MatrixSymbol, ZeroMatrix
323
+ >>> X = MatrixSymbol('X', 2, 2)
324
+ >>> ask(Q.diagonal(ZeroMatrix(3, 3)))
325
+ True
326
+ >>> ask(Q.diagonal(X), Q.lower_triangular(X) &
327
+ ... Q.upper_triangular(X))
328
+ True
329
+
330
+ References
331
+ ==========
332
+
333
+ .. [1] https://en.wikipedia.org/wiki/Diagonal_matrix
334
+
335
+ """
336
+ name = "diagonal"
337
+ handler = Dispatcher("DiagonalHandler", doc="Handler for key 'diagonal'.")
338
+
339
+
340
+ class IntegerElementsPredicate(Predicate):
341
+ """
342
+ Integer elements matrix predicate.
343
+
344
+ Explanation
345
+ ===========
346
+
347
+ ``Q.integer_elements(x)`` is true iff all the elements of ``x``
348
+ are integers.
349
+
350
+ Examples
351
+ ========
352
+
353
+ >>> from sympy import Q, ask, MatrixSymbol
354
+ >>> X = MatrixSymbol('X', 4, 4)
355
+ >>> ask(Q.integer(X[1, 2]), Q.integer_elements(X))
356
+ True
357
+
358
+ """
359
+ name = "integer_elements"
360
+ handler = Dispatcher("IntegerElementsHandler", doc="Handler for key 'integer_elements'.")
361
+
362
+
363
+ class RealElementsPredicate(Predicate):
364
+ """
365
+ Real elements matrix predicate.
366
+
367
+ Explanation
368
+ ===========
369
+
370
+ ``Q.real_elements(x)`` is true iff all the elements of ``x``
371
+ are real numbers.
372
+
373
+ Examples
374
+ ========
375
+
376
+ >>> from sympy import Q, ask, MatrixSymbol
377
+ >>> X = MatrixSymbol('X', 4, 4)
378
+ >>> ask(Q.real(X[1, 2]), Q.real_elements(X))
379
+ True
380
+
381
+ """
382
+ name = "real_elements"
383
+ handler = Dispatcher("RealElementsHandler", doc="Handler for key 'real_elements'.")
384
+
385
+
386
+ class ComplexElementsPredicate(Predicate):
387
+ """
388
+ Complex elements matrix predicate.
389
+
390
+ Explanation
391
+ ===========
392
+
393
+ ``Q.complex_elements(x)`` is true iff all the elements of ``x``
394
+ are complex numbers.
395
+
396
+ Examples
397
+ ========
398
+
399
+ >>> from sympy import Q, ask, MatrixSymbol
400
+ >>> X = MatrixSymbol('X', 4, 4)
401
+ >>> ask(Q.complex(X[1, 2]), Q.complex_elements(X))
402
+ True
403
+ >>> ask(Q.complex_elements(X), Q.integer_elements(X))
404
+ True
405
+
406
+ """
407
+ name = "complex_elements"
408
+ handler = Dispatcher("ComplexElementsHandler", doc="Handler for key 'complex_elements'.")
409
+
410
+
411
+ class SingularPredicate(Predicate):
412
+ """
413
+ Singular matrix predicate.
414
+
415
+ A matrix is singular iff the value of its determinant is 0.
416
+
417
+ Examples
418
+ ========
419
+
420
+ >>> from sympy import Q, ask, MatrixSymbol
421
+ >>> X = MatrixSymbol('X', 4, 4)
422
+ >>> ask(Q.singular(X), Q.invertible(X))
423
+ False
424
+ >>> ask(Q.singular(X), ~Q.invertible(X))
425
+ True
426
+
427
+ References
428
+ ==========
429
+
430
+ .. [1] https://mathworld.wolfram.com/SingularMatrix.html
431
+
432
+ """
433
+ name = "singular"
434
+ handler = Dispatcher("SingularHandler", doc="Predicate fore key 'singular'.")
435
+
436
+
437
+ class NormalPredicate(Predicate):
438
+ """
439
+ Normal matrix predicate.
440
+
441
+ A matrix is normal if it commutes with its conjugate transpose.
442
+
443
+ Examples
444
+ ========
445
+
446
+ >>> from sympy import Q, ask, MatrixSymbol
447
+ >>> X = MatrixSymbol('X', 4, 4)
448
+ >>> ask(Q.normal(X), Q.unitary(X))
449
+ True
450
+
451
+ References
452
+ ==========
453
+
454
+ .. [1] https://en.wikipedia.org/wiki/Normal_matrix
455
+
456
+ """
457
+ name = "normal"
458
+ handler = Dispatcher("NormalHandler", doc="Predicate fore key 'normal'.")
459
+
460
+
461
+ class TriangularPredicate(Predicate):
462
+ """
463
+ Triangular matrix predicate.
464
+
465
+ Explanation
466
+ ===========
467
+
468
+ ``Q.triangular(X)`` is true if ``X`` is one that is either lower
469
+ triangular or upper triangular.
470
+
471
+ Examples
472
+ ========
473
+
474
+ >>> from sympy import Q, ask, MatrixSymbol
475
+ >>> X = MatrixSymbol('X', 4, 4)
476
+ >>> ask(Q.triangular(X), Q.upper_triangular(X))
477
+ True
478
+ >>> ask(Q.triangular(X), Q.lower_triangular(X))
479
+ True
480
+
481
+ References
482
+ ==========
483
+
484
+ .. [1] https://en.wikipedia.org/wiki/Triangular_matrix
485
+
486
+ """
487
+ name = "triangular"
488
+ handler = Dispatcher("TriangularHandler", doc="Predicate fore key 'triangular'.")
489
+
490
+
491
+ class UnitTriangularPredicate(Predicate):
492
+ """
493
+ Unit triangular matrix predicate.
494
+
495
+ Explanation
496
+ ===========
497
+
498
+ A unit triangular matrix is a triangular matrix with 1s
499
+ on the diagonal.
500
+
501
+ Examples
502
+ ========
503
+
504
+ >>> from sympy import Q, ask, MatrixSymbol
505
+ >>> X = MatrixSymbol('X', 4, 4)
506
+ >>> ask(Q.triangular(X), Q.unit_triangular(X))
507
+ True
508
+
509
+ """
510
+ name = "unit_triangular"
511
+ handler = Dispatcher("UnitTriangularHandler", doc="Predicate fore key 'unit_triangular'.")
tool_server/.venv/lib/python3.12/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
+ )