Exemplo n.º 1
0
def test_TVB_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

    #Homogenous power form
    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

    right_number_of_roots = 4

    power_roots = tvb.solve([p1, p2], verbose=True)
    assert len(power_roots) == right_number_of_roots
    for root in power_roots:
        assert np.isclose(0, p1(root), atol=1.e-8)
        assert np.isclose(0, p2(root), atol=1.e-8)

    cheb_roots = tvb.solve([c1, c2], verbose=True)
    assert len(cheb_roots) == right_number_of_roots
    for root in cheb_roots:
        assert np.isclose(0, c1(root), atol=1.e-8)
        assert np.isclose(0, c1(root), atol=1.e-8)
Exemplo 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])))
Exemplo n.º 3
0
def Mxi_Matrix(i, basisDict, VB, dim, poly_type, verbose=False):
    '''
    Uses the reduced Macaulay matrix to construct the Moller-Stetter matrix M_xi, which
    represents the linear map of multiplying by xi in the space C[x1, ..., xn]/I.

    Parameters
    ----------
    i : int
        The index of the variable xi to make the Moller-Stetter matrix of, where variables
        are indexed as x1, x2, ..., xn.
    basisDict: dictionary
        A dictionary which maps monomials not in the basis to linear combinations
        of monomials in the basis. Generated using the TVB method.
    VB: numpy array
        Represents a vector basis for the space C[x1, ..., xn]/I created with the TVB method.
        Each row represents a monomial in the basis as the degrees of each variable.
        For example, x^2y^5 would be represented as [2,5].
    dim: int
        The dimension of the system (n)
    verbose : bool
        Prints information about how the roots are computed.

    Returns
    -------
    Mxi : 2D numpy array
        The Moller-Stetter matrix which represents multiplying by xi
    '''
    VB = VB.tolist() #convert to list bc numpy's __contains__() function is broken

    #Construct the polynomial to create the MS Matrix of (xi)
    xi_ind = np.zeros(dim, dtype=int)
    xi_ind[i-1] = 1
    coef = np.zeros((2,)*dim)
    coef[tuple(xi_ind)] = 1
    if poly_type == "MultiPower":
        xi = MultiPower(np.array(coef))
    elif poly_type == "MultiCheb":
        xi = MultiCheb(np.array(coef))

    if verbose:
        print("\nCoefficients of polynomial whose Moller-Stetter matrix we construt\n", xi.coeff)

    # Build multiplication matrix M_xi
    Mxi = np.zeros((len(VB), len(VB)))
    for j in range(len(VB)): #multiply each monomial in the basis by xi
        product_coef = xi.mon_mult(VB[j], returnType = 'Matrix')
        for monomial in zip(*np.where(product_coef != 0)):
            if list(monomial) in VB: #convert to list to test if list of lists
                Mxi[VB.index(list(monomial))][j] += product_coef[monomial]
            else:
                Mxi[:,j] -= product_coef[monomial]*basisDict[monomial]

    # Construct var_dict
    var_dict = {}
    for i in range(len(VB)):
        mon = VB[i]
        if np.sum(mon) == 1 or np.sum(mon) == 0:
            var_dict[tuple(mon)] = i

    return Mxi
Exemplo n.º 4
0
def test_mon_mult_random():

    np.random.seed(988)

    #test with random matrices
    possible_dim = np.random.randint(1,5, (1,10))
    dim = possible_dim[0, np.random.randint(1,9)]

    shape = list()
    for i in range(dim):
        shape.append(np.random.randint(2,4))
    matrix1 = np.random.randint(1,11,(shape))
    M1 = MultiPower(matrix1)

    shape2 = list()
    for i in range(dim):
        shape2.append(np.random.randint(2,4))
    matrix2 = np.ones(shape2)
    M2 = MultiPower(matrix2)

    M3 = M1*M2

    for index, i in np.ndenumerate(M2.coeff):
        if sum(index) == 0:
            M4 = MultiPower.mon_mult(M1, index)
        else:
            M4 = M4 + MultiPower.mon_mult(M1, index)

    if M3.shape != M4.shape:
        new_M3_coeff, new_M4_coeff = utils.match_size(M3.coeff,M4.coeff)
    else:
        new_M3_coeff, new_M4_coeff = M3.coeff, M4.coeff

    assert np.allclose(new_M3_coeff, new_M4_coeff)
Exemplo n.º 5
0
def test_add():
    a1 = np.arange(27).reshape((3,3,3))
    Test2 = MultiPower(a1)
    a2 = np.ones((3,3,3))
    Test3 = MultiPower(a2)
    addTest = Test2 + Test3
    assert (addTest.coeff == (Test2.coeff + Test3.coeff)).all()
Exemplo n.º 6
0
def testReducePoly():
    poly = MultiPower(np.array([[-3], [2], [-4], [1]]))
    g = MultiPower(np.array([[2], [1]]))
    basisSet = set()
    basisSet.add((0, 0))

    reduced = rf.reduce_poly(poly, [g], basisSet)
    assert (MultiPower(reduced).coeff == np.array([[-31.]]))
Exemplo n.º 7
0
def test_mult():
    test1 = np.array([[0,1],[2,1]])
    test2 = np.array([[2,2],[3,0]])
    p1 = MultiPower(test1)
    p2 = MultiPower(test2)
    new_poly = p1*p2
    truth = MultiPower(np.array([[0, 2, 2],[4,9,2],[6,3,0]]))
    assert np.allclose(new_poly.coeff, truth.coeff)
Exemplo n.º 8
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])))
Exemplo n.º 9
0
def testReducePoly_2():
    poly = MultiPower(np.array([[-7], [2], [-13], [4]]))
    g = MultiPower(np.array([[-2], [3], [1]]))
    basisSet = set()
    basisSet.add((0, 0))
    basisSet.add((1, 0))

    reduced = rf.reduce_poly(poly, [g], basisSet)
    assert (np.all(MultiPower(reduced).coeff == np.array([[-57.], [85.]])))
Exemplo n.º 10
0
def test_vectorSpaceBasis():
    f1 = MultiPower(np.array([[0, -1.5, .5], [-1.5, 1.5, 0], [1, 0, 0]]))
    f2 = MultiPower(np.array([[0, 0, 0], [-1, 0, 1], [0, 0, 0]]))
    f3 = MultiPower(
        np.array([[0, -1, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]))
    G = [f1, f2, f3]
    basis = rf.vectorSpaceBasis(G)[0]
    trueBasis = [(0, 0), (1, 0), (0, 1), (1, 1), (0, 2)]

    assert ((len(basis) == len(trueBasis)) and (m in basis for m in trueBasis))
Exemplo n.º 11
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)))
Exemplo n.º 12
0
def test_roots_at_inf():
    g = MultiPower(np.array([[0, 0, 1], [-1, 0, 0]]).T)  #f(x,y) = x^2 - y
    assert roots_at_inf(g) == [(0, 1)]

    f = MultiPower(np.array([[0, 1]]))  #f(x,y) = y
    g = MultiPower(np.array([[0, 0, -1], [1, 0, 0]]))  #f(x,y) = x - y^2
    assert roots_at_inf(f) == [(1, 0)]
    assert roots_at_inf(g) == [(1, 0)]

    f = MultiPower(np.array([[5, 6, 2], [9, 3, 0], [1, 0, 0]]))
    assert roots_at_inf(f) == [(-2, 1), (-1, 1)]
Exemplo n.º 13
0
def test_evaluate_grid1():
    #Evaluate 2 + yx^2 + 3y^2 - xy on grid (0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)
    poly = MultiPower(np.array([[2,0,3],
                                [0,-1,0],
                                [0,1,0]]))
    x = np.arange(3)
    xy = np.column_stack([x,x])
    sol = np.array([[2,5,14],
                    [2,5,14],
                    [2,7,18]])
    assert(np.all(poly.evaluate_grid(xy) == sol))
Exemplo n.º 14
0
def test_find_degree():
    #Test Case #1 - 2,3,4, and 5 2D Polynomials of degree 3

    degree3Coeff = np.array([[1, 1, 1, 1], [1, 1, 1, 0], [1, 1, 0, 0],
                             [1, 0, 0, 0]])

    A = MultiPower(degree3Coeff)
    B = MultiPower(degree3Coeff)
    C = MultiPower(degree3Coeff)
    D = MultiPower(degree3Coeff)
    E = MultiPower(degree3Coeff)
    assert (find_degree([A, B]) == 5)
    assert (find_degree([A, B, C]) == 7)
    assert (find_degree([A, B, C, D]) == 9)
    assert (find_degree([A, B, C, D, E]) == 11)

    #Test Case #2 - A 2D polynomials of degree 3 and one of degree 5
    degree5Coeff = np.array([[1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 0],
                             [1, 1, 1, 1, 0, 0], [1, 1, 1, 0, 0, 0],
                             [1, 1, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0]])
    F = MultiPower(degree5Coeff)
    assert (find_degree([A, F]) == 7)

    #Test Case #3 - Two 3D polynomials of degree 15
    G = MultiPower(np.random.rand(6, 6, 6))
    H = MultiPower(np.random.rand(6, 6, 6))
    assert (find_degree([G, H]) == 29)
Exemplo n.º 15
0
def test_find_degree():
    '''Test Case #1 - 2,3,4, and 5 2D Polynomials of degree 3'''
    degree3Coeff = np.array([
                    [1,1,1,1],
                    [1,1,1,0],
                    [1,1,0,0],
                    [1,0,0,0]])
    A = MultiPower(degree3Coeff)
    B = MultiPower(degree3Coeff)
    C = MultiPower(degree3Coeff)
    D = MultiPower(degree3Coeff)
    E = MultiPower(degree3Coeff)
    assert(find_degree([A,B]) == 5)
    assert(find_degree([A,B,C]) == 7)
    assert(find_degree([A,B,C,D]) == 9)
    assert(find_degree([A,B,C,D,E]) == 11)

    '''Test Case #2 - A 2D polynomials of degree 3 and one of degree 5'''
    degree5Coeff = np.array([
                    [1,1,1,1,1,1],
                    [1,1,1,1,1,0],
                    [1,1,1,1,0,0],
                    [1,1,1,0,0,0],
                    [1,1,0,0,0,0],
                    [1,0,0,0,0,0]])
    F = MultiPower(degree5Coeff)
    assert(find_degree([A,F]) == 7)

    ''' Test Case #3 - Two 3D polynomials of degree 15'''
    G = MultiPower(np.random.rand(6,6,6))
    H = MultiPower(np.random.rand(6,6,6))
    assert(find_degree([G,H]) == 29)

    #Test 3 - Simple Example in 2D
    poly1 = MultiPower(np.array([[3,0,1],[0,0,0],[0,0,1]]))
    poly2 = MultiPower(np.array([[3,0],[1,1],[0,1]]))
    found_degree = find_degree([poly1,poly2])
    correct_degree = 6
    assert found_degree == correct_degree

    #Test 4 - Simple Example in 3D
    a = np.zeros((4,4,4))
    a[3,3,3] = 1
    poly1 = MultiCheb(a)
    poly2 = MultiCheb(np.ones((3,5,4)))
    poly3 = MultiCheb(np.ones((2,4,5)))
    found_degree1 = find_degree([poly1,poly2,poly3])
    correct_degree1 = 24
    assert found_degree1 == correct_degree1
Exemplo n.º 16
0
def test_sorted_polys_coeff():
    A = MultiPower(
        np.array([[2, 0, -3, 0, 0], [0, 1, 0, 0, 0], [-2, 0, 0, 0, 0],
                  [0, 0, 4, 0, 0], [0, 0, 0, 0, -2]]))
    B = MultiPower(
        np.array([[3, 0, -5, 0, 0, 4, 2, -6, 3, -6],
                  [-2, 0, -1, 1, -1, 4, 2, -6, -5, -2],
                  [-1, 3, 2, -2, 0, 4, -1, -2, -4, 6],
                  [4, 2, 5, 9, 0, 3, 2, -1, -3, -3],
                  [3, -3, -5, -2, 0, 4, -2, 2, 1, -6]]))

    assert ([A, B] == ut.sorted_polys_coeff([A, B]))

    C = MultiPower(np.array([[1]]))
    assert ([C, A, B] == ut.sorted_polys_coeff([A, B, C]))
Exemplo n.º 17
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)))
Exemplo n.º 18
0
def roots_at_inf(f):
    '''
    Finds the roots at infinity of a homogenous, bivarite power-basis polynomial.

    Parameters
    ----------
    poly: a bivariate power-basis polynomial

    returns
    -------
    the roots at infinity of the polynomial (list of tuples). In the form (x,y). Since at infinity, z = 0
    '''
    #get the coefficients of the homogenous part of f: [y^d ... x^d]
    f_d_coefs = np.diag(np.fliplr(pad_with_zeros(f.coeff)))

    #find all x s.t. f_d(x,1) == 0
    #essentially, set y = 1 and solve a 1D polynomial with coefficients from f_d
    #if there is only 1 nonzero coefficient in this 1D polynomial have to handle separately
    if sum(f_d_coefs != 0) == 1:
        if np.where(f_d_coefs != 0) == np.array([0]): #if the nonzero coef in the 1D poly is the constant, only root at inf is (1,0)
            return [(1,0)]
        else: #f_d(x,y) = x^a y^b, so 0 = f_d(x,1) == x^a implies x = 0
            x_coords = np.array([0])
    else:
        x_coords = solve(MultiPower(f_d_coefs))

    inf_roots_f_d = list(zip(x_coords, np.ones(len(x_coords))))

    #check if f_d(1,0) == 0
    #f_d(1,0) = the coef of x^d
    if np.isclose(f_d_coefs[-1],0):
        inf_roots_f_d.append((1,0))

    return inf_roots_f_d
Exemplo n.º 19
0
def _random_poly(_type, dim):
    '''
    Generates a random polynomial that has the form
    c_1x_1 + c_2x_2 + ... + c_nx_n where n = dim and each c_i is a randomly
    chosen integer between 0 and 1000.

    Parameters
    ----------
    _type : string
        Type of Polynomial to generate. "MultiCheb" or "MultiPower".
    dim : int
        Degree of polynomial to generate (?).

    Returns
    -------
    Polynomial
        Randomly generated Polynomial.
    '''
    _vars = get_var_list(dim)

    random_poly_shape = [2 for i in range(dim)]

    random_poly_coeff = np.zeros(tuple(random_poly_shape), dtype=int)
    for var in _vars:
        random_poly_coeff[var] = np.random.randint(1000)

    if _type == 'MultiCheb':
        return MultiCheb(random_poly_coeff), _vars
    else:
        return MultiPower(random_poly_coeff), _vars
Exemplo n.º 20
0
def testReducePoly_4():
    poly = MultiPower(
        np.array([[[-1, 2, 0], [0, 0, 0], [-3, 0, 0]],
                  [[0, 0, 0], [2, 0, 0], [0, 0, 0]],
                  [[0, 0, 0], [0, 0, 1], [0, 0, 0]]]))
    d1 = MultiPower(np.array([[[0, -3, 0], [0, 0, 0], [1, 0, 0]]]))
    d2 = MultiPower(np.array([[[0, 0, 0, 1], [4, 0, 0, 0]]]))
    d3 = MultiPower(np.array([[[-1]], [[1]]]))

    basisSet = set()
    for i in range(2):
        for j in range(3):
            for k in range(1):
                basisSet.add((k, i, j))

    reduced = rf.reduce_poly(poly, [d1, d2, d3], basisSet)

    assert (np.all(
        MultiPower(reduced).coeff == np.array([[[-1, -7, 0], [2, 0, 1]]])))
Exemplo n.º 21
0
def test_evaluate():
    # Evaluate .5xyz + 2x + y + z at (4,2,1)
    poly = MultiPower(np.array([[[0,1,0],
                                [1,0,0],
                                [0,0,0]],
                                [[2,0,0],
                                [0,.5,0],
                                [0,0,0]]]))

    assert(poly((4,2,1)) == 15)
Exemplo n.º 22
0
def getPoly(deg, power):
    '''
    A helper function for testing. Returns a random 1D polynomial of the given degree.
    power is a boolean indicating whether or not the polynomial should be MultiPower.
    '''
    coeff = np.random.random_sample(deg+1)
    if power:
        return MultiPower(coeff)
    else:
        return MultiCheb(coeff)
Exemplo n.º 23
0
def test_common_root_at_inf():

    f = MultiPower(np.array([[0, 1]]))  #f(x,y) = y
    g = MultiPower(np.array([[0, 0, -1], [1, 0, 0]]))  #f(x,y) = x - y^2
    #assert common_root_at_inf([f,g]) == True

    f = MultiPower(np.array([[0, 1]]))  #f(x,y) = y
    g = MultiPower(np.array([[0, 0, -1], [1, 0, 0]]).T)  #f(x,y) = x^2 - y
    #assert common_root_at_inf([f,g]) == False

    #from ./Easy/dpdx-dpdy_ex016.mat, which is a dupliate of ./Easy/dpdx-dpdy_ex007.mat and almost the same as ./Easy/dpdx-dpdy_ex008.mat
    f = MultiPower(
        np.array([[-0.21875, 0., 1.3125, 0., -1.09375, 0., 0.21875],
                  [0., -2.625, 0., 4.375, 0., -1.3125, 0.],
                  [1.3125, 0., -6.5625, 0., 3.28125, 0., 0.],
                  [0., 4.375, 0., -4.375, 0., 0., 0.],
                  [-1.09375, 0., 3.28125, 0., 0., 0., 0.],
                  [0., -1.3125, 0., 0., 0., 0., 0.],
                  [0.21875, 0., 0., 0., 0., 0., 0.]]))
    g = MultiPower(
        np.array([[0., -0.65625, 0., 1.09375, 0., -0.328125],
                  [0.65625, 0., -3.28125, 0., 1.640625, 0.],
                  [0., 3.28125, 0., -3.28125, 0., 0.],
                  [-1.09375, 0., 3.28125, 0., 0., 0.],
                  [0., -1.640625, 0., 0., 0., 0.],
                  [0.328125, 0., 0., 0., 0., 0.]]))
    assert common_root_at_inf([f, g]) == True

    #from ./Easy/dpdx-dpdy_C1.mat
    f = MultiPower(
        np.array([[0., 1., -1.5, -0.5, -1.875, 0., 0.875],
                  [0., -3., 1.5, 1.5, -0.625, 0., 0.],
                  [-0.125, 2.5, 0.375, -1.5, 1.25, 0., 0.],
                  [0.125, 0.5, -0.75, 0.5, 0., 0., 0.],
                  [0.125, -1.5, 0.375, 0., 0., 0., 0.],
                  [-0.125, 0.5, 0., 0., 0., 0., 0.]]))
    g = MultiPower(
        np.array([[0., 0.25, 9.], [0., 0., 0.], [-0.75, -1., 0.], [0, 0, 0]]))
    assert common_root_at_inf([f, g]) == False

    f = MultiPower(np.array([[0., 0., 0., 1.]]))
    g = MultiPower(np.array([[0., 0., 0., 0., 1.25]]))
    assert common_root_at_inf([f, g]) == True
Exemplo n.º 24
0
def testMultMatrix_2():
    f1 = MultiPower(np.array([[0, -1.5, .5], [-1.5, 1.5, 0], [1, 0, 0]]))
    f2 = MultiPower(np.array([[0, 0, 0], [-1, 0, 1], [0, 0, 0]]))
    f3 = MultiPower(
        np.array([[0, -1, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]))

    GB = [f1, f2, f3]
    VB = rf.vectorSpaceBasis(GB)[0]

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

    mx_Eig = np.linalg.eigvals(rf.multMatrix(x, GB, VB))
    my_Eig = np.linalg.eigvals(rf.multMatrix(y, GB, VB))

    assert (len(mx_Eig) == 5)
    assert (len(my_Eig) == 5)
    assert (np.allclose(mx_Eig, [-1., 2., 1., 1., 0.]))
    assert (np.allclose(my_Eig, [1., -1., 1., -1., 0.]))
Exemplo n.º 25
0
def testReducePoly_3():
    poly = MultiPower(
        np.array([[0, -1, 0, 1], [0, 2, 0, 0], [0, 0, 1, 0], [1, 0, 0, 0]]))

    g1 = MultiPower(np.array([[0, 0, 0], [-2, 0, 0], [1, 0, 0]]))

    g2 = MultiPower(
        np.array([[0, -1, 0, 1], [3, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]))
    basisSet = set()
    basisSet.add((0, 0))
    basisSet.add((0, 1))
    basisSet.add((0, 2))
    basisSet.add((1, 0))
    basisSet.add((1, 1))
    basisSet.add((1, 2))

    reduced = rf.reduce_poly(poly, [g1, g2], basisSet)
    assert (np.all(
        MultiPower(reduced).coeff == np.array([[0, 0, 0], [1, 2, 2]])))
Exemplo n.º 26
0
def test_mon_mult():
    """
    Tests monomial multiplication using normal polynomial multiplication.
    """

    np.random.seed(4)

    #Simple 2D test cases
    cheb1 = MultiCheb(np.array([[0, 0, 0], [0, 0, 0], [0, 0, 1]]))
    mon1 = (1, 1)
    result1 = cheb1.mon_mult(mon1)
    truth1 = np.array([[0, 0, 0, 0], [0, 0.25, 0, 0.25], [0, 0, 0, 0],
                       [0, 0.25, 0, 0.25]])

    assert np.allclose(result1.coeff, truth1)

    #test with random matrices
    cheb2 = np.random.randint(-9, 9, (4, 4))
    C1 = MultiCheb(cheb2)
    C2 = cheb2poly(C1)
    C3 = MultiCheb.mon_mult(C1, (1, 1))
    C4 = MultiPower.mon_mult(C2, (1, 1))
    C5 = poly2cheb(C4)

    assert np.allclose(C3.coeff, C5.coeff)

    # test results of chebyshev mult compared to power multiplication
    cheb3 = np.random.randn(5, 4)
    c1 = MultiCheb(cheb3)
    c2 = MultiCheb(np.ones((4, 2)))
    for index, i in np.ndenumerate(c2.coeff):
        if sum(index) == 0:
            c3 = c1.mon_mult(index)
        else:
            c3 = c3 + c1.mon_mult(index)
    p1 = cheb2poly(c1)
    p2 = cheb2poly(c2)
    p3 = p1 * p2
    p4 = cheb2poly(c3)
    assert np.allclose(p3.coeff, p4.coeff)

    # test results of chebyshev mult compared to power multiplication in 3D
    cheb4 = np.random.randn(3, 3, 3)
    a1 = MultiCheb(cheb4)
    a2 = MultiCheb(np.ones((3, 3, 3)))
    for index, i in np.ndenumerate(a2.coeff):
        if sum(index) == 0:
            a3 = a1.mon_mult(index)
        else:
            a3 = a3 + a1.mon_mult(index)
    q1 = cheb2poly(a1)
    q2 = cheb2poly(a2)
    q3 = q1 * q2
    q4 = cheb2poly(a3)
    assert np.allclose(q3.coeff, q4.coeff)
Exemplo n.º 27
0
def getPoly(deg, dim):
    '''
    A helper function for testing. Returns a random upper triangular polynomial of the given dimension and degree.
    power is a boolean indicating whether or not the polynomial should be MultiPower.
    '''
    deg += 1
    ACoeff = np.random.random_sample(deg * np.ones(dim, dtype=int))
    for i, j in np.ndenumerate(ACoeff):
        if np.sum(i) >= deg:
            ACoeff[i] = 0
    return MultiPower(ACoeff)
Exemplo n.º 28
0
def test_evaluate2():
    # Evaluate -.5x^2y + 2xy^2 - 3z^2 + yz at (7.4,2.33,.25)
    poly = MultiPower(np.array([[[0,0,-3],
                                [0,1,0],
                                [0,0,0]],
                                [[0,0,0],
                                [0,0,0],
                                [2,0,0]],
                                [[0,0,0],
                                [-.5,0,0],
                                [0,0,0]]]))

    assert(np.isclose(poly((7.4, 2.33, .25)), 16.94732))
Exemplo n.º 29
0
def test_evaluate_grid2():
    #Evaluate zy^2 + 3z^2 + 3x + zx^2y^2 on grid {0, 1, 2}X{0, 1, 2}X{0, 1, 2}
    poly = MultiPower(np.array([[[0,0,3],
                                [0,0,0],
                                [0,1,0]],
                                [[3,0,0],
                                [0,0,0],
                                [0,0,0]],
                                [[0,0,0],
                                [0,0,0],
                                [0,1,0]]]))
    x = np.arange(3)
    xyz = np.column_stack([x,x,x])
    sol = np.array([[[0,3,12],
                    [0,4,14],
                    [0,7,20]],
                    [[3,6,15],
                    [3,8,19],
                    [3,14,31]],
                    [[6,9,18],
                    [6,14,28],
                    [6,29,58]]])
    assert(np.all(poly.evaluate_grid(xyz) == sol))
Exemplo n.º 30
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)))