Beispiel #1
0
def test_heugcd_univariate_integers():
    R, x = ring('x', ZZ)

    f = x**4 + 8 * x**3 + 21 * x**2 + 22 * x + 8
    g = x**3 + 6 * x**2 + 11 * x + 6

    h = x**2 + 3 * x + 2

    cff = x**2 + 5 * x + 4
    cfg = x + 3

    assert heugcd(f, g) == (h, cff, cfg)

    f = x**4 - 4
    g = x**4 + 4 * x**2 + 4

    h = x**2 + 2

    cff = x**2 - 2
    cfg = x**2 + 2

    assert heugcd(f, g) == (h, cff, cfg)

    f = x**8 + x**6 - 3 * x**4 - 3 * x**3 + 8 * x**2 + 2 * x - 5
    g = 3 * x**6 + 5 * x**4 - 4 * x**2 - 9 * x + 21

    h = 1

    cff = f
    cfg = g

    assert heugcd(f, g) == (h, cff, cfg)

    f = - 352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272*x**49 \
        + 46818041807522713962450042363465092040687472354933295397472942006618953623327997952*x**42 \
        + 378182690892293941192071663536490788434899030680411695933646320291525827756032*x**35 \
        + 112806468807371824947796775491032386836656074179286744191026149539708928*x**28 \
        - 12278371209708240950316872681744825481125965781519138077173235712*x**21 \
        + 289127344604779611146960547954288113529690984687482920704*x**14 \
        + 19007977035740498977629742919480623972236450681*x**7 \
        + 311973482284542371301330321821976049

    h = 365431878023781158602430064717380211405897160759702125019136*x**21 \
        + 197599133478719444145775798221171663643171734081650688*x**14 \
        - 9504116979659010018253915765478924103928886144*x**7 \
        - 311973482284542371301330321821976049
    cff = -964661685087874498642420170752*x**28 + 649736296036977287118848*x**21 \
        + 658473216967637120*x**14 - 30463679113*x**7 - 1
    cfg = -47268422569305850433478588366848*x**27 + 30940259392972115602096128*x**20 \
        + 18261628279718027904*x**13 - 426497272383*x**6

    assert heugcd(f, f.diff(x)) == (h, cff, cfg)

    f = 1317378933230047068160 * x + 2945748836994210856960
    g = 120352542776360960 * x + 269116466014453760

    h = 120352542776360960 * x + 269116466014453760
    cff = 10946
    cfg = 1

    assert heugcd(f, g) == (h, cff, cfg)

    with using(heu_gcd_max=0):
        pytest.raises(HeuristicGCDFailed, lambda: heugcd(f, g))
def test_benchmark_czichowski(method):
    # This is very slow (> 2 minutes on 3.4 GHz) without GMPY

    with config.using(groebner=method):
        R, x, t = ring('x t', ZZ, lex)
        I = [
            9 * x**8 + 36 * x**7 - 32 * x**6 -
            252 * x**5 - 78 * x**4 + 468 * x**3 + 288 * x**2 - 108 * x + 9,
            (-72 - 72 * t) * x**7 + (-256 - 252 * t) * x**6 +
            (192 + 192 * t) * x**5 + (1280 + 1260 * t) * x**4 +
            (312 + 312 * t) * x**3 + (-404 * t) * x**2 + (-576 - 576 * t) * x +
            96 + 108 * t
        ]

        assert groebner(I, R) == [
            3725588592068034903797967297424801242396746870413359539263038139343329273586196480000
            * x -
            160420835591776763325581422211936558925462474417709511019228211783493866564923546661604487873
            * t**7 -
            1406108495478033395547109582678806497509499966197028487131115097902188374051595011248311352864
            * t**6 -
            5241326875850889518164640374668786338033653548841427557880599579174438246266263602956254030352
            * t**5 -
            10758917262823299139373269714910672770004760114329943852726887632013485035262879510837043892416
            * t**4 -
            13119383576444715672578819534846747735372132018341964647712009275306635391456880068261130581248
            * t**3 -
            9491412317016197146080450036267011389660653495578680036574753839055748080962214787557853941760
            * t**2 -
            3767520915562795326943800040277726397326609797172964377014046018280260848046603967211258368000
            * t -
            632314652371226552085897259159210286886724229880266931574701654721512325555116066073245696000,
            610733380717522355121 * t**8 + 6243748742141230639968 * t**7 +
            27761407182086143225024 * t**6 + 70066148869420956398592 * t**5 +
            109701225644313784229376 * t**4 + 109009005495588442152960 * t**3 +
            67072101084384786432000 * t**2 + 23339979742629593088000 * t +
            3513592776846090240000,
        ]

        R, x, t = ring('x t', ZZ, grlex)
        I = [i.set_ring(R) for i in I]

        assert groebner(I, R) == [
            16996618586000601590732959134095643086442 * t**3 * x -
            32936701459297092865176560282688198064839 * t**3 +
            78592411049800639484139414821529525782364 * t**2 * x -
            120753953358671750165454009478961405619916 * t**2 +
            120988399875140799712152158915653654637280 * t * x -
            144576390266626470824138354942076045758736 * t +
            60017634054270480831259316163620768960 * x**2 +
            61976058033571109604821862786675242894400 * x -
            56266268491293858791834120380427754600960,
            576689018321912327136790519059646508441672750656050290242749 * t**4
            + 2326673103677477425562248201573604572527893938459296513327336 *
            t**3 +
            110743790416688497407826310048520299245819959064297990236000 *
            t**2 * x +
            3308669114229100853338245486174247752683277925010505284338016 *
            t**2 +
            323150205645687941261103426627818874426097912639158572428800 * t *
            x +
            1914335199925152083917206349978534224695445819017286960055680 * t +
            861662882561803377986838989464278045397192862768588480000 * x**2 +
            235296483281783440197069672204341465480107019878814196672000 * x +
            361850798943225141738895123621685122544503614946436727532800,
            -117584925286448670474763406733005510014188341867 * t**3 +
            68566565876066068463853874568722190223721653044 * t**2 * x -
            435970731348366266878180788833437896139920683940 * t**2 +
            196297602447033751918195568051376792491869233408 * t * x -
            525011527660010557871349062870980202067479780112 * t +
            517905853447200553360289634770487684447317120 * x**3 +
            569119014870778921949288951688799397569321920 * x**2 +
            138877356748142786670127389526667463202210102080 * x -
            205109210539096046121625447192779783475018619520,
            -3725142681462373002731339445216700112264527 * t**3 +
            583711207282060457652784180668273817487940 * t**2 * x -
            12381382393074485225164741437227437062814908 * t**2 +
            151081054097783125250959636747516827435040 * t * x**2 +
            1814103857455163948531448580501928933873280 * t * x -
            13353115629395094645843682074271212731433648 * t +
            236415091385250007660606958022544983766080 * x**2 +
            1390443278862804663728298060085399578417600 * x -
            4716885828494075789338754454248931750698880,
        ]
def test__zz_wang():
    R, x, y, z = ring('x y z', ZZ)
    UV, _x = ring('x', ZZ)

    p = ZZ(nextprime(R._zz_mignotte_bound(w_1)))

    assert p == 6291469

    t_1, k_1, e_1 = y, 1, ZZ(-14)
    t_2, k_2, e_2 = z, 2, ZZ(3)
    t_3, k_3, e_3 = y + z, 2, ZZ(-11)
    t_4, k_4, e_4 = y - z, 1, ZZ(-17)

    T = [t_1, t_2, t_3, t_4]
    K = [k_1, k_2, k_3, k_4]
    E = [e_1, e_2, e_3, e_4]

    T = list(zip([t.drop(x) for t in T], K))

    A = [ZZ(-14), ZZ(3)]

    S = w_1.eval([(y, A[0]), (z, A[1])])
    cs, s = S.primitive()

    assert cs == 1 and s == S == (1036728 * _x**6 + 915552 * _x**5 +
                                  55748 * _x**4 + 105621 * _x**3 -
                                  17304 * _x**2 - 26841 * _x - 644)

    assert R._zz_wang_non_divisors(E, cs, ZZ(4)) == [7, 3, 11, 17]
    assert s.is_squarefree and s.degree() == w_1.degree()

    _, H = UV._zz_factor_sqf(s)

    h_1 = 187 * _x**2 - 23
    h_2 = 44 * _x**2 + 42 * _x + 1
    h_3 = 126 * _x**2 - 9 * _x + 28

    LC = [lc.drop(x) for lc in [y**2 - z**2, -4 * y - 4 * z, -y * z**2]]
    factors = R._zz_wang_hensel_lifting(w_1, H, LC, A, p)

    assert H == [h_1, h_2, h_3]
    assert R._zz_wang_lead_coeffs(w_1, T, cs, E, H, A) == (w_1, H, LC)
    assert functools.reduce(operator.mul, factors) == w_1

    # coverage tests
    f = x**6 + 5 * x**4 * y - 5 * x**2 * y**2 - y**3

    assert R._zz_wang(f, mod=4,
                      seed=1) == [x**2 - y, x**4 + 6 * x**2 * y + y**2]

    # This tests a bug in the Wang algorithm that occured only with a very
    # specific set of random numbers; issue sympy/sympy#6355.
    random_sequence = [
        -1, -1, 0, 0, 0, 0, -1, -1, 0, -1, 3, -1, 3, 3, 3, 3, -1, 3
    ]

    R, x, y, z = ring('x y z', ZZ)

    f = 2 * x**2 + y * z - y - z**2 + z

    assert R._zz_wang(f, seed=random_sequence) == [f]

    with using(eez_restart_if_needed=False):
        pytest.raises(ExtraneousFactors,
                      lambda: R._zz_wang(f, seed=random_sequence))
Beispiel #4
0
def test_dmp_prem():
    _, x = ring('x', FF(7))

    f = x**2 + x + 3
    g = 2 * x + 2

    assert f.prem(g) == 5  # issue sympy/sympy#20397

    _, x = ring('x', ZZ)

    f = 3 * x**3 + x**2 + x + 5
    g = 5 * x**2 - 3 * x + 1

    r = 52 * x + 111

    assert f.prem(g) == r

    pytest.raises(ZeroDivisionError, lambda: f.prem(0))

    f = x**2 + 1
    g = 2 * x - 4
    r = 20

    assert f.prem(g) == r

    _, x = ring('x', QQ)

    f = 3 * x**3 + x**2 + x + 5
    g = 5 * x**2 - 3 * x + 1

    r = 52 * x + 111

    assert g.prem(f) == g
    assert f.prem(g) == r

    _, x, y = ring('x y', ZZ)

    f = x**2 - y**2
    g = x - y

    assert f.prem(g) == 0

    f = x**2 + y**2
    g = x - y

    r = 2 * y**2

    assert f.prem(g) == r

    pytest.raises(ZeroDivisionError, lambda: f.prem(0))

    g = 2 * x - 2 * y

    r = 8 * y**2

    assert g.prem(f) == g
    assert f.prem(g) == r

    f = x**2 + x * y
    g = 2 * x + 2

    r = -4 * y + 4

    assert f.prem(g) == r
Beispiel #5
0
def test_PolyElement_cancel():
    R, x = ring('x', ZZ)

    f = 2 * x**2 - 2
    g = x**2 - 2 * x + 1

    p = 2 * x + 2
    q = x - 1

    assert f.cancel(g) == (p, q)
    assert f.cancel(g, include=False) == (1, 1, p, q)

    f = -x - 2
    g = 3 * x - 4

    F = x + 2
    G = -3 * x + 4

    assert f.cancel(g) == (f, g)
    assert F.cancel(G) == (f, g)

    assert R(0).cancel(R(0)) == (0, 0)
    assert R(0).cancel(R(0), include=False) == (1, 1, 0, 0)

    assert x.cancel(R(0)) == (1, 0)
    assert x.cancel(R(0), include=False) == (1, 1, 1, 0)

    assert R(0).cancel(x) == (0, 1)
    assert R(0).cancel(x, include=False) == (1, 1, 0, 1)

    f = R(0)
    g = x
    one = 1

    assert f.cancel(g, include=True) == (f, one)

    R, x = ring('x', QQ)

    assert (x**2 / 4 - 1).cancel(x / 2 - 1) == (x + 2, 2)

    Fx, x = field('x', ZZ)
    _, t = ring('t', Fx)

    f = (-x**2 - 4) / 4 * t
    g = t**2 + (x**2 + 2) / 2

    assert f.cancel(g) == ((-x**2 - 4) * t, 4 * t**2 + 2 * x**2 + 4)

    R, x, y = ring('x y', ZZ)

    f = 2 * x**2 - 2
    g = x**2 - 2 * x + 1

    p = 2 * x + 2
    q = x - 1

    assert f.cancel(g) == (p, q)
    assert f.cancel(g, include=False) == (1, 1, p, q)

    assert R(0).cancel(R(0)) == (0, 0)
    assert R(0).cancel(R(0), include=False) == (1, 1, 0, 0)

    assert y.cancel(R(0)) == (1, 0)
    assert y.cancel(R(0), include=False) == (1, 1, 1, 0)

    assert R(0).cancel(y) == (0, 1)
    assert R(0).cancel(y, include=False) == (1, 1, 0, 1)

    assert (y**2 - x**2).cancel(y - x) == (x + y, 1)

    f = 2 * x**3 + 4 * x**2 + 2 * x
    g = 3 * x**2 + 3 * x
    F = 2 * x + 2
    G = 3

    assert f.cancel(g) == (F, G)

    assert (-f).cancel(g) == (-F, G)
    assert f.cancel(-g) == (-F, G)

    R, x, y = ring('x y', QQ)

    f = x**3 / 2 + x**2 + x / 2
    g = x**2 / 3 + x / 3
    F = 3 * x + 3
    G = 2

    assert f.cancel(g) == (F, G)

    assert (-f).cancel(g) == (-F, G)
    assert f.cancel(-g) == (-F, G)
Beispiel #6
0
def test_PolyElement_is_irreducible():
    R, x = ring('x', FF(5))

    f = (x**10 + 4 * x**9 + 2 * x**8 + 2 * x**7 + 3 * x**6 + 2 * x**5 +
         4 * x**4 + x**3 + 4 * x**2 + 4)
    g = 3 * x**2 + 2 * x + 4

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert f.is_irreducible is True
            assert g.is_irreducible is False

    R, x = ring('x', FF(11))

    f = R(7)
    g = 7 * x + 3
    h = 7 * x**2 + 3 * x + 1

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert f.is_irreducible is True
            assert g.is_irreducible is True
            assert h.is_irreducible is False

    with using(gf_irred_method='other'):
        pytest.raises(KeyError, lambda: f.is_irreducible)

    R, x = ring('x', FF(13))

    f = 2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 6
    g = 2 * x**4 + 3 * x**3 + 4 * x**2 + 5 * x + 8

    with using(gf_irred_method='ben-or'):
        assert f.is_irreducible is False
        assert g.is_irreducible is True

    R, x = ring('x', FF(17))

    f = (x**10 + 9 * x**9 + 9 * x**8 + 13 * x**7 + 16 * x**6 + 15 * x**5 +
         6 * x**4 + 7 * x**3 + 7 * x**2 + 7 * x + 10)
    g = (x**10 + 7 * x**9 + 16 * x**8 + 7 * x**7 + 15 * x**6 + 13 * x**5 +
         13 * x**4 + 11 * x**3 + 16 * x**2 + 10 * x + 9)
    h = f * g

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert f.is_irreducible is True
            assert g.is_irreducible is True
            assert h.is_irreducible is False

    F9 = FF(3, [2, 2, 1])
    R, x = ring('x', F9)

    f = x**3 + F9(8) * x**2 + F9(8) * x + F9(4)

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert f.is_irreducible is False

    F27 = FF(3, [1, 0, 2, 1])
    R, x = ring('x', F27)

    f = x**3 + F27(8) * x**2 + F27(19) * x + F27(24)

    for method in ('ben-or', 'rabin'):
        with using(gf_irred_method=method):
            assert f.is_irreducible is True

    R, x = ring('x', ZZ)

    assert x.is_irreducible is True
    assert (x**2 + x + 1).is_irreducible is True
    assert (x**2 + 2 * x + 1).is_irreducible is False
    assert (x**2 - 1).is_irreducible is False

    f = 3 * x**4 + 2 * x**3 + 6 * x**2 + 8 * x

    assert (f + 7).is_irreducible is True
    assert (f + 4).is_irreducible is True
    assert (f + 10).is_irreducible is True
    assert (f + 14).is_irreducible is True

    R, x, y = ring('x y', ZZ)

    assert R(2).is_irreducible is True
    assert (x**2 + x + 1).is_irreducible is True
    assert (x**2 + 2 * x + 1).is_irreducible is False
    assert ((x - 2 * y) * (x + y)).is_irreducible is False
    assert (x**2 + y**2).is_irreducible is True

    R, x, y, _ = ring('x y z', QQ)

    assert (x**2 + x + 1).is_irreducible
    assert (x**2 + 2 * x + 1).is_irreducible is False
Beispiel #7
0
def test_PolyElement_subresultants():
    R, x = ring('x', ZZ)

    for check in (True, False):
        with using(use_collins_resultant=check):
            assert R(0).resultant(R(0)) == 0
            assert R(0).resultant(R(0), includePRS=True) == (0, [])
            assert R(1).resultant(R(0)) == 0
            assert R(1).subresultants(R(0)) == [1]
            assert R(0).resultant(R(1)) == 0
            assert R(0).resultant(R(1), includePRS=True) == (0, [1])

            f = x**8 + x**6 - 3 * x**4 - 3 * x**3 + 8 * x**2 + 2 * x - 5
            g = 3 * x**6 + 5 * x**4 - 4 * x**2 - 9 * x + 21

            a = 15 * x**4 - 3 * x**2 + 9
            b = 65 * x**2 + 125 * x - 245
            c = 9326 * x - 12300
            d = R(260708)

            assert f.subresultants(g) == [f, g, a, b, c, d]
            assert f.resultant(g) == d.drop(x)

            f = x**2 - 2 * x + 1
            g = x**2 - 1

            a = 2 * x - 2

            assert f.subresultants(g) == [f, g, a]
            assert f.resultant(g) == 0

            f = x**2 + 1
            g = x**2 - 1

            a = -2

            assert f.subresultants(g) == [f, g, a]
            assert f.resultant(g) == 4
            assert f.resultant(g,
                               includePRS=True) == (4,
                                                    [x**2 + 1, x**2 - 1, -2])

            f = x**2 - 1
            g = x**3 - x**2 + 2

            assert f.resultant(g) == 0

            f = 3 * x**3 - x
            g = 5 * x**2 + 1

            assert f.resultant(g) == 64

            f = x**2 - 2 * x + 7
            g = x**3 - x + 5

            assert f.resultant(g) == 265

            f = x**3 - 6 * x**2 + 11 * x - 6
            g = x**3 - 15 * x**2 + 74 * x - 120

            assert f.resultant(g) == -8640

            f = x**3 - 6 * x**2 + 11 * x - 6
            g = x**3 - 10 * x**2 + 29 * x - 20

            assert f.resultant(g) == 0

            f = x**3 - 1
            g = x**3 + 2 * x**2 + 2 * x - 1

            assert f.resultant(g) == 16

            f = x**8 - 2
            g = x - 1

            assert f.resultant(g) == -1

            # issue sympy/sympy#10666
            f = x**3 - 7 * x + 7
            g = x

            assert f.resultant(g) == -g.resultant(f) == -7

    Rt, t = ring('t', ZZ)
    _, x = ring('x', Rt)

    f = x**6 - 5 * x**4 + 5 * x**2 + 4
    g = -6 * t * x**5 + x**4 + 20 * t * x**3 - 3 * x**2 - 10 * t * x + 6

    assert f.resultant(
        g) == 2930944 * t**6 + 2198208 * t**4 + 549552 * t**2 + 45796
    assert (x - 1).resultant(x + 1, includePRS=True) == (2, [x - 1, x + 1, 2])

    R, x, y = ring('x y', ZZ)

    for check in (True, False):
        with using(use_collins_resultant=check):
            assert R(0).resultant(R(0)) == 0
            assert R(0).resultant(R(0), includePRS=True) == (0, [])

            assert R(0).resultant(R(1)) == 0
            assert R(1).resultant(R(0)) == 0
            assert R(1).subresultants(R(0)) == [1]
            assert R(0).resultant(R(1), includePRS=True) == (0, [1])

            f = x + y + 2
            g = 2 * x * y + x + 3

            assert f.resultant(g) == (-2 * y**2 - 5 * y + 1).drop(x)

            f = 3 * x**2 * y - y**3 - 4
            g = x**2 + x * y**3 - 9

            a = 3 * x * y**4 + y**3 - 27 * y + 4
            b = (-3 * y**10 - 12 * y**7 + y**6 - 54 * y**4 + 8 * y**3 +
                 729 * y**2 - 216 * y + 16)

            r = b.drop(x)
            rr = (r, [
                3 * x**2 * y - y**3 - 4, x**2 + x * y**3 - 9,
                3 * x * y**4 + y**3 - 27 * y + 4, -3 * y**10 - 12 * y**7 +
                y**6 - 54 * y**4 + 8 * y**3 + 729 * y**2 - 216 * y + 16
            ])

            assert f.subresultants(g) == [f, g, a, b]

            assert f.resultant(g) == r
            assert f.resultant(g, includePRS=True) == rr

            f = -x**3 + 5
            g = 3 * x**2 * y + x**2

            a = 45 * y**2 + 30 * y + 5
            b = 675 * y**3 + 675 * y**2 + 225 * y + 25

            r = b.drop(x)

            assert f.subresultants(g) == [f, g, a]
            assert f.resultant(g) == r
            assert f.resultant(g, includePRS=True)[0] == r

            f = x + y
            g = x**2 - x * y + 1

            assert f.resultant(g) == (1 + 2 * y**2).drop(x)

            g += 1

            assert f.resultant(g) == (2 + 2 * y**2).drop(x)

    R, x, y = ring('x y', QQ)

    for check in (True, False):
        with using(use_collins_resultant=check):
            assert R(0).resultant(R(0)) == 0
            assert R(0).resultant(R(1)) == 0

            f = x + y
            g = x**2 - x * y + 1

            assert f.resultant(g) == (1 + 2 * y**2).drop(x)

            f = x / 2 + y + QQ(2, 3)
            g = 2 * x * y + x + 3

            assert f.resultant(g) == (-2 * y**2 - 7 * y / 3 + QQ(5, 6)).drop(x)

            f = 3 * x**2 * y - y**3 - 4
            g = x**2 + x * y**3 - 9

            assert f.resultant(g) == (-3 * y**10 - 12 * y**7 + y**6 -
                                      54 * y**4 + 8 * y**3 + 729 * y**2 -
                                      216 * y + 16).drop(x)

            f = -x**3 + 5
            g = 3 * x**2 * y + x**2

            assert f.resultant(g) == (675 * y**3 + 675 * y**2 + 225 * y +
                                      25).drop(x)

    R, x, y, z, u, v = ring('x y z u v', ZZ)

    for check in (True, False):
        with using(use_collins_resultant=check):
            f = 6 * x**2 - 3 * x * y - 2 * x * z + y * z
            g = x**2 - x * u - x * v + u * v

            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 - 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 - 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)

            assert f.resultant(g) == r.drop(x)

    R, x, y, z, u, v = ring('x y z u v', QQ)

    for check in (True, False):
        with using(use_collins_resultant=check):
            f = x**2 - x * y / 2 - x * z / 3 + y * z / 6
            g = x**2 - x * u - x * v + u * v

            r = (y**2 * z**2 / 36 - y**2 * z * u / 12 - y**2 * z * v / 12 +
                 y**2 * u * v / 4 - y * z**2 * u / 18 - y * z**2 * v / 18 +
                 y * z * u**2 / 6 + y * z * u * v / 3 + y * z * v**2 / 6 -
                 y * u**2 * v / 2 - y * u * v**2 / 2 + z**2 * u * v / 9 -
                 z * u**2 * v / 3 - z * u * v**2 / 3 + u**2 * v**2)

            assert f.resultant(g) == r.drop(x)
def test__isolate_real_roots_sqf():
    R, x = ring('x', ZZ)

    assert R._isolate_real_roots_sqf(R(0)) == []
    assert R._isolate_real_roots_sqf(R(5)) == []

    assert R._isolate_real_roots_sqf(x) == [(0, 0)]

    f = x * (x + 1)

    assert R._isolate_real_roots_sqf(f) == [(-1, -1), (0, 0)]
    assert R._isolate_real_roots_sqf(f, inf=+1) == []
    assert R._isolate_real_roots_sqf(f, sup=-1) == [(-1, -1)]
    assert R._isolate_real_roots_sqf(f, sup=-2) == []

    f = x * (x - 1)

    assert R._isolate_real_roots_sqf(f) == [(0, 0), (1, 1)]

    assert R._isolate_real_roots_sqf(x**4 + x + 1) == []

    i = [(-2, -1), (1, 2)]
    f = x**2 - 2

    assert R._isolate_real_roots_sqf(+f) == i
    assert R._isolate_real_roots_sqf(-f) == i

    for r in range(2, 7):
        for s in (1, 10, -1, -10):
            f = R(prod(x - s * _ for _ in range(1, r)))
            ans = sorted((s * _, s * _) for _ in range(1, r))
            assert R._isolate_real_roots_sqf(f) == ans

    assert R._isolate_real_roots_sqf(x**2 - 5) == [(-3, -2), (2, 3)]
    assert R._isolate_real_roots_sqf(x**3 - 5) == [(1, 2)]
    assert R._isolate_real_roots_sqf(x**4 - 5) == [(-2, -1), (1, 2)]
    assert R._isolate_real_roots_sqf(x**5 - 5) == [(1, 2)]
    assert R._isolate_real_roots_sqf(x**6 - 5) == [(-2, -1), (1, 2)]
    assert R._isolate_real_roots_sqf(x**7 - 5) == [(1, 2)]
    assert R._isolate_real_roots_sqf(x**8 - 5) == [(-2, -1), (1, 2)]
    assert R._isolate_real_roots_sqf(x**9 - 5) == [(1, 2)]

    for roots in subsets(range(1, 4)):
        f = R(prod(x - r for r in roots))
        ans = sorted((_, _) for _ in roots)
        assert R._isolate_real_roots_sqf(f) == ans

    assert R._isolate_real_roots_sqf((x - 3)*(x - 2)*(x - 1)*(x + 1)*(x + 2)*(x + 3)*(2*x + 1)) == \
        [(-3, -3), (-2, -2), (-1, -1), (-1, 0), (1, 1), (2, 2), (3, 3)]
    assert R._isolate_real_roots_sqf((x - 3)*(x - 2)*(x - 1)*(x + 1)*(x + 2)*(x + 3)*(2*x - 1)*(2*x + 1)) == \
        [(-3, -3), (-2, -2), (-1, -1), (-1, 0), (0, 1), (1, 1), (2, 2), (3, 3)]

    f = 9 * x**2 - 2

    assert R._isolate_real_roots_sqf(f) == \
        [(-1, 0), (0, 1)]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 10)) == \
        [(QQ(-1, 2), QQ(-3, 7)), (QQ(3, 7), QQ(1, 2))]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 100)) == \
        [(QQ(-9, 19), QQ(-8, 17)), (QQ(8, 17), QQ(9, 19))]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 1000)) == \
        [(QQ(-33, 70), QQ(-8, 17)), (QQ(8, 17), QQ(33, 70))]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 10000)) == \
        [(QQ(-33, 70), QQ(-107, 227)), (QQ(107, 227), QQ(33, 70))]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 100000)) == \
        [(QQ(-305, 647), QQ(-272, 577)), (QQ(272, 577), QQ(305, 647))]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 1000000)) == \
        [(QQ(-1121, 2378), QQ(-272, 577)), (QQ(272, 577), QQ(1121, 2378))]

    f = (x - 2) * (x - 1) * (2 * x - 1) * (10002 * x - 1) * (10003 * x - 1)

    assert R._isolate_real_roots_sqf(f) == \
        [(QQ(15, 150046), QQ(47, 470110)), (QQ(47, 470110), QQ(17, 170018)),
         (QQ(1, 2), QQ(1, 2)), (1, 1), (2, 2)]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 100000000000)) == \
        [(QQ(1, 10003), QQ(1, 10003)), (QQ(1, 10002), QQ(1, 10002)),
         (QQ(1, 2), QQ(1, 2)), (1, 1), (2, 2)]

    a, b, c, d = 10000090000001, 2000100003, 10000300007, 10000005000008
    f = (x - d) * (x + a) * (b * x + 1) * (c * x - 1)

    assert R._isolate_real_roots_sqf(f) == \
        [(-13194139533313, -8796093022209), (-1, 0), (0, 1),
         (8796093022209, 13194139533313)]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 100000000000)) == \
        [(-a, -a), (QQ(-7, 13958643719), QQ(-1, 2013265921)),
         (QQ(3, 30064771075), QQ(1, 9663676417)),
         (QQ(1328823874562668133568119, 132882321015),
          QQ(37336367728494399224248237, 3733634906029))]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 100000000000000000000000000000)) == \
        [(-a, -a), (-QQ(1, b), -QQ(1, b)), (QQ(1, c), QQ(1, c)), (d, d)]

    f = -2 * (x - 2) * (x + 2) * (5 * x**2 - 4 * x - 20)

    assert R._isolate_real_roots_sqf(f) == \
        [(-2, -2), (-2, -1), (2, 2), (2, 3)]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 100)) == \
        [(-2, -2), (-QQ(23, 14), -QQ(18, 11)), (2, 2), (QQ(39, 16), QQ(22, 9))]

    f = x - 1

    assert R._isolate_real_roots_sqf(f, inf=2) == []
    assert R._isolate_real_roots_sqf(f, sup=0) == []
    assert R._isolate_real_roots_sqf(f) == [(1, 1)]
    assert R._isolate_real_roots_sqf(f, inf=1) == [(1, 1)]
    assert R._isolate_real_roots_sqf(f, sup=1) == [(1, 1)]
    assert R._isolate_real_roots_sqf(f, inf=1, sup=1) == [(1, 1)]

    f = x**2 - 2

    assert R._isolate_real_roots_sqf(f, inf=QQ(7, 4)) == []
    assert R._isolate_real_roots_sqf(f, inf=QQ(7, 5)) == [(QQ(7, 5), QQ(3, 2))]
    assert R._isolate_real_roots_sqf(f, sup=QQ(7, 5)) == [(-2, -1)]
    assert R._isolate_real_roots_sqf(f, sup=QQ(7, 4)) == [(-2, -1),
                                                          (1, QQ(3, 2))]
    assert R._isolate_real_roots_sqf(f, sup=-QQ(7, 4)) == []
    assert R._isolate_real_roots_sqf(f,
                                     sup=-QQ(7, 5)) == [(-QQ(3, 2), -QQ(7, 5))]
    assert R._isolate_real_roots_sqf(f, inf=-QQ(7, 5)) == [(1, 2)]
    assert R._isolate_real_roots_sqf(f, inf=-QQ(7, 4)) == [(-QQ(3, 2), -1),
                                                           (1, 2)]

    i = [(-2, -1), (1, 2)]

    assert R._isolate_real_roots_sqf(f, inf=-2) == i
    assert R._isolate_real_roots_sqf(f, sup=+2) == i
    assert R._isolate_real_roots_sqf(f, inf=-2, sup=2) == i
    assert R._isolate_real_roots_sqf(f, inf=+1) == [i[1]]
    assert R._isolate_real_roots_sqf(f, sup=-1) == [i[0]]

    f = (2 * x - 3) * (x**2 - 3) * (x**2 - 2)

    assert R._isolate_real_roots_sqf(f) == \
        [(-2, -QQ(3, 2)), (-QQ(3, 2), -QQ(1, 1)), (1, QQ(3, 2)),
         (QQ(3, 2), QQ(3, 2)), (QQ(3, 2), 2)]

    f = 7 * x**4 - 19 * x**3 + 20 * x**2 + 17 * x + 20

    assert R._isolate_real_roots_sqf(f) == []

    R, x = ring('x', QQ)

    f = (6 * x - 85) * (1028 * x + 1) / 3855

    assert R._isolate_real_roots_sqf(f) == [(-1, 0), (14, 15)]
    assert [_.as_tuple()
            for _ in R._isolate_real_roots_sqf(f, blackbox=True)] == [(-1, 0),
                                                                      (14, 15)]

    f = (2 * x / 5 - QQ(17, 3)) * (4 * x + QQ(1, 257))

    assert R._isolate_real_roots_sqf(f) == [(-1, 0), (14, 15)]

    R, x = ring('x', EX)

    pytest.raises(DomainError, lambda: R._isolate_real_roots_sqf(x + 3))

    R, x = ring('x', QQ.algebraic_field(I))

    f = (x - 1) * (x**3 + I * x - 2)

    assert R._isolate_real_roots_sqf(f) == [(1, 1)]
    assert R._isolate_real_roots_sqf(f, sup=0) == []

    f = (x**2 - 2) * (x**3 - x + I)

    assert R._isolate_real_roots_sqf(f) == [(QQ(-3, 2), QQ(-4, 3)),
                                            (QQ(4, 3), QQ(3, 2))]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 10),
                                     inf=0) == [(QQ(7, 5), QQ(10, 7))]

    assert R._isolate_real_roots_sqf(x) == [(0, 0)]
    assert R._isolate_real_roots_sqf(x - 1) == [(1, 1)]
    assert R._isolate_real_roots_sqf(x - I) == []

    f = (x + I) * (x - 1)

    assert [_.as_tuple()
            for _ in R._isolate_real_roots_sqf(f, blackbox=True)] == [(1, 1)]

    R, x = ring('x', QQ.algebraic_field(sqrt(2)))

    f = (-x**3 + sqrt(2) * x - 1) * (x**2 + 1)

    assert R._isolate_real_roots_sqf(f) == [(-2, -1)]
    assert R._isolate_real_roots_sqf(f, eps=QQ(1, 1000)) == [(QQ(-132, 91),
                                                              QQ(-29, 20))]

    f = (x - sqrt(2)) * (x + 2 * sqrt(2)) * (x - 7 + sqrt(2)) * (
        x + 3 * sqrt(2)) * (x - 1) * (x + 1 - sqrt(2))

    assert R._isolate_real_roots_sqf(f) == [(-5, -4), (-3, -2), (0, 1), (1, 1),
                                            (1, 2), (5, 6)]

    R, x = ring('x', QQ.algebraic_field(sqrt(2), sqrt(3)))

    f = (x - sqrt(2)) * (x - sqrt(3)) * (x - 2 * sqrt(6)) * (x - sqrt(6)) * (
        x**2 + 2)

    assert R._isolate_real_roots_sqf(f) == [(1, QQ(3, 2)), (QQ(3, 2), 2),
                                            (2, 3), (4, 5)]
    assert (R._isolate_real_roots_sqf(f, eps=QQ(1, 1000)) == [
        (QQ(41, 29), QQ(58, 41)), (QQ(71, 41), QQ(97, 56)),
        (QQ(218, 89), QQ(49, 20)), (QQ(436, 89), QQ(485, 99))
    ])
def test__isolate_real_roots():
    R, x = ring('x', ZZ)

    assert R._isolate_real_roots(R(0)) == []
    assert R._isolate_real_roots(R(1)) == []
    assert R._isolate_real_roots(R(3)) == []

    assert R._isolate_real_roots(x) == [((0, 0), 1)]
    assert R._isolate_real_roots(5 * x) == [((0, 0), 1)]
    assert R._isolate_real_roots(7 * x**4) == [((0, 0), 4)]
    assert R._isolate_real_roots(x**128) == [((0, 0), 128)]

    assert R._isolate_real_roots(x * (x + 1)) == [((-1, -1), 1), ((0, 0), 1)]
    assert R._isolate_real_roots(x * (x - 1)) == [((0, 0), 1), ((1, 1), 1)]

    assert R._isolate_real_roots(x**4 + x + 1) == []

    i = [((-2, -1), 1), ((1, 2), 1)]

    assert R._isolate_real_roots(+x**2 - 2) == i
    assert R._isolate_real_roots(-x**2 + 2) == i

    f = (2 * x - 3)**4 * (x**2 - 3)**2 * (x**2 - 2)**3

    assert R._isolate_real_roots(f) == \
        [((-2, -QQ(3, 2)), 2), ((-QQ(3, 2), -QQ(1, 1)), 3), ((1, QQ(3, 2)), 3),
         ((QQ(3, 2), QQ(3, 2)), 4), ((QQ(5, 3), 2), 2)]

    f = (2 * x - 3) * (x**2 - 3) * (x**2 - 2)

    assert R._isolate_real_roots(f) == \
        [((-2, -QQ(3, 2)), 1), ((-QQ(3, 2), -QQ(1, 1)), 1), ((1, QQ(3, 2)), 1),
         ((QQ(3, 2), QQ(3, 2)), 1), ((QQ(3, 2), 2), 1)]

    f = x - 1

    assert R._isolate_real_roots(f, inf=2) == []
    assert R._isolate_real_roots(f, sup=0) == []
    assert R._isolate_real_roots(f) == [((1, 1), 1)]
    assert R._isolate_real_roots(f, inf=1) == [((1, 1), 1)]
    assert R._isolate_real_roots(f, sup=1) == [((1, 1), 1)]
    assert R._isolate_real_roots(f, inf=1, sup=1) == [((1, 1), 1)]

    f = (x**2 - 2)**2

    assert R._isolate_real_roots(f, inf=QQ(7, 4)) == []
    assert R._isolate_real_roots(f, inf=QQ(7, 5)) == [((QQ(7, 5), QQ(3,
                                                                     2)), 2)]
    assert R._isolate_real_roots(f, sup=QQ(7, 5)) == [((-2, -1), 2)]
    assert R._isolate_real_roots(f, sup=QQ(7, 4)) == [((-2, -1), 2),
                                                      ((1, QQ(3, 2)), 2)]
    assert R._isolate_real_roots(f, sup=-QQ(7, 4)) == []
    assert R._isolate_real_roots(f, sup=-QQ(7, 5)) == [((-QQ(3, 2), -QQ(7, 5)),
                                                        2)]
    assert R._isolate_real_roots(f, inf=-QQ(7, 5)) == [((1, 2), 2)]
    assert R._isolate_real_roots(f, inf=-QQ(7, 4)) == [((-QQ(3, 2), -1), 2),
                                                       ((1, 2), 2)]

    i = [((-2, -1), 2), ((1, 2), 2)]

    assert R._isolate_real_roots(f, inf=-2) == i
    assert R._isolate_real_roots(f, sup=+2) == i
    assert R._isolate_real_roots(f, inf=-2, sup=2) == i

    f = x**4 * (x - 1)**3 * (x**2 - 2)**2

    assert R._isolate_real_roots(f) == \
        [((-2, -1), 2), ((0, 0), 4), ((1, 1), 3), ((1, 2), 2)]

    f = x**45 - 45 * x**44 + 990 * x**43 - 1
    g = (x**46 - 15180 * x**43 + 9366819 * x**40 - 53524680 * x**39 +
         260932815 * x**38 - 1101716330 * x**37 + 4076350421 * x**36 -
         13340783196 * x**35 + 38910617655 * x**34 - 101766230790 * x**33 +
         239877544005 * x**32 - 511738760544 * x**31 + 991493848554 * x**30 -
         1749695026860 * x**29 + 2818953098830 * x**28 -
         4154246671960 * x**27 + 5608233007146 * x**26 -
         6943526580276 * x**25 + 7890371113950 * x**24 -
         8233430727600 * x**23 + 7890371113950 * x**22 -
         6943526580276 * x**21 + 5608233007146 * x**20 -
         4154246671960 * x**19 + 2818953098830 * x**18 -
         1749695026860 * x**17 + 991493848554 * x**16 - 511738760544 * x**15 +
         239877544005 * x**14 - 101766230790 * x**13 + 38910617655 * x**12 -
         13340783196 * x**11 + 4076350421 * x**10 - 1101716330 * x**9 +
         260932815 * x**8 - 53524680 * x**7 + 9366819 * x**6 - 1370754 * x**5 +
         163185 * x**4 - 15180 * x**3 + 1035 * x**2 - 47 * x + 1)

    assert R._isolate_real_roots(f*g) == \
        [((0, QQ(1, 2)), 1), ((QQ(2, 3), QQ(3, 4)), 1), ((QQ(3, 4), 1), 1), ((6, 7), 1), ((24, 25), 1)]

    f = x**2 - 3

    assert R._isolate_real_roots(f) == [((-2, -1), 1), ((1, 2), 1)]
    assert R._isolate_real_roots(f, eps=QQ(1, 100)) == [
        ((QQ(-26, 15), QQ(-19, 11)), 1), ((QQ(19, 11), QQ(26, 15)), 1)
    ]

    f = x**4 - 4 * x**2 + 4

    assert R._isolate_real_roots(f, inf=QQ(7, 4)) == []
    assert R._isolate_real_roots(f, inf=QQ(7, 5)) == [((QQ(7, 5), QQ(3,
                                                                     2)), 2)]
    assert R._isolate_real_roots(f, sup=QQ(7, 4)) == [((-2, -1), 2),
                                                      ((1, QQ(3, 2)), 2)]
    assert R._isolate_real_roots(f, sup=QQ(7, 5)) == [((-2, -1), 2)]

    f = (x**2 - 2) * (x**2 - 3)**7 * (x + 1) * (7 * x + 3)**3

    assert R._isolate_real_roots(f) == [((-2, -QQ(3, 2)), 7),
                                        ((-QQ(3, 2), -1), 1), ((-1, -1), 1),
                                        ((-1, 0), 3), ((1, QQ(3, 2)), 1),
                                        ((QQ(3, 2), 2), 7)]

    f = 7 * x**4 - 19 * x**3 + 20 * x**2 + 17 * x + 20

    assert R._isolate_real_roots(f) == []

    R, x = ring('x', QQ)

    f = (2 * x / 5 - QQ(17, 3)) * (4 * x + QQ(1, 257))

    assert R._isolate_real_roots(f) == [((-1, 0), 1), ((14, 15), 1)]

    assert R._isolate_real_roots(f, eps=QQ(1, 10)) == [((-QQ(1, 513), 0), 1),
                                                       ((QQ(85, 6), QQ(85,
                                                                       6)), 1)]
    assert R._isolate_real_roots(f, eps=QQ(1, 100)) == [((-QQ(1, 513), 0), 1),
                                                        ((QQ(85,
                                                             6), QQ(85,
                                                                    6)), 1)]
    assert R._isolate_real_roots(f, eps=QQ(1,
                                           1000)) == [((-QQ(1, 1025), 0), 1),
                                                      ((QQ(85, 6), QQ(85,
                                                                      6)), 1)]
    assert R._isolate_real_roots(f, eps=QQ(1, 10000)) == [
        ((-QQ(1, 1025), -QQ(65, 66881)), 1), ((QQ(85, 6), QQ(85, 6)), 1)
    ]

    R, x = ring('x', EX)

    pytest.raises(DomainError, lambda: R._isolate_real_roots(x + 3))
    pytest.raises(DomainError, lambda: R._isolate_real_roots((x + 2) *
                                                             (x + 3)**2))

    R, x = ring('x', QQ.algebraic_field(I))

    f = (x**2 - I)**2 * (x - 2 * I)**3

    assert R._isolate_real_roots(f) == []  # issue diofant/diofant#789
    assert R._isolate_real_roots(f * (x - 1)**3) == [((1, 1), 3)]

    f = x**4 * (x - 1)**3 * (x**2 - 2)**2

    assert R._isolate_real_roots(f) == \
        [((-2, -1), 2), ((0, 0), 4), ((1, 1), 3), ((QQ(4, 3), QQ(3, 2)), 2)]
def test__step_refine_real_root():
    R, x = ring('x', ZZ)

    assert R._step_refine_real_root(x + 1,
                                    (-2, 0, 1, 1)) == (x + 2, (0, -2, 1, 2))
def test__refine_real_root():
    R, x = ring('x', ZZ)

    f = x**2 - 2

    assert R._refine_real_root(f, 1, 1, steps=1) == (1, 1)
    assert R._refine_real_root(f, 1, 1, steps=9) == (1, 1)

    pytest.raises(ValueError, lambda: R._refine_real_root(f, -2, 2))

    s, t = 1, 2

    assert R._refine_real_root(f, s, t, steps=0) == (1, 2)
    assert R._refine_real_root(f, s, t, steps=1) == (1, QQ(3, 2))
    assert R._refine_real_root(f, s, t, steps=2) == (QQ(4, 3), QQ(3, 2))
    assert R._refine_real_root(f, s, t, steps=3) == (QQ(7, 5), QQ(3, 2))
    assert R._refine_real_root(f, s, t, steps=4) == (QQ(7, 5), QQ(10, 7))
    assert R._refine_real_root(f, s, t, eps=QQ(1, 100)) == (QQ(24,
                                                               17), QQ(17, 12))

    s, t = 1, QQ(3, 2)

    assert R._refine_real_root(f, s, t, steps=0) == (1, QQ(3, 2))
    assert R._refine_real_root(f, s, t, steps=1) == (QQ(4, 3), QQ(3, 2))
    assert R._refine_real_root(f, s, t, steps=2) == (QQ(7, 5), QQ(3, 2))
    assert R._refine_real_root(f, s, t, steps=3) == (QQ(7, 5), QQ(10, 7))
    assert R._refine_real_root(f, s, t, steps=4) == (QQ(7, 5), QQ(17, 12))

    s, t = 1, QQ(5, 3)

    assert R._refine_real_root(f, s, t, steps=0) == (1, QQ(5, 3))
    assert R._refine_real_root(f, s, t, steps=1) == (1, QQ(3, 2))
    assert R._refine_real_root(f, s, t, steps=2) == (QQ(7, 5), QQ(3, 2))
    assert R._refine_real_root(f, s, t, steps=3) == (QQ(7, 5), QQ(13, 9))
    assert R._refine_real_root(f, s, t, steps=4) == (QQ(7, 5), QQ(27, 19))

    s, t = -1, -2

    assert R._refine_real_root(f, s, t, steps=0) == (-2, -1)
    assert R._refine_real_root(f, s, t, steps=1) == (-QQ(3, 2), -1)
    assert R._refine_real_root(f, s, t, steps=2) == (-QQ(3, 2), -QQ(4, 3))
    assert R._refine_real_root(f, s, t, steps=3) == (-QQ(3, 2), -QQ(7, 5))
    assert R._refine_real_root(f, s, t, steps=4) == (-QQ(10, 7), -QQ(7, 5))

    pytest.raises(RefinementFailed, lambda: R._refine_real_root(f, 0, 1))

    s, t, u, v, w = 1, 2, QQ(24, 17), QQ(17, 12), QQ(7, 5)

    assert R._refine_real_root(f, s, t, eps=QQ(1, 100)) == (u, v)
    assert R._refine_real_root(f, s, t, steps=6) == (u, v)

    assert R._refine_real_root(f, s, t, eps=QQ(1, 100), steps=5) == (w, v)
    assert R._refine_real_root(f, s, t, eps=QQ(1, 100), steps=6) == (u, v)
    assert R._refine_real_root(f, s, t, eps=QQ(1, 100), steps=7) == (u, v)

    s, t, u, v = -2, -1, QQ(-3, 2), QQ(-4, 3)

    assert R._refine_real_root(f, s, t, disjoint=-5) == (s, t)
    assert R._refine_real_root(f, s, t, disjoint=-v) == (s, t)
    assert R._refine_real_root(f, s, t, disjoint=v) == (u, v)

    s, t, u, v = 1, 2, QQ(4, 3), QQ(3, 2)

    assert R._refine_real_root(f, s, t, disjoint=5) == (s, t)
    assert R._refine_real_root(f, s, t, disjoint=-u) == (s, t)
    assert R._refine_real_root(f, s, t, disjoint=u) == (u, v)

    f = x**2 - 3

    assert R._refine_real_root(f, 1, 2, eps=QQ(1, 100)) == (QQ(19,
                                                               11), QQ(26, 15))

    R, x = ring('x', QQ)

    f = (x - QQ(1, 2)) * (x + QQ(1, 2))

    assert R._refine_real_root(f, 0, 1, steps=1) == (QQ(1, 2), QQ(1, 2))

    D, y = ring('y', ZZ)
    R, x = ring('x', D)

    f = x**2 + y * x - 1

    pytest.raises(DomainError, lambda: R._refine_real_root(f, 0, 1))
def test_diofantissue_745():
    D, y = ring('y', ZZ)
    R, x = ring('x', D)

    pytest.raises(DomainError, lambda: R._count_real_roots(x**7 + y * x + 1))
def test_RealInterval():
    R, x = ring('x', ZZ)

    f = (x - 1)**2

    pytest.raises(ValueError, lambda: RealInterval((-2, 1), f))
def test__isolate_complex_roots_sqf():
    R, x = ring('x', ZZ)

    f = x**2 - 2 * x + 3

    assert R._isolate_complex_roots_sqf(f) == \
        [((0, -6), (6, 0)), ((0, 0), (6, 6))]
    assert [r.as_tuple() for r in R._isolate_complex_roots_sqf(f, blackbox=True)] == \
        [((0, -6), (6, 0)), ((0, 0), (6, 6))]

    assert R._isolate_complex_roots_sqf(f, inf=1, sup=3) == [((1, -3), (3, 0)),
                                                             ((1, 0), (3, 3))]
    assert R._isolate_complex_roots_sqf(f, inf=(1, 0),
                                        sup=3) == [((1, 0), (3, 3))]
    assert R._isolate_complex_roots_sqf(f, inf=(1, QQ(-1, 2)),
                                        sup=3) == [((1, 0), (3, 3))]
    assert R._isolate_complex_roots_sqf(f, inf=(1, -3),
                                        sup=(3, -1)) == [((1, -3), (3, -1))]
    assert R._isolate_complex_roots_sqf(f, inf=0, sup=QQ(1, 6)) == []

    assert R._isolate_complex_roots_sqf(R.zero) == []

    pytest.raises(ValueError,
                  lambda: R._isolate_complex_roots_sqf(f, inf=1, sup=1))

    assert R._isolate_complex_roots_sqf(f, eps=QQ(1, 10)) == \
        [((QQ(15, 16), -QQ(3, 2)), (QQ(33, 32), -QQ(45, 32))),
         ((QQ(15, 16), QQ(45, 32)), (QQ(33, 32), QQ(3, 2)))]
    assert R._isolate_complex_roots_sqf(f, eps=QQ(1, 100)) == \
        [((QQ(255, 256), -QQ(363, 256)), (QQ(513, 512), -QQ(723, 512))),
         ((QQ(255, 256), QQ(723, 512)), (QQ(513, 512), QQ(363, 256)))]

    f = 7 * x**4 - 19 * x**3 + 20 * x**2 + 17 * x + 20

    assert R._isolate_complex_roots_sqf(f) == \
        [((-QQ(40, 7), -QQ(40, 7)), (0, 0)), ((-QQ(40, 7), 0), (0, QQ(40, 7))),
         ((0, -QQ(40, 7)), (QQ(40, 7), 0)), ((0, 0), (QQ(40, 7), QQ(40, 7)))]
    assert R._isolate_complex_roots_sqf(f, eps=QQ(1, 10)) == \
        [((QQ(-25, 56), QQ(-5, 8)), (QQ(-5, 14), QQ(-15, 28))),
         ((QQ(-25, 56), QQ(15, 28)), (QQ(-5, 14), QQ(5, 8))),
         ((QQ(95, 56), QQ(-85, 56)), (QQ(25, 14), QQ(-10, 7))),
         ((QQ(95, 56), QQ(10, 7)), (QQ(25, 14), QQ(85, 56)))]

    R, x = ring('x', QQ)

    f = x**2 / 2 - 3 * x / 7 + 1

    assert R._isolate_complex_roots_sqf(f) == [((0, -4), (4, 0)),
                                               ((0, 0), (4, 4))]

    R, x = ring('x', EX)

    pytest.raises(
        DomainError,
        lambda: R._isolate_complex_roots_sqf(x, inf=(-1, 0), sup=(1, 1)))

    R, x = ring('x', QQ.algebraic_field(I))

    f = x**4 + I * x**3 - x + 1

    assert R._isolate_complex_roots_sqf(f, inf=(0, 0),
                                        sup=(1, 1)) == [((0, 0), (1, QQ(1,
                                                                        2)))]
    assert R._isolate_complex_roots_sqf(f,
                                        inf=(0, 0),
                                        sup=(1, 1),
                                        eps=QQ(1, 100)) == [
                                            ((QQ(79, 128), QQ(19, 64)),
                                             (QQ(5, 8), QQ(39, 128)))
                                        ]
    assert R._isolate_complex_roots_sqf(f, inf=(0, -1), sup=(1, 1)) == [
        ((0, -1), (1, QQ(-1, 2))), ((0, 0), (1, QQ(1, 2)))
    ]
    assert R._isolate_complex_roots_sqf(f,
                                        inf=(0, -1),
                                        sup=(1, 1),
                                        eps=QQ(1, 100)) == [
                                            ((QQ(79, 128), QQ(19, 64)),
                                             (QQ(5, 8), QQ(39, 128))),
                                            ((QQ(45, 64), QQ(-91, 128)),
                                             (QQ(91, 128), QQ(-45, 64)))
                                        ]

    f *= (x - 1)

    assert R._isolate_complex_roots_sqf(f) == [
        ((QQ(-401, 100), QQ(-401, 100)), (0, 0)),
        ((QQ(-401, 100), 0), (0, QQ(401, 100))),
        ((0, QQ(-401, 100)), (QQ(401, 100), 0)),
        ((0, 0), (QQ(401, 100), QQ(401, 100)))
    ]

    f = x**7 + I * x**4 - (2 + I) * x**3 - 3 * x + 5

    assert R._isolate_complex_roots_sqf(f) == [
        ((QQ(-1001, 100), 0), (0, QQ(1001, 100))),
        ((QQ(-1001, 400), QQ(-1001, 800)), (QQ(-1001, 800), 0)),
        ((QQ(-1001, 800), QQ(-1001, 800)), (0, 0)),
        ((0, QQ(-1001, 400)), (QQ(1001, 400), QQ(-1001, 800))),
        ((0, QQ(-1001, 800)), (QQ(1001, 400), 0)),
        ((0, 0), (QQ(1001, 400), QQ(1001, 800))),
        ((0, QQ(1001, 800)), (QQ(1001, 400), QQ(1001, 400)))
    ]

    R, x = ring('x', QQ.algebraic_field(sqrt(2)))

    f = -x**3 + sqrt(2) * x - 1

    assert R._isolate_complex_roots_sqf(f) == [
        ((0, QQ(-283, 100)), (QQ(283, 100), 0)),
        ((0, 0), (QQ(283, 100), QQ(283, 100)))
    ]

    R, x = ring('x', QQ.algebraic_field(sqrt(2)).algebraic_field(I))

    f = -x**3 + I * x**2 + sqrt(2) * x - 1

    assert R._isolate_complex_roots_sqf(f) == [
        ((QQ(-283, 100), 0), (0, QQ(283, 100))),
        ((0, QQ(-283, 100)), (QQ(283, 100), 0)),
        ((0, 0), (QQ(283, 100), QQ(283, 100)))
    ]

    R, x = ring('x', EX)

    pytest.raises(DomainError, lambda: R._isolate_complex_roots_sqf(x))
Beispiel #15
0
def test_factor_list():
    R, x = ring('x', FF(2))

    assert (x**2 + 1).factor_list() == (1, [(x + 1, 2)])

    R, x = ring('x', ZZ)

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])

    assert (x**2 + 2 * x + 1).factor_list() == (1, [(x + 1, 2)])

    # issue sympy/sympy#8037
    assert (6 * x**2 - 5 * x - 6).factor_list() == (1, [(2 * x - 3, 1),
                                                        (3 * x + 2, 1)])

    R, x = ring('x', QQ)

    assert R(0).factor_list() == (0, [])
    assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), [])

    assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)])

    R, x = ring('x', QQ.algebraic_field(I))

    f = x**4 + 2 * x**2

    assert f.factor_list() == (1, [(x, 2), (x**2 + 2, 1)])

    R, x = ring('x', RR)

    assert (1.0 * x**2 + 2.0 * x + 1.0).factor_list() == (1.0, [(1.0 * x + 1.0,
                                                                 2)])
    assert (2.0 * x**2 + 4.0 * x + 2.0).factor_list() == (2.0, [(1.0 * x + 1.0,
                                                                 2)])

    f = 6.7225336055071 * x**2 - 10.6463972754741 * x - 0.33469524022264

    assert f.factor_list() == (1.0, [(f, 1)])

    # issue diofant/diofant#238
    f = 0.1 * x**2 + 1.1 * x + 1.0

    assert f.factor_list() == (10.0, [(0.1 * x + 0.1, 1), (0.1 * x + 1.0, 1)])

    f = 0.25 + 1.0 * x + 1.0 * x**2

    assert f.factor_list() == (4.0, [(0.25 + 0.5 * x, 2)])

    Rt, t = ring('t', ZZ)
    R, x = ring('x', Rt)

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])

    assert (4 * t * x**2 + 4 * t**2 * x).factor_list() == (4 * t, [(x, 1),
                                                                   (x + t, 1)])

    Rt, t = ring('t', QQ)
    R, x = ring('x', Rt)

    assert R(0).factor_list() == (0, [])
    assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), [])

    assert (t * x**2 / 2 + t**2 * x / 2).factor_list() == (t / 2, [(x, 1),
                                                                   (x + t, 1)])

    R, x = ring('x', EX)

    pytest.raises(DomainError, R(EX(sin(1))).factor_list)

    R, x, y = ring('x y', FF(2))

    pytest.raises(NotImplementedError, (x**2 + y**2).factor_list)

    R, x, y = ring('x y', ZZ)

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])

    assert (x**2 + 2 * x + 1).factor_list() == (1, [(x + 1, 2)])
    assert (4 * x**2 * y + 4 * x * y**2).factor_list() == (4, [(y, 1), (x, 1),
                                                               (x + y, 1)])

    R, x, y = ring('x y', QQ)

    assert R(0).factor_list() == (0, [])
    assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), [])

    assert (x**2 / 2 + x + QQ(1, 2)).factor_list() == (QQ(1, 2), [(x + 1, 2)])
    assert (x**2 * y / 2 + x * y**2 / 2).factor_list() == (QQ(1,
                                                              2), [(y, 1),
                                                                   (x, 1),
                                                                   (x + y, 1)])

    R, x, y = ring('x y', QQ.algebraic_field(I))

    f, r = x**2 + y**2, (1, [(x - I * y, 1), (x + I * y, 1)])

    for method in ('trager', 'modular'):
        with using(aa_factor_method=method):
            assert f.factor_list() == r

    R, x, y = ring('x y', RR)

    f = 2.0 * x**2 - 8.0 * y**2

    assert f.factor_list() == (2.0, [(1.0 * x - 2.0 * y, 1),
                                     (1.0 * x + 2.0 * y, 1)])

    f = 6.7225336055071 * x**2 * y**2 - 10.6463972754741 * x * y - 0.33469524022264

    assert f.factor_list() == (1.0, [(f, 1)])

    Rt, t = ring('t', ZZ)
    R, x, y = ring('x y', Rt)

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])

    assert (4 * t * x**2 + 4 * t**2 * x).factor_list() == (4 * t, [(x, 1),
                                                                   (x + t, 1)])

    Rt, t = ring('t', QQ)
    R, x, y = ring('x y', Rt)

    assert R(0).factor_list() == (0, [])
    assert R(QQ(1, 7)).factor_list() == (QQ(1, 7), [])

    assert (t * x**2 / 2 + t**2 * x / 2).factor_list() == (t / 2, [(x, 1),
                                                                   (x + t, 1)])

    R, x, y = ring('x y', EX)

    pytest.raises(DomainError, lambda: R(EX(sin(1))).factor_list())

    # issue diofant/diofant#238
    R, x, y, z = ring('x y z', RR)

    f = x * y + x * z + 0.1 * y + 0.1 * z

    assert f.factor_list() == (10.0, [(x + 0.1, 1), (0.1 * y + 0.1 * z, 1)])

    f = 0.25 * x**2 + 1.0 * x * y * z + 1.0 * y**2 * z**2

    assert f.factor_list() == (4.0, [(0.25 * x + 0.5 * y * z, 2)])

    R, *X = ring('x:200', ZZ)

    f, g = X[0]**2 + 2 * X[0] + 1, X[0] + 1

    assert f.factor_list() == (1, [(g, 2)])

    f, g = X[-1]**2 + 2 * X[-1] + 1, X[-1] + 1

    assert f.factor_list() == (1, [(g, 2)])
def test__isolate_real_roots_pair():
    R, x = ring('x', ZZ)

    assert R._isolate_real_roots_pair(x*(x + 1), x) == \
        [((-1, -1), {0: 1}), ((0, 0), {0: 1, 1: 1})]
    assert R._isolate_real_roots_pair(x*(x - 1), x) == \
        [((0, 0), {0: 1, 1: 1}), ((1, 1), {0: 1})]

    f, g = (x**2 - 2)**2, x - 1

    assert R._isolate_real_roots_pair(f, g, inf=QQ(7, 4)) == []
    assert R._isolate_real_roots_pair(f, g, inf=QQ(7, 5)) == \
        [((QQ(7, 5), QQ(3, 2)), {0: 2})]
    assert R._isolate_real_roots_pair(f, g, sup=QQ(7, 5)) == \
        [((-2, -1), {0: 2}), ((1, 1), {1: 1})]
    assert R._isolate_real_roots_pair(f, g, sup=QQ(7, 4)) == \
        [((-2, -1), {0: 2}), ((1, 1), {1: 1}), ((1, QQ(3, 2)), {0: 2})]
    assert R._isolate_real_roots_pair(f, g, sup=-QQ(7, 4)) == []
    assert R._isolate_real_roots_pair(f, g, sup=-QQ(7, 5)) == \
        [((-QQ(3, 2), -QQ(7, 5)), {0: 2})]
    assert R._isolate_real_roots_pair(f, g, inf=-QQ(7, 5)) == \
        [((1, 1), {1: 1}), ((1, 2), {0: 2})]
    assert R._isolate_real_roots_pair(f, g, inf=-QQ(7, 4)) == \
        [((-QQ(3, 2), -1), {0: 2}), ((1, 1), {1: 1}), ((1, 2), {0: 2})]

    f, g = 2 * x**2 - 1, x**2 - 2

    assert R._isolate_real_roots_pair(f, g) == \
        [((-2, -1), {1: 1}), ((-1, 0), {0: 1}),
         ((0, 1), {0: 1}), ((1, 2), {1: 1})]
    assert R._isolate_real_roots_pair(f, g, strict=True) == \
        [((-QQ(3, 2), -QQ(4, 3)), {1: 1}), ((-1, -QQ(2, 3)), {0: 1}),
         ((QQ(2, 3), 1), {0: 1}), ((QQ(4, 3), QQ(3, 2)), {1: 1})]

    f, g = x**2 - 2, (x - 1) * (x**2 - 2)

    assert R._isolate_real_roots_pair(f, g) == \
        [((-2, -1), {1: 1, 0: 1}), ((1, 1), {1: 1}), ((1, 2), {1: 1, 0: 1})]

    f, g = x * (x**2 - 2), x**2 * (x - 1) * (x**2 - 2)

    assert R._isolate_real_roots_pair(f, g) == \
        [((-2, -1), {1: 1, 0: 1}), ((0, 0), {0: 1, 1: 2}),
         ((1, 1), {1: 1}), ((1, 2), {1: 1, 0: 1})]

    f, g = x**2 * (x - 1)**3 * (x**2 - 2)**2, x * (x - 1)**2 * (x**2 + 2)
    _x = R.clone(domain=ZZ.field).x

    assert R._isolate_real_roots_pair(f, g) == \
        [((-2, -1), {0: 2}), ((0, 0), {0: 2, 1: 1}),
         ((1, 1), {0: 3, 1: 2}), ((1, 2), {0: 2})]
    assert R._isolate_real_roots_pair(f, g, basis=True) == \
        [((-2, -1), {0: 2}, _x**2 - 2), ((0, 0), {0: 2, 1: 1}, _x),
         ((1, 1), {0: 3, 1: 2}, _x - 1), ((1, 2), {0: 2}, _x**2 - 2)]

    f, g = x, R.zero

    assert R._isolate_real_roots_pair(f, g) == \
        R._isolate_real_roots_pair(g, f) == [((0, 0), {0: 1, 1: 1})]

    f *= x**2

    assert R._isolate_real_roots_pair(f, g) == \
        R._isolate_real_roots_pair(g, f) == [((0, 0), {0: 3, 1: 3})]

    R, x = ring('x', EX)

    pytest.raises(DomainError, lambda: R._isolate_real_roots_pair(x, x + 3))

    R, x = ring('x', ZZ)

    f, g = x**5 - 200, x**5 - 201

    assert R._isolate_real_roots_pair(f, g) == \
        [((QQ(75, 26), QQ(101, 35)), {0: 1}), ((QQ(309, 107), QQ(26, 9)), {1: 1})]

    R, x = ring('x', QQ)

    f, g = -x**5 / 200 + 1, -x**5 / 201 + 1

    assert R._isolate_real_roots_pair(f, g) == \
        [((QQ(75, 26), QQ(101, 35)), {0: 1}), ((QQ(309, 107), QQ(26, 9)), {1: 1})]
Beispiel #17
0
def test_gf_factor():
    R, x = ring('x', FF(2))

    f = x**4 + x
    g = (1, [(x, 1), (x + 1, 1), (x**2 + x + 1, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**18 + x**17 + x**16 + x**14 + x**12 + x**11 + x**8 + x**5 + x**3 + 1
    g = (1, [(x + 1, 4), (x**4 + x**3 + 1, 1),
             (x**10 + x**8 + x**7 + x**5 + 1, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**63 + 1
    g = (1, [(x + 1, 1), (x**2 + x + 1, 1),
             (x**3 + x + 1, 1), (x**6 + x + 1, 1), (x**3 + x**2 + 1, 1),
             (x**6 + x**3 + 1, 1), (x**6 + x**5 + 1, 1),
             (x**6 + x**4 + x**2 + x + 1, 1), (x**6 + x**5 + x**2 + x + 1, 1),
             (x**6 + x**4 + x**3 + x + 1, 1), (x**6 + x**5 + x**4 + x + 1, 1),
             (x**6 + x**5 + x**3 + x**2 + 1, 1),
             (x**6 + x**5 + x**4 + x**2 + 1, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = (x**28 + x**27 + x**26 + x**25 + x**24 + x**20 + x**19 + x**17 +
         x**16 + x**15 + x**14 + x**13 + x**12 + x**11 + x**9 + x**8 + x**5 +
         x**4 + x**2 + x)
    g = (1, [(x, 1), (x + 1, 2), (x**5 + x**4 + x**3 + x + 1, 1),
             (x**10 + x**9 + x**8 + x**7 + 1, 1),
             (x**10 + x**9 + x**8 + x**5 + x**4 + x**2 + 1, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    R, x = ring('x', FF(3))

    f = x**6 - x**5 + x**4 + x**3 - x
    g = (1, [(x, 1), (x + 1, 1), (x**2 + 1, 1), (x**2 + x + 2, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**4 + x**3 + x + 2
    g = (1, [(x**2 + 1, 1), (x**2 + x + 2, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    R, x = ring('x', FF(11))

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert R(0).factor_list() == (0, [])
            assert R(1).factor_list() == (1, [])
            assert x.factor_list() == (1, [(x, 1)])
            assert (x + 1).factor_list() == (1, [(x + 1, 1)])
            assert (2 * x + 3).factor_list() == (2, [(x + 7, 1)])

    assert (5 * x**3 + 2 * x**2 + 7 * x + 2).factor_list() == (5, [(x + 2, 1),
                                                                   (x + 8, 2)])

    f = x**6 + 8 * x**5 + x**4 + 8 * x**3 + 10 * x**2 + 8 * x + 1
    g = (1, [(x + 1, 1), (x**2 + 5 * x + 3, 1),
             (x**3 + 2 * x**2 + 3 * x + 4, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**3 + 5 * x**2 + 8 * x + 4
    g = (1, [(x + 1, 1), (x + 2, 2)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**9 + x**8 + 10 * x**7 + x**6 + 10 * x**4 + 10 * x**3 + 10 * x**2
    g = (1, [(x, 2), (x**2 + 9 * x + 5, 1),
             (x**5 + 3 * x**4 + 8 * x**2 + 5 * x + 2, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**32 + 1
    g = (1, [(x**16 + 3 * x**8 + 10, 1), (x**16 + 8 * x**8 + 10, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = 8 * x**32 + 5
    g = (8, [(x + 3, 1), (x + 8, 1), (x**2 + 9, 1), (x**2 + 2 * x + 2, 1),
             (x**2 + 9 * x + 2, 1), (x**8 + x**4 + 6, 1),
             (x**8 + 10 * x**4 + 6, 1), (x**4 + 5 * x**2 + 7, 1),
             (x**4 + 6 * x**2 + 7, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = 8 * x**63 + 5
    g = (8,
         [(x + 7, 1), (x**6 + 9 * x**3 + 4, 1), (x**2 + 4 * x + 5, 1),
          (x**3 + 6 * x**2 + 8 * x + 2, 1), (x**3 + 9 * x**2 + 9 * x + 2, 1),
          (x**6 + 2 * x**5 + 6 * x**4 + 8 * x**2 + 4 * x + 4, 1),
          (x**6 + 2 * x**5 + 8 * x**3 + 4 * x**2 + 6 * x + 4, 1),
          (x**6 + 5 * x**5 + 6 * x**4 + 8 * x**2 + 6 * x + 4, 1),
          (x**6 + 2 * x**5 + 3 * x**4 + 8 * x**3 + 6 * x + 4, 1),
          (x**6 + 10 * x**5 + 4 * x**4 + 7 * x**3 + 10 * x**2 + 7 * x + 4, 1),
          (x**6 + 3 * x**5 + 3 * x**4 + x**3 + 6 * x**2 + 8 * x + 4, 1),
          (x**6 + 6 * x**5 + 2 * x**4 + 7 * x**3 + 9 * x**2 + 8 * x + 4, 1),
          (x**6 + 10 * x**5 + 10 * x**4 + x**3 + 4 * x**2 + 9 * x + 4, 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**15 - 1
    g = (1, [(x + 2, 1), (x + 6, 1), (x + 7, 1), (x + 8, 1), (x + 10, 1),
             (x**2 + x + 1, 1), (x**2 + 5 * x + 3, 1), (x**2 + 9 * x + 4, 1),
             (x**2 + 4 * x + 5, 1), (x**2 + 3 * x + 9, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    with using(gf_factor_method='other'):
        pytest.raises(KeyError, (x + 1).factor_list)

    R, x = ring('x', FF(13))

    f = x**8 + x**6 + 10 * x**4 + 10 * x**3 + 8 * x**2 + 2 * x + 8
    g = (1, [(x + 3, 1), (x**3 + 8 * x**2 + 4 * x + 12, 1),
             (x**4 + 2 * x**3 + 3 * x**2 + 4 * x + 6, 1)])

    with using(gf_factor_method='berlekamp'):
        assert f.factor_list() == g

    R, x = ring('x', FF(809))

    f = (x**10 + 2 * x**9 + 5 * x**8 + 26 * x**7 + 677 * x**6 + 436 * x**5 +
         791 * x**4 + 325 * x**3 + 456 * x**2 + 24 * x + 577)
    g = (1, [(x + 701, 1),
             (x**9 + 110 * x**8 + 559 * x**7 + 532 * x**6 + 694 * x**5 +
              151 * x**4 + 110 * x**3 + 70 * x**2 + 735 * x + 122, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    # Gathen polynomials: x**n + x + 1 (mod p > 2**n * pi)

    R, x = ring('x', FF(nextprime(2**15 * pi)))

    f = x**15 + x + 1
    g = (1, [
        (x**2 + 22730 * x + 68144, 1),
        (x**4 + 81553 * x**3 + 77449 * x**2 + 86810 * x + 4724, 1),
        (x**4 + 86276 * x**3 + 56779 * x**2 + 14859 * x + 31575, 1),
        (x**5 + 15347 * x**4 + 95022 * x**3 + 84569 * x**2 + 94508 * x + 92335,
         1)
    ])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    # Shoup polynomials: f = a_0 x**n + a_1 x**(n-1) + ... + a_n
    # (mod p > 2**(n-2) * pi), where a_n = a_{n-1}**2 + 1, a_0 = 1

    R, x = ring('x', FF(nextprime(2**4 * pi)))

    f = x**6 + 2 * x**5 + 5 * x**4 + 26 * x**3 + 41 * x**2 + 39 * x + 38
    g = (1, [(x**2 + 44 * x + 26, 1),
             (x**4 + 11 * x**3 + 25 * x**2 + 18 * x + 30, 1)])

    for method in ('zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    F4 = FF(2, [1, 1, 1])
    R, x = ring('x', F4)

    f = x**3 + F4(3) * x + F4(2)
    g = (1, [(x + 1, 1), (x**2 + x + F4(2), 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    F8 = FF(2, [1, 1, 0, 1])
    R, x = ring('x', F8)

    f = x**10 + x**9 + F8(2) * x**8 + F8(2) * x**7 + F8(5) * x**6 + F8(
        3) * x**5
    g = (1, [(x, 5), (x + F8(3), 1), (x + F8(6), 1),
             (x**3 + F8(4) * x**2 + x + F8(3), 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    f = x**4 + F8(5) * x**2 + 1
    g = (1, [(x + F8(4), 2), (x + F8(7), 2)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g

    F9 = FF(3, [2, 2, 1])
    R, x = ring('x', F9)

    f = x**5 + F9(2) * x**4 + F9(6) * x**3 + F9(8) * x**2 + F9(5) * x + F9(4)
    g = (1, [(x + F9(8), 1), (x**2 + 2 * x + F9(4), 1),
             (x**2 + F9(4) * x + F9(4), 1)])

    for method in ('berlekamp', 'zassenhaus', 'shoup'):
        with using(gf_factor_method=method):
            assert f.factor_list() == g
def test__reverse():
    R, x = ring('x', ZZ)

    assert R._reverse(x**3 + 2 * x**2 + 3) == 3 * x**3 + 2 * x + 1
    assert R._reverse(x**3 + 2 * x**2 + 3 * x) == 3 * x**2 + 2 * x + 1
Beispiel #19
0
def test_solve_lin_sys_3x4_none():
    domain,  x1, x2, x3, x4 = ring('x1 x2 x3 x4', QQ)
    eqs = [2*x1 + x2 + 7*x3 - 7*x4 - 2,
           -3*x1 + 4*x2 - 5*x3 - 6*x4 - 3,
           x1 + x2 + 4*x3 - 5*x4 - 2]
    assert solve_lin_sys(eqs, domain) is None
def test__sqf_p():
    R, x, z = ring('x z', ZZ)

    assert _sqf_p(z**2, (z**2 - 2).drop(0), 2) is True
Beispiel #21
0
def test_dmp_gcd():
    R, x = ring('x', FF(5))

    f = 3 * x**2 + 2 * x + 4
    g = 2 * x**2 + 2 * x + 3

    assert f.cofactors(g) == (x + 3, 3 * x + 3, 2 * x + 1)

    R, x = ring('x', FF(11))

    assert R(0).cofactors(R(0)) == (0, 0, 0)
    assert R(2).cofactors(R(0)) == (1, 2, 0)
    assert R(0).cofactors(R(2)) == (1, 0, 2)
    assert R(2).cofactors(R(2)) == (1, 2, 2)

    assert R(0).cofactors(x) == (x, 0, 1)
    assert x.cofactors(R(0)) == (x, 1, 0)

    assert (3 * x).cofactors(3 * x) == (x, 3, 3)
    assert (x**2 + 8 * x + 7).cofactors(x**3 + 7 * x**2 + x + 7) == (x + 7,
                                                                     x + 1,
                                                                     x**2 + 1)

    R, x = ring('x', ZZ)

    for test in (True, False):
        for method in ('prs', 'modgcd'):
            with using(use_heu_gcd=test, fallback_gcd_zz_method=method):
                assert R(0).cofactors(R(0)) == (0, 0, 0)
                assert R(0).cofactors(x) == (x, 0, 1)
                assert x.cofactors(R(0)) == (x, 1, 0)
                assert R(0).cofactors(-x) == (x, 0, -1)
                assert (-x).cofactors(R(0)) == (x, -1, 0)
                assert (2 * x).cofactors(R(2)) == (2, x, 1)
                assert R(2).cofactors(R(0)) == (2, 1, 0)
                assert R(-2).cofactors(R(0)) == (2, -1, 0)
                assert R(0).cofactors(R(-2)) == (2, 0, -1)
                assert R(0).cofactors(2 * x + 4) == (2 * x + 4, 0, 1)
                assert (2 * x + 4).cofactors(R(0)) == (2 * x + 4, 1, 0)
                assert R(2).cofactors(R(2)) == (2, 1, 1)
                assert R(-2).cofactors(R(2)) == (2, -1, 1)
                assert R(2).cofactors(R(-2)) == (2, 1, -1)
                assert R(-2).cofactors(R(-2)) == (2, -1, -1)
                assert (x**2 + 2 * x + 1).cofactors(R(1)) == (1,
                                                              x**2 + 2 * x + 1,
                                                              1)
                assert (x**2 + 2 * x + 1).cofactors(R(2)) == (1,
                                                              x**2 + 2 * x + 1,
                                                              2)
                assert (2 * x**2 + 4 * x + 2).cofactors(R(2)) == (2, x**2 +
                                                                  2 * x + 1, 1)
                assert R(2).cofactors(2 * x**2 + 4 * x + 2) == (2, 1, x**2 +
                                                                2 * x + 1)
                assert (2 * x**2 + 4 * x + 2).cofactors(x + 1) == (x + 1,
                                                                   2 * x + 2,
                                                                   1)
                assert (x + 1).cofactors(2 * x**2 + 4 * x + 2) == (x + 1, 1,
                                                                   2 * x + 2)
                assert (x - 31).cofactors(x) == (1, x - 31, x)

                f, g = 2 * x + 2, 6 * x**2 - 6

                assert f.cofactors(g) == (2 * x + 2, 1, 3 * x - 3)

                f, g = [1000000000000 * x + 998549000000] * 2

                assert f.cofactors(g) == (f, 1, 1)

                f, g = 999530000000 * x + 1000000000000, 999530000000 * x + 999999000000

                assert f.cofactors(g) == (1000000, 999530 * x + 1000000,
                                          999530 * x + 999999)

                f = x**2 - 1
                g = x**2 - 3 * x + 2

                assert f.cofactors(g) == (x - 1, x + 1, x - 2)

                f = x**4 + 8 * x**3 + 21 * x**2 + 22 * x + 8
                g = x**3 + 6 * x**2 + 11 * x + 6

                assert f.cofactors(g) == (x**2 + 3 * x + 2, x**2 + 5 * x + 4,
                                          x + 3)

                f = x**4 - 4
                g = x**4 + 4 * x**2 + 4

                assert f.cofactors(g) == (x**2 + 2, x**2 - 2, x**2 + 2)

                f = x**8 + x**6 - 3 * x**4 - 3 * x**3 + 8 * x**2 + 2 * x - 5
                g = 3 * x**6 + 5 * x**4 - 4 * x**2 - 9 * x + 21

                assert f.cofactors(g) == (1, f, g)

                f = (
                    -352518131239247345597970242177235495263669787845475025293906825864749649589178600387510272
                    * x**49 +
                    46818041807522713962450042363465092040687472354933295397472942006618953623327997952
                    * x**42 +
                    378182690892293941192071663536490788434899030680411695933646320291525827756032
                    * x**35 +
                    112806468807371824947796775491032386836656074179286744191026149539708928
                    * x**28 -
                    12278371209708240950316872681744825481125965781519138077173235712
                    * x**21 +
                    289127344604779611146960547954288113529690984687482920704 *
                    x**14 +
                    19007977035740498977629742919480623972236450681 * x**7 +
                    311973482284542371301330321821976049)

                h = (
                    365431878023781158602430064717380211405897160759702125019136
                    * x**21 +
                    197599133478719444145775798221171663643171734081650688 *
                    x**14 -
                    9504116979659010018253915765478924103928886144 * x**7 -
                    311973482284542371301330321821976049)
                cff = (-964661685087874498642420170752 * x**28 +
                       649736296036977287118848 * x**21 +
                       658473216967637120 * x**14 - 30463679113 * x**7 - 1)
                cfg = (-47268422569305850433478588366848 * x**27 +
                       30940259392972115602096128 * x**20 +
                       18261628279718027904 * x**13 - 426497272383 * x**6)

                assert f.cofactors(f.diff()) == (h, cff, cfg)

                f = 1317378933230047068160 * x + 2945748836994210856960
                g = 120352542776360960 * x + 269116466014453760

                H, cff, cfg = 120352542776360960 * x + 269116466014453760, 10946, 1

                assert f.cofactors(g) == (H, cff, cfg)

                with using(heu_gcd_max=0):
                    assert f.cofactors(g) == (H, cff, cfg)

    R, x = ring('x', QQ)

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='prs'):
            f = x**8 + x**6 - 3 * x**4 - 3 * x**3 + 8 * x**2 + 2 * x - 5
            g = 3 * x**6 + 5 * x**4 - 4 * x**2 - 9 * x + 21

            assert f.cofactors(g) == (1, f, g)

            assert R(0).cofactors(R(0)) == (0, 0, 0)

            f, g = x**2 / 2 + x + QQ(1, 2), x / 2 + QQ(1, 2)

            assert f.cofactors(g) == (x + 1, g, QQ(1, 2))

            f, g = x**2 - 1, x**2 - 3 * x + 2

            assert f.cofactors(g) == (x - 1, x + 1, x - 2)

    R, x = ring('x', QQ.algebraic_field(sqrt(2)))

    for method in ('modgcd', 'prs'):
        with using(gcd_aa_method=method):
            f, g = 2 * x, R(2)

            assert f.cofactors(g) == (1, f, g)

            f, g = 2 * x, R(sqrt(2))

            assert f.cofactors(g) == (1, f, g)

            f, g = 2 * x + 2, 6 * x**2 - 6

            assert f.cofactors(g) == (x + 1, 2, 6 * x - 6)

    R, x = ring('x', QQ.algebraic_field(sqrt(2)**(-1) * sqrt(3)))

    for method in ('modgcd', 'prs'):
        with using(gcd_aa_method=method):
            f, g = x + 1, x - 1

            assert f.cofactors(g) == (1, f, g)

    R, x = ring('x', CC)

    f, g = x**2 - 1, x**3 - 3 * x + 2

    assert f.cofactors(g) == (x - 1, x + 1, x**2 + x - 2)

    R, x, y = ring('x y', ZZ)

    for test in (True, False):
        for method in ('prs', 'modgcd'):
            with using(use_heu_gcd=test, fallback_gcd_zz_method=method):
                assert R(0).cofactors(R(0)) == (0, 0, 0)
                assert R(2).cofactors(R(0)) == (2, 1, 0)
                assert R(-2).cofactors(R(0)) == (2, -1, 0)
                assert R(0).cofactors(R(-2)) == (2, 0, -1)
                assert R(0).cofactors(2 * x + 4) == (2 * x + 4, 0, 1)
                assert (2 * x).cofactors(R(2)) == (2, x, 1)
                assert (2 * x + 4).cofactors(R(0)) == (2 * x + 4, 1, 0)
                assert R(2).cofactors(R(2)) == (2, 1, 1)
                assert R(-2).cofactors(R(2)) == (2, -1, 1)
                assert R(2).cofactors(R(-2)) == (2, 1, -1)
                assert R(-2).cofactors(R(-2)) == (2, -1, -1)
                assert (x**2 + 2 * x + 1).cofactors(R(1)) == (1,
                                                              x**2 + 2 * x + 1,
                                                              1)
                assert (x**2 + 2 * x + 1).cofactors(R(2)) == (1,
                                                              x**2 + 2 * x + 1,
                                                              2)
                assert (2 * x**2 + 4 * x + 2).cofactors(R(2)) == (2, x**2 +
                                                                  2 * x + 1, 1)
                assert R(2).cofactors(2 * x**2 + 4 * x + 2) == (2, 1, x**2 +
                                                                2 * x + 1)

                f, g = 2 * x**2 + 4 * x + 2, x + 1

                assert f.cofactors(g) == (g, 2 * x + 2, 1)
                assert g.cofactors(f) == (g, 1, 2 * x + 2)

                with using(heu_gcd_max=0):
                    assert f.cofactors(g) == (g, 2 * x + 2, 1)

                f = x**4 + 8 * x**3 + 21 * x**2 + 22 * x + 8
                g = x**3 + 6 * x**2 + 11 * x + 6

                assert f.cofactors(g) == (x**2 + 3 * x + 2, x**2 + 5 * x + 4,
                                          x + 3)

                f, g = x + 2 * y, x + y

                assert f.cofactors(g) == (1, f, g)

                f, g = x**2 + 2 * x * y + y**2, x**2 + x * y

                assert f.cofactors(g) == (x + y, x + y, x)

                f, g = x**2 + 2 * x * y + y**2, x**3 + y**3

                assert f.cofactors(g) == (x + y, x + y, x**2 - x * y + y**2)

                f, g = x * y**2 + 2 * x * y + x, x * y**3 + x

                assert f.cofactors(g) == (x * y + x, y + 1, y**2 - y + 1)

                f, g = x**2 * y**2 + x**2 * y + 1, x * y**2 + x * y + 1

                assert f.cofactors(g) == (1, f, g)

                f = 2 * x * y**2 + 4 * x * y + 2 * x + y**2 + 2 * y + 1
                g = 2 * x * y**3 + 2 * x + y**3 + 1

                assert f.cofactors(g) == (2 * x * y + 2 * x + y + 1, y + 1,
                                          y**2 - y + 1)

                f = 2 * x**2 + 4 * x * y - 2 * x - 4 * y
                g = x**2 + x - 2

                assert f.cofactors(g) == (x - 1, 2 * x + 4 * y, x + 2)

                f = 2 * x**2 + 2 * x * y - 3 * x - 3 * y
                g = 4 * x * y - 2 * x + 4 * y**2 - 2 * y

                assert f.cofactors(g) == (x + y, 2 * x - 3, 4 * y - 2)

                f = (
                    -17434367009167300000000000000000000000000000000000000000000000000000000
                    * x**4 * y -
                    250501827896299135568887342575961783764139560000000000000000000000000000000000000000000
                    * x**3 * y -
                    2440935909299672540738135183426056447877858000000000000000000000000000000
                    * x**3 -
                    1349729941723537919695626818065131519270095220127010623905326719279566297660000000000000000000000000000
                    * x**2 * y -
                    26304033868956978374552886858060487282904504027042515077682955951658838800000000000000000
                    * x**2 -
                    3232215785736369696036755035364398565076440134133908303058376297547504030528179314849416971379040931276000000000000000
                    * x * y -
                    94485916261760032526508027937078714464844205539023800247528621905831259414691631156161537919255129011800
                    * x -
                    2902585888465621357542575571971656665554321652262249362701116665830760628936600958940851960635161420991047110815678789984677193092993
                    * y -
                    113133324167442997472440652189550843502029192913459268196939183295294085146407870078840385860571627108778756267503630290
                )
                g = (
                    10000000000000000000000000000 * x**2 +
                    71841388839807267676152024786000000000000000 * x +
                    129029628760809605749020969023932901278290735413660734705971
                )

                h = (
                    -1743436700916730000000000000000000000000000 * x**2 * y -
                    12525091394814956778444367128798089188206978000000000000000
                    * x * y -
                    244093590929967254073813518342605644787785800 * x -
                    22495499028725631994927113634418779135935898997901327211111875586270479483
                    * y -
                    876801128965234839118530545935732755107147297241756982389990
                )

                assert f.cofactors(g) == (g, h, 1)

    R, x, y = ring('x y', QQ)

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='prs'):
            f, g = x**2 / 2 + x + QQ(1, 2), x / 2 + QQ(1, 2)

            assert f.cofactors(g) == (x + 1, g, QQ(1, 2))
            assert g.cofactors(f) == (x + 1, QQ(1, 2), g)

            assert f.gcd(g) == x + 1
            with using(fallback_gcd_zz_method='modgcd'):
                assert f.gcd(g) == x + 1

            assert R(0).cofactors(R(0)) == (0, 0, 0)
            assert R(0).cofactors(g) == (x + 1, 0, QQ(1, 2))

            f, g = x**2 / 4 + x * y + y**2, x**2 / 2 + x * y

            assert f.cofactors(g) == (x + 2 * y, x / 4 + y / 2, x / 2)

            f, g = x**2 / 2 + x * y + y**2 / 2, x**2 + x * y

            assert f.cofactors(g) == (x + y, x / 2 + y / 2, x)

    R, x, y = ring('x y', QQ.algebraic_field(sqrt(2)))

    for method in ('modgcd', 'prs'):
        with using(gcd_aa_method=method):
            f, g = (x + sqrt(2) * y)**2, x + sqrt(2) * y

            assert f.cofactors(g) == (g, g, 1)

            f, g = x + sqrt(2) * y, x + y

            assert f.cofactors(g) == (1, f, g)

            f, g = x * y + sqrt(2) * y**2, sqrt(2) * y

            assert f.cofactors(g) == (y, x + sqrt(2) * y, sqrt(2))

            f, g = x**2 + 2 * sqrt(2) * x * y + 2 * y**2, x + sqrt(2) * y

            assert f.cofactors(g) == (g, g, 1)

    R, x, y = ring('x y', RR)

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='prs'):
            f, g = 2.1 * x * y**2 - 2.2 * x * y + 2.1 * x, 1.0 * x**3
            h = 1.0 * x

            assert f.cofactors(g) == (h, 2.1 * y**2 - 2.2 * y + 2.1,
                                      1.0 * x**2)

            f, g = 2.1 * x * y**2 - 2.1 * x * y + 2.1 * x, 2.1 * x**3

            assert f.cofactors(g) == (h, f // h, g // h)
            assert g.cofactors(f) == (h, g // h, f // h)

    R, x, y = ring('x y', CC)

    f, g = x**2 - y, x**3 - y * x + 2

    assert f.cofactors(g) == (1, f, g)

    R, x, y, z = ring('x y z', ZZ)

    for test in (True, False):
        for method in ('prs', 'modgcd'):
            with using(use_heu_gcd=test, fallback_gcd_zz_method=method):
                f, g = x - y * z, x - y * z

                assert f.cofactors(g) == (x - y * z, 1, 1)

                f, g, h = R.fateman_poly_F_1()
                H, cff, cfg = f.cofactors(g)

                assert H == h and H * cff == f and H * cfg == g

                f, g, h = R.fateman_poly_F_2()
                H, cff, cfg = f.cofactors(g)

                assert H == h and H * cff == f and H * cfg == g

                f, g, h = R.fateman_poly_F_3()
                H, cff, cfg = f.cofactors(g)

                assert H == h and H * cff == f and H * cfg == g

    R, x, y, z = ring('x y z', QQ.algebraic_field(sqrt(2), sqrt(3)))

    with using(gcd_aa_method='modgcd'):
        h = x**2 * y**7 + sqrt(6) / 21 * z
        f, g = h * (27 * y**3 + 1), h * (y + x)

        assert f.cofactors(g) == (h, 27 * y**3 + 1, x + y)

        h = x**13 * y**3 + x**10 / 2 + 1 / sqrt(2)
        f, g = h * (x + 1), h * sqrt(2) / sqrt(3)

        assert f.cofactors(g) == (h, x + 1, sqrt(2) / sqrt(3))

        h = x**4 * y**9 + sqrt(6) / 22 * z
        f, g = h * (21 * y**3 + 1), h * (y + x)

        assert f.cofactors(g) == (x**4 * y**9 + sqrt(6) / 22 * z,
                                  21 * y**3 + 1, x + y)

        h = x**4 * y**3 + sqrt(6) / 22 * z
        f, g = h * (11 * y**3 + 1), h * (y + x)

        assert f.cofactors(g) == (x**4 * y**3 + sqrt(6) / 22 * z,
                                  11 * y**3 + 1, x + y)

        h = x**2 * y**3 + 1111 * sqrt(6) / 12 * z
        a, b = 11 * y**3 + 2, (y + x - 1) * h

        assert (h * a).cofactors(h * b) == (h, a, b)

        a, b = 12 * y + 2 * x - 1, (y + x - 1) * h

        assert (h * a).cofactors(h * b) == (h, a, b)

    R, x, y, z = ring('x y z', QQ.algebraic_field(I))

    for method in ('prs', 'modgcd'):
        with using(gcd_aa_method=method):
            f, g = R.one, I * z

            assert f.cofactors(g) == (1, f, g)
            assert g.cofactors(f) == (1, g, f)

    R, x, y, z, u = ring('x y z u', ZZ)

    for test in (True, False):
        for method in ('prs', 'modgcd'):
            with using(use_heu_gcd=test, fallback_gcd_zz_method=method):
                f, g = u**2 + 2 * u + 1, 2 * u + 2

                assert f.cofactors(g) == (u + 1, u + 1, 2)

                f, g = z**2 * u**2 + 2 * z**2 * u + z**2 + z * u + z, u**2 + 2 * u + 1
                h, cff, cfg = u + 1, z**2 * u + z**2 + z, u + 1

                assert f.cofactors(g) == (h, cff, cfg)
                assert g.cofactors(f) == (h, cfg, cff)

                f, g = x + y + z, -x - y - z - u

                assert f.cofactors(g) == (1, f, g)

                f, g, h = R.fateman_poly_F_3()
                H, cff, cfg = f.cofactors(g)

                assert H == h and H * cff == f and H * cfg == g

                f, g, h = (1199999999999991 * x**17 - y,
                           2 * y - 19989798798 + x**211,
                           12 * x * y**7 + x**4 - 1)

                for _ in range(10):
                    assert (f * h).cofactors(g * h) == (h, f, g)

    R, x, y, z, u, _ = ring('x y z u v', ZZ)

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='modgcd'):
            f, g, h = R.fateman_poly_F_1()
            H, cff, cfg = f.cofactors(g)

            assert H == h and H * cff == f and H * cfg == g

            f, g, h = R.fateman_poly_F_3()
            H, cff, cfg = f.cofactors(g)

            assert H == h and H * cff == f and H * cfg == g

    R, x, y, z, u, _, a, b = ring('x y z u v a b', ZZ)

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='modgcd'):
            f, g, h = R.fateman_poly_F_1()
            H, cff, cfg = f.cofactors(g)

            assert H == h and H * cff == f and H * cfg == g

    R, x, y, z, u, _, a, b, *_ = ring('x y z u v a b c d', ZZ)

    for test in (True, False):
        with using(use_heu_gcd=test, fallback_gcd_zz_method='modgcd'):
            f, g, h = R.fateman_poly_F_1()
            H, cff, cfg = f.cofactors(g)

            assert H == h and H * cff == f and H * cfg == g

    F, x = field('x', QQ)
    R, _ = ring('t', F)

    assert R(x).gcd(R(0)) == 1
def test_efactor_1():
    R, x, y = ring('x y', QQ.algebraic_field(sqrt(2)))

    f = (x**2 + sqrt(2)*y)
    assert efactor(f) == (1, [(f, 1)])

    f1 = x + y
    f2 = x**2 + sqrt(2)*y
    f = f1 * f2

    assert efactor(f) == (1, [(f1, 1), (f2, 1)])
    assert efactor(f, save=False) == (1, [(f1, 1), (f2, 1)])

    f1 = x + 2**10*y
    f2 = x**2 + sqrt(2)*y
    f = f1 * f2

    assert efactor(f) == (1, [(f1, 1), (f2, 1)])

    R, x, y, z = ring('x y z', QQ.algebraic_field(sqrt(2)))

    f1 = x - sqrt(2)*z
    f = f1**2

    assert efactor(f) == (1, [(f1, 2)])

    A3 = QQ.algebraic_field(sqrt(3))
    R, x, y, z = ring('x y z', A3)

    f1 = z + 1
    f2 = 3*x*y**2/4 + sqrt(3)
    f3 = sqrt(3)*y*x**2 + 2*y + z
    f = f1 * f2**2 * f3

    lc2 = f2.LC
    lc3 = f3.LC

    assert efactor(f) == (lc2**2*lc3, [(f1, 1), (f2.quo_ground(lc2), 2),
                                       (f3.quo_ground(lc3), 1)])

    R, x, y = ring('x y', QQ.algebraic_field(I))

    f1 = x - I*y
    f2 = x + I*y
    f = f1*f2

    assert efactor(f) == (1, [(f1, 1), (f2, 1)])

    f1 = x*(y + 1) + 1
    f2 = x*(y + I) + 1
    f3 = x**2*(y - I) + 1
    f = f1*f2*f3

    assert efactor(f) == (1, [(f1, 1), (f2, 1), (f3, 1)])

    lc = R.domain(-2)
    f1 = x*(y - 3*I) + lc**(-1)
    f2 = x*(y + 2) + 1
    f3 = x*(y + I) + 1
    f = lc*f1*f2*f3

    assert efactor(f) == (lc, [(f1, 1), (f2, 1), (f3, 1)])

    a = sqrt(2)*(1 + I)/2
    A = QQ.algebraic_field(a)
    R, x, y = ring('x y', A)

    f1 = x - a**3*y
    f2 = x - a*y
    f3 = x + a**3*y
    f4 = x + a*y
    f = x**4 + y**4

    assert f1*f2*f3*f4 == f
    assert efactor(f) == (1, [(f1, 1), (f2, 1), (f3, 1), (f4, 1)])

    R, x, y, z = ring('x y z', QQ.algebraic_field(root(2, 5)))

    f1 = y
    f2 = x - y
    f3 = x**2 + root(2, 5)*y*z
    f = f1*f2*f3

    assert efactor(f) == (1, [(f1, 1), (f2, 1), (f3, 1)])

    R, x, y, z = ring('x y z', QQ.algebraic_field(sqrt(2), root(3, 3)))

    lc = R.domain.from_expr(root(3, 3))
    f1 = x - z*root(3, 3)**2/3
    f2 = x**2 + 2*y + sqrt(2)
    f = lc*f1*f2

    assert efactor(f) == (lc, [(f1, 1), (f2, 1)])

    a = (-1 + sqrt(5))/4 - I*sqrt((sqrt(5) + 5)/8)
    A = QQ.algebraic_field(a)
    a = A.unit
    R, x, y, z = ring('x y z', A)

    f1 = x**2 + 2*(a**3 + a**2 + a + 1)*x + a*z**2 + a**3*y + 12*a**2
    f2 = x**2 - 2*a*x - (a**3 + a**2 + a + 1)*z**2 + a**2*y + 12*a**3
    f = f1*f2

    assert efactor(f) == (1, [(f1, 1), (f2, 1)])

    R, x, y = ring('x y', QQ.algebraic_field(root(1, 5, 3)))
    A = R.domain

    a = A([QQ(-19125, 42722), QQ(23337, 21361), QQ(46350, 21361), QQ(17315, 21361)])
    b = A([QQ(-17355, 85444), QQ(-15120, 21361), QQ(-7870, 21361), QQ(45917, 85444)])
    c = A([QQ(5, 521), QQ(130, 521), QQ(650, 521), QQ(104, 521)])
    d = A([QQ(16196, 21361), QQ(-6645, 21361), QQ(-20200, 21361), QQ(-29909, 42722)])

    e = a*y + b*y**2 + x*c + x*y*d + x**2

    r = efactor(e)

    e1 = A([QQ(75, 82), QQ(-10, 41), QQ(-25, 41), QQ(-47, 41)])*y + x
    e2 = (x + A([QQ(-163, 1042), QQ(-35, 521), QQ(-175, 521),
                 QQ(465, 1042)])*y + A([QQ(5, 521), QQ(130, 521),
                                        QQ(650, 521), QQ(104, 521)]))

    assert r == (1, [(e1, 1), (e2, 1)])
Beispiel #23
0
def test_PolyElement_lcm():
    R, x = ring('x', FF(5))

    assert (3 * x**2 + 2 * x + 4).lcm(2 * x**2 + 2 * x +
                                      3) == x**3 + 2 * x**2 + 4

    R, x = ring('x', FF(11))

    assert R.zero.lcm(R(2)) == 0
    assert R(2).lcm(R(2)) == 1

    assert R.zero.lcm(x) == 0

    assert (3 * x).lcm(3 * x) == x
    assert (x**2 + 8 * x + 7).lcm(x**3 + 7 * x**2 + x +
                                  7) == (x**4 + 8 * x**3 + 8 * x**2 + 8 * x +
                                         7)

    R, x = ring('x', ZZ)

    assert R(2).lcm(R(6)) == 6

    assert (2 * x**3).lcm(6 * x) == 6 * x**3
    assert (2 * x**3).lcm(3 * x) == 6 * x**3

    assert (x**2 + x).lcm(x) == x**2 + x
    assert (x**2 + x).lcm(2 * x) == 2 * x**2 + 2 * x
    assert (x**2 + 2 * x).lcm(x) == x**2 + 2 * x
    assert (2 * x**2 + x).lcm(x) == 2 * x**2 + x
    assert (2 * x**2 + x).lcm(2 * x) == 4 * x**2 + 2 * x
    assert (x**2 - 1).lcm(x**2 - 3 * x + 2) == x**3 - 2 * x**2 - x + 2

    R, x = ring('x', QQ)

    f = (x**2 + 7 * x / 2 + 3) / 2
    g = x**2 / 2 + x
    h = x**3 + 7 / 2 * x**2 + 3 * x

    assert f.lcm(g) == h

    R, x, y = ring('x y', ZZ)

    assert R(2).lcm(R(6)) == 6
    assert x.lcm(y) == x * y

    assert (2 * x**3).lcm(6 * x * y**2) == 6 * x**3 * y**2
    assert (2 * x**3).lcm(3 * x * y**2) == 6 * x**3 * y**2

    assert (x**2 * y).lcm(x * y**2) == x**2 * y**2

    f = 2 * x * y**5 - 3 * x * y**4 - 2 * x * y**3 + 3 * x * y**2
    g = y**5 - 2 * y**3 + y
    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

    assert f.lcm(g) == h

    f = x**3 - 3 * x**2 * y - 9 * x * y**2 - 5 * y**3
    g = x**4 + 6 * x**3 * y + 12 * x**2 * y**2 + 10 * x * y**3 + 3 * y**4
    h = x**5 + x**4 * y - 18 * x**3 * y**2 - 50 * x**2 * y**3 - 47 * x * y**4 - 15 * y**5

    assert f.lcm(g) == h

    f = x**2 + 2 * x * y + y**2
    g = x**2 + x * y
    h = x**3 + 2 * x**2 * y + x * y**2

    assert f.lcm(g) == h

    R, x, y = ring('x y', QQ)

    f = 2 * x * y - x**2 / 2 + QQ(1, 3)
    g = 3 * x**3 - x * y**2 - QQ(1, 2)
    h = (x**5 - 4 * x**4 * y - x**3 * y**2 / 3 - 2 * x**3 / 3 +
         4 * x**2 * y**3 / 3 - x**2 / 6 + 2 * x * y**2 / 9 + 2 * x * y / 3 +
         QQ(1, 9))

    assert f.lcm(g) == h

    f = x**2 / 4 + x * y + y**2
    g = x**2 / 2 + x * y
    h = x**3 + 4 * x**2 * y + 4 * x * y**2

    assert f.lcm(g) == h
Beispiel #24
0
def test_dmp_ground_content():
    R, x = ring('x', ZZ)

    assert R(0).content() == 0
    assert R(+1).content() == 1
    assert R(-1).content() == -1
    assert (x + 1).content() == 1
    assert (2 * x + 2).content() == 2
    assert (x**2 + 2 * x + 1).content() == 1
    assert (2 * x**2 + 4 * x + 2).content() == 2
    assert (6 * x**2 + 8 * x + 12).content() == 2

    R, x = ring('x', QQ)

    assert (6 * x**2 + 8 * x + 12).content() == 2

    assert (2 * x / 3 + QQ(4, 9)).content() == QQ(2, 9)
    assert (2 * x / 3 + QQ(4, 5)).content() == QQ(2, 15)

    R, x, y = ring('x y', ZZ)

    assert R(0).content() == 0
    assert R(+1).content() == 1
    assert R(-1).content() == -1
    assert (x + 1).content() == 1
    assert (2 * x + 2).content() == 2
    assert (x**2 + 2 * x + 1).content() == 1
    assert (2 * x**2 + 4 * x + 2).content() == 2

    R, x, y = ring('x y', QQ)

    assert R(0).content() == 0
    assert (2 * x / 3 + QQ(4, 9)).content() == QQ(2, 9)
    assert (2 * x / 3 + QQ(4, 5)).content() == QQ(2, 15)

    R, x, y, z = ring('x y z', ZZ)

    f = f_polys()[0]

    assert f.content() == 1
    assert (2 * f).content() == 2

    f = f_polys()[1]

    assert f.content() == 1
    assert (3 * f).content() == 3

    f = f_polys()[2]

    assert f.content() == 1
    assert (4 * f).content() == 4

    f = f_polys()[3]

    assert f.content() == 1
    assert (5 * f).content() == 5

    f = f_polys()[4]

    assert f.content() == -1
    assert (6 * f).content() == -6

    f = f_polys()[5]

    assert f.content() == -1
    assert (7 * f).content() == -7

    R, x, y, z, t = ring('x y, z t', ZZ)

    f = f_polys()[6]

    assert f.content() == 1
    assert (8 * f).content() == 8
def test_groebner(method):
    with config.using(groebner=method):
        R, x, y = ring('x y', QQ, lex)
        f = x**2 + 2 * x * y**2
        g = x * y + 2 * y**3 - 1

        assert not is_groebner([f, g])
        ans = [x, y**3 - QQ(1, 2)]
        assert groebner([f, g], R) == ans
        assert is_groebner(ans)
        assert is_minimal(ans, R)

        assert groebner([x, x**2], R) == [x]
        assert groebner([x**2, x], R) == [x]

        R, x, y = ring('x y', ZZ)
        f = x**2 * y + y**6 + 1
        g = x**2 - 2 * x * y

        assert not is_groebner([f, g])
        ans = [
            2 * x - y**10 - 4 * y**7 - y**4 - 4 * y,
            y**12 + 4 * y**9 + 2 * y**6 + 4 * y**3 + 1
        ]
        assert groebner([f, g], R) == ans
        assert is_groebner(ans)
        assert is_minimal(ans, R) is False

        R, y, x = ring('y x', QQ, lex)
        f = 2 * x**2 * y + y**2
        g = 2 * x**3 + x * y - 1

        assert groebner([f, g], R) == [y, x**3 - QQ(1, 2)]

        R, x, y, z = ring('x y z', QQ, lex)
        f = x - z**2
        g = y - z**3

        assert groebner([f, g], R) == [f, g]

        R, x, y = ring('x y', QQ, grlex)
        f = x**3 - 2 * x * y
        g = x**2 * y + x - 2 * y**2

        assert groebner([f, g], R) == [x**2, x * y, -x / 2 + y**2]

        R, x, y, z = ring('x y z', QQ, lex)
        f = -x**2 + y
        g = -x**3 + z

        assert groebner([f, g],
                        R) == [x**2 - y, x * y - z, x * z - y**2, y**3 - z**2]

        R, x, y, z = ring('x y z', QQ, grlex)
        f = -x**2 + y
        g = -x**3 + z

        assert groebner([f, g],
                        R) == [y**3 - z**2, x**2 - y, x * y - z, x * z - y**2]

        R, x, y, z = ring('x y z', QQ, lex)
        f = -x**2 + z
        g = -x**3 + y

        assert groebner([f, g],
                        R) == [x**2 - z, x * y - z**2, x * z - y, y**2 - z**3]

        R, x, y, z = ring('x y z', QQ, grlex)
        f = -x**2 + z
        g = -x**3 + y

        assert groebner(
            [f, g], R) == [-y**2 + z**3, x**2 - z, x * y - z**2, x * z - y]

        R, x, y, z = ring('x y z', QQ, lex)
        f = x - y**2
        g = -y**3 + z

        assert groebner([f, g], R) == [x - y**2, y**3 - z]

        R, x, y, z = ring('x y z', QQ, grlex)
        f = x - y**2
        g = -y**3 + z

        assert groebner([f, g], R) == [x**2 - y * z, x * y - z, -x + y**2]

        R, x, y, z = ring('x y z', QQ, lex)
        f = x - z**2
        g = y - z**3

        assert groebner([f, g], R) == [x - z**2, y - z**3]

        R, x, y, z = ring('x y z', QQ, grlex)
        f = x - z**2
        g = y - z**3

        assert groebner([f, g], R) == [x**2 - y * z, x * z - y, -x + z**2]

        R, x, y, z = ring('x y z', QQ, lex)
        f = -y**2 + z
        g = x - y**3

        assert groebner([f, g], R) == [x - y * z, y**2 - z]

        R, x, y, z = ring('x y z', QQ, grlex)
        f = -y**2 + z
        g = x - y**3

        assert groebner(
            [f, g], R) == [-x**2 + z**3, x * y - z**2, y**2 - z, -x + y * z]

        R, x, y, z = ring('x y z', QQ, lex)
        f = y - z**2
        g = x - z**3

        assert groebner([f, g], R) == [x - z**3, y - z**2]

        R, x, y, z = ring('x y z', QQ, grlex)
        f = y - z**2
        g = x - z**3

        assert groebner(
            [f, g], R) == [-x**2 + y**3, x * z - y**2, -x + y * z, -y + z**2]

        R, x, y, z = ring('x y z', QQ, lex)
        f = 4 * x**2 * y**2 + 4 * x * y + 1
        g = x**2 + y**2 - 1

        assert groebner([f, g], R) == [
            x - 4 * y**7 + 8 * y**5 - 7 * y**3 + 3 * y,
            y**8 - 2 * y**6 + 3 * y**4 / 2 - y**2 / 2 + QQ(1, 16),
        ]

        b = [y**2 + x * y + x**2, y + x, y, x**2, x]
        assert is_groebner(b)
        assert not is_minimal(b, R)
Beispiel #26
0
def test_dmp_ground_primitive():
    R, x = ring('x', ZZ)

    assert R(0).primitive() == (0, 0)
    assert R(1).primitive() == (1, 1)
    assert (x + 1).primitive() == (1, x + 1)
    assert (2 * x + 2).primitive() == (2, x + 1)
    assert (x**2 + 2 * x + 1).primitive() == (1, x**2 + 2 * x + 1)
    assert (2 * x**2 + 4 * x + 2).primitive() == (2, x**2 + 2 * x + 1)
    assert (6 * x**2 + 8 * x + 12).primitive() == (2, 3 * x**2 + 4 * x + 6)

    R, x = ring('x', QQ)

    assert R(0).primitive() == (0, 0)
    assert R(1).primitive() == (1, 1)
    assert (x + 1).primitive() == (1, x + 1)
    assert (2 * x + 2).primitive() == (2, x + 1)
    assert (x**2 + 2 * x + 1).primitive() == (1, x**2 + 2 * x + 1)
    assert (2 * x**2 + 4 * x + 2).primitive() == (2, x**2 + 2 * x + 1)
    assert (6 * x**2 + 8 * x + 12).primitive() == (2, 3 * x**2 + 4 * x + 6)

    assert (2 * x / 3 + QQ(4, 9)).primitive() == (QQ(2, 9), 3 * x + 2)
    assert (2 * x / 3 + QQ(4, 5)).primitive() == (QQ(2, 15), 5 * x + 6)

    R, x, y = ring('x y', ZZ)

    assert R(0).primitive() == (0, 0)
    assert R(2).primitive() == (2, 1)

    R, x, y, z = ring('x y z', ZZ)

    f = f_polys()[0]

    assert f.primitive() == (1, f)
    assert (2 * f).primitive() == (2, f)

    f = f_polys()[1]

    assert f.primitive() == (1, f)
    assert (3 * f).primitive() == (3, f)

    f = f_polys()[2]

    assert f.primitive() == (1, f)
    assert (4 * f).primitive() == (4, f)

    f = f_polys()[3]

    assert f.primitive() == (1, f)
    assert (5 * f).primitive() == (5, f)

    f = f_polys()[4]

    assert f.primitive() == (-1, -f)
    assert (6 * f).primitive() == (-6, -f)

    f = f_polys()[5]

    assert f.primitive() == (-1, -f)
    assert (7 * f).primitive() == (-7, -f)

    R, x, y, z, t = ring('x y z t', ZZ)

    f = f_polys()[6]

    assert f.primitive() == (1, f)
    assert (8 * f).primitive() == (8, f)

    R, x, y = ring('x y', QQ)

    assert R(2).primitive() == (2, 1)

    assert (2 * x / 3 + QQ(4, 9)).primitive() == (QQ(2, 9), 3 * x + 2)
    assert (2 * x / 3 + QQ(4, 5)).primitive() == (QQ(2, 15), 5 * x + 6)
def test_dup_zz_factor():
    R, x = ring('x', ZZ)

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])
    assert R(-7).factor_list() == (-7, [])

    assert R._zz_factor_sqf(R(0)) == (0, [])
    assert R._zz_factor_sqf(R(7)) == (7, [])
    assert R._zz_factor_sqf(R(-7)) == (-7, [])

    assert (2 * x + 4).factor_list() == (2, [(x + 2, 1)])
    assert R._zz_factor_sqf(2 * x + 4) == (2, [x + 2])

    f = x**4 + x + 1

    for i in range(20):
        assert f.factor_list() == (1, [(f, 1)])

    f = x**5 - x**3 - x**2 + 1

    assert f.factor_list() == (1, [(x + 1, 1), (x - 1, 2), (x**2 + x + 1, 1)])

    for test in (True, False):
        with using(use_irreducible_in_factor=test):
            assert (x**2 + 2 * x + 2).factor_list() == (1, [(x**2 + 2 * x + 2,
                                                             1)])

            assert (18 * x**2 + 12 * x + 2).factor_list() == (2, [(3 * x + 1,
                                                                   2)])

            f = -9 * x**2 + 1

            assert R._zz_factor_sqf(f) == (-1, [3 * x - 1, 3 * x + 1])
            assert f.factor_list() == (-1, [(3 * x - 1, 1), (3 * x + 1, 1)])

            assert R._zz_factor_sqf(3 * x**4 + 2 * x**3 + 6 * x**2 + 8 * x +
                                    10) == (1, [
                                        3 * x**4 + 2 * x**3 + 6 * x**2 +
                                        8 * x + 10
                                    ])

    with using(use_cyclotomic_factor=False):
        assert R._zz_factor_sqf(-9 * x**2 + 1) == (-1, [3 * x - 1, 3 * x + 1])

    assert (x**3 - 6 * x**2 + 11 * x - 6).factor_list() == (1, [(x - 3, 1),
                                                                (x - 2, 1),
                                                                (x - 1, 1)])

    assert R._zz_factor_sqf(x**3 - 6 * x**2 + 11 * x -
                            6) == (1, [x - 3, x - 2, x - 1])

    assert (3 * x**3 + 10 * x**2 + 13 * x + 10).factor_list() == (1, [
        (x + 2, 1), (3 * x**2 + 4 * x + 5, 1)
    ])

    assert R._zz_factor_sqf(3 * x**3 + 10 * x**2 + 13 * x +
                            10) == (1, [x + 2, 3 * x**2 + 4 * x + 5])

    assert (-x**6 + x**2).factor_list() == (-1, [(x, 2), (x - 1, 1),
                                                 (x + 1, 1), (x**2 + 1, 1)])

    f = (1080 * x**8 + 5184 * x**7 + 2099 * x**6 + 744 * x**5 + 2736 * x**4 -
         648 * x**3 + 129 * x**2 - 324)

    assert f.factor_list() == (1, [(216 * x**4 + 31 * x**2 - 27, 1),
                                   (5 * x**4 + 24 * x**3 + 9 * x**2 + 12, 1)])

    f = (-29802322387695312500000000000000000000 * x**25 +
         2980232238769531250000000000000000 * x**20 +
         1743435859680175781250000000000 * x**15 +
         114142894744873046875000000 * x**10 - 210106372833251953125 * x**5 +
         +95367431640625)

    assert (f.factor_list() == (-95367431640625, [
        (5 * x - 1, 1), (100 * x**2 + 10 * x - 1, 2),
        (625 * x**4 + 125 * x**3 + 25 * x**2 + 5 * x + 1, 1),
        (10000 * x**4 - 3000 * x**3 + 400 * x**2 - 20 * x + 1, 2),
        (10000 * x**4 + 2000 * x**3 + 400 * x**2 + 30 * x + 1, 2)
    ]))

    f = x**10 - 1

    for test in (True, False):
        with using(use_cyclotomic_factor=test):
            f = x**10 - 1

            assert f.factor_list() == (1, [(x - 1, 1), (x + 1, 1),
                                           (x**4 - x**3 + x**2 - x + 1, 1),
                                           (x**4 + x**3 + x**2 + x + 1, 1)])

            f = x**10 + 1

            assert f.factor_list() == (1, [(x**2 + 1, 1),
                                           (x**8 - x**6 + x**4 - x**2 + 1, 1)])
Beispiel #28
0
def test_dmp_eval_tail():
    R, x, y = ring('x y', ZZ)

    assert R.dmp_eval_tail(0, [ZZ(1)]) == 0

    f = 2 * x * y + 3 * x + y + 2

    R0 = R.drop(y)

    assert R.dmp_eval_tail(f, [ZZ(2)]) == 7 * R0.x + 4
    assert R.dmp_eval_tail(f, [ZZ(2), ZZ(2)]) == 18

    R, x, y, z = ring('x y z', ZZ)
    R12 = R.drop(y, z)
    R2 = R.drop(z)

    assert R.dmp_eval_tail(0, [ZZ(1)]) == 0
    assert R.dmp_eval_tail(0, [ZZ(1), ZZ(2)]) == 0

    f = f_polys()[0]

    assert R.dmp_eval_tail(f, []) == f

    assert R.dmp_eval_tail(f, [ZZ(1), ZZ(-17), ZZ(8)]) == 84496
    assert R.dmp_eval_tail(
        f, [ZZ(-17), ZZ(8)]) == -1409 * R12.x**2 + 3 * R12.x + 85902
    assert (R.dmp_eval_tail(f, [ZZ(8)]) == 83 * R2.x**2 * R2.y + 2 * R2.x**2 +
            3 * R2.x + 302 * R2.y**2 + 81 * R2.y + 1)

    f = f_polys()[1]

    assert (R.dmp_eval_tail(f, [ZZ(-17), ZZ(8)]) == -136 * R12.x**3 +
            15699 * R12.x**2 + 9166 * R12.x - 27144)

    f = f_polys()[2]

    assert (R.dmp_eval_tail(f, [ZZ(-12), ZZ(3)]) == -1377 * R12.x**5 -
            702 * R12.x**3 - 1224 * R12.x**2 - 624)

    f = f_polys()[3]

    assert (R.dmp_eval_tail(
        f, [ZZ(-12), ZZ(3)]) == 144 * R12.x**5 + 82 * R12.x**4 -
            5181 * R12.x**3 - 28872 * R12.x**2 - 14868 * R12.x - 540)

    f = f_polys()[4]

    assert (R.dmp_eval_tail(f, [ZZ(25), ZZ(-1)]) == 152587890625 * R12.x**9 +
            9765625 * R12.x**8 - 59605407714843750 * R12.x**7 -
            3839159765625 * R12.x**6 - 1562475 * R12.x**5 +
            9536712644531250 * R12.x**4 + 610349546750 * R12.x**3 -
            4 * R12.x**2 + 24414375000 * R12.x + 1562520)

    f = f_polys()[5]

    assert (R.dmp_eval_tail(f, [ZZ(25), ZZ(-1)]) == -R12.x**3 - 78 * R12.x**2 -
            2028 * R12.x - 17576)

    R, x, y, z, t = ring('x y z t', ZZ)
    R123 = R.drop(y, z, t)

    f = f_polys()[6]

    assert R.dmp_eval_tail(f, [ZZ(0), ZZ(2), ZZ(4)]) == 5040 * R123.x**3 + 4480
def test_dmp_zz_factor():
    R, x = ring('x', ZZ)

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])
    assert R(-7).factor_list() == (-7, [])

    assert (x**2 - 9).factor_list() == (1, [(x - 3, 1), (x + 3, 1)])

    R, x, y = ring('x y', ZZ)

    assert R(0).factor_list() == (0, [])
    assert R(7).factor_list() == (7, [])
    assert R(-7).factor_list() == (-7, [])

    assert x.factor_list() == (1, [(x, 1)])
    assert (4 * x).factor_list() == (4, [(x, 1)])
    assert (4 * x + 2).factor_list() == (2, [(2 * x + 1, 1)])
    assert (x * y + 1).factor_list() == (1, [(x * y + 1, 1)])
    assert (y**2 + 1).factor_list() == (1, [(y**2 + 1, 1)])
    assert (y**2 - 1).factor_list() == (1, [(y - 1, 1), (y + 1, 1)])

    assert (x**2 * y**2 + 6 * x**2 * y + 9 * x**2 - 1).factor_list() == (1, [
        (x * y + 3 * x - 1, 1), (x * y + 3 * x + 1, 1)
    ])
    assert (x**2 * y**2 - 9).factor_list() == (1, [(x * y - 3, 1),
                                                   (x * y + 3, 1)])

    f = (-12 * x**16 * y + 240 * x**12 * y**3 - 768 * x**10 * y**4 +
         1080 * x**8 * y**5 - 768 * x**6 * y**6 + 240 * x**4 * y**7 -
         12 * y**9)

    assert f.factor_list() == (-12, [(y, 1), (x**2 - y, 6),
                                     (x**4 + 6 * x**2 * y + y**2, 1)])

    R, x, y, z = ring('x y z', ZZ)

    assert (x**2 * y**2 * z**2 - 9).factor_list() == (1, [(x * y * z - 3, 1),
                                                          (x * y * z + 3, 1)])

    assert f_1.factor_list() == (1, [(x * y + z + 10, 1), (x + y * z + 20, 1),
                                     (x * z + y + 30, 1)])

    assert f_2.factor_list() == (1, [(x**3 * y + x**3 * z + z - 11, 1),
                                     (x**2 * y**2 + x**2 * z**2 + y + 90, 1)])

    assert f_3.factor_list() == (1, [(x**2 * y**2 + x * z**4 + x + z, 1),
                                     (x**3 + x * y * z + y**2 + y * z**3, 1)])

    assert f_4.factor_list() == (-1, [(x * y**3 + z**2, 1),
                                      (x**3 * y**4 + z**2, 1),
                                      (x**3 * y - z**2 - 3, 1),
                                      (x**2 * z + y**4 * z**2 + 5, 1)])

    assert f_5.factor_list() == (-1, [(x + y - z, 3)])

    assert w_1.factor_list() == (1, [
        (x**2 * y * z**2 + 3 * x * z + 2 * y, 1),
        (4 * x**2 * y + 4 * x**2 * z + x * y * z - 1, 1),
        (x**2 * y**2 - x**2 * z**2 + y - z**2, 1)
    ])

    R, x, y, z, t = ring('x y z t', ZZ)

    assert (x**2 * y**2 * z**2 * t**2 - 9).factor_list() == (1, [
        (x * y * z * t - 3, 1), (x * y * z * t + 3, 1)
    ])

    assert f_6.factor_list() == (1, [
        (47 * x * y + z**3 * t**2 - t**2, 1),
        (45 * x**3 - 9 * y**3 - y**2 + 3 * z**3 + 2 * z * t, 1)
    ])
Beispiel #30
0
def test_heugcd_multivariate_integers():
    R, x, y = ring('x,y', ZZ)

    f, g = 2 * x**2 + 4 * x + 2, x + 1
    assert heugcd(f, g) == (x + 1, 2 * x + 2, 1)

    f, g = x + 1, 2 * x**2 + 4 * x + 2
    assert heugcd(f, g) == (x + 1, 1, 2 * x + 2)

    R, x, y, z, u = ring('x,y,z,u', ZZ)

    f, g = u**2 + 2 * u + 1, 2 * u + 2
    assert heugcd(f, g) == (u + 1, u + 1, 2)

    f, g = z**2 * u**2 + 2 * z**2 * u + z**2 + z * u + z, u**2 + 2 * u + 1
    h, cff, cfg = u + 1, z**2 * u + z**2 + z, u + 1

    assert heugcd(f, g) == (h, cff, cfg)
    assert heugcd(g, f) == (h, cfg, cff)

    R, x, y, z = ring('x,y,z', ZZ)

    f, g, h = R.fateman_poly_F_1()
    H, cff, cfg = heugcd(f, g)

    assert H == h and H * cff == f and H * cfg == g

    R, x, y, z, u, v = ring('x,y,z,u,v', ZZ)

    f, g, h = R.fateman_poly_F_1()
    H, cff, cfg = heugcd(f, g)

    assert H == h and H * cff == f and H * cfg == g

    R, x, y, z, u, v, a, b = ring('x,y,z,u,v,a,b', ZZ)

    f, g, h = R.fateman_poly_F_1()
    H, cff, cfg = heugcd(f, g)

    assert H == h and H * cff == f and H * cfg == g

    R, x, y, z, u, v, a, b, c, d = ring('x,y,z,u,v,a,b,c,d', ZZ)

    f, g, h = R.fateman_poly_F_1()
    H, cff, cfg = heugcd(f, g)

    assert H == h and H * cff == f and H * cfg == g

    R, x, y, z = ring('x,y,z', ZZ)

    f, g, h = R.fateman_poly_F_2()
    H, cff, cfg = heugcd(f, g)

    assert H == h and H * cff == f and H * cfg == g

    f, g, h = R.fateman_poly_F_3()
    H, cff, cfg = heugcd(f, g)

    assert H == h and H * cff == f and H * cfg == g

    R, x, y, z, t = ring('x,y,z,t', ZZ)

    f, g, h = R.fateman_poly_F_3()
    H, cff, cfg = heugcd(f, g)

    assert H == h and H * cff == f and H * cfg == g