示例#1
0
def correctZeros(polys, 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 = pr.solve(polys, MSmatrix=MSmatrix)
    correct = 0
    outOfRange = 0
    for zero in zeros:
        good = True
        for poly in 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)
    #print("~ ~ ~ Cheb Form, M_x Matrix ~ ~ ~")
    #cmx = polyroots.solve([c1, c2], MSmatrix = 1)
    #print("Roots:\n",cmx)

    #flip left/right because x and y are switched. Same for M_y and M_1/y matrices below
    #print("~ ~ ~ Power Form, M_y Matrix ~ ~ ~")
    #pmy = polyroots.solve([p1_switch_xy, p2_switch_xy], MSmatrix = 1)[:,::-1]
    #print("Roots:\n",pmy)

    #print("~ ~ ~ Cheb Form, M_y Matrix ~ ~ ~")
    #cmy = polyroots.solve([c1_switch_xy, c2_switch_xy], MSmatrix = 1)[:,::-1]
    #print("Roots:\n",cmy)

    print("~ ~ ~ Power Form, Division Matrix 1/y ~ ~ ~")
    pdy = polyroots.solve([p1_switch_xy, p2_switch_xy], MSmatrix=-1,
                          verbose=v)[:, ::-1]
    print("Roots:\n", pdy)

    print("~ ~ ~ Cheb Form, Division Matrix 1/y ~ ~ ~")
    cdy = polyroots.solve([c1_switch_xy, c2_switch_xy], MSmatrix=-1,
                          verbose=v)[:, ::-1]
    print("Roots:\n", cdy)

    print("~ ~ ~ Power Form, Division Matrix 1/x ~ ~ ~")
    pdx = polyroots.solve([p1, p2], MSmatrix=-1, verbose=v)
    print("Roots:\n", pdx)

    print("~ ~ ~ Cheb Form, Division Matrix 1/x ~ ~ ~")
    cdx = polyroots.solve([c1, c2], MSmatrix=-1, verbose=v)
    print("Roots:\n", cdx)
示例#3
0
from numalgsolve import polynomial
from numalgsolve import polyroots
import numpy as np

p1 = polynomial.MultiPower(np.array([[1, -4, 0], [0, 3, 0],
                                     [1, 0, 0]]).T)  #y^2 + 3xy - 4x +1
p2 = polynomial.MultiPower(np.array([[3, 0, -2], [6, -6, 0],
                                     [0, 0, 0]]).T)  #-6xy -2x^2 + 6y +3
c1 = polynomial.MultiCheb(np.array([[2, 0, -1], [6, -6, 0],
                                    [0, 0, 0]]).T)  #p2 in Cheb form
c2 = polynomial.MultiCheb(np.array([[1.5, -4, 0], [0, 3, 0],
                                    [.5, 0, 0]]).T)  #p2 in Cheb form

print("~ ~ ~ Power Form, Mx Matrix ~ ~ ~")
print("Roots\n", polyroots.solve([p1, p2], MSmatrix=1, verbose=True))

print("~ ~ ~ Cheb Form, Mx Matrix ~ ~ ~")
print("Roots\n", polyroots.solve([c1, c2], MSmatrix=1, verbose=True))

print("~ ~ ~ Power Form,  My Matrix ~ ~ ~")
print("Roots\n", polyroots.solve([p1, p2], MSmatrix=2, verbose=True))

print("~ ~ ~ Cheb Form, My Matrix ~ ~ ~")
print("Roots\n", polyroots.solve([c1, c2], MSmatrix=2, verbose=True))

print("~ ~ ~ Power Form, Pseudorandom Multiplication Matrix ~ ~ ~")
print("Roots\n", polyroots.solve([p1, p2], MSmatrix=0, verbose=True))

print("~ ~ ~ Cheb Form, Pseudorandom Multiplication Matrix ~ ~ ~")
print("Roots\n", polyroots.solve([c1, c2], MSmatrix=0, verbose=True))
示例#4
0
 try:
     assert np.allclose(np.fliplr(pad_with_zeros(p.coeff)), np.triu(np.fliplr(pad_with_zeros(p.coeff)))), "p's coefficients are not upper left triangular. \n p.coef = \n{}\nq.coeff = \n{}".format(p.coeff, q.coeff)
     assert np.allclose(np.fliplr(pad_with_zeros(q.coeff)), np.triu(np.fliplr(pad_with_zeros(q.coeff)))), "q's coefficients are not upper left triangular. \n p.coef = \n{}\nq.coeff = \n{}".format(p.coeff, q.coeff)
     assert not np.any(np.isclose(np.diag(np.fliplr(pad_with_zeros(p.coeff))), 0)), "p has highest term coefficients close to zero. \n p.coef = \n{}\nq.coeff = \n{}".format(p.coeff, q.coeff)
     assert not np.any(np.isclose(np.diag(np.fliplr(pad_with_zeros(q.coeff))), 0)), "p has highest term coefficients close to zero. \n p.coef = \n{}\nq.coeff = \n{}".format(p.coeff, q.coeff)
 except AssertionError as e:
     error_message += '\nNot Upper Triangular:' + str(e)
 finally:
     try:
         if common_root_at_inf([p,q]) != False:
             error_message += '\nCommon root at infinity:' + str(common_root_at_inf([p,q])[1])
     except Exception as e:
         error_message += '\nFailed finding roots at Infinity:' + str(e)
     finally:
         try:
             mult_solutions = solve([p,q], MSmatrix=1)
         except Exception as e:
             mult_error = '\n' + str(e)
             not_solved(filename, 'mult', error_message + mult_error)
         else:
             solved(filename, 'mult', error_message)
             multpercent = percent_good_roots(mult_solutions, [p,q], ignore_out_of_range=False, tolerance=1e-9)
             if multpercent == 1: #if 100% of the roots were right, report that
                 right_roots(filename, 'mult', mult_solutions, multpercent)
             else:
                 wrong_roots(filename, 'mult', mult_solutions, multpercent)
         finally:
             try:
                 multrand_solutions = solve([p,q], MSmatrix=0)
             except Exception as e:
                 multrand_error = '\n' + str(e)
示例#5
0
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)