Пример #1
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]))
Пример #2
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()
Пример #3
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)
Пример #4
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)
Пример #5
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])))
Пример #6
0
    def calc_s(self, a, b):
        '''
        Calculates the S-polynomial of a,b
        '''
        lcm = self._lcm(a, b)
        a_coeffs = np.zeros_like(a.coeff)
        a_coeffs[tuple([i - j for i, j in zip(lcm, a.lead_term)
                        ])] = 1. / (a.coeff[tuple(a.lead_term)])

        b_coeffs = np.zeros_like(b.coeff)
        b_coeffs[tuple([i - j for i, j in zip(lcm, b.lead_term)
                        ])] = 1. / (b.coeff[tuple(b.lead_term)])

        if isinstance(a, MultiPower) and isinstance(b, MultiPower):
            b_ = MultiPower(b_coeffs)
            a_ = MultiPower(a_coeffs)
        elif isinstance(a, MultiCheb) and isinstance(b, MultiCheb):
            b_ = MultiCheb(b_coeffs)
            a_ = MultiCheb(a_coeffs)
        else:
            raise ValueError('Incompatiable polynomials')
        a1 = a_ * a
        b1 = b_ * b
        s = a_ * a - b_ * b
        #self.polys.append(s)
        return s
Пример #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)
Пример #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_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)))
Пример #11
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), \
            "Failed on MultiPower in 2 vars."
Пример #12
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)))
Пример #13
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]])))
Пример #14
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]]])))
Пример #15
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])
    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(values_at_roots==0))
Пример #16
0
def multMatrix(poly, GB, basis):
    '''
    Finds the matrix of the linear operator m_f on A = C[x_1,...,x_n]/I
    where f is the polynomial argument. The linear operator m_f is defined
    as m_f([g]) = [f]*[g] where [f] represents the coset of f in
    A. Since m_f is a linear operator on A, it can be represented by its
    matrix with respect to the vector space basis.

    parameters
    ----------
    poly : polynomial object
        The polynomial f for which to find the matrix m_f.
    GB: list of polynomial objects
        Polynomials that make up a Groebner basis for the ideal
    basis : list of tuples
        The monomials that make up a basis for the vector space A

    return
    ------
    multOperatorMatrix : square numpy array
        The matrix m_f
    '''

    # Reshape poly's coefficienet matrix if it is not in the same number
    # of variables as the polynomials in the Groebner basis.
    # (i.e. len(shape) is the number of variables the polynomial is in)

    numVars = len(GB[0].shape)

    polyVars = len(poly.coeff.shape)
    if polyVars != numVars:
        new_shape = [i for i in poly.coeff.shape]
        for j in range(numVars-polyVars): new_shape.append(1)
        if type(poly) is MultiPower:
            poly = MultiPower(poly.coeff.reshape(tuple(new_shape)))
        if type(poly) is MultiCheb:
            poly = MultiCheb(poly.coeff.reshape(tuple(new_shape)))

    dim = len(basis)
    operatorMatrix = np.zeros((dim, dim))

    for i in range(dim):
        monomial = basis[i]
        poly_ = poly.mon_mult(monomial)

        operatorMatrix[:,i] = coordinateVector(poly_, GB, basis)

    return operatorMatrix
Пример #17
0
    def sm_to_poly(self, rows, reduced_matrix):
        '''
        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 self.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 self.power:
                poly = MultiPower(coeff)
                p_list.append(poly)
            else:
                poly = MultiCheb(coeff)
                p_list.append(poly)
        return p_list
Пример #18
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.]))
Пример #19
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)

    #This breaks becasue it hasn't been initialized.
    #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)

    #This breaks becasue it hasn't been initialized.
    #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)
    assert (grob.reduce_matrix())
    #assert(len(grob.old_polys) == 3)
    assert (len(grob.new_polys) == 2)
Пример #20
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)
Пример #21
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."
Пример #22
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))
Пример #23
0
 def calc_r(self, m):
     '''
     Finds the r polynomial that has a leading monomial m
     Returns the polynomial.
     '''
     for p in self.new_polys + self.old_polys:
         l = list(p.lead_term)
         if all([i <= j for i, j in zip(l, m)
                 ]) and len(l) == len(m):  #Checks to see if l divides m
             c = [j - i for i, j in zip(l, m)]
             if not l == m:  #Make sure c isn't all 0
                 return p.mon_mult(c)
     if self.power:
         return MultiPower(np.array([0]))
     else:
         return MultiCheb(np.array([0]))
Пример #24
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])

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

    assert(np.all(values_at_roots==0))
Пример #25
0
 def add_r_to_matrix(self):
     '''
     Makes Heap out of all monomials, and finds lcms to add them into the matrix
     '''
     for monomial in self.term_set:
         m = list(monomial)
         for p in self.polys:
             l = list(p.lead_term)
             if all([i <= j for i, j in zip(l, m)]) and len(l) == len(m):
                 c = [j - i for i, j in zip(l, m)]
                 c_coeff = np.zeros(np.array(self.largest_mon.val) + 1)
                 c_coeff[tuple(c)] = 1
                 if isinstance(p, MultiCheb):
                     c = MultiCheb(c_coeff)
                 elif isinstance(p, MultiPower):
                     c = MultiPower(c_coeff)
                 r = c * p
                 self.add_poly_to_matrix(r)
                 break
     pass
Пример #26
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()
    VB = rf.vectorSpaceBasis(GB)

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

    mx_RealEig = [eig.real for eig in \
        np.linalg.eigvals(rf.multMatrix(x, GB, VB)) if (eig.imag == 0)]

    my_RealEig = [eig.real for eig in \
        np.linalg.eigvals(rf.multMatrix(y, GB, VB)) if (eig.imag==0)]

    mz_RealEig = [eig.real for eig in \
        np.linalg.eigvals(rf.multMatrix(z, GB, VB)) if (eig.imag==0)]

    assert (len(mx_RealEig) == 2)
    assert (len(my_RealEig) == 2)
    assert (len(mz_RealEig) == 2)
    assert (np.allclose(mx_RealEig, [-1.100987715, .9657124563], atol=1.e-8))
    assert (np.allclose(my_RealEig, [-2.878002536, -2.81249605], atol=1.e-8))
    assert (np.allclose(mz_RealEig, [3.071618528, -2.821182227], atol=1.e-8))
Пример #27
0
def test_mon_mult():
    """
    Tests monomial multiplication using normal polynomial multiplication.
    """

    mon = (1, 2)
    Poly = MultiPower(
        np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],
                  [13, 14, 15, 16]]))
    mon_matr = MultiPower(
        np.array([[0, 0, 0, 0], [0, 0, 1, 0], [0, 0, 0, 0], [0, 0, 0, 0]]))
    P1 = mon_matr * Poly
    P2 = MultiPower.mon_mult(Poly, mon)

    mon2 = (0, 1, 1)
    Poly2 = MultiPower(np.arange(1, 9).reshape(2, 2, 2))
    mon_matr2 = MultiPower(np.array([[[0, 0], [0, 1]], [[0, 0], [0, 0]]]))
    T1 = mon_matr2 * Poly2
    T2 = MultiPower.mon_mult(Poly2, mon2)

    assert np.allclose(P1.coeff, P2.coeff, atol=1.0e-10)
    assert np.allclose(T1.coeff, T2.coeff, atol=1.0e-10)
Пример #28
0
def test_solve():
    #First Test
    A = MultiPower(np.array([[-10, 0], [0, 1], [1, 0]]))
    B = MultiPower(np.array([[-26, 0, 0], [0, 0, 1], [0, 0, 0], [1, 0, 0]]))
    C = MultiPower(
        np.array([[-70, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0],
                  [1, 0, 0, 0]]))
    grob = Groebner([A, B, C])
    X = MultiPower(np.array([[-2.], [1.]]))
    Y = MultiPower(np.array([[-3., 1.]]))
    x1, y1 = grob.solve()
    assert (np.any([X == i and Y == j for i, j in permutations((x1, y1), 2)]))

    #Second Test
    A = MultiPower(
        np.array([[[[
            0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0
        ],
                    [
                        -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, 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, 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, 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
                    ]]]]))
    B = MultiPower(
        np.array([[[[
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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
                    ]],
                   [[
                       -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, 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, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0
                    ]]]]))
    C = MultiPower(
        np.array([[[[
            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, 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, 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
                    ]]],
                  [[[
                      -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, 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, 0, 0, 0,
                        0, 0, 0, 0, 0, 0, 0, 0
                    ]]]]))
    grob = Groebner([A, B, C])
    w1, x1, y1, z1 = grob.solve()

    W = MultiPower(
        np.array([[[[0.], [0.]], [[0.], [0.]], [[0.], [0.]], [[1.], [0.]]],
                  [[[0.], [-1.]], [[0.], [0.]], [[0.], [0.]], [[0.], [0.]]]]))
    X = MultiPower(
        np.array([[[[0., 0., 0., 0., 0., 1.], [-1., 0., 0., 0., 0., 0.]]]]))
    Y = MultiPower(np.array([[[[0.], [0.], [1.]], [[-1.], [0.], [0.]]]]))
    Z = MultiPower(
        np.array([[[[0.], [0.]], [[0.], [0.]], [[0.], [1.]]],
                  [[[-1.], [0.]], [[0.], [0.]], [[0.], [0.]]]]))

    assert (np.any([
        W == i and X == j and Y == k and Z == l
        for i, j, k, l in permutations((w1, x1, y1, z1), 4)
    ]))

    #Third Test
    A = MultiPower(np.array([[-1, 0, 1], [0, 0, 0]]))
    B = MultiPower(np.array([[-1, 0, 0], [0, 1, 0], [1, 0, 0]]))
    grob = Groebner([A, B])
    x1, y1 = grob.solve()
    assert (np.any([A == i and B == j for i, j in permutations((x1, y1), 2)]))

    #Fourth Test
    A = MultiPower(np.array([[-10, 0], [0, 1], [1, 0]]))
    B = MultiPower(np.array([[-25, 0, 0], [0, 0, 1], [0, 0, 0], [1, 0, 0]]))
    C = MultiPower(
        np.array([[-70, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0], [0, 0, 0, 0],
                  [1, 0, 0, 0]]))
    grob = Groebner([A, B, C])
    X = MultiPower(np.array([[1.]]))
    x1 = grob.solve()
    assert (X == x1[0])

    #Fifth Test
    A = MultiPower(np.array([[1, 1], [0, 0]]))
    B = MultiPower(np.array([[1, 0], [1, 0]]))
    C = MultiPower(np.array([[1, 0], [1, 0], [0, 1]]))
    grob = Groebner([A, B, C])
    X = MultiPower(np.array([[1.]]))
    x1 = grob.solve()
    assert (X == x1[0])
Пример #29
0
def testReducePoly_2():
    poly = MultiPower(np.array([[-7], [2], [-13], [4]]))
    g = MultiPower(np.array([[-2], [3], [1]]))

    reduced = rf.reduce_poly(poly, [g])
    assert (np.all(reduced.coeff == np.array([[-57.], [85.]])))
Пример #30
0
def testReducePoly():
    poly = MultiPower(np.array([[-3], [2], [-4], [1]]))
    g = MultiPower(np.array([[2], [1]]))

    reduced = rf.reduce_poly(poly, [g])
    assert (reduced.coeff == np.array([[-31.]]))