Exemplo n.º 1
0
    def _get_reals_sqf(cls, factor):
        """Compute real root isolating intervals for a square-free polynomial. """
        if factor in _reals_cache:
            real_part = _reals_cache[factor]
        else:
            _reals_cache[factor] = real_part = dup_isolate_real_roots_sqf(factor.rep.rep, factor.rep.dom, blackbox=True)

        return real_part
Exemplo n.º 2
0
    def _get_reals_sqf(cls, factor):
        """Compute real root isolating intervals for a square-free polynomial. """
        if factor in _reals_cache:
            real_part = _reals_cache[factor]
        else:
            _reals_cache[factor] = real_part = \
                dup_isolate_real_roots_sqf(factor.rep.rep, factor.rep.dom, blackbox=True)

        return real_part
Exemplo n.º 3
0
    def _get_reals_sqf(cls, currentfactor, use_cache=True):
        """Get real root isolating intervals for a square-free factor."""
        if use_cache and currentfactor in _reals_cache:
            real_part = _reals_cache[currentfactor]
        else:
            _reals_cache[currentfactor] = real_part = \
                dup_isolate_real_roots_sqf(
                    currentfactor.rep.rep, currentfactor.rep.dom, blackbox=True)

        return real_part
Exemplo n.º 4
0
    def _get_reals_sqf(cls, factor, use_cache=True):
        """Get real root isolating intervals for a square-free factor."""
        if use_cache and factor in _reals_cache:
            real_part = _reals_cache[factor]
        else:
            _reals_cache[factor] = real_part = \
                dup_isolate_real_roots_sqf(
                    factor.rep.rep, factor.rep.dom, blackbox=True)

        return real_part
Exemplo n.º 5
0
 def intervals(f, all=False, eps=None, inf=None, sup=None, fast=False, sqf=False):
     """Compute isolating intervals for roots of `f`. """
     if not f.lev:
         if not all:
             if not sqf:
                 return dup_isolate_real_roots(f.rep, f.dom, eps=eps, inf=inf, sup=sup, fast=fast)
             else:
                 return dup_isolate_real_roots_sqf(f.rep, f.dom, eps=eps, inf=inf, sup=sup, fast=fast)
         else:
             if not sqf:
                 return dup_isolate_all_roots(f.rep, f.dom, eps=eps, inf=inf, sup=sup, fast=fast)
             else:
                 return dup_isolate_all_roots_sqf(f.rep, f.dom, eps=eps, inf=inf, sup=sup, fast=fast)
     else:
         raise PolynomialError("can't isolate roots of a multivariate polynomial")
Exemplo n.º 6
0
 def intervals(f, all=False, eps=None, inf=None, sup=None, fast=False, sqf=False):
     """Compute isolating intervals for roots of `f`. """
     if not f.lev:
         if not all:
             if not sqf:
                 return dup_isolate_real_roots(f.rep, f.dom, eps=eps, inf=inf, sup=sup, fast=fast)
             else:
                 return dup_isolate_real_roots_sqf(f.rep, f.dom, eps=eps, inf=inf, sup=sup, fast=fast)
         else:
             if not sqf:
                 return dup_isolate_all_roots(f.rep, f.dom, eps=eps, inf=inf, sup=sup, fast=fast)
             else:
                 return dup_isolate_all_roots_sqf(f.rep, f.dom, eps=eps, inf=inf, sup=sup, fast=fast)
     else:
         raise PolynomialError("can't isolate roots of a multivariate polynomial")
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, "dup_isolate_real_roots_sqf([EX(1), EX(2)], EX)")
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, "dup_isolate_real_roots([EX(1), EX(2)], EX)")
Exemplo n.º 9
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, "dup_isolate_real_roots([EX(1), EX(2)], EX)")
Exemplo n.º 10
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, "dup_isolate_real_roots_sqf([EX(1), EX(2)], EX)")