示例#1
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])[0]

    assert(all(np.isclose(root, [-2,3])))
示例#2
0
def test_mon_mult_random():
    #test with random matrices
    possible_dim = np.random.randint(1, 5, (1, 10))
    dim = possible_dim[0, random.randint(1, 9)]

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

    shape2 = list()
    for i in range(dim):
        shape2.append(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, new_M4 = MultiPower.match_size(M3, M3, M4)
    else:
        new_M3, new_M4 = M3, M4

    assert np.allclose(new_M3.coeff, new_M4.coeff)
示例#3
0
def _match_poly_dim(poly1, poly2):
    # Do nothing if they are already the same dimension
    if poly1.dim == poly2.dim:
        return poly1, poly2

    poly_type = ''
    if type(poly1) == MultiPower and type(poly2) == MultiPower:
        poly_type = 'MultiPower'
    elif type(poly1) == MultiCheb and type(poly2) == MultiCheb:
        poly_type = 'MultiCheb'
    else:
        raise ValueError('Polynomials must be the same type')

    poly1_vars = poly1.dim
    poly2_vars = poly2.dim
    max_vars = max(poly1_vars, poly2_vars)

    if poly1_vars < max_vars:
        for j in range(max_vars - poly1_vars):
            coeff_reshaped = poly1.coeff[..., np.newaxis]
        if poly_type == 'MultiPower':
            poly1 = MultiPower(coeff_reshaped)
        elif poly_type == 'MultiCheb':
            poly1 = MultiCheb(coeff_reshaped)
    elif poly2_vars < max_vars:
        for j in range(max_vars - poly2_vars):
            coeff_reshaped = poly2.coeff[..., np.newaxis]
        if poly_type == 'MultiPower':
            poly2 = MultiPower(coeff_reshaped)
        elif poly_type == 'MultiCheb':
            poly2 = MultiCheb(coeff_reshaped)

    return poly1, poly2
示例#4
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()
示例#5
0
def test_inverse_P():

    # Simple Test Case.
    C = MultiPower(np.array([[-1, 0, 1], [0, 0, 0]]))
    D = MultiPower(np.array([[-1, 0, 0], [0, 1, 0], [1, 0, 0]]))
    # Creating a random object to run tests.
    grob = Groebner([C, D])

    # Create matrix
    M = np.array([[0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5], [0, 1, 2, 3, 4, 5],
                  [0, 1, 2, 3, 4, 5]])

    # Order of Column flips.
    p = [1, 4, 3, 2, 0, 5]

    # N is the matrix with the columns flipped.
    N = M[:, p]

    # Find the order of columns to flip back to.
    pt = grob.inverse_P(p)
    # Result done by hand.
    pt_inv = [4, 0, 3, 2, 1, 5]
    assert (np.allclose(M, N[:, pt])), "Matrix are not the same."
    assert (all(pt == pt_inv)), "Wrong matrix order."

    # Test Case 2:
    A = np.random.random((5, 10))

    Q, R, p = qr(A, pivoting=True)

    pt = grob.inverse_P(p)

    # We know that A[:,p] = QR, want to see if pt flips QR back to A.
    assert (np.allclose(A, np.dot(Q, R)[:, pt]))
示例#6
0
def test_evaluate_at():
    # 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.evaluate_at((4, 2, 1)) == 15)
示例#7
0
 def test_add(self):
     a1 = np.arange(27).reshape((3, 3, 3))
     Test2 = MultiPower(a1)
     a2 = np.ones((3, 3, 3))
     Test3 = MultiPower(a2)
     addTest = Test2 + Test3
     self.assertTrue(addTest.coeff.all() == (Test2.coeff +
                                             Test3.coeff).all())
示例#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])

    assert(all(np.isclose(roots[0], [-0.61803399,  0.38196601])))
    assert(all(np.isclose(roots[1], [1.61803399,  2.61803399])))
示例#9
0
def test_evaluate_at2():
    # 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.evaluate_at((7.4, 2.33, .25)), 16.94732))
示例#10
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)
示例#11
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)
示例#12
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))
示例#13
0
def test_triangular_solve():
    """This tests the triangular_solve() method. 
    A visual graph of zeroes on the diagonal was also used to test this function.
    """

    # Simple Test Case.
    M = MultiPower(np.array([[-1, 0, 1], [0, 0, 0]]))
    N = MultiPower(np.array([[-1, 0, 0], [0, 1, 0], [1, 0, 0]]))
    # Creating a random object to run tests.
    grob = Groebner([M, N])

    A = np.array([[1, 2, 3, 4, 5], [0, 1, 2, 3, 4], [0, 0, 0, 1, 2]])

    matrix = grob.triangular_solve(A)
    answer = np.array([[1., 0., -1., 0., 1.], [0., 1., 2., 0., -2.],
                       [0., 0., 0., 1., 2.]])
    assert (np.allclose(matrix, answer))

    # Randomize test case: square matrix.
    A = np.random.random((50, 50))
    Q, R, p = qr(A, pivoting=True)
    diagonal = np.diag(R)
    r = np.sum(np.abs(diagonal) > 1e-10)
    matrix = R[:r]
    new_matrix = grob.triangular_solve(matrix)
    true = sy.Matrix(new_matrix).rref()
    x = sy.symbols('x')
    f = sy.lambdify(x, true[0])
    assert (np.allclose(new_matrix, f(1)))

    # Randomize test case: shorter rows than column.
    A = np.random.random((10, 50))
    Q, R, p = qr(A, pivoting=True)
    diagonal = np.diag(R)
    r = np.sum(np.abs(diagonal) > 1e-10)
    matrix = R[:r]
    new_matrix = grob.triangular_solve(matrix)
    true = sy.Matrix(new_matrix).rref()
    x = sy.symbols('x')
    f = sy.lambdify(x, true[0])
    print(f(1))
    print(new_matrix)
    assert (np.allclose(new_matrix, f(1)))

    # Randomize test case: longer rows than columns.
    A = np.random.random((50, 10))
    Q, R, p = qr(A, pivoting=True)
    diagonal = np.diag(R)
    r = np.sum(np.abs(diagonal) > 1e-10)
    matrix = R[:r]
    new_matrix = grob.triangular_solve(matrix)
    true = sy.Matrix(new_matrix).rref()
    x = sy.symbols('x')
    f = sy.lambdify(x, true[0])
    print(f(1))
    print(new_matrix)
    assert (np.allclose(new_matrix, f(1)))
示例#14
0
 def test_mult(self):
     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 = np.array([[0, 2, 2], [4, 9, 2], [6, 3, 0]])
     test = np.allclose(new_poly.coeff, truth)
     self.assertTrue(test)
示例#15
0
def testRoots_7(): # This test sometimes fails due to stability issues...
    f1_coeff = np.zeros((3,3,3))
    f1_coeff[(0,2,0)] = 1
    f1_coeff[(1,1,0)] = -1
    f1_coeff[(1,0,1)] = -2
    f1 = MultiPower(f1_coeff)

    f2_coeff = np.zeros((4,4,4))
    f2_coeff[(0,3,0)] = 1
    f2_coeff[(0,0,2)] = 1
    f2_coeff[(0,0,0)] = 1
    f2 = MultiPower(f2_coeff)

    f3_coeff = np.zeros((3,3,3))
    f3_coeff[(2,1,1)] = 1
    f3_coeff[(0,1,1)] = -1
    f3 = MultiPower(f3_coeff)

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

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

    assert(np.all(np.isclose(values_at_roots, 0)))
示例#16
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]]))

    reduced = rf.reduce_poly(poly, [g1, g2])
    assert (np.all(reduced.coeff == np.array([[0, 0, 0], [1, 2, 2]])))
示例#17
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]]]))

    reduced = rf.reduce_poly(poly, [d1, d2, d3])
    assert (np.all(reduced.coeff == np.array([[[1], [0]], [[0], [2]]])))
示例#18
0
 def test_s_poly(self):
     a2 = np.array([[0, 0, 0, 1], [0, -2, 0, 0], [0, 0, 0, 0], [0, 0, 0,
                                                                0]])
     a3 = np.array([[0, 1, 0, 0], [0, 0, 1, 0], [-2, 0, 0, 0], [0, 0, 0,
                                                                0]])
     c2 = MultiPower(a2.T)
     c3 = MultiPower(a3.T)
     grob = Grobner([c2, c3])
     s1 = np.round(grob.calc_s(c2, c3).coeff)
     true_s = np.array([[0, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 0],
                        [0, 0, 0, 0]])
     self.assertTrue(s1.all() == true_s.all())
示例#19
0
def get_poly_from_matrix(rows,matrix,matrix_terms,power):
    '''
    Takes a list of indicies corresponding to the rows of the reduced matrix and
    returns a list of polynomial objects
    '''
    shape = []
    p_list = []
    matrix_term_vals = [i.val for i in matrix_terms]

    # Finds the maximum size needed for each of the poly coeff tensors
    for i in range(len(matrix_term_vals[0])):
        # add 1 to each to compensate for constant term
        shape.append(max(matrix_term_vals, key=itemgetter(i))[i]+1)
    # Grabs each polynomial, makes coeff matrix and constructs object
    for i in rows:
        p = matrix[i]
        coeff = np.zeros(shape)
        for j,term in enumerate(matrix_term_vals):
            coeff[term] = p[j]

        if power:
            poly = MultiPower(coeff)
        else:
            poly = MultiCheb(coeff)

        if poly.lead_term != None:
            p_list.append(poly)
    return p_list
示例#20
0
def sm_to_poly(items, rows, reduced_matrix, power):
    '''
    Takes a list of indicies corresponding to the rows of the reduced matrix and
    returns a list of polynomial objects
    '''
    shape = []
    p_list = []
    matrix_term_vals = [i.val for i in items['matrix_terms']]

    # Finds the maximum size needed for each of the poly coeff tensors
    for i in range(len(matrix_term_vals[0])):
        # add 1 to each to compensate for constant term
        shape.append(max(matrix_term_vals, key=itemgetter(i))[i] + 1)
    # Grabs each polynomial, makes coeff matrix and constructs object
    for i in rows:
        p = reduced_matrix[i]
        p[np.where(abs(p) < global_accuracy)] = 0
        coeff = np.zeros(shape)
        for j, term in enumerate(matrix_term_vals):
            coeff[term] = p[j]

        if power:
            poly = MultiPower(coeff)
            p_list.append(poly)
        else:
            poly = MultiCheb(coeff)
            p_list.append(poly)
    return p_list
示例#21
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]]))
    grob = Groebner([A, B])
    assert (list((A, B)) == grob.sorted_polys_coeff())

    C = MultiPower(np.array([[1]]))
    grob = Groebner([A, B, C])
    assert (list((A, B, C)) == grob.sorted_polys_coeff())
示例#22
0
def testMultMatrix():
    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]]]))

    F = [f1, f2, f3]
    Gr = Groebner(F)

    GB = Gr.solve()
示例#23
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.]))
示例#24
0
def cheb2poly(T):
    """
    Convert a chebyshev polynomial to a standard polynomial in multiple dimensions.

    """
    dim = len(T.shape)
    A = T.coeff
    for i in range(dim):
        A = np.apply_along_axis(conv_cheb, i, A)
    return MultiPower(A)
示例#25
0
def test_reduce_matrix():
    poly1 = MultiPower(np.array([[1., 0.], [0., 1.]]))
    poly2 = MultiPower(np.array([[0., 0.], [1., 0.]]))
    poly3 = MultiPower(np.array([[1., 0.], [12., -5.]]))
    grob = Groebner([])
    grob.new_polys = list((poly1, poly2, poly3))
    grob.matrix_terms = []
    grob.np_matrix = np.array([])
    grob.term_set = set()
    grob.lead_term_set = set()
    grob._add_polys(grob.new_polys)
    grob.create_matrix()

    assert (grob.reduce_matrix())
    #assert(len(grob.old_polys) == 2)
    assert (len(grob.new_polys) == 1)

    poly1 = MultiPower(np.array([[1., 0.], [0., 0.]]))
    poly2 = MultiPower(np.array([[0., 0.], [1., 0.]]))
    poly3 = MultiPower(np.array([[1., 0.], [12., 0.]]))
    grob = Groebner([])
    grob.new_polys = list((poly1, poly2, poly3))
    grob.matrix_terms = []
    grob.np_matrix = np.array([])
    grob.term_set = set()
    grob.lead_term_set = set()
    grob._add_polys(grob.new_polys)
    grob.create_matrix()

    assert (not grob.reduce_matrix())
    #assert(len(grob.old_polys) == 3)
    assert (len(grob.new_polys) == 0)

    poly1 = MultiPower(np.array([[1., -14.], [0., 2.]]))
    poly2 = MultiPower(np.array([[0., 3.], [1., 6.]]))
    poly3 = MultiPower(np.array([[1., 0.], [12., -5.]]))
    grob = Groebner([])
    grob.new_polys = list((poly1, poly2, poly3))
    grob.matrix_terms = []
    grob.np_matrix = np.array([])
    grob.term_set = set()
    grob.lead_term_set = set()

    grob._add_polys(grob.new_polys)
    grob.create_matrix()
    assert (grob.reduce_matrix())
    #assert(len(grob.old_polys) == 3)
    assert (len(grob.new_polys) == 2)
示例#26
0
    def solve(self, qr_reduction=True, reducedGroebner=True):
        '''
        The main function. Initializes the matrix, adds the phi's and r's, and then reduces it. Repeats until the reduction
        no longer adds any more polynomials to the matrix. Print statements let us see the progress of the code.
        '''
        MultiCheb.clearTime()
        MultiPower.clearTime()
        startTime = time.time()

        polys_were_added = True
        i = 1  #Tracks what loop we are on.
        while polys_were_added:
            #print("Starting Loop #"+str(i))
            #print("Num Polys - ", len(self.new_polys + self.old_polys))
            self.initialize_np_matrix()
            self.add_phi_to_matrix()
            self.add_r_to_matrix()
            self.create_matrix()
            #print(self.np_matrix.shape)
            polys_were_added = self.reduce_matrix(
                qr_reduction=qr_reduction, triangular_solve=False
            )  #Get rid of triangular solve when done testing
            i += 1

        #print("Basis found!")

        self.get_groebner()
        if reducedGroebner:
            self.reduce_groebner_basis()

        endTime = time.time()
        #print("WE WIN")
        print("Run time was {} seconds".format(endTime - startTime))
        print(times)
        MultiCheb.printTime()
        MultiPower.printTime()
        #print("Basis - ")
        #for poly in self.groebner_basis:
        #    print(poly.coeff)
        #break #print just one

        return self.groebner_basis
示例#27
0
def test_mon_mult():
    """
    Tests monomial multiplication using normal polynomial multiplication.
    """

    #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)
示例#28
0
def test_vectorSpaceBasis_2():
    f1 = MultiPower(np.array([[[0,0,1],[0,3/20,0],[0,0,0]],
                              [[0,0,0],[-3/40,1,0],[0,0,0]],
                              [[0,0,0],[0,0,0],[0,0,0]]]))

    f2 = MultiPower(np.array([[[3/16,-5/2,0],[0,3/16,0],[0,0,0]],
                              [[0,0,1],[0,0,0],[0,0,0]],
                              [[0,0,0],[0,0,0],[0,0,0]]]))

    f3 = MultiPower(np.array([[[0,1,1/2],[0,3/40,1],[0,0,0]],
                              [[-1/2,20/3,0],[-3/80,0,0],[0,0,0]],
                              [[0,0,0],[0,0,0],[0,0,0]]]))

    f4 = MultiPower(np.array([[[3/32,-7/5,0,1],[-3/16,83/32,0,0],[0,0,0,0],[0,0,0,0]],
                              [[3/40,-1,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],
                              [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]],
                              [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]]))

    f5 = 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]]]))

    f6 = MultiPower(np.array([[[0,0,0],[0,0,0],[1,0,0]],
                              [[0,-8/3,0],[0,0,0],[0,0,0]],
                              [[0,0,0],[0,0,0],[0,0,0]]]))

    G = [f1, f2, f3, f4, f5, f6]
    basis = rf.vectorSpaceBasis(G)[0]
    trueBasis = [(0,0,0),(1,0,0),(0,1,0),(1,1,0),(0,0,1),(0,0,2),(1,0,1),(0,1,1)]

    assert (len(basis) == len(trueBasis)) and (m in basis for m in trueBasis), \
            "Failed on MultiPower in 3 vars."
示例#29
0
def test_reduce_matrix():
    poly1 = MultiPower(np.array([[1., 0.], [0., 1.]]))
    poly2 = MultiPower(np.array([[0., 0.], [1., 0.]]))
    poly3 = MultiPower(np.array([[1., 0.], [12., -5.]]))

    new_polys = list((poly1, poly2, poly3))
    old_polys = []
    items = {'power': True}
    items = groebner_basis.initialize_np_matrix(new_polys, old_polys, items)
    groebner_basis._add_poly_to_matrix(new_polys, items)
    added, new_polys, old_polys = groebner_basis.reduce_matrix(items)
    #This breaks becasue it hasn't been initialized.
    assert (added)
    assert (len(old_polys) == 2)
    assert (len(new_polys) == 1)

    poly1 = MultiPower(np.array([[1., 0.], [0., 0.]]))
    poly2 = MultiPower(np.array([[0., 0.], [1., 0.]]))
    poly3 = MultiPower(np.array([[1., 0.], [12., 0.]]))
    new_polys = list((poly1, poly2, poly3))
    old_polys = []
    items = {'power': True}
    items = groebner_basis.initialize_np_matrix(new_polys, old_polys, items)
    items = groebner_basis._add_poly_to_matrix(new_polys, items)
    added, new_polys, old_polys = groebner_basis.reduce_matrix(items)

    #This breaks becasue it hasn't been initialized.
    assert (not added)
    #assert(len(new_polys) == 0)
    #assert(len(old_polys) == 3) # --> this gives error right now. old_poly is 2.

    poly1 = MultiPower(np.array([[1., -14.], [0., 2.]]))
    poly2 = MultiPower(np.array([[0., 3.], [1., 6.]]))
    poly3 = MultiPower(np.array([[1., 0.], [12., -5.]]))

    new_polys = list((poly1, poly2, poly3))
    old_polys = []
    items = {'power': True}
    items = groebner_basis.initialize_np_matrix(new_polys, old_polys, items)
    items = groebner_basis._add_poly_to_matrix(new_polys, items)
    added, new_polys, old_polys = groebner_basis.reduce_matrix(items)
    assert (added)
    # assert(len(old_polys) == 3) # --> Also error. len(old_poly) gives 1.
    assert (len(new_polys) == 2)
示例#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])

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

    assert(np.all(abs(values_at_roots)<1.e-5))