Beispiel #1
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")
Beispiel #2
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():
    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)")
Beispiel #4
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)")