Пример #1
0
def test_FracElement___mul__():
    F,  x, y = field("x,y", QQ)

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

    assert x*F.ring.gens[0] == F.ring.gens[0]*x == x**2

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

    Fuv,  u, v = field("u,v", ZZ)
    Fxyzt,  x, y, z, t = field("x,y,z,t", Fuv)

    f = ((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)
    assert dict(f.numer) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
    assert dict(f.denom) == {(0, 0, 1, 0): v - 1, (0, 0, 0, 1): -u*v, (0, 0, 0, 0): -1}

    Ruv,  u, v = ring("u,v", ZZ)
    Fxyzt,  x, y, z, t = field("x,y,z,t", Ruv)

    f = ((u + 1)*x*y + 1)/((v - 1)*z - t*u*v - 1)
    assert dict(f.numer) == {(1, 1, 0, 0): u + 1, (0, 0, 0, 0): 1}
    assert dict(f.denom) == {(0, 0, 1, 0): v - 1, (0, 0, 0, 1): -u*v, (0, 0, 0, 0): -1}
Пример #2
0
def test_FracElement_eval():
    F,  x, y, z = field("x,y,z", ZZ)
    Fyz = field("y,z", ZZ)[0]
    f = (x**2 + 3*y)/z

    assert f.eval(x, 0) == 3*Fyz.y/Fyz.z
    pytest.raises(ZeroDivisionError, lambda: f.eval(z, 0))
Пример #3
0
def test_FracElement___add__():
    F,  x, y = field("x,y", QQ)

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

    z = symbols('z')
    pytest.raises(TypeError, lambda: x + z)

    assert x + F.ring.gens[0] == F.ring.gens[0] + x == 2*x

    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)
    Fxyzt,  x, y, z, t = field("x,y,z,t", Fuv)

    f = (u*v + x)/(y + u*v)
    assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u*v}
    assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u*v}

    Ruv,  u, v = ring("u,v", ZZ)
    Fxyzt,  x, y, z, t = field("x,y,z,t", Ruv)

    f = (u*v + x)/(y + u*v)
    assert dict(f.numer) == {(1, 0, 0, 0): 1, (0, 0, 0, 0): u*v}
    assert dict(f.denom) == {(0, 1, 0, 0): 1, (0, 0, 0, 0): u*v}
Пример #4
0
def test_FracElement_diff():
    F,  x, y, z = field("x,y,z", ZZ)

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

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

    assert ((x - y)/x).diff(x) == y/x**2
Пример #5
0
def test_PolyElement___sub__():
    Rt, t = ring("t", ZZ)
    Ruv,  u, v = ring("u,v", ZZ)
    Rxyz,  x, y, z = ring("x,y,z", Ruv)

    assert u - x == -x + u
    assert (x + u) - 2*u == x - u

    assert dict(x - 3*y) == {(1, 0, 0): 1, (0, 1, 0): -3}

    assert dict(-u + x) == dict(x - u) == {(1, 0, 0): 1, (0, 0, 0): -u}
    assert dict(-u + x*y) == dict(x*y - u) == {(1, 1, 0): 1, (0, 0, 0): -u}
    assert dict(-u + x*y + z) == dict(x*y + z - u) == {(1, 1, 0): 1, (0, 0, 1): 1, (0, 0, 0): -u}

    assert dict(-u*x + x) == dict(x - u*x) == {(1, 0, 0): -u + 1}
    assert dict(-u*x + x*y) == dict(x*y - u*x) == {(1, 1, 0): 1, (1, 0, 0): -u}
    assert dict(-u*x + x*y + z) == dict(x*y + z - u*x) == {(1, 1, 0): 1, (0, 0, 1): 1, (1, 0, 0): -u}

    pytest.raises(TypeError, lambda: t - x)
    pytest.raises(TypeError, lambda: x - t)
    pytest.raises(TypeError, lambda: t - u)
    pytest.raises(TypeError, lambda: u - t)

    Fuv,  u, v = field("u,v", ZZ)
    Rxyz,  x, y, z = ring("x,y,z", Fuv)

    assert dict(-u + x) == dict(x - u) == {(1, 0, 0): 1, (0, 0, 0): -u}

    Rxyz,  x, y, z = ring("x,y,z", EX)

    assert dict(-EX(pi) + x*y*z) == dict(x*y*z - EX(pi)) == {(1, 1, 1): EX(1), (0, 0, 0): -EX(pi)}
Пример #6
0
def test_PolyElement_cancel():
    R,  x, y = ring("x,y", ZZ)

    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)

    Fx, x = field("x", ZZ)
    Rt, 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)
Пример #7
0
def test_FracElement_copy():
    F, x, y, z = field("x,y,z", ZZ)

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

    assert f == g
    g.numer[(1, 1, 1)] = 7
    assert f != g
Пример #8
0
def test_FracElement__pos_neg__():
    F,  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
Пример #9
0
def test_FracElement_subs():
    F,  x, y, z = field("x,y,z", ZZ)
    f = (x**2 + 3*y)/z

    assert f.subs({(x, 0)}) == 3*y/z
    assert f.subs({x: 0}) == 3*y/z
    pytest.raises(ValueError, lambda: f.subs(object()))
    pytest.raises(ZeroDivisionError, lambda: f.subs({z: 0}))

    assert f.subs([(x, 0), (y, 1)]) == 3/z
Пример #10
0
def test_FracElement___call__():
    F,  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.frac_field("z")
    assert f(1, 1) == 4/Fz.z
Пример #11
0
def test_FracElement___pow__():
    F,  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)
Пример #12
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)
    Fxyzt,  x, y, z, t = field("x,y,z,t", Fuv)

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

    Ruv,  u, v = ring("u,v", ZZ)
    Fxyzt,  x, y, z, t = field("x,y,z,t", Ruv)

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

    Fuv,  u, v = field("u,v", ZZ)
    Rxyz,  x, y, z = ring("x,y,z", Fuv)

    f = u - x
    assert dict(f) == {(0, 0, 0): u, (1, 0, 0): -Fuv.one}
Пример #13
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
Пример #14
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
Пример #15
0
def test_PolyElement_sturm():
    F, t = field("t", ZZ)
    _, x = ring("x", F)

    f = 1024/(15625*t**8)*x**5 - 4096/(625*t**8)*x**4 + 32/(15625*t**4)*x**3 - 128/(625*t**4)*x**2 + F(1)/62500*x - F(1)/625

    assert f.sturm() == [
        x**3 - 100*x**2 + t**4/64*x - 25*t**4/16,
        3*x**2 - 200*x + t**4/64,
        (-t**4/96 + F(20000)/9)*x + 25*t**4/18,
        (-9*t**12 - 11520000*t**8 - 3686400000000*t**4)/(576*t**8 - 245760000*t**4 + 26214400000000),
    ]

    R, x, y = ring("x,y", ZZ)
    pytest.raises(MultivariatePolynomialError, lambda: (x + y).sturm())
Пример #16
0
def test_PolyElement___mul__():
    Rt, t = ring("t", ZZ)
    Ruv,  u, v = ring("u,v", ZZ)
    Rxyz,  x, y, z = ring("x,y,z", Ruv)

    assert dict(u*x) == dict(x*u) == {(1, 0, 0): u}

    assert dict(2*u*x + z) == dict(x*2*u + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
    assert dict(u*2*x + z) == dict(2*x*u + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
    assert dict(2*u*x + z) == dict(x*2*u + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}
    assert dict(u*x*2 + z) == dict(x*u*2 + z) == {(1, 0, 0): 2*u, (0, 0, 1): 1}

    assert dict(2*u*x*y + z) == dict(x*y*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
    assert dict(u*2*x*y + z) == dict(2*x*y*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
    assert dict(2*u*x*y + z) == dict(x*y*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
    assert dict(u*x*y*2 + z) == dict(x*y*u*2 + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}

    assert dict(2*u*y*x + z) == dict(y*x*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
    assert dict(u*2*y*x + z) == dict(2*y*x*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
    assert dict(2*u*y*x + z) == dict(y*x*2*u + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}
    assert dict(u*y*x*2 + z) == dict(y*x*u*2 + z) == {(1, 1, 0): 2*u, (0, 0, 1): 1}

    assert dict(3*u*(x + y) + z) == dict((x + y)*3*u + z) == {(1, 0, 0): 3*u, (0, 1, 0): 3*u, (0, 0, 1): 1}

    pytest.raises(TypeError, lambda: t*x + z)
    pytest.raises(TypeError, lambda: x*t + z)
    pytest.raises(TypeError, lambda: t*u + z)
    pytest.raises(TypeError, lambda: u*t + z)

    Fuv,  u, v = field("u,v", ZZ)
    Rxyz,  x, y, z = ring("x,y,z", Fuv)

    assert dict(u*x) == dict(x*u) == {(1, 0, 0): u}

    Rxyz,  x, y, z = ring("x,y,z", EX)

    assert dict(EX(pi)*x*y*z) == dict(x*y*z*EX(pi)) == {(1, 1, 1): EX(pi)}

    assert (x + 2*y).mul_ground(0) == Rxyz.zero

    R, x, y = ring("x,y", ZZ)
    p = x + y**2
    p1 = p._imul_num(3)
    assert p == p1 and p1 == 3*x + 3*y**2
    p2 = p._imul_num(0)
    assert p == p2 and p2 == R.zero
Пример #17
0
def test_PolyElement___add__():
    Rt, t = ring("t", ZZ)
    Ruv,  u, v = ring("u,v", ZZ)
    Rxyz,  x, y, z = ring("x,y,z", Ruv)

    assert dict(+x) == dict(x)

    assert dict(x + 3*y) == {(1, 0, 0): 1, (0, 1, 0): 3}

    assert dict(u + x) == dict(x + u) == {(1, 0, 0): 1, (0, 0, 0): u}
    assert dict(u + x*y) == dict(x*y + u) == {(1, 1, 0): 1, (0, 0, 0): u}
    assert dict(u + x*y + z) == dict(x*y + z + u) == {(1, 1, 0): 1, (0, 0, 1): 1, (0, 0, 0): u}

    assert dict(u*x + x) == dict(x + u*x) == {(1, 0, 0): u + 1}
    assert dict(u*x + x*y) == dict(x*y + u*x) == {(1, 1, 0): 1, (1, 0, 0): u}
    assert dict(u*x + x*y + z) == dict(x*y + z + u*x) == {(1, 1, 0): 1, (0, 0, 1): 1, (1, 0, 0): u}

    pytest.raises(TypeError, lambda: t + x)
    pytest.raises(TypeError, lambda: x + t)
    pytest.raises(TypeError, lambda: t + u)
    pytest.raises(TypeError, lambda: u + t)

    Fuv,  u, v = field("u,v", ZZ)
    Rxyz,  x, y, z = ring("x,y,z", Fuv)

    assert u + (x - u) == x
    assert dict(u + x) == dict(x + u) == {(1, 0, 0): 1, (0, 0, 0): u}

    Rxyz,  x, y, z = ring("x,y,z", EX)

    assert dict(EX(pi) + x*y*z) == dict(x*y*z + EX(pi)) == {(1, 1, 1): EX(1), (0, 0, 0): EX(pi)}

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

    p = x**4 + 2*y
    m = (1, 2)
    p1 = p._iadd_monom((m, 5))
    assert p == p1 and p1 == x**4 + 5*x*y**2 + 2*y

    p2 = p._iadd_monom(((0, 1), 2))
    assert p == p2 and p2 == x**4 + 5*x*y**2 + 4*y

    p3 = p._iadd_monom(((0, 1), -4))
    assert p == p3 and p3 == x**4 + 5*x*y**2

    assert x._iadd_monom((m, 5)) == 5*x*y**2 + x
Пример #18
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))
Пример #19
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)
    Fxyzt,  x, y, z, t = field("x,y,z,t", Fuv)

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

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

    Ruv,  u, v = ring("u,v", ZZ)
    Fxyzt,  x, y, z, t = field("x,y,z,t", Ruv)

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

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

    Fuv,  u, v = field("u,v", ZZ)
    Rxyz,  x, y, z = ring("x,y,z", Fuv)

    pytest.raises(TypeError, lambda: u/x)
Пример #20
0
def test_FracElement_diff():
    F, x, y, z = field("x,y,z", ZZ)

    assert ((x**2 + y) / (z + 1)).diff(x) == 2 * x / (z + 1)
Пример #21
0
def test_FracElement___hash__():
    F, x, y, z = field("x,y,z", QQ)
    assert hash(x * y / z)
Пример #22
0
def test_FracField___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]
Пример #23
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]
Пример #24
0
def test_FracElement___hash__():
    F, x, y, z = field("x,y,z", QQ)
    assert hash(x*y/z)
Пример #25
0
def test_FracElement_to_poly():
    F, x, y = field("x y", ZZ)
    pytest.raises(ValueError, lambda: (x / y).to_poly())
Пример #26
0
def test_FracElement_to_poly():
    F, x, y = field("x y", ZZ)
    pytest.raises(ValueError, lambda: (x/y).to_poly())
Пример #27
0
def test_FracField___hash__():
    F, x, y, z = field("x,y,z", QQ)
    assert hash(F)
Пример #28
0
def test_FracElement_subs():
    F, x, y, z = field("x,y,z", ZZ)
    f = (x**2 + 3 * y) / z

    assert f.subs(x, 0) == 3 * y / z
    pytest.raises(ZeroDivisionError, lambda: f.subs(z, 0))
Пример #29
0
def test_FractionField___hash__():
    F, x, y, z = field("x,y,z", QQ)
    assert hash(F)