Ejemplo n.º 1
0
def test_PolyRing():
    assert srepr(ring("x", ZZ, lex)[0]) == "PolyRing((Symbol('x'),), ZZ, lex)"
    assert srepr(
        ring("x,y", QQ,
             grlex)[0]) == "PolyRing((Symbol('x'), Symbol('y')), QQ, grlex)"
    assert srepr(
        ring("x,y,z", ZZ["t"], lex)
        [0]) == "PolyRing((Symbol('x'), Symbol('y'), Symbol('z')), ZZ[t], lex)"
Ejemplo n.º 2
0
def test_PolyRing():
    assert str(ring("x", ZZ,
                    lex)[0]) == "Polynomial ring in x over ZZ with lex order"
    assert str(
        ring("x,y", QQ,
             grlex)[0]) == "Polynomial ring in x, y over QQ with grlex order"
    assert str(
        ring("x,y,z", ZZ["t"],
             lex)[0]) == "Polynomial ring in x, y, z over ZZ[t] with lex order"
Ejemplo n.º 3
0
def test_PolyRing():
    sT(ring("x", ZZ, lex)[0], "PolyRing((Symbol('x'),), "
                              "%s, LexOrder())" % repr(ZZ))
    sT(ring("x,y", QQ, grlex)[0], "PolyRing((Symbol('x'), Symbol('y')), "
                                  "%s, GradedLexOrder())" % repr(QQ))
    sT(ring("x,y,z", ZZ["t"], lex)[0],
            "PolyRing((Symbol('x'), Symbol('y'), Symbol('z')), "
            "PolynomialRing(PolyRing((Symbol('t'),), "
            "%s, LexOrder())), LexOrder())" % repr(ZZ))
Ejemplo n.º 4
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))
Ejemplo n.º 5
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))
Ejemplo n.º 6
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"
Ejemplo n.º 7
0
def test_PolyElement():
    R, x, y = ring("x,y", ZZ)
    g = R.domain.dtype
    assert srepr(3 * x**2 * y +
                 1) == ("PolyElement(PolyRing((Symbol('x'), "
                        "Symbol('y')), ZZ, lex), [((2, 1), %s), "
                        "((0, 0), %s)])" % (repr(g(3)), repr(g(1))))
Ejemplo n.º 8
0
def test_PolyElement():
    R, x, y = ring("x,y", ZZ)
    g = R.domain.dtype
    assert repr(3 * x**2 * y +
                1) == ("PolyElement(PolynomialRing(%s, (Symbol('x'), "
                       "Symbol('y')), LexOrder()), [((2, 1), "
                       "%s), ((0, 0), %s)])" %
                       (repr(ZZ), repr(g(3)), repr(g(1))))
Ejemplo n.º 9
0
def test_PolyElement():
    R, x, y = ring("x,y", ZZ)
    g = R.domain.dtype
    assert repr(3*x**2*y + 1) == ("PolyElement(PolynomialRing(%s, (Symbol('x'), "
                                  "Symbol('y')), LexOrder()), [((2, 1), "
                                  "%s), ((0, 0), %s)])" % (repr(ZZ),
                                                           repr(g(3)),
                                                           repr(g(1))))
Ejemplo n.º 10
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'
Ejemplo n.º 11
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
Ejemplo n.º 12
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))
    assert R.convert(g, F) == X**2/4 + Y**2/4
    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
Ejemplo n.º 13
0
def test_Domain_convert():
    assert QQ.convert(10e-52) == QQ(
        1684996666696915,
        1684996666696914987166688442938726917102321526408785780068975640576)

    R, x = ring("x", ZZ)
    assert ZZ.convert(x - x) == 0
    assert ZZ.convert(x - x, R) == 0

    F3 = FF(3)
    assert F3.convert(Float(2.0)) == F3.dtype(2)
    assert F3.convert(PythonRational(2, 1)) == F3.dtype(2)
    pytest.raises(CoercionFailed, lambda: F3.convert(PythonRational(1, 2)))
    assert F3.convert(2.0) == F3.dtype(2)
    pytest.raises(CoercionFailed, lambda: F3.convert(2.1))

    assert RR.convert(CC(1)) == RR(1)
    pytest.raises(CoercionFailed, lambda: RR.convert(CC(1, 2)))

    assert QQ.convert(ALG(1), ALG) == QQ(1)
    pytest.raises(CoercionFailed, lambda: QQ.convert(ALG([1, 1]), ALG))

    assert ZZ.convert(ALG(1), ALG) == ZZ(1)
    pytest.raises(CoercionFailed, lambda: ZZ.convert(ALG([1, 1]), ALG))

    assert EX.convert(ALG([1, 1]), ALG) == sqrt(2) + sqrt(3) + 1

    ALG2 = QQ.algebraic_field(sqrt(2))
    a2 = ALG2.convert(sqrt(2))
    a = ALG.convert(a2, ALG2)
    assert a.rep.to_dense() == [QQ(1, 2), 0, -QQ(9, 2), 0]
    assert RR.convert(a) == RR(1.4142135623730951)
    assert CC.convert(a) == CC(1.4142135623730951)

    assert ZZ_python.convert(3.0) == ZZ_python.dtype(3)
    pytest.raises(CoercionFailed, lambda: ZZ_python.convert(3.2))

    assert CC.convert(QQ_python(1, 2)) == CC(0.5)
    CC01 = ComplexField(tol=0.1)
    assert CC.convert(CC01(0.3)) == CC(0.3)

    assert RR.convert(complex(2 + 0j)) == RR(2)
    pytest.raises(CoercionFailed, lambda: RR.convert(complex(2 + 3j)))

    assert ALG.convert(EX(sqrt(2)), EX) == ALG.from_expr(sqrt(2))
    pytest.raises(CoercionFailed, lambda: ALG.convert(EX(sqrt(5)), EX))

    pytest.raises(CoercionFailed, lambda: ALG2.convert(ALG.unit))
Ejemplo n.º 14
0
def test_Domain_convert():
    assert QQ.convert(10e-52) == QQ(1684996666696915, 1684996666696914987166688442938726917102321526408785780068975640576)

    R, x = ring("x", ZZ)
    assert ZZ.convert(x - x) == 0
    assert ZZ.convert(x - x, R) == 0

    F3 = FF(3)
    assert F3.convert(Float(2.0)) == F3.dtype(2)
    assert F3.convert(PythonRational(2, 1)) == F3.dtype(2)
    pytest.raises(CoercionFailed, lambda: F3.convert(PythonRational(1, 2)))
    assert F3.convert(2.0) == F3.dtype(2)
    pytest.raises(CoercionFailed, lambda: F3.convert(2.1))

    assert RR.convert(CC(1)) == RR(1)
    pytest.raises(CoercionFailed, lambda: RR.convert(CC(1, 2)))

    assert QQ.convert(ALG.new(1), ALG) == QQ(1)
    pytest.raises(CoercionFailed, lambda: QQ.convert(ALG.new([1, 1]), ALG))

    assert ZZ.convert(ALG.new(1), ALG) == ZZ(1)
    pytest.raises(CoercionFailed, lambda: ZZ.convert(ALG.new([1, 1]), ALG))

    assert EX.convert(ALG.new([1, 1]), ALG) == sqrt(2) + sqrt(3) + 1

    ALG2 = QQ.algebraic_field(sqrt(2))
    a2 = ALG2.convert(sqrt(2))
    a = ALG.convert(a2, ALG2)
    assert a.rep.to_dense() == [QQ(1, 2), 0, -QQ(9, 2), 0]

    assert ZZ_python.convert(3.0) == ZZ_python.dtype(3)
    pytest.raises(CoercionFailed, lambda: ZZ_python.convert(3.2))

    assert CC.convert(QQ_python(1, 2)) == CC(0.5)
    CC01 = ComplexField(tol=0.1)
    assert CC.convert(CC01(0.3)) == CC(0.3)

    assert RR.convert(complex(2 + 0j)) == RR(2)
    pytest.raises(CoercionFailed, lambda: RR.convert(complex(2 + 3j)))

    assert ALG.convert(EX(sqrt(2)), EX) == ALG.from_expr(sqrt(2))
    pytest.raises(CoercionFailed, lambda: ALG.convert(EX(sqrt(5)), EX))

    pytest.raises(CoercionFailed, lambda: ALG2.convert(ALG.unit))