Esempio n. 1
0
def test_FracElement():
    Fuv, u, v = field('u v', ZZ)
    Fxyzt, x, y, z, t = field('x y z t', Fuv)

    assert str(x - x) == '0'
    assert str(x - 1) == 'x - 1'
    assert str(x + 1) == 'x + 1'

    assert str(x / 3) == 'x/3'
    assert str(x / z) == 'x/z'
    assert str(x * y / z) == 'x*y/z'
    assert str(x / (z * t)) == 'x/(z*t)'
    assert str(x * y / (z * t)) == 'x*y/(z*t)'

    assert str((x - 1) / y) == '(x - 1)/y'
    assert str((x + 1) / y) == '(x + 1)/y'
    assert str((-x - 1) / y) == '(-x - 1)/y'
    assert str((x + 1) / (y * z)) == '(x + 1)/(y*z)'
    assert str(-y / (x + 1)) == '-y/(x + 1)'
    assert str(y * z / (x + 1)) == 'y*z/(x + 1)'

    assert str(((u + 1) * x * y + 1) /
               ((v - 1) * z - 1)) == '((u + 1)*x*y + 1)/((v - 1)*z - 1)'
    assert str(((u + 1) * x * y + 1) /
               ((v - 1) * z - t * u * v -
                1)) == '((u + 1)*x*y + 1)/((v - 1)*z - u*v*t - 1)'
Esempio n. 2
0
def test_FracElement_diff():
    _, x, y, z = field('x y z', ZZ)

    assert ((x**2 + y) / (z + 1)).diff(x) == 2 * x / (z + 1)

    _, x, y = field('x y', QQ.algebraic_field(I))

    assert ((x - y) / x).diff(x) == y / x**2
Esempio n. 3
0
def test_PolyElement():
    Ruv, u, v = ring('u v', ZZ)
    Rxyz, x, y, z = ring('x y z', Ruv)

    assert str(x - x) == '0'
    assert str(x - 1) == 'x - 1'
    assert str(x + 1) == 'x + 1'

    assert str((u**2 + 3 * u * v + 1) * x**2 * y + u +
               1) == '(u**2 + 3*u*v + 1)*x**2*y + u + 1'
    assert str((u**2 + 3 * u * v + 1) * x**2 * y +
               (u + 1) * x) == '(u**2 + 3*u*v + 1)*x**2*y + (u + 1)*x'
    assert str((u**2 + 3 * u * v + 1) * x**2 * y + (u + 1) * x +
               1) == '(u**2 + 3*u*v + 1)*x**2*y + (u + 1)*x + 1'
    assert str((-u**2 + 3 * u * v - 1) * x**2 * y - (u + 1) * x -
               1) == '-(u**2 - 3*u*v + 1)*x**2*y - (u + 1)*x - 1'

    assert str(-(v**2 + v + 1) * x + 3 * u * v +
               1) == '-(v**2 + v + 1)*x + 3*u*v + 1'
    assert str(-(v**2 + v + 1) * x - 3 * u * v +
               1) == '-(v**2 + v + 1)*x - 3*u*v + 1'

    K, t = field('t', ZZ)
    R, x = ring('x', K)

    assert str(x / t) == '1/t*x'
Esempio n. 4
0
def test_PolyElement():
    Ruv, u, v = ring('u v', ZZ)
    _, x, y, _ = ring('x y z', Ruv)

    assert str(x - x) == '0'
    assert str(x - 1) == 'x - 1'
    assert str(x + 1) == 'x + 1'

    assert str((u**2 + 3 * u * v + 1) * x**2 * y + u +
               1) == '(u**2 + 3*u*v + 1)*x**2*y + u + 1'
    assert str((u**2 + 3 * u * v + 1) * x**2 * y +
               (u + 1) * x) == '(u**2 + 3*u*v + 1)*x**2*y + (u + 1)*x'
    assert str((u**2 + 3 * u * v + 1) * x**2 * y + (u + 1) * x +
               1) == '(u**2 + 3*u*v + 1)*x**2*y + (u + 1)*x + 1'
    assert str((-u**2 + 3 * u * v - 1) * x**2 * y - (u + 1) * x -
               1) == '-(u**2 - 3*u*v + 1)*x**2*y - (u + 1)*x - 1'

    assert str(-(v**2 + v + 1) * x + 3 * u * v +
               1) == '-(v**2 + v + 1)*x + 3*u*v + 1'
    assert str(-(v**2 + v + 1) * x - 3 * u * v +
               1) == '-(v**2 + v + 1)*x - 3*u*v + 1'

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

    assert str(x / t) == '1/t*x'

    assert str(CC.inject(w).convert(I)) == '(0.0 + 1.0j)'
Esempio n. 5
0
def test_FracElement_from_expr():
    x, y, z = symbols('x y z')
    F, X, Y, Z = field((x, y, z), ZZ)

    f = F.convert(1)
    assert f == 1 and isinstance(f, F.dtype)

    f = F.convert(Rational(3, 7))
    assert f == F(3) / 7 and isinstance(f, F.dtype)

    f = F.convert(x)
    assert f == X and isinstance(f, F.dtype)

    f = F.convert(Rational(3, 7) * x)
    assert f == 3 * X / 7 and isinstance(f, F.dtype)

    f = F.convert(1 / x)
    assert f == 1 / X and isinstance(f, F.dtype)

    f = F.convert(x * y * z)
    assert f == X * Y * Z and isinstance(f, F.dtype)

    f = F.convert(x * y / z)
    assert f == X * Y / Z and isinstance(f, F.dtype)

    f = F.convert(x * y * z + x * y + x)
    assert f == X * Y * Z + X * Y + X and isinstance(f, F.dtype)

    f = F.convert((x * y * z + x * y + x) / (x * y + 7))
    assert f == (X * Y * Z + X * Y + X) / (X * Y + 7) and isinstance(
        f, F.dtype)

    f = F.convert(x**3 * y * z + x**2 * y**7 + 1)
    assert f == X**3 * Y * Z + X**2 * Y**7 + 1 and isinstance(f, F.dtype)

    pytest.raises(CoercionFailed, lambda: F.convert(2**x))
    pytest.raises(CoercionFailed, lambda: F.convert(7 * x + sqrt(2)))

    F, X, Y = field((2**x, y), ZZ)
    f = F.convert(2**(2 * x) + 1)
    assert f == X**2 + 1

    # issue sympy/sympy#20985
    F, X = field(x, CC)

    assert F.convert(I / x) == F.convert(CC(0, 1)) / X
Esempio n. 6
0
def test_FracElement():
    F, x, y = field('x y', ZZ)
    g = F.domain.dtype
    assert repr(
        (3 * x**2 * y + 1) /
        (x - y**2)) == (f"FracElement(FractionField({ZZ!r}, (Symbol('x'), "
                        f"Symbol('y')), LexOrder()), [((2, 1), {g(3)!r}), "
                        f'((0, 0), {g(1)!r})], [((1, 0), {g(1)!r}), '
                        f'((0, 2), {g(-1)!r})])')
Esempio n. 7
0
def test_FracElement_copy():
    _, x, y, z = field('x y z', ZZ)

    f = x * y / 3 * z
    g = f.copy()

    assert f == g
    g.numerator[(1, 1, 1)] = 7
    assert f != g
Esempio n. 8
0
def test_FracElement__pos_neg__():
    _, x, y = field('x y', QQ)

    f = (7 * x - 9) / y
    g = (-7 * x + 9) / y

    assert +f == f
    assert +g == g
    assert -f == g
    assert -g == f
Esempio n. 9
0
def test_FracElement_compose():
    _, x, y, z = field('x y z', QQ)

    f = x**3

    assert f.compose([(x, x / (y + z)), (y, z / x)
                      ]) == x**3 / (y**3 + 3 * y**2 * z + 3 * y * z**2 + z**3)

    # issue sympy/sympy#20484
    assert f.compose(
        x, x / (y + z)) == x**3 / (y**3 + 3 * y**2 * z + 3 * y * z**2 + z**3)
Esempio n. 10
0
def test_FracElement():
    F, x, y = field('x,y', ZZ)
    g = F.domain.dtype
    assert repr((3*x**2*y + 1)/(x - y**2)) == ("FracElement(FractionField(%s, (Symbol('x'), "
                                               "Symbol('y')), LexOrder()), [((2, 1), %s), "
                                               '((0, 0), %s)], [((1, 0), %s), '
                                               '((0, 2), %s)])' % (repr(ZZ),
                                                                   repr(g(3)),
                                                                   repr(g(1)),
                                                                   repr(g(1)),
                                                                   repr(g(-1))))
Esempio n. 11
0
def test_FracElement___call__():
    _, x, y, z = field('x y z', ZZ)
    f = (x**2 + 3 * y) / z

    pytest.raises(ValueError, lambda: f(1, 1, 1, 1))

    r = f(1, 1, 1)
    assert r == 4 and not isinstance(r, FracElement)
    pytest.raises(ZeroDivisionError, lambda: f(1, 1, 0))

    Fz = ZZ.inject('z').field
    assert f(1, 1) == 4 / Fz.z
Esempio n. 12
0
def test_FracElement___pow__():
    _, x, y = field('x y', QQ)

    f, g = 1 / x, 1 / y

    assert f**3 == 1 / x**3
    assert g**3 == 1 / y**3

    assert (f * g)**3 == 1 / (x**3 * y**3)
    assert (f * g)**-3 == (x * y)**3

    pytest.raises(ZeroDivisionError, lambda: (x - x)**-3)
Esempio n. 13
0
def test_FracElement___sub__():
    F, x, y = field('x y', QQ)

    f, g = 1 / x, 1 / y
    assert f - g == (-x + y) / (x * y)

    assert x - F.ring.gens[0] == F.ring.gens[0] - x == 0

    F, x, y = field('x y', ZZ)
    assert x - 3 == -(3 - x)
    assert x - QQ(3, 7) == -(QQ(3, 7) - x) == (7 * x - 3) / 7

    Fuv, u, v = field('u v', ZZ)
    _, x, y, *_ = field('x y z t', Fuv)

    f = (u * v - x) / (y - u * v)
    assert dict(f.numerator) == {(1, 0, 0, 0): -1, (0, 0, 0, 0): u * v}
    assert dict(f.denominator) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): -u * v}

    Ruv, u, v = ring('u v', ZZ)
    _, x, y, *_ = field('x y z t', Ruv)

    f = (u * v - x) / (y - u * v)
    assert dict(f.numerator) == {(1, 0, 0, 0): -1, (0, 0, 0, 0): u * v}
    assert dict(f.denominator) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): -u * v}

    Fuv, u, _ = field('u v', ZZ)
    _, x, *_ = ring('x y z', Fuv)

    f = u - x
    assert dict(f) == {(0, 0, 0): u, (1, 0, 0): -Fuv.one}
Esempio n. 14
0
def test_FractionField_from_PolynomialRing():
    R, x, y = ring('x y', QQ)
    F, X, Y = field('x y', ZZ)

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

    assert F.convert(f, R) == 3 * X**2 + 5 * Y**2
    assert F.convert(g, R) == (5 * X**2 + 3 * Y**2) / 15

    RALG, u, v = ring('u v', ALG)
    pytest.raises(CoercionFailed,
                  lambda: F.convert(3 * u**2 + 5 * sqrt(2) * v**2))
Esempio n. 15
0
def test_PolynomialRing_from_FractionField():
    F, x, y = field('x y', ZZ)
    R, X, Y = ring('x y', ZZ)

    f = (x**2 + y**2) / (x + 1)
    g = (x**2 + y**2) / 4
    h = x**2 + y**2

    pytest.raises(CoercionFailed, lambda: R.convert(f, F))
    pytest.raises(CoercionFailed, lambda: R.convert(g, F))
    assert R.convert(h, F) == X**2 + Y**2

    F, x, y = field('x y', QQ)
    R, X, Y = ring('x y', QQ)

    f = (x**2 + y**2) / (x + 1)
    g = (x**2 + y**2) / 4
    h = x**2 + y**2

    pytest.raises(CoercionFailed, lambda: R.convert(f, F))
    assert R.convert(g, F) == X**2 / 4 + Y**2 / 4
    assert R.convert(h, F) == X**2 + Y**2
Esempio n. 16
0
def test_FracElement_as_expr():
    F, x, y, z = field('x y z', ZZ)
    f = (3 * x**2 * y - x * y * z) / (7 * z**3 + 1)

    X, Y, Z = F.symbols
    g = (3 * X**2 * Y - X * Y * Z) / (7 * Z**3 + 1)

    assert f != g
    assert F.to_expr(f) == g

    X, Y, Z = symbols('x y z')
    g = (3 * X**2 * Y - X * Y * Z) / (7 * Z**3 + 1)

    assert f != g
Esempio n. 17
0
def test_FracElement__lt_le_gt_ge__():
    F, x, y = field('x,y', ZZ)

    assert F(1) < 1 / x < 1 / x**2 < 1 / x**3
    assert F(1) <= 1 / x <= 1 / x**2 <= 1 / x**3

    assert -7 / x < 1 / x < 3 / x < y / x < 1 / x**2
    assert -7 / x <= 1 / x <= 3 / x <= y / x <= 1 / x**2

    assert 1 / x**3 > 1 / x**2 > 1 / x > F(1)
    assert 1 / x**3 >= 1 / x**2 >= 1 / x >= F(1)

    assert 1 / x**2 > y / x > 3 / x > 1 / x > -7 / x
    assert 1 / x**2 >= y / x >= 3 / x >= 1 / x >= -7 / x
Esempio n. 18
0
def test_solve_lin_sys_6x6_2():
    ground,  d, r, e, g, i, j, l, o, m, p, q = field('d r e g i j l o m p q', ZZ)
    domain,  c, f, h, k, n, b = ring('c f h k n b', ground)

    eqs = [b + r/d - c/d, c*(1/d + 1/e + 1/g) - f/g - r/d, f*(1/g + 1/i + 1/j) - c/g - h/i, h*(1/i + 1/l + 1/m) - f/i - k/m, k*(1/m + 1/o + 1/p) - h/m - n/p, n*(1/p + 1/q) - k/p]
    sol = {
        b: -((l*q*e*o + l*q*g*o + i*m*q*e + i*l*q*e + i*l*p*e + i*j*o*q + j*e*o*q + g*j*o*q + i*e*o*q + g*i*o*q + e*l*o*p + e*l*m*p + e*l*m*o + e*i*o*p + e*i*m*p + e*i*m*o + e*i*l*o + j*e*o*p + j*e*m*q + j*e*m*p + j*e*m*o + j*l*m*q + j*l*m*p + j*l*m*o + i*j*m*p + i*j*m*o + i*j*l*q + i*j*l*o + i*j*m*q + j*l*o*p + j*e*l*o + g*j*o*p + g*j*m*q + g*j*m*p + i*j*l*p + i*j*o*p + j*e*l*q + j*e*l*p + j*l*o*q + g*j*m*o + g*j*l*q + g*j*l*p + g*j*l*o + g*l*o*p + g*l*m*p + g*l*m*o + g*i*m*o + g*i*o*p + g*i*m*q + g*i*m*p + g*i*l*q + g*i*l*p + g*i*l*o + l*m*q*e + l*m*q*g)*r)/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
        c: (r*e*(l*q*g*o + i*j*o*q + g*j*o*q + g*i*o*q + j*l*m*q + j*l*m*p + j*l*m*o + i*j*m*p + i*j*m*o + i*j*l*q + i*j*l*o + i*j*m*q + j*l*o*p + g*j*o*p + g*j*m*q + g*j*m*p + i*j*l*p + i*j*o*p + j*l*o*q + g*j*m*o + g*j*l*q + g*j*l*p + g*j*l*o + g*l*o*p + g*l*m*p + g*l*m*o + g*i*m*o + g*i*o*p + g*i*m*q + g*i*m*p + g*i*l*q + g*i*l*p + g*i*l*o + l*m*q*g))/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
        f: (r*e*j*(l*q*o + l*o*p + l*m*q + l*m*p + l*m*o + i*o*q + i*o*p + i*m*q + i*m*p + i*m*o + i*l*q + i*l*p + i*l*o))/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
        h: (j*e*r*l*(o*q + o*p + m*q + m*p + m*o))/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
        k: (j*e*r*o*l*(q + p))/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
        n: (j*e*r*o*q*l)/(l*q*d*e*o + l*q*d*g*o + l*q*e*g*o + i*j*d*o*q + i*j*e*o*q + j*d*e*o*q + g*j*d*o*q + g*j*e*o*q + g*i*e*o*q + i*d*e*o*q + g*i*d*o*q + g*i*d*o*p + g*i*d*m*q + g*i*d*m*p + g*i*d*m*o + g*i*d*l*q + g*i*d*l*p + g*i*d*l*o + g*e*l*m*p + g*e*l*o*p + g*j*e*l*q + g*e*l*m*o + g*j*e*m*p + g*j*e*m*o + d*e*l*m*p + d*e*l*m*o + i*d*e*m*p + g*j*e*l*p + g*j*e*l*o + d*e*l*o*p + i*j*d*l*o + i*j*e*o*p + i*j*e*m*q + i*j*d*m*q + i*j*d*m*p + i*j*d*m*o + i*j*d*l*q + i*j*d*l*p + i*j*e*m*p + i*j*e*m*o + i*j*e*l*q + i*j*e*l*p + i*j*e*l*o + i*d*e*m*q + i*d*e*m*o + i*d*e*l*q + i*d*e*l*p + j*d*l*o*p + j*d*e*l*o + g*j*d*o*p + g*j*d*m*q + g*j*d*m*p + g*j*d*m*o + g*j*d*l*q + g*j*d*l*p + g*j*d*l*o + g*j*e*o*p + g*j*e*m*q + g*d*l*o*p + g*d*l*m*p + g*d*l*m*o + j*d*e*m*p + i*d*e*o*p + j*e*o*q*l + j*e*o*p*l + j*e*m*q*l + j*d*e*o*p + j*d*e*m*q + i*j*d*o*p + g*i*e*o*p + j*d*e*m*o + j*d*e*l*q + j*d*e*l*p + j*e*m*p*l + j*e*m*o*l + g*i*e*m*q + g*i*e*m*p + g*i*e*m*o + g*i*e*l*q + g*i*e*l*p + g*i*e*l*o + j*d*l*o*q + j*d*l*m*q + j*d*l*m*p + j*d*l*m*o + i*d*e*l*o + l*m*q*d*e + l*m*q*d*g + l*m*q*e*g),
    }

    assert solve_lin_sys(eqs, domain) == sol
Esempio n. 19
0
def test_solve_lin_sys_6x6_1():
    ground,  d, _, e, g, i, j, l, o, m, p, q = field('d r e g i j l o m p q', ZZ)
    domain,  c, f, h, k, n, b = ring('c f h k n b', ground)

    eqs = [b + q/d - c/d, c*(1/d + 1/e + 1/g) - f/g - q/d, f*(1/g + 1/i + 1/j) - c/g - h/i, h*(1/i + 1/l + 1/m) - f/i - k/m, k*(1/m + 1/o + 1/p) - h/m - n/p, n/p - k/p]
    sol = {
        b: (e*i*l*q + e*i*m*q + e*i*o*q + e*j*l*q + e*j*m*q + e*j*o*q + e*l*m*q + e*l*o*q + g*i*l*q + g*i*m*q + g*i*o*q + g*j*l*q + g*j*m*q + g*j*o*q + g*l*m*q + g*l*o*q + i*j*l*q + i*j*m*q + i*j*o*q + j*l*m*q + j*l*o*q)/(-d*e*i*l - d*e*i*m - d*e*i*o - d*e*j*l - d*e*j*m - d*e*j*o - d*e*l*m - d*e*l*o - d*g*i*l - d*g*i*m - d*g*i*o - d*g*j*l - d*g*j*m - d*g*j*o - d*g*l*m - d*g*l*o - d*i*j*l - d*i*j*m - d*i*j*o - d*j*l*m - d*j*l*o - e*g*i*l - e*g*i*m - e*g*i*o - e*g*j*l - e*g*j*m - e*g*j*o - e*g*l*m - e*g*l*o - e*i*j*l - e*i*j*m - e*i*j*o - e*j*l*m - e*j*l*o),
        c: (-e*g*i*l*q - e*g*i*m*q - e*g*i*o*q - e*g*j*l*q - e*g*j*m*q - e*g*j*o*q - e*g*l*m*q - e*g*l*o*q - e*i*j*l*q - e*i*j*m*q - e*i*j*o*q - e*j*l*m*q - e*j*l*o*q)/(-d*e*i*l - d*e*i*m - d*e*i*o - d*e*j*l - d*e*j*m - d*e*j*o - d*e*l*m - d*e*l*o - d*g*i*l - d*g*i*m - d*g*i*o - d*g*j*l - d*g*j*m - d*g*j*o - d*g*l*m - d*g*l*o - d*i*j*l - d*i*j*m - d*i*j*o - d*j*l*m - d*j*l*o - e*g*i*l - e*g*i*m - e*g*i*o - e*g*j*l - e*g*j*m - e*g*j*o - e*g*l*m - e*g*l*o - e*i*j*l - e*i*j*m - e*i*j*o - e*j*l*m - e*j*l*o),
        f: (-e*i*j*l*q - e*i*j*m*q - e*i*j*o*q - e*j*l*m*q - e*j*l*o*q)/(-d*e*i*l - d*e*i*m - d*e*i*o - d*e*j*l - d*e*j*m - d*e*j*o - d*e*l*m - d*e*l*o - d*g*i*l - d*g*i*m - d*g*i*o - d*g*j*l - d*g*j*m - d*g*j*o - d*g*l*m - d*g*l*o - d*i*j*l - d*i*j*m - d*i*j*o - d*j*l*m - d*j*l*o - e*g*i*l - e*g*i*m - e*g*i*o - e*g*j*l - e*g*j*m - e*g*j*o - e*g*l*m - e*g*l*o - e*i*j*l - e*i*j*m - e*i*j*o - e*j*l*m - e*j*l*o),
        h: (-e*j*l*m*q - e*j*l*o*q)/(-d*e*i*l - d*e*i*m - d*e*i*o - d*e*j*l - d*e*j*m - d*e*j*o - d*e*l*m - d*e*l*o - d*g*i*l - d*g*i*m - d*g*i*o - d*g*j*l - d*g*j*m - d*g*j*o - d*g*l*m - d*g*l*o - d*i*j*l - d*i*j*m - d*i*j*o - d*j*l*m - d*j*l*o - e*g*i*l - e*g*i*m - e*g*i*o - e*g*j*l - e*g*j*m - e*g*j*o - e*g*l*m - e*g*l*o - e*i*j*l - e*i*j*m - e*i*j*o - e*j*l*m - e*j*l*o),
        k: e*j*l*o*q/(d*e*i*l + d*e*i*m + d*e*i*o + d*e*j*l + d*e*j*m + d*e*j*o + d*e*l*m + d*e*l*o + d*g*i*l + d*g*i*m + d*g*i*o + d*g*j*l + d*g*j*m + d*g*j*o + d*g*l*m + d*g*l*o + d*i*j*l + d*i*j*m + d*i*j*o + d*j*l*m + d*j*l*o + e*g*i*l + e*g*i*m + e*g*i*o + e*g*j*l + e*g*j*m + e*g*j*o + e*g*l*m + e*g*l*o + e*i*j*l + e*i*j*m + e*i*j*o + e*j*l*m + e*j*l*o),
        n: e*j*l*o*q/(d*e*i*l + d*e*i*m + d*e*i*o + d*e*j*l + d*e*j*m + d*e*j*o + d*e*l*m + d*e*l*o + d*g*i*l + d*g*i*m + d*g*i*o + d*g*j*l + d*g*j*m + d*g*j*o + d*g*l*m + d*g*l*o + d*i*j*l + d*i*j*m + d*i*j*o + d*j*l*m + d*j*l*o + e*g*i*l + e*g*i*m + e*g*i*o + e*g*j*l + e*g*j*m + e*g*j*o + e*g*l*m + e*g*l*o + e*i*j*l + e*i*j*m + e*i*j*o + e*j*l*m + e*j*l*o),
    }

    assert solve_lin_sys(eqs, domain) == sol
Esempio n. 20
0
def test_FracElement_as_expr():
    F, x, y, z = field('x,y,z', ZZ)
    f = (3 * x**2 * y - x * y * z) / (7 * z**3 + 1)

    X, Y, Z = F.symbols
    g = (3 * X**2 * Y - X * Y * Z) / (7 * Z**3 + 1)

    assert f != g
    assert f.as_expr() == g

    X, Y, Z = symbols('x,y,z')
    g = (3 * X**2 * Y - X * Y * Z) / (7 * Z**3 + 1)

    assert f != g
    assert f.as_expr(X, Y, Z) == g

    pytest.raises(ValueError, lambda: f.as_expr(X))
Esempio n. 21
0
def test_FracElement___truediv__():
    F, x, y = field('x y', QQ)

    f, g = 1 / x, 1 / y
    assert f / g == y / x

    assert x / F.ring.gens[0] == F.ring.gens[0] / x == 1

    F, x, y = field('x y', ZZ)
    assert x * 3 == 3 * x
    assert x / QQ(3, 7) == (QQ(3, 7) / x)**-1 == 7 * x / 3

    pytest.raises(ZeroDivisionError, lambda: x / 0)
    pytest.raises(ZeroDivisionError, lambda: 1 / (x - x))
    pytest.raises(ZeroDivisionError, lambda: x / (x - x))

    Fuv, u, v = field('u v', ZZ)
    _, x, y, *_ = field('x y z t', Fuv)

    f = (u * v) / (x * y)
    assert dict(f.numerator) == {(0, 0, 0, 0): u * v}
    assert dict(f.denominator) == {(1, 1, 0, 0): 1}

    g = (x * y) / (u * v)
    assert dict(g.numerator) == {(1, 1, 0, 0): 1}
    assert dict(g.denominator) == {(0, 0, 0, 0): u * v}

    Ruv, u, v = ring('u v', ZZ)
    _, x, y, *_ = field('x y z t', Ruv)

    f = (u * v) / (x * y)
    assert dict(f.numerator) == {(0, 0, 0, 0): u * v}
    assert dict(f.denominator) == {(1, 1, 0, 0): 1}

    g = (x * y) / (u * v)
    assert dict(g.numerator) == {(1, 1, 0, 0): 1}
    assert dict(g.denominator) == {(0, 0, 0, 0): u * v}

    Fuv, u, _ = field('u v', ZZ)
    _, x, *_ = ring('x y z', Fuv)

    pytest.raises(TypeError, lambda: u / x)
Esempio n. 22
0
def test_FractionField___hash__():
    F, x, y, z = field('x,y,z', QQ)
    assert hash(F)
Esempio n. 23
0
def test_FracElement_to_poly():
    _, x, y = field('x y', ZZ)
    pytest.raises(ValueError, (x / y).to_poly)
Esempio n. 24
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
Esempio n. 25
0
def test_FracElement_to_poly():
    F, x, y = field('x y', ZZ)
    pytest.raises(ValueError, lambda: (x / y).to_poly())
Esempio n. 26
0
def test_FracElement___hash__():
    _, x, y, z = field('x y z', QQ)
    assert hash(x * y / z)
Esempio n. 27
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)
Esempio n. 28
0
def test_FractionField_convert():
    F, X, Y = field('x y', QQ)
    assert F.convert(QQ_python(1, 3)) == F.one / 3
    assert F.convert(RR(1)) == F.one
Esempio n. 29
0
def test_FractionField___eq__():
    assert field('x y z', QQ)[0] == field('x y z', QQ)[0]
    assert field('x y z', QQ)[0] is field('x y z', QQ)[0]

    assert field('x y z', QQ)[0] != field('x y z', ZZ)[0]
    assert field('x y z', QQ)[0] is not field('x y z', ZZ)[0]

    assert field('x y z', ZZ)[0] != field('x y z', QQ)[0]
    assert field('x y z', ZZ)[0] is not field('x y z', QQ)[0]

    assert field('x y z', QQ)[0] != field('x y', QQ)[0]
    assert field('x y z', QQ)[0] is not field('x y', QQ)[0]

    assert field('x y', QQ)[0] != field('x y z', QQ)[0]
    assert field('x y', QQ)[0] is not field('x y z', QQ)[0]
Esempio n. 30
0
def test_FractionField___hash__():
    F, *_ = field('x y z', QQ)
    assert hash(F)