Exemplo n.º 1
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 + 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}

    raises(TypeError, lambda: t + x)
    raises(TypeError, lambda: x + t)
    raises(TypeError, lambda: t + u)
    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)}
Exemplo n.º 2
0
def test_dup_isolate_real_roots_list():
    assert dup_isolate_real_roots_list([[1, 1,0], [1,0]], ZZ) == \
        [((-QQ(1), -QQ(1)), {0: 1}), ((QQ(0), QQ(0)), {0: 1, 1: 1})]
    assert dup_isolate_real_roots_list([[1,-1,0], [1,0]], ZZ) == \
        [((QQ(0), QQ(0)), {0: 1, 1: 1}), ((QQ(1), QQ(1)), {0: 1})]

    f = dup_from_raw_dict({5: ZZ(1), 0: -ZZ(200)}, ZZ)
    g = dup_from_raw_dict({5: ZZ(1), 0: -ZZ(201)}, ZZ)

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((QQ(75,26), QQ(101,35)), {0: 1}), ((QQ(283,98), QQ(26,9)), {1: 1})]

    f = dup_from_raw_dict({5: -QQ(1,200), 0: QQ(1)}, QQ)
    g = dup_from_raw_dict({5: -QQ(1,201), 0: QQ(1)}, QQ)

    assert dup_isolate_real_roots_list([f, g], QQ) == \
        [((QQ(75,26), QQ(101,35)), {0: 1}), ((QQ(283,98), QQ(26,9)), {1: 1})]

    assert dup_isolate_real_roots_list([[1,1], [1,2], [1,-1], [1,1], [1,-1], [1,-1]], ZZ) == \
        [((-QQ(2), -QQ(2)), {1: 1}), ((-QQ(1), -QQ(1)), {0: 1, 3: 1}), ((QQ(1), QQ(1)), {2: 1, 4: 1, 5: 1})]

    assert dup_isolate_real_roots_list([[1,1], [1,2], [1,-1], [1,1], [1,-1], [1,2]], ZZ) == \
        [((-QQ(2), -QQ(2)), {1: 1, 5: 1}), ((-QQ(1), -QQ(1)), {0: 1, 3: 1}), ((QQ(1), QQ(1)), {2: 1, 4: 1})]

    f, g = [1, 0, -4, 0, 4], [1, -1]

    assert dup_isolate_real_roots_list([f, g], ZZ, inf=QQ(7,4)) == []
    assert dup_isolate_real_roots_list([f, g], ZZ, inf=QQ(7,5)) == [((QQ(7,5), QQ(3,2)), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=QQ(7,5)) == [((-2, -1), {0: 2}), ((1, 1), {1: 1})]
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=QQ(7,4)) == [((-2, -1), {0: 2}), ((1, 1), {1: 1}), ((1, QQ(3,2)), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=-QQ(7,4)) == []
    assert dup_isolate_real_roots_list([f, g], ZZ, sup=-QQ(7,5)) == [((-QQ(3,2), -QQ(7,5)), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, inf=-QQ(7,5)) == [((1, 1), {1: 1}), ((1, 2), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, inf=-QQ(7,4)) == [((-QQ(3,2), -1), {0: 2}), ((1, 1), {1: 1}), ((1, 2), {0: 2})]

    f, g = [2, 0, -1], [1, 0, -2]

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

    f, g = [1, 0, -2], [1, -1, -2, 2]

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((-QQ(2), -QQ(1)), {1: 1, 0: 1}), ((QQ(1), QQ(1)), {1: 1}), ((QQ(1), QQ(2)), {1: 1, 0: 1})]

    f, g = [1, 0, -2, 0], [1, -1, -2, 2, 0, 0]

    assert dup_isolate_real_roots_list([f, g], ZZ) == \
        [((-QQ(2), -QQ(1)), {1: 1, 0: 1}), ((QQ(0), QQ(0)), {0: 1, 1: 2}), ((QQ(1), QQ(1)), {1: 1}), ((QQ(1), QQ(2)), {1: 1, 0: 1})]

    f, g = [1, -3, -1, 11, -8, -8, 12, -4, 0, 0], [1, -2, 3, -4, 2, 0]

    assert dup_isolate_real_roots_list([f, g], ZZ, basis=False) == \
        [((-2, -1), {0: 2}), ((0, 0), {0: 2, 1: 1}), ((1, 1), {0: 3, 1: 2}), ((1, 2), {0: 2})]
    assert dup_isolate_real_roots_list([f, g], ZZ, basis=True) == \
        [((-2, -1), {0: 2}, [1, 0, -2]), ((0, 0), {0: 2, 1: 1}, [1, 0]), ((1, 1), {0: 3, 1: 2}, [1, -1]), ((1, 2), {0: 2}, [1, 0, -2])]

    raises(DomainError, lambda: dup_isolate_real_roots_list([[EX(1), EX(2)]], EX))
Exemplo n.º 3
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 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}

    raises(TypeError, lambda: t - x)
    raises(TypeError, lambda: x - t)
    raises(TypeError, lambda: t - u)
    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)}
Exemplo n.º 4
0
def test_dmp_lift():
    q = [QQ(1, 1), QQ(0, 1), QQ(1, 1)]

    f = [
        ANP([QQ(1, 1)], q, QQ),
        ANP([], q, QQ),
        ANP([], q, QQ),
        ANP([QQ(1, 1), QQ(0, 1)], q, QQ),
        ANP([QQ(17, 1), QQ(0, 1)], q, QQ),
    ]

    assert dmp_lift(f, 0, QQ.algebraic_field(I)) == [
        QQ(1),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(2),
        QQ(0),
        QQ(578),
        QQ(0),
        QQ(0),
        QQ(0),
        QQ(1),
        QQ(0),
        QQ(-578),
        QQ(0),
        QQ(83521),
    ]

    raises(DomainError, lambda: dmp_lift([EX(1), EX(2)], 0, EX))
Exemplo n.º 5
0
def test_dup_real_imag():
    assert dup_real_imag([], ZZ) == ([[]], [[]])
    assert dup_real_imag([1], ZZ) == ([[1]], [[]])

    assert dup_real_imag([1,1], ZZ) == ([[1], [1]], [[1,0]])
    assert dup_real_imag([1,2], ZZ) == ([[1], [2]], [[1,0]])

    assert dup_real_imag([1,2,3], ZZ) == ([[1], [2], [-1,0,3]], [[2,0], [2,0]])

    raises(DomainError, lambda: dup_real_imag([EX(1), EX(2)], EX))
Exemplo n.º 6
0
def test_composite_option():
    assert construct_domain({(1,): sin(y)}, composite=False) == \
        (EX, {(1,): EX(sin(y))})

    assert construct_domain({(1,): y}, composite=False) == \
        (EX, {(1,): EX(y)})

    assert construct_domain({(1, 1): 1}, composite=False) == \
        (ZZ, {(1, 1): 1})

    assert construct_domain({(1, 0): y}, composite=False) == \
        (EX, {(1, 0): EX(y)})
Exemplo n.º 7
0
def test_dup_clear_denoms():
    assert dup_clear_denoms([], QQ, ZZ) == (ZZ(1), [])

    assert dup_clear_denoms([QQ(1)], QQ, ZZ) == (ZZ(1), [QQ(1)])
    assert dup_clear_denoms([QQ(7)], QQ, ZZ) == (ZZ(1), [QQ(7)])

    assert dup_clear_denoms([QQ(7,3)], QQ) == (ZZ(3), [QQ(7)])
    assert dup_clear_denoms([QQ(7,3)], QQ, ZZ) == (ZZ(3), [QQ(7)])

    assert dup_clear_denoms([QQ(3),QQ(1),QQ(0)], QQ, ZZ) == (ZZ(1), [QQ(3),QQ(1),QQ(0)])
    assert dup_clear_denoms([QQ(1),QQ(1,2),QQ(0)], QQ, ZZ) == (ZZ(2), [QQ(2),QQ(1),QQ(0)])

    assert dup_clear_denoms([QQ(3),QQ(1),QQ(0)], QQ, ZZ, convert=True) == (ZZ(1), [ZZ(3),ZZ(1),ZZ(0)])
    assert dup_clear_denoms([QQ(1),QQ(1,2),QQ(0)], QQ, ZZ, convert=True) == (ZZ(2), [ZZ(2),ZZ(1),ZZ(0)])

    assert dup_clear_denoms([EX(S(3)/2), EX(S(9)/4)], EX) == (EX(4), [EX(6), EX(9)])
Exemplo n.º 8
0
def test_dmp_clear_denoms():
    assert dmp_clear_denoms([[]], 1, QQ, ZZ) == (ZZ(1), [[]])

    assert dmp_clear_denoms([[QQ(1)]], 1, QQ, ZZ) == (ZZ(1), [[QQ(1)]])
    assert dmp_clear_denoms([[QQ(7)]], 1, QQ, ZZ) == (ZZ(1), [[QQ(7)]])

    assert dmp_clear_denoms([[QQ(7, 3)]], 1, QQ) == (ZZ(3), [[QQ(7)]])
    assert dmp_clear_denoms([[QQ(7, 3)]], 1, QQ, ZZ) == (ZZ(3), [[QQ(7)]])

    assert dmp_clear_denoms([[QQ(3)], [QQ(1)], []], 1, QQ,
                            ZZ) == (ZZ(1), [[QQ(3)], [QQ(1)], []])
    assert dmp_clear_denoms([[QQ(1)], [QQ(1, 2)], []], 1, QQ,
                            ZZ) == (ZZ(2), [[QQ(2)], [QQ(1)], []])

    assert dmp_clear_denoms([QQ(3), QQ(1), QQ(0)], 0, QQ, ZZ,
                            convert=True) == (ZZ(1), [ZZ(3),
                                                      ZZ(1),
                                                      ZZ(0)])
    assert dmp_clear_denoms([QQ(1), QQ(1, 2), QQ(0)], 0, QQ, ZZ,
                            convert=True) == (ZZ(2), [ZZ(2),
                                                      ZZ(1),
                                                      ZZ(0)])

    assert dmp_clear_denoms([[QQ(3)], [QQ(1)], []], 1, QQ, ZZ,
                            convert=True) == (ZZ(1), [[QQ(3)], [QQ(1)], []])
    assert dmp_clear_denoms([[QQ(1)], [QQ(1, 2)], []], 1, QQ, ZZ,
                            convert=True) == (ZZ(2), [[QQ(2)], [QQ(1)], []])

    raises(DomainError, lambda: dmp_clear_denoms([[EX(7)]], 1, EX))
Exemplo n.º 9
0
def test_dmp_clear_denoms():
    assert dmp_clear_denoms([[]], 1, QQ, ZZ) == (ZZ(1), [[]])

    assert dmp_clear_denoms([[QQ(1)]], 1, QQ, ZZ) == (ZZ(1), [[QQ(1)]])
    assert dmp_clear_denoms([[QQ(7)]], 1, QQ, ZZ) == (ZZ(1), [[QQ(7)]])

    assert dmp_clear_denoms([[QQ(7,3)]], 1, QQ) == (ZZ(3), [[QQ(7)]])
    assert dmp_clear_denoms([[QQ(7,3)]], 1, QQ, ZZ) == (ZZ(3), [[QQ(7)]])

    assert dmp_clear_denoms([[QQ(3)],[QQ(1)],[]], 1, QQ, ZZ) == (ZZ(1), [[QQ(3)],[QQ(1)],[]])
    assert dmp_clear_denoms([[QQ(1)],[QQ(1,2)],[]], 1, QQ, ZZ) == (ZZ(2), [[QQ(2)],[QQ(1)],[]])

    assert dmp_clear_denoms([QQ(3),QQ(1),QQ(0)], 0, QQ, ZZ, convert=True) == (ZZ(1), [ZZ(3),ZZ(1),ZZ(0)])
    assert dmp_clear_denoms([QQ(1),QQ(1,2),QQ(0)], 0, QQ, ZZ, convert=True) == (ZZ(2), [ZZ(2),ZZ(1),ZZ(0)])

    assert dmp_clear_denoms([[QQ(3)],[QQ(1)],[]], 1, QQ, ZZ, convert=True) == (ZZ(1), [[QQ(3)],[QQ(1)],[]])
    assert dmp_clear_denoms([[QQ(1)],[QQ(1,2)],[]], 1, QQ, ZZ, convert=True) == (ZZ(2), [[QQ(2)],[QQ(1)],[]])

    assert dmp_clear_denoms([[EX(S(3)/2)], [EX(S(9)/4)]], 1, EX) == (EX(4), [[EX(6)], [EX(9)]])
Exemplo n.º 10
0
def test_EX_EXRAW():
    assert EXRAW.zero is S.Zero
    assert EXRAW.one is S.One

    assert EX(1) == EX.Expression(1)
    assert EX(1).ex is S.One
    assert EXRAW(1) is S.One

    # EX has cancelling but EXRAW does not
    assert 2*EX((x + y*x)/x) == EX(2 + 2*y) != 2*((x + y*x)/x)
    assert 2*EXRAW((x + y*x)/x) == 2*((x + y*x)/x) != (1 + y)

    assert EXRAW.convert_from(EX(1), EX) is EXRAW.one
    assert EX.convert_from(EXRAW(1), EXRAW) == EX.one

    assert EXRAW.from_sympy(S.One) is S.One
    assert EXRAW.to_sympy(EXRAW.one) is S.One
    raises(CoercionFailed, lambda: EXRAW.from_sympy([]))

    assert EXRAW.get_field() == EXRAW

    assert EXRAW.unify(EX) == EXRAW
    assert EX.unify(EXRAW) == EXRAW
Exemplo n.º 11
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}

    raises(TypeError, lambda: t*x + z)
    raises(TypeError, lambda: x*t + z)
    raises(TypeError, lambda: t*u + z)
    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)}
Exemplo n.º 12
0
def test_tanh():
    R, x, y = ring('x, y', QQ)
    assert rs_tanh(x, x, 9) == -17 * x**7 / 315 + 2 * x**5 / 15 - x**3 / 3 + x
    assert rs_tanh(x*y + x**2*y**3 , x, 9) == 4*x**8*y**11/3 - \
        17*x**8*y**9/45 + 4*x**7*y**9/3 - 17*x**7*y**7/315 - x**6*y**9/3 + \
        2*x**6*y**7/3 - x**5*y**7 + 2*x**5*y**5/15 - x**4*y**5 - \
        x**3*y**3/3 + x**2*y**3 + x*y

    # Constant term in series
    a = symbols('a')
    R, x, y = ring('x, y', EX)
    assert rs_tanh(x + a, x, 5) == EX(tanh(a)**5 - 5*tanh(a)**3/3 + \
        2*tanh(a)/3)*x**4 + EX(-tanh(a)**4 + 4*tanh(a)**2/3 - QQ(1, 3))*x**3 + \
        EX(tanh(a)**3 - tanh(a))*x**2 + EX(-tanh(a)**2 + 1)*x + EX(tanh(a))

    p = rs_tanh(x + x**2 * y + a, x, 4)
    assert (p.compose(x, 10)).compose(y, 5) == EX(-1000*tanh(a)**4 + \
        10100*tanh(a)**3 + 2470*tanh(a)**2/3 - 10099*tanh(a) + QQ(530, 3))
Exemplo n.º 13
0
def test_dup_clear_denoms():
    assert dup_clear_denoms([], QQ, ZZ) == (ZZ(1), [])

    assert dup_clear_denoms([QQ(1)], QQ, ZZ) == (ZZ(1), [QQ(1)])
    assert dup_clear_denoms([QQ(7)], QQ, ZZ) == (ZZ(1), [QQ(7)])

    assert dup_clear_denoms([QQ(7, 3)], QQ) == (ZZ(3), [QQ(7)])
    assert dup_clear_denoms([QQ(7, 3)], QQ, ZZ) == (ZZ(3), [QQ(7)])

    assert dup_clear_denoms([QQ(3), QQ(1), QQ(0)], QQ,
                            ZZ) == (ZZ(1), [QQ(3), QQ(1), QQ(0)])
    assert dup_clear_denoms([QQ(1), QQ(1, 2), QQ(0)], QQ,
                            ZZ) == (ZZ(2), [QQ(2), QQ(1), QQ(0)])

    assert dup_clear_denoms([QQ(3), QQ(1), QQ(0)], QQ, ZZ,
                            convert=True) == (ZZ(1), [ZZ(3),
                                                      ZZ(1),
                                                      ZZ(0)])
    assert dup_clear_denoms([QQ(1), QQ(1, 2), QQ(0)], QQ, ZZ,
                            convert=True) == (ZZ(2), [ZZ(2),
                                                      ZZ(1),
                                                      ZZ(0)])

    raises(DomainError, lambda: dup_clear_denoms([EX(7)], EX))
Exemplo n.º 14
0
def test_tanh():
    R, x, y = ring("x, y", QQ)
    assert rs_tanh(x, x, 9) / x**5 == Rational(-17, 315) * x**2 + Rational(
        2, 15) - Rational(1, 3) * x**(-2) + x**(-4)
    assert (rs_tanh(x * y + x**2 * y**3, x,
                    9) == 4 * x**8 * y**11 / 3 - 17 * x**8 * y**9 / 45 +
            4 * x**7 * y**9 / 3 - 17 * x**7 * y**7 / 315 - x**6 * y**9 / 3 +
            2 * x**6 * y**7 / 3 - x**5 * y**7 + 2 * x**5 * y**5 / 15 -
            x**4 * y**5 - x**3 * y**3 / 3 + x**2 * y**3 + x * y)

    # Constant term in series
    a = symbols("a")
    R, x, y = ring("x, y", EX)
    assert rs_tanh(
        x + a, x, 5
    ) == EX(tanh(a)**5 - 5 * tanh(a)**3 / 3 + 2 * tanh(a) / 3) * x**4 + EX(
        -tanh(a)**4 + 4 * tanh(a)**2 / 3 - QQ(1, 3)) * x**3 + EX(
            tanh(a)**3 - tanh(a)) * x**2 + EX(-tanh(a)**2 + 1) * x + EX(
                tanh(a))

    p = rs_tanh(x + x**2 * y + a, x, 4)
    assert (p.compose(x, 10)).compose(
        y, 5) == EX(-1000 * tanh(a)**4 + 10100 * tanh(a)**3 +
                    2470 * tanh(a)**2 / 3 - 10099 * tanh(a) + QQ(530, 3))
Exemplo n.º 15
0
def test_cos():
    R, x, y = ring('x, y', QQ)
    assert rs_cos(x, x, 9)/x**5 == \
        Rational(1, 40320)*x**3 - Rational(1, 720)*x + Rational(1, 24)*x**(-1) - S.Half*x**(-3) + x**(-5)
    assert rs_cos(x*y + x**2*y**3, x, 9) == x**8*y**12/24 - \
        x**8*y**10/48 + x**8*y**8/40320 + x**7*y**10/6 - \
        x**7*y**8/120 + x**6*y**8/4 - x**6*y**6/720 + x**5*y**6/6 - \
        x**4*y**6/2 + x**4*y**4/24 - x**3*y**4 - x**2*y**2/2 + 1

    # Constant term in series
    a = symbols('a')
    R, x, y = ring('x, y', QQ[sin(a), cos(a), a])
    assert rs_cos(x + a, x, 5) == cos(a)*x**4/24 + sin(a)*x**3/6 - \
        cos(a)*x**2/2 - sin(a)*x + cos(a)
    assert rs_cos(x + x**2*y + a, x, 5) == -cos(a)*x**4*y**2/2 + \
        sin(a)*x**4*y/2 + cos(a)*x**4/24 - cos(a)*x**3*y + sin(a)*x**3/6 - \
        sin(a)*x**2*y - cos(a)*x**2/2 - sin(a)*x + cos(a)

    R, x, y = ring('x, y', EX)
    assert rs_cos(x + a, x, 5) == EX(cos(a)/24)*x**4 + EX(sin(a)/6)*x**3 - \
        EX(cos(a)/2)*x**2 - EX(sin(a))*x + EX(cos(a))
    assert rs_cos(x + x**2*y + a, x, 5) == -EX(cos(a)/2)*x**4*y**2 + \
        EX(sin(a)/2)*x**4*y + EX(cos(a)/24)*x**4 - EX(cos(a))*x**3*y + \
        EX(sin(a)/6)*x**3 - EX(sin(a))*x**2*y - EX(cos(a)/2)*x**2 - \
        EX(sin(a))*x + EX(cos(a))
Exemplo n.º 16
0
def test_sin():
    R, x, y = ring('x, y', QQ)
    assert rs_sin(x, x, 9)/x**5 == \
        Rational(-1, 5040)*x**2 + Rational(1, 120) - Rational(1, 6)*x**(-2) + x**(-4)
    assert rs_sin(x*y + x**2*y**3, x, 9) == x**8*y**11/12 - \
        x**8*y**9/720 + x**7*y**9/12 - x**7*y**7/5040 - x**6*y**9/6 + \
        x**6*y**7/24 - x**5*y**7/2 + x**5*y**5/120 - x**4*y**5/2 - \
        x**3*y**3/6 + x**2*y**3 + x*y

    # Constant term in series
    a = symbols('a')
    R, x, y = ring('x, y', QQ[sin(a), cos(a), a])
    assert rs_sin(x + a, x, 5) == sin(a)*x**4/24 - cos(a)*x**3/6 - \
        sin(a)*x**2/2 + cos(a)*x + sin(a)
    assert rs_sin(x + x**2*y + a, x, 5) == -sin(a)*x**4*y**2/2 - \
        cos(a)*x**4*y/2 + sin(a)*x**4/24 - sin(a)*x**3*y - cos(a)*x**3/6 + \
        cos(a)*x**2*y - sin(a)*x**2/2 + cos(a)*x + sin(a)

    R, x, y = ring('x, y', EX)
    assert rs_sin(x + a, x, 5) == EX(sin(a)/24)*x**4 - EX(cos(a)/6)*x**3 - \
        EX(sin(a)/2)*x**2 + EX(cos(a))*x + EX(sin(a))
    assert rs_sin(x + x**2*y + a, x, 5) == -EX(sin(a)/2)*x**4*y**2 - \
        EX(cos(a)/2)*x**4*y + EX(sin(a)/24)*x**4 - EX(sin(a))*x**3*y - \
        EX(cos(a)/6)*x**3 + EX(cos(a))*x**2*y - EX(sin(a)/2)*x**2 + \
        EX(cos(a))*x + EX(sin(a))
Exemplo n.º 17
0
def test_tan():
    R, x, y = ring('x, y', QQ)
    assert rs_tan(x, x, 9)/x**5 == \
        Rational(17, 315)*x**2 + Rational(2, 15) + Rational(1, 3)*x**(-2) + x**(-4)
    assert rs_tan(x*y + x**2*y**3, x, 9) == 4*x**8*y**11/3 + 17*x**8*y**9/45 + \
        4*x**7*y**9/3 + 17*x**7*y**7/315 + x**6*y**9/3 + 2*x**6*y**7/3 + \
        x**5*y**7 + 2*x**5*y**5/15 + x**4*y**5 + x**3*y**3/3 + x**2*y**3 + x*y

    # Constant term in series
    a = symbols('a')
    R, x, y = ring('x, y', QQ[tan(a), a])
    assert rs_tan(x + a, x, 5) == (tan(a)**5 + 5*tan(a)**3/3 +
        2*tan(a)/3)*x**4 + (tan(a)**4 + 4*tan(a)**2/3 + Rational(1, 3))*x**3 + \
        (tan(a)**3 + tan(a))*x**2 + (tan(a)**2 + 1)*x + tan(a)
    assert rs_tan(x + x**2*y + a, x, 4) == (2*tan(a)**3 + 2*tan(a))*x**3*y + \
        (tan(a)**4 + Rational(4, 3)*tan(a)**2 + Rational(1, 3))*x**3 + (tan(a)**2 + 1)*x**2*y + \
        (tan(a)**3 + tan(a))*x**2 + (tan(a)**2 + 1)*x + tan(a)

    R, x, y = ring('x, y', EX)
    assert rs_tan(x + a, x, 5) == EX(tan(a)**5 + 5*tan(a)**3/3 +
        2*tan(a)/3)*x**4 + EX(tan(a)**4 + 4*tan(a)**2/3 + EX(1)/3)*x**3 + \
        EX(tan(a)**3 + tan(a))*x**2 + EX(tan(a)**2 + 1)*x + EX(tan(a))
    assert rs_tan(x + x**2*y + a, x, 4) == EX(2*tan(a)**3 +
        2*tan(a))*x**3*y + EX(tan(a)**4 + 4*tan(a)**2/3 + EX(1)/3)*x**3 + \
        EX(tan(a)**2 + 1)*x**2*y + EX(tan(a)**3 + tan(a))*x**2 + \
        EX(tan(a)**2 + 1)*x + EX(tan(a))

    p = x + x**2 + 5
    assert rs_atan(p, x, 10).compose(x, 10) == EX(atan(5) + S(67701870330562640) / \
        668083460499)
Exemplo n.º 18
0
def test_atan():
    R, x, y = ring('x, y', QQ)
    assert rs_atan(x, x, 9) == -x**7 / 7 + x**5 / 5 - x**3 / 3 + x
    assert rs_atan(x*y + x**2*y**3, x, 9) == 2*x**8*y**11 - x**8*y**9 + \
        2*x**7*y**9 - x**7*y**7/7 - x**6*y**9/3 + x**6*y**7 - x**5*y**7 + \
        x**5*y**5/5 - x**4*y**5 - x**3*y**3/3 + x**2*y**3 + x*y

    # Constant term in series
    a = symbols('a')
    R, x, y = ring('x, y', EX)
    assert rs_atan(x + a, x, 5) == -EX((a**3 - a)/(a**8 + 4*a**6 + 6*a**4 + \
        4*a**2 + 1))*x**4 + EX((3*a**2 - 1)/(3*a**6 + 9*a**4 + \
        9*a**2 + 3))*x**3 - EX(a/(a**4 + 2*a**2 + 1))*x**2 + \
        EX(1/(a**2 + 1))*x + EX(atan(a))
    assert rs_atan(x + x**2*y + a, x, 4) == -EX(2*a/(a**4 + 2*a**2 + 1)) \
        *x**3*y + EX((3*a**2 - 1)/(3*a**6 + 9*a**4 + 9*a**2 + 3))*x**3 + \
        EX(1/(a**2 + 1))*x**2*y - EX(a/(a**4 + 2*a**2 + 1))*x**2 + EX(1/(a**2 \
        + 1))*x + EX(atan(a))
Exemplo n.º 19
0
def test_nth_root():
    R, x, y = ring('x, y', QQ)
    assert rs_nth_root(1 + x**2*y, 4, x, 10) == -77*x**8*y**4/2048 + \
        7*x**6*y**3/128 - 3*x**4*y**2/32 + x**2*y/4 + 1
    assert rs_nth_root(1 + x*y + x**2*y**3, 3, x, 5) == -x**4*y**6/9 + \
        5*x**4*y**5/27 - 10*x**4*y**4/243 - 2*x**3*y**4/9 + 5*x**3*y**3/81 + \
        x**2*y**3/3 - x**2*y**2/9 + x*y/3 + 1
    assert rs_nth_root(8 * x, 3, x, 3) == 2 * x**QQ(1, 3)
    assert rs_nth_root(8 * x + x**2 + x**3, 3, x,
                       3) == x**QQ(4, 3) / 12 + 2 * x**QQ(1, 3)
    r = rs_nth_root(8 * x + x**2 * y + x**3, 3, x, 4)
    assert r == -x**QQ(7, 3) * y**2 / 288 + x**QQ(7, 3) / 12 + x**QQ(
        4, 3) * y / 12 + 2 * x**QQ(1, 3)

    # Constant term in series
    a = symbols('a')
    R, x, y = ring('x, y', EX)
    assert rs_nth_root(x + a, 3, x, 4) == EX(5/(81*a**QQ(8, 3)))*x**3 - \
        EX(1/(9*a**QQ(5, 3)))*x**2 + EX(1/(3*a**QQ(2, 3)))*x + EX(a**QQ(1, 3))
    assert rs_nth_root(x**QQ(2, 3) + x**2*y + 5, 2, x, 3) == -EX(sqrt(5)/100)*\
        x**QQ(8, 3)*y - EX(sqrt(5)/16000)*x**QQ(8, 3) + EX(sqrt(5)/10)*x**2*y + \
        EX(sqrt(5)/2000)*x**2 - EX(sqrt(5)/200)*x**QQ(4, 3) + \
        EX(sqrt(5)/10)*x**QQ(2, 3) + EX(sqrt(5))
Exemplo n.º 20
0
def test_dup_isolate_real_roots_sqf():
    assert dup_isolate_real_roots_sqf([], ZZ) == []
    assert dup_isolate_real_roots_sqf([5], ZZ) == []

    assert dup_isolate_real_roots_sqf([1, 1,0], ZZ) == [(-QQ(1), -QQ(1)), (QQ(0), QQ(0))]
    assert dup_isolate_real_roots_sqf([1,-1,0], ZZ) == [( QQ(0),  QQ(0)), (QQ(1), QQ(1))]

    assert dup_isolate_real_roots_sqf([1,0,0,1,1], ZZ) == []

    I = [ (-QQ(2), -QQ(1)), (QQ(1), QQ(2))]

    assert dup_isolate_real_roots_sqf([1,0,-2], ZZ) == I
    assert dup_isolate_real_roots_sqf([-1,0,2], ZZ) == I

    assert dup_isolate_real_roots_sqf([1,-1], ZZ) == \
        [(QQ(1), QQ(1))]
    assert dup_isolate_real_roots_sqf([1,-3,2], ZZ) == \
        [(QQ(1), QQ(1)), (QQ(2), QQ(2))]
    assert dup_isolate_real_roots_sqf([1,-6,11,-6], ZZ) == \
        [(QQ(1), QQ(1)), (QQ(2), QQ(2)), (QQ(3), QQ(3))]
    assert dup_isolate_real_roots_sqf([1,-10,35,-50,24], ZZ) == \
        [(QQ(1), QQ(1)), (QQ(2), QQ(2)), (QQ(3), QQ(3)), (QQ(4), QQ(4))]
    assert dup_isolate_real_roots_sqf([1,-15,85,-225,274,-120], ZZ) == \
        [(QQ(1), QQ(1)), (QQ(2), QQ(2)), (QQ(3), QQ(3)), (QQ(4), QQ(4)), (QQ(5), QQ(5))]

    assert dup_isolate_real_roots_sqf([1,-10], ZZ) == \
        [(QQ(10), QQ(10))]
    assert dup_isolate_real_roots_sqf([1,-30,200], ZZ) == \
        [(QQ(10), QQ(10)), (QQ(20), QQ(20))]
    assert dup_isolate_real_roots_sqf([1,-60,1100,-6000], ZZ) == \
        [(QQ(10), QQ(10)), (QQ(20), QQ(20)), (QQ(30), QQ(30))]
    assert dup_isolate_real_roots_sqf([1,-100,3500,-50000,240000], ZZ) == \
        [(QQ(10), QQ(10)), (QQ(20), QQ(20)), (QQ(30), QQ(30)), (QQ(40), QQ(40))]
    assert dup_isolate_real_roots_sqf([1,-150,8500,-225000,2740000,-12000000], ZZ) == \
        [(QQ(10), QQ(10)), (QQ(20), QQ(20)), (QQ(30), QQ(30)), (QQ(40), QQ(40)), (QQ(50), QQ(50))]

    assert dup_isolate_real_roots_sqf([1,1], ZZ) == \
        [(-QQ(1), -QQ(1))]
    assert dup_isolate_real_roots_sqf([1,3,2], ZZ) == \
        [(-QQ(2), -QQ(2)), (-QQ(1), -QQ(1))]
    assert dup_isolate_real_roots_sqf([1,6,11,6], ZZ) == \
        [(-QQ(3), -QQ(3)), (-QQ(2), -QQ(2)), (-QQ(1), -QQ(1))]
    assert dup_isolate_real_roots_sqf([1,10,35,50,24], ZZ) == \
        [(-QQ(4), -QQ(4)), (-QQ(3), -QQ(3)), (-QQ(2), -QQ(2)), (-QQ(1), -QQ(1))]
    assert dup_isolate_real_roots_sqf([1,15,85,225,274,120], ZZ) == \
        [(-QQ(5), -QQ(5)), (-QQ(4), -QQ(4)), (-QQ(3), -QQ(3)), (-QQ(2), -QQ(2)), (-QQ(1), -QQ(1))]

    assert dup_isolate_real_roots_sqf([1,10], ZZ) == \
        [(-QQ(10), -QQ(10))]
    assert dup_isolate_real_roots_sqf([1,30,200], ZZ) == \
        [(-QQ(20), -QQ(20)), (-QQ(10), -QQ(10))]
    assert dup_isolate_real_roots_sqf([1,60,1100,6000], ZZ) == \
        [(-QQ(30), -QQ(30)), (-QQ(20), -QQ(20)), (-QQ(10), -QQ(10))]
    assert dup_isolate_real_roots_sqf([1,100,3500,50000,240000], ZZ) == \
        [(-QQ(40), -QQ(40)), (-QQ(30), -QQ(30)), (-QQ(20), -QQ(20)), (-QQ(10), -QQ(10))]
    assert dup_isolate_real_roots_sqf([1,150,8500,225000,2740000,12000000], ZZ) == \
        [(-QQ(50), -QQ(50)), (-QQ(40), -QQ(40)), (-QQ(30), -QQ(30)), (-QQ(20), -QQ(20)), (-QQ(10), -QQ(10))]

    assert dup_isolate_real_roots_sqf([1,0,-5], ZZ) == \
        [(QQ(-3), QQ(-2)), (QQ(2), QQ(3))]
    assert dup_isolate_real_roots_sqf([1,0,0,-5], ZZ) == \
        [(QQ(1), QQ(2))]
    assert dup_isolate_real_roots_sqf([1,0,0,0,-5], ZZ) == \
        [(QQ(-2), QQ(-1)), (QQ(1), QQ(2))]
    assert dup_isolate_real_roots_sqf([1,0,0,0,0,-5], ZZ) == \
        [(QQ(1), QQ(2))]
    assert dup_isolate_real_roots_sqf([1,0,0,0,0,0,-5], ZZ) == \
        [(QQ(-2), QQ(-1)), (QQ(1), QQ(2))]
    assert dup_isolate_real_roots_sqf([1,0,0,0,0,0,0,-5], ZZ) == \
        [(QQ(1), QQ(2))]
    assert dup_isolate_real_roots_sqf([1,0,0,0,0,0,0,0,-5], ZZ) == \
        [(QQ(-2), QQ(-1)), (QQ(1), QQ(2))]
    assert dup_isolate_real_roots_sqf([1,0,0,0,0,0,0,0,0,-5], ZZ) == \
        [(QQ(1), QQ(2))]

    assert dup_isolate_real_roots_sqf([1,0,-1], ZZ) == \
        [(-QQ(1), -QQ(1)), (QQ(1), QQ(1))]
    assert dup_isolate_real_roots_sqf([1,2,-1,-2], ZZ) == \
        [(-QQ(2), -QQ(2)), (-QQ(1), -QQ(1)), (QQ(1), QQ(1))]
    assert dup_isolate_real_roots_sqf([1,0,-5,0,4], ZZ) == \
        [(-QQ(2), -QQ(2)), (-QQ(1), -QQ(1)), (QQ(1), QQ(1)), (QQ(2), QQ(2))]
    assert dup_isolate_real_roots_sqf([1,3,-5,-15,4,12], ZZ) == \
        [(-QQ(3), -QQ(3)), (-QQ(2), -QQ(2)), (-QQ(1), -QQ(1)), (QQ(1), QQ(1)),
         ( QQ(2),  QQ(2))]
    assert dup_isolate_real_roots_sqf([1,0,-14,0,49,0,-36], ZZ) == \
        [(-QQ(3), -QQ(3)), (-QQ(2), -QQ(2)), (-QQ(1), -QQ(1)), (QQ(1), QQ(1)),
         ( QQ(2),  QQ(2)), ( QQ(3),  QQ(3))]
    assert dup_isolate_real_roots_sqf([2,1,-28,-14,98,49,-72,-36], ZZ) == \
        [(-QQ(3), -QQ(3)), (-QQ(2), -QQ(2)), (-QQ(1), -QQ(1)), (-QQ(1), QQ(0)),
         ( QQ(1),  QQ(1)), ( QQ(2),  QQ(2)), ( QQ(3),  QQ(3))]
    assert dup_isolate_real_roots_sqf([4,0,-57,0,210,0,-193,0,36], ZZ) == \
        [(-QQ(3), -QQ(3)), (-QQ(2), -QQ(2)), (-QQ(1), -QQ(1)), (-QQ(1), QQ(0)),
         ( QQ(0),  QQ(1)), ( QQ(1),  QQ(1)), ( QQ(2),  QQ(2)), ( QQ(3), QQ(3))]

    f = [9,0,-2]

    assert dup_isolate_real_roots_sqf(f, ZZ) == \
        [(QQ(-1), QQ(0)), (QQ(0), QQ(1))]

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

    f = [200100012, -700390052, 700490079, -200240054, 40017, -2]

    assert dup_isolate_real_roots_sqf(f, ZZ) == \
        [(QQ(0), QQ(1,10002)), (QQ(1,10002), QQ(1,10002)), (QQ(1,2), QQ(1,2)), (QQ(1), QQ(1)), (QQ(2), QQ(2))]

    assert dup_isolate_real_roots_sqf(f, ZZ, eps=QQ(1,100000)) == \
        [(QQ(1,10003), QQ(1,10003)), (QQ(1,10002), QQ(1,10002)), (QQ(1,2), QQ(1,2)), (QQ(1), QQ(1)), (QQ(2), QQ(2))]

    a, b, c, d = 10000090000001, 2000100003, 10000300007, 10000005000008

    f = [ 20001600074001600021,
          1700135866278935491773999857,
         -2000179008931031182161141026995283662899200197,
         -800027600594323913802305066986600025,
          100000950000540000725000008]

    assert dup_isolate_real_roots_sqf(f, ZZ) == \
        [(-QQ(a), -QQ(a)), (-QQ(1,1),  QQ(0,1)), (QQ(0,1), QQ(1,1)), (QQ(d), QQ(d))]

    assert dup_isolate_real_roots_sqf(f, ZZ, eps=QQ(1,100000000000)) == \
        [(-QQ(a), -QQ(a)), (-QQ(1,b), -QQ(1,b)), (QQ(1,c), QQ(1,c)), (QQ(d), QQ(d))]

    (u, v), B, C, (s, t) = dup_isolate_real_roots_sqf(f, ZZ, fast=True)

    assert u < -a < v and B == (-QQ(1), QQ(0)) and C == (QQ(0), QQ(1)) and s < d < t

    assert dup_isolate_real_roots_sqf(f, ZZ, fast=True, eps=QQ(1,100000000000000000000000000000)) == \
        [(-QQ(a), -QQ(a)), (-QQ(1,b), -QQ(1,b)), (QQ(1,c), QQ(1,c)), (QQ(d), QQ(d))]

    assert dup_isolate_real_roots_sqf([QQ(8,5), QQ(-87374,3855), QQ(-17,771)], QQ) == \
        [(QQ(-1), QQ(0)), (QQ(14), QQ(15))]

    f = [-10, 8, 80, -32, -160]

    assert dup_isolate_real_roots_sqf(f, ZZ) == \
        [(-QQ(2), -QQ(2)), (-QQ(2), -QQ(1)), (QQ(2), QQ(2)), (QQ(2), QQ(3))]

    assert dup_isolate_real_roots_sqf(f, ZZ, eps=QQ(1,100)) == \
        [(-QQ(2), -QQ(2)), (-QQ(23,14), -QQ(18,11)), (QQ(2), QQ(2)), (QQ(39,16), QQ(22,9))]

    assert dup_isolate_real_roots_sqf([1, -1], ZZ, inf=2) == []
    assert dup_isolate_real_roots_sqf([1, -1], ZZ, sup=0) == []

    assert dup_isolate_real_roots_sqf([1, -1], ZZ) == [(1, 1)]
    assert dup_isolate_real_roots_sqf([1, -1], ZZ, inf=1) == [(1, 1)]
    assert dup_isolate_real_roots_sqf([1, -1], ZZ, sup=1) == [(1, 1)]
    assert dup_isolate_real_roots_sqf([1, -1], ZZ, inf=1, sup=1) == [(1, 1)]

    f = [1, 0, -2]

    assert dup_isolate_real_roots_sqf(f, ZZ, inf=QQ(7,4)) == []
    assert dup_isolate_real_roots_sqf(f, ZZ, inf=QQ(7,5)) == [(QQ(7,5), QQ(3,2))]
    assert dup_isolate_real_roots_sqf(f, ZZ, sup=QQ(7,5)) == [(-2, -1)]
    assert dup_isolate_real_roots_sqf(f, ZZ, sup=QQ(7,4)) == [(-2, -1), (1, QQ(3,2))]
    assert dup_isolate_real_roots_sqf(f, ZZ, sup=-QQ(7,4)) == []
    assert dup_isolate_real_roots_sqf(f, ZZ, sup=-QQ(7,5)) == [(-QQ(3,2), -QQ(7,5))]
    assert dup_isolate_real_roots_sqf(f, ZZ, inf=-QQ(7,5)) == [(1, 2)]
    assert dup_isolate_real_roots_sqf(f, ZZ, inf=-QQ(7,4)) == [(-QQ(3,2), -1), (1, 2)]

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

    assert dup_isolate_real_roots_sqf(f, ZZ, inf=-2) == I
    assert dup_isolate_real_roots_sqf(f, ZZ, sup=+2) == I

    assert dup_isolate_real_roots_sqf(f, ZZ, inf=-2, sup=2) == I

    raises(DomainError, lambda: dup_isolate_real_roots_sqf([EX(1), EX(2)], EX))
Exemplo n.º 21
0
def test_dmp_factor_list():
    R, x, y = ring("x,y", ZZ)
    assert R.dmp_factor_list(0) == (ZZ(0), [])
    assert R.dmp_factor_list(7) == (7, [])

    R, x, y = ring("x,y", QQ)
    assert R.dmp_factor_list(0) == (QQ(0), [])
    assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x, y = ring("x,y", ZZ['y'])
    assert R.dmp_factor_list(0) == (DMP([], ZZ), [])
    assert R.dmp_factor_list(DMP([ZZ(7)], ZZ)) == (DMP([ZZ(7)], ZZ), [])

    R, x, y = ring("x,y", QQ['y'])
    assert R.dmp_factor_list(0) == (DMP([], QQ), [])
    assert R.dmp_factor_list(DMP([QQ(1, 7)], QQ)) == (DMP([QQ(1, 7)], QQ), [])

    R, x, y = ring("x,y", ZZ)
    assert R.dmp_factor_list_include(0) == [(0, 1)]
    assert R.dmp_factor_list_include(7) == [(7, 1)]

    R, X = xring("x:200", ZZ)

    f, g = X[0]**2 + 2 * X[0] + 1, X[0] + 1
    assert R.dmp_factor_list(f) == (1, [(g, 2)])

    f, g = X[-1]**2 + 2 * X[-1] + 1, X[-1] + 1
    assert R.dmp_factor_list(f) == (1, [(g, 2)])

    R, x = ring("x", ZZ)
    assert R.dmp_factor_list(x**2 + 2 * x + 1) == (1, [(x + 1, 2)])
    R, x = ring("x", QQ)
    assert R.dmp_factor_list(QQ(1, 2) * x**2 + x + QQ(1, 2)) == (QQ(1, 2),
                                                                 [(x + 1, 2)])

    R, x, y = ring("x,y", ZZ)
    assert R.dmp_factor_list(x**2 + 2 * x + 1) == (1, [(x + 1, 2)])
    R, x, y = ring("x,y", QQ)
    assert R.dmp_factor_list(QQ(1, 2) * x**2 + x + QQ(1, 2)) == (QQ(1, 2),
                                                                 [(x + 1, 2)])

    R, x, y = ring("x,y", ZZ)
    f = 4 * x**2 * y + 4 * x * y**2

    assert R.dmp_factor_list(f) == \
        (4, [(y, 1),
             (x, 1),
             (x + y, 1)])

    assert R.dmp_factor_list_include(f) == \
        [(4*y, 1),
         (x, 1),
         (x + y, 1)]

    R, x, y = ring("x,y", QQ)
    f = QQ(1, 2) * x**2 * y + QQ(1, 2) * x * y**2

    assert R.dmp_factor_list(f) == \
        (QQ(1,2), [(y, 1),
                   (x, 1),
                   (x + y, 1)])

    R, x, y = ring("x,y", RR)
    f = 2.0 * x**2 - 8.0 * y**2

    assert R.dmp_factor_list(f) == \
        (RR(2.0), [(1.0*x - 2.0*y, 1),
                   (1.0*x + 2.0*y, 1)])

    R, x, y = ring("x,y", ZZ['t'])
    f = DMP([ZZ(4), ZZ(0)], ZZ) * x**2 + DMP([ZZ(4), ZZ(0), ZZ(0)], ZZ) * x

    assert R.dmp_factor_list(f) == \
        (DMP([ZZ(4)], ZZ), [(DMP([ZZ(1), ZZ(0)], ZZ), 1),
                            (DMP([ZZ(1)], ZZ)*x, 1),
                            (DMP([ZZ(1)], ZZ)*x + DMP([ZZ(1), ZZ(0)], ZZ), 1)])

    R, x, y = ring("x,y", QQ['t'])
    f = DMP([QQ(1, 2), QQ(0)], QQ) * x**2 + DMP(
        [QQ(1, 2), QQ(0), QQ(0)], QQ) * x

    assert R.dmp_factor_list(f) == \
        (DMP([QQ(1, 2)], QQ), [(DMP([QQ(1), QQ(0)], QQ), 1),
                               (DMP([QQ(1)], QQ)*x, 1),
                               (DMP([QQ(1)], QQ)*x + DMP([QQ(1), QQ(0)], QQ), 1)])

    R, x, y = ring("x,y", FF(2))
    raises(NotImplementedError, lambda: R.dmp_factor_list(x**2 + y**2))

    R, x, y = ring("x,y", EX)
    raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1))))
Exemplo n.º 22
0
def test_atan():
    R, x, y = ring("x, y", QQ)
    assert rs_atan(x, x, 9) == -(x**7) / 7 + x**5 / 5 - x**3 / 3 + x
    assert (rs_atan(x * y + x**2 * y**3, x, 9) == 2 * x**8 * y**11 -
            x**8 * y**9 + 2 * x**7 * y**9 - x**7 * y**7 / 7 - x**6 * y**9 / 3 +
            x**6 * y**7 - x**5 * y**7 + x**5 * y**5 / 5 - x**4 * y**5 -
            x**3 * y**3 / 3 + x**2 * y**3 + x * y)

    # Constant term in series
    a = symbols("a")
    R, x, y = ring("x, y", EX)
    assert rs_atan(x + a, x, 5) == -EX(
        (a**3 - a) / (a**8 + 4 * a**6 + 6 * a**4 + 4 * a**2 + 1)) * x**4 + EX(
            (3 * a**2 - 1) / (3 * a**6 + 9 * a**4 + 9 * a**2 + 3)) * x**3 - EX(
                a / (a**4 + 2 * a**2 + 1)) * x**2 + EX(1 /
                                                       (a**2 + 1)) * x + EX(
                                                           atan(a))
    assert rs_atan(
        x + x**2 * y + a, x,
        4) == -EX(2 * a / (a**4 + 2 * a**2 + 1)) * x**3 * y + EX(
            (3 * a**2 - 1) / (3 * a**6 + 9 * a**4 + 9 * a**2 + 3)) * x**3 + EX(
                1 /
                (a**2 + 1)) * x**2 * y - EX(a /
                                            (a**4 + 2 * a**2 + 1)) * x**2 + EX(
                                                1 /
                                                (a**2 + 1)) * x + EX(atan(a))
Exemplo n.º 23
0
def test_dmp_factor_list():
    R, x, y = ring("x,y", ZZ)
    assert R.dmp_factor_list(0) == (ZZ(0), [])
    assert R.dmp_factor_list(7) == (7, [])

    R, x, y = ring("x,y", QQ)
    assert R.dmp_factor_list(0) == (QQ(0), [])
    assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    Rt, t = ring("t", ZZ)
    R, x, y = ring("x,y", Rt)
    assert R.dmp_factor_list(0) == (0, [])
    assert R.dmp_factor_list(7) == (ZZ(7), [])

    Rt, t = ring("t", QQ)
    R, x, y = ring("x,y", Rt)
    assert R.dmp_factor_list(0) == (0, [])
    assert R.dmp_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x, y = ring("x,y", ZZ)
    assert R.dmp_factor_list_include(0) == [(0, 1)]
    assert R.dmp_factor_list_include(7) == [(7, 1)]

    R, X = xring("x:200", ZZ)

    f, g = X[0]**2 + 2*X[0] + 1, X[0] + 1
    assert R.dmp_factor_list(f) == (1, [(g, 2)])

    f, g = X[-1]**2 + 2*X[-1] + 1, X[-1] + 1
    assert R.dmp_factor_list(f) == (1, [(g, 2)])

    R, x = ring("x", ZZ)
    assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
    R, x = ring("x", QQ)
    assert R.dmp_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1,2), [(x + 1, 2)])

    R, x, y = ring("x,y", ZZ)
    assert R.dmp_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
    R, x, y = ring("x,y", QQ)
    assert R.dmp_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1,2), [(x + 1, 2)])

    R, x, y = ring("x,y", ZZ)
    f = 4*x**2*y + 4*x*y**2

    assert R.dmp_factor_list(f) == \
        (4, [(y, 1),
             (x, 1),
             (x + y, 1)])

    assert R.dmp_factor_list_include(f) == \
        [(4*y, 1),
         (x, 1),
         (x + y, 1)]

    R, x, y = ring("x,y", QQ)
    f = QQ(1,2)*x**2*y + QQ(1,2)*x*y**2

    assert R.dmp_factor_list(f) == \
        (QQ(1,2), [(y, 1),
                   (x, 1),
                   (x + y, 1)])

    R, x, y = ring("x,y", RR)
    f = 2.0*x**2 - 8.0*y**2

    assert R.dmp_factor_list(f) == \
        (RR(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
    coeff, factors = R.dmp_factor_list(f)
    assert coeff == RR(1.0) and len(factors) == 1 and factors[0][0].almosteq(f, 1e-10) and factors[0][1] == 1

    Rt, t = ring("t", ZZ)
    R, x, y = ring("x,y", Rt)
    f = 4*t*x**2 + 4*t**2*x

    assert R.dmp_factor_list(f) == \
        (4*t, [(x, 1),
             (x + t, 1)])

    Rt, t = ring("t", QQ)
    R, x, y = ring("x,y", Rt)
    f = QQ(1, 2)*t*x**2 + QQ(1, 2)*t**2*x

    assert R.dmp_factor_list(f) == \
        (QQ(1, 2)*t, [(x, 1),
                    (x + t, 1)])

    R, x, y = ring("x,y", FF(2))
    raises(NotImplementedError, lambda: R.dmp_factor_list(x**2 + y**2))

    R, x, y = ring("x,y", EX)
    raises(DomainError, lambda: R.dmp_factor_list(EX(sin(1))))
Exemplo n.º 24
0
def test_construct_domain():
    assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([1, 2, 3],
                            field=True) == (QQ, [QQ(1), QQ(2),
                                                 QQ(3)])

    assert construct_domain([S.One, S(2), S(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([S.One, S(2), S(3)],
                            field=True) == (QQ, [QQ(1), QQ(2),
                                                 QQ(3)])

    assert construct_domain([S.Half, S(2)]) == (QQ, [QQ(1, 2), QQ(2)])
    result = construct_domain([3.14, 1, S.Half])
    assert isinstance(result[0], RealField)
    assert result[1] == [RR(3.14), RR(1.0), RR(0.5)]

    assert construct_domain([3.14, sqrt(2)],
                            extension=None) == (EX, [EX(3.14),
                                                     EX(sqrt(2))])
    assert construct_domain([3.14, sqrt(2)],
                            extension=True) == (EX, [EX(3.14),
                                                     EX(sqrt(2))])

    assert construct_domain([1, sqrt(2)],
                            extension=None) == (EX, [EX(1), EX(sqrt(2))])

    assert construct_domain([x, sqrt(x)]) == (EX, [EX(x), EX(sqrt(x))])
    assert construct_domain([x, sqrt(x), sqrt(y)
                             ]) == (EX, [EX(x),
                                         EX(sqrt(x)),
                                         EX(sqrt(y))])

    alg = QQ.algebraic_field(sqrt(2))

    assert construct_domain([7, S.Half, sqrt(2)], extension=True) == \
        (alg, [alg.convert(7), alg.convert(S.Half), alg.convert(sqrt(2))])

    alg = QQ.algebraic_field(sqrt(2) + sqrt(3))

    assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == \
        (alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))])

    dom = ZZ[x]

    assert construct_domain([2*x, 3]) == \
        (dom, [dom.convert(2*x), dom.convert(3)])

    dom = ZZ[x, y]

    assert construct_domain([2*x, 3*y]) == \
        (dom, [dom.convert(2*x), dom.convert(3*y)])

    dom = QQ[x]

    assert construct_domain([x/2, 3]) == \
        (dom, [dom.convert(x/2), dom.convert(3)])

    dom = QQ[x, y]

    assert construct_domain([x/2, 3*y]) == \
        (dom, [dom.convert(x/2), dom.convert(3*y)])

    dom = RR[x]

    assert construct_domain([x/2, 3.5]) == \
        (dom, [dom.convert(x/2), dom.convert(3.5)])

    dom = RR[x, y]

    assert construct_domain([x/2, 3.5*y]) == \
        (dom, [dom.convert(x/2), dom.convert(3.5*y)])

    dom = ZZ.frac_field(x)

    assert construct_domain([2/x, 3]) == \
        (dom, [dom.convert(2/x), dom.convert(3)])

    dom = ZZ.frac_field(x, y)

    assert construct_domain([2/x, 3*y]) == \
        (dom, [dom.convert(2/x), dom.convert(3*y)])

    dom = RR.frac_field(x)

    assert construct_domain([2/x, 3.5]) == \
        (dom, [dom.convert(2/x), dom.convert(3.5)])

    dom = RR.frac_field(x, y)

    assert construct_domain([2/x, 3.5*y]) == \
        (dom, [dom.convert(2/x), dom.convert(3.5*y)])

    dom = RealField(prec=336)[x]

    assert construct_domain([pi.evalf(100)*x]) == \
        (dom, [dom.convert(pi.evalf(100)*x)])

    assert construct_domain(2) == (ZZ, ZZ(2))
    assert construct_domain(S(2) / 3) == (QQ, QQ(2, 3))
    assert construct_domain(Rational(2, 3)) == (QQ, QQ(2, 3))

    assert construct_domain({}) == (ZZ, {})
Exemplo n.º 25
0
def test_atanh():
    R, x, y = ring('x, y', QQ)
    assert rs_atanh(x, x, 9) / x**5 == Rational(1, 7) * x**2 + Rational(
        1, 5) + Rational(1, 3) * x**(-2) + x**(-4)
    assert rs_atanh(x*y + x**2*y**3, x, 9) == 2*x**8*y**11 + x**8*y**9 + \
        2*x**7*y**9 + x**7*y**7/7 + x**6*y**9/3 + x**6*y**7 + x**5*y**7 + \
        x**5*y**5/5 + x**4*y**5 + x**3*y**3/3 + x**2*y**3 + x*y

    # Constant term in series
    a = symbols('a')
    R, x, y = ring('x, y', EX)
    assert rs_atanh(x + a, x, 5) == EX((a**3 + a)/(a**8 - 4*a**6 + 6*a**4 - \
        4*a**2 + 1))*x**4 - EX((3*a**2 + 1)/(3*a**6 - 9*a**4 + \
        9*a**2 - 3))*x**3 + EX(a/(a**4 - 2*a**2 + 1))*x**2 - EX(1/(a**2 - \
        1))*x + EX(atanh(a))
    assert rs_atanh(x + x**2*y + a, x, 4) == EX(2*a/(a**4 - 2*a**2 + \
        1))*x**3*y - EX((3*a**2 + 1)/(3*a**6 - 9*a**4 + 9*a**2 - 3))*x**3 - \
        EX(1/(a**2 - 1))*x**2*y + EX(a/(a**4 - 2*a**2 + 1))*x**2 - \
        EX(1/(a**2 - 1))*x + EX(atanh(a))

    p = x + x**2 + 5
    assert rs_atanh(p, x, 10).compose(x, 10) == EX(Rational(-733442653682135, 5079158784) \
        + atanh(5))
Exemplo n.º 26
0
def test_log():
    R, x = ring('x', QQ)
    p = 1 + x
    p1 = rs_log(p, x, 4) / x**2
    assert p1 == Rational(1, 3) * x - S.Half + x**(-1)
    p = 1 + x + 2 * x**2 / 3
    p1 = rs_log(p, x, 9)
    assert p1 == -17*x**8/648 + 13*x**7/189 - 11*x**6/162 - x**5/45 + \
      7*x**4/36 - x**3/3 + x**2/6 + x
    p2 = rs_series_inversion(p, x, 9)
    p3 = rs_log(p2, x, 9)
    assert p3 == -p1

    R, x, y = ring('x, y', QQ)
    p = 1 + x + 2 * y * x**2
    p1 = rs_log(p, x, 6)
    assert p1 == (4 * x**5 * y**2 - 2 * x**5 * y - 2 * x**4 * y**2 + x**5 / 5 +
                  2 * x**4 * y - x**4 / 4 - 2 * x**3 * y + x**3 / 3 +
                  2 * x**2 * y - x**2 / 2 + x)

    # Constant term in series
    a = symbols('a')
    R, x, y = ring('x, y', EX)
    assert rs_log(x + a, x, 5) == -EX(1/(4*a**4))*x**4 + EX(1/(3*a**3))*x**3 \
        - EX(1/(2*a**2))*x**2 + EX(1/a)*x + EX(log(a))
    assert rs_log(x + x**2*y + a, x, 4) == -EX(a**(-2))*x**3*y + \
        EX(1/(3*a**3))*x**3 + EX(1/a)*x**2*y - EX(1/(2*a**2))*x**2 + \
        EX(1/a)*x + EX(log(a))

    p = x + x**2 + 3
    assert rs_log(p, x, 10).compose(
        x, 5) == EX(log(3) + Rational(19281291595, 9920232))
Exemplo n.º 27
0
def test_construct_domain():
    assert construct_domain([1, 2, 3]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([1, 2, 3],
                            field=True) == (QQ, [QQ(1), QQ(2),
                                                 QQ(3)])

    assert construct_domain([S(1), S(2), S(3)]) == (ZZ, [ZZ(1), ZZ(2), ZZ(3)])
    assert construct_domain([S(1), S(2), S(3)],
                            field=True) == (QQ, [QQ(1), QQ(2),
                                                 QQ(3)])

    assert construct_domain([S(1) / 2, S(2)]) == (QQ, [QQ(1, 2), QQ(2)])
    assert construct_domain([3.14, 1,
                             S(1) / 2]) == (RR, [RR(3.14),
                                                 RR(1.0),
                                                 RR(0.5)])

    assert construct_domain([3.14, sqrt(2)],
                            extension=None) == (EX, [EX(3.14),
                                                     EX(sqrt(2))])
    assert construct_domain([3.14, sqrt(2)],
                            extension=True) == (EX, [EX(3.14),
                                                     EX(sqrt(2))])

    assert construct_domain([1, sqrt(2)],
                            extension=None) == (EX, [EX(1), EX(sqrt(2))])

    alg = QQ.algebraic_field(sqrt(2))

    assert construct_domain([7, S(1)/2, sqrt(2)], extension=True) == \
        (alg, [alg.convert(7), alg.convert(S(1)/2), alg.convert(sqrt(2))])

    alg = QQ.algebraic_field(sqrt(2) + sqrt(3))

    assert construct_domain([7, sqrt(2), sqrt(3)], extension=True) == \
        (alg, [alg.convert(7), alg.convert(sqrt(2)), alg.convert(sqrt(3))])

    dom = ZZ[x]

    assert construct_domain([2*x, 3]) == \
        (dom, [dom.convert(2*x), dom.convert(3)])

    dom = ZZ[x, y]

    assert construct_domain([2*x, 3*y]) == \
        (dom, [dom.convert(2*x), dom.convert(3*y)])

    dom = QQ[x]

    assert construct_domain([x/2, 3]) == \
        (dom, [dom.convert(x/2), dom.convert(3)])

    dom = QQ[x, y]

    assert construct_domain([x/2, 3*y]) == \
        (dom, [dom.convert(x/2), dom.convert(3*y)])

    dom = RR[x]

    assert construct_domain([x/2, 3.5]) == \
        (dom, [dom.convert(x/2), dom.convert(3.5)])

    dom = RR[x, y]

    assert construct_domain([x/2, 3.5*y]) == \
        (dom, [dom.convert(x/2), dom.convert(3.5*y)])

    dom = ZZ.frac_field(x)

    assert construct_domain([2/x, 3]) == \
        (dom, [dom.convert(2/x), dom.convert(3)])

    dom = ZZ.frac_field(x, y)

    assert construct_domain([2/x, 3*y]) == \
        (dom, [dom.convert(2/x), dom.convert(3*y)])
Exemplo n.º 28
0
def test_exp():
    R, x = ring('x', QQ)
    p = x + x**4
    for h in [10, 30]:
        q = rs_series_inversion(1 + p, x, h) - 1
        p1 = rs_exp(q, x, h)
        q1 = rs_log(p1, x, h)
        assert q1 == q
    p1 = rs_exp(p, x, 30)
    assert p1.coeff(x**29) == QQ(74274246775059676726972369,
                                 353670479749588078181744640000)
    prec = 21
    p = rs_log(1 + x, x, prec)
    p1 = rs_exp(p, x, prec)
    assert p1 == x + 1

    # Constant term in series
    a = symbols('a')
    R, x, y = ring('x, y', QQ[exp(a), a])
    assert rs_exp(x + a, x, 5) == exp(a)*x**4/24 + exp(a)*x**3/6 + \
        exp(a)*x**2/2 + exp(a)*x + exp(a)
    assert rs_exp(x + x**2*y + a, x, 5) == exp(a)*x**4*y**2/2 + \
            exp(a)*x**4*y/2 + exp(a)*x**4/24 + exp(a)*x**3*y + \
            exp(a)*x**3/6 + exp(a)*x**2*y + exp(a)*x**2/2 + exp(a)*x + exp(a)

    R, x, y = ring('x, y', EX)
    assert rs_exp(x + a, x, 5) ==  EX(exp(a)/24)*x**4 + EX(exp(a)/6)*x**3 + \
        EX(exp(a)/2)*x**2 + EX(exp(a))*x + EX(exp(a))
    assert rs_exp(x + x**2*y + a, x, 5) == EX(exp(a)/2)*x**4*y**2 + \
        EX(exp(a)/2)*x**4*y + EX(exp(a)/24)*x**4 + EX(exp(a))*x**3*y + \
        EX(exp(a)/6)*x**3 + EX(exp(a))*x**2*y + EX(exp(a)/2)*x**2 + \
        EX(exp(a))*x + EX(exp(a))
Exemplo n.º 29
0
def test_dup_factor_list():
    R, x = ring("x", ZZ)
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(7) == (7, [])

    R, x = ring("x", QQ)
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x = ring("x", ZZ['t'])
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(7) == (7, [])

    R, x = ring("x", QQ['t'])
    assert R.dup_factor_list(0) == (0, [])
    assert R.dup_factor_list(QQ(1, 7)) == (QQ(1, 7), [])

    R, x = ring("x", ZZ)
    assert R.dup_factor_list_include(0) == [(0, 1)]
    assert R.dup_factor_list_include(7) == [(7, 1)]

    assert R.dup_factor_list(x**2 + 2*x + 1) == (1, [(x + 1, 2)])
    assert R.dup_factor_list_include(x**2 + 2*x + 1) == [(x + 1, 2)]

    R, x = ring("x", QQ)
    assert R.dup_factor_list(QQ(1,2)*x**2 + x + QQ(1,2)) == (QQ(1, 2), [(x + 1, 2)])

    R, x = ring("x", FF(2))
    assert R.dup_factor_list(x**2 + 1) == (1, [(x + 1, 2)])

    R, x = ring("x", RR)
    assert R.dup_factor_list(1.0*x**2 + 2.0*x + 1.0) == (1.0, [(1.0*x + 1.0, 2)])
    assert R.dup_factor_list(2.0*x**2 + 4.0*x + 2.0) == (2.0, [(1.0*x + 1.0, 2)])

    f = 6.7225336055071*x**2 - 10.6463972754741*x - 0.33469524022264
    coeff, factors = R.dup_factor_list(f)
    assert coeff == RR(1.0) and len(factors) == 1 and factors[0][0].almosteq(f, 1e-10) and factors[0][1] == 1

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

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

    assert R.dup_factor_list(f) == \
        (4*t, [(x, 1),
             (x + t, 1)])

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

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

    assert R.dup_factor_list(f) == \
        (QQ(1, 2)*t, [(x, 1),
                    (x + t, 1)])

    R, x = ring("x", QQ.algebraic_field(I))
    def anp(element):
        return ANP(element, [QQ(1), QQ(0), QQ(1)], QQ)

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

    assert R.dup_factor_list(f) == \
        (anp([QQ(1, 1)]), [(anp([QQ(1, 1)])*x, 2),
                           (anp([QQ(1, 1)])*x**2 + anp([])*x + anp([QQ(2, 1)]), 1)])

    R, x = ring("x", EX)
    raises(DomainError, lambda: R.dup_factor_list(EX(sin(1))))
Exemplo n.º 30
0
def test_dup_isolate_real_roots():
    assert dup_isolate_real_roots([], ZZ) == []
    assert dup_isolate_real_roots([3], ZZ) == []

    assert dup_isolate_real_roots([5,0], ZZ) ==  [((QQ(0), QQ(0)), 1)]
    assert dup_isolate_real_roots([7,0,0,0,0], ZZ) == [((QQ(0), QQ(0)), 4)]

    assert dup_isolate_real_roots([1, 1,0], ZZ) == [((-QQ(1), -QQ(1)), 1), ((QQ(0), QQ(0)), 1)]
    assert dup_isolate_real_roots([1,-1,0], ZZ) == [(( QQ(0),  QQ(0)), 1), ((QQ(1), QQ(1)), 1)]

    assert dup_isolate_real_roots([1,0,0,1,1], ZZ) == []

    I = [((-QQ(2), -QQ(1)), 1), ((QQ(1), QQ(2)), 1)]

    assert dup_isolate_real_roots([1,0,-2], ZZ) == I
    assert dup_isolate_real_roots([-1,0,2], ZZ) == I

    f = [16,-96,24,936,-1599,-2880,9196,552,-21831,13968,21690,-26784,-2916,15552,-5832]
    g = dup_sqf_part(f, ZZ)

    assert dup_isolate_real_roots(f, ZZ) == \
        [((-QQ(2), -QQ(3,2)), 2), ((-QQ(3,2), -QQ(1,1)), 3),
         (( QQ(1),  QQ(3,2)), 3), (( QQ(3,2),  QQ(3,2)), 4), ((QQ(5,3), QQ(2)), 2)]

    assert dup_isolate_real_roots_sqf(g, ZZ) == \
        [(-QQ(2), -QQ(3,2)), (-QQ(3,2), -QQ(1,1)),
         ( QQ(1),  QQ(3,2)), ( QQ(3,2),  QQ(3,2)), (QQ(3,2), QQ(2))]
    assert dup_isolate_real_roots(g, ZZ) == \
        [((-QQ(2), -QQ(3,2)), 1), ((-QQ(3,2), -QQ(1,1)), 1),
         (( QQ(1),  QQ(3,2)), 1), (( QQ(3,2),  QQ(3,2)), 1), ((QQ(3,2), QQ(2)), 1)]

    assert dup_isolate_real_roots([1, -1], ZZ, inf=2) == []
    assert dup_isolate_real_roots([1, -1], ZZ, sup=0) == []

    assert dup_isolate_real_roots([1, -1], ZZ) == [((1, 1), 1)]
    assert dup_isolate_real_roots([1, -1], ZZ, inf=1) == [((1, 1), 1)]
    assert dup_isolate_real_roots([1, -1], ZZ, sup=1) == [((1, 1), 1)]
    assert dup_isolate_real_roots([1, -1], ZZ, inf=1, sup=1) == [((1, 1), 1)]

    f = [1, 0, -4, 0, 4]

    assert dup_isolate_real_roots(f, ZZ, inf=QQ(7,4)) == []
    assert dup_isolate_real_roots(f, ZZ, inf=QQ(7,5)) == [((QQ(7,5), QQ(3,2)), 2)]
    assert dup_isolate_real_roots(f, ZZ, sup=QQ(7,5)) == [((-2, -1), 2)]
    assert dup_isolate_real_roots(f, ZZ, sup=QQ(7,4)) == [((-2, -1), 2), ((1, QQ(3,2)), 2)]
    assert dup_isolate_real_roots(f, ZZ, sup=-QQ(7,4)) == []
    assert dup_isolate_real_roots(f, ZZ, sup=-QQ(7,5)) == [((-QQ(3,2), -QQ(7,5)), 2)]
    assert dup_isolate_real_roots(f, ZZ, inf=-QQ(7,5)) == [((1, 2), 2)]
    assert dup_isolate_real_roots(f, ZZ, inf=-QQ(7,4)) == [((-QQ(3,2), -1), 2), ((1, 2), 2)]

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

    assert dup_isolate_real_roots(f, ZZ, inf=-2) == I
    assert dup_isolate_real_roots(f, ZZ, sup=+2) == I

    assert dup_isolate_real_roots(f, ZZ, inf=-2, sup=2) == I

    f = [1, -3, -1, 11, -8, -8, 12, -4, 0, 0, 0, 0]

    assert dup_isolate_real_roots(f, ZZ, basis=False) == \
        [((-2, -1), 2), ((0, 0), 4), ((1, 1), 3), ((1, 2), 2)]
    assert dup_isolate_real_roots(f, ZZ, basis=True) == \
        [((-2, -1), 2, [1, 0, -2]), ((0, 0), 4, [1, 0]), ((1, 1), 3, [1, -1]), ((1, 2), 2, [1, 0, -2])]

    raises(DomainError, lambda: dup_isolate_real_roots([EX(1), EX(2)], EX))