Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .gitattributes +1 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_appellseqs.py +91 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_constructor.py +208 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_densearith.py +997 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_dispersion.py +95 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_distributedmodules.py +208 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_euclidtools.py +712 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_fields.py +362 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_heuristicgcd.py +152 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_orderings.py +124 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_partfrac.py +249 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_polyclasses.py +588 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_polyoptions.py +485 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_pythonrational.py +139 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_rationaltools.py +63 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_rootoftools.py +653 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_specialpolys.py +152 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_sqfreetools.py +160 -0
- evalkit_internvl/lib/python3.10/site-packages/sympy/stats/__pycache__/crv_types.cpython-310.pyc +3 -0
- evalkit_tf437/lib/python3.10/site-packages/google_auth_oauthlib-1.2.1.dist-info/METADATA +82 -0
- evalkit_tf437/lib/python3.10/site-packages/google_auth_oauthlib-1.2.1.dist-info/top_level.txt +4 -0
- evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/__pycache__/__init__.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/__pycache__/ansi.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/__pycache__/html.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/__pycache__/pygments.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/__pycache__/utils.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/ansi.py +297 -0
- evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/shortcuts/__pycache__/utils.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/_deprecation_warning.py +12 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/__init__.py +131 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/__pycache__/differentiable_collectives.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/__pycache__/seqpar.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/__pycache__/sequence_parallel_fused_ops.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/__pycache__/swiglu_op.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__init__.py +19 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/__init__.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/k_index_select_cat.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/k_scaled_index_add.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/rmsnorm_kernels.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/rope_padded_kernels.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/sequence_parallel_fused_kernels.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/tiled_matmul_kernels.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/k_index_select_cat.py +184 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/rmsnorm_kernels.py +158 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/rope_padded_kernels.py +188 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/tiled_matmul_kernels.py +430 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/common.py +186 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/differentiable_collectives.py +178 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/fmha/__pycache__/__init__.cpython-310.pyc +0 -0
- evalkit_tf437/lib/python3.10/site-packages/xformers/ops/fmha/__pycache__/attn_bias.cpython-310.pyc +0 -0
.gitattributes
CHANGED
|
@@ -1617,3 +1617,4 @@ evalkit_internvl/lib/python3.10/site-packages/sympy/matrices/__pycache__/matrixb
|
|
| 1617 |
evalkit_tf437/lib/python3.10/site-packages/pip/_vendor/distlib/w64-arm.exe filter=lfs diff=lfs merge=lfs -text
|
| 1618 |
evalkit_internvl/lib/python3.10/site-packages/sympy/core/__pycache__/function.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1619 |
evalkit_internvl/lib/python3.10/site-packages/sympy/core/__pycache__/numbers.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 1617 |
evalkit_tf437/lib/python3.10/site-packages/pip/_vendor/distlib/w64-arm.exe filter=lfs diff=lfs merge=lfs -text
|
| 1618 |
evalkit_internvl/lib/python3.10/site-packages/sympy/core/__pycache__/function.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1619 |
evalkit_internvl/lib/python3.10/site-packages/sympy/core/__pycache__/numbers.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
| 1620 |
+
evalkit_internvl/lib/python3.10/site-packages/sympy/stats/__pycache__/crv_types.cpython-310.pyc filter=lfs diff=lfs merge=lfs -text
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_appellseqs.py
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for efficient functions for generating Appell sequences."""
|
| 2 |
+
from sympy.core.numbers import Rational as Q
|
| 3 |
+
from sympy.polys.polytools import Poly
|
| 4 |
+
from sympy.testing.pytest import raises
|
| 5 |
+
from sympy.polys.appellseqs import (bernoulli_poly, bernoulli_c_poly,
|
| 6 |
+
euler_poly, genocchi_poly, andre_poly)
|
| 7 |
+
from sympy.abc import x
|
| 8 |
+
|
| 9 |
+
def test_bernoulli_poly():
|
| 10 |
+
raises(ValueError, lambda: bernoulli_poly(-1, x))
|
| 11 |
+
assert bernoulli_poly(1, x, polys=True) == Poly(x - Q(1,2))
|
| 12 |
+
|
| 13 |
+
assert bernoulli_poly(0, x) == 1
|
| 14 |
+
assert bernoulli_poly(1, x) == x - Q(1,2)
|
| 15 |
+
assert bernoulli_poly(2, x) == x**2 - x + Q(1,6)
|
| 16 |
+
assert bernoulli_poly(3, x) == x**3 - Q(3,2)*x**2 + Q(1,2)*x
|
| 17 |
+
assert bernoulli_poly(4, x) == x**4 - 2*x**3 + x**2 - Q(1,30)
|
| 18 |
+
assert bernoulli_poly(5, x) == x**5 - Q(5,2)*x**4 + Q(5,3)*x**3 - Q(1,6)*x
|
| 19 |
+
assert bernoulli_poly(6, x) == x**6 - 3*x**5 + Q(5,2)*x**4 - Q(1,2)*x**2 + Q(1,42)
|
| 20 |
+
|
| 21 |
+
assert bernoulli_poly(1).dummy_eq(x - Q(1,2))
|
| 22 |
+
assert bernoulli_poly(1, polys=True) == Poly(x - Q(1,2))
|
| 23 |
+
|
| 24 |
+
def test_bernoulli_c_poly():
|
| 25 |
+
raises(ValueError, lambda: bernoulli_c_poly(-1, x))
|
| 26 |
+
assert bernoulli_c_poly(1, x, polys=True) == Poly(x, domain='QQ')
|
| 27 |
+
|
| 28 |
+
assert bernoulli_c_poly(0, x) == 1
|
| 29 |
+
assert bernoulli_c_poly(1, x) == x
|
| 30 |
+
assert bernoulli_c_poly(2, x) == x**2 - Q(1,3)
|
| 31 |
+
assert bernoulli_c_poly(3, x) == x**3 - x
|
| 32 |
+
assert bernoulli_c_poly(4, x) == x**4 - 2*x**2 + Q(7,15)
|
| 33 |
+
assert bernoulli_c_poly(5, x) == x**5 - Q(10,3)*x**3 + Q(7,3)*x
|
| 34 |
+
assert bernoulli_c_poly(6, x) == x**6 - 5*x**4 + 7*x**2 - Q(31,21)
|
| 35 |
+
|
| 36 |
+
assert bernoulli_c_poly(1).dummy_eq(x)
|
| 37 |
+
assert bernoulli_c_poly(1, polys=True) == Poly(x, domain='QQ')
|
| 38 |
+
|
| 39 |
+
assert 2**8 * bernoulli_poly(8, (x+1)/2).expand() == bernoulli_c_poly(8, x)
|
| 40 |
+
assert 2**9 * bernoulli_poly(9, (x+1)/2).expand() == bernoulli_c_poly(9, x)
|
| 41 |
+
|
| 42 |
+
def test_genocchi_poly():
|
| 43 |
+
raises(ValueError, lambda: genocchi_poly(-1, x))
|
| 44 |
+
assert genocchi_poly(2, x, polys=True) == Poly(-2*x + 1)
|
| 45 |
+
|
| 46 |
+
assert genocchi_poly(0, x) == 0
|
| 47 |
+
assert genocchi_poly(1, x) == -1
|
| 48 |
+
assert genocchi_poly(2, x) == 1 - 2*x
|
| 49 |
+
assert genocchi_poly(3, x) == 3*x - 3*x**2
|
| 50 |
+
assert genocchi_poly(4, x) == -1 + 6*x**2 - 4*x**3
|
| 51 |
+
assert genocchi_poly(5, x) == -5*x + 10*x**3 - 5*x**4
|
| 52 |
+
assert genocchi_poly(6, x) == 3 - 15*x**2 + 15*x**4 - 6*x**5
|
| 53 |
+
|
| 54 |
+
assert genocchi_poly(2).dummy_eq(-2*x + 1)
|
| 55 |
+
assert genocchi_poly(2, polys=True) == Poly(-2*x + 1)
|
| 56 |
+
|
| 57 |
+
assert 2 * (bernoulli_poly(8, x) - bernoulli_c_poly(8, x)) == genocchi_poly(8, x)
|
| 58 |
+
assert 2 * (bernoulli_poly(9, x) - bernoulli_c_poly(9, x)) == genocchi_poly(9, x)
|
| 59 |
+
|
| 60 |
+
def test_euler_poly():
|
| 61 |
+
raises(ValueError, lambda: euler_poly(-1, x))
|
| 62 |
+
assert euler_poly(1, x, polys=True) == Poly(x - Q(1,2))
|
| 63 |
+
|
| 64 |
+
assert euler_poly(0, x) == 1
|
| 65 |
+
assert euler_poly(1, x) == x - Q(1,2)
|
| 66 |
+
assert euler_poly(2, x) == x**2 - x
|
| 67 |
+
assert euler_poly(3, x) == x**3 - Q(3,2)*x**2 + Q(1,4)
|
| 68 |
+
assert euler_poly(4, x) == x**4 - 2*x**3 + x
|
| 69 |
+
assert euler_poly(5, x) == x**5 - Q(5,2)*x**4 + Q(5,2)*x**2 - Q(1,2)
|
| 70 |
+
assert euler_poly(6, x) == x**6 - 3*x**5 + 5*x**3 - 3*x
|
| 71 |
+
|
| 72 |
+
assert euler_poly(1).dummy_eq(x - Q(1,2))
|
| 73 |
+
assert euler_poly(1, polys=True) == Poly(x - Q(1,2))
|
| 74 |
+
|
| 75 |
+
assert genocchi_poly(9, x) == euler_poly(8, x) * -9
|
| 76 |
+
assert genocchi_poly(10, x) == euler_poly(9, x) * -10
|
| 77 |
+
|
| 78 |
+
def test_andre_poly():
|
| 79 |
+
raises(ValueError, lambda: andre_poly(-1, x))
|
| 80 |
+
assert andre_poly(1, x, polys=True) == Poly(x)
|
| 81 |
+
|
| 82 |
+
assert andre_poly(0, x) == 1
|
| 83 |
+
assert andre_poly(1, x) == x
|
| 84 |
+
assert andre_poly(2, x) == x**2 - 1
|
| 85 |
+
assert andre_poly(3, x) == x**3 - 3*x
|
| 86 |
+
assert andre_poly(4, x) == x**4 - 6*x**2 + 5
|
| 87 |
+
assert andre_poly(5, x) == x**5 - 10*x**3 + 25*x
|
| 88 |
+
assert andre_poly(6, x) == x**6 - 15*x**4 + 75*x**2 - 61
|
| 89 |
+
|
| 90 |
+
assert andre_poly(1).dummy_eq(x)
|
| 91 |
+
assert andre_poly(1, polys=True) == Poly(x)
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_constructor.py
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for tools for constructing domains for expressions. """
|
| 2 |
+
|
| 3 |
+
from sympy.polys.constructor import construct_domain
|
| 4 |
+
from sympy.polys.domains import ZZ, QQ, ZZ_I, QQ_I, RR, CC, EX
|
| 5 |
+
from sympy.polys.domains.realfield import RealField
|
| 6 |
+
from sympy.polys.domains.complexfield import ComplexField
|
| 7 |
+
|
| 8 |
+
from sympy.core import (Catalan, GoldenRatio)
|
| 9 |
+
from sympy.core.numbers import (E, Float, I, Rational, pi)
|
| 10 |
+
from sympy.core.singleton import S
|
| 11 |
+
from sympy.functions.elementary.exponential import exp
|
| 12 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 13 |
+
from sympy.functions.elementary.trigonometric import sin
|
| 14 |
+
from sympy.abc import x, y
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def test_construct_domain():
|
| 18 |
+
|
| 19 |
+
assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
|
| 20 |
+
assert construct_domain([1, 2, 3], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])
|
| 21 |
+
|
| 22 |
+
assert construct_domain([S.One, S(2), S(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
|
| 23 |
+
assert construct_domain([S.One, S(2), S(3)], field=True) == (QQ, [QQ(1), QQ(2), QQ(3)])
|
| 24 |
+
|
| 25 |
+
assert construct_domain([S.Half, S(2)]) == (QQ, [QQ(1, 2), QQ(2)])
|
| 26 |
+
result = construct_domain([3.14, 1, S.Half])
|
| 27 |
+
assert isinstance(result[0], RealField)
|
| 28 |
+
assert result[1] == [RR(3.14), RR(1.0), RR(0.5)]
|
| 29 |
+
|
| 30 |
+
result = construct_domain([3.14, I, S.Half])
|
| 31 |
+
assert isinstance(result[0], ComplexField)
|
| 32 |
+
assert result[1] == [CC(3.14), CC(1.0j), CC(0.5)]
|
| 33 |
+
|
| 34 |
+
assert construct_domain([1.0+I]) == (CC, [CC(1.0, 1.0)])
|
| 35 |
+
assert construct_domain([2.0+3.0*I]) == (CC, [CC(2.0, 3.0)])
|
| 36 |
+
|
| 37 |
+
assert construct_domain([1, I]) == (ZZ_I, [ZZ_I(1, 0), ZZ_I(0, 1)])
|
| 38 |
+
assert construct_domain([1, I/2]) == (QQ_I, [QQ_I(1, 0), QQ_I(0, S.Half)])
|
| 39 |
+
|
| 40 |
+
assert construct_domain([3.14, sqrt(2)], extension=None) == (EX, [EX(3.14), EX(sqrt(2))])
|
| 41 |
+
assert construct_domain([3.14, sqrt(2)], extension=True) == (EX, [EX(3.14), EX(sqrt(2))])
|
| 42 |
+
|
| 43 |
+
assert construct_domain([1, sqrt(2)], extension=None) == (EX, [EX(1), EX(sqrt(2))])
|
| 44 |
+
|
| 45 |
+
assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
|
| 46 |
+
assert construct_domain([x, sqrt(x), sqrt(y)]) == (EX, [EX(x), EX(sqrt(x)), EX(sqrt(y))])
|
| 47 |
+
|
| 48 |
+
alg = QQ.algebraic_field(sqrt(2))
|
| 49 |
+
|
| 50 |
+
assert construct_domain([7, S.Half, sqrt(2)], extension=True) == \
|
| 51 |
+
(alg, [alg.convert(7), alg.convert(S.Half), alg.convert(sqrt(2))])
|
| 52 |
+
|
| 53 |
+
alg = QQ.algebraic_field(sqrt(2) + sqrt(3))
|
| 54 |
+
|
| 55 |
+
assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == \
|
| 56 |
+
(alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))])
|
| 57 |
+
|
| 58 |
+
dom = ZZ[x]
|
| 59 |
+
|
| 60 |
+
assert construct_domain([2*x, 3]) == \
|
| 61 |
+
(dom, [dom.convert(2*x), dom.convert(3)])
|
| 62 |
+
|
| 63 |
+
dom = ZZ[x, y]
|
| 64 |
+
|
| 65 |
+
assert construct_domain([2*x, 3*y]) == \
|
| 66 |
+
(dom, [dom.convert(2*x), dom.convert(3*y)])
|
| 67 |
+
|
| 68 |
+
dom = QQ[x]
|
| 69 |
+
|
| 70 |
+
assert construct_domain([x/2, 3]) == \
|
| 71 |
+
(dom, [dom.convert(x/2), dom.convert(3)])
|
| 72 |
+
|
| 73 |
+
dom = QQ[x, y]
|
| 74 |
+
|
| 75 |
+
assert construct_domain([x/2, 3*y]) == \
|
| 76 |
+
(dom, [dom.convert(x/2), dom.convert(3*y)])
|
| 77 |
+
|
| 78 |
+
dom = ZZ_I[x]
|
| 79 |
+
|
| 80 |
+
assert construct_domain([2*x, I]) == \
|
| 81 |
+
(dom, [dom.convert(2*x), dom.convert(I)])
|
| 82 |
+
|
| 83 |
+
dom = ZZ_I[x, y]
|
| 84 |
+
|
| 85 |
+
assert construct_domain([2*x, I*y]) == \
|
| 86 |
+
(dom, [dom.convert(2*x), dom.convert(I*y)])
|
| 87 |
+
|
| 88 |
+
dom = QQ_I[x]
|
| 89 |
+
|
| 90 |
+
assert construct_domain([x/2, I]) == \
|
| 91 |
+
(dom, [dom.convert(x/2), dom.convert(I)])
|
| 92 |
+
|
| 93 |
+
dom = QQ_I[x, y]
|
| 94 |
+
|
| 95 |
+
assert construct_domain([x/2, I*y]) == \
|
| 96 |
+
(dom, [dom.convert(x/2), dom.convert(I*y)])
|
| 97 |
+
|
| 98 |
+
dom = RR[x]
|
| 99 |
+
|
| 100 |
+
assert construct_domain([x/2, 3.5]) == \
|
| 101 |
+
(dom, [dom.convert(x/2), dom.convert(3.5)])
|
| 102 |
+
|
| 103 |
+
dom = RR[x, y]
|
| 104 |
+
|
| 105 |
+
assert construct_domain([x/2, 3.5*y]) == \
|
| 106 |
+
(dom, [dom.convert(x/2), dom.convert(3.5*y)])
|
| 107 |
+
|
| 108 |
+
dom = CC[x]
|
| 109 |
+
|
| 110 |
+
assert construct_domain([I*x/2, 3.5]) == \
|
| 111 |
+
(dom, [dom.convert(I*x/2), dom.convert(3.5)])
|
| 112 |
+
|
| 113 |
+
dom = CC[x, y]
|
| 114 |
+
|
| 115 |
+
assert construct_domain([I*x/2, 3.5*y]) == \
|
| 116 |
+
(dom, [dom.convert(I*x/2), dom.convert(3.5*y)])
|
| 117 |
+
|
| 118 |
+
dom = CC[x]
|
| 119 |
+
|
| 120 |
+
assert construct_domain([x/2, I*3.5]) == \
|
| 121 |
+
(dom, [dom.convert(x/2), dom.convert(I*3.5)])
|
| 122 |
+
|
| 123 |
+
dom = CC[x, y]
|
| 124 |
+
|
| 125 |
+
assert construct_domain([x/2, I*3.5*y]) == \
|
| 126 |
+
(dom, [dom.convert(x/2), dom.convert(I*3.5*y)])
|
| 127 |
+
|
| 128 |
+
dom = ZZ.frac_field(x)
|
| 129 |
+
|
| 130 |
+
assert construct_domain([2/x, 3]) == \
|
| 131 |
+
(dom, [dom.convert(2/x), dom.convert(3)])
|
| 132 |
+
|
| 133 |
+
dom = ZZ.frac_field(x, y)
|
| 134 |
+
|
| 135 |
+
assert construct_domain([2/x, 3*y]) == \
|
| 136 |
+
(dom, [dom.convert(2/x), dom.convert(3*y)])
|
| 137 |
+
|
| 138 |
+
dom = RR.frac_field(x)
|
| 139 |
+
|
| 140 |
+
assert construct_domain([2/x, 3.5]) == \
|
| 141 |
+
(dom, [dom.convert(2/x), dom.convert(3.5)])
|
| 142 |
+
|
| 143 |
+
dom = RR.frac_field(x, y)
|
| 144 |
+
|
| 145 |
+
assert construct_domain([2/x, 3.5*y]) == \
|
| 146 |
+
(dom, [dom.convert(2/x), dom.convert(3.5*y)])
|
| 147 |
+
|
| 148 |
+
dom = RealField(prec=336)[x]
|
| 149 |
+
|
| 150 |
+
assert construct_domain([pi.evalf(100)*x]) == \
|
| 151 |
+
(dom, [dom.convert(pi.evalf(100)*x)])
|
| 152 |
+
|
| 153 |
+
assert construct_domain(2) == (ZZ, ZZ(2))
|
| 154 |
+
assert construct_domain(S(2)/3) == (QQ, QQ(2, 3))
|
| 155 |
+
assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3))
|
| 156 |
+
|
| 157 |
+
assert construct_domain({}) == (ZZ, {})
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def test_complex_exponential():
|
| 161 |
+
w = exp(-I*2*pi/3, evaluate=False)
|
| 162 |
+
alg = QQ.algebraic_field(w)
|
| 163 |
+
assert construct_domain([w**2, w, 1], extension=True) == (
|
| 164 |
+
alg,
|
| 165 |
+
[alg.convert(w**2),
|
| 166 |
+
alg.convert(w),
|
| 167 |
+
alg.convert(1)]
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def test_composite_option():
|
| 172 |
+
assert construct_domain({(1,): sin(y)}, composite=False) == \
|
| 173 |
+
(EX, {(1,): EX(sin(y))})
|
| 174 |
+
|
| 175 |
+
assert construct_domain({(1,): y}, composite=False) == \
|
| 176 |
+
(EX, {(1,): EX(y)})
|
| 177 |
+
|
| 178 |
+
assert construct_domain({(1, 1): 1}, composite=False) == \
|
| 179 |
+
(ZZ, {(1, 1): 1})
|
| 180 |
+
|
| 181 |
+
assert construct_domain({(1, 0): y}, composite=False) == \
|
| 182 |
+
(EX, {(1, 0): EX(y)})
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
def test_precision():
|
| 186 |
+
f1 = Float("1.01")
|
| 187 |
+
f2 = Float("1.0000000000000000000001")
|
| 188 |
+
for u in [1, 1e-2, 1e-6, 1e-13, 1e-14, 1e-16, 1e-20, 1e-100, 1e-300,
|
| 189 |
+
f1, f2]:
|
| 190 |
+
result = construct_domain([u])
|
| 191 |
+
v = float(result[1][0])
|
| 192 |
+
assert abs(u - v) / u < 1e-14 # Test relative accuracy
|
| 193 |
+
|
| 194 |
+
result = construct_domain([f1])
|
| 195 |
+
y = result[1][0]
|
| 196 |
+
assert y-1 > 1e-50
|
| 197 |
+
|
| 198 |
+
result = construct_domain([f2])
|
| 199 |
+
y = result[1][0]
|
| 200 |
+
assert y-1 > 1e-50
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def test_issue_11538():
|
| 204 |
+
for n in [E, pi, Catalan]:
|
| 205 |
+
assert construct_domain(n)[0] == ZZ[n]
|
| 206 |
+
assert construct_domain(x + n)[0] == ZZ[x, n]
|
| 207 |
+
assert construct_domain(GoldenRatio)[0] == EX
|
| 208 |
+
assert construct_domain(x + GoldenRatio)[0] == EX
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_densearith.py
ADDED
|
@@ -0,0 +1,997 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for dense recursive polynomials' arithmetics. """
|
| 2 |
+
|
| 3 |
+
from sympy.external.gmpy import GROUND_TYPES
|
| 4 |
+
|
| 5 |
+
from sympy.polys.densebasic import (
|
| 6 |
+
dup_normal, dmp_normal,
|
| 7 |
+
)
|
| 8 |
+
|
| 9 |
+
from sympy.polys.densearith import (
|
| 10 |
+
dup_add_term, dmp_add_term,
|
| 11 |
+
dup_sub_term, dmp_sub_term,
|
| 12 |
+
dup_mul_term, dmp_mul_term,
|
| 13 |
+
dup_add_ground, dmp_add_ground,
|
| 14 |
+
dup_sub_ground, dmp_sub_ground,
|
| 15 |
+
dup_mul_ground, dmp_mul_ground,
|
| 16 |
+
dup_quo_ground, dmp_quo_ground,
|
| 17 |
+
dup_exquo_ground, dmp_exquo_ground,
|
| 18 |
+
dup_lshift, dup_rshift,
|
| 19 |
+
dup_abs, dmp_abs,
|
| 20 |
+
dup_neg, dmp_neg,
|
| 21 |
+
dup_add, dmp_add,
|
| 22 |
+
dup_sub, dmp_sub,
|
| 23 |
+
dup_mul, dmp_mul,
|
| 24 |
+
dup_sqr, dmp_sqr,
|
| 25 |
+
dup_pow, dmp_pow,
|
| 26 |
+
dup_add_mul, dmp_add_mul,
|
| 27 |
+
dup_sub_mul, dmp_sub_mul,
|
| 28 |
+
dup_pdiv, dup_prem, dup_pquo, dup_pexquo,
|
| 29 |
+
dmp_pdiv, dmp_prem, dmp_pquo, dmp_pexquo,
|
| 30 |
+
dup_rr_div, dmp_rr_div,
|
| 31 |
+
dup_ff_div, dmp_ff_div,
|
| 32 |
+
dup_div, dup_rem, dup_quo, dup_exquo,
|
| 33 |
+
dmp_div, dmp_rem, dmp_quo, dmp_exquo,
|
| 34 |
+
dup_max_norm, dmp_max_norm,
|
| 35 |
+
dup_l1_norm, dmp_l1_norm,
|
| 36 |
+
dup_l2_norm_squared, dmp_l2_norm_squared,
|
| 37 |
+
dup_expand, dmp_expand,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
from sympy.polys.polyerrors import (
|
| 41 |
+
ExactQuotientFailed,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
from sympy.polys.specialpolys import f_polys
|
| 45 |
+
from sympy.polys.domains import FF, ZZ, QQ
|
| 46 |
+
|
| 47 |
+
from sympy.testing.pytest import raises
|
| 48 |
+
|
| 49 |
+
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
|
| 50 |
+
F_0 = dmp_mul_ground(dmp_normal(f_0, 2, QQ), QQ(1, 7), 2, QQ)
|
| 51 |
+
|
| 52 |
+
def test_dup_add_term():
|
| 53 |
+
f = dup_normal([], ZZ)
|
| 54 |
+
|
| 55 |
+
assert dup_add_term(f, ZZ(0), 0, ZZ) == dup_normal([], ZZ)
|
| 56 |
+
|
| 57 |
+
assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1], ZZ)
|
| 58 |
+
assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 0], ZZ)
|
| 59 |
+
assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([1, 0, 0], ZZ)
|
| 60 |
+
|
| 61 |
+
f = dup_normal([1, 1, 1], ZZ)
|
| 62 |
+
|
| 63 |
+
assert dup_add_term(f, ZZ(1), 0, ZZ) == dup_normal([1, 1, 2], ZZ)
|
| 64 |
+
assert dup_add_term(f, ZZ(1), 1, ZZ) == dup_normal([1, 2, 1], ZZ)
|
| 65 |
+
assert dup_add_term(f, ZZ(1), 2, ZZ) == dup_normal([2, 1, 1], ZZ)
|
| 66 |
+
|
| 67 |
+
assert dup_add_term(f, ZZ(1), 3, ZZ) == dup_normal([1, 1, 1, 1], ZZ)
|
| 68 |
+
assert dup_add_term(f, ZZ(1), 4, ZZ) == dup_normal([1, 0, 1, 1, 1], ZZ)
|
| 69 |
+
assert dup_add_term(f, ZZ(1), 5, ZZ) == dup_normal([1, 0, 0, 1, 1, 1], ZZ)
|
| 70 |
+
assert dup_add_term(
|
| 71 |
+
f, ZZ(1), 6, ZZ) == dup_normal([1, 0, 0, 0, 1, 1, 1], ZZ)
|
| 72 |
+
|
| 73 |
+
assert dup_add_term(f, ZZ(-1), 2, ZZ) == dup_normal([1, 1], ZZ)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def test_dmp_add_term():
|
| 77 |
+
assert dmp_add_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, 0, ZZ) == \
|
| 78 |
+
dup_add_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, ZZ)
|
| 79 |
+
assert dmp_add_term(f_0, [[]], 3, 2, ZZ) == f_0
|
| 80 |
+
assert dmp_add_term(F_0, [[]], 3, 2, QQ) == F_0
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def test_dup_sub_term():
|
| 84 |
+
f = dup_normal([], ZZ)
|
| 85 |
+
|
| 86 |
+
assert dup_sub_term(f, ZZ(0), 0, ZZ) == dup_normal([], ZZ)
|
| 87 |
+
|
| 88 |
+
assert dup_sub_term(f, ZZ(1), 0, ZZ) == dup_normal([-1], ZZ)
|
| 89 |
+
assert dup_sub_term(f, ZZ(1), 1, ZZ) == dup_normal([-1, 0], ZZ)
|
| 90 |
+
assert dup_sub_term(f, ZZ(1), 2, ZZ) == dup_normal([-1, 0, 0], ZZ)
|
| 91 |
+
|
| 92 |
+
f = dup_normal([1, 1, 1], ZZ)
|
| 93 |
+
|
| 94 |
+
assert dup_sub_term(f, ZZ(2), 0, ZZ) == dup_normal([ 1, 1, -1], ZZ)
|
| 95 |
+
assert dup_sub_term(f, ZZ(2), 1, ZZ) == dup_normal([ 1, -1, 1], ZZ)
|
| 96 |
+
assert dup_sub_term(f, ZZ(2), 2, ZZ) == dup_normal([-1, 1, 1], ZZ)
|
| 97 |
+
|
| 98 |
+
assert dup_sub_term(f, ZZ(1), 3, ZZ) == dup_normal([-1, 1, 1, 1], ZZ)
|
| 99 |
+
assert dup_sub_term(f, ZZ(1), 4, ZZ) == dup_normal([-1, 0, 1, 1, 1], ZZ)
|
| 100 |
+
assert dup_sub_term(f, ZZ(1), 5, ZZ) == dup_normal([-1, 0, 0, 1, 1, 1], ZZ)
|
| 101 |
+
assert dup_sub_term(
|
| 102 |
+
f, ZZ(1), 6, ZZ) == dup_normal([-1, 0, 0, 0, 1, 1, 1], ZZ)
|
| 103 |
+
|
| 104 |
+
assert dup_sub_term(f, ZZ(1), 2, ZZ) == dup_normal([1, 1], ZZ)
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def test_dmp_sub_term():
|
| 108 |
+
assert dmp_sub_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, 0, ZZ) == \
|
| 109 |
+
dup_sub_term([ZZ(1), ZZ(1), ZZ(1)], ZZ(1), 2, ZZ)
|
| 110 |
+
assert dmp_sub_term(f_0, [[]], 3, 2, ZZ) == f_0
|
| 111 |
+
assert dmp_sub_term(F_0, [[]], 3, 2, QQ) == F_0
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def test_dup_mul_term():
|
| 115 |
+
f = dup_normal([], ZZ)
|
| 116 |
+
|
| 117 |
+
assert dup_mul_term(f, ZZ(2), 3, ZZ) == dup_normal([], ZZ)
|
| 118 |
+
|
| 119 |
+
f = dup_normal([1, 1], ZZ)
|
| 120 |
+
|
| 121 |
+
assert dup_mul_term(f, ZZ(0), 3, ZZ) == dup_normal([], ZZ)
|
| 122 |
+
|
| 123 |
+
f = dup_normal([1, 2, 3], ZZ)
|
| 124 |
+
|
| 125 |
+
assert dup_mul_term(f, ZZ(2), 0, ZZ) == dup_normal([2, 4, 6], ZZ)
|
| 126 |
+
assert dup_mul_term(f, ZZ(2), 1, ZZ) == dup_normal([2, 4, 6, 0], ZZ)
|
| 127 |
+
assert dup_mul_term(f, ZZ(2), 2, ZZ) == dup_normal([2, 4, 6, 0, 0], ZZ)
|
| 128 |
+
assert dup_mul_term(f, ZZ(2), 3, ZZ) == dup_normal([2, 4, 6, 0, 0, 0], ZZ)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def test_dmp_mul_term():
|
| 132 |
+
assert dmp_mul_term([ZZ(1), ZZ(2), ZZ(3)], ZZ(2), 1, 0, ZZ) == \
|
| 133 |
+
dup_mul_term([ZZ(1), ZZ(2), ZZ(3)], ZZ(2), 1, ZZ)
|
| 134 |
+
|
| 135 |
+
assert dmp_mul_term([[]], [ZZ(2)], 3, 1, ZZ) == [[]]
|
| 136 |
+
assert dmp_mul_term([[ZZ(1)]], [], 3, 1, ZZ) == [[]]
|
| 137 |
+
|
| 138 |
+
assert dmp_mul_term([[ZZ(1), ZZ(2)], [ZZ(3)]], [ZZ(2)], 2, 1, ZZ) == \
|
| 139 |
+
[[ZZ(2), ZZ(4)], [ZZ(6)], [], []]
|
| 140 |
+
|
| 141 |
+
assert dmp_mul_term([[]], [QQ(2, 3)], 3, 1, QQ) == [[]]
|
| 142 |
+
assert dmp_mul_term([[QQ(1, 2)]], [], 3, 1, QQ) == [[]]
|
| 143 |
+
|
| 144 |
+
assert dmp_mul_term([[QQ(1, 5), QQ(2, 5)], [QQ(3, 5)]], [QQ(2, 3)], 2, 1, QQ) == \
|
| 145 |
+
[[QQ(2, 15), QQ(4, 15)], [QQ(6, 15)], [], []]
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
def test_dup_add_ground():
|
| 149 |
+
f = ZZ.map([1, 2, 3, 4])
|
| 150 |
+
g = ZZ.map([1, 2, 3, 8])
|
| 151 |
+
|
| 152 |
+
assert dup_add_ground(f, ZZ(4), ZZ) == g
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def test_dmp_add_ground():
|
| 156 |
+
f = ZZ.map([[1], [2], [3], [4]])
|
| 157 |
+
g = ZZ.map([[1], [2], [3], [8]])
|
| 158 |
+
|
| 159 |
+
assert dmp_add_ground(f, ZZ(4), 1, ZZ) == g
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
def test_dup_sub_ground():
|
| 163 |
+
f = ZZ.map([1, 2, 3, 4])
|
| 164 |
+
g = ZZ.map([1, 2, 3, 0])
|
| 165 |
+
|
| 166 |
+
assert dup_sub_ground(f, ZZ(4), ZZ) == g
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def test_dmp_sub_ground():
|
| 170 |
+
f = ZZ.map([[1], [2], [3], [4]])
|
| 171 |
+
g = ZZ.map([[1], [2], [3], []])
|
| 172 |
+
|
| 173 |
+
assert dmp_sub_ground(f, ZZ(4), 1, ZZ) == g
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def test_dup_mul_ground():
|
| 177 |
+
f = dup_normal([], ZZ)
|
| 178 |
+
|
| 179 |
+
assert dup_mul_ground(f, ZZ(2), ZZ) == dup_normal([], ZZ)
|
| 180 |
+
|
| 181 |
+
f = dup_normal([1, 2, 3], ZZ)
|
| 182 |
+
|
| 183 |
+
assert dup_mul_ground(f, ZZ(0), ZZ) == dup_normal([], ZZ)
|
| 184 |
+
assert dup_mul_ground(f, ZZ(2), ZZ) == dup_normal([2, 4, 6], ZZ)
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
def test_dmp_mul_ground():
|
| 188 |
+
assert dmp_mul_ground(f_0, ZZ(2), 2, ZZ) == [
|
| 189 |
+
[[ZZ(2), ZZ(4), ZZ(6)], [ZZ(4)]],
|
| 190 |
+
[[ZZ(6)]],
|
| 191 |
+
[[ZZ(8), ZZ(10), ZZ(12)], [ZZ(2), ZZ(4), ZZ(2)], [ZZ(2)]]
|
| 192 |
+
]
|
| 193 |
+
|
| 194 |
+
assert dmp_mul_ground(F_0, QQ(1, 2), 2, QQ) == [
|
| 195 |
+
[[QQ(1, 14), QQ(2, 14), QQ(3, 14)], [QQ(2, 14)]],
|
| 196 |
+
[[QQ(3, 14)]],
|
| 197 |
+
[[QQ(4, 14), QQ(5, 14), QQ(6, 14)], [QQ(1, 14), QQ(2, 14),
|
| 198 |
+
QQ(1, 14)], [QQ(1, 14)]]
|
| 199 |
+
]
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def test_dup_quo_ground():
|
| 203 |
+
raises(ZeroDivisionError, lambda: dup_quo_ground(dup_normal([1, 2,
|
| 204 |
+
3], ZZ), ZZ(0), ZZ))
|
| 205 |
+
|
| 206 |
+
f = dup_normal([], ZZ)
|
| 207 |
+
|
| 208 |
+
assert dup_quo_ground(f, ZZ(3), ZZ) == dup_normal([], ZZ)
|
| 209 |
+
|
| 210 |
+
f = dup_normal([6, 2, 8], ZZ)
|
| 211 |
+
|
| 212 |
+
assert dup_quo_ground(f, ZZ(1), ZZ) == f
|
| 213 |
+
assert dup_quo_ground(f, ZZ(2), ZZ) == dup_normal([3, 1, 4], ZZ)
|
| 214 |
+
|
| 215 |
+
assert dup_quo_ground(f, ZZ(3), ZZ) == dup_normal([2, 0, 2], ZZ)
|
| 216 |
+
|
| 217 |
+
f = dup_normal([6, 2, 8], QQ)
|
| 218 |
+
|
| 219 |
+
assert dup_quo_ground(f, QQ(1), QQ) == f
|
| 220 |
+
assert dup_quo_ground(f, QQ(2), QQ) == [QQ(3), QQ(1), QQ(4)]
|
| 221 |
+
assert dup_quo_ground(f, QQ(7), QQ) == [QQ(6, 7), QQ(2, 7), QQ(8, 7)]
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
def test_dup_exquo_ground():
|
| 225 |
+
raises(ZeroDivisionError, lambda: dup_exquo_ground(dup_normal([1,
|
| 226 |
+
2, 3], ZZ), ZZ(0), ZZ))
|
| 227 |
+
raises(ExactQuotientFailed, lambda: dup_exquo_ground(dup_normal([1,
|
| 228 |
+
2, 3], ZZ), ZZ(3), ZZ))
|
| 229 |
+
|
| 230 |
+
f = dup_normal([], ZZ)
|
| 231 |
+
|
| 232 |
+
assert dup_exquo_ground(f, ZZ(3), ZZ) == dup_normal([], ZZ)
|
| 233 |
+
|
| 234 |
+
f = dup_normal([6, 2, 8], ZZ)
|
| 235 |
+
|
| 236 |
+
assert dup_exquo_ground(f, ZZ(1), ZZ) == f
|
| 237 |
+
assert dup_exquo_ground(f, ZZ(2), ZZ) == dup_normal([3, 1, 4], ZZ)
|
| 238 |
+
|
| 239 |
+
f = dup_normal([6, 2, 8], QQ)
|
| 240 |
+
|
| 241 |
+
assert dup_exquo_ground(f, QQ(1), QQ) == f
|
| 242 |
+
assert dup_exquo_ground(f, QQ(2), QQ) == [QQ(3), QQ(1), QQ(4)]
|
| 243 |
+
assert dup_exquo_ground(f, QQ(7), QQ) == [QQ(6, 7), QQ(2, 7), QQ(8, 7)]
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
def test_dmp_quo_ground():
|
| 247 |
+
f = dmp_normal([[6], [2], [8]], 1, ZZ)
|
| 248 |
+
|
| 249 |
+
assert dmp_quo_ground(f, ZZ(1), 1, ZZ) == f
|
| 250 |
+
assert dmp_quo_ground(
|
| 251 |
+
f, ZZ(2), 1, ZZ) == dmp_normal([[3], [1], [4]], 1, ZZ)
|
| 252 |
+
|
| 253 |
+
assert dmp_normal(dmp_quo_ground(
|
| 254 |
+
f, ZZ(3), 1, ZZ), 1, ZZ) == dmp_normal([[2], [], [2]], 1, ZZ)
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
def test_dmp_exquo_ground():
|
| 258 |
+
f = dmp_normal([[6], [2], [8]], 1, ZZ)
|
| 259 |
+
|
| 260 |
+
assert dmp_exquo_ground(f, ZZ(1), 1, ZZ) == f
|
| 261 |
+
assert dmp_exquo_ground(
|
| 262 |
+
f, ZZ(2), 1, ZZ) == dmp_normal([[3], [1], [4]], 1, ZZ)
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
def test_dup_lshift():
|
| 266 |
+
assert dup_lshift([], 3, ZZ) == []
|
| 267 |
+
assert dup_lshift([1], 3, ZZ) == [1, 0, 0, 0]
|
| 268 |
+
|
| 269 |
+
|
| 270 |
+
def test_dup_rshift():
|
| 271 |
+
assert dup_rshift([], 3, ZZ) == []
|
| 272 |
+
assert dup_rshift([1, 0, 0, 0], 3, ZZ) == [1]
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def test_dup_abs():
|
| 276 |
+
assert dup_abs([], ZZ) == []
|
| 277 |
+
assert dup_abs([ZZ( 1)], ZZ) == [ZZ(1)]
|
| 278 |
+
assert dup_abs([ZZ(-7)], ZZ) == [ZZ(7)]
|
| 279 |
+
assert dup_abs([ZZ(-1), ZZ(2), ZZ(3)], ZZ) == [ZZ(1), ZZ(2), ZZ(3)]
|
| 280 |
+
|
| 281 |
+
assert dup_abs([], QQ) == []
|
| 282 |
+
assert dup_abs([QQ( 1, 2)], QQ) == [QQ(1, 2)]
|
| 283 |
+
assert dup_abs([QQ(-7, 3)], QQ) == [QQ(7, 3)]
|
| 284 |
+
assert dup_abs(
|
| 285 |
+
[QQ(-1, 7), QQ(2, 7), QQ(3, 7)], QQ) == [QQ(1, 7), QQ(2, 7), QQ(3, 7)]
|
| 286 |
+
|
| 287 |
+
|
| 288 |
+
def test_dmp_abs():
|
| 289 |
+
assert dmp_abs([ZZ(-1)], 0, ZZ) == [ZZ(1)]
|
| 290 |
+
assert dmp_abs([QQ(-1, 2)], 0, QQ) == [QQ(1, 2)]
|
| 291 |
+
|
| 292 |
+
assert dmp_abs([[[]]], 2, ZZ) == [[[]]]
|
| 293 |
+
assert dmp_abs([[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
|
| 294 |
+
assert dmp_abs([[[ZZ(-7)]]], 2, ZZ) == [[[ZZ(7)]]]
|
| 295 |
+
|
| 296 |
+
assert dmp_abs([[[]]], 2, QQ) == [[[]]]
|
| 297 |
+
assert dmp_abs([[[QQ(1, 2)]]], 2, QQ) == [[[QQ(1, 2)]]]
|
| 298 |
+
assert dmp_abs([[[QQ(-7, 9)]]], 2, QQ) == [[[QQ(7, 9)]]]
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def test_dup_neg():
|
| 302 |
+
assert dup_neg([], ZZ) == []
|
| 303 |
+
assert dup_neg([ZZ(1)], ZZ) == [ZZ(-1)]
|
| 304 |
+
assert dup_neg([ZZ(-7)], ZZ) == [ZZ(7)]
|
| 305 |
+
assert dup_neg([ZZ(-1), ZZ(2), ZZ(3)], ZZ) == [ZZ(1), ZZ(-2), ZZ(-3)]
|
| 306 |
+
|
| 307 |
+
assert dup_neg([], QQ) == []
|
| 308 |
+
assert dup_neg([QQ(1, 2)], QQ) == [QQ(-1, 2)]
|
| 309 |
+
assert dup_neg([QQ(-7, 9)], QQ) == [QQ(7, 9)]
|
| 310 |
+
assert dup_neg([QQ(
|
| 311 |
+
-1, 7), QQ(2, 7), QQ(3, 7)], QQ) == [QQ(1, 7), QQ(-2, 7), QQ(-3, 7)]
|
| 312 |
+
|
| 313 |
+
|
| 314 |
+
def test_dmp_neg():
|
| 315 |
+
assert dmp_neg([ZZ(-1)], 0, ZZ) == [ZZ(1)]
|
| 316 |
+
assert dmp_neg([QQ(-1, 2)], 0, QQ) == [QQ(1, 2)]
|
| 317 |
+
|
| 318 |
+
assert dmp_neg([[[]]], 2, ZZ) == [[[]]]
|
| 319 |
+
assert dmp_neg([[[ZZ(1)]]], 2, ZZ) == [[[ZZ(-1)]]]
|
| 320 |
+
assert dmp_neg([[[ZZ(-7)]]], 2, ZZ) == [[[ZZ(7)]]]
|
| 321 |
+
|
| 322 |
+
assert dmp_neg([[[]]], 2, QQ) == [[[]]]
|
| 323 |
+
assert dmp_neg([[[QQ(1, 9)]]], 2, QQ) == [[[QQ(-1, 9)]]]
|
| 324 |
+
assert dmp_neg([[[QQ(-7, 9)]]], 2, QQ) == [[[QQ(7, 9)]]]
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
def test_dup_add():
|
| 328 |
+
assert dup_add([], [], ZZ) == []
|
| 329 |
+
assert dup_add([ZZ(1)], [], ZZ) == [ZZ(1)]
|
| 330 |
+
assert dup_add([], [ZZ(1)], ZZ) == [ZZ(1)]
|
| 331 |
+
assert dup_add([ZZ(1)], [ZZ(1)], ZZ) == [ZZ(2)]
|
| 332 |
+
assert dup_add([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(3)]
|
| 333 |
+
|
| 334 |
+
assert dup_add([ZZ(1), ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1), ZZ(3)]
|
| 335 |
+
assert dup_add([ZZ(1)], [ZZ(1), ZZ(2)], ZZ) == [ZZ(1), ZZ(3)]
|
| 336 |
+
|
| 337 |
+
assert dup_add([ZZ(1), ZZ(
|
| 338 |
+
2), ZZ(3)], [ZZ(8), ZZ(9), ZZ(10)], ZZ) == [ZZ(9), ZZ(11), ZZ(13)]
|
| 339 |
+
|
| 340 |
+
assert dup_add([], [], QQ) == []
|
| 341 |
+
assert dup_add([QQ(1, 2)], [], QQ) == [QQ(1, 2)]
|
| 342 |
+
assert dup_add([], [QQ(1, 2)], QQ) == [QQ(1, 2)]
|
| 343 |
+
assert dup_add([QQ(1, 4)], [QQ(1, 4)], QQ) == [QQ(1, 2)]
|
| 344 |
+
assert dup_add([QQ(1, 4)], [QQ(1, 2)], QQ) == [QQ(3, 4)]
|
| 345 |
+
|
| 346 |
+
assert dup_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ) == [QQ(1, 2), QQ(5, 3)]
|
| 347 |
+
assert dup_add([QQ(1)], [QQ(1, 2), QQ(2, 3)], QQ) == [QQ(1, 2), QQ(5, 3)]
|
| 348 |
+
|
| 349 |
+
assert dup_add([QQ(1, 7), QQ(2, 7), QQ(3, 7)], [QQ(
|
| 350 |
+
8, 7), QQ(9, 7), QQ(10, 7)], QQ) == [QQ(9, 7), QQ(11, 7), QQ(13, 7)]
|
| 351 |
+
|
| 352 |
+
|
| 353 |
+
def test_dmp_add():
|
| 354 |
+
assert dmp_add([ZZ(1), ZZ(2)], [ZZ(1)], 0, ZZ) == \
|
| 355 |
+
dup_add([ZZ(1), ZZ(2)], [ZZ(1)], ZZ)
|
| 356 |
+
assert dmp_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], 0, QQ) == \
|
| 357 |
+
dup_add([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ)
|
| 358 |
+
|
| 359 |
+
assert dmp_add([[[]]], [[[]]], 2, ZZ) == [[[]]]
|
| 360 |
+
assert dmp_add([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[ZZ(1)]]]
|
| 361 |
+
assert dmp_add([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
|
| 362 |
+
assert dmp_add([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(3)]]]
|
| 363 |
+
assert dmp_add([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(3)]]]
|
| 364 |
+
|
| 365 |
+
assert dmp_add([[[]]], [[[]]], 2, QQ) == [[[]]]
|
| 366 |
+
assert dmp_add([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[QQ(1, 2)]]]
|
| 367 |
+
assert dmp_add([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[QQ(1, 2)]]]
|
| 368 |
+
assert dmp_add([[[QQ(2, 7)]]], [[[QQ(1, 7)]]], 2, QQ) == [[[QQ(3, 7)]]]
|
| 369 |
+
assert dmp_add([[[QQ(1, 7)]]], [[[QQ(2, 7)]]], 2, QQ) == [[[QQ(3, 7)]]]
|
| 370 |
+
|
| 371 |
+
|
| 372 |
+
def test_dup_sub():
|
| 373 |
+
assert dup_sub([], [], ZZ) == []
|
| 374 |
+
assert dup_sub([ZZ(1)], [], ZZ) == [ZZ(1)]
|
| 375 |
+
assert dup_sub([], [ZZ(1)], ZZ) == [ZZ(-1)]
|
| 376 |
+
assert dup_sub([ZZ(1)], [ZZ(1)], ZZ) == []
|
| 377 |
+
assert dup_sub([ZZ(1)], [ZZ(2)], ZZ) == [ZZ(-1)]
|
| 378 |
+
|
| 379 |
+
assert dup_sub([ZZ(1), ZZ(2)], [ZZ(1)], ZZ) == [ZZ(1), ZZ(1)]
|
| 380 |
+
assert dup_sub([ZZ(1)], [ZZ(1), ZZ(2)], ZZ) == [ZZ(-1), ZZ(-1)]
|
| 381 |
+
|
| 382 |
+
assert dup_sub([ZZ(3), ZZ(
|
| 383 |
+
2), ZZ(1)], [ZZ(8), ZZ(9), ZZ(10)], ZZ) == [ZZ(-5), ZZ(-7), ZZ(-9)]
|
| 384 |
+
|
| 385 |
+
assert dup_sub([], [], QQ) == []
|
| 386 |
+
assert dup_sub([QQ(1, 2)], [], QQ) == [QQ(1, 2)]
|
| 387 |
+
assert dup_sub([], [QQ(1, 2)], QQ) == [QQ(-1, 2)]
|
| 388 |
+
assert dup_sub([QQ(1, 3)], [QQ(1, 3)], QQ) == []
|
| 389 |
+
assert dup_sub([QQ(1, 3)], [QQ(2, 3)], QQ) == [QQ(-1, 3)]
|
| 390 |
+
|
| 391 |
+
assert dup_sub([QQ(1, 7), QQ(2, 7)], [QQ(1)], QQ) == [QQ(1, 7), QQ(-5, 7)]
|
| 392 |
+
assert dup_sub([QQ(1)], [QQ(1, 7), QQ(2, 7)], QQ) == [QQ(-1, 7), QQ(5, 7)]
|
| 393 |
+
|
| 394 |
+
assert dup_sub([QQ(3, 7), QQ(2, 7), QQ(1, 7)], [QQ(
|
| 395 |
+
8, 7), QQ(9, 7), QQ(10, 7)], QQ) == [QQ(-5, 7), QQ(-7, 7), QQ(-9, 7)]
|
| 396 |
+
|
| 397 |
+
|
| 398 |
+
def test_dmp_sub():
|
| 399 |
+
assert dmp_sub([ZZ(1), ZZ(2)], [ZZ(1)], 0, ZZ) == \
|
| 400 |
+
dup_sub([ZZ(1), ZZ(2)], [ZZ(1)], ZZ)
|
| 401 |
+
assert dmp_sub([QQ(1, 2), QQ(2, 3)], [QQ(1)], 0, QQ) == \
|
| 402 |
+
dup_sub([QQ(1, 2), QQ(2, 3)], [QQ(1)], QQ)
|
| 403 |
+
|
| 404 |
+
assert dmp_sub([[[]]], [[[]]], 2, ZZ) == [[[]]]
|
| 405 |
+
assert dmp_sub([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[ZZ(1)]]]
|
| 406 |
+
assert dmp_sub([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(-1)]]]
|
| 407 |
+
assert dmp_sub([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(1)]]]
|
| 408 |
+
assert dmp_sub([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(-1)]]]
|
| 409 |
+
|
| 410 |
+
assert dmp_sub([[[]]], [[[]]], 2, QQ) == [[[]]]
|
| 411 |
+
assert dmp_sub([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[QQ(1, 2)]]]
|
| 412 |
+
assert dmp_sub([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[QQ(-1, 2)]]]
|
| 413 |
+
assert dmp_sub([[[QQ(2, 7)]]], [[[QQ(1, 7)]]], 2, QQ) == [[[QQ(1, 7)]]]
|
| 414 |
+
assert dmp_sub([[[QQ(1, 7)]]], [[[QQ(2, 7)]]], 2, QQ) == [[[QQ(-1, 7)]]]
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
def test_dup_add_mul():
|
| 418 |
+
assert dup_add_mul([ZZ(1), ZZ(2), ZZ(3)], [ZZ(3), ZZ(2), ZZ(1)],
|
| 419 |
+
[ZZ(1), ZZ(2)], ZZ) == [ZZ(3), ZZ(9), ZZ(7), ZZ(5)]
|
| 420 |
+
assert dmp_add_mul([[ZZ(1), ZZ(2)], [ZZ(3)]], [[ZZ(3)], [ZZ(2), ZZ(1)]],
|
| 421 |
+
[[ZZ(1)], [ZZ(2)]], 1, ZZ) == [[ZZ(3)], [ZZ(3), ZZ(9)], [ZZ(4), ZZ(5)]]
|
| 422 |
+
|
| 423 |
+
|
| 424 |
+
def test_dup_sub_mul():
|
| 425 |
+
assert dup_sub_mul([ZZ(1), ZZ(2), ZZ(3)], [ZZ(3), ZZ(2), ZZ(1)],
|
| 426 |
+
[ZZ(1), ZZ(2)], ZZ) == [ZZ(-3), ZZ(-7), ZZ(-3), ZZ(1)]
|
| 427 |
+
assert dmp_sub_mul([[ZZ(1), ZZ(2)], [ZZ(3)]], [[ZZ(3)], [ZZ(2), ZZ(1)]],
|
| 428 |
+
[[ZZ(1)], [ZZ(2)]], 1, ZZ) == [[ZZ(-3)], [ZZ(-1), ZZ(-5)], [ZZ(-4), ZZ(1)]]
|
| 429 |
+
|
| 430 |
+
|
| 431 |
+
def test_dup_mul():
|
| 432 |
+
assert dup_mul([], [], ZZ) == []
|
| 433 |
+
assert dup_mul([], [ZZ(1)], ZZ) == []
|
| 434 |
+
assert dup_mul([ZZ(1)], [], ZZ) == []
|
| 435 |
+
assert dup_mul([ZZ(1)], [ZZ(1)], ZZ) == [ZZ(1)]
|
| 436 |
+
assert dup_mul([ZZ(5)], [ZZ(7)], ZZ) == [ZZ(35)]
|
| 437 |
+
|
| 438 |
+
assert dup_mul([], [], QQ) == []
|
| 439 |
+
assert dup_mul([], [QQ(1, 2)], QQ) == []
|
| 440 |
+
assert dup_mul([QQ(1, 2)], [], QQ) == []
|
| 441 |
+
assert dup_mul([QQ(1, 2)], [QQ(4, 7)], QQ) == [QQ(2, 7)]
|
| 442 |
+
assert dup_mul([QQ(5, 7)], [QQ(3, 7)], QQ) == [QQ(15, 49)]
|
| 443 |
+
|
| 444 |
+
f = dup_normal([3, 0, 0, 6, 1, 2], ZZ)
|
| 445 |
+
g = dup_normal([4, 0, 1, 0], ZZ)
|
| 446 |
+
h = dup_normal([12, 0, 3, 24, 4, 14, 1, 2, 0], ZZ)
|
| 447 |
+
|
| 448 |
+
assert dup_mul(f, g, ZZ) == h
|
| 449 |
+
assert dup_mul(g, f, ZZ) == h
|
| 450 |
+
|
| 451 |
+
f = dup_normal([2, 0, 0, 1, 7], ZZ)
|
| 452 |
+
h = dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
|
| 453 |
+
|
| 454 |
+
assert dup_mul(f, f, ZZ) == h
|
| 455 |
+
|
| 456 |
+
K = FF(6)
|
| 457 |
+
|
| 458 |
+
assert dup_mul([K(2), K(1)], [K(3), K(4)], K) == [K(5), K(4)]
|
| 459 |
+
|
| 460 |
+
p1 = dup_normal([79, -1, 78, -94, -10, 11, 32, -19, 78, 2, -89, 30, 73, 42,
|
| 461 |
+
85, 77, 83, -30, -34, -2, 95, -81, 37, -49, -46, -58, -16, 37, 35, -11,
|
| 462 |
+
-57, -15, -31, 67, -20, 27, 76, 2, 70, 67, -65, 65, -26, -93, -44, -12,
|
| 463 |
+
-92, 57, -90, -57, -11, -67, -98, -69, 97, -41, 89, 33, 89, -50, 81,
|
| 464 |
+
-31, 60, -27, 43, 29, -77, 44, 21, -91, 32, -57, 33, 3, 53, -51, -38,
|
| 465 |
+
-99, -84, 23, -50, 66, -100, 1, -75, -25, 27, -60, 98, -51, -87, 6, 8,
|
| 466 |
+
78, -28, -95, -88, 12, -35, 26, -9, 16, -92, 55, -7, -86, 68, -39, -46,
|
| 467 |
+
84, 94, 45, 60, 92, 68, -75, -74, -19, 8, 75, 78, 91, 57, 34, 14, -3,
|
| 468 |
+
-49, 65, 78, -18, 6, -29, -80, -98, 17, 13, 58, 21, 20, 9, 37, 7, -30,
|
| 469 |
+
-53, -20, 34, 67, -42, 89, -22, 73, 43, -6, 5, 51, -8, -15, -52, -22,
|
| 470 |
+
-58, -72, -3, 43, -92, 82, 83, -2, -13, -23, -60, 16, -94, -8, -28,
|
| 471 |
+
-95, -72, 63, -90, 76, 6, -43, -100, -59, 76, 3, 3, 46, -85, 75, 62,
|
| 472 |
+
-71, -76, 88, 97, -72, -1, 30, -64, 72, -48, 14, -78, 58, 63, -91, 24,
|
| 473 |
+
-87, -27, -80, -100, -44, 98, 70, 100, -29, -38, 11, 77, 100, 52, 86,
|
| 474 |
+
65, -5, -42, -81, -38, -42, 43, -2, -70, -63, -52], ZZ)
|
| 475 |
+
p2 = dup_normal([65, -19, -47, 1, 90, 81, -15, -34, 25, -75, 9, -83, 50, -5,
|
| 476 |
+
-44, 31, 1, 70, -7, 78, 74, 80, 85, 65, 21, 41, 66, 19, -40, 63, -21,
|
| 477 |
+
-27, 32, 69, 83, 34, -35, 14, 81, 57, -75, 32, -67, -89, -100, -61, 46,
|
| 478 |
+
84, -78, -29, -50, -94, -24, -32, -68, -16, 100, -7, -72, -89, 35, 82,
|
| 479 |
+
58, 81, -92, 62, 5, -47, -39, -58, -72, -13, 84, 44, 55, -25, 48, -54,
|
| 480 |
+
-31, -56, -11, -50, -84, 10, 67, 17, 13, -14, 61, 76, -64, -44, -40,
|
| 481 |
+
-96, 11, -11, -94, 2, 6, 27, -6, 68, -54, 66, -74, -14, -1, -24, -73,
|
| 482 |
+
96, 89, -11, -89, 56, -53, 72, -43, 96, 25, 63, -31, 29, 68, 83, 91,
|
| 483 |
+
-93, -19, -38, -40, 40, -12, -19, -79, 44, 100, -66, -29, -77, 62, 39,
|
| 484 |
+
-8, 11, -97, 14, 87, 64, 21, -18, 13, 15, -59, -75, -99, -88, 57, 54,
|
| 485 |
+
56, -67, 6, -63, -59, -14, 28, 87, -20, -39, 84, -91, -2, 49, -75, 11,
|
| 486 |
+
-24, -95, 36, 66, 5, 25, -72, -40, 86, 90, 37, -33, 57, -35, 29, -18,
|
| 487 |
+
4, -79, 64, -17, -27, 21, 29, -5, -44, -87, -24, 52, 78, 11, -23, -53,
|
| 488 |
+
36, 42, 21, -68, 94, -91, -51, -21, 51, -76, 72, 31, 24, -48, -80, -9,
|
| 489 |
+
37, -47, -6, -8, -63, -91, 79, -79, -100, 38, -20, 38, 100, 83, -90,
|
| 490 |
+
87, 63, -36, 82, -19, 18, -98, -38, 26, 98, -70, 79, 92, 12, 12, 70,
|
| 491 |
+
74, 36, 48, -13, 31, 31, -47, -71, -12, -64, 36, -42, 32, -86, 60, 83,
|
| 492 |
+
70, 55, 0, 1, 29, -35, 8, -82, 8, -73, -46, -50, 43, 48, -5, -86, -72,
|
| 493 |
+
44, -90, 19, 19, 5, -20, 97, -13, -66, -5, 5, -69, 64, -30, 41, 51, 36,
|
| 494 |
+
13, -99, -61, 94, -12, 74, 98, 68, 24, 46, -97, -87, -6, -27, 82, 62,
|
| 495 |
+
-11, -77, 86, 66, -47, -49, -50, 13, 18, 89, -89, 46, -80, 13, 98, -35,
|
| 496 |
+
-36, -25, 12, 20, 26, -52, 79, 27, 79, 100, 8, 62, -58, -28, 37], ZZ)
|
| 497 |
+
res = dup_normal([5135, -1566, 1376, -7466, 4579, 11710, 8001, -7183,
|
| 498 |
+
-3737, -7439, 345, -10084, 24522, -1201, 1070, -10245, 9582, 9264,
|
| 499 |
+
1903, 23312, 18953, 10037, -15268, -5450, 6442, -6243, -3777, 5110,
|
| 500 |
+
10936, -16649, -6022, 16255, 31300, 24818, 31922, 32760, 7854, 27080,
|
| 501 |
+
15766, 29596, 7139, 31945, -19810, 465, -38026, -3971, 9641, 465,
|
| 502 |
+
-19375, 5524, -30112, -11960, -12813, 13535, 30670, 5925, -43725,
|
| 503 |
+
-14089, 11503, -22782, 6371, 43881, 37465, -33529, -33590, -39798,
|
| 504 |
+
-37854, -18466, -7908, -35825, -26020, -36923, -11332, -5699, 25166,
|
| 505 |
+
-3147, 19885, 12962, -20659, -1642, 27723, -56331, -24580, -11010,
|
| 506 |
+
-20206, 20087, -23772, -16038, 38580, 20901, -50731, 32037, -4299,
|
| 507 |
+
26508, 18038, -28357, 31846, -7405, -20172, -15894, 2096, 25110,
|
| 508 |
+
-45786, 45918, -55333, -31928, -49428, -29824, -58796, -24609, -15408,
|
| 509 |
+
69, -35415, -18439, 10123, -20360, -65949, 33356, -20333, 26476,
|
| 510 |
+
-32073, 33621, 930, 28803, -42791, 44716, 38164, 12302, -1739, 11421,
|
| 511 |
+
73385, -7613, 14297, 38155, -414, 77587, 24338, -21415, 29367, 42639,
|
| 512 |
+
13901, -288, 51027, -11827, 91260, 43407, 88521, -15186, 70572, -12049,
|
| 513 |
+
5090, -12208, -56374, 15520, -623, -7742, 50825, 11199, -14894, 40892,
|
| 514 |
+
59591, -31356, -28696, -57842, -87751, -33744, -28436, -28945, -40287,
|
| 515 |
+
37957, -35638, 33401, -61534, 14870, 40292, 70366, -10803, 102290,
|
| 516 |
+
-71719, -85251, 7902, -22409, 75009, 99927, 35298, -1175, -762, -34744,
|
| 517 |
+
-10587, -47574, -62629, -19581, -43659, -54369, -32250, -39545, 15225,
|
| 518 |
+
-24454, 11241, -67308, -30148, 39929, 37639, 14383, -73475, -77636,
|
| 519 |
+
-81048, -35992, 41601, -90143, 76937, -8112, 56588, 9124, -40094,
|
| 520 |
+
-32340, 13253, 10898, -51639, 36390, 12086, -1885, 100714, -28561,
|
| 521 |
+
-23784, -18735, 18916, 16286, 10742, -87360, -13697, 10689, -19477,
|
| 522 |
+
-29770, 5060, 20189, -8297, 112407, 47071, 47743, 45519, -4109, 17468,
|
| 523 |
+
-68831, 78325, -6481, -21641, -19459, 30919, 96115, 8607, 53341, 32105,
|
| 524 |
+
-16211, 23538, 57259, -76272, -40583, 62093, 38511, -34255, -40665,
|
| 525 |
+
-40604, -37606, -15274, 33156, -13885, 103636, 118678, -14101, -92682,
|
| 526 |
+
-100791, 2634, 63791, 98266, 19286, -34590, -21067, -71130, 25380,
|
| 527 |
+
-40839, -27614, -26060, 52358, -15537, 27138, -6749, 36269, -33306,
|
| 528 |
+
13207, -91084, -5540, -57116, 69548, 44169, -57742, -41234, -103327,
|
| 529 |
+
-62904, -8566, 41149, -12866, 71188, 23980, 1838, 58230, 73950, 5594,
|
| 530 |
+
43113, -8159, -15925, 6911, 85598, -75016, -16214, -62726, -39016,
|
| 531 |
+
8618, -63882, -4299, 23182, 49959, 49342, -3238, -24913, -37138, 78361,
|
| 532 |
+
32451, 6337, -11438, -36241, -37737, 8169, -3077, -24829, 57953, 53016,
|
| 533 |
+
-31511, -91168, 12599, -41849, 41576, 55275, -62539, 47814, -62319,
|
| 534 |
+
12300, -32076, -55137, -84881, -27546, 4312, -3433, -54382, 113288,
|
| 535 |
+
-30157, 74469, 18219, 79880, -2124, 98911, 17655, -33499, -32861,
|
| 536 |
+
47242, -37393, 99765, 14831, -44483, 10800, -31617, -52710, 37406,
|
| 537 |
+
22105, 29704, -20050, 13778, 43683, 36628, 8494, 60964, -22644, 31550,
|
| 538 |
+
-17693, 33805, -124879, -12302, 19343, 20400, -30937, -21574, -34037,
|
| 539 |
+
-33380, 56539, -24993, -75513, -1527, 53563, 65407, -101, 53577, 37991,
|
| 540 |
+
18717, -23795, -8090, -47987, -94717, 41967, 5170, -14815, -94311,
|
| 541 |
+
17896, -17734, -57718, -774, -38410, 24830, 29682, 76480, 58802,
|
| 542 |
+
-46416, -20348, -61353, -68225, -68306, 23822, -31598, 42972, 36327,
|
| 543 |
+
28968, -65638, -21638, 24354, -8356, 26777, 52982, -11783, -44051,
|
| 544 |
+
-26467, -44721, -28435, -53265, -25574, -2669, 44155, 22946, -18454,
|
| 545 |
+
-30718, -11252, 58420, 8711, 67447, 4425, 41749, 67543, 43162, 11793,
|
| 546 |
+
-41907, 20477, -13080, 6559, -6104, -13244, 42853, 42935, 29793, 36730,
|
| 547 |
+
-28087, 28657, 17946, 7503, 7204, 21491, -27450, -24241, -98156,
|
| 548 |
+
-18082, -42613, -24928, 10775, -14842, -44127, 55910, 14777, 31151, -2194,
|
| 549 |
+
39206, -2100, -4211, 11827, -8918, -19471, 72567, 36447, -65590, -34861,
|
| 550 |
+
-17147, -45303, 9025, -7333, -35473, 11101, 11638, 3441, 6626, -41800,
|
| 551 |
+
9416, 13679, 33508, 40502, -60542, 16358, 8392, -43242, -35864, -34127,
|
| 552 |
+
-48721, 35878, 30598, 28630, 20279, -19983, -14638, -24455, -1851, -11344,
|
| 553 |
+
45150, 42051, 26034, -28889, -32382, -3527, -14532, 22564, -22346, 477,
|
| 554 |
+
11706, 28338, -25972, -9185, -22867, -12522, 32120, -4424, 11339, -33913,
|
| 555 |
+
-7184, 5101, -23552, -17115, -31401, -6104, 21906, 25708, 8406, 6317,
|
| 556 |
+
-7525, 5014, 20750, 20179, 22724, 11692, 13297, 2493, -253, -16841, -17339,
|
| 557 |
+
-6753, -4808, 2976, -10881, -10228, -13816, -12686, 1385, 2316, 2190, -875,
|
| 558 |
+
-1924], ZZ)
|
| 559 |
+
|
| 560 |
+
assert dup_mul(p1, p2, ZZ) == res
|
| 561 |
+
|
| 562 |
+
p1 = dup_normal([83, -61, -86, -24, 12, 43, -88, -9, 42, 55, -66, 74, 95,
|
| 563 |
+
-25, -12, 68, -99, 4, 45, 6, -15, -19, 78, 65, -55, 47, -13, 17, 86,
|
| 564 |
+
81, -58, -27, 50, -40, -24, 39, -41, -92, 75, 90, -1, 40, -15, -27,
|
| 565 |
+
-35, 68, 70, -64, -40, 78, -88, -58, -39, 69, 46, 12, 28, -94, -37,
|
| 566 |
+
-50, -80, -96, -61, 25, 1, 71, 4, 12, 48, 4, 34, -47, -75, 5, 48, 82,
|
| 567 |
+
88, 23, 98, 35, 17, -10, 48, -61, -95, 47, 65, -19, -66, -57, -6, -51,
|
| 568 |
+
-42, -89, 66, -13, 18, 37, 90, -23, 72, 96, -53, 0, 40, -73, -52, -68,
|
| 569 |
+
32, -25, -53, 79, -52, 18, 44, 73, -81, 31, -90, 70, 3, 36, 48, 76,
|
| 570 |
+
-24, -44, 23, 98, -4, 73, 69, 88, -70, 14, -68, 94, -78, -15, -64, -97,
|
| 571 |
+
-70, -35, 65, 88, 49, -53, -7, 12, -45, -7, 59, -94, 99, -2, 67, -60,
|
| 572 |
+
-71, 29, -62, -77, 1, 51, 17, 80, -20, -47, -19, 24, -9, 39, -23, 21,
|
| 573 |
+
-84, 10, 84, 56, -17, -21, -66, 85, 70, 46, -51, -22, -95, 78, -60,
|
| 574 |
+
-96, -97, -45, 72, 35, 30, -61, -92, -93, -60, -61, 4, -4, -81, -73,
|
| 575 |
+
46, 53, -11, 26, 94, 45, 14, -78, 55, 84, -68, 98, 60, 23, 100, -63,
|
| 576 |
+
68, 96, -16, 3, 56, 21, -58, 62, -67, 66, 85, 41, -79, -22, 97, -67,
|
| 577 |
+
82, 82, -96, -20, -7, 48, -67, 48, -9, -39, 78], ZZ)
|
| 578 |
+
p2 = dup_normal([52, 88, 76, 66, 9, -64, 46, -20, -28, 69, 60, 96, -36,
|
| 579 |
+
-92, -30, -11, -35, 35, 55, 63, -92, -7, 25, -58, 74, 55, -6, 4, 47,
|
| 580 |
+
-92, -65, 67, -45, 74, -76, 59, -6, 69, 39, 24, -71, -7, 39, -45, 60,
|
| 581 |
+
-68, 98, 97, -79, 17, 4, 94, -64, 68, -100, -96, -2, 3, 22, 96, 54,
|
| 582 |
+
-77, -86, 67, 6, 57, 37, 40, 89, -78, 64, -94, -45, -92, 57, 87, -26,
|
| 583 |
+
36, 19, 97, 25, 77, -87, 24, 43, -5, 35, 57, 83, 71, 35, 63, 61, 96,
|
| 584 |
+
-22, 8, -1, 96, 43, 45, 94, -93, 36, 71, -41, -99, 85, -48, 59, 52,
|
| 585 |
+
-17, 5, 87, -16, -68, -54, 76, -18, 100, 91, -42, -70, -66, -88, -12,
|
| 586 |
+
1, 95, -82, 52, 43, -29, 3, 12, 72, -99, -43, -32, -93, -51, 16, -20,
|
| 587 |
+
-12, -11, 5, 33, -38, 93, -5, -74, 25, 74, -58, 93, 59, -63, -86, 63,
|
| 588 |
+
-20, -4, -74, -73, -95, 29, -28, 93, -91, -2, -38, -62, 77, -58, -85,
|
| 589 |
+
-28, 95, 38, 19, -69, 86, 94, 25, -2, -4, 47, 34, -59, 35, -48, 29,
|
| 590 |
+
-63, -53, 34, 29, 66, 73, 6, 92, -84, 89, 15, 81, 93, 97, 51, -72, -78,
|
| 591 |
+
25, 60, 90, -45, 39, 67, -84, -62, 57, 26, -32, -56, -14, -83, 76, 5,
|
| 592 |
+
-2, 99, -100, 28, 46, 94, -7, 53, -25, 16, -23, -36, 89, -78, -63, 31,
|
| 593 |
+
1, 84, -99, -52, 76, 48, 90, -76, 44, -19, 54, -36, -9, -73, -100, -69,
|
| 594 |
+
31, 42, 25, -39, 76, -26, -8, -14, 51, 3, 37, 45, 2, -54, 13, -34, -92,
|
| 595 |
+
17, -25, -65, 53, -63, 30, 4, -70, -67, 90, 52, 51, 18, -3, 31, -45,
|
| 596 |
+
-9, 59, 63, -87, 22, -32, 29, -38, 21, 36, -82, 27, -11], ZZ)
|
| 597 |
+
res = dup_normal([4316, 4132, -3532, -7974, -11303, -10069, 5484, -3330,
|
| 598 |
+
-5874, 7734, 4673, 11327, -9884, -8031, 17343, 21035, -10570, -9285,
|
| 599 |
+
15893, 3780, -14083, 8819, 17592, 10159, 7174, -11587, 8598, -16479,
|
| 600 |
+
3602, 25596, 9781, 12163, 150, 18749, -21782, -12307, 27578, -2757,
|
| 601 |
+
-12573, 12565, 6345, -18956, 19503, -15617, 1443, -16778, 36851, 23588,
|
| 602 |
+
-28474, 5749, 40695, -7521, -53669, -2497, -18530, 6770, 57038, 3926,
|
| 603 |
+
-6927, -15399, 1848, -64649, -27728, 3644, 49608, 15187, -8902, -9480,
|
| 604 |
+
-7398, -40425, 4824, 23767, -7594, -6905, 33089, 18786, 12192, 24670,
|
| 605 |
+
31114, 35334, -4501, -14676, 7107, -59018, -21352, 20777, 19661, 20653,
|
| 606 |
+
33754, -885, -43758, 6269, 51897, -28719, -97488, -9527, 13746, 11644,
|
| 607 |
+
17644, -21720, 23782, -10481, 47867, 20752, 33810, -1875, 39918, -7710,
|
| 608 |
+
-40840, 19808, -47075, 23066, 46616, 25201, 9287, 35436, -1602, 9645,
|
| 609 |
+
-11978, 13273, 15544, 33465, 20063, 44539, 11687, 27314, -6538, -37467,
|
| 610 |
+
14031, 32970, -27086, 41323, 29551, 65910, -39027, -37800, -22232,
|
| 611 |
+
8212, 46316, -28981, -55282, 50417, -44929, -44062, 73879, 37573,
|
| 612 |
+
-2596, -10877, -21893, -133218, -33707, -25753, -9531, 17530, 61126,
|
| 613 |
+
2748, -56235, 43874, -10872, -90459, -30387, 115267, -7264, -44452,
|
| 614 |
+
122626, 14839, -599, 10337, 57166, -67467, -54957, 63669, 1202, 18488,
|
| 615 |
+
52594, 7205, -97822, 612, 78069, -5403, -63562, 47236, 36873, -154827,
|
| 616 |
+
-26188, 82427, -39521, 5628, 7416, 5276, -53095, 47050, 26121, -42207,
|
| 617 |
+
79021, -13035, 2499, -66943, 29040, -72355, -23480, 23416, -12885,
|
| 618 |
+
-44225, -42688, -4224, 19858, 55299, 15735, 11465, 101876, -39169,
|
| 619 |
+
51786, 14723, 43280, -68697, 16410, 92295, 56767, 7183, 111850, 4550,
|
| 620 |
+
115451, -38443, -19642, -35058, 10230, 93829, 8925, 63047, 3146, 29250,
|
| 621 |
+
8530, 5255, -98117, -115517, -76817, -8724, 41044, 1312, -35974, 79333,
|
| 622 |
+
-28567, 7547, -10580, -24559, -16238, 10794, -3867, 24848, 57770,
|
| 623 |
+
-51536, -35040, 71033, 29853, 62029, -7125, -125585, -32169, -47907,
|
| 624 |
+
156811, -65176, -58006, -15757, -57861, 11963, 30225, -41901, -41681,
|
| 625 |
+
31310, 27982, 18613, 61760, 60746, -59096, 33499, 30097, -17997, 24032,
|
| 626 |
+
56442, -83042, 23747, -20931, -21978, -158752, -9883, -73598, -7987,
|
| 627 |
+
-7333, -125403, -116329, 30585, 53281, 51018, -29193, 88575, 8264,
|
| 628 |
+
-40147, -16289, 113088, 12810, -6508, 101552, -13037, 34440, -41840,
|
| 629 |
+
101643, 24263, 80532, 61748, 65574, 6423, -20672, 6591, -10834, -71716,
|
| 630 |
+
86919, -92626, 39161, 28490, 81319, 46676, 106720, 43530, 26998, 57456,
|
| 631 |
+
-8862, 60989, 13982, 3119, -2224, 14743, 55415, -49093, -29303, 28999,
|
| 632 |
+
1789, 55953, -84043, -7780, -65013, 57129, -47251, 61484, 61994,
|
| 633 |
+
-78361, -82778, 22487, -26894, 9756, -74637, -15519, -4360, 30115,
|
| 634 |
+
42433, 35475, 15286, 69768, 21509, -20214, 78675, -21163, 13596, 11443,
|
| 635 |
+
-10698, -53621, -53867, -24155, 64500, -42784, -33077, -16500, 873,
|
| 636 |
+
-52788, 14546, -38011, 36974, -39849, -34029, -94311, 83068, -50437,
|
| 637 |
+
-26169, -46746, 59185, 42259, -101379, -12943, 30089, -59086, 36271,
|
| 638 |
+
22723, -30253, -52472, -70826, -23289, 3331, -31687, 14183, -857,
|
| 639 |
+
-28627, 35246, -51284, 5636, -6933, 66539, 36654, 50927, 24783, 3457,
|
| 640 |
+
33276, 45281, 45650, -4938, -9968, -22590, 47995, 69229, 5214, -58365,
|
| 641 |
+
-17907, -14651, 18668, 18009, 12649, -11851, -13387, 20339, 52472,
|
| 642 |
+
-1087, -21458, -68647, 52295, 15849, 40608, 15323, 25164, -29368,
|
| 643 |
+
10352, -7055, 7159, 21695, -5373, -54849, 101103, -24963, -10511,
|
| 644 |
+
33227, 7659, 41042, -69588, 26718, -20515, 6441, 38135, -63, 24088,
|
| 645 |
+
-35364, -12785, -18709, 47843, 48533, -48575, 17251, -19394, 32878,
|
| 646 |
+
-9010, -9050, 504, -12407, 28076, -3429, 25324, -4210, -26119, 752,
|
| 647 |
+
-29203, 28251, -11324, -32140, -3366, -25135, 18702, -31588, -7047,
|
| 648 |
+
-24267, 49987, -14975, -33169, 37744, -7720, -9035, 16964, -2807, -421,
|
| 649 |
+
14114, -17097, -13662, 40628, -12139, -9427, 5369, 17551, -13232, -16211,
|
| 650 |
+
9804, -7422, 2677, 28635, -8280, -4906, 2908, -22558, 5604, 12459, 8756,
|
| 651 |
+
-3980, -4745, -18525, 7913, 5970, -16457, 20230, -6247, -13812, 2505,
|
| 652 |
+
11899, 1409, -15094, 22540, -18863, 137, 11123, -4516, 2290, -8594, 12150,
|
| 653 |
+
-10380, 3005, 5235, -7350, 2535, -858], ZZ)
|
| 654 |
+
|
| 655 |
+
assert dup_mul(p1, p2, ZZ) == res
|
| 656 |
+
|
| 657 |
+
|
| 658 |
+
def test_dmp_mul():
|
| 659 |
+
assert dmp_mul([ZZ(5)], [ZZ(7)], 0, ZZ) == \
|
| 660 |
+
dup_mul([ZZ(5)], [ZZ(7)], ZZ)
|
| 661 |
+
assert dmp_mul([QQ(5, 7)], [QQ(3, 7)], 0, QQ) == \
|
| 662 |
+
dup_mul([QQ(5, 7)], [QQ(3, 7)], QQ)
|
| 663 |
+
|
| 664 |
+
assert dmp_mul([[[]]], [[[]]], 2, ZZ) == [[[]]]
|
| 665 |
+
assert dmp_mul([[[ZZ(1)]]], [[[]]], 2, ZZ) == [[[]]]
|
| 666 |
+
assert dmp_mul([[[]]], [[[ZZ(1)]]], 2, ZZ) == [[[]]]
|
| 667 |
+
assert dmp_mul([[[ZZ(2)]]], [[[ZZ(1)]]], 2, ZZ) == [[[ZZ(2)]]]
|
| 668 |
+
assert dmp_mul([[[ZZ(1)]]], [[[ZZ(2)]]], 2, ZZ) == [[[ZZ(2)]]]
|
| 669 |
+
|
| 670 |
+
assert dmp_mul([[[]]], [[[]]], 2, QQ) == [[[]]]
|
| 671 |
+
assert dmp_mul([[[QQ(1, 2)]]], [[[]]], 2, QQ) == [[[]]]
|
| 672 |
+
assert dmp_mul([[[]]], [[[QQ(1, 2)]]], 2, QQ) == [[[]]]
|
| 673 |
+
assert dmp_mul([[[QQ(2, 7)]]], [[[QQ(1, 3)]]], 2, QQ) == [[[QQ(2, 21)]]]
|
| 674 |
+
assert dmp_mul([[[QQ(1, 7)]]], [[[QQ(2, 3)]]], 2, QQ) == [[[QQ(2, 21)]]]
|
| 675 |
+
|
| 676 |
+
K = FF(6)
|
| 677 |
+
|
| 678 |
+
assert dmp_mul(
|
| 679 |
+
[[K(2)], [K(1)]], [[K(3)], [K(4)]], 1, K) == [[K(5)], [K(4)]]
|
| 680 |
+
|
| 681 |
+
|
| 682 |
+
def test_dup_sqr():
|
| 683 |
+
assert dup_sqr([], ZZ) == []
|
| 684 |
+
assert dup_sqr([ZZ(2)], ZZ) == [ZZ(4)]
|
| 685 |
+
assert dup_sqr([ZZ(1), ZZ(2)], ZZ) == [ZZ(1), ZZ(4), ZZ(4)]
|
| 686 |
+
|
| 687 |
+
assert dup_sqr([], QQ) == []
|
| 688 |
+
assert dup_sqr([QQ(2, 3)], QQ) == [QQ(4, 9)]
|
| 689 |
+
assert dup_sqr([QQ(1, 3), QQ(2, 3)], QQ) == [QQ(1, 9), QQ(4, 9), QQ(4, 9)]
|
| 690 |
+
|
| 691 |
+
f = dup_normal([2, 0, 0, 1, 7], ZZ)
|
| 692 |
+
|
| 693 |
+
assert dup_sqr(f, ZZ) == dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
|
| 694 |
+
|
| 695 |
+
K = FF(9)
|
| 696 |
+
|
| 697 |
+
assert dup_sqr([K(3), K(4)], K) == [K(6), K(7)]
|
| 698 |
+
|
| 699 |
+
|
| 700 |
+
def test_dmp_sqr():
|
| 701 |
+
assert dmp_sqr([ZZ(1), ZZ(2)], 0, ZZ) == \
|
| 702 |
+
dup_sqr([ZZ(1), ZZ(2)], ZZ)
|
| 703 |
+
|
| 704 |
+
assert dmp_sqr([[[]]], 2, ZZ) == [[[]]]
|
| 705 |
+
assert dmp_sqr([[[ZZ(2)]]], 2, ZZ) == [[[ZZ(4)]]]
|
| 706 |
+
|
| 707 |
+
assert dmp_sqr([[[]]], 2, QQ) == [[[]]]
|
| 708 |
+
assert dmp_sqr([[[QQ(2, 3)]]], 2, QQ) == [[[QQ(4, 9)]]]
|
| 709 |
+
|
| 710 |
+
K = FF(9)
|
| 711 |
+
|
| 712 |
+
assert dmp_sqr([[K(3)], [K(4)]], 1, K) == [[K(6)], [K(7)]]
|
| 713 |
+
|
| 714 |
+
|
| 715 |
+
def test_dup_pow():
|
| 716 |
+
assert dup_pow([], 0, ZZ) == [ZZ(1)]
|
| 717 |
+
assert dup_pow([], 0, QQ) == [QQ(1)]
|
| 718 |
+
|
| 719 |
+
assert dup_pow([], 1, ZZ) == []
|
| 720 |
+
assert dup_pow([], 7, ZZ) == []
|
| 721 |
+
|
| 722 |
+
assert dup_pow([ZZ(1)], 0, ZZ) == [ZZ(1)]
|
| 723 |
+
assert dup_pow([ZZ(1)], 1, ZZ) == [ZZ(1)]
|
| 724 |
+
assert dup_pow([ZZ(1)], 7, ZZ) == [ZZ(1)]
|
| 725 |
+
|
| 726 |
+
assert dup_pow([ZZ(3)], 0, ZZ) == [ZZ(1)]
|
| 727 |
+
assert dup_pow([ZZ(3)], 1, ZZ) == [ZZ(3)]
|
| 728 |
+
assert dup_pow([ZZ(3)], 7, ZZ) == [ZZ(2187)]
|
| 729 |
+
|
| 730 |
+
assert dup_pow([QQ(1, 1)], 0, QQ) == [QQ(1, 1)]
|
| 731 |
+
assert dup_pow([QQ(1, 1)], 1, QQ) == [QQ(1, 1)]
|
| 732 |
+
assert dup_pow([QQ(1, 1)], 7, QQ) == [QQ(1, 1)]
|
| 733 |
+
|
| 734 |
+
assert dup_pow([QQ(3, 7)], 0, QQ) == [QQ(1, 1)]
|
| 735 |
+
assert dup_pow([QQ(3, 7)], 1, QQ) == [QQ(3, 7)]
|
| 736 |
+
assert dup_pow([QQ(3, 7)], 7, QQ) == [QQ(2187, 823543)]
|
| 737 |
+
|
| 738 |
+
f = dup_normal([2, 0, 0, 1, 7], ZZ)
|
| 739 |
+
|
| 740 |
+
assert dup_pow(f, 0, ZZ) == dup_normal([1], ZZ)
|
| 741 |
+
assert dup_pow(f, 1, ZZ) == dup_normal([2, 0, 0, 1, 7], ZZ)
|
| 742 |
+
assert dup_pow(f, 2, ZZ) == dup_normal([4, 0, 0, 4, 28, 0, 1, 14, 49], ZZ)
|
| 743 |
+
assert dup_pow(f, 3, ZZ) == dup_normal(
|
| 744 |
+
[8, 0, 0, 12, 84, 0, 6, 84, 294, 1, 21, 147, 343], ZZ)
|
| 745 |
+
|
| 746 |
+
|
| 747 |
+
def test_dmp_pow():
|
| 748 |
+
assert dmp_pow([[]], 0, 1, ZZ) == [[ZZ(1)]]
|
| 749 |
+
assert dmp_pow([[]], 0, 1, QQ) == [[QQ(1)]]
|
| 750 |
+
|
| 751 |
+
assert dmp_pow([[]], 1, 1, ZZ) == [[]]
|
| 752 |
+
assert dmp_pow([[]], 7, 1, ZZ) == [[]]
|
| 753 |
+
|
| 754 |
+
assert dmp_pow([[ZZ(1)]], 0, 1, ZZ) == [[ZZ(1)]]
|
| 755 |
+
assert dmp_pow([[ZZ(1)]], 1, 1, ZZ) == [[ZZ(1)]]
|
| 756 |
+
assert dmp_pow([[ZZ(1)]], 7, 1, ZZ) == [[ZZ(1)]]
|
| 757 |
+
|
| 758 |
+
assert dmp_pow([[QQ(3, 7)]], 0, 1, QQ) == [[QQ(1, 1)]]
|
| 759 |
+
assert dmp_pow([[QQ(3, 7)]], 1, 1, QQ) == [[QQ(3, 7)]]
|
| 760 |
+
assert dmp_pow([[QQ(3, 7)]], 7, 1, QQ) == [[QQ(2187, 823543)]]
|
| 761 |
+
|
| 762 |
+
f = dup_normal([2, 0, 0, 1, 7], ZZ)
|
| 763 |
+
|
| 764 |
+
assert dmp_pow(f, 2, 0, ZZ) == dup_pow(f, 2, ZZ)
|
| 765 |
+
|
| 766 |
+
|
| 767 |
+
def test_dup_pdiv():
|
| 768 |
+
f = dup_normal([3, 1, 1, 5], ZZ)
|
| 769 |
+
g = dup_normal([5, -3, 1], ZZ)
|
| 770 |
+
|
| 771 |
+
q = dup_normal([15, 14], ZZ)
|
| 772 |
+
r = dup_normal([52, 111], ZZ)
|
| 773 |
+
|
| 774 |
+
assert dup_pdiv(f, g, ZZ) == (q, r)
|
| 775 |
+
assert dup_pquo(f, g, ZZ) == q
|
| 776 |
+
assert dup_prem(f, g, ZZ) == r
|
| 777 |
+
|
| 778 |
+
raises(ExactQuotientFailed, lambda: dup_pexquo(f, g, ZZ))
|
| 779 |
+
|
| 780 |
+
f = dup_normal([3, 1, 1, 5], QQ)
|
| 781 |
+
g = dup_normal([5, -3, 1], QQ)
|
| 782 |
+
|
| 783 |
+
q = dup_normal([15, 14], QQ)
|
| 784 |
+
r = dup_normal([52, 111], QQ)
|
| 785 |
+
|
| 786 |
+
assert dup_pdiv(f, g, QQ) == (q, r)
|
| 787 |
+
assert dup_pquo(f, g, QQ) == q
|
| 788 |
+
assert dup_prem(f, g, QQ) == r
|
| 789 |
+
|
| 790 |
+
raises(ExactQuotientFailed, lambda: dup_pexquo(f, g, QQ))
|
| 791 |
+
|
| 792 |
+
|
| 793 |
+
def test_dmp_pdiv():
|
| 794 |
+
f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
|
| 795 |
+
g = dmp_normal([[1], [-1, 0]], 1, ZZ)
|
| 796 |
+
|
| 797 |
+
q = dmp_normal([[1], [1, 0]], 1, ZZ)
|
| 798 |
+
r = dmp_normal([[2, 0, 0]], 1, ZZ)
|
| 799 |
+
|
| 800 |
+
assert dmp_pdiv(f, g, 1, ZZ) == (q, r)
|
| 801 |
+
assert dmp_pquo(f, g, 1, ZZ) == q
|
| 802 |
+
assert dmp_prem(f, g, 1, ZZ) == r
|
| 803 |
+
|
| 804 |
+
raises(ExactQuotientFailed, lambda: dmp_pexquo(f, g, 1, ZZ))
|
| 805 |
+
|
| 806 |
+
f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
|
| 807 |
+
g = dmp_normal([[2], [-2, 0]], 1, ZZ)
|
| 808 |
+
|
| 809 |
+
q = dmp_normal([[2], [2, 0]], 1, ZZ)
|
| 810 |
+
r = dmp_normal([[8, 0, 0]], 1, ZZ)
|
| 811 |
+
|
| 812 |
+
assert dmp_pdiv(f, g, 1, ZZ) == (q, r)
|
| 813 |
+
assert dmp_pquo(f, g, 1, ZZ) == q
|
| 814 |
+
assert dmp_prem(f, g, 1, ZZ) == r
|
| 815 |
+
|
| 816 |
+
raises(ExactQuotientFailed, lambda: dmp_pexquo(f, g, 1, ZZ))
|
| 817 |
+
|
| 818 |
+
|
| 819 |
+
def test_dup_rr_div():
|
| 820 |
+
raises(ZeroDivisionError, lambda: dup_rr_div([1, 2, 3], [], ZZ))
|
| 821 |
+
|
| 822 |
+
f = dup_normal([3, 1, 1, 5], ZZ)
|
| 823 |
+
g = dup_normal([5, -3, 1], ZZ)
|
| 824 |
+
|
| 825 |
+
q, r = [], f
|
| 826 |
+
|
| 827 |
+
assert dup_rr_div(f, g, ZZ) == (q, r)
|
| 828 |
+
|
| 829 |
+
|
| 830 |
+
def test_dmp_rr_div():
|
| 831 |
+
raises(ZeroDivisionError, lambda: dmp_rr_div([[1, 2], [3]], [[]], 1, ZZ))
|
| 832 |
+
|
| 833 |
+
f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
|
| 834 |
+
g = dmp_normal([[1], [-1, 0]], 1, ZZ)
|
| 835 |
+
|
| 836 |
+
q = dmp_normal([[1], [1, 0]], 1, ZZ)
|
| 837 |
+
r = dmp_normal([[2, 0, 0]], 1, ZZ)
|
| 838 |
+
|
| 839 |
+
assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
|
| 840 |
+
|
| 841 |
+
f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
|
| 842 |
+
g = dmp_normal([[-1], [1, 0]], 1, ZZ)
|
| 843 |
+
|
| 844 |
+
q = dmp_normal([[-1], [-1, 0]], 1, ZZ)
|
| 845 |
+
r = dmp_normal([[2, 0, 0]], 1, ZZ)
|
| 846 |
+
|
| 847 |
+
assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
|
| 848 |
+
|
| 849 |
+
f = dmp_normal([[1], [], [1, 0, 0]], 1, ZZ)
|
| 850 |
+
g = dmp_normal([[2], [-2, 0]], 1, ZZ)
|
| 851 |
+
|
| 852 |
+
q, r = [[]], f
|
| 853 |
+
|
| 854 |
+
assert dmp_rr_div(f, g, 1, ZZ) == (q, r)
|
| 855 |
+
|
| 856 |
+
|
| 857 |
+
def test_dup_ff_div():
|
| 858 |
+
raises(ZeroDivisionError, lambda: dup_ff_div([1, 2, 3], [], QQ))
|
| 859 |
+
|
| 860 |
+
f = dup_normal([3, 1, 1, 5], QQ)
|
| 861 |
+
g = dup_normal([5, -3, 1], QQ)
|
| 862 |
+
|
| 863 |
+
q = [QQ(3, 5), QQ(14, 25)]
|
| 864 |
+
r = [QQ(52, 25), QQ(111, 25)]
|
| 865 |
+
|
| 866 |
+
assert dup_ff_div(f, g, QQ) == (q, r)
|
| 867 |
+
|
| 868 |
+
def test_dup_ff_div_gmpy2():
|
| 869 |
+
if GROUND_TYPES != 'gmpy2':
|
| 870 |
+
return
|
| 871 |
+
|
| 872 |
+
from gmpy2 import mpq
|
| 873 |
+
from sympy.polys.domains import GMPYRationalField
|
| 874 |
+
K = GMPYRationalField()
|
| 875 |
+
|
| 876 |
+
f = [mpq(1,3), mpq(3,2)]
|
| 877 |
+
g = [mpq(2,1)]
|
| 878 |
+
assert dmp_ff_div(f, g, 0, K) == ([mpq(1,6), mpq(3,4)], [])
|
| 879 |
+
|
| 880 |
+
f = [mpq(1,2), mpq(1,3), mpq(1,4), mpq(1,5)]
|
| 881 |
+
g = [mpq(-1,1), mpq(1,1), mpq(-1,1)]
|
| 882 |
+
assert dmp_ff_div(f, g, 0, K) == ([mpq(-1,2), mpq(-5,6)], [mpq(7,12), mpq(-19,30)])
|
| 883 |
+
|
| 884 |
+
def test_dmp_ff_div():
|
| 885 |
+
raises(ZeroDivisionError, lambda: dmp_ff_div([[1, 2], [3]], [[]], 1, QQ))
|
| 886 |
+
|
| 887 |
+
f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
|
| 888 |
+
g = dmp_normal([[1], [-1, 0]], 1, QQ)
|
| 889 |
+
|
| 890 |
+
q = [[QQ(1, 1)], [QQ(1, 1), QQ(0, 1)]]
|
| 891 |
+
r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
|
| 892 |
+
|
| 893 |
+
assert dmp_ff_div(f, g, 1, QQ) == (q, r)
|
| 894 |
+
|
| 895 |
+
f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
|
| 896 |
+
g = dmp_normal([[-1], [1, 0]], 1, QQ)
|
| 897 |
+
|
| 898 |
+
q = [[QQ(-1, 1)], [QQ(-1, 1), QQ(0, 1)]]
|
| 899 |
+
r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
|
| 900 |
+
|
| 901 |
+
assert dmp_ff_div(f, g, 1, QQ) == (q, r)
|
| 902 |
+
|
| 903 |
+
f = dmp_normal([[1], [], [1, 0, 0]], 1, QQ)
|
| 904 |
+
g = dmp_normal([[2], [-2, 0]], 1, QQ)
|
| 905 |
+
|
| 906 |
+
q = [[QQ(1, 2)], [QQ(1, 2), QQ(0, 1)]]
|
| 907 |
+
r = [[QQ(2, 1), QQ(0, 1), QQ(0, 1)]]
|
| 908 |
+
|
| 909 |
+
assert dmp_ff_div(f, g, 1, QQ) == (q, r)
|
| 910 |
+
|
| 911 |
+
|
| 912 |
+
def test_dup_div():
|
| 913 |
+
f, g, q, r = [5, 4, 3, 2, 1], [1, 2, 3], [5, -6, 0], [20, 1]
|
| 914 |
+
|
| 915 |
+
assert dup_div(f, g, ZZ) == (q, r)
|
| 916 |
+
assert dup_quo(f, g, ZZ) == q
|
| 917 |
+
assert dup_rem(f, g, ZZ) == r
|
| 918 |
+
|
| 919 |
+
raises(ExactQuotientFailed, lambda: dup_exquo(f, g, ZZ))
|
| 920 |
+
|
| 921 |
+
f, g, q, r = [5, 4, 3, 2, 1, 0], [1, 2, 0, 0, 9], [5, -6], [15, 2, -44, 54]
|
| 922 |
+
|
| 923 |
+
assert dup_div(f, g, ZZ) == (q, r)
|
| 924 |
+
assert dup_quo(f, g, ZZ) == q
|
| 925 |
+
assert dup_rem(f, g, ZZ) == r
|
| 926 |
+
|
| 927 |
+
raises(ExactQuotientFailed, lambda: dup_exquo(f, g, ZZ))
|
| 928 |
+
|
| 929 |
+
|
| 930 |
+
def test_dmp_div():
|
| 931 |
+
f, g, q, r = [5, 4, 3, 2, 1], [1, 2, 3], [5, -6, 0], [20, 1]
|
| 932 |
+
|
| 933 |
+
assert dmp_div(f, g, 0, ZZ) == (q, r)
|
| 934 |
+
assert dmp_quo(f, g, 0, ZZ) == q
|
| 935 |
+
assert dmp_rem(f, g, 0, ZZ) == r
|
| 936 |
+
|
| 937 |
+
raises(ExactQuotientFailed, lambda: dmp_exquo(f, g, 0, ZZ))
|
| 938 |
+
|
| 939 |
+
f, g, q, r = [[[1]]], [[[2]], [1]], [[[]]], [[[1]]]
|
| 940 |
+
|
| 941 |
+
assert dmp_div(f, g, 2, ZZ) == (q, r)
|
| 942 |
+
assert dmp_quo(f, g, 2, ZZ) == q
|
| 943 |
+
assert dmp_rem(f, g, 2, ZZ) == r
|
| 944 |
+
|
| 945 |
+
raises(ExactQuotientFailed, lambda: dmp_exquo(f, g, 2, ZZ))
|
| 946 |
+
|
| 947 |
+
|
| 948 |
+
def test_dup_max_norm():
|
| 949 |
+
assert dup_max_norm([], ZZ) == 0
|
| 950 |
+
assert dup_max_norm([1], ZZ) == 1
|
| 951 |
+
|
| 952 |
+
assert dup_max_norm([1, 4, 2, 3], ZZ) == 4
|
| 953 |
+
|
| 954 |
+
|
| 955 |
+
def test_dmp_max_norm():
|
| 956 |
+
assert dmp_max_norm([[[]]], 2, ZZ) == 0
|
| 957 |
+
assert dmp_max_norm([[[1]]], 2, ZZ) == 1
|
| 958 |
+
|
| 959 |
+
assert dmp_max_norm(f_0, 2, ZZ) == 6
|
| 960 |
+
|
| 961 |
+
|
| 962 |
+
def test_dup_l1_norm():
|
| 963 |
+
assert dup_l1_norm([], ZZ) == 0
|
| 964 |
+
assert dup_l1_norm([1], ZZ) == 1
|
| 965 |
+
assert dup_l1_norm([1, 4, 2, 3], ZZ) == 10
|
| 966 |
+
|
| 967 |
+
|
| 968 |
+
def test_dmp_l1_norm():
|
| 969 |
+
assert dmp_l1_norm([[[]]], 2, ZZ) == 0
|
| 970 |
+
assert dmp_l1_norm([[[1]]], 2, ZZ) == 1
|
| 971 |
+
|
| 972 |
+
assert dmp_l1_norm(f_0, 2, ZZ) == 31
|
| 973 |
+
|
| 974 |
+
|
| 975 |
+
def test_dup_l2_norm_squared():
|
| 976 |
+
assert dup_l2_norm_squared([], ZZ) == 0
|
| 977 |
+
assert dup_l2_norm_squared([1], ZZ) == 1
|
| 978 |
+
assert dup_l2_norm_squared([1, 4, 2, 3], ZZ) == 30
|
| 979 |
+
|
| 980 |
+
|
| 981 |
+
def test_dmp_l2_norm_squared():
|
| 982 |
+
assert dmp_l2_norm_squared([[[]]], 2, ZZ) == 0
|
| 983 |
+
assert dmp_l2_norm_squared([[[1]]], 2, ZZ) == 1
|
| 984 |
+
assert dmp_l2_norm_squared(f_0, 2, ZZ) == 111
|
| 985 |
+
|
| 986 |
+
|
| 987 |
+
def test_dup_expand():
|
| 988 |
+
assert dup_expand((), ZZ) == [1]
|
| 989 |
+
assert dup_expand(([1, 2, 3], [1, 2], [7, 5, 4, 3]), ZZ) == \
|
| 990 |
+
dup_mul([1, 2, 3], dup_mul([1, 2], [7, 5, 4, 3], ZZ), ZZ)
|
| 991 |
+
|
| 992 |
+
|
| 993 |
+
def test_dmp_expand():
|
| 994 |
+
assert dmp_expand((), 1, ZZ) == [[1]]
|
| 995 |
+
assert dmp_expand(([[1], [2], [3]], [[1], [2]], [[7], [5], [4], [3]]), 1, ZZ) == \
|
| 996 |
+
dmp_mul([[1], [2], [3]], dmp_mul([[1], [2]], [[7], [5], [
|
| 997 |
+
4], [3]], 1, ZZ), 1, ZZ)
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_dispersion.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.core import Symbol, S, oo
|
| 2 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 3 |
+
from sympy.polys import poly
|
| 4 |
+
from sympy.polys.dispersion import dispersion, dispersionset
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def test_dispersion():
|
| 8 |
+
x = Symbol("x")
|
| 9 |
+
a = Symbol("a")
|
| 10 |
+
|
| 11 |
+
fp = poly(S.Zero, x)
|
| 12 |
+
assert sorted(dispersionset(fp)) == [0]
|
| 13 |
+
|
| 14 |
+
fp = poly(S(2), x)
|
| 15 |
+
assert sorted(dispersionset(fp)) == [0]
|
| 16 |
+
|
| 17 |
+
fp = poly(x + 1, x)
|
| 18 |
+
assert sorted(dispersionset(fp)) == [0]
|
| 19 |
+
assert dispersion(fp) == 0
|
| 20 |
+
|
| 21 |
+
fp = poly((x + 1)*(x + 2), x)
|
| 22 |
+
assert sorted(dispersionset(fp)) == [0, 1]
|
| 23 |
+
assert dispersion(fp) == 1
|
| 24 |
+
|
| 25 |
+
fp = poly(x*(x + 3), x)
|
| 26 |
+
assert sorted(dispersionset(fp)) == [0, 3]
|
| 27 |
+
assert dispersion(fp) == 3
|
| 28 |
+
|
| 29 |
+
fp = poly((x - 3)*(x + 3), x)
|
| 30 |
+
assert sorted(dispersionset(fp)) == [0, 6]
|
| 31 |
+
assert dispersion(fp) == 6
|
| 32 |
+
|
| 33 |
+
fp = poly(x**4 - 3*x**2 + 1, x)
|
| 34 |
+
gp = fp.shift(-3)
|
| 35 |
+
assert sorted(dispersionset(fp, gp)) == [2, 3, 4]
|
| 36 |
+
assert dispersion(fp, gp) == 4
|
| 37 |
+
assert sorted(dispersionset(gp, fp)) == []
|
| 38 |
+
assert dispersion(gp, fp) is -oo
|
| 39 |
+
|
| 40 |
+
fp = poly(x*(3*x**2+a)*(x-2536)*(x**3+a), x)
|
| 41 |
+
gp = fp.as_expr().subs(x, x-345).as_poly(x)
|
| 42 |
+
assert sorted(dispersionset(fp, gp)) == [345, 2881]
|
| 43 |
+
assert sorted(dispersionset(gp, fp)) == [2191]
|
| 44 |
+
|
| 45 |
+
gp = poly((x-2)**2*(x-3)**3*(x-5)**3, x)
|
| 46 |
+
assert sorted(dispersionset(gp)) == [0, 1, 2, 3]
|
| 47 |
+
assert sorted(dispersionset(gp, (gp+4)**2)) == [1, 2]
|
| 48 |
+
|
| 49 |
+
fp = poly(x*(x+2)*(x-1), x)
|
| 50 |
+
assert sorted(dispersionset(fp)) == [0, 1, 2, 3]
|
| 51 |
+
|
| 52 |
+
fp = poly(x**2 + sqrt(5)*x - 1, x, domain='QQ<sqrt(5)>')
|
| 53 |
+
gp = poly(x**2 + (2 + sqrt(5))*x + sqrt(5), x, domain='QQ<sqrt(5)>')
|
| 54 |
+
assert sorted(dispersionset(fp, gp)) == [2]
|
| 55 |
+
assert sorted(dispersionset(gp, fp)) == [1, 4]
|
| 56 |
+
|
| 57 |
+
# There are some difficulties if we compute over Z[a]
|
| 58 |
+
# and alpha happenes to lie in Z[a] instead of simply Z.
|
| 59 |
+
# Hence we can not decide if alpha is indeed integral
|
| 60 |
+
# in general.
|
| 61 |
+
|
| 62 |
+
fp = poly(4*x**4 + (4*a + 8)*x**3 + (a**2 + 6*a + 4)*x**2 + (a**2 + 2*a)*x, x)
|
| 63 |
+
assert sorted(dispersionset(fp)) == [0, 1]
|
| 64 |
+
|
| 65 |
+
# For any specific value of a, the dispersion is 3*a
|
| 66 |
+
# but the algorithm can not find this in general.
|
| 67 |
+
# This is the point where the resultant based Ansatz
|
| 68 |
+
# is superior to the current one.
|
| 69 |
+
fp = poly(a**2*x**3 + (a**3 + a**2 + a + 1)*x, x)
|
| 70 |
+
gp = fp.as_expr().subs(x, x - 3*a).as_poly(x)
|
| 71 |
+
assert sorted(dispersionset(fp, gp)) == []
|
| 72 |
+
|
| 73 |
+
fpa = fp.as_expr().subs(a, 2).as_poly(x)
|
| 74 |
+
gpa = gp.as_expr().subs(a, 2).as_poly(x)
|
| 75 |
+
assert sorted(dispersionset(fpa, gpa)) == [6]
|
| 76 |
+
|
| 77 |
+
# Work with Expr instead of Poly
|
| 78 |
+
f = (x + 1)*(x + 2)
|
| 79 |
+
assert sorted(dispersionset(f)) == [0, 1]
|
| 80 |
+
assert dispersion(f) == 1
|
| 81 |
+
|
| 82 |
+
f = x**4 - 3*x**2 + 1
|
| 83 |
+
g = x**4 - 12*x**3 + 51*x**2 - 90*x + 55
|
| 84 |
+
assert sorted(dispersionset(f, g)) == [2, 3, 4]
|
| 85 |
+
assert dispersion(f, g) == 4
|
| 86 |
+
|
| 87 |
+
# Work with Expr and specify a generator
|
| 88 |
+
f = (x + 1)*(x + 2)
|
| 89 |
+
assert sorted(dispersionset(f, None, x)) == [0, 1]
|
| 90 |
+
assert dispersion(f, None, x) == 1
|
| 91 |
+
|
| 92 |
+
f = x**4 - 3*x**2 + 1
|
| 93 |
+
g = x**4 - 12*x**3 + 51*x**2 - 90*x + 55
|
| 94 |
+
assert sorted(dispersionset(f, g, x)) == [2, 3, 4]
|
| 95 |
+
assert dispersion(f, g, x) == 4
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_distributedmodules.py
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for sparse distributed modules. """
|
| 2 |
+
|
| 3 |
+
from sympy.polys.distributedmodules import (
|
| 4 |
+
sdm_monomial_mul, sdm_monomial_deg, sdm_monomial_divides,
|
| 5 |
+
sdm_add, sdm_LM, sdm_LT, sdm_mul_term, sdm_zero, sdm_deg,
|
| 6 |
+
sdm_LC, sdm_from_dict,
|
| 7 |
+
sdm_spoly, sdm_ecart, sdm_nf_mora, sdm_groebner,
|
| 8 |
+
sdm_from_vector, sdm_to_vector, sdm_monomial_lcm
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
from sympy.polys.orderings import lex, grlex, InverseOrder
|
| 12 |
+
from sympy.polys.domains import QQ
|
| 13 |
+
|
| 14 |
+
from sympy.abc import x, y, z
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def test_sdm_monomial_mul():
|
| 18 |
+
assert sdm_monomial_mul((1, 1, 0), (1, 3)) == (1, 2, 3)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def test_sdm_monomial_deg():
|
| 22 |
+
assert sdm_monomial_deg((5, 2, 1)) == 3
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_sdm_monomial_lcm():
|
| 26 |
+
assert sdm_monomial_lcm((1, 2, 3), (1, 5, 0)) == (1, 5, 3)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_sdm_monomial_divides():
|
| 30 |
+
assert sdm_monomial_divides((1, 0, 0), (1, 0, 0)) is True
|
| 31 |
+
assert sdm_monomial_divides((1, 0, 0), (1, 2, 1)) is True
|
| 32 |
+
assert sdm_monomial_divides((5, 1, 1), (5, 2, 1)) is True
|
| 33 |
+
|
| 34 |
+
assert sdm_monomial_divides((1, 0, 0), (2, 0, 0)) is False
|
| 35 |
+
assert sdm_monomial_divides((1, 1, 0), (1, 0, 0)) is False
|
| 36 |
+
assert sdm_monomial_divides((5, 1, 2), (5, 0, 1)) is False
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
def test_sdm_LC():
|
| 40 |
+
assert sdm_LC([((1, 2, 3), QQ(5))], QQ) == QQ(5)
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def test_sdm_from_dict():
|
| 44 |
+
dic = {(1, 2, 1, 1): QQ(1), (1, 1, 2, 1): QQ(1), (1, 0, 2, 1): QQ(1),
|
| 45 |
+
(1, 0, 0, 3): QQ(1), (1, 1, 1, 0): QQ(1)}
|
| 46 |
+
assert sdm_from_dict(dic, grlex) == \
|
| 47 |
+
[((1, 2, 1, 1), QQ(1)), ((1, 1, 2, 1), QQ(1)),
|
| 48 |
+
((1, 0, 2, 1), QQ(1)), ((1, 0, 0, 3), QQ(1)), ((1, 1, 1, 0), QQ(1))]
|
| 49 |
+
|
| 50 |
+
# TODO test to_dict?
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def test_sdm_add():
|
| 54 |
+
assert sdm_add([((1, 1, 1), QQ(1))], [((2, 0, 0), QQ(1))], lex, QQ) == \
|
| 55 |
+
[((2, 0, 0), QQ(1)), ((1, 1, 1), QQ(1))]
|
| 56 |
+
assert sdm_add([((1, 1, 1), QQ(1))], [((1, 1, 1), QQ(-1))], lex, QQ) == []
|
| 57 |
+
assert sdm_add([((1, 0, 0), QQ(1))], [((1, 0, 0), QQ(2))], lex, QQ) == \
|
| 58 |
+
[((1, 0, 0), QQ(3))]
|
| 59 |
+
assert sdm_add([((1, 0, 1), QQ(1))], [((1, 1, 0), QQ(1))], lex, QQ) == \
|
| 60 |
+
[((1, 1, 0), QQ(1)), ((1, 0, 1), QQ(1))]
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def test_sdm_LM():
|
| 64 |
+
dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(1), (4, 0, 1): QQ(1)}
|
| 65 |
+
assert sdm_LM(sdm_from_dict(dic, lex)) == (4, 0, 1)
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_sdm_LT():
|
| 69 |
+
dic = {(1, 2, 3): QQ(1), (4, 0, 0): QQ(2), (4, 0, 1): QQ(3)}
|
| 70 |
+
assert sdm_LT(sdm_from_dict(dic, lex)) == ((4, 0, 1), QQ(3))
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_sdm_mul_term():
|
| 74 |
+
assert sdm_mul_term([((1, 0, 0), QQ(1))], ((0, 0), QQ(0)), lex, QQ) == []
|
| 75 |
+
assert sdm_mul_term([], ((1, 0), QQ(1)), lex, QQ) == []
|
| 76 |
+
assert sdm_mul_term([((1, 0, 0), QQ(1))], ((1, 0), QQ(1)), lex, QQ) == \
|
| 77 |
+
[((1, 1, 0), QQ(1))]
|
| 78 |
+
f = [((2, 0, 1), QQ(4)), ((1, 1, 0), QQ(3))]
|
| 79 |
+
assert sdm_mul_term(f, ((1, 1), QQ(2)), lex, QQ) == \
|
| 80 |
+
[((2, 1, 2), QQ(8)), ((1, 2, 1), QQ(6))]
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def test_sdm_zero():
|
| 84 |
+
assert sdm_zero() == []
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
def test_sdm_deg():
|
| 88 |
+
assert sdm_deg([((1, 2, 3), 1), ((10, 0, 1), 1), ((2, 3, 4), 4)]) == 7
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def test_sdm_spoly():
|
| 92 |
+
f = [((2, 1, 1), QQ(1)), ((1, 0, 1), QQ(1))]
|
| 93 |
+
g = [((2, 3, 0), QQ(1))]
|
| 94 |
+
h = [((1, 2, 3), QQ(1))]
|
| 95 |
+
assert sdm_spoly(f, h, lex, QQ) == []
|
| 96 |
+
assert sdm_spoly(f, g, lex, QQ) == [((1, 2, 1), QQ(1))]
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def test_sdm_ecart():
|
| 100 |
+
assert sdm_ecart([((1, 2, 3), 1), ((1, 0, 1), 1)]) == 0
|
| 101 |
+
assert sdm_ecart([((2, 2, 1), 1), ((1, 5, 1), 1)]) == 3
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def test_sdm_nf_mora():
|
| 105 |
+
f = sdm_from_dict({(1, 2, 1, 1): QQ(1), (1, 1, 2, 1): QQ(1),
|
| 106 |
+
(1, 0, 2, 1): QQ(1), (1, 0, 0, 3): QQ(1), (1, 1, 1, 0): QQ(1)},
|
| 107 |
+
grlex)
|
| 108 |
+
f1 = sdm_from_dict({(1, 1, 1, 0): QQ(1), (1, 0, 2, 0): QQ(1),
|
| 109 |
+
(1, 0, 0, 0): QQ(-1)}, grlex)
|
| 110 |
+
f2 = sdm_from_dict({(1, 1, 1, 0): QQ(1)}, grlex)
|
| 111 |
+
(id0, id1, id2) = [sdm_from_dict({(i, 0, 0, 0): QQ(1)}, grlex)
|
| 112 |
+
for i in range(3)]
|
| 113 |
+
|
| 114 |
+
assert sdm_nf_mora(f, [f1, f2], grlex, QQ, phantom=(id0, [id1, id2])) == \
|
| 115 |
+
([((1, 0, 2, 1), QQ(1)), ((1, 0, 0, 3), QQ(1)), ((1, 1, 1, 0), QQ(1)),
|
| 116 |
+
((1, 1, 0, 1), QQ(1))],
|
| 117 |
+
[((1, 1, 0, 1), QQ(-1)), ((0, 0, 0, 0), QQ(1))])
|
| 118 |
+
assert sdm_nf_mora(f, [f2, f1], grlex, QQ, phantom=(id0, [id2, id1])) == \
|
| 119 |
+
([((1, 0, 2, 1), QQ(1)), ((1, 0, 0, 3), QQ(1)), ((1, 1, 1, 0), QQ(1))],
|
| 120 |
+
[((2, 1, 0, 1), QQ(-1)), ((2, 0, 1, 1), QQ(-1)), ((0, 0, 0, 0), QQ(1))])
|
| 121 |
+
|
| 122 |
+
f = sdm_from_vector([x*z, y**2 + y*z - z, y], lex, QQ, gens=[x, y, z])
|
| 123 |
+
f1 = sdm_from_vector([x, y, 1], lex, QQ, gens=[x, y, z])
|
| 124 |
+
f2 = sdm_from_vector([x*y, z, z**2], lex, QQ, gens=[x, y, z])
|
| 125 |
+
assert sdm_nf_mora(f, [f1, f2], lex, QQ) == \
|
| 126 |
+
sdm_nf_mora(f, [f2, f1], lex, QQ) == \
|
| 127 |
+
[((1, 0, 1, 1), QQ(1)), ((1, 0, 0, 1), QQ(-1)), ((0, 1, 1, 0), QQ(-1)),
|
| 128 |
+
((0, 1, 0, 1), QQ(1))]
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def test_conversion():
|
| 132 |
+
f = [x**2 + y**2, 2*z]
|
| 133 |
+
g = [((1, 0, 0, 1), QQ(2)), ((0, 2, 0, 0), QQ(1)), ((0, 0, 2, 0), QQ(1))]
|
| 134 |
+
assert sdm_to_vector(g, [x, y, z], QQ) == f
|
| 135 |
+
assert sdm_from_vector(f, lex, QQ) == g
|
| 136 |
+
assert sdm_from_vector(
|
| 137 |
+
[x, 1], lex, QQ) == [((1, 0), QQ(1)), ((0, 1), QQ(1))]
|
| 138 |
+
assert sdm_to_vector([((1, 1, 0, 0), 1)], [x, y, z], QQ, n=3) == [0, x, 0]
|
| 139 |
+
assert sdm_from_vector([0, 0], lex, QQ, gens=[x, y]) == sdm_zero()
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def test_nontrivial():
|
| 143 |
+
gens = [x, y, z]
|
| 144 |
+
|
| 145 |
+
def contains(I, f):
|
| 146 |
+
S = [sdm_from_vector([g], lex, QQ, gens=gens) for g in I]
|
| 147 |
+
G = sdm_groebner(S, sdm_nf_mora, lex, QQ)
|
| 148 |
+
return sdm_nf_mora(sdm_from_vector([f], lex, QQ, gens=gens),
|
| 149 |
+
G, lex, QQ) == sdm_zero()
|
| 150 |
+
|
| 151 |
+
assert contains([x, y], x)
|
| 152 |
+
assert contains([x, y], x + y)
|
| 153 |
+
assert not contains([x, y], 1)
|
| 154 |
+
assert not contains([x, y], z)
|
| 155 |
+
assert contains([x**2 + y, x**2 + x], x - y)
|
| 156 |
+
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
|
| 157 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**3)
|
| 158 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4)
|
| 159 |
+
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y**2)
|
| 160 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x**4 + y**3 + 2*z*y*x)
|
| 161 |
+
assert contains([x + y + z, x*y + x*z + y*z, x*y*z], x*y*z)
|
| 162 |
+
assert contains([x, 1 + x + y, 5 - 7*y], 1)
|
| 163 |
+
assert contains(
|
| 164 |
+
[x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
|
| 165 |
+
x**3)
|
| 166 |
+
assert not contains(
|
| 167 |
+
[x**3 + y**3, y**3 + z**3, z**3 + x**3, x**2*y + x**2*z + y**2*z],
|
| 168 |
+
x**2 + y**2)
|
| 169 |
+
|
| 170 |
+
# compare local order
|
| 171 |
+
assert not contains([x*(1 + x + y), y*(1 + z)], x)
|
| 172 |
+
assert not contains([x*(1 + x + y), y*(1 + z)], x + y)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def test_local():
|
| 176 |
+
igrlex = InverseOrder(grlex)
|
| 177 |
+
gens = [x, y, z]
|
| 178 |
+
|
| 179 |
+
def contains(I, f):
|
| 180 |
+
S = [sdm_from_vector([g], igrlex, QQ, gens=gens) for g in I]
|
| 181 |
+
G = sdm_groebner(S, sdm_nf_mora, igrlex, QQ)
|
| 182 |
+
return sdm_nf_mora(sdm_from_vector([f], lex, QQ, gens=gens),
|
| 183 |
+
G, lex, QQ) == sdm_zero()
|
| 184 |
+
assert contains([x, y], x)
|
| 185 |
+
assert contains([x, y], x + y)
|
| 186 |
+
assert not contains([x, y], 1)
|
| 187 |
+
assert not contains([x, y], z)
|
| 188 |
+
assert contains([x**2 + y, x**2 + x], x - y)
|
| 189 |
+
assert not contains([x + y + z, x*y + x*z + y*z, x*y*z], x**2)
|
| 190 |
+
assert contains([x*(1 + x + y), y*(1 + z)], x)
|
| 191 |
+
assert contains([x*(1 + x + y), y*(1 + z)], x + y)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def test_uncovered_line():
|
| 195 |
+
gens = [x, y]
|
| 196 |
+
f1 = sdm_zero()
|
| 197 |
+
f2 = sdm_from_vector([x, 0], lex, QQ, gens=gens)
|
| 198 |
+
f3 = sdm_from_vector([0, y], lex, QQ, gens=gens)
|
| 199 |
+
|
| 200 |
+
assert sdm_spoly(f1, f2, lex, QQ) == sdm_zero()
|
| 201 |
+
assert sdm_spoly(f3, f2, lex, QQ) == sdm_zero()
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
def test_chain_criterion():
|
| 205 |
+
gens = [x]
|
| 206 |
+
f1 = sdm_from_vector([1, x], grlex, QQ, gens=gens)
|
| 207 |
+
f2 = sdm_from_vector([0, x - 2], grlex, QQ, gens=gens)
|
| 208 |
+
assert len(sdm_groebner([f1, f2], sdm_nf_mora, grlex, QQ)) == 2
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_euclidtools.py
ADDED
|
@@ -0,0 +1,712 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for Euclidean algorithms, GCDs, LCMs and polynomial remainder sequences. """
|
| 2 |
+
|
| 3 |
+
from sympy.polys.rings import ring
|
| 4 |
+
from sympy.polys.domains import ZZ, QQ, RR
|
| 5 |
+
|
| 6 |
+
from sympy.polys.specialpolys import (
|
| 7 |
+
f_polys,
|
| 8 |
+
dmp_fateman_poly_F_1,
|
| 9 |
+
dmp_fateman_poly_F_2,
|
| 10 |
+
dmp_fateman_poly_F_3)
|
| 11 |
+
|
| 12 |
+
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = f_polys()
|
| 13 |
+
|
| 14 |
+
def test_dup_gcdex():
|
| 15 |
+
R, x = ring("x", QQ)
|
| 16 |
+
|
| 17 |
+
f = x**4 - 2*x**3 - 6*x**2 + 12*x + 15
|
| 18 |
+
g = x**3 + x**2 - 4*x - 4
|
| 19 |
+
|
| 20 |
+
s = -QQ(1,5)*x + QQ(3,5)
|
| 21 |
+
t = QQ(1,5)*x**2 - QQ(6,5)*x + 2
|
| 22 |
+
h = x + 1
|
| 23 |
+
|
| 24 |
+
assert R.dup_half_gcdex(f, g) == (s, h)
|
| 25 |
+
assert R.dup_gcdex(f, g) == (s, t, h)
|
| 26 |
+
|
| 27 |
+
f = x**4 + 4*x**3 - x + 1
|
| 28 |
+
g = x**3 - x + 1
|
| 29 |
+
|
| 30 |
+
s, t, h = R.dup_gcdex(f, g)
|
| 31 |
+
S, T, H = R.dup_gcdex(g, f)
|
| 32 |
+
|
| 33 |
+
assert R.dup_add(R.dup_mul(s, f),
|
| 34 |
+
R.dup_mul(t, g)) == h
|
| 35 |
+
assert R.dup_add(R.dup_mul(S, g),
|
| 36 |
+
R.dup_mul(T, f)) == H
|
| 37 |
+
|
| 38 |
+
f = 2*x
|
| 39 |
+
g = x**2 - 16
|
| 40 |
+
|
| 41 |
+
s = QQ(1,32)*x
|
| 42 |
+
t = -QQ(1,16)
|
| 43 |
+
h = 1
|
| 44 |
+
|
| 45 |
+
assert R.dup_half_gcdex(f, g) == (s, h)
|
| 46 |
+
assert R.dup_gcdex(f, g) == (s, t, h)
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def test_dup_invert():
|
| 50 |
+
R, x = ring("x", QQ)
|
| 51 |
+
assert R.dup_invert(2*x, x**2 - 16) == QQ(1,32)*x
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def test_dup_euclidean_prs():
|
| 55 |
+
R, x = ring("x", QQ)
|
| 56 |
+
|
| 57 |
+
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
|
| 58 |
+
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
|
| 59 |
+
|
| 60 |
+
assert R.dup_euclidean_prs(f, g) == [
|
| 61 |
+
f,
|
| 62 |
+
g,
|
| 63 |
+
-QQ(5,9)*x**4 + QQ(1,9)*x**2 - QQ(1,3),
|
| 64 |
+
-QQ(117,25)*x**2 - 9*x + QQ(441,25),
|
| 65 |
+
QQ(233150,19773)*x - QQ(102500,6591),
|
| 66 |
+
-QQ(1288744821,543589225)]
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def test_dup_primitive_prs():
|
| 70 |
+
R, x = ring("x", ZZ)
|
| 71 |
+
|
| 72 |
+
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
|
| 73 |
+
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
|
| 74 |
+
|
| 75 |
+
assert R.dup_primitive_prs(f, g) == [
|
| 76 |
+
f,
|
| 77 |
+
g,
|
| 78 |
+
-5*x**4 + x**2 - 3,
|
| 79 |
+
13*x**2 + 25*x - 49,
|
| 80 |
+
4663*x - 6150,
|
| 81 |
+
1]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def test_dup_subresultants():
|
| 85 |
+
R, x = ring("x", ZZ)
|
| 86 |
+
|
| 87 |
+
assert R.dup_resultant(0, 0) == 0
|
| 88 |
+
|
| 89 |
+
assert R.dup_resultant(1, 0) == 0
|
| 90 |
+
assert R.dup_resultant(0, 1) == 0
|
| 91 |
+
|
| 92 |
+
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
|
| 93 |
+
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
|
| 94 |
+
|
| 95 |
+
a = 15*x**4 - 3*x**2 + 9
|
| 96 |
+
b = 65*x**2 + 125*x - 245
|
| 97 |
+
c = 9326*x - 12300
|
| 98 |
+
d = 260708
|
| 99 |
+
|
| 100 |
+
assert R.dup_subresultants(f, g) == [f, g, a, b, c, d]
|
| 101 |
+
assert R.dup_resultant(f, g) == R.dup_LC(d)
|
| 102 |
+
|
| 103 |
+
f = x**2 - 2*x + 1
|
| 104 |
+
g = x**2 - 1
|
| 105 |
+
|
| 106 |
+
a = 2*x - 2
|
| 107 |
+
|
| 108 |
+
assert R.dup_subresultants(f, g) == [f, g, a]
|
| 109 |
+
assert R.dup_resultant(f, g) == 0
|
| 110 |
+
|
| 111 |
+
f = x**2 + 1
|
| 112 |
+
g = x**2 - 1
|
| 113 |
+
|
| 114 |
+
a = -2
|
| 115 |
+
|
| 116 |
+
assert R.dup_subresultants(f, g) == [f, g, a]
|
| 117 |
+
assert R.dup_resultant(f, g) == 4
|
| 118 |
+
|
| 119 |
+
f = x**2 - 1
|
| 120 |
+
g = x**3 - x**2 + 2
|
| 121 |
+
|
| 122 |
+
assert R.dup_resultant(f, g) == 0
|
| 123 |
+
|
| 124 |
+
f = 3*x**3 - x
|
| 125 |
+
g = 5*x**2 + 1
|
| 126 |
+
|
| 127 |
+
assert R.dup_resultant(f, g) == 64
|
| 128 |
+
|
| 129 |
+
f = x**2 - 2*x + 7
|
| 130 |
+
g = x**3 - x + 5
|
| 131 |
+
|
| 132 |
+
assert R.dup_resultant(f, g) == 265
|
| 133 |
+
|
| 134 |
+
f = x**3 - 6*x**2 + 11*x - 6
|
| 135 |
+
g = x**3 - 15*x**2 + 74*x - 120
|
| 136 |
+
|
| 137 |
+
assert R.dup_resultant(f, g) == -8640
|
| 138 |
+
|
| 139 |
+
f = x**3 - 6*x**2 + 11*x - 6
|
| 140 |
+
g = x**3 - 10*x**2 + 29*x - 20
|
| 141 |
+
|
| 142 |
+
assert R.dup_resultant(f, g) == 0
|
| 143 |
+
|
| 144 |
+
f = x**3 - 1
|
| 145 |
+
g = x**3 + 2*x**2 + 2*x - 1
|
| 146 |
+
|
| 147 |
+
assert R.dup_resultant(f, g) == 16
|
| 148 |
+
|
| 149 |
+
f = x**8 - 2
|
| 150 |
+
g = x - 1
|
| 151 |
+
|
| 152 |
+
assert R.dup_resultant(f, g) == -1
|
| 153 |
+
|
| 154 |
+
|
| 155 |
+
def test_dmp_subresultants():
|
| 156 |
+
R, x, y = ring("x,y", ZZ)
|
| 157 |
+
|
| 158 |
+
assert R.dmp_resultant(0, 0) == 0
|
| 159 |
+
assert R.dmp_prs_resultant(0, 0)[0] == 0
|
| 160 |
+
assert R.dmp_zz_collins_resultant(0, 0) == 0
|
| 161 |
+
assert R.dmp_qq_collins_resultant(0, 0) == 0
|
| 162 |
+
|
| 163 |
+
assert R.dmp_resultant(1, 0) == 0
|
| 164 |
+
assert R.dmp_resultant(1, 0) == 0
|
| 165 |
+
assert R.dmp_resultant(1, 0) == 0
|
| 166 |
+
|
| 167 |
+
assert R.dmp_resultant(0, 1) == 0
|
| 168 |
+
assert R.dmp_prs_resultant(0, 1)[0] == 0
|
| 169 |
+
assert R.dmp_zz_collins_resultant(0, 1) == 0
|
| 170 |
+
assert R.dmp_qq_collins_resultant(0, 1) == 0
|
| 171 |
+
|
| 172 |
+
f = 3*x**2*y - y**3 - 4
|
| 173 |
+
g = x**2 + x*y**3 - 9
|
| 174 |
+
|
| 175 |
+
a = 3*x*y**4 + y**3 - 27*y + 4
|
| 176 |
+
b = -3*y**10 - 12*y**7 + y**6 - 54*y**4 + 8*y**3 + 729*y**2 - 216*y + 16
|
| 177 |
+
|
| 178 |
+
r = R.dmp_LC(b)
|
| 179 |
+
|
| 180 |
+
assert R.dmp_subresultants(f, g) == [f, g, a, b]
|
| 181 |
+
|
| 182 |
+
assert R.dmp_resultant(f, g) == r
|
| 183 |
+
assert R.dmp_prs_resultant(f, g)[0] == r
|
| 184 |
+
assert R.dmp_zz_collins_resultant(f, g) == r
|
| 185 |
+
assert R.dmp_qq_collins_resultant(f, g) == r
|
| 186 |
+
|
| 187 |
+
f = -x**3 + 5
|
| 188 |
+
g = 3*x**2*y + x**2
|
| 189 |
+
|
| 190 |
+
a = 45*y**2 + 30*y + 5
|
| 191 |
+
b = 675*y**3 + 675*y**2 + 225*y + 25
|
| 192 |
+
|
| 193 |
+
r = R.dmp_LC(b)
|
| 194 |
+
|
| 195 |
+
assert R.dmp_subresultants(f, g) == [f, g, a]
|
| 196 |
+
assert R.dmp_resultant(f, g) == r
|
| 197 |
+
assert R.dmp_prs_resultant(f, g)[0] == r
|
| 198 |
+
assert R.dmp_zz_collins_resultant(f, g) == r
|
| 199 |
+
assert R.dmp_qq_collins_resultant(f, g) == r
|
| 200 |
+
|
| 201 |
+
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
|
| 202 |
+
|
| 203 |
+
f = 6*x**2 - 3*x*y - 2*x*z + y*z
|
| 204 |
+
g = x**2 - x*u - x*v + u*v
|
| 205 |
+
|
| 206 |
+
r = y**2*z**2 - 3*y**2*z*u - 3*y**2*z*v + 9*y**2*u*v - 2*y*z**2*u \
|
| 207 |
+
- 2*y*z**2*v + 6*y*z*u**2 + 12*y*z*u*v + 6*y*z*v**2 - 18*y*u**2*v \
|
| 208 |
+
- 18*y*u*v**2 + 4*z**2*u*v - 12*z*u**2*v - 12*z*u*v**2 + 36*u**2*v**2
|
| 209 |
+
|
| 210 |
+
assert R.dmp_zz_collins_resultant(f, g) == r.drop(x)
|
| 211 |
+
|
| 212 |
+
R, x, y, z, u, v = ring("x,y,z,u,v", QQ)
|
| 213 |
+
|
| 214 |
+
f = x**2 - QQ(1,2)*x*y - QQ(1,3)*x*z + QQ(1,6)*y*z
|
| 215 |
+
g = x**2 - x*u - x*v + u*v
|
| 216 |
+
|
| 217 |
+
r = QQ(1,36)*y**2*z**2 - QQ(1,12)*y**2*z*u - QQ(1,12)*y**2*z*v + QQ(1,4)*y**2*u*v \
|
| 218 |
+
- QQ(1,18)*y*z**2*u - QQ(1,18)*y*z**2*v + QQ(1,6)*y*z*u**2 + QQ(1,3)*y*z*u*v \
|
| 219 |
+
+ QQ(1,6)*y*z*v**2 - QQ(1,2)*y*u**2*v - QQ(1,2)*y*u*v**2 + QQ(1,9)*z**2*u*v \
|
| 220 |
+
- QQ(1,3)*z*u**2*v - QQ(1,3)*z*u*v**2 + u**2*v**2
|
| 221 |
+
|
| 222 |
+
assert R.dmp_qq_collins_resultant(f, g) == r.drop(x)
|
| 223 |
+
|
| 224 |
+
Rt, t = ring("t", ZZ)
|
| 225 |
+
Rx, x = ring("x", Rt)
|
| 226 |
+
|
| 227 |
+
f = x**6 - 5*x**4 + 5*x**2 + 4
|
| 228 |
+
g = -6*t*x**5 + x**4 + 20*t*x**3 - 3*x**2 - 10*t*x + 6
|
| 229 |
+
|
| 230 |
+
assert Rx.dup_resultant(f, g) == 2930944*t**6 + 2198208*t**4 + 549552*t**2 + 45796
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
def test_dup_discriminant():
|
| 234 |
+
R, x = ring("x", ZZ)
|
| 235 |
+
|
| 236 |
+
assert R.dup_discriminant(0) == 0
|
| 237 |
+
assert R.dup_discriminant(x) == 1
|
| 238 |
+
|
| 239 |
+
assert R.dup_discriminant(x**3 + 3*x**2 + 9*x - 13) == -11664
|
| 240 |
+
assert R.dup_discriminant(5*x**5 + x**3 + 2) == 31252160
|
| 241 |
+
assert R.dup_discriminant(x**4 + 2*x**3 + 6*x**2 - 22*x + 13) == 0
|
| 242 |
+
assert R.dup_discriminant(12*x**7 + 15*x**4 + 30*x**3 + x**2 + 1) == -220289699947514112
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def test_dmp_discriminant():
|
| 246 |
+
R, x = ring("x", ZZ)
|
| 247 |
+
|
| 248 |
+
assert R.dmp_discriminant(0) == 0
|
| 249 |
+
|
| 250 |
+
R, x, y = ring("x,y", ZZ)
|
| 251 |
+
|
| 252 |
+
assert R.dmp_discriminant(0) == 0
|
| 253 |
+
assert R.dmp_discriminant(y) == 0
|
| 254 |
+
|
| 255 |
+
assert R.dmp_discriminant(x**3 + 3*x**2 + 9*x - 13) == -11664
|
| 256 |
+
assert R.dmp_discriminant(5*x**5 + x**3 + 2) == 31252160
|
| 257 |
+
assert R.dmp_discriminant(x**4 + 2*x**3 + 6*x**2 - 22*x + 13) == 0
|
| 258 |
+
assert R.dmp_discriminant(12*x**7 + 15*x**4 + 30*x**3 + x**2 + 1) == -220289699947514112
|
| 259 |
+
|
| 260 |
+
assert R.dmp_discriminant(x**2*y + 2*y) == (-8*y**2).drop(x)
|
| 261 |
+
assert R.dmp_discriminant(x*y**2 + 2*x) == 1
|
| 262 |
+
|
| 263 |
+
R, x, y, z = ring("x,y,z", ZZ)
|
| 264 |
+
assert R.dmp_discriminant(x*y + z) == 1
|
| 265 |
+
|
| 266 |
+
R, x, y, z, u = ring("x,y,z,u", ZZ)
|
| 267 |
+
assert R.dmp_discriminant(x**2*y + x*z + u) == (-4*y*u + z**2).drop(x)
|
| 268 |
+
|
| 269 |
+
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
|
| 270 |
+
assert R.dmp_discriminant(x**3*y + x**2*z + x*u + v) == \
|
| 271 |
+
(-27*y**2*v**2 + 18*y*z*u*v - 4*y*u**3 - 4*z**3*v + z**2*u**2).drop(x)
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def test_dup_gcd():
|
| 275 |
+
R, x = ring("x", ZZ)
|
| 276 |
+
|
| 277 |
+
f, g = 0, 0
|
| 278 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (0, 0, 0)
|
| 279 |
+
|
| 280 |
+
f, g = 2, 0
|
| 281 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 0)
|
| 282 |
+
|
| 283 |
+
f, g = -2, 0
|
| 284 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 0)
|
| 285 |
+
|
| 286 |
+
f, g = 0, -2
|
| 287 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 0, -1)
|
| 288 |
+
|
| 289 |
+
f, g = 0, 2*x + 4
|
| 290 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 0, 1)
|
| 291 |
+
|
| 292 |
+
f, g = 2*x + 4, 0
|
| 293 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2*x + 4, 1, 0)
|
| 294 |
+
|
| 295 |
+
f, g = 2, 2
|
| 296 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, 1)
|
| 297 |
+
|
| 298 |
+
f, g = -2, 2
|
| 299 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, 1)
|
| 300 |
+
|
| 301 |
+
f, g = 2, -2
|
| 302 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, -1)
|
| 303 |
+
|
| 304 |
+
f, g = -2, -2
|
| 305 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, -1, -1)
|
| 306 |
+
|
| 307 |
+
f, g = x**2 + 2*x + 1, 1
|
| 308 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1)
|
| 309 |
+
|
| 310 |
+
f, g = x**2 + 2*x + 1, 2
|
| 311 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2)
|
| 312 |
+
|
| 313 |
+
f, g = 2*x**2 + 4*x + 2, 2
|
| 314 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1)
|
| 315 |
+
|
| 316 |
+
f, g = 2, 2*x**2 + 4*x + 2
|
| 317 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1)
|
| 318 |
+
|
| 319 |
+
f, g = 2*x**2 + 4*x + 2, x + 1
|
| 320 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1)
|
| 321 |
+
|
| 322 |
+
f, g = x + 1, 2*x**2 + 4*x + 2
|
| 323 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2)
|
| 324 |
+
|
| 325 |
+
f, g = x - 31, x
|
| 326 |
+
assert R.dup_zz_heu_gcd(f, g) == R.dup_rr_prs_gcd(f, g) == (1, f, g)
|
| 327 |
+
|
| 328 |
+
f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
|
| 329 |
+
g = x**3 + 6*x**2 + 11*x + 6
|
| 330 |
+
|
| 331 |
+
h = x**2 + 3*x + 2
|
| 332 |
+
|
| 333 |
+
cff = x**2 + 5*x + 4
|
| 334 |
+
cfg = x + 3
|
| 335 |
+
|
| 336 |
+
assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
|
| 337 |
+
assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)
|
| 338 |
+
|
| 339 |
+
f = x**4 - 4
|
| 340 |
+
g = x**4 + 4*x**2 + 4
|
| 341 |
+
|
| 342 |
+
h = x**2 + 2
|
| 343 |
+
|
| 344 |
+
cff = x**2 - 2
|
| 345 |
+
cfg = x**2 + 2
|
| 346 |
+
|
| 347 |
+
assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
|
| 348 |
+
assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)
|
| 349 |
+
|
| 350 |
+
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
|
| 351 |
+
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
|
| 352 |
+
|
| 353 |
+
h = 1
|
| 354 |
+
|
| 355 |
+
cff = f
|
| 356 |
+
cfg = g
|
| 357 |
+
|
| 358 |
+
assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
|
| 359 |
+
assert R.dup_rr_prs_gcd(f, g) == (h, cff, cfg)
|
| 360 |
+
|
| 361 |
+
R, x = ring("x", QQ)
|
| 362 |
+
|
| 363 |
+
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
|
| 364 |
+
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
|
| 365 |
+
|
| 366 |
+
h = 1
|
| 367 |
+
|
| 368 |
+
cff = f
|
| 369 |
+
cfg = g
|
| 370 |
+
|
| 371 |
+
assert R.dup_qq_heu_gcd(f, g) == (h, cff, cfg)
|
| 372 |
+
assert R.dup_ff_prs_gcd(f, g) == (h, cff, cfg)
|
| 373 |
+
|
| 374 |
+
R, x = ring("x", ZZ)
|
| 375 |
+
|
| 376 |
+
f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
|
| 377 |
+
+ 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
|
| 378 |
+
+ 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
|
| 379 |
+
+ 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
|
| 380 |
+
- 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
|
| 381 |
+
+ 289127344604779611146960547954288113529690984687482920704*x**14 \
|
| 382 |
+
+ 19007977035740498977629742919480623972236450681*x**7 \
|
| 383 |
+
+ 311973482284542371301330321821976049
|
| 384 |
+
|
| 385 |
+
g = 365431878023781158602430064717380211405897160759702125019136*x**21 \
|
| 386 |
+
+ 197599133478719444145775798221171663643171734081650688*x**14 \
|
| 387 |
+
- 9504116979659010018253915765478924103928886144*x**7 \
|
| 388 |
+
- 311973482284542371301330321821976049
|
| 389 |
+
|
| 390 |
+
assert R.dup_zz_heu_gcd(f, R.dup_diff(f, 1))[0] == g
|
| 391 |
+
assert R.dup_rr_prs_gcd(f, R.dup_diff(f, 1))[0] == g
|
| 392 |
+
|
| 393 |
+
R, x = ring("x", QQ)
|
| 394 |
+
|
| 395 |
+
f = QQ(1,2)*x**2 + x + QQ(1,2)
|
| 396 |
+
g = QQ(1,2)*x + QQ(1,2)
|
| 397 |
+
|
| 398 |
+
h = x + 1
|
| 399 |
+
|
| 400 |
+
assert R.dup_qq_heu_gcd(f, g) == (h, g, QQ(1,2))
|
| 401 |
+
assert R.dup_ff_prs_gcd(f, g) == (h, g, QQ(1,2))
|
| 402 |
+
|
| 403 |
+
R, x = ring("x", ZZ)
|
| 404 |
+
|
| 405 |
+
f = 1317378933230047068160*x + 2945748836994210856960
|
| 406 |
+
g = 120352542776360960*x + 269116466014453760
|
| 407 |
+
|
| 408 |
+
h = 120352542776360960*x + 269116466014453760
|
| 409 |
+
cff = 10946
|
| 410 |
+
cfg = 1
|
| 411 |
+
|
| 412 |
+
assert R.dup_zz_heu_gcd(f, g) == (h, cff, cfg)
|
| 413 |
+
|
| 414 |
+
|
| 415 |
+
def test_dmp_gcd():
|
| 416 |
+
R, x, y = ring("x,y", ZZ)
|
| 417 |
+
|
| 418 |
+
f, g = 0, 0
|
| 419 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (0, 0, 0)
|
| 420 |
+
|
| 421 |
+
f, g = 2, 0
|
| 422 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 0)
|
| 423 |
+
|
| 424 |
+
f, g = -2, 0
|
| 425 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 0)
|
| 426 |
+
|
| 427 |
+
f, g = 0, -2
|
| 428 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 0, -1)
|
| 429 |
+
|
| 430 |
+
f, g = 0, 2*x + 4
|
| 431 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 0, 1)
|
| 432 |
+
|
| 433 |
+
f, g = 2*x + 4, 0
|
| 434 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2*x + 4, 1, 0)
|
| 435 |
+
|
| 436 |
+
f, g = 2, 2
|
| 437 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, 1)
|
| 438 |
+
|
| 439 |
+
f, g = -2, 2
|
| 440 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, 1)
|
| 441 |
+
|
| 442 |
+
f, g = 2, -2
|
| 443 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, -1)
|
| 444 |
+
|
| 445 |
+
f, g = -2, -2
|
| 446 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, -1, -1)
|
| 447 |
+
|
| 448 |
+
f, g = x**2 + 2*x + 1, 1
|
| 449 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 1)
|
| 450 |
+
|
| 451 |
+
f, g = x**2 + 2*x + 1, 2
|
| 452 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (1, x**2 + 2*x + 1, 2)
|
| 453 |
+
|
| 454 |
+
f, g = 2*x**2 + 4*x + 2, 2
|
| 455 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, x**2 + 2*x + 1, 1)
|
| 456 |
+
|
| 457 |
+
f, g = 2, 2*x**2 + 4*x + 2
|
| 458 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (2, 1, x**2 + 2*x + 1)
|
| 459 |
+
|
| 460 |
+
f, g = 2*x**2 + 4*x + 2, x + 1
|
| 461 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 2*x + 2, 1)
|
| 462 |
+
|
| 463 |
+
f, g = x + 1, 2*x**2 + 4*x + 2
|
| 464 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (x + 1, 1, 2*x + 2)
|
| 465 |
+
|
| 466 |
+
R, x, y, z, u = ring("x,y,z,u", ZZ)
|
| 467 |
+
|
| 468 |
+
f, g = u**2 + 2*u + 1, 2*u + 2
|
| 469 |
+
assert R.dmp_zz_heu_gcd(f, g) == R.dmp_rr_prs_gcd(f, g) == (u + 1, u + 1, 2)
|
| 470 |
+
|
| 471 |
+
f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1
|
| 472 |
+
h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1
|
| 473 |
+
|
| 474 |
+
assert R.dmp_zz_heu_gcd(f, g) == (h, cff, cfg)
|
| 475 |
+
assert R.dmp_rr_prs_gcd(f, g) == (h, cff, cfg)
|
| 476 |
+
|
| 477 |
+
assert R.dmp_zz_heu_gcd(g, f) == (h, cfg, cff)
|
| 478 |
+
assert R.dmp_rr_prs_gcd(g, f) == (h, cfg, cff)
|
| 479 |
+
|
| 480 |
+
R, x, y, z = ring("x,y,z", ZZ)
|
| 481 |
+
|
| 482 |
+
f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(2, ZZ))
|
| 483 |
+
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
|
| 484 |
+
|
| 485 |
+
assert H == h and R.dmp_mul(H, cff) == f \
|
| 486 |
+
and R.dmp_mul(H, cfg) == g
|
| 487 |
+
|
| 488 |
+
H, cff, cfg = R.dmp_rr_prs_gcd(f, g)
|
| 489 |
+
|
| 490 |
+
assert H == h and R.dmp_mul(H, cff) == f \
|
| 491 |
+
and R.dmp_mul(H, cfg) == g
|
| 492 |
+
|
| 493 |
+
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
|
| 494 |
+
|
| 495 |
+
f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(4, ZZ))
|
| 496 |
+
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
|
| 497 |
+
|
| 498 |
+
assert H == h and R.dmp_mul(H, cff) == f \
|
| 499 |
+
and R.dmp_mul(H, cfg) == g
|
| 500 |
+
|
| 501 |
+
R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ)
|
| 502 |
+
|
| 503 |
+
f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(6, ZZ))
|
| 504 |
+
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
|
| 505 |
+
|
| 506 |
+
assert H == h and R.dmp_mul(H, cff) == f \
|
| 507 |
+
and R.dmp_mul(H, cfg) == g
|
| 508 |
+
|
| 509 |
+
R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ)
|
| 510 |
+
|
| 511 |
+
f, g, h = map(R.from_dense, dmp_fateman_poly_F_1(8, ZZ))
|
| 512 |
+
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
|
| 513 |
+
|
| 514 |
+
assert H == h and R.dmp_mul(H, cff) == f \
|
| 515 |
+
and R.dmp_mul(H, cfg) == g
|
| 516 |
+
|
| 517 |
+
R, x, y, z = ring("x,y,z", ZZ)
|
| 518 |
+
|
| 519 |
+
f, g, h = map(R.from_dense, dmp_fateman_poly_F_2(2, ZZ))
|
| 520 |
+
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
|
| 521 |
+
|
| 522 |
+
assert H == h and R.dmp_mul(H, cff) == f \
|
| 523 |
+
and R.dmp_mul(H, cfg) == g
|
| 524 |
+
|
| 525 |
+
H, cff, cfg = R.dmp_rr_prs_gcd(f, g)
|
| 526 |
+
|
| 527 |
+
assert H == h and R.dmp_mul(H, cff) == f \
|
| 528 |
+
and R.dmp_mul(H, cfg) == g
|
| 529 |
+
|
| 530 |
+
f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(2, ZZ))
|
| 531 |
+
H, cff, cfg = R.dmp_zz_heu_gcd(f, g)
|
| 532 |
+
|
| 533 |
+
assert H == h and R.dmp_mul(H, cff) == f \
|
| 534 |
+
and R.dmp_mul(H, cfg) == g
|
| 535 |
+
|
| 536 |
+
H, cff, cfg = R.dmp_rr_prs_gcd(f, g)
|
| 537 |
+
|
| 538 |
+
assert H == h and R.dmp_mul(H, cff) == f \
|
| 539 |
+
and R.dmp_mul(H, cfg) == g
|
| 540 |
+
|
| 541 |
+
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
|
| 542 |
+
|
| 543 |
+
f, g, h = map(R.from_dense, dmp_fateman_poly_F_3(4, ZZ))
|
| 544 |
+
H, cff, cfg = R.dmp_inner_gcd(f, g)
|
| 545 |
+
|
| 546 |
+
assert H == h and R.dmp_mul(H, cff) == f \
|
| 547 |
+
and R.dmp_mul(H, cfg) == g
|
| 548 |
+
|
| 549 |
+
R, x, y = ring("x,y", QQ)
|
| 550 |
+
|
| 551 |
+
f = QQ(1,2)*x**2 + x + QQ(1,2)
|
| 552 |
+
g = QQ(1,2)*x + QQ(1,2)
|
| 553 |
+
|
| 554 |
+
h = x + 1
|
| 555 |
+
|
| 556 |
+
assert R.dmp_qq_heu_gcd(f, g) == (h, g, QQ(1,2))
|
| 557 |
+
assert R.dmp_ff_prs_gcd(f, g) == (h, g, QQ(1,2))
|
| 558 |
+
|
| 559 |
+
R, x, y = ring("x,y", RR)
|
| 560 |
+
|
| 561 |
+
f = 2.1*x*y**2 - 2.2*x*y + 2.1*x
|
| 562 |
+
g = 1.0*x**3
|
| 563 |
+
|
| 564 |
+
assert R.dmp_ff_prs_gcd(f, g) == \
|
| 565 |
+
(1.0*x, 2.1*y**2 - 2.2*y + 2.1, 1.0*x**2)
|
| 566 |
+
|
| 567 |
+
|
| 568 |
+
def test_dup_lcm():
|
| 569 |
+
R, x = ring("x", ZZ)
|
| 570 |
+
|
| 571 |
+
assert R.dup_lcm(2, 6) == 6
|
| 572 |
+
|
| 573 |
+
assert R.dup_lcm(2*x**3, 6*x) == 6*x**3
|
| 574 |
+
assert R.dup_lcm(2*x**3, 3*x) == 6*x**3
|
| 575 |
+
|
| 576 |
+
assert R.dup_lcm(x**2 + x, x) == x**2 + x
|
| 577 |
+
assert R.dup_lcm(x**2 + x, 2*x) == 2*x**2 + 2*x
|
| 578 |
+
assert R.dup_lcm(x**2 + 2*x, x) == x**2 + 2*x
|
| 579 |
+
assert R.dup_lcm(2*x**2 + x, x) == 2*x**2 + x
|
| 580 |
+
assert R.dup_lcm(2*x**2 + x, 2*x) == 4*x**2 + 2*x
|
| 581 |
+
|
| 582 |
+
|
| 583 |
+
def test_dmp_lcm():
|
| 584 |
+
R, x, y = ring("x,y", ZZ)
|
| 585 |
+
|
| 586 |
+
assert R.dmp_lcm(2, 6) == 6
|
| 587 |
+
assert R.dmp_lcm(x, y) == x*y
|
| 588 |
+
|
| 589 |
+
assert R.dmp_lcm(2*x**3, 6*x*y**2) == 6*x**3*y**2
|
| 590 |
+
assert R.dmp_lcm(2*x**3, 3*x*y**2) == 6*x**3*y**2
|
| 591 |
+
|
| 592 |
+
assert R.dmp_lcm(x**2*y, x*y**2) == x**2*y**2
|
| 593 |
+
|
| 594 |
+
f = 2*x*y**5 - 3*x*y**4 - 2*x*y**3 + 3*x*y**2
|
| 595 |
+
g = y**5 - 2*y**3 + y
|
| 596 |
+
h = 2*x*y**7 - 3*x*y**6 - 4*x*y**5 + 6*x*y**4 + 2*x*y**3 - 3*x*y**2
|
| 597 |
+
|
| 598 |
+
assert R.dmp_lcm(f, g) == h
|
| 599 |
+
|
| 600 |
+
f = x**3 - 3*x**2*y - 9*x*y**2 - 5*y**3
|
| 601 |
+
g = x**4 + 6*x**3*y + 12*x**2*y**2 + 10*x*y**3 + 3*y**4
|
| 602 |
+
h = x**5 + x**4*y - 18*x**3*y**2 - 50*x**2*y**3 - 47*x*y**4 - 15*y**5
|
| 603 |
+
|
| 604 |
+
assert R.dmp_lcm(f, g) == h
|
| 605 |
+
|
| 606 |
+
|
| 607 |
+
def test_dmp_content():
|
| 608 |
+
R, x,y = ring("x,y", ZZ)
|
| 609 |
+
|
| 610 |
+
assert R.dmp_content(-2) == 2
|
| 611 |
+
|
| 612 |
+
f, g, F = 3*y**2 + 2*y + 1, 1, 0
|
| 613 |
+
|
| 614 |
+
for i in range(0, 5):
|
| 615 |
+
g *= f
|
| 616 |
+
F += x**i*g
|
| 617 |
+
|
| 618 |
+
assert R.dmp_content(F) == f.drop(x)
|
| 619 |
+
|
| 620 |
+
R, x,y,z = ring("x,y,z", ZZ)
|
| 621 |
+
|
| 622 |
+
assert R.dmp_content(f_4) == 1
|
| 623 |
+
assert R.dmp_content(f_5) == 1
|
| 624 |
+
|
| 625 |
+
R, x,y,z,t = ring("x,y,z,t", ZZ)
|
| 626 |
+
assert R.dmp_content(f_6) == 1
|
| 627 |
+
|
| 628 |
+
|
| 629 |
+
def test_dmp_primitive():
|
| 630 |
+
R, x,y = ring("x,y", ZZ)
|
| 631 |
+
|
| 632 |
+
assert R.dmp_primitive(0) == (0, 0)
|
| 633 |
+
assert R.dmp_primitive(1) == (1, 1)
|
| 634 |
+
|
| 635 |
+
f, g, F = 3*y**2 + 2*y + 1, 1, 0
|
| 636 |
+
|
| 637 |
+
for i in range(0, 5):
|
| 638 |
+
g *= f
|
| 639 |
+
F += x**i*g
|
| 640 |
+
|
| 641 |
+
assert R.dmp_primitive(F) == (f.drop(x), F / f)
|
| 642 |
+
|
| 643 |
+
R, x,y,z = ring("x,y,z", ZZ)
|
| 644 |
+
|
| 645 |
+
cont, f = R.dmp_primitive(f_4)
|
| 646 |
+
assert cont == 1 and f == f_4
|
| 647 |
+
cont, f = R.dmp_primitive(f_5)
|
| 648 |
+
assert cont == 1 and f == f_5
|
| 649 |
+
|
| 650 |
+
R, x,y,z,t = ring("x,y,z,t", ZZ)
|
| 651 |
+
|
| 652 |
+
cont, f = R.dmp_primitive(f_6)
|
| 653 |
+
assert cont == 1 and f == f_6
|
| 654 |
+
|
| 655 |
+
|
| 656 |
+
def test_dup_cancel():
|
| 657 |
+
R, x = ring("x", ZZ)
|
| 658 |
+
|
| 659 |
+
f = 2*x**2 - 2
|
| 660 |
+
g = x**2 - 2*x + 1
|
| 661 |
+
|
| 662 |
+
p = 2*x + 2
|
| 663 |
+
q = x - 1
|
| 664 |
+
|
| 665 |
+
assert R.dup_cancel(f, g) == (p, q)
|
| 666 |
+
assert R.dup_cancel(f, g, include=False) == (1, 1, p, q)
|
| 667 |
+
|
| 668 |
+
f = -x - 2
|
| 669 |
+
g = 3*x - 4
|
| 670 |
+
|
| 671 |
+
F = x + 2
|
| 672 |
+
G = -3*x + 4
|
| 673 |
+
|
| 674 |
+
assert R.dup_cancel(f, g) == (f, g)
|
| 675 |
+
assert R.dup_cancel(F, G) == (f, g)
|
| 676 |
+
|
| 677 |
+
assert R.dup_cancel(0, 0) == (0, 0)
|
| 678 |
+
assert R.dup_cancel(0, 0, include=False) == (1, 1, 0, 0)
|
| 679 |
+
|
| 680 |
+
assert R.dup_cancel(x, 0) == (1, 0)
|
| 681 |
+
assert R.dup_cancel(x, 0, include=False) == (1, 1, 1, 0)
|
| 682 |
+
|
| 683 |
+
assert R.dup_cancel(0, x) == (0, 1)
|
| 684 |
+
assert R.dup_cancel(0, x, include=False) == (1, 1, 0, 1)
|
| 685 |
+
|
| 686 |
+
f = 0
|
| 687 |
+
g = x
|
| 688 |
+
one = 1
|
| 689 |
+
|
| 690 |
+
assert R.dup_cancel(f, g, include=True) == (f, one)
|
| 691 |
+
|
| 692 |
+
|
| 693 |
+
def test_dmp_cancel():
|
| 694 |
+
R, x, y = ring("x,y", ZZ)
|
| 695 |
+
|
| 696 |
+
f = 2*x**2 - 2
|
| 697 |
+
g = x**2 - 2*x + 1
|
| 698 |
+
|
| 699 |
+
p = 2*x + 2
|
| 700 |
+
q = x - 1
|
| 701 |
+
|
| 702 |
+
assert R.dmp_cancel(f, g) == (p, q)
|
| 703 |
+
assert R.dmp_cancel(f, g, include=False) == (1, 1, p, q)
|
| 704 |
+
|
| 705 |
+
assert R.dmp_cancel(0, 0) == (0, 0)
|
| 706 |
+
assert R.dmp_cancel(0, 0, include=False) == (1, 1, 0, 0)
|
| 707 |
+
|
| 708 |
+
assert R.dmp_cancel(y, 0) == (1, 0)
|
| 709 |
+
assert R.dmp_cancel(y, 0, include=False) == (1, 1, 1, 0)
|
| 710 |
+
|
| 711 |
+
assert R.dmp_cancel(0, y) == (0, 1)
|
| 712 |
+
assert R.dmp_cancel(0, y, include=False) == (1, 1, 0, 1)
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_fields.py
ADDED
|
@@ -0,0 +1,362 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Test sparse rational functions. """
|
| 2 |
+
|
| 3 |
+
from sympy.polys.fields import field, sfield, FracField, FracElement
|
| 4 |
+
from sympy.polys.rings import ring
|
| 5 |
+
from sympy.polys.domains import ZZ, QQ
|
| 6 |
+
from sympy.polys.orderings import lex
|
| 7 |
+
|
| 8 |
+
from sympy.testing.pytest import raises, XFAIL
|
| 9 |
+
from sympy.core import symbols, E
|
| 10 |
+
from sympy.core.numbers import Rational
|
| 11 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
| 12 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 13 |
+
|
| 14 |
+
def test_FracField___init__():
|
| 15 |
+
F1 = FracField("x,y", ZZ, lex)
|
| 16 |
+
F2 = FracField("x,y", ZZ, lex)
|
| 17 |
+
F3 = FracField("x,y,z", ZZ, lex)
|
| 18 |
+
|
| 19 |
+
assert F1.x == F1.gens[0]
|
| 20 |
+
assert F1.y == F1.gens[1]
|
| 21 |
+
assert F1.x == F2.x
|
| 22 |
+
assert F1.y == F2.y
|
| 23 |
+
assert F1.x != F3.x
|
| 24 |
+
assert F1.y != F3.y
|
| 25 |
+
|
| 26 |
+
def test_FracField___hash__():
|
| 27 |
+
F, x, y, z = field("x,y,z", QQ)
|
| 28 |
+
assert hash(F)
|
| 29 |
+
|
| 30 |
+
def test_FracField___eq__():
|
| 31 |
+
assert field("x,y,z", QQ)[0] == field("x,y,z", QQ)[0]
|
| 32 |
+
assert field("x,y,z", QQ)[0] is field("x,y,z", QQ)[0]
|
| 33 |
+
|
| 34 |
+
assert field("x,y,z", QQ)[0] != field("x,y,z", ZZ)[0]
|
| 35 |
+
assert field("x,y,z", QQ)[0] is not field("x,y,z", ZZ)[0]
|
| 36 |
+
|
| 37 |
+
assert field("x,y,z", ZZ)[0] != field("x,y,z", QQ)[0]
|
| 38 |
+
assert field("x,y,z", ZZ)[0] is not field("x,y,z", QQ)[0]
|
| 39 |
+
|
| 40 |
+
assert field("x,y,z", QQ)[0] != field("x,y", QQ)[0]
|
| 41 |
+
assert field("x,y,z", QQ)[0] is not field("x,y", QQ)[0]
|
| 42 |
+
|
| 43 |
+
assert field("x,y", QQ)[0] != field("x,y,z", QQ)[0]
|
| 44 |
+
assert field("x,y", QQ)[0] is not field("x,y,z", QQ)[0]
|
| 45 |
+
|
| 46 |
+
def test_sfield():
|
| 47 |
+
x = symbols("x")
|
| 48 |
+
|
| 49 |
+
F = FracField((E, exp(exp(x)), exp(x)), ZZ, lex)
|
| 50 |
+
e, exex, ex = F.gens
|
| 51 |
+
assert sfield(exp(x)*exp(exp(x) + 1 + log(exp(x) + 3)/2)**2/(exp(x) + 3)) \
|
| 52 |
+
== (F, e**2*exex**2*ex)
|
| 53 |
+
|
| 54 |
+
F = FracField((x, exp(1/x), log(x), x**QQ(1, 3)), ZZ, lex)
|
| 55 |
+
_, ex, lg, x3 = F.gens
|
| 56 |
+
assert sfield(((x-3)*log(x)+4*x**2)*exp(1/x+log(x)/3)/x**2) == \
|
| 57 |
+
(F, (4*F.x**2*ex + F.x*ex*lg - 3*ex*lg)/x3**5)
|
| 58 |
+
|
| 59 |
+
F = FracField((x, log(x), sqrt(x + log(x))), ZZ, lex)
|
| 60 |
+
_, lg, srt = F.gens
|
| 61 |
+
assert sfield((x + 1) / (x * (x + log(x))**QQ(3, 2)) - 1/(x * log(x)**2)) \
|
| 62 |
+
== (F, (F.x*lg**2 - F.x*srt + lg**2 - lg*srt)/
|
| 63 |
+
(F.x**2*lg**2*srt + F.x*lg**3*srt))
|
| 64 |
+
|
| 65 |
+
def test_FracElement___hash__():
|
| 66 |
+
F, x, y, z = field("x,y,z", QQ)
|
| 67 |
+
assert hash(x*y/z)
|
| 68 |
+
|
| 69 |
+
def test_FracElement_copy():
|
| 70 |
+
F, x, y, z = field("x,y,z", ZZ)
|
| 71 |
+
|
| 72 |
+
f = x*y/3*z
|
| 73 |
+
g = f.copy()
|
| 74 |
+
|
| 75 |
+
assert f == g
|
| 76 |
+
g.numer[(1, 1, 1)] = 7
|
| 77 |
+
assert f != g
|
| 78 |
+
|
| 79 |
+
def test_FracElement_as_expr():
|
| 80 |
+
F, x, y, z = field("x,y,z", ZZ)
|
| 81 |
+
f = (3*x**2*y - x*y*z)/(7*z**3 + 1)
|
| 82 |
+
|
| 83 |
+
X, Y, Z = F.symbols
|
| 84 |
+
g = (3*X**2*Y - X*Y*Z)/(7*Z**3 + 1)
|
| 85 |
+
|
| 86 |
+
assert f != g
|
| 87 |
+
assert f.as_expr() == g
|
| 88 |
+
|
| 89 |
+
X, Y, Z = symbols("x,y,z")
|
| 90 |
+
g = (3*X**2*Y - X*Y*Z)/(7*Z**3 + 1)
|
| 91 |
+
|
| 92 |
+
assert f != g
|
| 93 |
+
assert f.as_expr(X, Y, Z) == g
|
| 94 |
+
|
| 95 |
+
raises(ValueError, lambda: f.as_expr(X))
|
| 96 |
+
|
| 97 |
+
def test_FracElement_from_expr():
|
| 98 |
+
x, y, z = symbols("x,y,z")
|
| 99 |
+
F, X, Y, Z = field((x, y, z), ZZ)
|
| 100 |
+
|
| 101 |
+
f = F.from_expr(1)
|
| 102 |
+
assert f == 1 and isinstance(f, F.dtype)
|
| 103 |
+
|
| 104 |
+
f = F.from_expr(Rational(3, 7))
|
| 105 |
+
assert f == F(3)/7 and isinstance(f, F.dtype)
|
| 106 |
+
|
| 107 |
+
f = F.from_expr(x)
|
| 108 |
+
assert f == X and isinstance(f, F.dtype)
|
| 109 |
+
|
| 110 |
+
f = F.from_expr(Rational(3,7)*x)
|
| 111 |
+
assert f == X*Rational(3, 7) and isinstance(f, F.dtype)
|
| 112 |
+
|
| 113 |
+
f = F.from_expr(1/x)
|
| 114 |
+
assert f == 1/X and isinstance(f, F.dtype)
|
| 115 |
+
|
| 116 |
+
f = F.from_expr(x*y*z)
|
| 117 |
+
assert f == X*Y*Z and isinstance(f, F.dtype)
|
| 118 |
+
|
| 119 |
+
f = F.from_expr(x*y/z)
|
| 120 |
+
assert f == X*Y/Z and isinstance(f, F.dtype)
|
| 121 |
+
|
| 122 |
+
f = F.from_expr(x*y*z + x*y + x)
|
| 123 |
+
assert f == X*Y*Z + X*Y + X and isinstance(f, F.dtype)
|
| 124 |
+
|
| 125 |
+
f = F.from_expr((x*y*z + x*y + x)/(x*y + 7))
|
| 126 |
+
assert f == (X*Y*Z + X*Y + X)/(X*Y + 7) and isinstance(f, F.dtype)
|
| 127 |
+
|
| 128 |
+
f = F.from_expr(x**3*y*z + x**2*y**7 + 1)
|
| 129 |
+
assert f == X**3*Y*Z + X**2*Y**7 + 1 and isinstance(f, F.dtype)
|
| 130 |
+
|
| 131 |
+
raises(ValueError, lambda: F.from_expr(2**x))
|
| 132 |
+
raises(ValueError, lambda: F.from_expr(7*x + sqrt(2)))
|
| 133 |
+
|
| 134 |
+
assert isinstance(ZZ[2**x].get_field().convert(2**(-x)),
|
| 135 |
+
FracElement)
|
| 136 |
+
assert isinstance(ZZ[x**2].get_field().convert(x**(-6)),
|
| 137 |
+
FracElement)
|
| 138 |
+
assert isinstance(ZZ[exp(Rational(1, 3))].get_field().convert(E),
|
| 139 |
+
FracElement)
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def test_FracField_nested():
|
| 143 |
+
a, b, x = symbols('a b x')
|
| 144 |
+
F1 = ZZ.frac_field(a, b)
|
| 145 |
+
F2 = F1.frac_field(x)
|
| 146 |
+
frac = F2(a + b)
|
| 147 |
+
assert frac.numer == F1.poly_ring(x)(a + b)
|
| 148 |
+
assert frac.numer.coeffs() == [F1(a + b)]
|
| 149 |
+
assert frac.denom == F1.poly_ring(x)(1)
|
| 150 |
+
|
| 151 |
+
F3 = ZZ.poly_ring(a, b)
|
| 152 |
+
F4 = F3.frac_field(x)
|
| 153 |
+
frac = F4(a + b)
|
| 154 |
+
assert frac.numer == F3.poly_ring(x)(a + b)
|
| 155 |
+
assert frac.numer.coeffs() == [F3(a + b)]
|
| 156 |
+
assert frac.denom == F3.poly_ring(x)(1)
|
| 157 |
+
|
| 158 |
+
frac = F2(F3(a + b))
|
| 159 |
+
assert frac.numer == F1.poly_ring(x)(a + b)
|
| 160 |
+
assert frac.numer.coeffs() == [F1(a + b)]
|
| 161 |
+
assert frac.denom == F1.poly_ring(x)(1)
|
| 162 |
+
|
| 163 |
+
frac = F4(F1(a + b))
|
| 164 |
+
assert frac.numer == F3.poly_ring(x)(a + b)
|
| 165 |
+
assert frac.numer.coeffs() == [F3(a + b)]
|
| 166 |
+
assert frac.denom == F3.poly_ring(x)(1)
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
def test_FracElement__lt_le_gt_ge__():
|
| 170 |
+
F, x, y = field("x,y", ZZ)
|
| 171 |
+
|
| 172 |
+
assert F(1) < 1/x < 1/x**2 < 1/x**3
|
| 173 |
+
assert F(1) <= 1/x <= 1/x**2 <= 1/x**3
|
| 174 |
+
|
| 175 |
+
assert -7/x < 1/x < 3/x < y/x < 1/x**2
|
| 176 |
+
assert -7/x <= 1/x <= 3/x <= y/x <= 1/x**2
|
| 177 |
+
|
| 178 |
+
assert 1/x**3 > 1/x**2 > 1/x > F(1)
|
| 179 |
+
assert 1/x**3 >= 1/x**2 >= 1/x >= F(1)
|
| 180 |
+
|
| 181 |
+
assert 1/x**2 > y/x > 3/x > 1/x > -7/x
|
| 182 |
+
assert 1/x**2 >= y/x >= 3/x >= 1/x >= -7/x
|
| 183 |
+
|
| 184 |
+
def test_FracElement___neg__():
|
| 185 |
+
F, x,y = field("x,y", QQ)
|
| 186 |
+
|
| 187 |
+
f = (7*x - 9)/y
|
| 188 |
+
g = (-7*x + 9)/y
|
| 189 |
+
|
| 190 |
+
assert -f == g
|
| 191 |
+
assert -g == f
|
| 192 |
+
|
| 193 |
+
def test_FracElement___add__():
|
| 194 |
+
F, x,y = field("x,y", QQ)
|
| 195 |
+
|
| 196 |
+
f, g = 1/x, 1/y
|
| 197 |
+
assert f + g == g + f == (x + y)/(x*y)
|
| 198 |
+
|
| 199 |
+
assert x + F.ring.gens[0] == F.ring.gens[0] + x == 2*x
|
| 200 |
+
|
| 201 |
+
F, x,y = field("x,y", ZZ)
|
| 202 |
+
assert x + 3 == 3 + x
|
| 203 |
+
assert x + QQ(3,7) == QQ(3,7) + x == (7*x + 3)/7
|
| 204 |
+
|
| 205 |
+
Fuv, u,v = field("u,v", ZZ)
|
| 206 |
+
Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
|
| 207 |
+
|
| 208 |
+
f = (u*v + x)/(y + u*v)
|
| 209 |
+
assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u*v}
|
| 210 |
+
assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u*v}
|
| 211 |
+
|
| 212 |
+
Ruv, u,v = ring("u,v", ZZ)
|
| 213 |
+
Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
|
| 214 |
+
|
| 215 |
+
f = (u*v + x)/(y + u*v)
|
| 216 |
+
assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u*v}
|
| 217 |
+
assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u*v}
|
| 218 |
+
|
| 219 |
+
def test_FracElement___sub__():
|
| 220 |
+
F, x,y = field("x,y", QQ)
|
| 221 |
+
|
| 222 |
+
f, g = 1/x, 1/y
|
| 223 |
+
assert f - g == (-x + y)/(x*y)
|
| 224 |
+
|
| 225 |
+
assert x - F.ring.gens[0] == F.ring.gens[0] - x == 0
|
| 226 |
+
|
| 227 |
+
F, x,y = field("x,y", ZZ)
|
| 228 |
+
assert x - 3 == -(3 - x)
|
| 229 |
+
assert x - QQ(3,7) == -(QQ(3,7) - x) == (7*x - 3)/7
|
| 230 |
+
|
| 231 |
+
Fuv, u,v = field("u,v", ZZ)
|
| 232 |
+
Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
|
| 233 |
+
|
| 234 |
+
f = (u*v - x)/(y - u*v)
|
| 235 |
+
assert dict(f.numer) == {(1, 0, 0, 0):-1, (0, 0, 0, 0): u*v}
|
| 236 |
+
assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0):-u*v}
|
| 237 |
+
|
| 238 |
+
Ruv, u,v = ring("u,v", ZZ)
|
| 239 |
+
Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
|
| 240 |
+
|
| 241 |
+
f = (u*v - x)/(y - u*v)
|
| 242 |
+
assert dict(f.numer) == {(1, 0, 0, 0):-1, (0, 0, 0, 0): u*v}
|
| 243 |
+
assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0):-u*v}
|
| 244 |
+
|
| 245 |
+
def test_FracElement___mul__():
|
| 246 |
+
F, x,y = field("x,y", QQ)
|
| 247 |
+
|
| 248 |
+
f, g = 1/x, 1/y
|
| 249 |
+
assert f*g == g*f == 1/(x*y)
|
| 250 |
+
|
| 251 |
+
assert x*F.ring.gens[0] == F.ring.gens[0]*x == x**2
|
| 252 |
+
|
| 253 |
+
F, x,y = field("x,y", ZZ)
|
| 254 |
+
assert x*3 == 3*x
|
| 255 |
+
assert x*QQ(3,7) == QQ(3,7)*x == x*Rational(3, 7)
|
| 256 |
+
|
| 257 |
+
Fuv, u,v = field("u,v", ZZ)
|
| 258 |
+
Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
|
| 259 |
+
|
| 260 |
+
f = ((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)
|
| 261 |
+
assert dict(f.numer) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
|
| 262 |
+
assert dict(f.denom) == {(0, 0, 1, 0): v - 1, (0, 0, 0, 1): -u*v, (0, 0, 0, 0): -1}
|
| 263 |
+
|
| 264 |
+
Ruv, u,v = ring("u,v", ZZ)
|
| 265 |
+
Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
|
| 266 |
+
|
| 267 |
+
f = ((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)
|
| 268 |
+
assert dict(f.numer) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
|
| 269 |
+
assert dict(f.denom) == {(0, 0, 1, 0): v - 1, (0, 0, 0, 1): -u*v, (0, 0, 0, 0): -1}
|
| 270 |
+
|
| 271 |
+
def test_FracElement___truediv__():
|
| 272 |
+
F, x,y = field("x,y", QQ)
|
| 273 |
+
|
| 274 |
+
f, g = 1/x, 1/y
|
| 275 |
+
assert f/g == y/x
|
| 276 |
+
|
| 277 |
+
assert x/F.ring.gens[0] == F.ring.gens[0]/x == 1
|
| 278 |
+
|
| 279 |
+
F, x,y = field("x,y", ZZ)
|
| 280 |
+
assert x*3 == 3*x
|
| 281 |
+
assert x/QQ(3,7) == (QQ(3,7)/x)**-1 == x*Rational(7, 3)
|
| 282 |
+
|
| 283 |
+
raises(ZeroDivisionError, lambda: x/0)
|
| 284 |
+
raises(ZeroDivisionError, lambda: 1/(x - x))
|
| 285 |
+
raises(ZeroDivisionError, lambda: x/(x - x))
|
| 286 |
+
|
| 287 |
+
Fuv, u,v = field("u,v", ZZ)
|
| 288 |
+
Fxyzt, x,y,z,t = field("x,y,z,t", Fuv)
|
| 289 |
+
|
| 290 |
+
f = (u*v)/(x*y)
|
| 291 |
+
assert dict(f.numer) == {(0, 0, 0, 0): u*v}
|
| 292 |
+
assert dict(f.denom) == {(1, 1, 0, 0): 1}
|
| 293 |
+
|
| 294 |
+
g = (x*y)/(u*v)
|
| 295 |
+
assert dict(g.numer) == {(1, 1, 0, 0): 1}
|
| 296 |
+
assert dict(g.denom) == {(0, 0, 0, 0): u*v}
|
| 297 |
+
|
| 298 |
+
Ruv, u,v = ring("u,v", ZZ)
|
| 299 |
+
Fxyzt, x,y,z,t = field("x,y,z,t", Ruv)
|
| 300 |
+
|
| 301 |
+
f = (u*v)/(x*y)
|
| 302 |
+
assert dict(f.numer) == {(0, 0, 0, 0): u*v}
|
| 303 |
+
assert dict(f.denom) == {(1, 1, 0, 0): 1}
|
| 304 |
+
|
| 305 |
+
g = (x*y)/(u*v)
|
| 306 |
+
assert dict(g.numer) == {(1, 1, 0, 0): 1}
|
| 307 |
+
assert dict(g.denom) == {(0, 0, 0, 0): u*v}
|
| 308 |
+
|
| 309 |
+
def test_FracElement___pow__():
|
| 310 |
+
F, x,y = field("x,y", QQ)
|
| 311 |
+
|
| 312 |
+
f, g = 1/x, 1/y
|
| 313 |
+
|
| 314 |
+
assert f**3 == 1/x**3
|
| 315 |
+
assert g**3 == 1/y**3
|
| 316 |
+
|
| 317 |
+
assert (f*g)**3 == 1/(x**3*y**3)
|
| 318 |
+
assert (f*g)**-3 == (x*y)**3
|
| 319 |
+
|
| 320 |
+
raises(ZeroDivisionError, lambda: (x - x)**-3)
|
| 321 |
+
|
| 322 |
+
def test_FracElement_diff():
|
| 323 |
+
F, x,y,z = field("x,y,z", ZZ)
|
| 324 |
+
|
| 325 |
+
assert ((x**2 + y)/(z + 1)).diff(x) == 2*x/(z + 1)
|
| 326 |
+
|
| 327 |
+
@XFAIL
|
| 328 |
+
def test_FracElement___call__():
|
| 329 |
+
F, x,y,z = field("x,y,z", ZZ)
|
| 330 |
+
f = (x**2 + 3*y)/z
|
| 331 |
+
|
| 332 |
+
r = f(1, 1, 1)
|
| 333 |
+
assert r == 4 and not isinstance(r, FracElement)
|
| 334 |
+
raises(ZeroDivisionError, lambda: f(1, 1, 0))
|
| 335 |
+
|
| 336 |
+
def test_FracElement_evaluate():
|
| 337 |
+
F, x,y,z = field("x,y,z", ZZ)
|
| 338 |
+
Fyz = field("y,z", ZZ)[0]
|
| 339 |
+
f = (x**2 + 3*y)/z
|
| 340 |
+
|
| 341 |
+
assert f.evaluate(x, 0) == 3*Fyz.y/Fyz.z
|
| 342 |
+
raises(ZeroDivisionError, lambda: f.evaluate(z, 0))
|
| 343 |
+
|
| 344 |
+
def test_FracElement_subs():
|
| 345 |
+
F, x,y,z = field("x,y,z", ZZ)
|
| 346 |
+
f = (x**2 + 3*y)/z
|
| 347 |
+
|
| 348 |
+
assert f.subs(x, 0) == 3*y/z
|
| 349 |
+
raises(ZeroDivisionError, lambda: f.subs(z, 0))
|
| 350 |
+
|
| 351 |
+
def test_FracElement_compose():
|
| 352 |
+
pass
|
| 353 |
+
|
| 354 |
+
def test_FracField_index():
|
| 355 |
+
a = symbols("a")
|
| 356 |
+
F, x, y, z = field('x y z', QQ)
|
| 357 |
+
assert F.index(x) == 0
|
| 358 |
+
assert F.index(y) == 1
|
| 359 |
+
|
| 360 |
+
raises(ValueError, lambda: F.index(1))
|
| 361 |
+
raises(ValueError, lambda: F.index(a))
|
| 362 |
+
pass
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_heuristicgcd.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sympy.polys.rings import ring
|
| 2 |
+
from sympy.polys.domains import ZZ
|
| 3 |
+
from sympy.polys.heuristicgcd import heugcd
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
def test_heugcd_univariate_integers():
|
| 7 |
+
R, x = ring("x", ZZ)
|
| 8 |
+
|
| 9 |
+
f = x**4 + 8*x**3 + 21*x**2 + 22*x + 8
|
| 10 |
+
g = x**3 + 6*x**2 + 11*x + 6
|
| 11 |
+
|
| 12 |
+
h = x**2 + 3*x + 2
|
| 13 |
+
|
| 14 |
+
cff = x**2 + 5*x + 4
|
| 15 |
+
cfg = x + 3
|
| 16 |
+
|
| 17 |
+
assert heugcd(f, g) == (h, cff, cfg)
|
| 18 |
+
|
| 19 |
+
f = x**4 - 4
|
| 20 |
+
g = x**4 + 4*x**2 + 4
|
| 21 |
+
|
| 22 |
+
h = x**2 + 2
|
| 23 |
+
|
| 24 |
+
cff = x**2 - 2
|
| 25 |
+
cfg = x**2 + 2
|
| 26 |
+
|
| 27 |
+
assert heugcd(f, g) == (h, cff, cfg)
|
| 28 |
+
|
| 29 |
+
f = x**8 + x**6 - 3*x**4 - 3*x**3 + 8*x**2 + 2*x - 5
|
| 30 |
+
g = 3*x**6 + 5*x**4 - 4*x**2 - 9*x + 21
|
| 31 |
+
|
| 32 |
+
h = 1
|
| 33 |
+
|
| 34 |
+
cff = f
|
| 35 |
+
cfg = g
|
| 36 |
+
|
| 37 |
+
assert heugcd(f, g) == (h, cff, cfg)
|
| 38 |
+
|
| 39 |
+
f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
|
| 40 |
+
+ 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
|
| 41 |
+
+ 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
|
| 42 |
+
+ 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
|
| 43 |
+
- 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
|
| 44 |
+
+ 289127344604779611146960547954288113529690984687482920704*x**14 \
|
| 45 |
+
+ 19007977035740498977629742919480623972236450681*x**7 \
|
| 46 |
+
+ 311973482284542371301330321821976049
|
| 47 |
+
|
| 48 |
+
g = 365431878023781158602430064717380211405897160759702125019136*x**21 \
|
| 49 |
+
+ 197599133478719444145775798221171663643171734081650688*x**14 \
|
| 50 |
+
- 9504116979659010018253915765478924103928886144*x**7 \
|
| 51 |
+
- 311973482284542371301330321821976049
|
| 52 |
+
|
| 53 |
+
# TODO: assert heugcd(f, f.diff(x))[0] == g
|
| 54 |
+
|
| 55 |
+
f = 1317378933230047068160*x + 2945748836994210856960
|
| 56 |
+
g = 120352542776360960*x + 269116466014453760
|
| 57 |
+
|
| 58 |
+
h = 120352542776360960*x + 269116466014453760
|
| 59 |
+
cff = 10946
|
| 60 |
+
cfg = 1
|
| 61 |
+
|
| 62 |
+
assert heugcd(f, g) == (h, cff, cfg)
|
| 63 |
+
|
| 64 |
+
def test_heugcd_multivariate_integers():
|
| 65 |
+
R, x, y = ring("x,y", ZZ)
|
| 66 |
+
|
| 67 |
+
f, g = 2*x**2 + 4*x + 2, x + 1
|
| 68 |
+
assert heugcd(f, g) == (x + 1, 2*x + 2, 1)
|
| 69 |
+
|
| 70 |
+
f, g = x + 1, 2*x**2 + 4*x + 2
|
| 71 |
+
assert heugcd(f, g) == (x + 1, 1, 2*x + 2)
|
| 72 |
+
|
| 73 |
+
R, x, y, z, u = ring("x,y,z,u", ZZ)
|
| 74 |
+
|
| 75 |
+
f, g = u**2 + 2*u + 1, 2*u + 2
|
| 76 |
+
assert heugcd(f, g) == (u + 1, u + 1, 2)
|
| 77 |
+
|
| 78 |
+
f, g = z**2*u**2 + 2*z**2*u + z**2 + z*u + z, u**2 + 2*u + 1
|
| 79 |
+
h, cff, cfg = u + 1, z**2*u + z**2 + z, u + 1
|
| 80 |
+
|
| 81 |
+
assert heugcd(f, g) == (h, cff, cfg)
|
| 82 |
+
assert heugcd(g, f) == (h, cfg, cff)
|
| 83 |
+
|
| 84 |
+
R, x, y, z = ring("x,y,z", ZZ)
|
| 85 |
+
|
| 86 |
+
f, g, h = R.fateman_poly_F_1()
|
| 87 |
+
H, cff, cfg = heugcd(f, g)
|
| 88 |
+
|
| 89 |
+
assert H == h and H*cff == f and H*cfg == g
|
| 90 |
+
|
| 91 |
+
R, x, y, z, u, v = ring("x,y,z,u,v", ZZ)
|
| 92 |
+
|
| 93 |
+
f, g, h = R.fateman_poly_F_1()
|
| 94 |
+
H, cff, cfg = heugcd(f, g)
|
| 95 |
+
|
| 96 |
+
assert H == h and H*cff == f and H*cfg == g
|
| 97 |
+
|
| 98 |
+
R, x, y, z, u, v, a, b = ring("x,y,z,u,v,a,b", ZZ)
|
| 99 |
+
|
| 100 |
+
f, g, h = R.fateman_poly_F_1()
|
| 101 |
+
H, cff, cfg = heugcd(f, g)
|
| 102 |
+
|
| 103 |
+
assert H == h and H*cff == f and H*cfg == g
|
| 104 |
+
|
| 105 |
+
R, x, y, z, u, v, a, b, c, d = ring("x,y,z,u,v,a,b,c,d", ZZ)
|
| 106 |
+
|
| 107 |
+
f, g, h = R.fateman_poly_F_1()
|
| 108 |
+
H, cff, cfg = heugcd(f, g)
|
| 109 |
+
|
| 110 |
+
assert H == h and H*cff == f and H*cfg == g
|
| 111 |
+
|
| 112 |
+
R, x, y, z = ring("x,y,z", ZZ)
|
| 113 |
+
|
| 114 |
+
f, g, h = R.fateman_poly_F_2()
|
| 115 |
+
H, cff, cfg = heugcd(f, g)
|
| 116 |
+
|
| 117 |
+
assert H == h and H*cff == f and H*cfg == g
|
| 118 |
+
|
| 119 |
+
f, g, h = R.fateman_poly_F_3()
|
| 120 |
+
H, cff, cfg = heugcd(f, g)
|
| 121 |
+
|
| 122 |
+
assert H == h and H*cff == f and H*cfg == g
|
| 123 |
+
|
| 124 |
+
R, x, y, z, t = ring("x,y,z,t", ZZ)
|
| 125 |
+
|
| 126 |
+
f, g, h = R.fateman_poly_F_3()
|
| 127 |
+
H, cff, cfg = heugcd(f, g)
|
| 128 |
+
|
| 129 |
+
assert H == h and H*cff == f and H*cfg == g
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
def test_issue_10996():
|
| 133 |
+
R, x, y, z = ring("x,y,z", ZZ)
|
| 134 |
+
|
| 135 |
+
f = 12*x**6*y**7*z**3 - 3*x**4*y**9*z**3 + 12*x**3*y**5*z**4
|
| 136 |
+
g = -48*x**7*y**8*z**3 + 12*x**5*y**10*z**3 - 48*x**5*y**7*z**2 + \
|
| 137 |
+
36*x**4*y**7*z - 48*x**4*y**6*z**4 + 12*x**3*y**9*z**2 - 48*x**3*y**4 \
|
| 138 |
+
- 9*x**2*y**9*z - 48*x**2*y**5*z**3 + 12*x*y**6 + 36*x*y**5*z**2 - 48*y**2*z
|
| 139 |
+
|
| 140 |
+
H, cff, cfg = heugcd(f, g)
|
| 141 |
+
|
| 142 |
+
assert H == 12*x**3*y**4 - 3*x*y**6 + 12*y**2*z
|
| 143 |
+
assert H*cff == f and H*cfg == g
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
def test_issue_25793():
|
| 147 |
+
R, x = ring("x", ZZ)
|
| 148 |
+
f = x - 4851 # failure starts for values more than 4850
|
| 149 |
+
g = f*(2*x + 1)
|
| 150 |
+
H, cff, cfg = R.dup_zz_heu_gcd(f, g)
|
| 151 |
+
assert H == f
|
| 152 |
+
# needs a test for dmp, too, that fails in master before this change
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_orderings.py
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests of monomial orderings. """
|
| 2 |
+
|
| 3 |
+
from sympy.polys.orderings import (
|
| 4 |
+
monomial_key, lex, grlex, grevlex, ilex, igrlex,
|
| 5 |
+
LexOrder, InverseOrder, ProductOrder, build_product_order,
|
| 6 |
+
)
|
| 7 |
+
|
| 8 |
+
from sympy.abc import x, y, z, t
|
| 9 |
+
from sympy.core import S
|
| 10 |
+
from sympy.testing.pytest import raises
|
| 11 |
+
|
| 12 |
+
def test_lex_order():
|
| 13 |
+
assert lex((1, 2, 3)) == (1, 2, 3)
|
| 14 |
+
assert str(lex) == 'lex'
|
| 15 |
+
|
| 16 |
+
assert lex((1, 2, 3)) == lex((1, 2, 3))
|
| 17 |
+
|
| 18 |
+
assert lex((2, 2, 3)) > lex((1, 2, 3))
|
| 19 |
+
assert lex((1, 3, 3)) > lex((1, 2, 3))
|
| 20 |
+
assert lex((1, 2, 4)) > lex((1, 2, 3))
|
| 21 |
+
|
| 22 |
+
assert lex((0, 2, 3)) < lex((1, 2, 3))
|
| 23 |
+
assert lex((1, 1, 3)) < lex((1, 2, 3))
|
| 24 |
+
assert lex((1, 2, 2)) < lex((1, 2, 3))
|
| 25 |
+
|
| 26 |
+
assert lex.is_global is True
|
| 27 |
+
assert lex == LexOrder()
|
| 28 |
+
assert lex != grlex
|
| 29 |
+
|
| 30 |
+
def test_grlex_order():
|
| 31 |
+
assert grlex((1, 2, 3)) == (6, (1, 2, 3))
|
| 32 |
+
assert str(grlex) == 'grlex'
|
| 33 |
+
|
| 34 |
+
assert grlex((1, 2, 3)) == grlex((1, 2, 3))
|
| 35 |
+
|
| 36 |
+
assert grlex((2, 2, 3)) > grlex((1, 2, 3))
|
| 37 |
+
assert grlex((1, 3, 3)) > grlex((1, 2, 3))
|
| 38 |
+
assert grlex((1, 2, 4)) > grlex((1, 2, 3))
|
| 39 |
+
|
| 40 |
+
assert grlex((0, 2, 3)) < grlex((1, 2, 3))
|
| 41 |
+
assert grlex((1, 1, 3)) < grlex((1, 2, 3))
|
| 42 |
+
assert grlex((1, 2, 2)) < grlex((1, 2, 3))
|
| 43 |
+
|
| 44 |
+
assert grlex((2, 2, 3)) > grlex((1, 2, 4))
|
| 45 |
+
assert grlex((1, 3, 3)) > grlex((1, 2, 4))
|
| 46 |
+
|
| 47 |
+
assert grlex((0, 2, 3)) < grlex((1, 2, 2))
|
| 48 |
+
assert grlex((1, 1, 3)) < grlex((1, 2, 2))
|
| 49 |
+
|
| 50 |
+
assert grlex((0, 1, 1)) > grlex((0, 0, 2))
|
| 51 |
+
assert grlex((0, 3, 1)) < grlex((2, 2, 1))
|
| 52 |
+
|
| 53 |
+
assert grlex.is_global is True
|
| 54 |
+
|
| 55 |
+
def test_grevlex_order():
|
| 56 |
+
assert grevlex((1, 2, 3)) == (6, (-3, -2, -1))
|
| 57 |
+
assert str(grevlex) == 'grevlex'
|
| 58 |
+
|
| 59 |
+
assert grevlex((1, 2, 3)) == grevlex((1, 2, 3))
|
| 60 |
+
|
| 61 |
+
assert grevlex((2, 2, 3)) > grevlex((1, 2, 3))
|
| 62 |
+
assert grevlex((1, 3, 3)) > grevlex((1, 2, 3))
|
| 63 |
+
assert grevlex((1, 2, 4)) > grevlex((1, 2, 3))
|
| 64 |
+
|
| 65 |
+
assert grevlex((0, 2, 3)) < grevlex((1, 2, 3))
|
| 66 |
+
assert grevlex((1, 1, 3)) < grevlex((1, 2, 3))
|
| 67 |
+
assert grevlex((1, 2, 2)) < grevlex((1, 2, 3))
|
| 68 |
+
|
| 69 |
+
assert grevlex((2, 2, 3)) > grevlex((1, 2, 4))
|
| 70 |
+
assert grevlex((1, 3, 3)) > grevlex((1, 2, 4))
|
| 71 |
+
|
| 72 |
+
assert grevlex((0, 2, 3)) < grevlex((1, 2, 2))
|
| 73 |
+
assert grevlex((1, 1, 3)) < grevlex((1, 2, 2))
|
| 74 |
+
|
| 75 |
+
assert grevlex((0, 1, 1)) > grevlex((0, 0, 2))
|
| 76 |
+
assert grevlex((0, 3, 1)) < grevlex((2, 2, 1))
|
| 77 |
+
|
| 78 |
+
assert grevlex.is_global is True
|
| 79 |
+
|
| 80 |
+
def test_InverseOrder():
|
| 81 |
+
ilex = InverseOrder(lex)
|
| 82 |
+
igrlex = InverseOrder(grlex)
|
| 83 |
+
|
| 84 |
+
assert ilex((1, 2, 3)) > ilex((2, 0, 3))
|
| 85 |
+
assert igrlex((1, 2, 3)) < igrlex((0, 2, 3))
|
| 86 |
+
assert str(ilex) == "ilex"
|
| 87 |
+
assert str(igrlex) == "igrlex"
|
| 88 |
+
assert ilex.is_global is False
|
| 89 |
+
assert igrlex.is_global is False
|
| 90 |
+
assert ilex != igrlex
|
| 91 |
+
assert ilex == InverseOrder(LexOrder())
|
| 92 |
+
|
| 93 |
+
def test_ProductOrder():
|
| 94 |
+
P = ProductOrder((grlex, lambda m: m[:2]), (grlex, lambda m: m[2:]))
|
| 95 |
+
assert P((1, 3, 3, 4, 5)) > P((2, 1, 5, 5, 5))
|
| 96 |
+
assert str(P) == "ProductOrder(grlex, grlex)"
|
| 97 |
+
assert P.is_global is True
|
| 98 |
+
assert ProductOrder((grlex, None), (ilex, None)).is_global is None
|
| 99 |
+
assert ProductOrder((igrlex, None), (ilex, None)).is_global is False
|
| 100 |
+
|
| 101 |
+
def test_monomial_key():
|
| 102 |
+
assert monomial_key() == lex
|
| 103 |
+
|
| 104 |
+
assert monomial_key('lex') == lex
|
| 105 |
+
assert monomial_key('grlex') == grlex
|
| 106 |
+
assert monomial_key('grevlex') == grevlex
|
| 107 |
+
|
| 108 |
+
raises(ValueError, lambda: monomial_key('foo'))
|
| 109 |
+
raises(ValueError, lambda: monomial_key(1))
|
| 110 |
+
|
| 111 |
+
M = [x, x**2*z**2, x*y, x**2, S.One, y**2, x**3, y, z, x*y**2*z, x**2*y**2]
|
| 112 |
+
assert sorted(M, key=monomial_key('lex', [z, y, x])) == \
|
| 113 |
+
[S.One, x, x**2, x**3, y, x*y, y**2, x**2*y**2, z, x*y**2*z, x**2*z**2]
|
| 114 |
+
assert sorted(M, key=monomial_key('grlex', [z, y, x])) == \
|
| 115 |
+
[S.One, x, y, z, x**2, x*y, y**2, x**3, x**2*y**2, x*y**2*z, x**2*z**2]
|
| 116 |
+
assert sorted(M, key=monomial_key('grevlex', [z, y, x])) == \
|
| 117 |
+
[S.One, x, y, z, x**2, x*y, y**2, x**3, x**2*y**2, x**2*z**2, x*y**2*z]
|
| 118 |
+
|
| 119 |
+
def test_build_product_order():
|
| 120 |
+
assert build_product_order((("grlex", x, y), ("grlex", z, t)), [x, y, z, t])((4, 5, 6, 7)) == \
|
| 121 |
+
((9, (4, 5)), (13, (6, 7)))
|
| 122 |
+
|
| 123 |
+
assert build_product_order((("grlex", x, y), ("grlex", z, t)), [x, y, z, t]) == \
|
| 124 |
+
build_product_order((("grlex", x, y), ("grlex", z, t)), [x, y, z, t])
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_partfrac.py
ADDED
|
@@ -0,0 +1,249 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for algorithms for partial fraction decomposition of rational
|
| 2 |
+
functions. """
|
| 3 |
+
|
| 4 |
+
from sympy.polys.partfrac import (
|
| 5 |
+
apart_undetermined_coeffs,
|
| 6 |
+
apart,
|
| 7 |
+
apart_list, assemble_partfrac_list
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
from sympy.core.expr import Expr
|
| 11 |
+
from sympy.core.function import Lambda
|
| 12 |
+
from sympy.core.numbers import (E, I, Rational, pi, all_close)
|
| 13 |
+
from sympy.core.relational import Eq
|
| 14 |
+
from sympy.core.singleton import S
|
| 15 |
+
from sympy.core.symbol import (Dummy, Symbol)
|
| 16 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 17 |
+
from sympy.matrices.dense import Matrix
|
| 18 |
+
from sympy.polys.polytools import (Poly, factor)
|
| 19 |
+
from sympy.polys.rationaltools import together
|
| 20 |
+
from sympy.polys.rootoftools import RootSum
|
| 21 |
+
from sympy.testing.pytest import raises, XFAIL
|
| 22 |
+
from sympy.abc import x, y, a, b, c
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def test_apart():
|
| 26 |
+
assert apart(1) == 1
|
| 27 |
+
assert apart(1, x) == 1
|
| 28 |
+
|
| 29 |
+
f, g = (x**2 + 1)/(x + 1), 2/(x + 1) + x - 1
|
| 30 |
+
|
| 31 |
+
assert apart(f, full=False) == g
|
| 32 |
+
assert apart(f, full=True) == g
|
| 33 |
+
|
| 34 |
+
f, g = 1/(x + 2)/(x + 1), 1/(1 + x) - 1/(2 + x)
|
| 35 |
+
|
| 36 |
+
assert apart(f, full=False) == g
|
| 37 |
+
assert apart(f, full=True) == g
|
| 38 |
+
|
| 39 |
+
f, g = 1/(x + 1)/(x + 5), -1/(5 + x)/4 + 1/(1 + x)/4
|
| 40 |
+
|
| 41 |
+
assert apart(f, full=False) == g
|
| 42 |
+
assert apart(f, full=True) == g
|
| 43 |
+
|
| 44 |
+
assert apart((E*x + 2)/(x - pi)*(x - 1), x) == \
|
| 45 |
+
2 - E + E*pi + E*x + (E*pi + 2)*(pi - 1)/(x - pi)
|
| 46 |
+
|
| 47 |
+
assert apart(Eq((x**2 + 1)/(x + 1), x), x) == Eq(x - 1 + 2/(x + 1), x)
|
| 48 |
+
|
| 49 |
+
assert apart(x/2, y) == x/2
|
| 50 |
+
|
| 51 |
+
f, g = (x+y)/(2*x - y), Rational(3, 2)*y/(2*x - y) + S.Half
|
| 52 |
+
|
| 53 |
+
assert apart(f, x, full=False) == g
|
| 54 |
+
assert apart(f, x, full=True) == g
|
| 55 |
+
|
| 56 |
+
f, g = (x+y)/(2*x - y), 3*x/(2*x - y) - 1
|
| 57 |
+
|
| 58 |
+
assert apart(f, y, full=False) == g
|
| 59 |
+
assert apart(f, y, full=True) == g
|
| 60 |
+
|
| 61 |
+
raises(NotImplementedError, lambda: apart(1/(x + 1)/(y + 2)))
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def test_apart_matrix():
|
| 65 |
+
M = Matrix(2, 2, lambda i, j: 1/(x + i + 1)/(x + j))
|
| 66 |
+
|
| 67 |
+
assert apart(M) == Matrix([
|
| 68 |
+
[1/x - 1/(x + 1), (x + 1)**(-2)],
|
| 69 |
+
[1/(2*x) - (S.Half)/(x + 2), 1/(x + 1) - 1/(x + 2)],
|
| 70 |
+
])
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_apart_symbolic():
|
| 74 |
+
f = a*x**4 + (2*b + 2*a*c)*x**3 + (4*b*c - a**2 + a*c**2)*x**2 + \
|
| 75 |
+
(-2*a*b + 2*b*c**2)*x - b**2
|
| 76 |
+
g = a**2*x**4 + (2*a*b + 2*c*a**2)*x**3 + (4*a*b*c + b**2 +
|
| 77 |
+
a**2*c**2)*x**2 + (2*c*b**2 + 2*a*b*c**2)*x + b**2*c**2
|
| 78 |
+
|
| 79 |
+
assert apart(f/g, x) == 1/a - 1/(x + c)**2 - b**2/(a*(a*x + b)**2)
|
| 80 |
+
|
| 81 |
+
assert apart(1/((x + a)*(x + b)*(x + c)), x) == \
|
| 82 |
+
1/((a - c)*(b - c)*(c + x)) - 1/((a - b)*(b - c)*(b + x)) + \
|
| 83 |
+
1/((a - b)*(a - c)*(a + x))
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def _make_extension_example():
|
| 87 |
+
# https://github.com/sympy/sympy/issues/18531
|
| 88 |
+
from sympy.core import Mul
|
| 89 |
+
def mul2(expr):
|
| 90 |
+
# 2-arg mul hack...
|
| 91 |
+
return Mul(2, expr, evaluate=False)
|
| 92 |
+
|
| 93 |
+
f = ((x**2 + 1)**3/((x - 1)**2*(x + 1)**2*(-x**2 + 2*x + 1)*(x**2 + 2*x - 1)))
|
| 94 |
+
g = (1/mul2(x - sqrt(2) + 1)
|
| 95 |
+
- 1/mul2(x - sqrt(2) - 1)
|
| 96 |
+
+ 1/mul2(x + 1 + sqrt(2))
|
| 97 |
+
- 1/mul2(x - 1 + sqrt(2))
|
| 98 |
+
+ 1/mul2((x + 1)**2)
|
| 99 |
+
+ 1/mul2((x - 1)**2))
|
| 100 |
+
return f, g
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def test_apart_extension():
|
| 104 |
+
f = 2/(x**2 + 1)
|
| 105 |
+
g = I/(x + I) - I/(x - I)
|
| 106 |
+
|
| 107 |
+
assert apart(f, extension=I) == g
|
| 108 |
+
assert apart(f, gaussian=True) == g
|
| 109 |
+
|
| 110 |
+
f = x/((x - 2)*(x + I))
|
| 111 |
+
|
| 112 |
+
assert factor(together(apart(f)).expand()) == f
|
| 113 |
+
|
| 114 |
+
f, g = _make_extension_example()
|
| 115 |
+
|
| 116 |
+
# XXX: Only works with dotprodsimp. See test_apart_extension_xfail below
|
| 117 |
+
from sympy.matrices import dotprodsimp
|
| 118 |
+
with dotprodsimp(True):
|
| 119 |
+
assert apart(f, x, extension={sqrt(2)}) == g
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def test_apart_extension_xfail():
|
| 123 |
+
f, g = _make_extension_example()
|
| 124 |
+
assert apart(f, x, extension={sqrt(2)}) == g
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def test_apart_full():
|
| 128 |
+
f = 1/(x**2 + 1)
|
| 129 |
+
|
| 130 |
+
assert apart(f, full=False) == f
|
| 131 |
+
assert apart(f, full=True).dummy_eq(
|
| 132 |
+
-RootSum(x**2 + 1, Lambda(a, a/(x - a)), auto=False)/2)
|
| 133 |
+
|
| 134 |
+
f = 1/(x**3 + x + 1)
|
| 135 |
+
|
| 136 |
+
assert apart(f, full=False) == f
|
| 137 |
+
assert apart(f, full=True).dummy_eq(
|
| 138 |
+
RootSum(x**3 + x + 1,
|
| 139 |
+
Lambda(a, (a**2*Rational(6, 31) - a*Rational(9, 31) + Rational(4, 31))/(x - a)), auto=False))
|
| 140 |
+
|
| 141 |
+
f = 1/(x**5 + 1)
|
| 142 |
+
|
| 143 |
+
assert apart(f, full=False) == \
|
| 144 |
+
(Rational(-1, 5))*((x**3 - 2*x**2 + 3*x - 4)/(x**4 - x**3 + x**2 -
|
| 145 |
+
x + 1)) + (Rational(1, 5))/(x + 1)
|
| 146 |
+
assert apart(f, full=True).dummy_eq(
|
| 147 |
+
-RootSum(x**4 - x**3 + x**2 - x + 1,
|
| 148 |
+
Lambda(a, a/(x - a)), auto=False)/5 + (Rational(1, 5))/(x + 1))
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def test_apart_full_floats():
|
| 152 |
+
# https://github.com/sympy/sympy/issues/26648
|
| 153 |
+
f = (
|
| 154 |
+
6.43369157032015e-9*x**3 + 1.35203404799555e-5*x**2
|
| 155 |
+
+ 0.00357538393743079*x + 0.085
|
| 156 |
+
)/(
|
| 157 |
+
4.74334912634438e-11*x**4 + 4.09576274286244e-6*x**3
|
| 158 |
+
+ 0.00334241812250921*x**2 + 0.15406018058983*x + 1.0
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
+
expected = (
|
| 162 |
+
133.599202650992/(x + 85524.0054884464)
|
| 163 |
+
+ 1.07757928431867/(x + 774.88576677949)
|
| 164 |
+
+ 0.395006955518971/(x + 40.7977016133126)
|
| 165 |
+
+ 0.564264854137341/(x + 7.79746609204661)
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
f_apart = apart(f, full=True).evalf()
|
| 169 |
+
|
| 170 |
+
# There is a significant floating point error in this operation.
|
| 171 |
+
assert all_close(f_apart, expected, rtol=1e-3, atol=1e-5)
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def test_apart_undetermined_coeffs():
|
| 175 |
+
p = Poly(2*x - 3)
|
| 176 |
+
q = Poly(x**9 - x**8 - x**6 + x**5 - 2*x**2 + 3*x - 1)
|
| 177 |
+
r = (-x**7 - x**6 - x**5 + 4)/(x**8 - x**5 - 2*x + 1) + 1/(x - 1)
|
| 178 |
+
|
| 179 |
+
assert apart_undetermined_coeffs(p, q) == r
|
| 180 |
+
|
| 181 |
+
p = Poly(1, x, domain='ZZ[a,b]')
|
| 182 |
+
q = Poly((x + a)*(x + b), x, domain='ZZ[a,b]')
|
| 183 |
+
r = 1/((a - b)*(b + x)) - 1/((a - b)*(a + x))
|
| 184 |
+
|
| 185 |
+
assert apart_undetermined_coeffs(p, q) == r
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
def test_apart_list():
|
| 189 |
+
from sympy.utilities.iterables import numbered_symbols
|
| 190 |
+
def dummy_eq(i, j):
|
| 191 |
+
if type(i) in (list, tuple):
|
| 192 |
+
return all(dummy_eq(i, j) for i, j in zip(i, j))
|
| 193 |
+
return i == j or i.dummy_eq(j)
|
| 194 |
+
|
| 195 |
+
w0, w1, w2 = Symbol("w0"), Symbol("w1"), Symbol("w2")
|
| 196 |
+
_a = Dummy("a")
|
| 197 |
+
|
| 198 |
+
f = (-2*x - 2*x**2) / (3*x**2 - 6*x)
|
| 199 |
+
got = apart_list(f, x, dummies=numbered_symbols("w"))
|
| 200 |
+
ans = (-1, Poly(Rational(2, 3), x, domain='QQ'),
|
| 201 |
+
[(Poly(w0 - 2, w0, domain='ZZ'), Lambda(_a, 2), Lambda(_a, -_a + x), 1)])
|
| 202 |
+
assert dummy_eq(got, ans)
|
| 203 |
+
|
| 204 |
+
got = apart_list(2/(x**2-2), x, dummies=numbered_symbols("w"))
|
| 205 |
+
ans = (1, Poly(0, x, domain='ZZ'), [(Poly(w0**2 - 2, w0, domain='ZZ'),
|
| 206 |
+
Lambda(_a, _a/2),
|
| 207 |
+
Lambda(_a, -_a + x), 1)])
|
| 208 |
+
assert dummy_eq(got, ans)
|
| 209 |
+
|
| 210 |
+
f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
|
| 211 |
+
got = apart_list(f, x, dummies=numbered_symbols("w"))
|
| 212 |
+
ans = (1, Poly(0, x, domain='ZZ'),
|
| 213 |
+
[(Poly(w0 - 2, w0, domain='ZZ'), Lambda(_a, 4), Lambda(_a, -_a + x), 1),
|
| 214 |
+
(Poly(w1**2 - 1, w1, domain='ZZ'), Lambda(_a, -3*_a - 6), Lambda(_a, -_a + x), 2),
|
| 215 |
+
(Poly(w2 + 1, w2, domain='ZZ'), Lambda(_a, -4), Lambda(_a, -_a + x), 1)])
|
| 216 |
+
assert dummy_eq(got, ans)
|
| 217 |
+
|
| 218 |
+
|
| 219 |
+
def test_assemble_partfrac_list():
|
| 220 |
+
f = 36 / (x**5 - 2*x**4 - 2*x**3 + 4*x**2 + x - 2)
|
| 221 |
+
pfd = apart_list(f)
|
| 222 |
+
assert assemble_partfrac_list(pfd) == -4/(x + 1) - 3/(x + 1)**2 - 9/(x - 1)**2 + 4/(x - 2)
|
| 223 |
+
|
| 224 |
+
a = Dummy("a")
|
| 225 |
+
pfd = (1, Poly(0, x, domain='ZZ'), [([sqrt(2),-sqrt(2)], Lambda(a, a/2), Lambda(a, -a + x), 1)])
|
| 226 |
+
assert assemble_partfrac_list(pfd) == -1/(sqrt(2)*(x + sqrt(2))) + 1/(sqrt(2)*(x - sqrt(2)))
|
| 227 |
+
|
| 228 |
+
|
| 229 |
+
@XFAIL
|
| 230 |
+
def test_noncommutative_pseudomultivariate():
|
| 231 |
+
# apart doesn't go inside noncommutative expressions
|
| 232 |
+
class foo(Expr):
|
| 233 |
+
is_commutative=False
|
| 234 |
+
e = x/(x + x*y)
|
| 235 |
+
c = 1/(1 + y)
|
| 236 |
+
assert apart(e + foo(e)) == c + foo(c)
|
| 237 |
+
assert apart(e*foo(e)) == c*foo(c)
|
| 238 |
+
|
| 239 |
+
def test_noncommutative():
|
| 240 |
+
class foo(Expr):
|
| 241 |
+
is_commutative=False
|
| 242 |
+
e = x/(x + x*y)
|
| 243 |
+
c = 1/(1 + y)
|
| 244 |
+
assert apart(e + foo()) == c + foo()
|
| 245 |
+
|
| 246 |
+
def test_issue_5798():
|
| 247 |
+
assert apart(
|
| 248 |
+
2*x/(x**2 + 1) - (x - 1)/(2*(x**2 + 1)) + 1/(2*(x + 1)) - 2/x) == \
|
| 249 |
+
(3*x + 1)/(x**2 + 1)/2 + 1/(x + 1)/2 - 2/x
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_polyclasses.py
ADDED
|
@@ -0,0 +1,588 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for OO layer of several polynomial representations. """
|
| 2 |
+
|
| 3 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 4 |
+
from sympy.polys.domains import ZZ, QQ
|
| 5 |
+
from sympy.polys.polyclasses import DMP, DMF, ANP
|
| 6 |
+
from sympy.polys.polyerrors import (CoercionFailed, ExactQuotientFailed,
|
| 7 |
+
NotInvertible)
|
| 8 |
+
from sympy.polys.specialpolys import f_polys
|
| 9 |
+
from sympy.testing.pytest import raises, warns_deprecated_sympy
|
| 10 |
+
|
| 11 |
+
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = [ f.to_dense() for f in f_polys() ]
|
| 12 |
+
|
| 13 |
+
def test_DMP___init__():
|
| 14 |
+
f = DMP([[ZZ(0)], [], [ZZ(0), ZZ(1), ZZ(2)], [ZZ(3)]], ZZ)
|
| 15 |
+
|
| 16 |
+
assert f._rep == [[1, 2], [3]]
|
| 17 |
+
assert f.dom == ZZ
|
| 18 |
+
assert f.lev == 1
|
| 19 |
+
|
| 20 |
+
f = DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ, 1)
|
| 21 |
+
|
| 22 |
+
assert f._rep == [[1, 2], [3]]
|
| 23 |
+
assert f.dom == ZZ
|
| 24 |
+
assert f.lev == 1
|
| 25 |
+
|
| 26 |
+
f = DMP.from_dict({(1, 1): ZZ(1), (0, 0): ZZ(2)}, 1, ZZ)
|
| 27 |
+
|
| 28 |
+
assert f._rep == [[1, 0], [2]]
|
| 29 |
+
assert f.dom == ZZ
|
| 30 |
+
assert f.lev == 1
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def test_DMP_rep_deprecation():
|
| 34 |
+
f = DMP([1, 2, 3], ZZ)
|
| 35 |
+
|
| 36 |
+
with warns_deprecated_sympy():
|
| 37 |
+
assert f.rep == [1, 2, 3]
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
def test_DMP___eq__():
|
| 41 |
+
assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ) == \
|
| 42 |
+
DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ)
|
| 43 |
+
|
| 44 |
+
assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ) == \
|
| 45 |
+
DMP([[QQ(1), QQ(2)], [QQ(3)]], QQ)
|
| 46 |
+
assert DMP([[QQ(1), QQ(2)], [QQ(3)]], QQ) == \
|
| 47 |
+
DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ)
|
| 48 |
+
|
| 49 |
+
assert DMP([[[ZZ(1)]]], ZZ) != DMP([[ZZ(1)]], ZZ)
|
| 50 |
+
assert DMP([[ZZ(1)]], ZZ) != DMP([[[ZZ(1)]]], ZZ)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def test_DMP___bool__():
|
| 54 |
+
assert bool(DMP([[]], ZZ)) is False
|
| 55 |
+
assert bool(DMP([[ZZ(1)]], ZZ)) is True
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def test_DMP_to_dict():
|
| 59 |
+
f = DMP([[ZZ(3)], [], [ZZ(2)], [], [ZZ(8)]], ZZ)
|
| 60 |
+
|
| 61 |
+
assert f.to_dict() == \
|
| 62 |
+
{(4, 0): 3, (2, 0): 2, (0, 0): 8}
|
| 63 |
+
assert f.to_sympy_dict() == \
|
| 64 |
+
{(4, 0): ZZ.to_sympy(3), (2, 0): ZZ.to_sympy(2), (0, 0):
|
| 65 |
+
ZZ.to_sympy(8)}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def test_DMP_properties():
|
| 69 |
+
assert DMP([[]], ZZ).is_zero is True
|
| 70 |
+
assert DMP([[ZZ(1)]], ZZ).is_zero is False
|
| 71 |
+
|
| 72 |
+
assert DMP([[ZZ(1)]], ZZ).is_one is True
|
| 73 |
+
assert DMP([[ZZ(2)]], ZZ).is_one is False
|
| 74 |
+
|
| 75 |
+
assert DMP([[ZZ(1)]], ZZ).is_ground is True
|
| 76 |
+
assert DMP([[ZZ(1)], [ZZ(2)], [ZZ(1)]], ZZ).is_ground is False
|
| 77 |
+
|
| 78 |
+
assert DMP([[ZZ(1)], [ZZ(2), ZZ(0)], [ZZ(1), ZZ(0)]], ZZ).is_sqf is True
|
| 79 |
+
assert DMP([[ZZ(1)], [ZZ(2), ZZ(0)], [ZZ(1), ZZ(0), ZZ(0)]], ZZ).is_sqf is False
|
| 80 |
+
|
| 81 |
+
assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ).is_monic is True
|
| 82 |
+
assert DMP([[ZZ(2), ZZ(2)], [ZZ(3)]], ZZ).is_monic is False
|
| 83 |
+
|
| 84 |
+
assert DMP([[ZZ(1), ZZ(2)], [ZZ(3)]], ZZ).is_primitive is True
|
| 85 |
+
assert DMP([[ZZ(2), ZZ(4)], [ZZ(6)]], ZZ).is_primitive is False
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def test_DMP_arithmetics():
|
| 89 |
+
f = DMP([[ZZ(2)], [ZZ(2), ZZ(0)]], ZZ)
|
| 90 |
+
|
| 91 |
+
assert f.mul_ground(2) == DMP([[ZZ(4)], [ZZ(4), ZZ(0)]], ZZ)
|
| 92 |
+
assert f.quo_ground(2) == DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
|
| 93 |
+
|
| 94 |
+
raises(ExactQuotientFailed, lambda: f.exquo_ground(3))
|
| 95 |
+
|
| 96 |
+
f = DMP([[ZZ(-5)]], ZZ)
|
| 97 |
+
g = DMP([[ZZ(5)]], ZZ)
|
| 98 |
+
|
| 99 |
+
assert f.abs() == g
|
| 100 |
+
assert abs(f) == g
|
| 101 |
+
|
| 102 |
+
assert g.neg() == f
|
| 103 |
+
assert -g == f
|
| 104 |
+
|
| 105 |
+
h = DMP([[]], ZZ)
|
| 106 |
+
|
| 107 |
+
assert f.add(g) == h
|
| 108 |
+
assert f + g == h
|
| 109 |
+
assert g + f == h
|
| 110 |
+
assert f + 5 == h
|
| 111 |
+
assert 5 + f == h
|
| 112 |
+
|
| 113 |
+
h = DMP([[ZZ(-10)]], ZZ)
|
| 114 |
+
|
| 115 |
+
assert f.sub(g) == h
|
| 116 |
+
assert f - g == h
|
| 117 |
+
assert g - f == -h
|
| 118 |
+
assert f - 5 == h
|
| 119 |
+
assert 5 - f == -h
|
| 120 |
+
|
| 121 |
+
h = DMP([[ZZ(-25)]], ZZ)
|
| 122 |
+
|
| 123 |
+
assert f.mul(g) == h
|
| 124 |
+
assert f * g == h
|
| 125 |
+
assert g * f == h
|
| 126 |
+
assert f * 5 == h
|
| 127 |
+
assert 5 * f == h
|
| 128 |
+
|
| 129 |
+
h = DMP([[ZZ(25)]], ZZ)
|
| 130 |
+
|
| 131 |
+
assert f.sqr() == h
|
| 132 |
+
assert f.pow(2) == h
|
| 133 |
+
assert f**2 == h
|
| 134 |
+
|
| 135 |
+
raises(TypeError, lambda: f.pow('x'))
|
| 136 |
+
|
| 137 |
+
f = DMP([[ZZ(1)], [], [ZZ(1), ZZ(0), ZZ(0)]], ZZ)
|
| 138 |
+
g = DMP([[ZZ(2)], [ZZ(-2), ZZ(0)]], ZZ)
|
| 139 |
+
|
| 140 |
+
q = DMP([[ZZ(2)], [ZZ(2), ZZ(0)]], ZZ)
|
| 141 |
+
r = DMP([[ZZ(8), ZZ(0), ZZ(0)]], ZZ)
|
| 142 |
+
|
| 143 |
+
assert f.pdiv(g) == (q, r)
|
| 144 |
+
assert f.pquo(g) == q
|
| 145 |
+
assert f.prem(g) == r
|
| 146 |
+
|
| 147 |
+
raises(ExactQuotientFailed, lambda: f.pexquo(g))
|
| 148 |
+
|
| 149 |
+
f = DMP([[ZZ(1)], [], [ZZ(1), ZZ(0), ZZ(0)]], ZZ)
|
| 150 |
+
g = DMP([[ZZ(1)], [ZZ(-1), ZZ(0)]], ZZ)
|
| 151 |
+
|
| 152 |
+
q = DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
|
| 153 |
+
r = DMP([[ZZ(2), ZZ(0), ZZ(0)]], ZZ)
|
| 154 |
+
|
| 155 |
+
assert f.div(g) == (q, r)
|
| 156 |
+
assert f.quo(g) == q
|
| 157 |
+
assert f.rem(g) == r
|
| 158 |
+
|
| 159 |
+
assert divmod(f, g) == (q, r)
|
| 160 |
+
assert f // g == q
|
| 161 |
+
assert f % g == r
|
| 162 |
+
|
| 163 |
+
raises(ExactQuotientFailed, lambda: f.exquo(g))
|
| 164 |
+
|
| 165 |
+
f = DMP([ZZ(1), ZZ(0), ZZ(-1)], ZZ)
|
| 166 |
+
g = DMP([ZZ(2), ZZ(-2)], ZZ)
|
| 167 |
+
|
| 168 |
+
q = DMP([], ZZ)
|
| 169 |
+
r = f
|
| 170 |
+
|
| 171 |
+
pq = DMP([ZZ(2), ZZ(2)], ZZ)
|
| 172 |
+
pr = DMP([], ZZ)
|
| 173 |
+
|
| 174 |
+
assert f.div(g) == (q, r)
|
| 175 |
+
assert f.quo(g) == q
|
| 176 |
+
assert f.rem(g) == r
|
| 177 |
+
|
| 178 |
+
assert divmod(f, g) == (q, r)
|
| 179 |
+
assert f // g == q
|
| 180 |
+
assert f % g == r
|
| 181 |
+
|
| 182 |
+
raises(ExactQuotientFailed, lambda: f.exquo(g))
|
| 183 |
+
|
| 184 |
+
assert f.pdiv(g) == (pq, pr)
|
| 185 |
+
assert f.pquo(g) == pq
|
| 186 |
+
assert f.prem(g) == pr
|
| 187 |
+
assert f.pexquo(g) == pq
|
| 188 |
+
|
| 189 |
+
|
| 190 |
+
def test_DMP_functionality():
|
| 191 |
+
f = DMP([[ZZ(1)], [ZZ(2), ZZ(0)], [ZZ(1), ZZ(0), ZZ(0)]], ZZ)
|
| 192 |
+
g = DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ)
|
| 193 |
+
h = DMP([[ZZ(1)]], ZZ)
|
| 194 |
+
|
| 195 |
+
assert f.degree() == 2
|
| 196 |
+
assert f.degree_list() == (2, 2)
|
| 197 |
+
assert f.total_degree() == 2
|
| 198 |
+
|
| 199 |
+
assert f.LC() == ZZ(1)
|
| 200 |
+
assert f.TC() == ZZ(0)
|
| 201 |
+
assert f.nth(1, 1) == ZZ(2)
|
| 202 |
+
|
| 203 |
+
raises(TypeError, lambda: f.nth(0, 'x'))
|
| 204 |
+
|
| 205 |
+
assert f.max_norm() == 2
|
| 206 |
+
assert f.l1_norm() == 4
|
| 207 |
+
|
| 208 |
+
u = DMP([[ZZ(2)], [ZZ(2), ZZ(0)]], ZZ)
|
| 209 |
+
|
| 210 |
+
assert f.diff(m=1, j=0) == u
|
| 211 |
+
assert f.diff(m=1, j=1) == u
|
| 212 |
+
|
| 213 |
+
raises(TypeError, lambda: f.diff(m='x', j=0))
|
| 214 |
+
|
| 215 |
+
u = DMP([ZZ(1), ZZ(2), ZZ(1)], ZZ)
|
| 216 |
+
v = DMP([ZZ(1), ZZ(2), ZZ(1)], ZZ)
|
| 217 |
+
|
| 218 |
+
assert f.eval(a=1, j=0) == u
|
| 219 |
+
assert f.eval(a=1, j=1) == v
|
| 220 |
+
|
| 221 |
+
assert f.eval(1).eval(1) == ZZ(4)
|
| 222 |
+
|
| 223 |
+
assert f.cofactors(g) == (g, g, h)
|
| 224 |
+
assert f.gcd(g) == g
|
| 225 |
+
assert f.lcm(g) == f
|
| 226 |
+
|
| 227 |
+
u = DMP([[QQ(45), QQ(30), QQ(5)]], QQ)
|
| 228 |
+
v = DMP([[QQ(1), QQ(2, 3), QQ(1, 9)]], QQ)
|
| 229 |
+
|
| 230 |
+
assert u.monic() == v
|
| 231 |
+
|
| 232 |
+
assert (4*f).content() == ZZ(4)
|
| 233 |
+
assert (4*f).primitive() == (ZZ(4), f)
|
| 234 |
+
|
| 235 |
+
f = DMP([QQ(1,3), QQ(1)], QQ)
|
| 236 |
+
g = DMP([QQ(1,7), QQ(1)], QQ)
|
| 237 |
+
|
| 238 |
+
assert f.cancel(g) == f.cancel(g, include=True) == (
|
| 239 |
+
DMP([QQ(7), QQ(21)], QQ),
|
| 240 |
+
DMP([QQ(3), QQ(21)], QQ)
|
| 241 |
+
)
|
| 242 |
+
assert f.cancel(g, include=False) == (
|
| 243 |
+
QQ(7),
|
| 244 |
+
QQ(3),
|
| 245 |
+
DMP([QQ(1), QQ(3)], QQ),
|
| 246 |
+
DMP([QQ(1), QQ(7)], QQ)
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
f = DMP([[ZZ(1)], [ZZ(2)], [ZZ(3)], [ZZ(4)], [ZZ(5)], [ZZ(6)]], ZZ)
|
| 250 |
+
|
| 251 |
+
assert f.trunc(3) == DMP([[ZZ(1)], [ZZ(-1)], [], [ZZ(1)], [ZZ(-1)], []], ZZ)
|
| 252 |
+
|
| 253 |
+
f = DMP(f_4, ZZ)
|
| 254 |
+
|
| 255 |
+
assert f.sqf_part() == -f
|
| 256 |
+
assert f.sqf_list() == (ZZ(-1), [(-f, 1)])
|
| 257 |
+
|
| 258 |
+
f = DMP([[ZZ(-1)], [], [], [ZZ(5)]], ZZ)
|
| 259 |
+
g = DMP([[ZZ(3), ZZ(1)], [], []], ZZ)
|
| 260 |
+
h = DMP([[ZZ(45), ZZ(30), ZZ(5)]], ZZ)
|
| 261 |
+
|
| 262 |
+
r = DMP([ZZ(675), ZZ(675), ZZ(225), ZZ(25)], ZZ)
|
| 263 |
+
|
| 264 |
+
assert f.subresultants(g) == [f, g, h]
|
| 265 |
+
assert f.resultant(g) == r
|
| 266 |
+
|
| 267 |
+
f = DMP([ZZ(1), ZZ(3), ZZ(9), ZZ(-13)], ZZ)
|
| 268 |
+
|
| 269 |
+
assert f.discriminant() == -11664
|
| 270 |
+
|
| 271 |
+
f = DMP([QQ(2), QQ(0)], QQ)
|
| 272 |
+
g = DMP([QQ(1), QQ(0), QQ(-16)], QQ)
|
| 273 |
+
|
| 274 |
+
s = DMP([QQ(1, 32), QQ(0)], QQ)
|
| 275 |
+
t = DMP([QQ(-1, 16)], QQ)
|
| 276 |
+
h = DMP([QQ(1)], QQ)
|
| 277 |
+
|
| 278 |
+
assert f.half_gcdex(g) == (s, h)
|
| 279 |
+
assert f.gcdex(g) == (s, t, h)
|
| 280 |
+
|
| 281 |
+
assert f.invert(g) == s
|
| 282 |
+
|
| 283 |
+
f = DMP([[QQ(1)], [QQ(2)], [QQ(3)]], QQ)
|
| 284 |
+
|
| 285 |
+
raises(ValueError, lambda: f.half_gcdex(f))
|
| 286 |
+
raises(ValueError, lambda: f.gcdex(f))
|
| 287 |
+
|
| 288 |
+
raises(ValueError, lambda: f.invert(f))
|
| 289 |
+
|
| 290 |
+
f = DMP(ZZ.map([1, 0, 20, 0, 150, 0, 500, 0, 625, -2, 0, -10, 9]), ZZ)
|
| 291 |
+
g = DMP([ZZ(1), ZZ(0), ZZ(0), ZZ(-2), ZZ(9)], ZZ)
|
| 292 |
+
h = DMP([ZZ(1), ZZ(0), ZZ(5), ZZ(0)], ZZ)
|
| 293 |
+
|
| 294 |
+
assert g.compose(h) == f
|
| 295 |
+
assert f.decompose() == [g, h]
|
| 296 |
+
|
| 297 |
+
f = DMP([[QQ(1)], [QQ(2)], [QQ(3)]], QQ)
|
| 298 |
+
|
| 299 |
+
raises(ValueError, lambda: f.decompose())
|
| 300 |
+
raises(ValueError, lambda: f.sturm())
|
| 301 |
+
|
| 302 |
+
|
| 303 |
+
def test_DMP_exclude():
|
| 304 |
+
f = [[[[[[[[[[[[[[[[[[[[[[[[[[ZZ(1)]], [[]]]]]]]]]]]]]]]]]]]]]]]]]]
|
| 305 |
+
J = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
|
| 306 |
+
18, 19, 20, 21, 22, 24, 25]
|
| 307 |
+
|
| 308 |
+
assert DMP(f, ZZ).exclude() == (J, DMP([ZZ(1), ZZ(0)], ZZ))
|
| 309 |
+
assert DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ).exclude() ==\
|
| 310 |
+
([], DMP([[ZZ(1)], [ZZ(1), ZZ(0)]], ZZ))
|
| 311 |
+
|
| 312 |
+
|
| 313 |
+
def test_DMF__init__():
|
| 314 |
+
f = DMF(([[0], [], [0, 1, 2], [3]], [[1, 2, 3]]), ZZ)
|
| 315 |
+
|
| 316 |
+
assert f.num == [[1, 2], [3]]
|
| 317 |
+
assert f.den == [[1, 2, 3]]
|
| 318 |
+
assert f.lev == 1
|
| 319 |
+
assert f.dom == ZZ
|
| 320 |
+
|
| 321 |
+
f = DMF(([[1, 2], [3]], [[1, 2, 3]]), ZZ, 1)
|
| 322 |
+
|
| 323 |
+
assert f.num == [[1, 2], [3]]
|
| 324 |
+
assert f.den == [[1, 2, 3]]
|
| 325 |
+
assert f.lev == 1
|
| 326 |
+
assert f.dom == ZZ
|
| 327 |
+
|
| 328 |
+
f = DMF(([[-1], [-2]], [[3], [-4]]), ZZ)
|
| 329 |
+
|
| 330 |
+
assert f.num == [[-1], [-2]]
|
| 331 |
+
assert f.den == [[3], [-4]]
|
| 332 |
+
assert f.lev == 1
|
| 333 |
+
assert f.dom == ZZ
|
| 334 |
+
|
| 335 |
+
f = DMF(([[1], [2]], [[-3], [4]]), ZZ)
|
| 336 |
+
|
| 337 |
+
assert f.num == [[-1], [-2]]
|
| 338 |
+
assert f.den == [[3], [-4]]
|
| 339 |
+
assert f.lev == 1
|
| 340 |
+
assert f.dom == ZZ
|
| 341 |
+
|
| 342 |
+
f = DMF(([[1], [2]], [[-3], [4]]), ZZ)
|
| 343 |
+
|
| 344 |
+
assert f.num == [[-1], [-2]]
|
| 345 |
+
assert f.den == [[3], [-4]]
|
| 346 |
+
assert f.lev == 1
|
| 347 |
+
assert f.dom == ZZ
|
| 348 |
+
|
| 349 |
+
f = DMF(([[]], [[-3], [4]]), ZZ)
|
| 350 |
+
|
| 351 |
+
assert f.num == [[]]
|
| 352 |
+
assert f.den == [[1]]
|
| 353 |
+
assert f.lev == 1
|
| 354 |
+
assert f.dom == ZZ
|
| 355 |
+
|
| 356 |
+
f = DMF(17, ZZ, 1)
|
| 357 |
+
|
| 358 |
+
assert f.num == [[17]]
|
| 359 |
+
assert f.den == [[1]]
|
| 360 |
+
assert f.lev == 1
|
| 361 |
+
assert f.dom == ZZ
|
| 362 |
+
|
| 363 |
+
f = DMF(([[1], [2]]), ZZ)
|
| 364 |
+
|
| 365 |
+
assert f.num == [[1], [2]]
|
| 366 |
+
assert f.den == [[1]]
|
| 367 |
+
assert f.lev == 1
|
| 368 |
+
assert f.dom == ZZ
|
| 369 |
+
|
| 370 |
+
f = DMF([[0], [], [0, 1, 2], [3]], ZZ)
|
| 371 |
+
|
| 372 |
+
assert f.num == [[1, 2], [3]]
|
| 373 |
+
assert f.den == [[1]]
|
| 374 |
+
assert f.lev == 1
|
| 375 |
+
assert f.dom == ZZ
|
| 376 |
+
|
| 377 |
+
f = DMF({(1, 1): 1, (0, 0): 2}, ZZ, 1)
|
| 378 |
+
|
| 379 |
+
assert f.num == [[1, 0], [2]]
|
| 380 |
+
assert f.den == [[1]]
|
| 381 |
+
assert f.lev == 1
|
| 382 |
+
assert f.dom == ZZ
|
| 383 |
+
|
| 384 |
+
f = DMF(([[QQ(1)], [QQ(2)]], [[-QQ(3)], [QQ(4)]]), QQ)
|
| 385 |
+
|
| 386 |
+
assert f.num == [[-QQ(1)], [-QQ(2)]]
|
| 387 |
+
assert f.den == [[QQ(3)], [-QQ(4)]]
|
| 388 |
+
assert f.lev == 1
|
| 389 |
+
assert f.dom == QQ
|
| 390 |
+
|
| 391 |
+
f = DMF(([[QQ(1, 5)], [QQ(2, 5)]], [[-QQ(3, 7)], [QQ(4, 7)]]), QQ)
|
| 392 |
+
|
| 393 |
+
assert f.num == [[-QQ(7)], [-QQ(14)]]
|
| 394 |
+
assert f.den == [[QQ(15)], [-QQ(20)]]
|
| 395 |
+
assert f.lev == 1
|
| 396 |
+
assert f.dom == QQ
|
| 397 |
+
|
| 398 |
+
raises(ValueError, lambda: DMF(([1], [[1]]), ZZ))
|
| 399 |
+
raises(ZeroDivisionError, lambda: DMF(([1], []), ZZ))
|
| 400 |
+
|
| 401 |
+
|
| 402 |
+
def test_DMF__bool__():
|
| 403 |
+
assert bool(DMF([[]], ZZ)) is False
|
| 404 |
+
assert bool(DMF([[1]], ZZ)) is True
|
| 405 |
+
|
| 406 |
+
|
| 407 |
+
def test_DMF_properties():
|
| 408 |
+
assert DMF([[]], ZZ).is_zero is True
|
| 409 |
+
assert DMF([[]], ZZ).is_one is False
|
| 410 |
+
|
| 411 |
+
assert DMF([[1]], ZZ).is_zero is False
|
| 412 |
+
assert DMF([[1]], ZZ).is_one is True
|
| 413 |
+
|
| 414 |
+
assert DMF(([[1]], [[2]]), ZZ).is_one is False
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
def test_DMF_arithmetics():
|
| 418 |
+
f = DMF([[7], [-9]], ZZ)
|
| 419 |
+
g = DMF([[-7], [9]], ZZ)
|
| 420 |
+
|
| 421 |
+
assert f.neg() == -f == g
|
| 422 |
+
|
| 423 |
+
f = DMF(([[1]], [[1], []]), ZZ)
|
| 424 |
+
g = DMF(([[1]], [[1, 0]]), ZZ)
|
| 425 |
+
|
| 426 |
+
h = DMF(([[1], [1, 0]], [[1, 0], []]), ZZ)
|
| 427 |
+
|
| 428 |
+
assert f.add(g) == f + g == h
|
| 429 |
+
assert g.add(f) == g + f == h
|
| 430 |
+
|
| 431 |
+
h = DMF(([[-1], [1, 0]], [[1, 0], []]), ZZ)
|
| 432 |
+
|
| 433 |
+
assert f.sub(g) == f - g == h
|
| 434 |
+
|
| 435 |
+
h = DMF(([[1]], [[1, 0], []]), ZZ)
|
| 436 |
+
|
| 437 |
+
assert f.mul(g) == f*g == h
|
| 438 |
+
assert g.mul(f) == g*f == h
|
| 439 |
+
|
| 440 |
+
h = DMF(([[1, 0]], [[1], []]), ZZ)
|
| 441 |
+
|
| 442 |
+
assert f.quo(g) == f/g == h
|
| 443 |
+
|
| 444 |
+
h = DMF(([[1]], [[1], [], [], []]), ZZ)
|
| 445 |
+
|
| 446 |
+
assert f.pow(3) == f**3 == h
|
| 447 |
+
|
| 448 |
+
h = DMF(([[1]], [[1, 0, 0, 0]]), ZZ)
|
| 449 |
+
|
| 450 |
+
assert g.pow(3) == g**3 == h
|
| 451 |
+
|
| 452 |
+
h = DMF(([[1, 0]], [[1]]), ZZ)
|
| 453 |
+
|
| 454 |
+
assert g.pow(-1) == g**-1 == h
|
| 455 |
+
|
| 456 |
+
|
| 457 |
+
def test_ANP___init__():
|
| 458 |
+
rep = [QQ(1), QQ(1)]
|
| 459 |
+
mod = [QQ(1), QQ(0), QQ(1)]
|
| 460 |
+
|
| 461 |
+
f = ANP(rep, mod, QQ)
|
| 462 |
+
|
| 463 |
+
assert f.to_list() == [QQ(1), QQ(1)]
|
| 464 |
+
assert f.mod_to_list() == [QQ(1), QQ(0), QQ(1)]
|
| 465 |
+
assert f.dom == QQ
|
| 466 |
+
|
| 467 |
+
rep = {1: QQ(1), 0: QQ(1)}
|
| 468 |
+
mod = {2: QQ(1), 0: QQ(1)}
|
| 469 |
+
|
| 470 |
+
f = ANP(rep, mod, QQ)
|
| 471 |
+
|
| 472 |
+
assert f.to_list() == [QQ(1), QQ(1)]
|
| 473 |
+
assert f.mod_to_list() == [QQ(1), QQ(0), QQ(1)]
|
| 474 |
+
assert f.dom == QQ
|
| 475 |
+
|
| 476 |
+
f = ANP(1, mod, QQ)
|
| 477 |
+
|
| 478 |
+
assert f.to_list() == [QQ(1)]
|
| 479 |
+
assert f.mod_to_list() == [QQ(1), QQ(0), QQ(1)]
|
| 480 |
+
assert f.dom == QQ
|
| 481 |
+
|
| 482 |
+
f = ANP([1, 0.5], mod, QQ)
|
| 483 |
+
|
| 484 |
+
assert all(QQ.of_type(a) for a in f.to_list())
|
| 485 |
+
|
| 486 |
+
raises(CoercionFailed, lambda: ANP([sqrt(2)], mod, QQ))
|
| 487 |
+
|
| 488 |
+
|
| 489 |
+
def test_ANP___eq__():
|
| 490 |
+
a = ANP([QQ(1), QQ(1)], [QQ(1), QQ(0), QQ(1)], QQ)
|
| 491 |
+
b = ANP([QQ(1), QQ(1)], [QQ(1), QQ(0), QQ(2)], QQ)
|
| 492 |
+
|
| 493 |
+
assert (a == a) is True
|
| 494 |
+
assert (a != a) is False
|
| 495 |
+
|
| 496 |
+
assert (a == b) is False
|
| 497 |
+
assert (a != b) is True
|
| 498 |
+
|
| 499 |
+
b = ANP([QQ(1), QQ(2)], [QQ(1), QQ(0), QQ(1)], QQ)
|
| 500 |
+
|
| 501 |
+
assert (a == b) is False
|
| 502 |
+
assert (a != b) is True
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
def test_ANP___bool__():
|
| 506 |
+
assert bool(ANP([], [QQ(1), QQ(0), QQ(1)], QQ)) is False
|
| 507 |
+
assert bool(ANP([QQ(1)], [QQ(1), QQ(0), QQ(1)], QQ)) is True
|
| 508 |
+
|
| 509 |
+
|
| 510 |
+
def test_ANP_properties():
|
| 511 |
+
mod = [QQ(1), QQ(0), QQ(1)]
|
| 512 |
+
|
| 513 |
+
assert ANP([QQ(0)], mod, QQ).is_zero is True
|
| 514 |
+
assert ANP([QQ(1)], mod, QQ).is_zero is False
|
| 515 |
+
|
| 516 |
+
assert ANP([QQ(1)], mod, QQ).is_one is True
|
| 517 |
+
assert ANP([QQ(2)], mod, QQ).is_one is False
|
| 518 |
+
|
| 519 |
+
|
| 520 |
+
def test_ANP_arithmetics():
|
| 521 |
+
mod = [QQ(1), QQ(0), QQ(0), QQ(-2)]
|
| 522 |
+
|
| 523 |
+
a = ANP([QQ(2), QQ(-1), QQ(1)], mod, QQ)
|
| 524 |
+
b = ANP([QQ(1), QQ(2)], mod, QQ)
|
| 525 |
+
|
| 526 |
+
c = ANP([QQ(-2), QQ(1), QQ(-1)], mod, QQ)
|
| 527 |
+
|
| 528 |
+
assert a.neg() == -a == c
|
| 529 |
+
|
| 530 |
+
c = ANP([QQ(2), QQ(0), QQ(3)], mod, QQ)
|
| 531 |
+
|
| 532 |
+
assert a.add(b) == a + b == c
|
| 533 |
+
assert b.add(a) == b + a == c
|
| 534 |
+
|
| 535 |
+
c = ANP([QQ(2), QQ(-2), QQ(-1)], mod, QQ)
|
| 536 |
+
|
| 537 |
+
assert a.sub(b) == a - b == c
|
| 538 |
+
|
| 539 |
+
c = ANP([QQ(-2), QQ(2), QQ(1)], mod, QQ)
|
| 540 |
+
|
| 541 |
+
assert b.sub(a) == b - a == c
|
| 542 |
+
|
| 543 |
+
c = ANP([QQ(3), QQ(-1), QQ(6)], mod, QQ)
|
| 544 |
+
|
| 545 |
+
assert a.mul(b) == a*b == c
|
| 546 |
+
assert b.mul(a) == b*a == c
|
| 547 |
+
|
| 548 |
+
c = ANP([QQ(-1, 43), QQ(9, 43), QQ(5, 43)], mod, QQ)
|
| 549 |
+
|
| 550 |
+
assert a.pow(0) == a**(0) == ANP(1, mod, QQ)
|
| 551 |
+
assert a.pow(1) == a**(1) == a
|
| 552 |
+
|
| 553 |
+
assert a.pow(-1) == a**(-1) == c
|
| 554 |
+
|
| 555 |
+
assert a.quo(a) == a.mul(a.pow(-1)) == a*a**(-1) == ANP(1, mod, QQ)
|
| 556 |
+
|
| 557 |
+
c = ANP([], [1, 0, 0, -2], QQ)
|
| 558 |
+
r1 = a.rem(b)
|
| 559 |
+
|
| 560 |
+
(q, r2) = a.div(b)
|
| 561 |
+
|
| 562 |
+
assert r1 == r2 == c == a % b
|
| 563 |
+
|
| 564 |
+
raises(NotInvertible, lambda: a.div(c))
|
| 565 |
+
raises(NotInvertible, lambda: a.rem(c))
|
| 566 |
+
|
| 567 |
+
# Comparison with "hard-coded" value fails despite looking identical
|
| 568 |
+
# from sympy import Rational
|
| 569 |
+
# c = ANP([Rational(11, 10), Rational(-1, 5), Rational(-3, 5)], [1, 0, 0, -2], QQ)
|
| 570 |
+
|
| 571 |
+
assert q == a/b # == c
|
| 572 |
+
|
| 573 |
+
def test_ANP_unify():
|
| 574 |
+
mod_z = [ZZ(1), ZZ(0), ZZ(-2)]
|
| 575 |
+
mod_q = [QQ(1), QQ(0), QQ(-2)]
|
| 576 |
+
|
| 577 |
+
a = ANP([QQ(1)], mod_q, QQ)
|
| 578 |
+
b = ANP([ZZ(1)], mod_z, ZZ)
|
| 579 |
+
|
| 580 |
+
assert a.unify(b)[0] == QQ
|
| 581 |
+
assert b.unify(a)[0] == QQ
|
| 582 |
+
assert a.unify(a)[0] == QQ
|
| 583 |
+
assert b.unify(b)[0] == ZZ
|
| 584 |
+
|
| 585 |
+
assert a.unify_ANP(b)[-1] == QQ
|
| 586 |
+
assert b.unify_ANP(a)[-1] == QQ
|
| 587 |
+
assert a.unify_ANP(a)[-1] == QQ
|
| 588 |
+
assert b.unify_ANP(b)[-1] == ZZ
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_polyoptions.py
ADDED
|
@@ -0,0 +1,485 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for options manager for :class:`Poly` and public API functions. """
|
| 2 |
+
|
| 3 |
+
from sympy.polys.polyoptions import (
|
| 4 |
+
Options, Expand, Gens, Wrt, Sort, Order, Field, Greedy, Domain,
|
| 5 |
+
Split, Gaussian, Extension, Modulus, Symmetric, Strict, Auto,
|
| 6 |
+
Frac, Formal, Polys, Include, All, Gen, Symbols, Method)
|
| 7 |
+
|
| 8 |
+
from sympy.polys.orderings import lex
|
| 9 |
+
from sympy.polys.domains import FF, GF, ZZ, QQ, QQ_I, RR, CC, EX
|
| 10 |
+
|
| 11 |
+
from sympy.polys.polyerrors import OptionError, GeneratorsError
|
| 12 |
+
|
| 13 |
+
from sympy.core.numbers import (I, Integer)
|
| 14 |
+
from sympy.core.symbol import Symbol
|
| 15 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 16 |
+
from sympy.testing.pytest import raises
|
| 17 |
+
from sympy.abc import x, y, z
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def test_Options_clone():
|
| 21 |
+
opt = Options((x, y, z), {'domain': 'ZZ'})
|
| 22 |
+
|
| 23 |
+
assert opt.gens == (x, y, z)
|
| 24 |
+
assert opt.domain == ZZ
|
| 25 |
+
assert ('order' in opt) is False
|
| 26 |
+
|
| 27 |
+
new_opt = opt.clone({'gens': (x, y), 'order': 'lex'})
|
| 28 |
+
|
| 29 |
+
assert opt.gens == (x, y, z)
|
| 30 |
+
assert opt.domain == ZZ
|
| 31 |
+
assert ('order' in opt) is False
|
| 32 |
+
|
| 33 |
+
assert new_opt.gens == (x, y)
|
| 34 |
+
assert new_opt.domain == ZZ
|
| 35 |
+
assert ('order' in new_opt) is True
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def test_Expand_preprocess():
|
| 39 |
+
assert Expand.preprocess(False) is False
|
| 40 |
+
assert Expand.preprocess(True) is True
|
| 41 |
+
|
| 42 |
+
assert Expand.preprocess(0) is False
|
| 43 |
+
assert Expand.preprocess(1) is True
|
| 44 |
+
|
| 45 |
+
raises(OptionError, lambda: Expand.preprocess(x))
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def test_Expand_postprocess():
|
| 49 |
+
opt = {'expand': True}
|
| 50 |
+
Expand.postprocess(opt)
|
| 51 |
+
|
| 52 |
+
assert opt == {'expand': True}
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def test_Gens_preprocess():
|
| 56 |
+
assert Gens.preprocess((None,)) == ()
|
| 57 |
+
assert Gens.preprocess((x, y, z)) == (x, y, z)
|
| 58 |
+
assert Gens.preprocess(((x, y, z),)) == (x, y, z)
|
| 59 |
+
|
| 60 |
+
a = Symbol('a', commutative=False)
|
| 61 |
+
|
| 62 |
+
raises(GeneratorsError, lambda: Gens.preprocess((x, x, y)))
|
| 63 |
+
raises(GeneratorsError, lambda: Gens.preprocess((x, y, a)))
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def test_Gens_postprocess():
|
| 67 |
+
opt = {'gens': (x, y)}
|
| 68 |
+
Gens.postprocess(opt)
|
| 69 |
+
|
| 70 |
+
assert opt == {'gens': (x, y)}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_Wrt_preprocess():
|
| 74 |
+
assert Wrt.preprocess(x) == ['x']
|
| 75 |
+
assert Wrt.preprocess('') == []
|
| 76 |
+
assert Wrt.preprocess(' ') == []
|
| 77 |
+
assert Wrt.preprocess('x,y') == ['x', 'y']
|
| 78 |
+
assert Wrt.preprocess('x y') == ['x', 'y']
|
| 79 |
+
assert Wrt.preprocess('x, y') == ['x', 'y']
|
| 80 |
+
assert Wrt.preprocess('x , y') == ['x', 'y']
|
| 81 |
+
assert Wrt.preprocess(' x, y') == ['x', 'y']
|
| 82 |
+
assert Wrt.preprocess(' x, y') == ['x', 'y']
|
| 83 |
+
assert Wrt.preprocess([x, y]) == ['x', 'y']
|
| 84 |
+
|
| 85 |
+
raises(OptionError, lambda: Wrt.preprocess(','))
|
| 86 |
+
raises(OptionError, lambda: Wrt.preprocess(0))
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
def test_Wrt_postprocess():
|
| 90 |
+
opt = {'wrt': ['x']}
|
| 91 |
+
Wrt.postprocess(opt)
|
| 92 |
+
|
| 93 |
+
assert opt == {'wrt': ['x']}
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def test_Sort_preprocess():
|
| 97 |
+
assert Sort.preprocess([x, y, z]) == ['x', 'y', 'z']
|
| 98 |
+
assert Sort.preprocess((x, y, z)) == ['x', 'y', 'z']
|
| 99 |
+
|
| 100 |
+
assert Sort.preprocess('x > y > z') == ['x', 'y', 'z']
|
| 101 |
+
assert Sort.preprocess('x>y>z') == ['x', 'y', 'z']
|
| 102 |
+
|
| 103 |
+
raises(OptionError, lambda: Sort.preprocess(0))
|
| 104 |
+
raises(OptionError, lambda: Sort.preprocess({x, y, z}))
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def test_Sort_postprocess():
|
| 108 |
+
opt = {'sort': 'x > y'}
|
| 109 |
+
Sort.postprocess(opt)
|
| 110 |
+
|
| 111 |
+
assert opt == {'sort': 'x > y'}
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def test_Order_preprocess():
|
| 115 |
+
assert Order.preprocess('lex') == lex
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def test_Order_postprocess():
|
| 119 |
+
opt = {'order': True}
|
| 120 |
+
Order.postprocess(opt)
|
| 121 |
+
|
| 122 |
+
assert opt == {'order': True}
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def test_Field_preprocess():
|
| 126 |
+
assert Field.preprocess(False) is False
|
| 127 |
+
assert Field.preprocess(True) is True
|
| 128 |
+
|
| 129 |
+
assert Field.preprocess(0) is False
|
| 130 |
+
assert Field.preprocess(1) is True
|
| 131 |
+
|
| 132 |
+
raises(OptionError, lambda: Field.preprocess(x))
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
def test_Field_postprocess():
|
| 136 |
+
opt = {'field': True}
|
| 137 |
+
Field.postprocess(opt)
|
| 138 |
+
|
| 139 |
+
assert opt == {'field': True}
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def test_Greedy_preprocess():
|
| 143 |
+
assert Greedy.preprocess(False) is False
|
| 144 |
+
assert Greedy.preprocess(True) is True
|
| 145 |
+
|
| 146 |
+
assert Greedy.preprocess(0) is False
|
| 147 |
+
assert Greedy.preprocess(1) is True
|
| 148 |
+
|
| 149 |
+
raises(OptionError, lambda: Greedy.preprocess(x))
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
def test_Greedy_postprocess():
|
| 153 |
+
opt = {'greedy': True}
|
| 154 |
+
Greedy.postprocess(opt)
|
| 155 |
+
|
| 156 |
+
assert opt == {'greedy': True}
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
def test_Domain_preprocess():
|
| 160 |
+
assert Domain.preprocess(ZZ) == ZZ
|
| 161 |
+
assert Domain.preprocess(QQ) == QQ
|
| 162 |
+
assert Domain.preprocess(EX) == EX
|
| 163 |
+
assert Domain.preprocess(FF(2)) == FF(2)
|
| 164 |
+
assert Domain.preprocess(ZZ[x, y]) == ZZ[x, y]
|
| 165 |
+
|
| 166 |
+
assert Domain.preprocess('Z') == ZZ
|
| 167 |
+
assert Domain.preprocess('Q') == QQ
|
| 168 |
+
|
| 169 |
+
assert Domain.preprocess('ZZ') == ZZ
|
| 170 |
+
assert Domain.preprocess('QQ') == QQ
|
| 171 |
+
|
| 172 |
+
assert Domain.preprocess('EX') == EX
|
| 173 |
+
|
| 174 |
+
assert Domain.preprocess('FF(23)') == FF(23)
|
| 175 |
+
assert Domain.preprocess('GF(23)') == GF(23)
|
| 176 |
+
|
| 177 |
+
raises(OptionError, lambda: Domain.preprocess('Z[]'))
|
| 178 |
+
|
| 179 |
+
assert Domain.preprocess('Z[x]') == ZZ[x]
|
| 180 |
+
assert Domain.preprocess('Q[x]') == QQ[x]
|
| 181 |
+
assert Domain.preprocess('R[x]') == RR[x]
|
| 182 |
+
assert Domain.preprocess('C[x]') == CC[x]
|
| 183 |
+
|
| 184 |
+
assert Domain.preprocess('ZZ[x]') == ZZ[x]
|
| 185 |
+
assert Domain.preprocess('QQ[x]') == QQ[x]
|
| 186 |
+
assert Domain.preprocess('RR[x]') == RR[x]
|
| 187 |
+
assert Domain.preprocess('CC[x]') == CC[x]
|
| 188 |
+
|
| 189 |
+
assert Domain.preprocess('Z[x,y]') == ZZ[x, y]
|
| 190 |
+
assert Domain.preprocess('Q[x,y]') == QQ[x, y]
|
| 191 |
+
assert Domain.preprocess('R[x,y]') == RR[x, y]
|
| 192 |
+
assert Domain.preprocess('C[x,y]') == CC[x, y]
|
| 193 |
+
|
| 194 |
+
assert Domain.preprocess('ZZ[x,y]') == ZZ[x, y]
|
| 195 |
+
assert Domain.preprocess('QQ[x,y]') == QQ[x, y]
|
| 196 |
+
assert Domain.preprocess('RR[x,y]') == RR[x, y]
|
| 197 |
+
assert Domain.preprocess('CC[x,y]') == CC[x, y]
|
| 198 |
+
|
| 199 |
+
raises(OptionError, lambda: Domain.preprocess('Z()'))
|
| 200 |
+
|
| 201 |
+
assert Domain.preprocess('Z(x)') == ZZ.frac_field(x)
|
| 202 |
+
assert Domain.preprocess('Q(x)') == QQ.frac_field(x)
|
| 203 |
+
|
| 204 |
+
assert Domain.preprocess('ZZ(x)') == ZZ.frac_field(x)
|
| 205 |
+
assert Domain.preprocess('QQ(x)') == QQ.frac_field(x)
|
| 206 |
+
|
| 207 |
+
assert Domain.preprocess('Z(x,y)') == ZZ.frac_field(x, y)
|
| 208 |
+
assert Domain.preprocess('Q(x,y)') == QQ.frac_field(x, y)
|
| 209 |
+
|
| 210 |
+
assert Domain.preprocess('ZZ(x,y)') == ZZ.frac_field(x, y)
|
| 211 |
+
assert Domain.preprocess('QQ(x,y)') == QQ.frac_field(x, y)
|
| 212 |
+
|
| 213 |
+
assert Domain.preprocess('Q<I>') == QQ.algebraic_field(I)
|
| 214 |
+
assert Domain.preprocess('QQ<I>') == QQ.algebraic_field(I)
|
| 215 |
+
|
| 216 |
+
assert Domain.preprocess('Q<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)
|
| 217 |
+
assert Domain.preprocess(
|
| 218 |
+
'QQ<sqrt(2), I>') == QQ.algebraic_field(sqrt(2), I)
|
| 219 |
+
|
| 220 |
+
raises(OptionError, lambda: Domain.preprocess('abc'))
|
| 221 |
+
|
| 222 |
+
|
| 223 |
+
def test_Domain_postprocess():
|
| 224 |
+
raises(GeneratorsError, lambda: Domain.postprocess({'gens': (x, y),
|
| 225 |
+
'domain': ZZ[y, z]}))
|
| 226 |
+
|
| 227 |
+
raises(GeneratorsError, lambda: Domain.postprocess({'gens': (),
|
| 228 |
+
'domain': EX}))
|
| 229 |
+
raises(GeneratorsError, lambda: Domain.postprocess({'domain': EX}))
|
| 230 |
+
|
| 231 |
+
|
| 232 |
+
def test_Split_preprocess():
|
| 233 |
+
assert Split.preprocess(False) is False
|
| 234 |
+
assert Split.preprocess(True) is True
|
| 235 |
+
|
| 236 |
+
assert Split.preprocess(0) is False
|
| 237 |
+
assert Split.preprocess(1) is True
|
| 238 |
+
|
| 239 |
+
raises(OptionError, lambda: Split.preprocess(x))
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
def test_Split_postprocess():
|
| 243 |
+
raises(NotImplementedError, lambda: Split.postprocess({'split': True}))
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
def test_Gaussian_preprocess():
|
| 247 |
+
assert Gaussian.preprocess(False) is False
|
| 248 |
+
assert Gaussian.preprocess(True) is True
|
| 249 |
+
|
| 250 |
+
assert Gaussian.preprocess(0) is False
|
| 251 |
+
assert Gaussian.preprocess(1) is True
|
| 252 |
+
|
| 253 |
+
raises(OptionError, lambda: Gaussian.preprocess(x))
|
| 254 |
+
|
| 255 |
+
|
| 256 |
+
def test_Gaussian_postprocess():
|
| 257 |
+
opt = {'gaussian': True}
|
| 258 |
+
Gaussian.postprocess(opt)
|
| 259 |
+
|
| 260 |
+
assert opt == {
|
| 261 |
+
'gaussian': True,
|
| 262 |
+
'domain': QQ_I,
|
| 263 |
+
}
|
| 264 |
+
|
| 265 |
+
|
| 266 |
+
def test_Extension_preprocess():
|
| 267 |
+
assert Extension.preprocess(True) is True
|
| 268 |
+
assert Extension.preprocess(1) is True
|
| 269 |
+
|
| 270 |
+
assert Extension.preprocess([]) is None
|
| 271 |
+
|
| 272 |
+
assert Extension.preprocess(sqrt(2)) == {sqrt(2)}
|
| 273 |
+
assert Extension.preprocess([sqrt(2)]) == {sqrt(2)}
|
| 274 |
+
|
| 275 |
+
assert Extension.preprocess([sqrt(2), I]) == {sqrt(2), I}
|
| 276 |
+
|
| 277 |
+
raises(OptionError, lambda: Extension.preprocess(False))
|
| 278 |
+
raises(OptionError, lambda: Extension.preprocess(0))
|
| 279 |
+
|
| 280 |
+
|
| 281 |
+
def test_Extension_postprocess():
|
| 282 |
+
opt = {'extension': {sqrt(2)}}
|
| 283 |
+
Extension.postprocess(opt)
|
| 284 |
+
|
| 285 |
+
assert opt == {
|
| 286 |
+
'extension': {sqrt(2)},
|
| 287 |
+
'domain': QQ.algebraic_field(sqrt(2)),
|
| 288 |
+
}
|
| 289 |
+
|
| 290 |
+
opt = {'extension': True}
|
| 291 |
+
Extension.postprocess(opt)
|
| 292 |
+
|
| 293 |
+
assert opt == {'extension': True}
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def test_Modulus_preprocess():
|
| 297 |
+
assert Modulus.preprocess(23) == 23
|
| 298 |
+
assert Modulus.preprocess(Integer(23)) == 23
|
| 299 |
+
|
| 300 |
+
raises(OptionError, lambda: Modulus.preprocess(0))
|
| 301 |
+
raises(OptionError, lambda: Modulus.preprocess(x))
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
def test_Modulus_postprocess():
|
| 305 |
+
opt = {'modulus': 5}
|
| 306 |
+
Modulus.postprocess(opt)
|
| 307 |
+
|
| 308 |
+
assert opt == {
|
| 309 |
+
'modulus': 5,
|
| 310 |
+
'domain': FF(5),
|
| 311 |
+
}
|
| 312 |
+
|
| 313 |
+
opt = {'modulus': 5, 'symmetric': False}
|
| 314 |
+
Modulus.postprocess(opt)
|
| 315 |
+
|
| 316 |
+
assert opt == {
|
| 317 |
+
'modulus': 5,
|
| 318 |
+
'domain': FF(5, False),
|
| 319 |
+
'symmetric': False,
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
def test_Symmetric_preprocess():
|
| 324 |
+
assert Symmetric.preprocess(False) is False
|
| 325 |
+
assert Symmetric.preprocess(True) is True
|
| 326 |
+
|
| 327 |
+
assert Symmetric.preprocess(0) is False
|
| 328 |
+
assert Symmetric.preprocess(1) is True
|
| 329 |
+
|
| 330 |
+
raises(OptionError, lambda: Symmetric.preprocess(x))
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
def test_Symmetric_postprocess():
|
| 334 |
+
opt = {'symmetric': True}
|
| 335 |
+
Symmetric.postprocess(opt)
|
| 336 |
+
|
| 337 |
+
assert opt == {'symmetric': True}
|
| 338 |
+
|
| 339 |
+
|
| 340 |
+
def test_Strict_preprocess():
|
| 341 |
+
assert Strict.preprocess(False) is False
|
| 342 |
+
assert Strict.preprocess(True) is True
|
| 343 |
+
|
| 344 |
+
assert Strict.preprocess(0) is False
|
| 345 |
+
assert Strict.preprocess(1) is True
|
| 346 |
+
|
| 347 |
+
raises(OptionError, lambda: Strict.preprocess(x))
|
| 348 |
+
|
| 349 |
+
|
| 350 |
+
def test_Strict_postprocess():
|
| 351 |
+
opt = {'strict': True}
|
| 352 |
+
Strict.postprocess(opt)
|
| 353 |
+
|
| 354 |
+
assert opt == {'strict': True}
|
| 355 |
+
|
| 356 |
+
|
| 357 |
+
def test_Auto_preprocess():
|
| 358 |
+
assert Auto.preprocess(False) is False
|
| 359 |
+
assert Auto.preprocess(True) is True
|
| 360 |
+
|
| 361 |
+
assert Auto.preprocess(0) is False
|
| 362 |
+
assert Auto.preprocess(1) is True
|
| 363 |
+
|
| 364 |
+
raises(OptionError, lambda: Auto.preprocess(x))
|
| 365 |
+
|
| 366 |
+
|
| 367 |
+
def test_Auto_postprocess():
|
| 368 |
+
opt = {'auto': True}
|
| 369 |
+
Auto.postprocess(opt)
|
| 370 |
+
|
| 371 |
+
assert opt == {'auto': True}
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
def test_Frac_preprocess():
|
| 375 |
+
assert Frac.preprocess(False) is False
|
| 376 |
+
assert Frac.preprocess(True) is True
|
| 377 |
+
|
| 378 |
+
assert Frac.preprocess(0) is False
|
| 379 |
+
assert Frac.preprocess(1) is True
|
| 380 |
+
|
| 381 |
+
raises(OptionError, lambda: Frac.preprocess(x))
|
| 382 |
+
|
| 383 |
+
|
| 384 |
+
def test_Frac_postprocess():
|
| 385 |
+
opt = {'frac': True}
|
| 386 |
+
Frac.postprocess(opt)
|
| 387 |
+
|
| 388 |
+
assert opt == {'frac': True}
|
| 389 |
+
|
| 390 |
+
|
| 391 |
+
def test_Formal_preprocess():
|
| 392 |
+
assert Formal.preprocess(False) is False
|
| 393 |
+
assert Formal.preprocess(True) is True
|
| 394 |
+
|
| 395 |
+
assert Formal.preprocess(0) is False
|
| 396 |
+
assert Formal.preprocess(1) is True
|
| 397 |
+
|
| 398 |
+
raises(OptionError, lambda: Formal.preprocess(x))
|
| 399 |
+
|
| 400 |
+
|
| 401 |
+
def test_Formal_postprocess():
|
| 402 |
+
opt = {'formal': True}
|
| 403 |
+
Formal.postprocess(opt)
|
| 404 |
+
|
| 405 |
+
assert opt == {'formal': True}
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
def test_Polys_preprocess():
|
| 409 |
+
assert Polys.preprocess(False) is False
|
| 410 |
+
assert Polys.preprocess(True) is True
|
| 411 |
+
|
| 412 |
+
assert Polys.preprocess(0) is False
|
| 413 |
+
assert Polys.preprocess(1) is True
|
| 414 |
+
|
| 415 |
+
raises(OptionError, lambda: Polys.preprocess(x))
|
| 416 |
+
|
| 417 |
+
|
| 418 |
+
def test_Polys_postprocess():
|
| 419 |
+
opt = {'polys': True}
|
| 420 |
+
Polys.postprocess(opt)
|
| 421 |
+
|
| 422 |
+
assert opt == {'polys': True}
|
| 423 |
+
|
| 424 |
+
|
| 425 |
+
def test_Include_preprocess():
|
| 426 |
+
assert Include.preprocess(False) is False
|
| 427 |
+
assert Include.preprocess(True) is True
|
| 428 |
+
|
| 429 |
+
assert Include.preprocess(0) is False
|
| 430 |
+
assert Include.preprocess(1) is True
|
| 431 |
+
|
| 432 |
+
raises(OptionError, lambda: Include.preprocess(x))
|
| 433 |
+
|
| 434 |
+
|
| 435 |
+
def test_Include_postprocess():
|
| 436 |
+
opt = {'include': True}
|
| 437 |
+
Include.postprocess(opt)
|
| 438 |
+
|
| 439 |
+
assert opt == {'include': True}
|
| 440 |
+
|
| 441 |
+
|
| 442 |
+
def test_All_preprocess():
|
| 443 |
+
assert All.preprocess(False) is False
|
| 444 |
+
assert All.preprocess(True) is True
|
| 445 |
+
|
| 446 |
+
assert All.preprocess(0) is False
|
| 447 |
+
assert All.preprocess(1) is True
|
| 448 |
+
|
| 449 |
+
raises(OptionError, lambda: All.preprocess(x))
|
| 450 |
+
|
| 451 |
+
|
| 452 |
+
def test_All_postprocess():
|
| 453 |
+
opt = {'all': True}
|
| 454 |
+
All.postprocess(opt)
|
| 455 |
+
|
| 456 |
+
assert opt == {'all': True}
|
| 457 |
+
|
| 458 |
+
|
| 459 |
+
def test_Gen_postprocess():
|
| 460 |
+
opt = {'gen': x}
|
| 461 |
+
Gen.postprocess(opt)
|
| 462 |
+
|
| 463 |
+
assert opt == {'gen': x}
|
| 464 |
+
|
| 465 |
+
|
| 466 |
+
def test_Symbols_preprocess():
|
| 467 |
+
raises(OptionError, lambda: Symbols.preprocess(x))
|
| 468 |
+
|
| 469 |
+
|
| 470 |
+
def test_Symbols_postprocess():
|
| 471 |
+
opt = {'symbols': [x, y, z]}
|
| 472 |
+
Symbols.postprocess(opt)
|
| 473 |
+
|
| 474 |
+
assert opt == {'symbols': [x, y, z]}
|
| 475 |
+
|
| 476 |
+
|
| 477 |
+
def test_Method_preprocess():
|
| 478 |
+
raises(OptionError, lambda: Method.preprocess(10))
|
| 479 |
+
|
| 480 |
+
|
| 481 |
+
def test_Method_postprocess():
|
| 482 |
+
opt = {'method': 'f5b'}
|
| 483 |
+
Method.postprocess(opt)
|
| 484 |
+
|
| 485 |
+
assert opt == {'method': 'f5b'}
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_pythonrational.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for PythonRational type. """
|
| 2 |
+
|
| 3 |
+
from sympy.polys.domains import PythonRational as QQ
|
| 4 |
+
from sympy.testing.pytest import raises
|
| 5 |
+
|
| 6 |
+
def test_PythonRational__init__():
|
| 7 |
+
assert QQ(0).numerator == 0
|
| 8 |
+
assert QQ(0).denominator == 1
|
| 9 |
+
assert QQ(0, 1).numerator == 0
|
| 10 |
+
assert QQ(0, 1).denominator == 1
|
| 11 |
+
assert QQ(0, -1).numerator == 0
|
| 12 |
+
assert QQ(0, -1).denominator == 1
|
| 13 |
+
|
| 14 |
+
assert QQ(1).numerator == 1
|
| 15 |
+
assert QQ(1).denominator == 1
|
| 16 |
+
assert QQ(1, 1).numerator == 1
|
| 17 |
+
assert QQ(1, 1).denominator == 1
|
| 18 |
+
assert QQ(-1, -1).numerator == 1
|
| 19 |
+
assert QQ(-1, -1).denominator == 1
|
| 20 |
+
|
| 21 |
+
assert QQ(-1).numerator == -1
|
| 22 |
+
assert QQ(-1).denominator == 1
|
| 23 |
+
assert QQ(-1, 1).numerator == -1
|
| 24 |
+
assert QQ(-1, 1).denominator == 1
|
| 25 |
+
assert QQ( 1, -1).numerator == -1
|
| 26 |
+
assert QQ( 1, -1).denominator == 1
|
| 27 |
+
|
| 28 |
+
assert QQ(1, 2).numerator == 1
|
| 29 |
+
assert QQ(1, 2).denominator == 2
|
| 30 |
+
assert QQ(3, 4).numerator == 3
|
| 31 |
+
assert QQ(3, 4).denominator == 4
|
| 32 |
+
|
| 33 |
+
assert QQ(2, 2).numerator == 1
|
| 34 |
+
assert QQ(2, 2).denominator == 1
|
| 35 |
+
assert QQ(2, 4).numerator == 1
|
| 36 |
+
assert QQ(2, 4).denominator == 2
|
| 37 |
+
|
| 38 |
+
def test_PythonRational__hash__():
|
| 39 |
+
assert hash(QQ(0)) == hash(0)
|
| 40 |
+
assert hash(QQ(1)) == hash(1)
|
| 41 |
+
assert hash(QQ(117)) == hash(117)
|
| 42 |
+
|
| 43 |
+
def test_PythonRational__int__():
|
| 44 |
+
assert int(QQ(-1, 4)) == 0
|
| 45 |
+
assert int(QQ( 1, 4)) == 0
|
| 46 |
+
assert int(QQ(-5, 4)) == -1
|
| 47 |
+
assert int(QQ( 5, 4)) == 1
|
| 48 |
+
|
| 49 |
+
def test_PythonRational__float__():
|
| 50 |
+
assert float(QQ(-1, 2)) == -0.5
|
| 51 |
+
assert float(QQ( 1, 2)) == 0.5
|
| 52 |
+
|
| 53 |
+
def test_PythonRational__abs__():
|
| 54 |
+
assert abs(QQ(-1, 2)) == QQ(1, 2)
|
| 55 |
+
assert abs(QQ( 1, 2)) == QQ(1, 2)
|
| 56 |
+
|
| 57 |
+
def test_PythonRational__pos__():
|
| 58 |
+
assert +QQ(-1, 2) == QQ(-1, 2)
|
| 59 |
+
assert +QQ( 1, 2) == QQ( 1, 2)
|
| 60 |
+
|
| 61 |
+
def test_PythonRational__neg__():
|
| 62 |
+
assert -QQ(-1, 2) == QQ( 1, 2)
|
| 63 |
+
assert -QQ( 1, 2) == QQ(-1, 2)
|
| 64 |
+
|
| 65 |
+
def test_PythonRational__add__():
|
| 66 |
+
assert QQ(-1, 2) + QQ( 1, 2) == QQ(0)
|
| 67 |
+
assert QQ( 1, 2) + QQ(-1, 2) == QQ(0)
|
| 68 |
+
|
| 69 |
+
assert QQ(1, 2) + QQ(1, 2) == QQ(1)
|
| 70 |
+
assert QQ(1, 2) + QQ(3, 2) == QQ(2)
|
| 71 |
+
assert QQ(3, 2) + QQ(1, 2) == QQ(2)
|
| 72 |
+
assert QQ(3, 2) + QQ(3, 2) == QQ(3)
|
| 73 |
+
|
| 74 |
+
assert 1 + QQ(1, 2) == QQ(3, 2)
|
| 75 |
+
assert QQ(1, 2) + 1 == QQ(3, 2)
|
| 76 |
+
|
| 77 |
+
def test_PythonRational__sub__():
|
| 78 |
+
assert QQ(-1, 2) - QQ( 1, 2) == QQ(-1)
|
| 79 |
+
assert QQ( 1, 2) - QQ(-1, 2) == QQ( 1)
|
| 80 |
+
|
| 81 |
+
assert QQ(1, 2) - QQ(1, 2) == QQ( 0)
|
| 82 |
+
assert QQ(1, 2) - QQ(3, 2) == QQ(-1)
|
| 83 |
+
assert QQ(3, 2) - QQ(1, 2) == QQ( 1)
|
| 84 |
+
assert QQ(3, 2) - QQ(3, 2) == QQ( 0)
|
| 85 |
+
|
| 86 |
+
assert 1 - QQ(1, 2) == QQ( 1, 2)
|
| 87 |
+
assert QQ(1, 2) - 1 == QQ(-1, 2)
|
| 88 |
+
|
| 89 |
+
def test_PythonRational__mul__():
|
| 90 |
+
assert QQ(-1, 2) * QQ( 1, 2) == QQ(-1, 4)
|
| 91 |
+
assert QQ( 1, 2) * QQ(-1, 2) == QQ(-1, 4)
|
| 92 |
+
|
| 93 |
+
assert QQ(1, 2) * QQ(1, 2) == QQ(1, 4)
|
| 94 |
+
assert QQ(1, 2) * QQ(3, 2) == QQ(3, 4)
|
| 95 |
+
assert QQ(3, 2) * QQ(1, 2) == QQ(3, 4)
|
| 96 |
+
assert QQ(3, 2) * QQ(3, 2) == QQ(9, 4)
|
| 97 |
+
|
| 98 |
+
assert 2 * QQ(1, 2) == QQ(1)
|
| 99 |
+
assert QQ(1, 2) * 2 == QQ(1)
|
| 100 |
+
|
| 101 |
+
def test_PythonRational__truediv__():
|
| 102 |
+
assert QQ(-1, 2) / QQ( 1, 2) == QQ(-1)
|
| 103 |
+
assert QQ( 1, 2) / QQ(-1, 2) == QQ(-1)
|
| 104 |
+
|
| 105 |
+
assert QQ(1, 2) / QQ(1, 2) == QQ(1)
|
| 106 |
+
assert QQ(1, 2) / QQ(3, 2) == QQ(1, 3)
|
| 107 |
+
assert QQ(3, 2) / QQ(1, 2) == QQ(3)
|
| 108 |
+
assert QQ(3, 2) / QQ(3, 2) == QQ(1)
|
| 109 |
+
|
| 110 |
+
assert 2 / QQ(1, 2) == QQ(4)
|
| 111 |
+
assert QQ(1, 2) / 2 == QQ(1, 4)
|
| 112 |
+
|
| 113 |
+
raises(ZeroDivisionError, lambda: QQ(1, 2) / QQ(0))
|
| 114 |
+
raises(ZeroDivisionError, lambda: QQ(1, 2) / 0)
|
| 115 |
+
|
| 116 |
+
def test_PythonRational__pow__():
|
| 117 |
+
assert QQ(1)**10 == QQ(1)
|
| 118 |
+
assert QQ(2)**10 == QQ(1024)
|
| 119 |
+
|
| 120 |
+
assert QQ(1)**(-10) == QQ(1)
|
| 121 |
+
assert QQ(2)**(-10) == QQ(1, 1024)
|
| 122 |
+
|
| 123 |
+
def test_PythonRational__eq__():
|
| 124 |
+
assert (QQ(1, 2) == QQ(1, 2)) is True
|
| 125 |
+
assert (QQ(1, 2) != QQ(1, 2)) is False
|
| 126 |
+
|
| 127 |
+
assert (QQ(1, 2) == QQ(1, 3)) is False
|
| 128 |
+
assert (QQ(1, 2) != QQ(1, 3)) is True
|
| 129 |
+
|
| 130 |
+
def test_PythonRational__lt_le_gt_ge__():
|
| 131 |
+
assert (QQ(1, 2) < QQ(1, 4)) is False
|
| 132 |
+
assert (QQ(1, 2) <= QQ(1, 4)) is False
|
| 133 |
+
assert (QQ(1, 2) > QQ(1, 4)) is True
|
| 134 |
+
assert (QQ(1, 2) >= QQ(1, 4)) is True
|
| 135 |
+
|
| 136 |
+
assert (QQ(1, 4) < QQ(1, 2)) is True
|
| 137 |
+
assert (QQ(1, 4) <= QQ(1, 2)) is True
|
| 138 |
+
assert (QQ(1, 4) > QQ(1, 2)) is False
|
| 139 |
+
assert (QQ(1, 4) >= QQ(1, 2)) is False
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_rationaltools.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for tools for manipulation of rational expressions. """
|
| 2 |
+
|
| 3 |
+
from sympy.polys.rationaltools import together
|
| 4 |
+
|
| 5 |
+
from sympy.core.mul import Mul
|
| 6 |
+
from sympy.core.numbers import Rational
|
| 7 |
+
from sympy.core.relational import Eq
|
| 8 |
+
from sympy.core.singleton import S
|
| 9 |
+
from sympy.core.symbol import symbols
|
| 10 |
+
from sympy.functions.elementary.exponential import exp
|
| 11 |
+
from sympy.functions.elementary.trigonometric import sin
|
| 12 |
+
from sympy.integrals.integrals import Integral
|
| 13 |
+
from sympy.abc import x, y, z
|
| 14 |
+
|
| 15 |
+
A, B = symbols('A,B', commutative=False)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def test_together():
|
| 19 |
+
assert together(0) == 0
|
| 20 |
+
assert together(1) == 1
|
| 21 |
+
|
| 22 |
+
assert together(x*y*z) == x*y*z
|
| 23 |
+
assert together(x + y) == x + y
|
| 24 |
+
|
| 25 |
+
assert together(1/x) == 1/x
|
| 26 |
+
|
| 27 |
+
assert together(1/x + 1) == (x + 1)/x
|
| 28 |
+
assert together(1/x + 3) == (3*x + 1)/x
|
| 29 |
+
assert together(1/x + x) == (x**2 + 1)/x
|
| 30 |
+
|
| 31 |
+
assert together(1/x + S.Half) == (x + 2)/(2*x)
|
| 32 |
+
assert together(S.Half + x/2) == Mul(S.Half, x + 1, evaluate=False)
|
| 33 |
+
|
| 34 |
+
assert together(1/x + 2/y) == (2*x + y)/(y*x)
|
| 35 |
+
assert together(1/(1 + 1/x)) == x/(1 + x)
|
| 36 |
+
assert together(x/(1 + 1/x)) == x**2/(1 + x)
|
| 37 |
+
|
| 38 |
+
assert together(1/x + 1/y + 1/z) == (x*y + x*z + y*z)/(x*y*z)
|
| 39 |
+
assert together(1/(1 + x + 1/y + 1/z)) == y*z/(y + z + y*z + x*y*z)
|
| 40 |
+
|
| 41 |
+
assert together(1/(x*y) + 1/(x*y)**2) == y**(-2)*x**(-2)*(1 + x*y)
|
| 42 |
+
assert together(1/(x*y) + 1/(x*y)**4) == y**(-4)*x**(-4)*(1 + x**3*y**3)
|
| 43 |
+
assert together(1/(x**7*y) + 1/(x*y)**4) == y**(-4)*x**(-7)*(x**3 + y**3)
|
| 44 |
+
|
| 45 |
+
assert together(5/(2 + 6/(3 + 7/(4 + 8/(5 + 9/x))))) == \
|
| 46 |
+
Rational(5, 2)*((171 + 119*x)/(279 + 203*x))
|
| 47 |
+
|
| 48 |
+
assert together(1 + 1/(x + 1)**2) == (1 + (x + 1)**2)/(x + 1)**2
|
| 49 |
+
assert together(1 + 1/(x*(1 + x))) == (1 + x*(1 + x))/(x*(1 + x))
|
| 50 |
+
assert together(
|
| 51 |
+
1/(x*(x + 1)) + 1/(x*(x + 2))) == (3 + 2*x)/(x*(1 + x)*(2 + x))
|
| 52 |
+
assert together(1 + 1/(2*x + 2)**2) == (4*(x + 1)**2 + 1)/(4*(x + 1)**2)
|
| 53 |
+
|
| 54 |
+
assert together(sin(1/x + 1/y)) == sin(1/x + 1/y)
|
| 55 |
+
assert together(sin(1/x + 1/y), deep=True) == sin((x + y)/(x*y))
|
| 56 |
+
|
| 57 |
+
assert together(1/exp(x) + 1/(x*exp(x))) == (1 + x)/(x*exp(x))
|
| 58 |
+
assert together(1/exp(2*x) + 1/(x*exp(3*x))) == (1 + exp(x)*x)/(x*exp(3*x))
|
| 59 |
+
|
| 60 |
+
assert together(Integral(1/x + 1/y, x)) == Integral((x + y)/(x*y), x)
|
| 61 |
+
assert together(Eq(1/x + 1/y, 1 + 1/z)) == Eq((x + y)/(x*y), (z + 1)/z)
|
| 62 |
+
|
| 63 |
+
assert together((A*B)**-1 + (B*A)**-1) == (A*B)**-1 + (B*A)**-1
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_rootoftools.py
ADDED
|
@@ -0,0 +1,653 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for the implementation of RootOf class and related tools. """
|
| 2 |
+
|
| 3 |
+
from sympy.polys.polytools import Poly
|
| 4 |
+
import sympy.polys.rootoftools as rootoftools
|
| 5 |
+
from sympy.polys.rootoftools import (rootof, RootOf, CRootOf, RootSum,
|
| 6 |
+
_pure_key_dict as D)
|
| 7 |
+
|
| 8 |
+
from sympy.polys.polyerrors import (
|
| 9 |
+
MultivariatePolynomialError,
|
| 10 |
+
GeneratorsNeeded,
|
| 11 |
+
PolynomialError,
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
from sympy.core.function import (Function, Lambda)
|
| 15 |
+
from sympy.core.numbers import (Float, I, Rational)
|
| 16 |
+
from sympy.core.relational import Eq
|
| 17 |
+
from sympy.core.singleton import S
|
| 18 |
+
from sympy.functions.elementary.exponential import (exp, log)
|
| 19 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 20 |
+
from sympy.functions.elementary.trigonometric import tan
|
| 21 |
+
from sympy.integrals.integrals import Integral
|
| 22 |
+
from sympy.polys.orthopolys import legendre_poly
|
| 23 |
+
from sympy.solvers.solvers import solve
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
from sympy.testing.pytest import raises, slow
|
| 27 |
+
from sympy.core.expr import unchanged
|
| 28 |
+
|
| 29 |
+
from sympy.abc import a, b, x, y, z, r
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def test_CRootOf___new__():
|
| 33 |
+
assert rootof(x, 0) == 0
|
| 34 |
+
assert rootof(x, -1) == 0
|
| 35 |
+
|
| 36 |
+
assert rootof(x, S.Zero) == 0
|
| 37 |
+
|
| 38 |
+
assert rootof(x - 1, 0) == 1
|
| 39 |
+
assert rootof(x - 1, -1) == 1
|
| 40 |
+
|
| 41 |
+
assert rootof(x + 1, 0) == -1
|
| 42 |
+
assert rootof(x + 1, -1) == -1
|
| 43 |
+
|
| 44 |
+
assert rootof(x**2 + 2*x + 3, 0) == -1 - I*sqrt(2)
|
| 45 |
+
assert rootof(x**2 + 2*x + 3, 1) == -1 + I*sqrt(2)
|
| 46 |
+
assert rootof(x**2 + 2*x + 3, -1) == -1 + I*sqrt(2)
|
| 47 |
+
assert rootof(x**2 + 2*x + 3, -2) == -1 - I*sqrt(2)
|
| 48 |
+
|
| 49 |
+
r = rootof(x**2 + 2*x + 3, 0, radicals=False)
|
| 50 |
+
assert isinstance(r, RootOf) is True
|
| 51 |
+
|
| 52 |
+
r = rootof(x**2 + 2*x + 3, 1, radicals=False)
|
| 53 |
+
assert isinstance(r, RootOf) is True
|
| 54 |
+
|
| 55 |
+
r = rootof(x**2 + 2*x + 3, -1, radicals=False)
|
| 56 |
+
assert isinstance(r, RootOf) is True
|
| 57 |
+
|
| 58 |
+
r = rootof(x**2 + 2*x + 3, -2, radicals=False)
|
| 59 |
+
assert isinstance(r, RootOf) is True
|
| 60 |
+
|
| 61 |
+
assert rootof((x - 1)*(x + 1), 0, radicals=False) == -1
|
| 62 |
+
assert rootof((x - 1)*(x + 1), 1, radicals=False) == 1
|
| 63 |
+
assert rootof((x - 1)*(x + 1), -1, radicals=False) == 1
|
| 64 |
+
assert rootof((x - 1)*(x + 1), -2, radicals=False) == -1
|
| 65 |
+
|
| 66 |
+
assert rootof((x - 1)*(x + 1), 0, radicals=True) == -1
|
| 67 |
+
assert rootof((x - 1)*(x + 1), 1, radicals=True) == 1
|
| 68 |
+
assert rootof((x - 1)*(x + 1), -1, radicals=True) == 1
|
| 69 |
+
assert rootof((x - 1)*(x + 1), -2, radicals=True) == -1
|
| 70 |
+
|
| 71 |
+
assert rootof((x - 1)*(x**3 + x + 3), 0) == rootof(x**3 + x + 3, 0)
|
| 72 |
+
assert rootof((x - 1)*(x**3 + x + 3), 1) == 1
|
| 73 |
+
assert rootof((x - 1)*(x**3 + x + 3), 2) == rootof(x**3 + x + 3, 1)
|
| 74 |
+
assert rootof((x - 1)*(x**3 + x + 3), 3) == rootof(x**3 + x + 3, 2)
|
| 75 |
+
assert rootof((x - 1)*(x**3 + x + 3), -1) == rootof(x**3 + x + 3, 2)
|
| 76 |
+
assert rootof((x - 1)*(x**3 + x + 3), -2) == rootof(x**3 + x + 3, 1)
|
| 77 |
+
assert rootof((x - 1)*(x**3 + x + 3), -3) == 1
|
| 78 |
+
assert rootof((x - 1)*(x**3 + x + 3), -4) == rootof(x**3 + x + 3, 0)
|
| 79 |
+
|
| 80 |
+
assert rootof(x**4 + 3*x**3, 0) == -3
|
| 81 |
+
assert rootof(x**4 + 3*x**3, 1) == 0
|
| 82 |
+
assert rootof(x**4 + 3*x**3, 2) == 0
|
| 83 |
+
assert rootof(x**4 + 3*x**3, 3) == 0
|
| 84 |
+
|
| 85 |
+
raises(GeneratorsNeeded, lambda: rootof(0, 0))
|
| 86 |
+
raises(GeneratorsNeeded, lambda: rootof(1, 0))
|
| 87 |
+
|
| 88 |
+
raises(PolynomialError, lambda: rootof(Poly(0, x), 0))
|
| 89 |
+
raises(PolynomialError, lambda: rootof(Poly(1, x), 0))
|
| 90 |
+
raises(PolynomialError, lambda: rootof(x - y, 0))
|
| 91 |
+
# issue 8617
|
| 92 |
+
raises(PolynomialError, lambda: rootof(exp(x), 0))
|
| 93 |
+
|
| 94 |
+
raises(NotImplementedError, lambda: rootof(x**3 - x + sqrt(2), 0))
|
| 95 |
+
raises(NotImplementedError, lambda: rootof(x**3 - x + I, 0))
|
| 96 |
+
|
| 97 |
+
raises(IndexError, lambda: rootof(x**2 - 1, -4))
|
| 98 |
+
raises(IndexError, lambda: rootof(x**2 - 1, -3))
|
| 99 |
+
raises(IndexError, lambda: rootof(x**2 - 1, 2))
|
| 100 |
+
raises(IndexError, lambda: rootof(x**2 - 1, 3))
|
| 101 |
+
raises(ValueError, lambda: rootof(x**2 - 1, x))
|
| 102 |
+
|
| 103 |
+
assert rootof(Poly(x - y, x), 0) == y
|
| 104 |
+
|
| 105 |
+
assert rootof(Poly(x**2 - y, x), 0) == -sqrt(y)
|
| 106 |
+
assert rootof(Poly(x**2 - y, x), 1) == sqrt(y)
|
| 107 |
+
|
| 108 |
+
assert rootof(Poly(x**3 - y, x), 0) == y**Rational(1, 3)
|
| 109 |
+
|
| 110 |
+
assert rootof(y*x**3 + y*x + 2*y, x, 0) == -1
|
| 111 |
+
raises(NotImplementedError, lambda: rootof(x**3 + x + 2*y, x, 0))
|
| 112 |
+
|
| 113 |
+
assert rootof(x**3 + x + 1, 0).is_commutative is True
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def test_CRootOf_attributes():
|
| 117 |
+
r = rootof(x**3 + x + 3, 0)
|
| 118 |
+
assert r.is_number
|
| 119 |
+
assert r.free_symbols == set()
|
| 120 |
+
# if the following assertion fails then multivariate polynomials
|
| 121 |
+
# are apparently supported and the RootOf.free_symbols routine
|
| 122 |
+
# should be changed to return whatever symbols would not be
|
| 123 |
+
# the PurePoly dummy symbol
|
| 124 |
+
raises(NotImplementedError, lambda: rootof(Poly(x**3 + y*x + 1, x), 0))
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
def test_CRootOf___eq__():
|
| 128 |
+
assert (rootof(x**3 + x + 3, 0) == rootof(x**3 + x + 3, 0)) is True
|
| 129 |
+
assert (rootof(x**3 + x + 3, 0) == rootof(x**3 + x + 3, 1)) is False
|
| 130 |
+
assert (rootof(x**3 + x + 3, 1) == rootof(x**3 + x + 3, 1)) is True
|
| 131 |
+
assert (rootof(x**3 + x + 3, 1) == rootof(x**3 + x + 3, 2)) is False
|
| 132 |
+
assert (rootof(x**3 + x + 3, 2) == rootof(x**3 + x + 3, 2)) is True
|
| 133 |
+
|
| 134 |
+
assert (rootof(x**3 + x + 3, 0) == rootof(y**3 + y + 3, 0)) is True
|
| 135 |
+
assert (rootof(x**3 + x + 3, 0) == rootof(y**3 + y + 3, 1)) is False
|
| 136 |
+
assert (rootof(x**3 + x + 3, 1) == rootof(y**3 + y + 3, 1)) is True
|
| 137 |
+
assert (rootof(x**3 + x + 3, 1) == rootof(y**3 + y + 3, 2)) is False
|
| 138 |
+
assert (rootof(x**3 + x + 3, 2) == rootof(y**3 + y + 3, 2)) is True
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
def test_CRootOf___eval_Eq__():
|
| 142 |
+
f = Function('f')
|
| 143 |
+
eq = x**3 + x + 3
|
| 144 |
+
r = rootof(eq, 2)
|
| 145 |
+
r1 = rootof(eq, 1)
|
| 146 |
+
assert Eq(r, r1) is S.false
|
| 147 |
+
assert Eq(r, r) is S.true
|
| 148 |
+
assert unchanged(Eq, r, x)
|
| 149 |
+
assert Eq(r, 0) is S.false
|
| 150 |
+
assert Eq(r, S.Infinity) is S.false
|
| 151 |
+
assert Eq(r, I) is S.false
|
| 152 |
+
assert unchanged(Eq, r, f(0))
|
| 153 |
+
sol = solve(eq)
|
| 154 |
+
for s in sol:
|
| 155 |
+
if s.is_real:
|
| 156 |
+
assert Eq(r, s) is S.false
|
| 157 |
+
r = rootof(eq, 0)
|
| 158 |
+
for s in sol:
|
| 159 |
+
if s.is_real:
|
| 160 |
+
assert Eq(r, s) is S.true
|
| 161 |
+
eq = x**3 + x + 1
|
| 162 |
+
sol = solve(eq)
|
| 163 |
+
assert [Eq(rootof(eq, i), j) for i in range(3) for j in sol
|
| 164 |
+
].count(True) == 3
|
| 165 |
+
assert Eq(rootof(eq, 0), 1 + S.ImaginaryUnit) == False
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
def test_CRootOf_is_real():
|
| 169 |
+
assert rootof(x**3 + x + 3, 0).is_real is True
|
| 170 |
+
assert rootof(x**3 + x + 3, 1).is_real is False
|
| 171 |
+
assert rootof(x**3 + x + 3, 2).is_real is False
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def test_CRootOf_is_complex():
|
| 175 |
+
assert rootof(x**3 + x + 3, 0).is_complex is True
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def test_CRootOf_subs():
|
| 179 |
+
assert rootof(x**3 + x + 1, 0).subs(x, y) == rootof(y**3 + y + 1, 0)
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def test_CRootOf_diff():
|
| 183 |
+
assert rootof(x**3 + x + 1, 0).diff(x) == 0
|
| 184 |
+
assert rootof(x**3 + x + 1, 0).diff(y) == 0
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
@slow
|
| 188 |
+
def test_CRootOf_evalf():
|
| 189 |
+
real = rootof(x**3 + x + 3, 0).evalf(n=20)
|
| 190 |
+
|
| 191 |
+
assert real.epsilon_eq(Float("-1.2134116627622296341"))
|
| 192 |
+
|
| 193 |
+
re, im = rootof(x**3 + x + 3, 1).evalf(n=20).as_real_imag()
|
| 194 |
+
|
| 195 |
+
assert re.epsilon_eq( Float("0.60670583138111481707"))
|
| 196 |
+
assert im.epsilon_eq(-Float("1.45061224918844152650"))
|
| 197 |
+
|
| 198 |
+
re, im = rootof(x**3 + x + 3, 2).evalf(n=20).as_real_imag()
|
| 199 |
+
|
| 200 |
+
assert re.epsilon_eq(Float("0.60670583138111481707"))
|
| 201 |
+
assert im.epsilon_eq(Float("1.45061224918844152650"))
|
| 202 |
+
|
| 203 |
+
p = legendre_poly(4, x, polys=True)
|
| 204 |
+
roots = [str(r.n(17)) for r in p.real_roots()]
|
| 205 |
+
# magnitudes are given by
|
| 206 |
+
# sqrt(3/S(7) - 2*sqrt(6/S(5))/7)
|
| 207 |
+
# and
|
| 208 |
+
# sqrt(3/S(7) + 2*sqrt(6/S(5))/7)
|
| 209 |
+
assert roots == [
|
| 210 |
+
"-0.86113631159405258",
|
| 211 |
+
"-0.33998104358485626",
|
| 212 |
+
"0.33998104358485626",
|
| 213 |
+
"0.86113631159405258",
|
| 214 |
+
]
|
| 215 |
+
|
| 216 |
+
re = rootof(x**5 - 5*x + 12, 0).evalf(n=20)
|
| 217 |
+
assert re.epsilon_eq(Float("-1.84208596619025438271"))
|
| 218 |
+
|
| 219 |
+
re, im = rootof(x**5 - 5*x + 12, 1).evalf(n=20).as_real_imag()
|
| 220 |
+
assert re.epsilon_eq(Float("-0.351854240827371999559"))
|
| 221 |
+
assert im.epsilon_eq(Float("-1.709561043370328882010"))
|
| 222 |
+
|
| 223 |
+
re, im = rootof(x**5 - 5*x + 12, 2).evalf(n=20).as_real_imag()
|
| 224 |
+
assert re.epsilon_eq(Float("-0.351854240827371999559"))
|
| 225 |
+
assert im.epsilon_eq(Float("+1.709561043370328882010"))
|
| 226 |
+
|
| 227 |
+
re, im = rootof(x**5 - 5*x + 12, 3).evalf(n=20).as_real_imag()
|
| 228 |
+
assert re.epsilon_eq(Float("+1.272897223922499190910"))
|
| 229 |
+
assert im.epsilon_eq(Float("-0.719798681483861386681"))
|
| 230 |
+
|
| 231 |
+
re, im = rootof(x**5 - 5*x + 12, 4).evalf(n=20).as_real_imag()
|
| 232 |
+
assert re.epsilon_eq(Float("+1.272897223922499190910"))
|
| 233 |
+
assert im.epsilon_eq(Float("+0.719798681483861386681"))
|
| 234 |
+
|
| 235 |
+
# issue 6393
|
| 236 |
+
assert str(rootof(x**5 + 2*x**4 + x**3 - 68719476736, 0).n(3)) == '147.'
|
| 237 |
+
eq = (531441*x**11 + 3857868*x**10 + 13730229*x**9 + 32597882*x**8 +
|
| 238 |
+
55077472*x**7 + 60452000*x**6 + 32172064*x**5 - 4383808*x**4 -
|
| 239 |
+
11942912*x**3 - 1506304*x**2 + 1453312*x + 512)
|
| 240 |
+
a, b = rootof(eq, 1).n(2).as_real_imag()
|
| 241 |
+
c, d = rootof(eq, 2).n(2).as_real_imag()
|
| 242 |
+
assert a == c
|
| 243 |
+
assert b < d
|
| 244 |
+
assert b == -d
|
| 245 |
+
# issue 6451
|
| 246 |
+
r = rootof(legendre_poly(64, x), 7)
|
| 247 |
+
assert r.n(2) == r.n(100).n(2)
|
| 248 |
+
# issue 9019
|
| 249 |
+
r0 = rootof(x**2 + 1, 0, radicals=False)
|
| 250 |
+
r1 = rootof(x**2 + 1, 1, radicals=False)
|
| 251 |
+
assert r0.n(4) == Float(-1.0, 4) * I
|
| 252 |
+
assert r1.n(4) == Float(1.0, 4) * I
|
| 253 |
+
|
| 254 |
+
# make sure verification is used in case a max/min traps the "root"
|
| 255 |
+
assert str(rootof(4*x**5 + 16*x**3 + 12*x**2 + 7, 0).n(3)) == '-0.976'
|
| 256 |
+
|
| 257 |
+
# watch out for UnboundLocalError
|
| 258 |
+
c = CRootOf(90720*x**6 - 4032*x**4 + 84*x**2 - 1, 0)
|
| 259 |
+
assert c._eval_evalf(2) # doesn't fail
|
| 260 |
+
|
| 261 |
+
# watch out for imaginary parts that don't want to evaluate
|
| 262 |
+
assert str(RootOf(x**16 + 32*x**14 + 508*x**12 + 5440*x**10 +
|
| 263 |
+
39510*x**8 + 204320*x**6 + 755548*x**4 + 1434496*x**2 +
|
| 264 |
+
877969, 10).n(2)) == '-3.4*I'
|
| 265 |
+
assert abs(RootOf(x**4 + 10*x**2 + 1, 0).n(2)) < 0.4
|
| 266 |
+
|
| 267 |
+
# check reset and args
|
| 268 |
+
r = [RootOf(x**3 + x + 3, i) for i in range(3)]
|
| 269 |
+
r[0]._reset()
|
| 270 |
+
for ri in r:
|
| 271 |
+
i = ri._get_interval()
|
| 272 |
+
ri.n(2)
|
| 273 |
+
assert i != ri._get_interval()
|
| 274 |
+
ri._reset()
|
| 275 |
+
assert i == ri._get_interval()
|
| 276 |
+
assert i == i.func(*i.args)
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def test_issue_24978():
|
| 280 |
+
# Irreducible poly with negative leading coeff is normalized
|
| 281 |
+
# (factor of -1 is extracted), before being stored as CRootOf.poly.
|
| 282 |
+
f = -x**2 + 2
|
| 283 |
+
r = CRootOf(f, 0)
|
| 284 |
+
assert r.poly.as_expr() == x**2 - 2
|
| 285 |
+
# An action that prompts calculation of an interval puts r.poly in
|
| 286 |
+
# the cache.
|
| 287 |
+
r.n()
|
| 288 |
+
assert r.poly in rootoftools._reals_cache
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
def test_CRootOf_evalf_caching_bug():
|
| 292 |
+
r = rootof(x**5 - 5*x + 12, 1)
|
| 293 |
+
r.n()
|
| 294 |
+
a = r._get_interval()
|
| 295 |
+
r = rootof(x**5 - 5*x + 12, 1)
|
| 296 |
+
r.n()
|
| 297 |
+
b = r._get_interval()
|
| 298 |
+
assert a == b
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
def test_CRootOf_real_roots():
|
| 302 |
+
assert Poly(x**5 + x + 1).real_roots() == [rootof(x**3 - x**2 + 1, 0)]
|
| 303 |
+
assert Poly(x**5 + x + 1).real_roots(radicals=False) == [rootof(
|
| 304 |
+
x**3 - x**2 + 1, 0)]
|
| 305 |
+
|
| 306 |
+
# https://github.com/sympy/sympy/issues/20902
|
| 307 |
+
p = Poly(-3*x**4 - 10*x**3 - 12*x**2 - 6*x - 1, x, domain='ZZ')
|
| 308 |
+
assert CRootOf.real_roots(p) == [S(-1), S(-1), S(-1), S(-1)/3]
|
| 309 |
+
|
| 310 |
+
|
| 311 |
+
def test_CRootOf_all_roots():
|
| 312 |
+
assert Poly(x**5 + x + 1).all_roots() == [
|
| 313 |
+
rootof(x**3 - x**2 + 1, 0),
|
| 314 |
+
Rational(-1, 2) - sqrt(3)*I/2,
|
| 315 |
+
Rational(-1, 2) + sqrt(3)*I/2,
|
| 316 |
+
rootof(x**3 - x**2 + 1, 1),
|
| 317 |
+
rootof(x**3 - x**2 + 1, 2),
|
| 318 |
+
]
|
| 319 |
+
|
| 320 |
+
assert Poly(x**5 + x + 1).all_roots(radicals=False) == [
|
| 321 |
+
rootof(x**3 - x**2 + 1, 0),
|
| 322 |
+
rootof(x**2 + x + 1, 0, radicals=False),
|
| 323 |
+
rootof(x**2 + x + 1, 1, radicals=False),
|
| 324 |
+
rootof(x**3 - x**2 + 1, 1),
|
| 325 |
+
rootof(x**3 - x**2 + 1, 2),
|
| 326 |
+
]
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
def test_CRootOf_eval_rational():
|
| 330 |
+
p = legendre_poly(4, x, polys=True)
|
| 331 |
+
roots = [r.eval_rational(n=18) for r in p.real_roots()]
|
| 332 |
+
for root in roots:
|
| 333 |
+
assert isinstance(root, Rational)
|
| 334 |
+
roots = [str(root.n(17)) for root in roots]
|
| 335 |
+
assert roots == [
|
| 336 |
+
"-0.86113631159405258",
|
| 337 |
+
"-0.33998104358485626",
|
| 338 |
+
"0.33998104358485626",
|
| 339 |
+
"0.86113631159405258",
|
| 340 |
+
]
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
def test_CRootOf_lazy():
|
| 344 |
+
# irreducible poly with both real and complex roots:
|
| 345 |
+
f = Poly(x**3 + 2*x + 2)
|
| 346 |
+
|
| 347 |
+
# real root:
|
| 348 |
+
CRootOf.clear_cache()
|
| 349 |
+
r = CRootOf(f, 0)
|
| 350 |
+
# Not yet in cache, after construction:
|
| 351 |
+
assert r.poly not in rootoftools._reals_cache
|
| 352 |
+
assert r.poly not in rootoftools._complexes_cache
|
| 353 |
+
r.evalf()
|
| 354 |
+
# In cache after evaluation:
|
| 355 |
+
assert r.poly in rootoftools._reals_cache
|
| 356 |
+
assert r.poly not in rootoftools._complexes_cache
|
| 357 |
+
|
| 358 |
+
# complex root:
|
| 359 |
+
CRootOf.clear_cache()
|
| 360 |
+
r = CRootOf(f, 1)
|
| 361 |
+
# Not yet in cache, after construction:
|
| 362 |
+
assert r.poly not in rootoftools._reals_cache
|
| 363 |
+
assert r.poly not in rootoftools._complexes_cache
|
| 364 |
+
r.evalf()
|
| 365 |
+
# In cache after evaluation:
|
| 366 |
+
assert r.poly in rootoftools._reals_cache
|
| 367 |
+
assert r.poly in rootoftools._complexes_cache
|
| 368 |
+
|
| 369 |
+
# composite poly with both real and complex roots:
|
| 370 |
+
f = Poly((x**2 - 2)*(x**2 + 1))
|
| 371 |
+
|
| 372 |
+
# real root:
|
| 373 |
+
CRootOf.clear_cache()
|
| 374 |
+
r = CRootOf(f, 0)
|
| 375 |
+
# In cache immediately after construction:
|
| 376 |
+
assert r.poly in rootoftools._reals_cache
|
| 377 |
+
assert r.poly not in rootoftools._complexes_cache
|
| 378 |
+
|
| 379 |
+
# complex root:
|
| 380 |
+
CRootOf.clear_cache()
|
| 381 |
+
r = CRootOf(f, 2)
|
| 382 |
+
# In cache immediately after construction:
|
| 383 |
+
assert r.poly in rootoftools._reals_cache
|
| 384 |
+
assert r.poly in rootoftools._complexes_cache
|
| 385 |
+
|
| 386 |
+
|
| 387 |
+
def test_RootSum___new__():
|
| 388 |
+
f = x**3 + x + 3
|
| 389 |
+
|
| 390 |
+
g = Lambda(r, log(r*x))
|
| 391 |
+
s = RootSum(f, g)
|
| 392 |
+
|
| 393 |
+
assert isinstance(s, RootSum) is True
|
| 394 |
+
|
| 395 |
+
assert RootSum(f**2, g) == 2*RootSum(f, g)
|
| 396 |
+
assert RootSum((x - 7)*f**3, g) == log(7*x) + 3*RootSum(f, g)
|
| 397 |
+
|
| 398 |
+
# issue 5571
|
| 399 |
+
assert hash(RootSum((x - 7)*f**3, g)) == hash(log(7*x) + 3*RootSum(f, g))
|
| 400 |
+
|
| 401 |
+
raises(MultivariatePolynomialError, lambda: RootSum(x**3 + x + y))
|
| 402 |
+
raises(ValueError, lambda: RootSum(x**2 + 3, lambda x: x))
|
| 403 |
+
|
| 404 |
+
assert RootSum(f, exp) == RootSum(f, Lambda(x, exp(x)))
|
| 405 |
+
assert RootSum(f, log) == RootSum(f, Lambda(x, log(x)))
|
| 406 |
+
|
| 407 |
+
assert isinstance(RootSum(f, auto=False), RootSum) is True
|
| 408 |
+
|
| 409 |
+
assert RootSum(f) == 0
|
| 410 |
+
assert RootSum(f, Lambda(x, x)) == 0
|
| 411 |
+
assert RootSum(f, Lambda(x, x**2)) == -2
|
| 412 |
+
|
| 413 |
+
assert RootSum(f, Lambda(x, 1)) == 3
|
| 414 |
+
assert RootSum(f, Lambda(x, 2)) == 6
|
| 415 |
+
|
| 416 |
+
assert RootSum(f, auto=False).is_commutative is True
|
| 417 |
+
|
| 418 |
+
assert RootSum(f, Lambda(x, 1/(x + x**2))) == Rational(11, 3)
|
| 419 |
+
assert RootSum(f, Lambda(x, y/(x + x**2))) == Rational(11, 3)*y
|
| 420 |
+
|
| 421 |
+
assert RootSum(x**2 - 1, Lambda(x, 3*x**2), x) == 6
|
| 422 |
+
assert RootSum(x**2 - y, Lambda(x, 3*x**2), x) == 6*y
|
| 423 |
+
|
| 424 |
+
assert RootSum(x**2 - 1, Lambda(x, z*x**2), x) == 2*z
|
| 425 |
+
assert RootSum(x**2 - y, Lambda(x, z*x**2), x) == 2*z*y
|
| 426 |
+
|
| 427 |
+
assert RootSum(
|
| 428 |
+
x**2 - 1, Lambda(x, exp(x)), quadratic=True) == exp(-1) + exp(1)
|
| 429 |
+
|
| 430 |
+
assert RootSum(x**3 + a*x + a**3, tan, x) == \
|
| 431 |
+
RootSum(x**3 + x + 1, Lambda(x, tan(a*x)))
|
| 432 |
+
assert RootSum(a**3*x**3 + a*x + 1, tan, x) == \
|
| 433 |
+
RootSum(x**3 + x + 1, Lambda(x, tan(x/a)))
|
| 434 |
+
|
| 435 |
+
|
| 436 |
+
def test_RootSum_free_symbols():
|
| 437 |
+
assert RootSum(x**3 + x + 3, Lambda(r, exp(r))).free_symbols == set()
|
| 438 |
+
assert RootSum(x**3 + x + 3, Lambda(r, exp(a*r))).free_symbols == {a}
|
| 439 |
+
assert RootSum(
|
| 440 |
+
x**3 + x + y, Lambda(r, exp(a*r)), x).free_symbols == {a, y}
|
| 441 |
+
|
| 442 |
+
|
| 443 |
+
def test_RootSum___eq__():
|
| 444 |
+
f = Lambda(x, exp(x))
|
| 445 |
+
|
| 446 |
+
assert (RootSum(x**3 + x + 1, f) == RootSum(x**3 + x + 1, f)) is True
|
| 447 |
+
assert (RootSum(x**3 + x + 1, f) == RootSum(y**3 + y + 1, f)) is True
|
| 448 |
+
|
| 449 |
+
assert (RootSum(x**3 + x + 1, f) == RootSum(x**3 + x + 2, f)) is False
|
| 450 |
+
assert (RootSum(x**3 + x + 1, f) == RootSum(y**3 + y + 2, f)) is False
|
| 451 |
+
|
| 452 |
+
|
| 453 |
+
def test_RootSum_doit():
|
| 454 |
+
rs = RootSum(x**2 + 1, exp)
|
| 455 |
+
|
| 456 |
+
assert isinstance(rs, RootSum) is True
|
| 457 |
+
assert rs.doit() == exp(-I) + exp(I)
|
| 458 |
+
|
| 459 |
+
rs = RootSum(x**2 + a, exp, x)
|
| 460 |
+
|
| 461 |
+
assert isinstance(rs, RootSum) is True
|
| 462 |
+
assert rs.doit() == exp(-sqrt(-a)) + exp(sqrt(-a))
|
| 463 |
+
|
| 464 |
+
|
| 465 |
+
def test_RootSum_evalf():
|
| 466 |
+
rs = RootSum(x**2 + 1, exp)
|
| 467 |
+
|
| 468 |
+
assert rs.evalf(n=20, chop=True).epsilon_eq(Float("1.0806046117362794348"))
|
| 469 |
+
assert rs.evalf(n=15, chop=True).epsilon_eq(Float("1.08060461173628"))
|
| 470 |
+
|
| 471 |
+
rs = RootSum(x**2 + a, exp, x)
|
| 472 |
+
|
| 473 |
+
assert rs.evalf() == rs
|
| 474 |
+
|
| 475 |
+
|
| 476 |
+
def test_RootSum_diff():
|
| 477 |
+
f = x**3 + x + 3
|
| 478 |
+
|
| 479 |
+
g = Lambda(r, exp(r*x))
|
| 480 |
+
h = Lambda(r, r*exp(r*x))
|
| 481 |
+
|
| 482 |
+
assert RootSum(f, g).diff(x) == RootSum(f, h)
|
| 483 |
+
|
| 484 |
+
|
| 485 |
+
def test_RootSum_subs():
|
| 486 |
+
f = x**3 + x + 3
|
| 487 |
+
g = Lambda(r, exp(r*x))
|
| 488 |
+
|
| 489 |
+
F = y**3 + y + 3
|
| 490 |
+
G = Lambda(r, exp(r*y))
|
| 491 |
+
|
| 492 |
+
assert RootSum(f, g).subs(y, 1) == RootSum(f, g)
|
| 493 |
+
assert RootSum(f, g).subs(x, y) == RootSum(F, G)
|
| 494 |
+
|
| 495 |
+
|
| 496 |
+
def test_RootSum_rational():
|
| 497 |
+
assert RootSum(
|
| 498 |
+
z**5 - z + 1, Lambda(z, z/(x - z))) == (4*x - 5)/(x**5 - x + 1)
|
| 499 |
+
|
| 500 |
+
f = 161*z**3 + 115*z**2 + 19*z + 1
|
| 501 |
+
g = Lambda(z, z*log(
|
| 502 |
+
-3381*z**4/4 - 3381*z**3/4 - 625*z**2/2 - z*Rational(125, 2) - 5 + exp(x)))
|
| 503 |
+
|
| 504 |
+
assert RootSum(f, g).diff(x) == -(
|
| 505 |
+
(5*exp(2*x) - 6*exp(x) + 4)*exp(x)/(exp(3*x) - exp(2*x) + 1))/7
|
| 506 |
+
|
| 507 |
+
|
| 508 |
+
def test_RootSum_independent():
|
| 509 |
+
f = (x**3 - a)**2*(x**4 - b)**3
|
| 510 |
+
|
| 511 |
+
g = Lambda(x, 5*tan(x) + 7)
|
| 512 |
+
h = Lambda(x, tan(x))
|
| 513 |
+
|
| 514 |
+
r0 = RootSum(x**3 - a, h, x)
|
| 515 |
+
r1 = RootSum(x**4 - b, h, x)
|
| 516 |
+
|
| 517 |
+
assert RootSum(f, g, x).as_ordered_terms() == [10*r0, 15*r1, 126]
|
| 518 |
+
|
| 519 |
+
|
| 520 |
+
def test_issue_7876():
|
| 521 |
+
l1 = Poly(x**6 - x + 1, x).all_roots()
|
| 522 |
+
l2 = [rootof(x**6 - x + 1, i) for i in range(6)]
|
| 523 |
+
assert frozenset(l1) == frozenset(l2)
|
| 524 |
+
|
| 525 |
+
|
| 526 |
+
def test_issue_8316():
|
| 527 |
+
f = Poly(7*x**8 - 9)
|
| 528 |
+
assert len(f.all_roots()) == 8
|
| 529 |
+
f = Poly(7*x**8 - 10)
|
| 530 |
+
assert len(f.all_roots()) == 8
|
| 531 |
+
|
| 532 |
+
|
| 533 |
+
def test__imag_count():
|
| 534 |
+
from sympy.polys.rootoftools import _imag_count_of_factor
|
| 535 |
+
def imag_count(p):
|
| 536 |
+
return sum(_imag_count_of_factor(f)*m for f, m in
|
| 537 |
+
p.factor_list()[1])
|
| 538 |
+
assert imag_count(Poly(x**6 + 10*x**2 + 1)) == 2
|
| 539 |
+
assert imag_count(Poly(x**2)) == 0
|
| 540 |
+
assert imag_count(Poly([1]*3 + [-1], x)) == 0
|
| 541 |
+
assert imag_count(Poly(x**3 + 1)) == 0
|
| 542 |
+
assert imag_count(Poly(x**2 + 1)) == 2
|
| 543 |
+
assert imag_count(Poly(x**2 - 1)) == 0
|
| 544 |
+
assert imag_count(Poly(x**4 - 1)) == 2
|
| 545 |
+
assert imag_count(Poly(x**4 + 1)) == 0
|
| 546 |
+
assert imag_count(Poly([1, 2, 3], x)) == 0
|
| 547 |
+
assert imag_count(Poly(x**3 + x + 1)) == 0
|
| 548 |
+
assert imag_count(Poly(x**4 + x + 1)) == 0
|
| 549 |
+
def q(r1, r2, p):
|
| 550 |
+
return Poly(((x - r1)*(x - r2)).subs(x, x**p), x)
|
| 551 |
+
assert imag_count(q(-1, -2, 2)) == 4
|
| 552 |
+
assert imag_count(q(-1, 2, 2)) == 2
|
| 553 |
+
assert imag_count(q(1, 2, 2)) == 0
|
| 554 |
+
assert imag_count(q(1, 2, 4)) == 4
|
| 555 |
+
assert imag_count(q(-1, 2, 4)) == 2
|
| 556 |
+
assert imag_count(q(-1, -2, 4)) == 0
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
def test_RootOf_is_imaginary():
|
| 560 |
+
r = RootOf(x**4 + 4*x**2 + 1, 1)
|
| 561 |
+
i = r._get_interval()
|
| 562 |
+
assert r.is_imaginary and i.ax*i.bx <= 0
|
| 563 |
+
|
| 564 |
+
|
| 565 |
+
def test_is_disjoint():
|
| 566 |
+
eq = x**3 + 5*x + 1
|
| 567 |
+
ir = rootof(eq, 0)._get_interval()
|
| 568 |
+
ii = rootof(eq, 1)._get_interval()
|
| 569 |
+
assert ir.is_disjoint(ii)
|
| 570 |
+
assert ii.is_disjoint(ir)
|
| 571 |
+
|
| 572 |
+
|
| 573 |
+
def test_pure_key_dict():
|
| 574 |
+
p = D()
|
| 575 |
+
assert (x in p) is False
|
| 576 |
+
assert (1 in p) is False
|
| 577 |
+
p[x] = 1
|
| 578 |
+
assert x in p
|
| 579 |
+
assert y in p
|
| 580 |
+
assert p[y] == 1
|
| 581 |
+
raises(KeyError, lambda: p[1])
|
| 582 |
+
def dont(k):
|
| 583 |
+
p[k] = 2
|
| 584 |
+
raises(ValueError, lambda: dont(1))
|
| 585 |
+
|
| 586 |
+
|
| 587 |
+
@slow
|
| 588 |
+
def test_eval_approx_relative():
|
| 589 |
+
CRootOf.clear_cache()
|
| 590 |
+
t = [CRootOf(x**3 + 10*x + 1, i) for i in range(3)]
|
| 591 |
+
assert [i.eval_rational(1e-1) for i in t] == [
|
| 592 |
+
Rational(-21, 220), Rational(15, 256) - I*805/256,
|
| 593 |
+
Rational(15, 256) + I*805/256]
|
| 594 |
+
t[0]._reset()
|
| 595 |
+
assert [i.eval_rational(1e-1, 1e-4) for i in t] == [
|
| 596 |
+
Rational(-21, 220), Rational(3275, 65536) - I*414645/131072,
|
| 597 |
+
Rational(3275, 65536) + I*414645/131072]
|
| 598 |
+
assert S(t[0]._get_interval().dx) < 1e-1
|
| 599 |
+
assert S(t[1]._get_interval().dx) < 1e-1
|
| 600 |
+
assert S(t[1]._get_interval().dy) < 1e-4
|
| 601 |
+
assert S(t[2]._get_interval().dx) < 1e-1
|
| 602 |
+
assert S(t[2]._get_interval().dy) < 1e-4
|
| 603 |
+
t[0]._reset()
|
| 604 |
+
assert [i.eval_rational(1e-4, 1e-4) for i in t] == [
|
| 605 |
+
Rational(-2001, 20020), Rational(6545, 131072) - I*414645/131072,
|
| 606 |
+
Rational(6545, 131072) + I*414645/131072]
|
| 607 |
+
assert S(t[0]._get_interval().dx) < 1e-4
|
| 608 |
+
assert S(t[1]._get_interval().dx) < 1e-4
|
| 609 |
+
assert S(t[1]._get_interval().dy) < 1e-4
|
| 610 |
+
assert S(t[2]._get_interval().dx) < 1e-4
|
| 611 |
+
assert S(t[2]._get_interval().dy) < 1e-4
|
| 612 |
+
# in the following, the actual relative precision is
|
| 613 |
+
# less than tested, but it should never be greater
|
| 614 |
+
t[0]._reset()
|
| 615 |
+
assert [i.eval_rational(n=2) for i in t] == [
|
| 616 |
+
Rational(-202201, 2024022), Rational(104755, 2097152) - I*6634255/2097152,
|
| 617 |
+
Rational(104755, 2097152) + I*6634255/2097152]
|
| 618 |
+
assert abs(S(t[0]._get_interval().dx)/t[0]) < 1e-2
|
| 619 |
+
assert abs(S(t[1]._get_interval().dx)/t[1]).n() < 1e-2
|
| 620 |
+
assert abs(S(t[1]._get_interval().dy)/t[1]).n() < 1e-2
|
| 621 |
+
assert abs(S(t[2]._get_interval().dx)/t[2]).n() < 1e-2
|
| 622 |
+
assert abs(S(t[2]._get_interval().dy)/t[2]).n() < 1e-2
|
| 623 |
+
t[0]._reset()
|
| 624 |
+
assert [i.eval_rational(n=3) for i in t] == [
|
| 625 |
+
Rational(-202201, 2024022), Rational(1676045, 33554432) - I*106148135/33554432,
|
| 626 |
+
Rational(1676045, 33554432) + I*106148135/33554432]
|
| 627 |
+
assert abs(S(t[0]._get_interval().dx)/t[0]) < 1e-3
|
| 628 |
+
assert abs(S(t[1]._get_interval().dx)/t[1]).n() < 1e-3
|
| 629 |
+
assert abs(S(t[1]._get_interval().dy)/t[1]).n() < 1e-3
|
| 630 |
+
assert abs(S(t[2]._get_interval().dx)/t[2]).n() < 1e-3
|
| 631 |
+
assert abs(S(t[2]._get_interval().dy)/t[2]).n() < 1e-3
|
| 632 |
+
|
| 633 |
+
t[0]._reset()
|
| 634 |
+
a = [i.eval_approx(2) for i in t]
|
| 635 |
+
assert [str(i) for i in a] == [
|
| 636 |
+
'-0.10', '0.05 - 3.2*I', '0.05 + 3.2*I']
|
| 637 |
+
assert all(abs(((a[i] - t[i])/t[i]).n()) < 1e-2 for i in range(len(a)))
|
| 638 |
+
|
| 639 |
+
|
| 640 |
+
def test_issue_15920():
|
| 641 |
+
r = rootof(x**5 - x + 1, 0)
|
| 642 |
+
p = Integral(x, (x, 1, y))
|
| 643 |
+
assert unchanged(Eq, r, p)
|
| 644 |
+
|
| 645 |
+
|
| 646 |
+
def test_issue_19113():
|
| 647 |
+
eq = y**3 - y + 1
|
| 648 |
+
# generator is a canonical x in RootOf
|
| 649 |
+
assert str(Poly(eq).real_roots()) == '[CRootOf(x**3 - x + 1, 0)]'
|
| 650 |
+
assert str(Poly(eq.subs(y, tan(y))).real_roots()
|
| 651 |
+
) == '[CRootOf(x**3 - x + 1, 0)]'
|
| 652 |
+
assert str(Poly(eq.subs(y, tan(x))).real_roots()
|
| 653 |
+
) == '[CRootOf(x**3 - x + 1, 0)]'
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_specialpolys.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for functions for generating interesting polynomials. """
|
| 2 |
+
|
| 3 |
+
from sympy.core.add import Add
|
| 4 |
+
from sympy.core.symbol import symbols
|
| 5 |
+
from sympy.functions.elementary.miscellaneous import sqrt
|
| 6 |
+
from sympy.ntheory.generate import prime
|
| 7 |
+
from sympy.polys.domains.integerring import ZZ
|
| 8 |
+
from sympy.polys.polytools import Poly
|
| 9 |
+
from sympy.utilities.iterables import permute_signs
|
| 10 |
+
from sympy.testing.pytest import raises
|
| 11 |
+
|
| 12 |
+
from sympy.polys.specialpolys import (
|
| 13 |
+
swinnerton_dyer_poly,
|
| 14 |
+
cyclotomic_poly,
|
| 15 |
+
symmetric_poly,
|
| 16 |
+
random_poly,
|
| 17 |
+
interpolating_poly,
|
| 18 |
+
fateman_poly_F_1,
|
| 19 |
+
dmp_fateman_poly_F_1,
|
| 20 |
+
fateman_poly_F_2,
|
| 21 |
+
dmp_fateman_poly_F_2,
|
| 22 |
+
fateman_poly_F_3,
|
| 23 |
+
dmp_fateman_poly_F_3,
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
from sympy.abc import x, y, z
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def test_swinnerton_dyer_poly():
|
| 30 |
+
raises(ValueError, lambda: swinnerton_dyer_poly(0, x))
|
| 31 |
+
|
| 32 |
+
assert swinnerton_dyer_poly(1, x, polys=True) == Poly(x**2 - 2)
|
| 33 |
+
|
| 34 |
+
assert swinnerton_dyer_poly(1, x) == x**2 - 2
|
| 35 |
+
assert swinnerton_dyer_poly(2, x) == x**4 - 10*x**2 + 1
|
| 36 |
+
assert swinnerton_dyer_poly(
|
| 37 |
+
3, x) == x**8 - 40*x**6 + 352*x**4 - 960*x**2 + 576
|
| 38 |
+
# we only need to check that the polys arg works but
|
| 39 |
+
# we may as well test that the roots are correct
|
| 40 |
+
p = [sqrt(prime(i)) for i in range(1, 5)]
|
| 41 |
+
assert str([i.n(3) for i in
|
| 42 |
+
swinnerton_dyer_poly(4, polys=True).all_roots()]
|
| 43 |
+
) == str(sorted([Add(*i).n(3) for i in permute_signs(p)]))
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def test_cyclotomic_poly():
|
| 47 |
+
raises(ValueError, lambda: cyclotomic_poly(0, x))
|
| 48 |
+
|
| 49 |
+
assert cyclotomic_poly(1, x, polys=True) == Poly(x - 1)
|
| 50 |
+
|
| 51 |
+
assert cyclotomic_poly(1, x) == x - 1
|
| 52 |
+
assert cyclotomic_poly(2, x) == x + 1
|
| 53 |
+
assert cyclotomic_poly(3, x) == x**2 + x + 1
|
| 54 |
+
assert cyclotomic_poly(4, x) == x**2 + 1
|
| 55 |
+
assert cyclotomic_poly(5, x) == x**4 + x**3 + x**2 + x + 1
|
| 56 |
+
assert cyclotomic_poly(6, x) == x**2 - x + 1
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def test_symmetric_poly():
|
| 60 |
+
raises(ValueError, lambda: symmetric_poly(-1, x, y, z))
|
| 61 |
+
raises(ValueError, lambda: symmetric_poly(5, x, y, z))
|
| 62 |
+
|
| 63 |
+
assert symmetric_poly(1, x, y, z, polys=True) == Poly(x + y + z)
|
| 64 |
+
assert symmetric_poly(1, (x, y, z), polys=True) == Poly(x + y + z)
|
| 65 |
+
|
| 66 |
+
assert symmetric_poly(0, x, y, z) == 1
|
| 67 |
+
assert symmetric_poly(1, x, y, z) == x + y + z
|
| 68 |
+
assert symmetric_poly(2, x, y, z) == x*y + x*z + y*z
|
| 69 |
+
assert symmetric_poly(3, x, y, z) == x*y*z
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def test_random_poly():
|
| 73 |
+
poly = random_poly(x, 10, -100, 100, polys=False)
|
| 74 |
+
|
| 75 |
+
assert Poly(poly).degree() == 10
|
| 76 |
+
assert all(-100 <= coeff <= 100 for coeff in Poly(poly).coeffs()) is True
|
| 77 |
+
|
| 78 |
+
poly = random_poly(x, 10, -100, 100, polys=True)
|
| 79 |
+
|
| 80 |
+
assert poly.degree() == 10
|
| 81 |
+
assert all(-100 <= coeff <= 100 for coeff in poly.coeffs()) is True
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def test_interpolating_poly():
|
| 85 |
+
x0, x1, x2, x3, y0, y1, y2, y3 = symbols('x:4, y:4')
|
| 86 |
+
|
| 87 |
+
assert interpolating_poly(0, x) == 0
|
| 88 |
+
assert interpolating_poly(1, x) == y0
|
| 89 |
+
|
| 90 |
+
assert interpolating_poly(2, x) == \
|
| 91 |
+
y0*(x - x1)/(x0 - x1) + y1*(x - x0)/(x1 - x0)
|
| 92 |
+
|
| 93 |
+
assert interpolating_poly(3, x) == \
|
| 94 |
+
y0*(x - x1)*(x - x2)/((x0 - x1)*(x0 - x2)) + \
|
| 95 |
+
y1*(x - x0)*(x - x2)/((x1 - x0)*(x1 - x2)) + \
|
| 96 |
+
y2*(x - x0)*(x - x1)/((x2 - x0)*(x2 - x1))
|
| 97 |
+
|
| 98 |
+
assert interpolating_poly(4, x) == \
|
| 99 |
+
y0*(x - x1)*(x - x2)*(x - x3)/((x0 - x1)*(x0 - x2)*(x0 - x3)) + \
|
| 100 |
+
y1*(x - x0)*(x - x2)*(x - x3)/((x1 - x0)*(x1 - x2)*(x1 - x3)) + \
|
| 101 |
+
y2*(x - x0)*(x - x1)*(x - x3)/((x2 - x0)*(x2 - x1)*(x2 - x3)) + \
|
| 102 |
+
y3*(x - x0)*(x - x1)*(x - x2)/((x3 - x0)*(x3 - x1)*(x3 - x2))
|
| 103 |
+
|
| 104 |
+
raises(ValueError, lambda:
|
| 105 |
+
interpolating_poly(2, x, (x, 2), (1, 3)))
|
| 106 |
+
raises(ValueError, lambda:
|
| 107 |
+
interpolating_poly(2, x, (x + y, 2), (1, 3)))
|
| 108 |
+
raises(ValueError, lambda:
|
| 109 |
+
interpolating_poly(2, x + y, (x, 2), (1, 3)))
|
| 110 |
+
raises(ValueError, lambda:
|
| 111 |
+
interpolating_poly(2, 3, (4, 5), (6, 7)))
|
| 112 |
+
raises(ValueError, lambda:
|
| 113 |
+
interpolating_poly(2, 3, (4, 5), (6, 7, 8)))
|
| 114 |
+
assert interpolating_poly(0, x, (1, 2), (3, 4)) == 0
|
| 115 |
+
assert interpolating_poly(1, x, (1, 2), (3, 4)) == 3
|
| 116 |
+
assert interpolating_poly(2, x, (1, 2), (3, 4)) == x + 2
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def test_fateman_poly_F_1():
|
| 120 |
+
f, g, h = fateman_poly_F_1(1)
|
| 121 |
+
F, G, H = dmp_fateman_poly_F_1(1, ZZ)
|
| 122 |
+
|
| 123 |
+
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
|
| 124 |
+
|
| 125 |
+
f, g, h = fateman_poly_F_1(3)
|
| 126 |
+
F, G, H = dmp_fateman_poly_F_1(3, ZZ)
|
| 127 |
+
|
| 128 |
+
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
def test_fateman_poly_F_2():
|
| 132 |
+
f, g, h = fateman_poly_F_2(1)
|
| 133 |
+
F, G, H = dmp_fateman_poly_F_2(1, ZZ)
|
| 134 |
+
|
| 135 |
+
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
|
| 136 |
+
|
| 137 |
+
f, g, h = fateman_poly_F_2(3)
|
| 138 |
+
F, G, H = dmp_fateman_poly_F_2(3, ZZ)
|
| 139 |
+
|
| 140 |
+
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def test_fateman_poly_F_3():
|
| 144 |
+
f, g, h = fateman_poly_F_3(1)
|
| 145 |
+
F, G, H = dmp_fateman_poly_F_3(1, ZZ)
|
| 146 |
+
|
| 147 |
+
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
|
| 148 |
+
|
| 149 |
+
f, g, h = fateman_poly_F_3(3)
|
| 150 |
+
F, G, H = dmp_fateman_poly_F_3(3, ZZ)
|
| 151 |
+
|
| 152 |
+
assert [ t.rep.to_list() for t in [f, g, h] ] == [F, G, H]
|
evalkit_internvl/lib/python3.10/site-packages/sympy/polys/tests/test_sqfreetools.py
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Tests for square-free decomposition algorithms and related tools. """
|
| 2 |
+
|
| 3 |
+
from sympy.polys.rings import ring
|
| 4 |
+
from sympy.polys.domains import FF, ZZ, QQ
|
| 5 |
+
from sympy.polys.specialpolys import f_polys
|
| 6 |
+
|
| 7 |
+
from sympy.testing.pytest import raises
|
| 8 |
+
from sympy.external.gmpy import MPQ
|
| 9 |
+
|
| 10 |
+
f_0, f_1, f_2, f_3, f_4, f_5, f_6 = f_polys()
|
| 11 |
+
|
| 12 |
+
def test_dup_sqf():
|
| 13 |
+
R, x = ring("x", ZZ)
|
| 14 |
+
|
| 15 |
+
assert R.dup_sqf_part(0) == 0
|
| 16 |
+
assert R.dup_sqf_p(0) is True
|
| 17 |
+
|
| 18 |
+
assert R.dup_sqf_part(7) == 1
|
| 19 |
+
assert R.dup_sqf_p(7) is True
|
| 20 |
+
|
| 21 |
+
assert R.dup_sqf_part(2*x + 2) == x + 1
|
| 22 |
+
assert R.dup_sqf_p(2*x + 2) is True
|
| 23 |
+
|
| 24 |
+
assert R.dup_sqf_part(x**3 + x + 1) == x**3 + x + 1
|
| 25 |
+
assert R.dup_sqf_p(x**3 + x + 1) is True
|
| 26 |
+
|
| 27 |
+
assert R.dup_sqf_part(-x**3 + x + 1) == x**3 - x - 1
|
| 28 |
+
assert R.dup_sqf_p(-x**3 + x + 1) is True
|
| 29 |
+
|
| 30 |
+
assert R.dup_sqf_part(2*x**3 + 3*x**2) == 2*x**2 + 3*x
|
| 31 |
+
assert R.dup_sqf_p(2*x**3 + 3*x**2) is False
|
| 32 |
+
|
| 33 |
+
assert R.dup_sqf_part(-2*x**3 + 3*x**2) == 2*x**2 - 3*x
|
| 34 |
+
assert R.dup_sqf_p(-2*x**3 + 3*x**2) is False
|
| 35 |
+
|
| 36 |
+
assert R.dup_sqf_list(0) == (0, [])
|
| 37 |
+
assert R.dup_sqf_list(1) == (1, [])
|
| 38 |
+
|
| 39 |
+
assert R.dup_sqf_list(x) == (1, [(x, 1)])
|
| 40 |
+
assert R.dup_sqf_list(2*x**2) == (2, [(x, 2)])
|
| 41 |
+
assert R.dup_sqf_list(3*x**3) == (3, [(x, 3)])
|
| 42 |
+
|
| 43 |
+
assert R.dup_sqf_list(-x**5 + x**4 + x - 1) == \
|
| 44 |
+
(-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)])
|
| 45 |
+
assert R.dup_sqf_list(x**8 + 6*x**6 + 12*x**4 + 8*x**2) == \
|
| 46 |
+
( 1, [(x, 2), (x**2 + 2, 3)])
|
| 47 |
+
|
| 48 |
+
assert R.dup_sqf_list(2*x**2 + 4*x + 2) == (2, [(x + 1, 2)])
|
| 49 |
+
|
| 50 |
+
R, x = ring("x", QQ)
|
| 51 |
+
assert R.dup_sqf_list(2*x**2 + 4*x + 2) == (2, [(x + 1, 2)])
|
| 52 |
+
|
| 53 |
+
R, x = ring("x", FF(2))
|
| 54 |
+
assert R.dup_sqf_list(x**2 + 1) == (1, [(x + 1, 2)])
|
| 55 |
+
|
| 56 |
+
R, x = ring("x", FF(3))
|
| 57 |
+
assert R.dup_sqf_list(x**10 + 2*x**7 + 2*x**4 + x) == \
|
| 58 |
+
(1, [(x, 1),
|
| 59 |
+
(x + 1, 3),
|
| 60 |
+
(x + 2, 6)])
|
| 61 |
+
|
| 62 |
+
R1, x = ring("x", ZZ)
|
| 63 |
+
R2, y = ring("y", FF(3))
|
| 64 |
+
|
| 65 |
+
f = x**3 + 1
|
| 66 |
+
g = y**3 + 1
|
| 67 |
+
|
| 68 |
+
assert R1.dup_sqf_part(f) == f
|
| 69 |
+
assert R2.dup_sqf_part(g) == y + 1
|
| 70 |
+
|
| 71 |
+
assert R1.dup_sqf_p(f) is True
|
| 72 |
+
assert R2.dup_sqf_p(g) is False
|
| 73 |
+
|
| 74 |
+
R, x, y = ring("x,y", ZZ)
|
| 75 |
+
|
| 76 |
+
A = x**4 - 3*x**2 + 6
|
| 77 |
+
D = x**6 - 5*x**4 + 5*x**2 + 4
|
| 78 |
+
|
| 79 |
+
f, g = D, R.dmp_sub(A, R.dmp_mul(R.dmp_diff(D, 1), y))
|
| 80 |
+
res = R.dmp_resultant(f, g)
|
| 81 |
+
h = (4*y**2 + 1).drop(x)
|
| 82 |
+
|
| 83 |
+
assert R.drop(x).dup_sqf_list(res) == (45796, [(h, 3)])
|
| 84 |
+
|
| 85 |
+
Rt, t = ring("t", ZZ)
|
| 86 |
+
R, x = ring("x", Rt)
|
| 87 |
+
assert R.dup_sqf_list_include(t**3*x**2) == [(t**3, 1), (x, 2)]
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
def test_dmp_sqf():
|
| 91 |
+
R, x, y = ring("x,y", ZZ)
|
| 92 |
+
assert R.dmp_sqf_part(0) == 0
|
| 93 |
+
assert R.dmp_sqf_p(0) is True
|
| 94 |
+
|
| 95 |
+
assert R.dmp_sqf_part(7) == 1
|
| 96 |
+
assert R.dmp_sqf_p(7) is True
|
| 97 |
+
|
| 98 |
+
assert R.dmp_sqf_list(3) == (3, [])
|
| 99 |
+
assert R.dmp_sqf_list_include(3) == [(3, 1)]
|
| 100 |
+
|
| 101 |
+
R, x, y, z = ring("x,y,z", ZZ)
|
| 102 |
+
assert R.dmp_sqf_p(f_0) is True
|
| 103 |
+
assert R.dmp_sqf_p(f_0**2) is False
|
| 104 |
+
assert R.dmp_sqf_p(f_1) is True
|
| 105 |
+
assert R.dmp_sqf_p(f_1**2) is False
|
| 106 |
+
assert R.dmp_sqf_p(f_2) is True
|
| 107 |
+
assert R.dmp_sqf_p(f_2**2) is False
|
| 108 |
+
assert R.dmp_sqf_p(f_3) is True
|
| 109 |
+
assert R.dmp_sqf_p(f_3**2) is False
|
| 110 |
+
assert R.dmp_sqf_p(f_5) is False
|
| 111 |
+
assert R.dmp_sqf_p(f_5**2) is False
|
| 112 |
+
|
| 113 |
+
assert R.dmp_sqf_p(f_4) is True
|
| 114 |
+
assert R.dmp_sqf_part(f_4) == -f_4
|
| 115 |
+
|
| 116 |
+
assert R.dmp_sqf_part(f_5) == x + y - z
|
| 117 |
+
|
| 118 |
+
R, x, y, z, t = ring("x,y,z,t", ZZ)
|
| 119 |
+
assert R.dmp_sqf_p(f_6) is True
|
| 120 |
+
assert R.dmp_sqf_part(f_6) == f_6
|
| 121 |
+
|
| 122 |
+
R, x = ring("x", ZZ)
|
| 123 |
+
f = -x**5 + x**4 + x - 1
|
| 124 |
+
|
| 125 |
+
assert R.dmp_sqf_list(f) == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)])
|
| 126 |
+
assert R.dmp_sqf_list_include(f) == [(-x**3 - x**2 - x - 1, 1), (x - 1, 2)]
|
| 127 |
+
|
| 128 |
+
R, x, y = ring("x,y", ZZ)
|
| 129 |
+
f = -x**5 + x**4 + x - 1
|
| 130 |
+
|
| 131 |
+
assert R.dmp_sqf_list(f) == (-1, [(x**3 + x**2 + x + 1, 1), (x - 1, 2)])
|
| 132 |
+
assert R.dmp_sqf_list_include(f) == [(-x**3 - x**2 - x - 1, 1), (x - 1, 2)]
|
| 133 |
+
|
| 134 |
+
f = -x**2 + 2*x - 1
|
| 135 |
+
assert R.dmp_sqf_list_include(f) == [(-1, 1), (x - 1, 2)]
|
| 136 |
+
|
| 137 |
+
f = (y**2 + 1)**2*(x**2 + 2*x + 2)
|
| 138 |
+
assert R.dmp_sqf_p(f) is False
|
| 139 |
+
assert R.dmp_sqf_list(f) == (1, [(x**2 + 2*x + 2, 1), (y**2 + 1, 2)])
|
| 140 |
+
|
| 141 |
+
R, x, y = ring("x,y", FF(2))
|
| 142 |
+
raises(NotImplementedError, lambda: R.dmp_sqf_list(y**2 + 1))
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
def test_dup_gff_list():
|
| 146 |
+
R, x = ring("x", ZZ)
|
| 147 |
+
|
| 148 |
+
f = x**5 + 2*x**4 - x**3 - 2*x**2
|
| 149 |
+
assert R.dup_gff_list(f) == [(x, 1), (x + 2, 4)]
|
| 150 |
+
|
| 151 |
+
g = x**9 - 20*x**8 + 166*x**7 - 744*x**6 + 1965*x**5 - 3132*x**4 + 2948*x**3 - 1504*x**2 + 320*x
|
| 152 |
+
assert R.dup_gff_list(g) == [(x**2 - 5*x + 4, 1), (x**2 - 5*x + 4, 2), (x, 3)]
|
| 153 |
+
|
| 154 |
+
raises(ValueError, lambda: R.dup_gff_list(0))
|
| 155 |
+
|
| 156 |
+
def test_issue_26178():
|
| 157 |
+
R, x, y, z = ring(['x', 'y', 'z'], QQ)
|
| 158 |
+
assert (x**2 - 2*y**2 + 1).sqf_list() == (MPQ(1,1), [(x**2 - 2*y**2 + 1, 1)])
|
| 159 |
+
assert (x**2 - 2*z**2 + 1).sqf_list() == (MPQ(1,1), [(x**2 - 2*z**2 + 1, 1)])
|
| 160 |
+
assert (y**2 - 2*z**2 + 1).sqf_list() == (MPQ(1,1), [(y**2 - 2*z**2 + 1, 1)])
|
evalkit_internvl/lib/python3.10/site-packages/sympy/stats/__pycache__/crv_types.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:21065f3ec96be31a86edd9d79fe5d1446785eb2778d8e763c82c51dc673e5d78
|
| 3 |
+
size 129032
|
evalkit_tf437/lib/python3.10/site-packages/google_auth_oauthlib-1.2.1.dist-info/METADATA
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Metadata-Version: 2.1
|
| 2 |
+
Name: google-auth-oauthlib
|
| 3 |
+
Version: 1.2.1
|
| 4 |
+
Summary: Google Authentication Library
|
| 5 |
+
Home-page: https://github.com/GoogleCloudPlatform/google-auth-library-python-oauthlib
|
| 6 |
+
Author: Google Cloud Platform
|
| 7 |
+
Author-email: googleapis-packages@google.com
|
| 8 |
+
License: Apache 2.0
|
| 9 |
+
Keywords: google auth oauth client oauthlib
|
| 10 |
+
Classifier: Programming Language :: Python :: 3
|
| 11 |
+
Classifier: Programming Language :: Python :: 3.6
|
| 12 |
+
Classifier: Programming Language :: Python :: 3.7
|
| 13 |
+
Classifier: Programming Language :: Python :: 3.8
|
| 14 |
+
Classifier: Programming Language :: Python :: 3.9
|
| 15 |
+
Classifier: Programming Language :: Python :: 3.10
|
| 16 |
+
Classifier: Programming Language :: Python :: 3.11
|
| 17 |
+
Classifier: Programming Language :: Python :: 3.12
|
| 18 |
+
Classifier: Development Status :: 5 - Production/Stable
|
| 19 |
+
Classifier: Intended Audience :: Developers
|
| 20 |
+
Classifier: License :: OSI Approved :: Apache Software License
|
| 21 |
+
Classifier: Operating System :: POSIX
|
| 22 |
+
Classifier: Operating System :: Microsoft :: Windows
|
| 23 |
+
Classifier: Operating System :: MacOS :: MacOS X
|
| 24 |
+
Classifier: Operating System :: OS Independent
|
| 25 |
+
Classifier: Topic :: Internet :: WWW/HTTP
|
| 26 |
+
Requires-Python: >=3.6
|
| 27 |
+
License-File: LICENSE
|
| 28 |
+
Requires-Dist: google-auth >=2.15.0
|
| 29 |
+
Requires-Dist: requests-oauthlib >=0.7.0
|
| 30 |
+
Provides-Extra: tool
|
| 31 |
+
Requires-Dist: click >=6.0.0 ; extra == 'tool'
|
| 32 |
+
|
| 33 |
+
oauthlib integration for Google Auth
|
| 34 |
+
====================================
|
| 35 |
+
|
| 36 |
+
|pypi|
|
| 37 |
+
|
| 38 |
+
This library provides `oauthlib`_ integration with `google-auth`_.
|
| 39 |
+
|
| 40 |
+
.. |build| image:: https://travis-ci.org/googleapis/google-auth-library-python-oauthlib.svg?branch=main
|
| 41 |
+
:target: https://googleapis.dev/python/google-auth-oauthlib/latest/index.html
|
| 42 |
+
.. |pypi| image:: https://img.shields.io/pypi/v/google-auth-oauthlib.svg
|
| 43 |
+
:target: https://pypi.python.org/pypi/google-auth-oauthlib
|
| 44 |
+
|
| 45 |
+
.. _oauthlib: https://github.com/idan/oauthlib
|
| 46 |
+
.. _google-auth: https://github.com/googleapis/google-auth-library-python
|
| 47 |
+
|
| 48 |
+
Installing
|
| 49 |
+
----------
|
| 50 |
+
|
| 51 |
+
You can install using `pip`_::
|
| 52 |
+
|
| 53 |
+
$ pip install google-auth-oauthlib
|
| 54 |
+
|
| 55 |
+
.. _pip: https://pip.pypa.io/en/stable/
|
| 56 |
+
|
| 57 |
+
Documentation
|
| 58 |
+
-------------
|
| 59 |
+
|
| 60 |
+
The latest documentation is available at `google-auth-oauthlib.googleapis.dev`_.
|
| 61 |
+
|
| 62 |
+
.. _google-auth-oauthlib.googleapis.dev: https://googleapis.dev/python/google-auth-oauthlib/latest/index.html
|
| 63 |
+
|
| 64 |
+
Supported Python Versions
|
| 65 |
+
-------------------------
|
| 66 |
+
Python >= 3.6
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
Unsupported Python Versions
|
| 70 |
+
---------------------------
|
| 71 |
+
|
| 72 |
+
Python == 2.7, Python == 3.5.
|
| 73 |
+
|
| 74 |
+
The last version of this library compatible with Python 2.7 and 3.5 is
|
| 75 |
+
`google-auth-oauthlib==0.4.1`.
|
| 76 |
+
|
| 77 |
+
License
|
| 78 |
+
-------
|
| 79 |
+
|
| 80 |
+
Apache 2.0 - See `the LICENSE`_ for more information.
|
| 81 |
+
|
| 82 |
+
.. _the LICENSE: https://github.com/googleapis/google-auth-library-python-oauthlib/blob/main/LICENSE
|
evalkit_tf437/lib/python3.10/site-packages/google_auth_oauthlib-1.2.1.dist-info/top_level.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
docs
|
| 2 |
+
google_auth_oauthlib
|
| 3 |
+
scripts
|
| 4 |
+
testing
|
evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (1.47 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/__pycache__/ansi.cpython-310.pyc
ADDED
|
Binary file (6.33 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/__pycache__/html.cpython-310.pyc
ADDED
|
Binary file (4.32 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/__pycache__/pygments.cpython-310.pyc
ADDED
|
Binary file (1.25 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/__pycache__/utils.cpython-310.pyc
ADDED
|
Binary file (3.28 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/formatted_text/ansi.py
ADDED
|
@@ -0,0 +1,297 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from __future__ import annotations
|
| 2 |
+
|
| 3 |
+
from string import Formatter
|
| 4 |
+
from typing import Generator
|
| 5 |
+
|
| 6 |
+
from prompt_toolkit.output.vt100 import BG_ANSI_COLORS, FG_ANSI_COLORS
|
| 7 |
+
from prompt_toolkit.output.vt100 import _256_colors as _256_colors_table
|
| 8 |
+
|
| 9 |
+
from .base import StyleAndTextTuples
|
| 10 |
+
|
| 11 |
+
__all__ = [
|
| 12 |
+
"ANSI",
|
| 13 |
+
"ansi_escape",
|
| 14 |
+
]
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
class ANSI:
|
| 18 |
+
"""
|
| 19 |
+
ANSI formatted text.
|
| 20 |
+
Take something ANSI escaped text, for use as a formatted string. E.g.
|
| 21 |
+
|
| 22 |
+
::
|
| 23 |
+
|
| 24 |
+
ANSI('\\x1b[31mhello \\x1b[32mworld')
|
| 25 |
+
|
| 26 |
+
Characters between ``\\001`` and ``\\002`` are supposed to have a zero width
|
| 27 |
+
when printed, but these are literally sent to the terminal output. This can
|
| 28 |
+
be used for instance, for inserting Final Term prompt commands. They will
|
| 29 |
+
be translated into a prompt_toolkit '[ZeroWidthEscape]' fragment.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
def __init__(self, value: str) -> None:
|
| 33 |
+
self.value = value
|
| 34 |
+
self._formatted_text: StyleAndTextTuples = []
|
| 35 |
+
|
| 36 |
+
# Default style attributes.
|
| 37 |
+
self._color: str | None = None
|
| 38 |
+
self._bgcolor: str | None = None
|
| 39 |
+
self._bold = False
|
| 40 |
+
self._underline = False
|
| 41 |
+
self._strike = False
|
| 42 |
+
self._italic = False
|
| 43 |
+
self._blink = False
|
| 44 |
+
self._reverse = False
|
| 45 |
+
self._hidden = False
|
| 46 |
+
|
| 47 |
+
# Process received text.
|
| 48 |
+
parser = self._parse_corot()
|
| 49 |
+
parser.send(None) # type: ignore
|
| 50 |
+
for c in value:
|
| 51 |
+
parser.send(c)
|
| 52 |
+
|
| 53 |
+
def _parse_corot(self) -> Generator[None, str, None]:
|
| 54 |
+
"""
|
| 55 |
+
Coroutine that parses the ANSI escape sequences.
|
| 56 |
+
"""
|
| 57 |
+
style = ""
|
| 58 |
+
formatted_text = self._formatted_text
|
| 59 |
+
|
| 60 |
+
while True:
|
| 61 |
+
# NOTE: CSI is a special token within a stream of characters that
|
| 62 |
+
# introduces an ANSI control sequence used to set the
|
| 63 |
+
# style attributes of the following characters.
|
| 64 |
+
csi = False
|
| 65 |
+
|
| 66 |
+
c = yield
|
| 67 |
+
|
| 68 |
+
# Everything between \001 and \002 should become a ZeroWidthEscape.
|
| 69 |
+
if c == "\001":
|
| 70 |
+
escaped_text = ""
|
| 71 |
+
while c != "\002":
|
| 72 |
+
c = yield
|
| 73 |
+
if c == "\002":
|
| 74 |
+
formatted_text.append(("[ZeroWidthEscape]", escaped_text))
|
| 75 |
+
c = yield
|
| 76 |
+
break
|
| 77 |
+
else:
|
| 78 |
+
escaped_text += c
|
| 79 |
+
|
| 80 |
+
# Check for CSI
|
| 81 |
+
if c == "\x1b":
|
| 82 |
+
# Start of color escape sequence.
|
| 83 |
+
square_bracket = yield
|
| 84 |
+
if square_bracket == "[":
|
| 85 |
+
csi = True
|
| 86 |
+
else:
|
| 87 |
+
continue
|
| 88 |
+
elif c == "\x9b":
|
| 89 |
+
csi = True
|
| 90 |
+
|
| 91 |
+
if csi:
|
| 92 |
+
# Got a CSI sequence. Color codes are following.
|
| 93 |
+
current = ""
|
| 94 |
+
params = []
|
| 95 |
+
|
| 96 |
+
while True:
|
| 97 |
+
char = yield
|
| 98 |
+
|
| 99 |
+
# Construct number
|
| 100 |
+
if char.isdigit():
|
| 101 |
+
current += char
|
| 102 |
+
|
| 103 |
+
# Eval number
|
| 104 |
+
else:
|
| 105 |
+
# Limit and save number value
|
| 106 |
+
params.append(min(int(current or 0), 9999))
|
| 107 |
+
|
| 108 |
+
# Get delimiter token if present
|
| 109 |
+
if char == ";":
|
| 110 |
+
current = ""
|
| 111 |
+
|
| 112 |
+
# Check and evaluate color codes
|
| 113 |
+
elif char == "m":
|
| 114 |
+
# Set attributes and token.
|
| 115 |
+
self._select_graphic_rendition(params)
|
| 116 |
+
style = self._create_style_string()
|
| 117 |
+
break
|
| 118 |
+
|
| 119 |
+
# Check and evaluate cursor forward
|
| 120 |
+
elif char == "C":
|
| 121 |
+
for i in range(params[0]):
|
| 122 |
+
# add <SPACE> using current style
|
| 123 |
+
formatted_text.append((style, " "))
|
| 124 |
+
break
|
| 125 |
+
|
| 126 |
+
else:
|
| 127 |
+
# Ignore unsupported sequence.
|
| 128 |
+
break
|
| 129 |
+
else:
|
| 130 |
+
# Add current character.
|
| 131 |
+
# NOTE: At this point, we could merge the current character
|
| 132 |
+
# into the previous tuple if the style did not change,
|
| 133 |
+
# however, it's not worth the effort given that it will
|
| 134 |
+
# be "Exploded" once again when it's rendered to the
|
| 135 |
+
# output.
|
| 136 |
+
formatted_text.append((style, c))
|
| 137 |
+
|
| 138 |
+
def _select_graphic_rendition(self, attrs: list[int]) -> None:
|
| 139 |
+
"""
|
| 140 |
+
Taken a list of graphics attributes and apply changes.
|
| 141 |
+
"""
|
| 142 |
+
if not attrs:
|
| 143 |
+
attrs = [0]
|
| 144 |
+
else:
|
| 145 |
+
attrs = list(attrs[::-1])
|
| 146 |
+
|
| 147 |
+
while attrs:
|
| 148 |
+
attr = attrs.pop()
|
| 149 |
+
|
| 150 |
+
if attr in _fg_colors:
|
| 151 |
+
self._color = _fg_colors[attr]
|
| 152 |
+
elif attr in _bg_colors:
|
| 153 |
+
self._bgcolor = _bg_colors[attr]
|
| 154 |
+
elif attr == 1:
|
| 155 |
+
self._bold = True
|
| 156 |
+
# elif attr == 2:
|
| 157 |
+
# self._faint = True
|
| 158 |
+
elif attr == 3:
|
| 159 |
+
self._italic = True
|
| 160 |
+
elif attr == 4:
|
| 161 |
+
self._underline = True
|
| 162 |
+
elif attr == 5:
|
| 163 |
+
self._blink = True # Slow blink
|
| 164 |
+
elif attr == 6:
|
| 165 |
+
self._blink = True # Fast blink
|
| 166 |
+
elif attr == 7:
|
| 167 |
+
self._reverse = True
|
| 168 |
+
elif attr == 8:
|
| 169 |
+
self._hidden = True
|
| 170 |
+
elif attr == 9:
|
| 171 |
+
self._strike = True
|
| 172 |
+
elif attr == 22:
|
| 173 |
+
self._bold = False # Normal intensity
|
| 174 |
+
elif attr == 23:
|
| 175 |
+
self._italic = False
|
| 176 |
+
elif attr == 24:
|
| 177 |
+
self._underline = False
|
| 178 |
+
elif attr == 25:
|
| 179 |
+
self._blink = False
|
| 180 |
+
elif attr == 27:
|
| 181 |
+
self._reverse = False
|
| 182 |
+
elif attr == 28:
|
| 183 |
+
self._hidden = False
|
| 184 |
+
elif attr == 29:
|
| 185 |
+
self._strike = False
|
| 186 |
+
elif not attr:
|
| 187 |
+
# Reset all style attributes
|
| 188 |
+
self._color = None
|
| 189 |
+
self._bgcolor = None
|
| 190 |
+
self._bold = False
|
| 191 |
+
self._underline = False
|
| 192 |
+
self._strike = False
|
| 193 |
+
self._italic = False
|
| 194 |
+
self._blink = False
|
| 195 |
+
self._reverse = False
|
| 196 |
+
self._hidden = False
|
| 197 |
+
|
| 198 |
+
elif attr in (38, 48) and len(attrs) > 1:
|
| 199 |
+
n = attrs.pop()
|
| 200 |
+
|
| 201 |
+
# 256 colors.
|
| 202 |
+
if n == 5 and len(attrs) >= 1:
|
| 203 |
+
if attr == 38:
|
| 204 |
+
m = attrs.pop()
|
| 205 |
+
self._color = _256_colors.get(m)
|
| 206 |
+
elif attr == 48:
|
| 207 |
+
m = attrs.pop()
|
| 208 |
+
self._bgcolor = _256_colors.get(m)
|
| 209 |
+
|
| 210 |
+
# True colors.
|
| 211 |
+
if n == 2 and len(attrs) >= 3:
|
| 212 |
+
try:
|
| 213 |
+
color_str = (
|
| 214 |
+
f"#{attrs.pop():02x}{attrs.pop():02x}{attrs.pop():02x}"
|
| 215 |
+
)
|
| 216 |
+
except IndexError:
|
| 217 |
+
pass
|
| 218 |
+
else:
|
| 219 |
+
if attr == 38:
|
| 220 |
+
self._color = color_str
|
| 221 |
+
elif attr == 48:
|
| 222 |
+
self._bgcolor = color_str
|
| 223 |
+
|
| 224 |
+
def _create_style_string(self) -> str:
|
| 225 |
+
"""
|
| 226 |
+
Turn current style flags into a string for usage in a formatted text.
|
| 227 |
+
"""
|
| 228 |
+
result = []
|
| 229 |
+
if self._color:
|
| 230 |
+
result.append(self._color)
|
| 231 |
+
if self._bgcolor:
|
| 232 |
+
result.append("bg:" + self._bgcolor)
|
| 233 |
+
if self._bold:
|
| 234 |
+
result.append("bold")
|
| 235 |
+
if self._underline:
|
| 236 |
+
result.append("underline")
|
| 237 |
+
if self._strike:
|
| 238 |
+
result.append("strike")
|
| 239 |
+
if self._italic:
|
| 240 |
+
result.append("italic")
|
| 241 |
+
if self._blink:
|
| 242 |
+
result.append("blink")
|
| 243 |
+
if self._reverse:
|
| 244 |
+
result.append("reverse")
|
| 245 |
+
if self._hidden:
|
| 246 |
+
result.append("hidden")
|
| 247 |
+
|
| 248 |
+
return " ".join(result)
|
| 249 |
+
|
| 250 |
+
def __repr__(self) -> str:
|
| 251 |
+
return f"ANSI({self.value!r})"
|
| 252 |
+
|
| 253 |
+
def __pt_formatted_text__(self) -> StyleAndTextTuples:
|
| 254 |
+
return self._formatted_text
|
| 255 |
+
|
| 256 |
+
def format(self, *args: str, **kwargs: str) -> ANSI:
|
| 257 |
+
"""
|
| 258 |
+
Like `str.format`, but make sure that the arguments are properly
|
| 259 |
+
escaped. (No ANSI escapes can be injected.)
|
| 260 |
+
"""
|
| 261 |
+
return ANSI(FORMATTER.vformat(self.value, args, kwargs))
|
| 262 |
+
|
| 263 |
+
def __mod__(self, value: object) -> ANSI:
|
| 264 |
+
"""
|
| 265 |
+
ANSI('<b>%s</b>') % value
|
| 266 |
+
"""
|
| 267 |
+
if not isinstance(value, tuple):
|
| 268 |
+
value = (value,)
|
| 269 |
+
|
| 270 |
+
value = tuple(ansi_escape(i) for i in value)
|
| 271 |
+
return ANSI(self.value % value)
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
# Mapping of the ANSI color codes to their names.
|
| 275 |
+
_fg_colors = {v: k for k, v in FG_ANSI_COLORS.items()}
|
| 276 |
+
_bg_colors = {v: k for k, v in BG_ANSI_COLORS.items()}
|
| 277 |
+
|
| 278 |
+
# Mapping of the escape codes for 256colors to their 'ffffff' value.
|
| 279 |
+
_256_colors = {}
|
| 280 |
+
|
| 281 |
+
for i, (r, g, b) in enumerate(_256_colors_table.colors):
|
| 282 |
+
_256_colors[i] = f"#{r:02x}{g:02x}{b:02x}"
|
| 283 |
+
|
| 284 |
+
|
| 285 |
+
def ansi_escape(text: object) -> str:
|
| 286 |
+
"""
|
| 287 |
+
Replace characters with a special meaning.
|
| 288 |
+
"""
|
| 289 |
+
return str(text).replace("\x1b", "?").replace("\b", "?")
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
class ANSIFormatter(Formatter):
|
| 293 |
+
def format_field(self, value: object, format_spec: str) -> str:
|
| 294 |
+
return ansi_escape(format(value, format_spec))
|
| 295 |
+
|
| 296 |
+
|
| 297 |
+
FORMATTER = ANSIFormatter()
|
evalkit_tf437/lib/python3.10/site-packages/prompt_toolkit/shortcuts/__pycache__/utils.cpython-310.pyc
ADDED
|
Binary file (6.68 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/_deprecation_warning.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the BSD license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
|
| 6 |
+
import warnings
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def deprecated_function(self):
|
| 10 |
+
name = repr(self) # self.__name__
|
| 11 |
+
msg = f"{name} is deprecated and is not maintained anymore. It might be removed in a future version of xFormers"
|
| 12 |
+
warnings.warn(msg, FutureWarning, stacklevel=2)
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/__init__.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the BSD license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
from .fmha import (
|
| 9 |
+
AttentionBias,
|
| 10 |
+
AttentionOp,
|
| 11 |
+
AttentionOpBase,
|
| 12 |
+
AttentionOpDispatch,
|
| 13 |
+
LowerTriangularMask,
|
| 14 |
+
MemoryEfficientAttentionCutlassFwdFlashBwOp,
|
| 15 |
+
MemoryEfficientAttentionCutlassOp,
|
| 16 |
+
MemoryEfficientAttentionFlashAttentionOp,
|
| 17 |
+
MemoryEfficientAttentionOp,
|
| 18 |
+
MemoryEfficientAttentionTritonFwdFlashBwOp,
|
| 19 |
+
TritonFlashAttentionOp,
|
| 20 |
+
memory_efficient_attention,
|
| 21 |
+
memory_efficient_attention_backward,
|
| 22 |
+
memory_efficient_attention_forward,
|
| 23 |
+
memory_efficient_attention_forward_requires_grad,
|
| 24 |
+
)
|
| 25 |
+
from .indexing import index_select_cat, scaled_index_add
|
| 26 |
+
from .modpar_layers import ColumnParallelLinear, RowParallelLinear
|
| 27 |
+
from .rmsnorm import RMSNorm
|
| 28 |
+
from .rope_padded import rope_padded
|
| 29 |
+
from .seqpar import sequence_parallel_leading_matmul, sequence_parallel_trailing_matmul
|
| 30 |
+
from .sequence_parallel_fused_ops import (
|
| 31 |
+
fused_allgather_and_anything,
|
| 32 |
+
fused_allgather_and_linear,
|
| 33 |
+
fused_anything_and_reducescatter,
|
| 34 |
+
fused_linear_and_reducescatter,
|
| 35 |
+
)
|
| 36 |
+
from .sp24 import Sparse24Tensor, sparsify24, sparsify24_like
|
| 37 |
+
from .swiglu_op import (
|
| 38 |
+
SwiGLU,
|
| 39 |
+
SwiGLUEagerOp,
|
| 40 |
+
SwiGLUFusedOp,
|
| 41 |
+
SwiGLUOp,
|
| 42 |
+
SwiGLUOpDispatch,
|
| 43 |
+
SwiGLUPackedFusedOp,
|
| 44 |
+
swiglu,
|
| 45 |
+
)
|
| 46 |
+
from .tiled_matmul import tiled_matmul
|
| 47 |
+
from .unbind import get_stack_strides, stack_or_none, unbind
|
| 48 |
+
|
| 49 |
+
# BW compatibility
|
| 50 |
+
AttentionMask = AttentionBias
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def masked_matmul(a, b, mask=None):
|
| 54 |
+
if torch.overrides.has_torch_function((a, b, mask)):
|
| 55 |
+
return torch.overrides.handle_torch_function(
|
| 56 |
+
masked_matmul, (a, b, mask), a, b, mask
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
att = a @ b
|
| 60 |
+
|
| 61 |
+
if mask is None:
|
| 62 |
+
return att
|
| 63 |
+
|
| 64 |
+
if mask.dtype == torch.bool:
|
| 65 |
+
if mask.ndim == 2:
|
| 66 |
+
mask = mask.unsqueeze(0).expand(att.shape[0], -1, -1)
|
| 67 |
+
# mask is presumed false == ignore
|
| 68 |
+
att[~mask] = float("-inf")
|
| 69 |
+
else:
|
| 70 |
+
# mask is presumed additive
|
| 71 |
+
att += mask
|
| 72 |
+
return att
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
__all__ = [
|
| 76 |
+
# fmha
|
| 77 |
+
"AttentionBias",
|
| 78 |
+
"AttentionMask",
|
| 79 |
+
"AttentionOp",
|
| 80 |
+
"AttentionOpBase",
|
| 81 |
+
"AttentionOpDispatch",
|
| 82 |
+
"LowerTriangularMask",
|
| 83 |
+
"MemoryEfficientAttentionCutlassFwdFlashBwOp",
|
| 84 |
+
"MemoryEfficientAttentionCutlassOp",
|
| 85 |
+
"MemoryEfficientAttentionFlashAttentionOp",
|
| 86 |
+
"MemoryEfficientAttentionOp",
|
| 87 |
+
"MemoryEfficientAttentionTritonFwdFlashBwOp",
|
| 88 |
+
"TritonFlashAttentionOp",
|
| 89 |
+
"memory_efficient_attention",
|
| 90 |
+
"memory_efficient_attention_backward",
|
| 91 |
+
"memory_efficient_attention_forward",
|
| 92 |
+
"memory_efficient_attention_forward_requires_grad",
|
| 93 |
+
# indexing
|
| 94 |
+
"index_select_cat",
|
| 95 |
+
"scaled_index_add",
|
| 96 |
+
# modpar_layers
|
| 97 |
+
"ColumnParallelLinear",
|
| 98 |
+
"RowParallelLinear",
|
| 99 |
+
# rmsnorm
|
| 100 |
+
"RMSNorm",
|
| 101 |
+
# rope_padded
|
| 102 |
+
"rope_padded",
|
| 103 |
+
# seqpar
|
| 104 |
+
"sequence_parallel_leading_matmul",
|
| 105 |
+
"sequence_parallel_trailing_matmul",
|
| 106 |
+
# sequence_parallel_fused_ops
|
| 107 |
+
"fused_allgather_and_anything",
|
| 108 |
+
"fused_allgather_and_linear",
|
| 109 |
+
"fused_anything_and_reducescatter",
|
| 110 |
+
"fused_linear_and_reducescatter",
|
| 111 |
+
# swiglu_op
|
| 112 |
+
"SwiGLU",
|
| 113 |
+
"SwiGLUEagerOp",
|
| 114 |
+
"SwiGLUFusedOp",
|
| 115 |
+
"SwiGLUOp",
|
| 116 |
+
"SwiGLUOpDispatch",
|
| 117 |
+
"SwiGLUPackedFusedOp",
|
| 118 |
+
"swiglu",
|
| 119 |
+
# tiled_matmul
|
| 120 |
+
"tiled_matmul",
|
| 121 |
+
# unbind
|
| 122 |
+
"get_stack_strides",
|
| 123 |
+
"stack_or_none",
|
| 124 |
+
"unbind",
|
| 125 |
+
# sp24
|
| 126 |
+
"sparsify24",
|
| 127 |
+
"sparsify24_like",
|
| 128 |
+
"Sparse24Tensor",
|
| 129 |
+
# .
|
| 130 |
+
"masked_matmul",
|
| 131 |
+
]
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/__pycache__/differentiable_collectives.cpython-310.pyc
ADDED
|
Binary file (4.63 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/__pycache__/seqpar.cpython-310.pyc
ADDED
|
Binary file (8.31 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/__pycache__/sequence_parallel_fused_ops.cpython-310.pyc
ADDED
|
Binary file (31.2 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/__pycache__/swiglu_op.cpython-310.pyc
ADDED
|
Binary file (16.3 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__init__.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the BSD license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
# One reason this module is called `_triton` instead of just `triton` is this:
|
| 8 |
+
# https://github.com/openai/triton/commit/c6040bcbd8a046785462481b2830b3fff5fc4aab
|
| 9 |
+
|
| 10 |
+
from typing import TYPE_CHECKING
|
| 11 |
+
|
| 12 |
+
import xformers
|
| 13 |
+
|
| 14 |
+
if TYPE_CHECKING or xformers._is_triton_available():
|
| 15 |
+
from .k_index_select_cat import index_select_cat_bwd, index_select_cat_fwd
|
| 16 |
+
from .k_scaled_index_add import scaled_index_add_bwd, scaled_index_add_fwd
|
| 17 |
+
else:
|
| 18 |
+
index_select_cat_fwd = index_select_cat_bwd = None
|
| 19 |
+
scaled_index_add_fwd = scaled_index_add_bwd = None
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (496 Bytes). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/k_index_select_cat.cpython-310.pyc
ADDED
|
Binary file (3.92 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/k_scaled_index_add.cpython-310.pyc
ADDED
|
Binary file (6.73 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/rmsnorm_kernels.cpython-310.pyc
ADDED
|
Binary file (3.21 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/rope_padded_kernels.cpython-310.pyc
ADDED
|
Binary file (3.32 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/sequence_parallel_fused_kernels.cpython-310.pyc
ADDED
|
Binary file (14.1 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/__pycache__/tiled_matmul_kernels.cpython-310.pyc
ADDED
|
Binary file (10.8 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/k_index_select_cat.py
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the BSD license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
import triton
|
| 8 |
+
import triton.language as tl
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
@triton.jit
|
| 12 |
+
def index_select_cat_fwd_kernel(
|
| 13 |
+
output_ptr, # *Pointer* to output tensor.
|
| 14 |
+
source_ptr, # *Pointer* to source tensor.
|
| 15 |
+
index_ptr, # *Pointer* to index tensor.
|
| 16 |
+
num_indices,
|
| 17 |
+
num_cols,
|
| 18 |
+
stride0, # Stride information of source tensor.
|
| 19 |
+
stride1,
|
| 20 |
+
BLOCK_SIZE_INDEX: tl.constexpr, # Number of indices each program should process.
|
| 21 |
+
BLOCK_SIZE_COL: tl.constexpr, # Number of cols each program should process.
|
| 22 |
+
):
|
| 23 |
+
pid0 = tl.program_id(axis=0) # We use 2D launch grid
|
| 24 |
+
pid1 = tl.program_id(axis=1)
|
| 25 |
+
|
| 26 |
+
indices = pid0 * BLOCK_SIZE_INDEX + tl.arange(0, BLOCK_SIZE_INDEX)
|
| 27 |
+
rows = tl.load(index_ptr + indices, mask=(indices < num_indices))
|
| 28 |
+
cols = pid1 * BLOCK_SIZE_COL + tl.arange(0, BLOCK_SIZE_COL)
|
| 29 |
+
|
| 30 |
+
source_offsets = source_ptr + rows[:, None] * stride0 + cols[None, :] * stride1
|
| 31 |
+
mask = (indices[:, None] < num_indices) & (cols[None, :] < num_cols)
|
| 32 |
+
output = tl.load(source_offsets, mask=mask)
|
| 33 |
+
|
| 34 |
+
output_offsets = output_ptr + indices[:, None] * stride0 + cols[None, :] * stride1
|
| 35 |
+
tl.store(output_offsets, output, mask=mask)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def index_select_cat_fwd(
|
| 39 |
+
output: torch.Tensor,
|
| 40 |
+
source: torch.Tensor,
|
| 41 |
+
index: torch.Tensor,
|
| 42 |
+
):
|
| 43 |
+
if not (source.is_cuda and index.is_cuda):
|
| 44 |
+
raise ValueError("The index tensor and the source tensor must be of type CUDA!")
|
| 45 |
+
|
| 46 |
+
if not source.ndim == 2:
|
| 47 |
+
raise ValueError(f"Expected 2-dimensional tensor, got {source.ndim}.")
|
| 48 |
+
if not index.ndim == 1:
|
| 49 |
+
raise ValueError(f"Expected 1-dimensional tensor, got {index.ndim}.")
|
| 50 |
+
|
| 51 |
+
num_rows, num_cols = source.shape
|
| 52 |
+
num_indices = index.shape[0]
|
| 53 |
+
|
| 54 |
+
if not num_indices < num_rows:
|
| 55 |
+
raise ValueError(
|
| 56 |
+
"The number of indices cannot exceed the number of rows in the source matrix."
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
stride0, stride1 = source.stride(0), source.stride(1)
|
| 60 |
+
|
| 61 |
+
def grid(meta):
|
| 62 |
+
return (
|
| 63 |
+
triton.cdiv(num_indices, meta["BLOCK_SIZE_INDEX"]),
|
| 64 |
+
triton.cdiv(num_cols, meta["BLOCK_SIZE_COL"]),
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
index_select_cat_fwd_kernel[grid](
|
| 68 |
+
output,
|
| 69 |
+
source,
|
| 70 |
+
index,
|
| 71 |
+
num_indices,
|
| 72 |
+
num_cols,
|
| 73 |
+
stride0,
|
| 74 |
+
stride1,
|
| 75 |
+
BLOCK_SIZE_INDEX=1,
|
| 76 |
+
BLOCK_SIZE_COL=512,
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
return output
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
@triton.jit
|
| 83 |
+
def index_select_cat_bwd_kernel(
|
| 84 |
+
grad_source_ptr, # *Pointer* to grad_source tensor.
|
| 85 |
+
index_ptr, # *Pointer* to index tensor.
|
| 86 |
+
grad_output_ptr, # *Pointer* to grad_output tensor.
|
| 87 |
+
num_rows,
|
| 88 |
+
num_indices,
|
| 89 |
+
num_cols,
|
| 90 |
+
stride0, # Stride information of input and source tensor.
|
| 91 |
+
stride1,
|
| 92 |
+
BLOCK_SIZE_INDEX: tl.constexpr, # Number of indices each program should process.
|
| 93 |
+
BLOCK_SIZE_COL: tl.constexpr, # Number of cols each program should process.
|
| 94 |
+
):
|
| 95 |
+
pid0 = tl.program_id(axis=0) # We use 3D launch grid
|
| 96 |
+
pid1 = tl.program_id(axis=1)
|
| 97 |
+
|
| 98 |
+
cols = pid1 * BLOCK_SIZE_COL + tl.arange(0, BLOCK_SIZE_COL)
|
| 99 |
+
|
| 100 |
+
# load grad_output
|
| 101 |
+
grad_output_indices = pid0 * BLOCK_SIZE_INDEX + tl.arange(0, BLOCK_SIZE_INDEX)
|
| 102 |
+
grad_output_offsets = (
|
| 103 |
+
grad_output_ptr
|
| 104 |
+
+ grad_output_indices[:, None] * stride0
|
| 105 |
+
+ cols[None, :] * stride1
|
| 106 |
+
)
|
| 107 |
+
grad_output_mask = (grad_output_indices[:, None] < num_indices) & (
|
| 108 |
+
cols[None, :] < num_cols
|
| 109 |
+
)
|
| 110 |
+
grad_output = tl.load(grad_output_offsets, mask=grad_output_mask).to(tl.float32)
|
| 111 |
+
|
| 112 |
+
# select indices from grad_source
|
| 113 |
+
grad_source_indices = tl.load(
|
| 114 |
+
index_ptr + grad_output_indices, mask=(grad_output_indices < num_indices)
|
| 115 |
+
)
|
| 116 |
+
grad_source_offsets = (
|
| 117 |
+
grad_source_ptr
|
| 118 |
+
+ grad_source_indices[:, None] * stride0
|
| 119 |
+
+ cols[None, :] * stride1
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
# compute scaled index add and save
|
| 123 |
+
tl.store(grad_source_offsets, grad_output, mask=grad_output_mask)
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def index_select_cat_bwd(
|
| 127 |
+
grad_source: torch.Tensor,
|
| 128 |
+
index: torch.Tensor,
|
| 129 |
+
grad_output: torch.Tensor,
|
| 130 |
+
):
|
| 131 |
+
if not (grad_source.is_cuda and grad_output.is_cuda):
|
| 132 |
+
raise ValueError("The grad_source and grad_output tensor must be of type CUDA!")
|
| 133 |
+
|
| 134 |
+
if not (grad_source.ndim == 2 and grad_output.ndim == 2):
|
| 135 |
+
raise ValueError(
|
| 136 |
+
f"The grad_source and grad_output must be three-dimensional "
|
| 137 |
+
f"(got {grad_source.ndim} and {grad_output.ndim})!"
|
| 138 |
+
)
|
| 139 |
+
if not grad_source.shape[1] == grad_output.shape[1]:
|
| 140 |
+
raise ValueError(
|
| 141 |
+
f"The number of elements along dimension 1 of grad_source and grad_output must be the same "
|
| 142 |
+
f"(got {grad_source.shape[1]} and {grad_output.shape[1]})"
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
num_rows, num_cols = grad_source.shape
|
| 146 |
+
num_indices, num_cols = grad_output.shape
|
| 147 |
+
if not num_rows >= num_indices:
|
| 148 |
+
raise ValueError(
|
| 149 |
+
f"The number of elements along dimension 0 of grad_source must be larger than that of grad_output "
|
| 150 |
+
f"(got {num_rows} and {num_indices})!"
|
| 151 |
+
)
|
| 152 |
+
if not index.shape[0] == num_indices:
|
| 153 |
+
raise ValueError(
|
| 154 |
+
f"The number of indices and the number of elements along dimension 0 of grad_output must match "
|
| 155 |
+
f"(got {index.shape[0]} and {num_indices})!"
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
stride0, stride1 = grad_source.stride(0), grad_source.stride(1)
|
| 159 |
+
if not (grad_output.stride(0) == stride0 and grad_output.stride(1) == stride1):
|
| 160 |
+
raise ValueError(
|
| 161 |
+
f"The strides of the grad_source and grad_output tensors must match "
|
| 162 |
+
f"(got {stride0} vs. {grad_output.stride(0)}, {stride1} vs. {grad_output.stride(1)})!"
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
def grid(meta):
|
| 166 |
+
return (
|
| 167 |
+
triton.cdiv(num_indices, meta["BLOCK_SIZE_INDEX"]),
|
| 168 |
+
triton.cdiv(num_cols, meta["BLOCK_SIZE_COL"]),
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
index_select_cat_bwd_kernel[grid](
|
| 172 |
+
grad_source,
|
| 173 |
+
index,
|
| 174 |
+
grad_output,
|
| 175 |
+
num_rows,
|
| 176 |
+
num_indices,
|
| 177 |
+
num_cols,
|
| 178 |
+
grad_source.stride(0),
|
| 179 |
+
grad_source.stride(1),
|
| 180 |
+
BLOCK_SIZE_INDEX=1,
|
| 181 |
+
BLOCK_SIZE_COL=512,
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
return
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/rmsnorm_kernels.py
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the BSD license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
import torch
|
| 6 |
+
import triton
|
| 7 |
+
import triton.language as tl
|
| 8 |
+
|
| 9 |
+
if hasattr(tl, "libdevice"):
|
| 10 |
+
tl_math = tl.libdevice
|
| 11 |
+
else:
|
| 12 |
+
tl_math = tl.math
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
@triton.jit
|
| 16 |
+
def _rms_norm_kernel(
|
| 17 |
+
x_ptr,
|
| 18 |
+
h1_ptr,
|
| 19 |
+
w_ptr,
|
| 20 |
+
eps,
|
| 21 |
+
stride,
|
| 22 |
+
N_COLS: tl.constexpr,
|
| 23 |
+
BLOCK_SIZE: tl.constexpr,
|
| 24 |
+
INCLUDE_WEIGHT: tl.constexpr,
|
| 25 |
+
):
|
| 26 |
+
row = tl.program_id(0)
|
| 27 |
+
x_ptr += row * stride
|
| 28 |
+
h1_ptr += row * stride
|
| 29 |
+
|
| 30 |
+
_mean = tl.zeros([BLOCK_SIZE], dtype=tl.float32)
|
| 31 |
+
for offset in range(0, N_COLS, BLOCK_SIZE):
|
| 32 |
+
cols = offset + tl.arange(0, BLOCK_SIZE)
|
| 33 |
+
a = tl.load(
|
| 34 |
+
x_ptr + cols, mask=cols < N_COLS, other=0.0, eviction_policy="evict_last"
|
| 35 |
+
).to(tl.float32)
|
| 36 |
+
_mean += a * a
|
| 37 |
+
rstd = tl_math.rsqrt((tl.sum(_mean, axis=0) / N_COLS) + eps)
|
| 38 |
+
for offset in range(0, N_COLS, BLOCK_SIZE):
|
| 39 |
+
cols = offset + tl.arange(0, BLOCK_SIZE)
|
| 40 |
+
mask = cols < N_COLS
|
| 41 |
+
a = tl.load(
|
| 42 |
+
x_ptr + cols, mask=mask, other=0.0, eviction_policy="evict_first"
|
| 43 |
+
).to(tl.float32)
|
| 44 |
+
if INCLUDE_WEIGHT:
|
| 45 |
+
w = tl.load(w_ptr + cols, mask=mask)
|
| 46 |
+
tl.store(h1_ptr + cols, a * rstd * w, mask=mask)
|
| 47 |
+
else:
|
| 48 |
+
tl.store(h1_ptr + cols, a * rstd, mask=mask)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
@triton.jit
|
| 52 |
+
def _rms_norm_add_kernel(
|
| 53 |
+
x_ptr,
|
| 54 |
+
y_ptr,
|
| 55 |
+
h1_ptr,
|
| 56 |
+
w_ptr,
|
| 57 |
+
eps,
|
| 58 |
+
stride,
|
| 59 |
+
N_COLS: tl.constexpr,
|
| 60 |
+
BLOCK_SIZE: tl.constexpr,
|
| 61 |
+
INCLUDE_WEIGHT: tl.constexpr,
|
| 62 |
+
):
|
| 63 |
+
row = tl.program_id(0)
|
| 64 |
+
x_ptr += row * stride
|
| 65 |
+
y_ptr += row * stride
|
| 66 |
+
h1_ptr += row * stride
|
| 67 |
+
|
| 68 |
+
_mean = tl.zeros([BLOCK_SIZE], dtype=tl.float32)
|
| 69 |
+
for offset in range(0, N_COLS, BLOCK_SIZE):
|
| 70 |
+
cols = offset + tl.arange(0, BLOCK_SIZE)
|
| 71 |
+
mask = cols < N_COLS
|
| 72 |
+
ax = tl.load(
|
| 73 |
+
x_ptr + cols, mask=mask, other=0.0, eviction_policy="evict_last"
|
| 74 |
+
).to(tl.float32)
|
| 75 |
+
ay = tl.load(
|
| 76 |
+
y_ptr + cols, mask=mask, other=0.0, eviction_policy="evict_first"
|
| 77 |
+
).to(tl.float32)
|
| 78 |
+
a = ax + ay
|
| 79 |
+
tl.store(x_ptr + cols, a, mask=mask)
|
| 80 |
+
_mean += a * a
|
| 81 |
+
rstd = tl_math.rsqrt((tl.sum(_mean, axis=0) / N_COLS) + eps)
|
| 82 |
+
for offset in range(0, N_COLS, BLOCK_SIZE):
|
| 83 |
+
cols = offset + tl.arange(0, BLOCK_SIZE)
|
| 84 |
+
mask = cols < N_COLS
|
| 85 |
+
a = tl.load(
|
| 86 |
+
x_ptr + cols, mask=mask, other=0.0, eviction_policy="evict_first"
|
| 87 |
+
).to(tl.float32)
|
| 88 |
+
if INCLUDE_WEIGHT:
|
| 89 |
+
w = tl.load(w_ptr + cols, mask=mask)
|
| 90 |
+
tl.store(h1_ptr + cols, a * rstd * w, mask=mask)
|
| 91 |
+
else:
|
| 92 |
+
tl.store(h1_ptr + cols, a * rstd, mask=mask)
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
def _rms_norm_forward(x, attn_norm_weights, eps):
|
| 96 |
+
if not x.is_contiguous():
|
| 97 |
+
raise ValueError("data must be contiguous")
|
| 98 |
+
if attn_norm_weights is not None:
|
| 99 |
+
if not attn_norm_weights.is_contiguous():
|
| 100 |
+
raise ValueError("weights must be contiguous")
|
| 101 |
+
out = torch.empty_like(x)
|
| 102 |
+
x_arg = x.reshape(-1, x.shape[-1])
|
| 103 |
+
M, N = x_arg.shape
|
| 104 |
+
# Less than 64KB per feature: enqueue fused kernel
|
| 105 |
+
MAX_FUSED_SIZE = 65536 // x.element_size()
|
| 106 |
+
BLOCK_SIZE = min(MAX_FUSED_SIZE, triton.next_power_of_2(N))
|
| 107 |
+
BLOCK_SIZE = max(BLOCK_SIZE, 128)
|
| 108 |
+
BLOCK_SIZE = min(BLOCK_SIZE, 8192)
|
| 109 |
+
# heuristics for number of warps
|
| 110 |
+
num_warps = min(max(BLOCK_SIZE // 256, 1), 8)
|
| 111 |
+
_rms_norm_kernel[(M,)](
|
| 112 |
+
x_arg,
|
| 113 |
+
out,
|
| 114 |
+
attn_norm_weights,
|
| 115 |
+
eps,
|
| 116 |
+
x_arg.stride(0),
|
| 117 |
+
N,
|
| 118 |
+
BLOCK_SIZE=BLOCK_SIZE,
|
| 119 |
+
num_warps=num_warps,
|
| 120 |
+
INCLUDE_WEIGHT=attn_norm_weights is not None,
|
| 121 |
+
)
|
| 122 |
+
return out
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def _rms_norm_add_forward(x, y, attn_norm_weights, eps):
|
| 126 |
+
# x, y contiguous of same shape [..., n]
|
| 127 |
+
# output of same shape, normed over the last dim.
|
| 128 |
+
if not x.is_contiguous():
|
| 129 |
+
raise ValueError("x must be contiguous")
|
| 130 |
+
if not y.is_contiguous():
|
| 131 |
+
raise ValueError("y must be contiguous")
|
| 132 |
+
if attn_norm_weights is not None:
|
| 133 |
+
if not attn_norm_weights.is_contiguous():
|
| 134 |
+
raise ValueError("weights must be contiguous")
|
| 135 |
+
out = torch.empty_like(x)
|
| 136 |
+
x_arg = x.reshape(-1, x.shape[-1])
|
| 137 |
+
y_arg = y.reshape(-1, x.shape[-1])
|
| 138 |
+
M, N = x_arg.shape
|
| 139 |
+
# Less than 64KB per feature: enqueue fused kernel
|
| 140 |
+
MAX_FUSED_SIZE = 65536 // x.element_size()
|
| 141 |
+
BLOCK_SIZE = min(MAX_FUSED_SIZE, triton.next_power_of_2(N))
|
| 142 |
+
BLOCK_SIZE = max(BLOCK_SIZE, 128)
|
| 143 |
+
BLOCK_SIZE = min(BLOCK_SIZE, 8192)
|
| 144 |
+
# heuristics for number of warps
|
| 145 |
+
num_warps = min(max(BLOCK_SIZE // 256, 1), 8)
|
| 146 |
+
_rms_norm_add_kernel[(M,)](
|
| 147 |
+
x_arg,
|
| 148 |
+
y_arg,
|
| 149 |
+
out,
|
| 150 |
+
attn_norm_weights,
|
| 151 |
+
eps,
|
| 152 |
+
x_arg.stride(0),
|
| 153 |
+
N,
|
| 154 |
+
BLOCK_SIZE=BLOCK_SIZE,
|
| 155 |
+
num_warps=num_warps,
|
| 156 |
+
INCLUDE_WEIGHT=attn_norm_weights is not None,
|
| 157 |
+
)
|
| 158 |
+
return out
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/rope_padded_kernels.py
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the BSD license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
import triton # type: ignore
|
| 6 |
+
import triton.language as tl # type: ignore
|
| 7 |
+
|
| 8 |
+
if hasattr(tl, "libdevice"):
|
| 9 |
+
tl_math = tl.libdevice
|
| 10 |
+
else:
|
| 11 |
+
tl_math = tl.math
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@triton.jit
|
| 15 |
+
def _rope_padded_kernel(
|
| 16 |
+
xq,
|
| 17 |
+
xk,
|
| 18 |
+
xv,
|
| 19 |
+
out_q,
|
| 20 |
+
cache_k,
|
| 21 |
+
cache_v,
|
| 22 |
+
seqstartq,
|
| 23 |
+
seqstartk,
|
| 24 |
+
seqlenk,
|
| 25 |
+
theta,
|
| 26 |
+
k_start: tl.constexpr,
|
| 27 |
+
v_start: tl.constexpr,
|
| 28 |
+
n_groups,
|
| 29 |
+
dim: tl.constexpr, # dimension of each head
|
| 30 |
+
stride_xqM,
|
| 31 |
+
stride_xqG,
|
| 32 |
+
stride_xqH,
|
| 33 |
+
stride_xkM,
|
| 34 |
+
stride_xkG,
|
| 35 |
+
stride_xkH,
|
| 36 |
+
stride_xvM,
|
| 37 |
+
stride_xvG,
|
| 38 |
+
stride_xvH,
|
| 39 |
+
stride_cachekM,
|
| 40 |
+
stride_cachekG,
|
| 41 |
+
stride_cachekH,
|
| 42 |
+
stride_cachevM,
|
| 43 |
+
stride_cachevG,
|
| 44 |
+
stride_cachevH,
|
| 45 |
+
stride_seqstartq,
|
| 46 |
+
stride_seqstartk,
|
| 47 |
+
stride_seqlenk,
|
| 48 |
+
stride_outqM,
|
| 49 |
+
stride_outqG,
|
| 50 |
+
stride_outqH,
|
| 51 |
+
internal_dtype: tl.constexpr,
|
| 52 |
+
# If True, seqstartq and seqstartk are not used but rather we
|
| 53 |
+
# assume that every batch element has the same number of
|
| 54 |
+
# queries (i.e. num_queries := tl.num_programs(1) )
|
| 55 |
+
# and the same cache space cache_padding_length.
|
| 56 |
+
# Always False when called below.
|
| 57 |
+
const_batch_strides: tl.constexpr,
|
| 58 |
+
# If const_batch_strides==True, the common cache length for each batch element.
|
| 59 |
+
# (Only the first seqlenk[i] elements are actually in use, and only the last
|
| 60 |
+
# num_queries of those are actually written to.)
|
| 61 |
+
cache_padding_length,
|
| 62 |
+
# offset added to all values in seqlenk before using them.
|
| 63 |
+
# Always 0 when called below.
|
| 64 |
+
seqlenk_shift: tl.constexpr,
|
| 65 |
+
BLOCK_SIZE: tl.constexpr,
|
| 66 |
+
adjacents: tl.constexpr,
|
| 67 |
+
):
|
| 68 |
+
"""
|
| 69 |
+
Each letter in this diagram is a whole row of length dim.
|
| 70 |
+
|
| 71 |
+
INPUT xq xk xv
|
| 72 |
+
|
| 73 |
+
head_dim ─►
|
| 74 |
+
|
| 75 |
+
batch qqqqqq kk vv
|
| 76 |
+
│ qqqqqq kk vv
|
| 77 |
+
▼ qqqqqq kk vv
|
| 78 |
+
|
| 79 |
+
head_idx: (goes across all heads of all 3 inputs)
|
| 80 |
+
▲ ▲ ▲ ▲ ▲ ▲
|
| 81 |
+
│ │ │ │ │ │
|
| 82 |
+
│ │
|
| 83 |
+
0 k_start │v_start │n_total_heads
|
| 84 |
+
│ │
|
| 85 |
+
│ │
|
| 86 |
+
k_start v_start
|
| 87 |
+
|
| 88 |
+
Output is to out_q (same shape as xq), an xk-shaped part
|
| 89 |
+
of cache_k and an xv-shaped part of cache_v
|
| 90 |
+
"""
|
| 91 |
+
batch_elt = tl.program_id(0)
|
| 92 |
+
query_pos_in_batch_elt = tl.program_id(1)
|
| 93 |
+
group_head_idx = tl.program_id(2)
|
| 94 |
+
group_idx = group_head_idx % n_groups
|
| 95 |
+
head_idx = group_head_idx // n_groups
|
| 96 |
+
|
| 97 |
+
if internal_dtype == "f32":
|
| 98 |
+
theta = theta.to(tl.float32)
|
| 99 |
+
elif internal_dtype == "f64":
|
| 100 |
+
theta = theta.to(tl.float64)
|
| 101 |
+
|
| 102 |
+
if const_batch_strides:
|
| 103 |
+
query_pos = query_pos_in_batch_elt + tl.num_programs(1) * batch_elt
|
| 104 |
+
end_query_pos = tl.num_programs(1) * (batch_elt + 1)
|
| 105 |
+
else:
|
| 106 |
+
query_pos = query_pos_in_batch_elt + tl.load(
|
| 107 |
+
seqstartq + batch_elt * stride_seqstartq
|
| 108 |
+
)
|
| 109 |
+
end_query_pos = tl.load(seqstartq + (batch_elt + 1) * stride_seqstartq)
|
| 110 |
+
if query_pos >= end_query_pos:
|
| 111 |
+
return
|
| 112 |
+
|
| 113 |
+
is_q = head_idx < k_start
|
| 114 |
+
is_v = head_idx >= v_start
|
| 115 |
+
|
| 116 |
+
xq += query_pos * stride_xqM + head_idx * stride_xqH + group_idx * stride_xqG
|
| 117 |
+
out_q += (
|
| 118 |
+
query_pos * stride_outqM + head_idx * stride_outqH + group_idx * stride_outqG
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
if const_batch_strides:
|
| 122 |
+
cache_start = cache_padding_length * batch_elt
|
| 123 |
+
else:
|
| 124 |
+
cache_start = tl.load(seqstartk + batch_elt * stride_seqstartk)
|
| 125 |
+
end_of_batch_elt_cache = (
|
| 126 |
+
cache_start + tl.load(seqlenk + batch_elt * stride_seqlenk) + seqlenk_shift
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
cache_pos = end_of_batch_elt_cache - (end_query_pos - query_pos)
|
| 130 |
+
seq_pos = cache_pos - cache_start
|
| 131 |
+
cache_k += (
|
| 132 |
+
(head_idx - k_start) * stride_cachekH
|
| 133 |
+
+ cache_pos * stride_cachekM
|
| 134 |
+
+ group_idx * stride_cachekG
|
| 135 |
+
)
|
| 136 |
+
xk += (
|
| 137 |
+
query_pos * stride_xkM
|
| 138 |
+
+ (head_idx - k_start) * stride_xkH
|
| 139 |
+
+ group_idx * stride_xkG
|
| 140 |
+
)
|
| 141 |
+
in_qk = tl.where(is_q, xq, xk)
|
| 142 |
+
out_qk = tl.where(is_q, out_q, cache_k)
|
| 143 |
+
|
| 144 |
+
cache_v += (
|
| 145 |
+
(head_idx - v_start) * stride_cachevH
|
| 146 |
+
+ cache_pos * stride_cachevM
|
| 147 |
+
+ group_idx * stride_cachevG
|
| 148 |
+
)
|
| 149 |
+
xv += (
|
| 150 |
+
query_pos * stride_xvM
|
| 151 |
+
+ (head_idx - v_start) * stride_xvH
|
| 152 |
+
+ group_idx * stride_xvG
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
out = tl.where(is_v, cache_v, out_qk)
|
| 156 |
+
x_in = tl.where(is_v, xv, in_qk)
|
| 157 |
+
|
| 158 |
+
for offset in range(0, dim // 2, BLOCK_SIZE // 2):
|
| 159 |
+
c = tl.arange(0, BLOCK_SIZE // 2)
|
| 160 |
+
powers = (offset + c) * 2.0
|
| 161 |
+
if adjacents:
|
| 162 |
+
cols_re = (offset + c) * 2
|
| 163 |
+
cols_im = cols_re + 1
|
| 164 |
+
else:
|
| 165 |
+
cols_re = offset + c
|
| 166 |
+
cols_im = cols_re + dim // 2
|
| 167 |
+
|
| 168 |
+
mask = cols_im < dim
|
| 169 |
+
|
| 170 |
+
re_x = tl.load(x_in + cols_re, mask=mask)
|
| 171 |
+
im_x = tl.load(x_in + cols_im, mask=mask)
|
| 172 |
+
# freqs = seq_pos / (theta ** (powers / dim))
|
| 173 |
+
freqs = seq_pos * tl_math.pow(theta, powers / (-dim))
|
| 174 |
+
sines = tl.sin(freqs)
|
| 175 |
+
cosines = tl.cos(freqs)
|
| 176 |
+
re_out = re_x * cosines - im_x * sines
|
| 177 |
+
im_out = im_x * cosines + re_x * sines
|
| 178 |
+
|
| 179 |
+
re_out_ = tl.where(is_v, re_x, re_out)
|
| 180 |
+
im_out_ = tl.where(is_v, im_x, im_out)
|
| 181 |
+
if internal_dtype == "f64":
|
| 182 |
+
if re_x.dtype == tl.bfloat16:
|
| 183 |
+
# triton 2.0.0 crashes if you try to convert
|
| 184 |
+
# float64 directly to bfloat16, so make an intermediate step.
|
| 185 |
+
re_out_ = re_out_.to(tl.float32)
|
| 186 |
+
im_out_ = im_out_.to(tl.float32)
|
| 187 |
+
tl.store(out + cols_re, re_out_, mask=mask)
|
| 188 |
+
tl.store(out + cols_im, im_out_, mask=mask)
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/_triton/tiled_matmul_kernels.py
ADDED
|
@@ -0,0 +1,430 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the BSD license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
import itertools
|
| 8 |
+
from typing import List, Tuple
|
| 9 |
+
|
| 10 |
+
import torch
|
| 11 |
+
import triton
|
| 12 |
+
import triton.language as tl
|
| 13 |
+
from triton.ops.matmul_perf_model import early_config_prune, estimate_matmul_time
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def init_to_zero(*names):
|
| 17 |
+
def result(nargs):
|
| 18 |
+
for name in names:
|
| 19 |
+
nargs[name].zero_()
|
| 20 |
+
|
| 21 |
+
return result
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def gen_config(
|
| 25 |
+
block_m: int,
|
| 26 |
+
block_n: int,
|
| 27 |
+
block_k: int,
|
| 28 |
+
stages: int,
|
| 29 |
+
warps: int,
|
| 30 |
+
split_k: int = 1,
|
| 31 |
+
group_m: int = 8,
|
| 32 |
+
) -> triton.Config:
|
| 33 |
+
"""A more compact way to define a triton.Config, so it fits on one line"""
|
| 34 |
+
|
| 35 |
+
return triton.Config(
|
| 36 |
+
{
|
| 37 |
+
"BLOCK_M": block_m,
|
| 38 |
+
"BLOCK_N": block_n,
|
| 39 |
+
"BLOCK_K": block_k,
|
| 40 |
+
"SPLIT_K": split_k,
|
| 41 |
+
"GROUP_M": group_m,
|
| 42 |
+
},
|
| 43 |
+
num_stages=stages,
|
| 44 |
+
num_warps=warps,
|
| 45 |
+
pre_hook=init_to_zero(*[f"C{i+1}{j+1}" for i in range(3) for j in range(3)])
|
| 46 |
+
if split_k > 1
|
| 47 |
+
else init_to_zero(),
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
BASIC_MATMUL_CONFIGS = [
|
| 52 |
+
gen_config(block_m=128, block_n=256, block_k=32, stages=3, warps=8),
|
| 53 |
+
gen_config(block_m=256, block_n=128, block_k=32, stages=3, warps=8),
|
| 54 |
+
gen_config(block_m=256, block_n=64, block_k=32, stages=4, warps=4),
|
| 55 |
+
gen_config(block_m=64, block_n=256, block_k=32, stages=4, warps=4),
|
| 56 |
+
gen_config(block_m=128, block_n=128, block_k=32, stages=4, warps=4),
|
| 57 |
+
gen_config(block_m=128, block_n=64, block_k=32, stages=4, warps=4),
|
| 58 |
+
gen_config(block_m=64, block_n=128, block_k=32, stages=4, warps=4),
|
| 59 |
+
gen_config(block_m=128, block_n=32, block_k=32, stages=4, warps=4),
|
| 60 |
+
gen_config(block_m=64, block_n=32, block_k=32, stages=5, warps=2),
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
INT8_MATMUL_CONFIGS = [
|
| 65 |
+
gen_config(block_m=128, block_n=256, block_k=128, stages=3, warps=8),
|
| 66 |
+
gen_config(block_m=256, block_n=128, block_k=128, stages=3, warps=8),
|
| 67 |
+
gen_config(block_m=256, block_n=64, block_k=128, stages=4, warps=4),
|
| 68 |
+
gen_config(block_m=64, block_n=256, block_k=128, stages=4, warps=4),
|
| 69 |
+
gen_config(block_m=128, block_n=128, block_k=128, stages=4, warps=4),
|
| 70 |
+
gen_config(block_m=128, block_n=64, block_k=64, stages=4, warps=4),
|
| 71 |
+
gen_config(block_m=64, block_n=128, block_k=64, stages=4, warps=4),
|
| 72 |
+
gen_config(block_m=128, block_n=32, block_k=64, stages=4, warps=4),
|
| 73 |
+
gen_config(block_m=64, block_n=32, block_k=64, stages=5, warps=2),
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
IO_BOUND_MATMUL_CONFIGS_STAGES = [2, 3, 4, 5, 6]
|
| 78 |
+
IO_BOUND_MATMUL_CONFIGS_BLOCK_M = [16, 32]
|
| 79 |
+
IO_BOUND_MATMUL_CONFIGS_BLOCK_K = [32, 64]
|
| 80 |
+
IO_BOUND_MATMUL_CONFIGS_BLOCK_N = [32, 64, 128, 256]
|
| 81 |
+
IO_BOUND_MATMUL_CONFIGS_SPLIT_K = [1, 2, 4, 8, 16]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
IO_BOUND_MATMUL_CONFIGS = [
|
| 85 |
+
gen_config(
|
| 86 |
+
block_m=block_m,
|
| 87 |
+
block_n=block_n,
|
| 88 |
+
block_k=block_k,
|
| 89 |
+
stages=stages,
|
| 90 |
+
warps=2 if block_n <= 64 else 4,
|
| 91 |
+
split_k=split_k,
|
| 92 |
+
)
|
| 93 |
+
for stages, block_m, block_k, block_n, split_k in itertools.product(
|
| 94 |
+
IO_BOUND_MATMUL_CONFIGS_STAGES,
|
| 95 |
+
IO_BOUND_MATMUL_CONFIGS_BLOCK_M,
|
| 96 |
+
IO_BOUND_MATMUL_CONFIGS_BLOCK_K,
|
| 97 |
+
IO_BOUND_MATMUL_CONFIGS_BLOCK_N,
|
| 98 |
+
IO_BOUND_MATMUL_CONFIGS_SPLIT_K,
|
| 99 |
+
)
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
TRITON_CONFIGS = BASIC_MATMUL_CONFIGS + INT8_MATMUL_CONFIGS + IO_BOUND_MATMUL_CONFIGS
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def our_estimate_matmul_time(
|
| 107 |
+
A11, B11, C11, M1, M2, M3, N1, N2, N3, K1, K2, K3, **kwargs
|
| 108 |
+
):
|
| 109 |
+
"""Call into Triton's upstream cost model, with the right args
|
| 110 |
+
|
| 111 |
+
The upstream function expects arguments to have certain names. Since we
|
| 112 |
+
renamed a few of them in our implementation, we rename them back.
|
| 113 |
+
|
| 114 |
+
At the time of writing (July 2023) the arguments that Triton expects are:
|
| 115 |
+
M, N, K, A, B, C, BLOCK_M, BLOCK_N, BLOCK_K, SPLIT_K, num_warps, num_stages.
|
| 116 |
+
|
| 117 |
+
"""
|
| 118 |
+
return estimate_matmul_time(
|
| 119 |
+
M=M1 + M2 + M3, N=N1 + N2 + N3, K=K1 + K2 + K3, A=A11, B=B11, C=C11, **kwargs
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def our_early_config_prune(config, named_args):
|
| 124 |
+
new_named_args = named_args.copy()
|
| 125 |
+
new_named_args["M"] = named_args["M1"] + named_args["M2"] + named_args["M3"]
|
| 126 |
+
new_named_args["N"] = named_args["N1"] + named_args["N2"] + named_args["N3"]
|
| 127 |
+
new_named_args["K"] = named_args["K1"] + named_args["K2"] + named_args["K3"]
|
| 128 |
+
new_named_args["A"] = named_args["A11"]
|
| 129 |
+
new_named_args["B"] = named_args["B11"]
|
| 130 |
+
new_named_args["C"] = named_args["C11"]
|
| 131 |
+
return early_config_prune(config, new_named_args)
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
@triton.autotune(
|
| 135 |
+
configs=TRITON_CONFIGS,
|
| 136 |
+
key=["M1", "M2", "M3", "N1", "N2", "N3", "K1", "K2", "K3"],
|
| 137 |
+
prune_configs_by={
|
| 138 |
+
"early_config_prune": our_early_config_prune,
|
| 139 |
+
"perf_model": our_estimate_matmul_time,
|
| 140 |
+
"top_k": 10,
|
| 141 |
+
},
|
| 142 |
+
)
|
| 143 |
+
@triton.heuristics(
|
| 144 |
+
{
|
| 145 |
+
"EVEN_K": lambda args: all(
|
| 146 |
+
k % (args["BLOCK_K"] * args["SPLIT_K"]) == 0
|
| 147 |
+
for k in [args["K1"], args["K2"], args["K3"]]
|
| 148 |
+
),
|
| 149 |
+
}
|
| 150 |
+
)
|
| 151 |
+
@triton.jit()
|
| 152 |
+
def _xformers_tiled_matmul_kernel(
|
| 153 |
+
A11,
|
| 154 |
+
A12,
|
| 155 |
+
A13,
|
| 156 |
+
A21,
|
| 157 |
+
A22,
|
| 158 |
+
A23,
|
| 159 |
+
A31,
|
| 160 |
+
A32,
|
| 161 |
+
A33,
|
| 162 |
+
B11,
|
| 163 |
+
B12,
|
| 164 |
+
B13,
|
| 165 |
+
B21,
|
| 166 |
+
B22,
|
| 167 |
+
B23,
|
| 168 |
+
B31,
|
| 169 |
+
B32,
|
| 170 |
+
B33,
|
| 171 |
+
C11,
|
| 172 |
+
C12,
|
| 173 |
+
C13,
|
| 174 |
+
C21,
|
| 175 |
+
C22,
|
| 176 |
+
C23,
|
| 177 |
+
C31,
|
| 178 |
+
C32,
|
| 179 |
+
C33,
|
| 180 |
+
M1,
|
| 181 |
+
M2,
|
| 182 |
+
M3,
|
| 183 |
+
N1,
|
| 184 |
+
N2,
|
| 185 |
+
N3,
|
| 186 |
+
K1,
|
| 187 |
+
K2,
|
| 188 |
+
K3,
|
| 189 |
+
stride_am1,
|
| 190 |
+
stride_am2,
|
| 191 |
+
stride_am3,
|
| 192 |
+
stride_ak1,
|
| 193 |
+
stride_ak2,
|
| 194 |
+
stride_ak3,
|
| 195 |
+
stride_bk1,
|
| 196 |
+
stride_bk2,
|
| 197 |
+
stride_bk3,
|
| 198 |
+
stride_bn1,
|
| 199 |
+
stride_bn2,
|
| 200 |
+
stride_bn3,
|
| 201 |
+
stride_cm1,
|
| 202 |
+
stride_cm2,
|
| 203 |
+
stride_cm3,
|
| 204 |
+
stride_cn1,
|
| 205 |
+
stride_cn2,
|
| 206 |
+
stride_cn3,
|
| 207 |
+
BLOCK_M: tl.constexpr, # DO NOT CHANGE NAME: MUST MATCH PERF MODEL
|
| 208 |
+
BLOCK_N: tl.constexpr, # DO NOT CHANGE NAME: MUST MATCH PERF MODEL
|
| 209 |
+
BLOCK_K: tl.constexpr, # DO NOT CHANGE NAME: MUST MATCH PERF MODEL
|
| 210 |
+
GROUP_M: tl.constexpr,
|
| 211 |
+
SPLIT_K: tl.constexpr, # DO NOT CHANGE NAME: MUST MATCH PERF MODEL
|
| 212 |
+
EVEN_K: tl.constexpr,
|
| 213 |
+
ACC_TYPE: tl.constexpr,
|
| 214 |
+
):
|
| 215 |
+
# matrix multiplication
|
| 216 |
+
pid = tl.program_id(0)
|
| 217 |
+
pid_k = tl.program_id(1)
|
| 218 |
+
grid_m1 = tl.cdiv(M1, BLOCK_M)
|
| 219 |
+
grid_m2 = tl.cdiv(M2, BLOCK_M)
|
| 220 |
+
grid_m3 = tl.cdiv(M3, BLOCK_M)
|
| 221 |
+
grid_n1 = tl.cdiv(N1, BLOCK_N)
|
| 222 |
+
grid_n2 = tl.cdiv(N2, BLOCK_N)
|
| 223 |
+
grid_n3 = tl.cdiv(N3, BLOCK_N)
|
| 224 |
+
grid_m = grid_m1 + grid_m2 + grid_m3
|
| 225 |
+
grid_n = grid_n1 + grid_n2 + grid_n3
|
| 226 |
+
|
| 227 |
+
# re-order program ID for better L2 performance
|
| 228 |
+
width = GROUP_M * grid_n
|
| 229 |
+
group_id = pid // width
|
| 230 |
+
group_size = min(grid_m - group_id * GROUP_M, GROUP_M)
|
| 231 |
+
pid_m = group_id * GROUP_M + (pid % group_size)
|
| 232 |
+
pid_n = (pid % width) // (group_size)
|
| 233 |
+
|
| 234 |
+
# We use tl.where to circumvent a regression in alignment auto-detection:
|
| 235 |
+
# https://github.com/openai/triton/issues/1784
|
| 236 |
+
|
| 237 |
+
A1 = tl.where(pid_m < grid_m1, A11, tl.where(pid_m < grid_m1 + grid_m2, A21, A31))
|
| 238 |
+
A2 = tl.where(pid_m < grid_m1, A12, tl.where(pid_m < grid_m1 + grid_m2, A22, A32))
|
| 239 |
+
A3 = tl.where(pid_m < grid_m1, A13, tl.where(pid_m < grid_m1 + grid_m2, A23, A33))
|
| 240 |
+
B1 = tl.where(pid_n < grid_n1, B11, tl.where(pid_n < grid_n1 + grid_n2, B12, B13))
|
| 241 |
+
B2 = tl.where(pid_n < grid_n1, B21, tl.where(pid_n < grid_n1 + grid_n2, B22, B23))
|
| 242 |
+
B3 = tl.where(pid_n < grid_n1, B31, tl.where(pid_n < grid_n1 + grid_n2, B32, B33))
|
| 243 |
+
C = tl.where(
|
| 244 |
+
pid_m < grid_m1,
|
| 245 |
+
tl.where(pid_n < grid_n1, C11, tl.where(pid_n < grid_n1 + grid_n2, C12, C13)),
|
| 246 |
+
tl.where(
|
| 247 |
+
pid_m < grid_m1 + grid_m2,
|
| 248 |
+
tl.where(
|
| 249 |
+
pid_n < grid_n1, C21, tl.where(pid_n < grid_n1 + grid_n2, C22, C23)
|
| 250 |
+
),
|
| 251 |
+
tl.where(
|
| 252 |
+
pid_n < grid_n1, C31, tl.where(pid_n < grid_n1 + grid_n2, C32, C33)
|
| 253 |
+
),
|
| 254 |
+
),
|
| 255 |
+
)
|
| 256 |
+
M = tl.where(pid_m < grid_m1, M1, tl.where(pid_m < grid_m1 + grid_m2, M2, M3))
|
| 257 |
+
N = tl.where(pid_n < grid_n1, N1, tl.where(pid_n < grid_n1 + grid_n2, N2, N3))
|
| 258 |
+
stride_ak = tl.where(
|
| 259 |
+
pid_m < grid_m1,
|
| 260 |
+
stride_ak1,
|
| 261 |
+
tl.where(pid_m < grid_m1 + grid_m2, stride_ak2, stride_ak3),
|
| 262 |
+
)
|
| 263 |
+
stride_bk = tl.where(
|
| 264 |
+
pid_n < grid_n1,
|
| 265 |
+
stride_bk1,
|
| 266 |
+
tl.where(pid_n < grid_n1 + grid_n2, stride_bk2, stride_bk3),
|
| 267 |
+
)
|
| 268 |
+
stride_cn = tl.where(
|
| 269 |
+
pid_m < grid_m1,
|
| 270 |
+
stride_cn1,
|
| 271 |
+
tl.where(pid_m < grid_m1 + grid_m2, stride_cn2, stride_cn3),
|
| 272 |
+
)
|
| 273 |
+
stride_cm = tl.where(
|
| 274 |
+
pid_n < grid_n1,
|
| 275 |
+
stride_cm1,
|
| 276 |
+
tl.where(pid_n < grid_n1 + grid_n2, stride_cm2, stride_cm3),
|
| 277 |
+
)
|
| 278 |
+
pid_m = tl.where(
|
| 279 |
+
pid_m < grid_m1,
|
| 280 |
+
pid_m,
|
| 281 |
+
tl.where(pid_m < grid_m1 + grid_m2, pid_m - grid_m1, pid_m - grid_m1 - grid_m2),
|
| 282 |
+
)
|
| 283 |
+
pid_n = tl.where(
|
| 284 |
+
pid_n < grid_n1,
|
| 285 |
+
pid_n,
|
| 286 |
+
tl.where(pid_n < grid_n1 + grid_n2, pid_n - grid_n1, pid_n - grid_n1 - grid_n2),
|
| 287 |
+
)
|
| 288 |
+
|
| 289 |
+
# do matrix multiplication
|
| 290 |
+
rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)
|
| 291 |
+
rn = pid_n * BLOCK_N + tl.arange(0, BLOCK_N)
|
| 292 |
+
ram = tl.max_contiguous(tl.multiple_of(rm % M, BLOCK_M), BLOCK_M)
|
| 293 |
+
rbn = tl.max_contiguous(tl.multiple_of(rn % N, BLOCK_N), BLOCK_N)
|
| 294 |
+
# pointers
|
| 295 |
+
acc = tl.zeros((BLOCK_M, BLOCK_N), dtype=ACC_TYPE)
|
| 296 |
+
grid_k1 = tl.cdiv(K1, BLOCK_K)
|
| 297 |
+
grid_k2 = tl.cdiv(K2, BLOCK_K)
|
| 298 |
+
grid_k3 = tl.cdiv(K3, BLOCK_K)
|
| 299 |
+
for tile in range(pid_k, grid_k1 + grid_k2 + grid_k3, SPLIT_K):
|
| 300 |
+
A = tl.where(tile < grid_k1, A1, tl.where(tile < grid_k1 + grid_k2, A2, A3))
|
| 301 |
+
B = tl.where(tile < grid_k1, B1, tl.where(tile < grid_k1 + grid_k2, B2, B3))
|
| 302 |
+
K = tl.where(tile < grid_k1, K1, tl.where(tile < grid_k1 + grid_k2, K2, K3))
|
| 303 |
+
stride_am = tl.where(
|
| 304 |
+
tile < grid_k1,
|
| 305 |
+
stride_am1,
|
| 306 |
+
tl.where(tile < grid_k1 + grid_k2, stride_am2, stride_am3),
|
| 307 |
+
)
|
| 308 |
+
stride_bn = tl.where(
|
| 309 |
+
tile < grid_k1,
|
| 310 |
+
stride_bn1,
|
| 311 |
+
tl.where(tile < grid_k1 + grid_k2, stride_bn2, stride_bn3),
|
| 312 |
+
)
|
| 313 |
+
my_tile = tl.where(
|
| 314 |
+
tile < grid_k1,
|
| 315 |
+
tile,
|
| 316 |
+
tl.where(
|
| 317 |
+
tile < grid_k1 + grid_k2, tile - grid_k1, tile - grid_k1 - grid_k2
|
| 318 |
+
),
|
| 319 |
+
)
|
| 320 |
+
rk = my_tile * BLOCK_K + tl.arange(0, BLOCK_K)
|
| 321 |
+
Ain = A + (ram[:, None] * stride_am + rk[None, :] * stride_ak)
|
| 322 |
+
Bin = B + (rk[:, None] * stride_bk + rbn[None, :] * stride_bn)
|
| 323 |
+
if EVEN_K:
|
| 324 |
+
a = tl.load(Ain)
|
| 325 |
+
b = tl.load(Bin)
|
| 326 |
+
else:
|
| 327 |
+
a = tl.load(Ain, mask=rk[None, :] < K, other=0.0)
|
| 328 |
+
b = tl.load(Bin, mask=rk[:, None] < K, other=0.0)
|
| 329 |
+
acc += tl.dot(a, b, allow_tf32=False)
|
| 330 |
+
acc = acc.to(C.dtype.element_ty)
|
| 331 |
+
# rematerialize rm and rn to save registers
|
| 332 |
+
rm = pid_m * BLOCK_M + tl.arange(0, BLOCK_M)
|
| 333 |
+
rn = pid_n * BLOCK_N + tl.arange(0, BLOCK_N)
|
| 334 |
+
C = C + (rm[:, None] * stride_cm + rn[None, :] * stride_cn)
|
| 335 |
+
mask = (rm < M)[:, None] & (rn < N)[None, :]
|
| 336 |
+
# handles write-back with reduction-splitting
|
| 337 |
+
if SPLIT_K == 1:
|
| 338 |
+
tl.store(C, acc, mask=mask)
|
| 339 |
+
else:
|
| 340 |
+
tl.atomic_add(C, acc, mask=mask)
|
| 341 |
+
|
| 342 |
+
|
| 343 |
+
def _check_row_or_column(row_or_col_type, row_or_col_idx, tensor_name, dim_name, vals):
|
| 344 |
+
assert len(vals) > 0
|
| 345 |
+
for pos, val in enumerate(vals[1:]):
|
| 346 |
+
assert val == vals[0], (
|
| 347 |
+
f"the tensors on {row_or_col_type} {row_or_col_idx} of the {tensor_name} "
|
| 348 |
+
f"must all have the same stride along the {dim_name} dimension, got "
|
| 349 |
+
f"{vals[0]} at position 0 and {val} at position {pos + 1}"
|
| 350 |
+
)
|
| 351 |
+
return vals[0]
|
| 352 |
+
|
| 353 |
+
|
| 354 |
+
def _get_strides(
|
| 355 |
+
ts: List[List[torch.Tensor]], tensor_name, dim_0_name, dim_1_name
|
| 356 |
+
) -> Tuple[List[int], List[int]]:
|
| 357 |
+
strides_0 = [
|
| 358 |
+
_check_row_or_column(
|
| 359 |
+
"column", idx, tensor_name, dim_0_name, [y.stride(0) for y in x]
|
| 360 |
+
)
|
| 361 |
+
for idx, x in enumerate(zip(*ts))
|
| 362 |
+
]
|
| 363 |
+
strides_1 = [
|
| 364 |
+
_check_row_or_column(
|
| 365 |
+
"row", idx, tensor_name, dim_1_name, [y.stride(1) for y in x]
|
| 366 |
+
)
|
| 367 |
+
for idx, x in enumerate(ts)
|
| 368 |
+
]
|
| 369 |
+
assert all(s == 1 for s in strides_0) or all(s == 1 for s in strides_1)
|
| 370 |
+
while len(strides_0) < 3:
|
| 371 |
+
strides_0.append(1 if strides_0[0] == 1 else 0)
|
| 372 |
+
while len(strides_1) < 3:
|
| 373 |
+
strides_1.append(1 if strides_1[0] == 1 else 0)
|
| 374 |
+
return strides_0, strides_1
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
def _launch_triton_matmul(
|
| 378 |
+
a: List[List[torch.Tensor]],
|
| 379 |
+
b: List[List[torch.Tensor]],
|
| 380 |
+
c: List[List[torch.Tensor]],
|
| 381 |
+
ms: List[int],
|
| 382 |
+
ns: List[int],
|
| 383 |
+
ks: List[int],
|
| 384 |
+
) -> None:
|
| 385 |
+
strides_am, strides_ak = _get_strides(a, "first operand", "m", "k")
|
| 386 |
+
strides_bk, strides_bn = _get_strides(b, "second operand", "k", "n")
|
| 387 |
+
strides_cm, strides_cn = _get_strides(c, "output", "m", "n")
|
| 388 |
+
|
| 389 |
+
# accumulator types
|
| 390 |
+
ACC_TYPE = (
|
| 391 |
+
tl.float32
|
| 392 |
+
if c[0][0].dtype in [torch.float16, torch.bfloat16, torch.float32]
|
| 393 |
+
else tl.int32
|
| 394 |
+
)
|
| 395 |
+
|
| 396 |
+
# launch kernel
|
| 397 |
+
def grid(META):
|
| 398 |
+
return (
|
| 399 |
+
sum(triton.cdiv(m, META["BLOCK_M"]) for m in ms)
|
| 400 |
+
* sum(triton.cdiv(n, META["BLOCK_N"]) for n in ns),
|
| 401 |
+
META["SPLIT_K"],
|
| 402 |
+
)
|
| 403 |
+
|
| 404 |
+
_xformers_tiled_matmul_kernel[grid](
|
| 405 |
+
*[
|
| 406 |
+
a[min(i, len(a) - 1)][min(j, len(a[0]) - 1)]
|
| 407 |
+
for i in range(3)
|
| 408 |
+
for j in range(3)
|
| 409 |
+
],
|
| 410 |
+
*[
|
| 411 |
+
b[min(i, len(b) - 1)][min(j, len(b[0]) - 1)]
|
| 412 |
+
for i in range(3)
|
| 413 |
+
for j in range(3)
|
| 414 |
+
],
|
| 415 |
+
*[
|
| 416 |
+
c[min(i, len(c) - 1)][min(j, len(c[0]) - 1)]
|
| 417 |
+
for i in range(3)
|
| 418 |
+
for j in range(3)
|
| 419 |
+
],
|
| 420 |
+
*[ms[i] if len(ms) > i else 0 for i in range(3)],
|
| 421 |
+
*[ns[i] if len(ns) > i else 0 for i in range(3)],
|
| 422 |
+
*[ks[i] if len(ks) > i else 0 for i in range(3)],
|
| 423 |
+
*strides_am,
|
| 424 |
+
*strides_ak,
|
| 425 |
+
*strides_bk,
|
| 426 |
+
*strides_bn,
|
| 427 |
+
*strides_cm,
|
| 428 |
+
*strides_cn,
|
| 429 |
+
ACC_TYPE=ACC_TYPE,
|
| 430 |
+
)
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/common.py
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the BSD license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
|
| 6 |
+
import inspect
|
| 7 |
+
from dataclasses import dataclass
|
| 8 |
+
from functools import wraps
|
| 9 |
+
from typing import Any, Callable, Dict, List, Type, TypeVar, Union
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
from torch.torch_version import TorchVersion
|
| 13 |
+
from typing_extensions import Annotated, get_args, get_origin
|
| 14 |
+
|
| 15 |
+
from .. import _is_triton_available
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def get_operator(library: str, name: str):
|
| 19 |
+
def no_such_operator(*args, **kwargs):
|
| 20 |
+
raise RuntimeError(
|
| 21 |
+
f"No such operator {library}::{name} - did you forget to build xformers with `python setup.py develop`?"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
return getattr(getattr(torch.ops, library), name)
|
| 26 |
+
except (RuntimeError, AttributeError):
|
| 27 |
+
return no_such_operator
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def get_xformers_operator(name: str):
|
| 31 |
+
return get_operator("xformers", name)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
class BaseOperator:
|
| 35 |
+
OPERATOR: Any
|
| 36 |
+
NAME: str
|
| 37 |
+
OPERATOR_CATEGORY: str
|
| 38 |
+
|
| 39 |
+
@classmethod
|
| 40 |
+
def is_available(cls) -> bool:
|
| 41 |
+
if cls.OPERATOR is None or cls.OPERATOR.__name__ == "no_such_operator":
|
| 42 |
+
return False
|
| 43 |
+
return True
|
| 44 |
+
|
| 45 |
+
@classmethod
|
| 46 |
+
def operator_flop(cls, *inputs) -> int:
|
| 47 |
+
"""Calculate number of FLOP given inputs to `OPERATOR`"""
|
| 48 |
+
return -1
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
OPERATORS_REGISTRY: List[Type[BaseOperator]] = []
|
| 52 |
+
FUNC_TO_XFORMERS_OPERATOR: Dict[Any, Type[BaseOperator]] = {}
|
| 53 |
+
|
| 54 |
+
ClsT = TypeVar("ClsT")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def register_operator(cls: ClsT) -> ClsT:
|
| 58 |
+
global OPERATORS_REGISTRY, FUNC_TO_XFORMERS_OPERATOR
|
| 59 |
+
OPERATORS_REGISTRY.append(cls) # type: ignore
|
| 60 |
+
FUNC_TO_XFORMERS_OPERATOR[cls.OPERATOR] = cls # type: ignore
|
| 61 |
+
return cls
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# post-2.0, avoids a warning
|
| 65 |
+
# (`torch.Tensor.storage` will also be deleted in the future)
|
| 66 |
+
_GET_TENSOR_STORAGE = getattr(torch.Tensor, "untyped_storage", None)
|
| 67 |
+
if _GET_TENSOR_STORAGE is None: # pre-2.0, `untyped_storage` didn't exist
|
| 68 |
+
_GET_TENSOR_STORAGE = torch.Tensor.storage
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
def _get_storage_base(x: torch.Tensor) -> int:
|
| 72 |
+
return _GET_TENSOR_STORAGE(x).data_ptr() # type: ignore
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
@dataclass(frozen=True)
|
| 76 |
+
class Alias:
|
| 77 |
+
name: str
|
| 78 |
+
write: bool
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def make_pytorch_cuda_operator(fn: ClsT) -> ClsT:
|
| 82 |
+
return turn_into_pytorch_op(fn, "CUDA")
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def make_pytorch_operator_for_dispatch_key(dispatch_key: str) -> Callable[[ClsT], ClsT]:
|
| 86 |
+
def decorator(fn: ClsT) -> ClsT:
|
| 87 |
+
return turn_into_pytorch_op(fn, dispatch_key)
|
| 88 |
+
|
| 89 |
+
return decorator
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def turn_into_pytorch_op(fn: ClsT, dispatch_key: str) -> ClsT:
|
| 93 |
+
from .. import get_python_lib
|
| 94 |
+
|
| 95 |
+
def render_arg_type(annotation) -> str:
|
| 96 |
+
# Optional[T] is an alias for Union[T, None]
|
| 97 |
+
if get_origin(annotation) is Union:
|
| 98 |
+
inner_types = [
|
| 99 |
+
t for t in get_args(annotation) if t is not type(None) # noqa: E721
|
| 100 |
+
]
|
| 101 |
+
if len(inner_types) == 1:
|
| 102 |
+
return f"{render_arg_type(inner_types[0])}?"
|
| 103 |
+
if get_origin(annotation) is list:
|
| 104 |
+
(inner_type,) = get_args(annotation)
|
| 105 |
+
return f"{render_arg_type(inner_type)}[]"
|
| 106 |
+
if get_origin(annotation) is tuple:
|
| 107 |
+
return (
|
| 108 |
+
"("
|
| 109 |
+
+ ", ".join([render_arg_type(t) for t in get_args(annotation)])
|
| 110 |
+
+ ")"
|
| 111 |
+
)
|
| 112 |
+
if get_origin(annotation) is Annotated:
|
| 113 |
+
inner_type, annotation = get_args(annotation)
|
| 114 |
+
if isinstance(annotation, Alias):
|
| 115 |
+
alias = annotation.name + ("!" if annotation.write else "")
|
| 116 |
+
return f"{render_arg_type(inner_type)}({alias})"
|
| 117 |
+
if annotation is torch.Tensor:
|
| 118 |
+
return "Tensor"
|
| 119 |
+
if annotation is bool:
|
| 120 |
+
return "bool"
|
| 121 |
+
if annotation is int:
|
| 122 |
+
return "int"
|
| 123 |
+
if annotation is float:
|
| 124 |
+
return "float"
|
| 125 |
+
if annotation is torch.dtype:
|
| 126 |
+
return "ScalarType"
|
| 127 |
+
if annotation is torch.distributed.ProcessGroup:
|
| 128 |
+
return "__torch__.torch.classes.c10d.ProcessGroup"
|
| 129 |
+
assert False, f"Unable to parse annotation: `{annotation}`"
|
| 130 |
+
|
| 131 |
+
def render_default_value(default):
|
| 132 |
+
if default is inspect.Parameter.empty:
|
| 133 |
+
return ""
|
| 134 |
+
return f" = {default!r}"
|
| 135 |
+
|
| 136 |
+
sign = inspect.signature(fn) # type: ignore
|
| 137 |
+
arguments = [
|
| 138 |
+
f"{render_arg_type(arg.annotation)} {arg.name}{render_default_value(arg.default)}"
|
| 139 |
+
for arg in sign.parameters.values()
|
| 140 |
+
]
|
| 141 |
+
op_name = fn.__name__ # type: ignore
|
| 142 |
+
definition = f"{op_name}({', '.join(arguments)}) -> {render_arg_type(sign.return_annotation)}"
|
| 143 |
+
|
| 144 |
+
def callee(*args, **kwargs):
|
| 145 |
+
ba = sign.bind(*args, **kwargs)
|
| 146 |
+
for name, value in ba.arguments.items():
|
| 147 |
+
if sign.parameters[name].annotation is torch.distributed.ProcessGroup:
|
| 148 |
+
from .._C import unbox_process_group
|
| 149 |
+
|
| 150 |
+
ba.arguments[name] = unbox_process_group(value)
|
| 151 |
+
return fn(*ba.args, **ba.kwargs)
|
| 152 |
+
|
| 153 |
+
xformers_lib = get_python_lib()
|
| 154 |
+
xformers_lib.define(definition)
|
| 155 |
+
xformers_lib.impl(op_name, callee, dispatch_key)
|
| 156 |
+
dispatcher_impl = getattr(getattr(torch.ops, xformers_lib.ns), op_name)
|
| 157 |
+
|
| 158 |
+
@wraps(fn) # type: ignore[arg-type]
|
| 159 |
+
def caller(*args, **kwargs):
|
| 160 |
+
ba = sign.bind(*args, **kwargs)
|
| 161 |
+
for name, value in ba.arguments.items():
|
| 162 |
+
if sign.parameters[name].annotation is torch.distributed.ProcessGroup:
|
| 163 |
+
from .._C import box_process_group
|
| 164 |
+
|
| 165 |
+
ba.arguments[name] = box_process_group(value)
|
| 166 |
+
return dispatcher_impl(*ba.args, **ba.kwargs)
|
| 167 |
+
|
| 168 |
+
return caller # type: ignore
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
def _has_triton2():
|
| 172 |
+
if not _is_triton_available():
|
| 173 |
+
return False
|
| 174 |
+
import triton
|
| 175 |
+
|
| 176 |
+
tv = TorchVersion(triton.__version__)
|
| 177 |
+
return tv >= (2, 1) or tv == (2, 0)
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def _has_triton21():
|
| 181 |
+
if not _is_triton_available():
|
| 182 |
+
return False
|
| 183 |
+
import triton
|
| 184 |
+
|
| 185 |
+
tv = TorchVersion(triton.__version__)
|
| 186 |
+
return tv >= (2, 1)
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/differentiable_collectives.py
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
|
| 2 |
+
#
|
| 3 |
+
# This source code is licensed under the BSD license found in the
|
| 4 |
+
# LICENSE file in the root directory of this source tree.
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
from typing import Optional, Tuple
|
| 8 |
+
|
| 9 |
+
import torch
|
| 10 |
+
import torch.distributed
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def all_reduce(
|
| 14 |
+
x: torch.Tensor, *, process_group: torch.distributed.ProcessGroup
|
| 15 |
+
) -> None:
|
| 16 |
+
assert x.is_contiguous()
|
| 17 |
+
|
| 18 |
+
mp_size = process_group.size()
|
| 19 |
+
if mp_size == 1:
|
| 20 |
+
return
|
| 21 |
+
|
| 22 |
+
torch.distributed.all_reduce(
|
| 23 |
+
tensor=x, op=torch.distributed.ReduceOp.SUM, group=process_group
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def gather_along_first_dim_async(
|
| 28 |
+
input_: torch.Tensor, *, process_group: torch.distributed.ProcessGroup
|
| 29 |
+
) -> Tuple[torch.Tensor, Optional[torch.distributed.Work]]:
|
| 30 |
+
assert input_.is_contiguous()
|
| 31 |
+
mp_size = process_group.size()
|
| 32 |
+
if mp_size == 1:
|
| 33 |
+
return input_, None
|
| 34 |
+
|
| 35 |
+
output = input_.new_empty((input_.shape[0] * mp_size,) + input_.shape[1:])
|
| 36 |
+
handle = torch.distributed.all_gather_into_tensor(
|
| 37 |
+
output_tensor=output,
|
| 38 |
+
input_tensor=input_,
|
| 39 |
+
group=process_group,
|
| 40 |
+
async_op=True,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
return output, handle
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def reduce_scatter_along_first_dim_async(
|
| 47 |
+
input_: torch.Tensor, *, process_group: torch.distributed.ProcessGroup
|
| 48 |
+
) -> Tuple[torch.Tensor, Optional[torch.distributed.Work]]:
|
| 49 |
+
assert input_.is_contiguous()
|
| 50 |
+
mp_size = process_group.size()
|
| 51 |
+
if mp_size == 1:
|
| 52 |
+
return input_, None
|
| 53 |
+
|
| 54 |
+
assert input_.shape[0] % mp_size == 0
|
| 55 |
+
output = input_.new_empty((input_.shape[0] // mp_size,) + input_.shape[1:])
|
| 56 |
+
handle = torch.distributed.reduce_scatter_tensor(
|
| 57 |
+
output=output,
|
| 58 |
+
input=input_,
|
| 59 |
+
op=torch.distributed.ReduceOp.SUM,
|
| 60 |
+
group=process_group,
|
| 61 |
+
async_op=True,
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
return output, handle
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
def gather_along_first_dim(
|
| 68 |
+
input_: torch.Tensor, *, process_group: torch.distributed.ProcessGroup
|
| 69 |
+
) -> torch.Tensor:
|
| 70 |
+
output, handle = gather_along_first_dim_async(input_, process_group=process_group)
|
| 71 |
+
if handle is not None:
|
| 72 |
+
handle.wait()
|
| 73 |
+
return output
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def reduce_scatter_along_first_dim(
|
| 77 |
+
input_: torch.Tensor, *, process_group: torch.distributed.ProcessGroup
|
| 78 |
+
) -> torch.Tensor:
|
| 79 |
+
output, handle = reduce_scatter_along_first_dim_async(
|
| 80 |
+
input_, process_group=process_group
|
| 81 |
+
)
|
| 82 |
+
if handle is not None:
|
| 83 |
+
handle.wait()
|
| 84 |
+
return output
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
class _CopyToModelParallelRegion(torch.autograd.Function):
|
| 88 |
+
@staticmethod
|
| 89 |
+
def forward( # type: ignore[override]
|
| 90 |
+
ctx, input_: torch.Tensor, process_group: torch.distributed.ProcessGroup
|
| 91 |
+
) -> torch.Tensor:
|
| 92 |
+
ctx.process_group = process_group
|
| 93 |
+
return input_
|
| 94 |
+
|
| 95 |
+
@staticmethod
|
| 96 |
+
def backward( # type: ignore[override]
|
| 97 |
+
ctx, grad_output: torch.Tensor
|
| 98 |
+
) -> Tuple[torch.Tensor, None]:
|
| 99 |
+
all_reduce(grad_output, process_group=ctx.process_group)
|
| 100 |
+
return grad_output, None
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def copy_to_model_parallel_region(
|
| 104 |
+
x: torch.Tensor, process_group: torch.distributed.ProcessGroup
|
| 105 |
+
) -> torch.Tensor:
|
| 106 |
+
return _CopyToModelParallelRegion.apply(x, process_group)
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
class _ReduceFromModelParallelRegion(torch.autograd.Function):
|
| 110 |
+
@staticmethod
|
| 111 |
+
def forward( # type: ignore[override]
|
| 112 |
+
ctx, input_: torch.Tensor, process_group: torch.distributed.ProcessGroup
|
| 113 |
+
) -> torch.Tensor:
|
| 114 |
+
all_reduce(input_, process_group=process_group)
|
| 115 |
+
ctx.mark_dirty(input_)
|
| 116 |
+
return input_
|
| 117 |
+
|
| 118 |
+
@staticmethod
|
| 119 |
+
def backward( # type: ignore[override]
|
| 120 |
+
ctx, grad_output: torch.Tensor
|
| 121 |
+
) -> Tuple[torch.Tensor, None]:
|
| 122 |
+
return grad_output, None
|
| 123 |
+
|
| 124 |
+
|
| 125 |
+
def reduce_from_model_parallel_region(
|
| 126 |
+
x: torch.Tensor, process_group: torch.distributed.ProcessGroup
|
| 127 |
+
) -> torch.Tensor:
|
| 128 |
+
return _ReduceFromModelParallelRegion.apply(x, process_group)
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
class _GatherFromSequenceParallelRegion(torch.autograd.Function):
|
| 132 |
+
@staticmethod
|
| 133 |
+
def forward( # type: ignore[override]
|
| 134 |
+
ctx, x: torch.Tensor, process_group: torch.distributed.ProcessGroup
|
| 135 |
+
) -> torch.Tensor:
|
| 136 |
+
ctx.process_group = process_group
|
| 137 |
+
return gather_along_first_dim(x, process_group=process_group)
|
| 138 |
+
|
| 139 |
+
@staticmethod
|
| 140 |
+
def backward( # type: ignore[override]
|
| 141 |
+
ctx, grad_output: torch.Tensor
|
| 142 |
+
) -> Tuple[torch.Tensor, None]:
|
| 143 |
+
return (
|
| 144 |
+
reduce_scatter_along_first_dim(
|
| 145 |
+
grad_output, process_group=ctx.process_group
|
| 146 |
+
),
|
| 147 |
+
None,
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
def gather_from_sequence_parallel_region(
|
| 152 |
+
x: torch.Tensor, process_group: torch.distributed.ProcessGroup
|
| 153 |
+
) -> torch.Tensor:
|
| 154 |
+
return _GatherFromSequenceParallelRegion.apply(x, process_group)
|
| 155 |
+
|
| 156 |
+
|
| 157 |
+
class _ScatterToSequenceParallelRegion(torch.autograd.Function):
|
| 158 |
+
@staticmethod
|
| 159 |
+
def forward( # type: ignore[override]
|
| 160 |
+
ctx, x: torch.Tensor, process_group: torch.distributed.ProcessGroup
|
| 161 |
+
) -> torch.Tensor:
|
| 162 |
+
ctx.process_group = process_group
|
| 163 |
+
return reduce_scatter_along_first_dim(x, process_group=process_group)
|
| 164 |
+
|
| 165 |
+
@staticmethod
|
| 166 |
+
def backward( # type: ignore[override]
|
| 167 |
+
ctx, grad_output: torch.Tensor
|
| 168 |
+
) -> Tuple[torch.Tensor, None]:
|
| 169 |
+
return (
|
| 170 |
+
gather_along_first_dim(grad_output, process_group=ctx.process_group),
|
| 171 |
+
None,
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
|
| 175 |
+
def scatter_to_sequence_parallel_region(
|
| 176 |
+
x: torch.Tensor, process_group: torch.distributed.ProcessGroup
|
| 177 |
+
) -> torch.Tensor:
|
| 178 |
+
return _ScatterToSequenceParallelRegion.apply(x, process_group)
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/fmha/__pycache__/__init__.cpython-310.pyc
ADDED
|
Binary file (12.1 kB). View file
|
|
|
evalkit_tf437/lib/python3.10/site-packages/xformers/ops/fmha/__pycache__/attn_bias.cpython-310.pyc
ADDED
|
Binary file (31.2 kB). View file
|
|
|