def test_maxdeg(dim, degrees, basis):
    if basis == "power": MultiX = MultiPower
    else: MultiX = MultiCheb
    for deg in degrees:
        coeffs = rand_coeffs(dim, deg, 1)[0]
        polys = [MultiX(coeff) for coeff in coeffs]
        t = timer()
        solve(polys)
        print(f"dim {dim}/deg {deg}: time = {timer()-t}")
Example #2
0
def run_test(polys):
    try:
        t = timer()
        roots = solve(polys)
        t = timer() - t
        res = np.abs([poly(roots) for poly in polys])
        logres = np.log10(res, out=-16 * np.ones_like(res), where=(res != 0))
        return np.array([logres.max(), logres.mean(), t, 0], dtype='float64')
    except ConditioningError:
        return np.array([0, 0, 0, 1], dtype='float64')
Example #3
0
def correctZeros(original_polys, new_polys, transform, MSmatrix):
    '''
    A helper function for polyroots tests. Takes in polynomials, find their common zeros using polyroots, and calculates
    how many of the zeros are correct.
    In this function it asserts that the number of zeros is equal to the product of the degrees, which is only valid if
    the polynomials are random and upper triangular, and that at least 95% of the zeros are correct (so it will pass even
    on bad random runs)
    '''
    zeros = transform(pr.solve(new_polys, MSmatrix=MSmatrix))
    correct = 0
    outOfRange = 0
    for zero in zeros:
        good = True
        for poly in original_polys:
            if not np.isclose(0, poly(zero), atol=1.e-3):
                good = False
                if (np.abs(zero) > 1).any():
                    outOfRange += 1
                break
        if good:
            correct += 1
    assert (100 * correct / (len(zeros) - outOfRange) > 95)
def test_paper_example():

    #Power form of the polys
    p1 = MultiPower(np.array([[1, -4, 0], [0, 3, 0], [1, 0,
                                                      0]]))  #y^2 + 3xy - 4x +1
    p2 = MultiPower(np.array([[3, 0, -2], [6, -6, 0],
                              [0, 0, 0]]))  #-6xy -2x^2 + 6y +3

    #Cheb form of the polys
    c1 = MultiCheb(np.array([[2, 0, -1], [6, -6, 0], [0, 0,
                                                      0]]))  #p1 in Cheb form
    c2 = MultiCheb(np.array([[1.5, -4, 0], [0, 3, 0], [.5, 0,
                                                       0]]))  #p2 in Cheb form

    right_number_of_roots = 4

    #~ ~ ~ Power Form, Mx Matrix ~ ~ ~
    power_mult_roots = pr.solve([p1, p2], MSmatrix=1)
    assert len(power_mult_roots) == right_number_of_roots
    for root in power_mult_roots:
        assert np.isclose(0, p1(root), atol=1.e-8)
        assert np.isclose(0, p2(root), atol=1.e-8)

    #~ ~ ~ Cheb Form, Mx Matrix ~ ~ ~
    cheb_mult_roots = pr.solve([c1, c2], MSmatrix=1)
    assert len(cheb_mult_roots) == right_number_of_roots
    for root in cheb_mult_roots:
        assert np.isclose(0, c1(root), atol=1.e-8)
        assert np.isclose(0, c1(root), atol=1.e-8)

    #~ ~ ~ Power Form, My Matrix ~ ~ ~
    power_multR_roots = pr.solve([p1, p2], MSmatrix=2)
    assert len(power_multR_roots) == right_number_of_roots
    for root in power_multR_roots:
        assert np.isclose(0, p1(root), atol=1.e-8)
        assert np.isclose(0, p2(root), atol=1.e-8)

    #~ ~ ~ Cheb Form, My Matrix ~ ~ ~
    cheb_multR_roots = pr.solve([c1, c2], MSmatrix=2)
    assert len(cheb_multR_roots) == right_number_of_roots
    for root in cheb_multR_roots:
        assert np.isclose(0, c1(root), atol=1.e-8)
        assert np.isclose(0, c1(root), atol=1.e-8)

    #~ ~ ~ Power Form, Pseudorandom Multiplication Matrix ~ ~ ~
    power_multrand_roots = pr.solve([p1, p2], MSmatrix=0)
    assert len(power_multrand_roots) == right_number_of_roots
    for root in power_multrand_roots:
        assert np.isclose(0, p1(root), atol=1.e-8)
        assert np.isclose(0, p2(root), atol=1.e-8)

    #~ ~ ~ Cheb Form, Pseudorandom Multiplication Matrix ~ ~ ~
    cheb_multrand_roots = pr.solve([c1, c2], MSmatrix=0)
    assert len(cheb_multrand_roots) == right_number_of_roots
    for root in cheb_multrand_roots:
        assert np.isclose(0, c1(root), atol=1.e-8)
        assert np.isclose(0, c1(root), atol=1.e-8)

    #~ ~ ~ Power Form, Division Matrix ~ ~ ~
    power_div_roots = pr.solve([p1, p2], MSmatrix=-1)
    assert len(power_div_roots) == right_number_of_roots
    for root in power_div_roots:
        assert np.isclose(0, p1(root), atol=1.e-8)
        assert np.isclose(0, p2(root), atol=1.e-8)

    #~ ~ ~ Cheb Form, Division Matrix ~ ~ ~
    cheb_div_roots = pr.solve([c1, c2], MSmatrix=-1)
    assert len(cheb_div_roots) == right_number_of_roots
    for root in cheb_div_roots:
        assert np.isclose(0, c1(root), atol=1.e-8)
        assert np.isclose(0, c2(root), atol=1.e-8)