Esempio n. 1
0
def correctZeros(polys, checkNumber = True):
    '''
    A helper function for test_TVB. Takes in polynomials, find their common zeros using TVB, 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 = roots(polys, method = 'Groebner')
    assert(zeros != -1)
    if checkNumber:
        expectedNum = np.product([poly.degree for poly in polys])
        assert(len(zeros) == expectedNum)
    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) > 80)
Esempio n. 2
0
def testRoots_4():
    f1 = MultiPower(np.array([[5, -1], [1, 0]]))
    f2 = MultiPower(np.array([[1, -1], [-1, 0]]))

    root = rf.roots([f1, f2], method='Macaulay')[0]

    assert (all(np.isclose(root, [-2, 3])))
Esempio n. 3
0
def testRoots_5():
    f1 = MultiPower(np.array([[0, -1], [0, 0], [1, 0]]))
    f2 = MultiPower(np.array([[1, -1], [1, 0]]))

    roots = rf.roots([f1, f2], method='Macaulay')

    assert (all(np.isclose(roots[0], [-0.61803399, 0.38196601])))
    assert (all(np.isclose(roots[1], [1.61803399, 2.61803399])))
Esempio n. 4
0
def testRoots_3():
    # roots of [x^2-y, x^3-y+1]
    f1 = MultiPower(np.array([[0, -1], [0, 0], [1, 0]]))
    f2 = MultiPower(np.array([[1, -1], [0, 0], [0, 0], [1, 0]]))

    roots = rf.roots([f1, f2], method='Groebner')

    values_at_roots = np.array([[f1(root) for root in roots],
                                [f2(root) for root in roots]])

    assert (np.all(np.isclose(values_at_roots, 0)))
Esempio n. 5
0
def testRoots():
    f1 = MultiPower(np.array([[0, -1.5, .5], [-1.5, 1.5, 0], [1, 0, 0]]),
                    clean_zeros=False)
    f2 = MultiPower(np.array([[0, 0, 0], [-1, 0, 1], [0, 0, 0]]),
                    clean_zeros=False)
    f3 = MultiPower(np.array([[0, -1, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0],
                              [0, 0, 0, 0]]),
                    clean_zeros=False)

    roots = rf.roots([f1, f2, f3], method='Groebner')
    values_at_roots = np.array([[f1(root) for root in roots],
                                [f2(root) for root in roots],
                                [f3(root) for root in roots]])

    assert (np.all(np.isclose(values_at_roots, 0)))
Esempio n. 6
0
def projective_solve(poly_list, rmSize=1.e-2):
    '''Finds the roots of given polynomials using projective space.
    
    Parameters
    ----------
    poly_list : list
        A list of polynomials.
    rmSize : float
        The size of the pertubations in the rotation matrix. The rotation matrix is the identity matrix
        with pertubations of about this size in each spot to make it random.

    Returns
    -------
    zero_set : set
        A set of the distinct zeros of the system. In order to be able to put them in a set and not
        double count equivalent zeros found in sperate hyperplanes, the zeros are rounded to 5 decimal spots.
    '''
    dim = poly_list[0].dim
    inv_rotation = get_rotation_matrix(
        dim + 1, size=rmSize
    )  #The inverse of the matrix is how the projective space is rotated.
    proejctive_poly_list = project_poly_list(poly_list)
    all_zeros = list()
    for hyperplane in range(dim + 1):
        values = list()
        cheb_poly_list = list()
        for poly in proejctive_poly_list:
            cheb = triangular_cheb_approx(poly, hyperplane, inv_rotation, dim,
                                          poly.degree)
            cheb_poly_list.append(cheb)
        #zeros = division_cheb(cheb_poly_list, divisor_var = 0)
        zeros = roots(cheb_poly_list, method='TVB')
        #print(zeros)
        for zero in zeros:
            pZero = np.insert(zero, hyperplane, 1)
            rZero = inv_rotation @ pZero
            fullZero = rZero / rZero[-1]
            all_zeros.append(fullZero[:-1])
    return getZeroSet(all_zeros, poly_list)
Esempio n. 7
0
def testRoots_2():
    f1 = MultiPower(
        np.array([[[5, 0, 0], [0, 0, 0], [0, 0, 0]],
                  [[0, -2, 0], [0, 0, 0], [0, 0, 0]],
                  [[1, 0, 0], [0, 0, 0], [0, 0, 0]]]))

    f2 = MultiPower(
        np.array([[[1, 0, 0], [0, 1, 0], [0, 0, 0]],
                  [[0, 0, 0], [0, 0, 0], [1, 0, 0]],
                  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]))

    f3 = MultiPower(
        np.array([[[0, 0, 0], [0, 0, 0], [3, 0, 0]],
                  [[0, -8, 0], [0, 0, 0], [0, 0, 0]],
                  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]))

    roots = rf.roots([f1, f2, f3], method='Groebner')

    values_at_roots = np.array([[f1(root) for root in roots],
                                [f2(root) for root in roots],
                                [f3(root) for root in roots]])

    assert (np.all(np.isclose(values_at_roots, 0)))
Esempio n. 8
0
def testRoots_6():  # test when ideal is not zero-dimensional
    f1 = MultiPower(np.array([[-12, -12], [1, 1], [1, 1]]))
    f2 = MultiPower(np.array([[6, 3, -3], [-2, -1, 1]]))

    roots = rf.roots([f1, f2], method='Macaulay')
    assert (roots == -1)