Exemple #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]))
Exemple #2
0
def roots(polys, method = 'Groebner'):
    '''
    Finds the roots of the given list of polynomials

    parameters
    ----------
    polys : list of polynomial objects
        polynomials to find the common roots of

    returns
    -------
    list of numpy arrays
        the common roots of the polynomials
    '''
    
    times["reducePoly"] = 0
    Polynomial.clearTime()
    
    startTime = time.time()
    
    # Determine polynomial type
    poly_type = ''
    if (all(type(p) == MultiCheb for p in polys)):
        poly_type = 'MultiCheb'
    elif (all(type(p) == MultiPower for p in polys)):
        poly_type = 'MultiPower'
    else:
        raise ValueError('All polynomials must be the same type')
    # Calculate groebner basis
    startBasis = time.time()
    if method == 'Groebner':
        G = Groebner(polys)
        GB = G.solve()
    else:
        GB = Macaulay(polys)
    endBasis = time.time()
    times["basis"] = (endBasis - startBasis)
    dim = max(g.dim for g in GB) # dimension of the polynomials

    # Check for no solutions
    if len(GB) == 1 and all([i==1 for i in GB[0].coeff.shape]):
        print("No solutions")
        return -1

    startRandPoly = time.time()
    # Make sure ideal is zero-dimensional and get random polynomial
    f, var_list = _random_poly(poly_type, dim)
    if not _test_zero_dimensional(var_list, GB):
        print("Ideal is not zero-dimensional; cannot calculate roots.")
        return -1
    endRandPoly = time.time()
    times["randPoly"] = (endRandPoly - startRandPoly)

    # Get multiplication matrix
    startVectorBasis = time.time()
    VB, var_dict = vectorSpaceBasis(GB)
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()
Exemple #4
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)))
Exemple #5
0
def test_sorted_polys_monomial():
    A = 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])
    x = grob.sorted_polys_monomial([A])
    assert (A == x[0])

    B = 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]]))
    assert (list((B, A)) == grob.sorted_polys_monomial([B, A]))

    C = MultiPower(
        np.array([[0, 0, -3, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0],
                  [0, 0, 4, 0, 0], [0, 0, 0, 0, -2]]))
    assert (list((C, B, A)) == grob.sorted_polys_monomial([A, B, C]))

    D = MultiPower(
        np.array([[2, 0, -3, 0, 0], [0, 1, 0, 0, 0], [-2, 0, 2, 0, 0],
                  [0, 0, 4, 0, 0], [0, 0, 0, 0, -2]]))
    assert (list((C, B, D, A)) == grob.sorted_polys_monomial([B, D, C, A]))

    E = 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, 2, 4, -2, 2, 1, -6]]))
    assert (list(
        (C, B, D, A, E)) == grob.sorted_polys_monomial([B, D, E, C, A]))

    F = MultiPower(
        np.array([[3, 0, -5, 0, 0, 0, 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 (list(
        (C, B, D, F, A, E)) == grob.sorted_polys_monomial([F, B, D, E, C, A]))
    pass
Exemple #6
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())
Exemple #7
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])
Exemple #8
0
def test_init_():
    C = MultiPower(np.array([[-1, 0, 1], [0, 0, 0]]))
    D = MultiCheb(np.array([[-1, 0, 0], [0, 1, 0], [1, 0, 0]]))
    with pytest.raises(ValueError):
        grob = Groebner([C, D])
    pass
Exemple #9
0
def test_phi_criterion():
    # Same as grob.solve(), but added true/false to test the phi's.
    # *WARNING* MAKE SURE TO CHANGE solve_phi() test to match .solve method always!
    def solve_phi(grob, phi=True):
        polys = True
        i = 1
        while polys:
            print("Starting Loop #" + str(i))
            grob.initialize_np_matrix()
            grob.add_phi_to_matrix(phi)
            grob.add_r_to_matrix()
            grob.create_matrix()
            print(grob2.np_matrix.shape)
            polys = grob.reduce_matrix()
            i += 1
        print("WE WIN")
        grob.get_groebner()
        print("Basis - ")

        grob.reduce_groebner_basis()

        for poly in grob.groebner_basis:
            print(poly.coeff)

        return grob.groebner_basis

    # Simple Test Case (Nothing gets added )
    A = MultiPower(np.array([[-1, 0, 1], [0, 0, 0]]))
    B = MultiPower(np.array([[-1, 0, 0], [0, 1, 0], [1, 0, 0]]))
    grob1 = Groebner([A, B])
    grob2 = Groebner([A, B])

    x1, y1 = solve_phi(grob1, True)
    x2, y2 = solve_phi(grob2, False)

    assert (np.any([x2 == i and y2 == j for i, j in permutations((x1, y1), 2)
                    ])), "Not the same basis!"

    #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
                    ]]]]))
    grob1 = Groebner([A, B, C])
    grob2 = Groebner([A, B, C])

    w1, x1, y1, z1 = solve_phi(grob1, True)
    w2, x2, y2, z2 = solve_phi(grob2, False)

    assert (np.any([
        w2 == i and x2 == j and y2 == k and z2 == 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]]))
    grob1 = Groebner([A, B])
    grob2 = Groebner([A, B])
    x1, y1 = solve_phi(grob1, True)
    x2, y2 = solve_phi(grob2, False)

    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]]))
    grob1 = Groebner([A, B, C])
    grob2 = Groebner([A, B, C])
    x1 = solve_phi(grob1, True)
    x2 = solve_phi(grob2, False)

    assert (x2[0] == 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]]))
    grob1 = Groebner([A, B, C])
    grob2 = Groebner([A, B, C])
    x1 = solve_phi(grob1, True)
    x2 = solve_phi(grob2, False)
    assert (x2[0] == x1[0])
Exemple #10
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)
Exemple #11
0
def roots(polys, method='Groebner'):
    '''
    Finds the roots of the given list of polynomials

    parameters
    ----------
    polys : list of polynomial objects
        polynomials to find the common roots of

    returns
    -------
    list of numpy arrays
        the common roots of the polynomials
    '''

    times["reducePoly"] = 0
    Polynomial.clearTime()

    startTime = time.time()

    # Determine polynomial type
    poly_type = ''
    if (all(type(p) == MultiCheb for p in polys)):
        poly_type = 'MultiCheb'
    elif (all(type(p) == MultiPower for p in polys)):
        poly_type = 'MultiPower'
    else:
        raise ValueError('All polynomials must be the same type')
    # Calculate groebner basis
    startBasis = time.time()
    if method == 'Groebner':
        G = Groebner(polys)
        GB = G.solve()
    else:
        GB = Macaulay(polys)
    endBasis = time.time()
    times["basis"] = (endBasis - startBasis)
    dim = max(g.dim for g in GB)  # dimension of the polynomials

    # Check for no solutions
    if len(GB) == 1 and all([i == 1 for i in GB[0].coeff.shape]):
        print("No solutions")
        return -1

    startRandPoly = time.time()
    # Make sure ideal is zero-dimensional and get random polynomial
    f, var_list = _random_poly(poly_type, dim)
    if not _test_zero_dimensional(var_list, GB):
        print("Ideal is not zero-dimensional; cannot calculate roots.")
        return -1
    endRandPoly = time.time()
    times["randPoly"] = (endRandPoly - startRandPoly)

    # Get multiplication matrix
    startVectorBasis = time.time()
    VB, var_dict = vectorSpaceBasis(GB)
    endVectorBasis = time.time()
    times["vectorBasis"] = (endVectorBasis - startVectorBasis)
    #print("VB:", VB)
    #print("var_dict:", var_dict)
    startMultMatrix = time.time()
    m_f = multMatrix(f, GB, VB)
    endMultMatrix = time.time()
    times["multMatrix"] = (endMultMatrix - startMultMatrix)

    startEndStuff = time.time()
    # Get list of indexes of single variables and store vars that were not
    # in the vector space basis.
    var_indexes = np.array([-1 for i in range(dim)])
    vars_not_in_basis = {}
    for i in range(len(var_list)):
        var = var_list[i]  # x_i
        if var in var_dict:
            var_indexes[i] = var_dict[var]
        else:
            # maps the position in the root to its variable
            vars_not_in_basis[i] = var

    vnib = False
    if len(vars_not_in_basis) != 0:
        vnib = True

    # Get left eigenvectors
    eig = np.linalg.eig(m_f.T)[1]
    num_vectors = eig.shape[1]
    eig_vectors = [eig[:, i].tolist()
                   for i in range(num_vectors)]  # columns of eig

    roots = []
    for v in eig_vectors:
        root = np.zeros(dim, dtype=complex)
        # This will always work because var_indexes and root have the
        # same length - dim - and var_indexes has the variables in the
        # order they should be in the root
        for i in range(dim):
            x_i_pos = var_indexes[i]
            if x_i_pos != -1:
                root[i] = v[x_i_pos] / v[0]
        if vnib:
            # Go through the indexes of variables not in the basis in
            # decreasing order. It must be done in decreasing order for the
            # roots to be calculated correctly, since the vars with lower
            # indexes depend on the ones with higher indexes
            for pos in list(vars_not_in_basis.keys())[::-1]:
                GB_poly = _get_poly_with_LT(vars_not_in_basis[pos], GB)
                var_value = GB_poly.evaluate_at(root) * -1
                root[pos] = var_value
        roots.append(root)
    endEndStuff = time.time()
    times["endStuff"] = (endEndStuff - startEndStuff)
    endTime = time.time()
    totalTime = (endTime - startTime)
    print("Total run time for roots is {}".format(totalTime))
    print(times)
    MultiCheb.printTime()
    MultiPower.printTime()
    Polynomial.printTime()
    print((times["basis"] + times["multMatrix"]) / totalTime)
    return roots
Exemple #12
0
A = MultiPower(
    np.array([[[1, 1, 3], [0, 0, 2], [6, 4, 3]],
              [[1, 2, 3], [1, 3, 2], [5, 1, 4]],
              [[2, 4, 3], [4, 1, 2], [4, 2, 3]]]))

B = MultiPower(
    np.array([[[1, 3, 3], [0, 3, 2], [6, 4, 3]],
              [[3, 2, 3], [1, 13, 1], [5, 4, 5]],
              [[2, 1, 3], [4, 1, 2], [2, 1, 2]]]))

C = MultiPower(
    np.array([[[2, 3, 3], [0, 3, -2], [-6, 4, 3]],
              [[-3, 2, 3], [1, 1, 1], [5, 4, 5]],
              [[2, 1, -3], [-4, 1, 2], [-2, 1, 2]]]))

grob = Groebner([A, B, C])
grob.solve()

# Example 2: 2-dimensional system

A = MultiPower(
    np.array([[1, 0, -2, 1], [2, 0, 5, 1], [1, 0, 4, 1], [2, 0, 3, 1]]))
B = MultiPower(
    np.array([[1, 0, 8, 7], [1, 0, 1, 2], [0, 4, 1, 2], [0, 1, 5, 4]]))

grob = Groebner([A, B])
grob.solve()

# Example 3: Step-by-step

# Step 1: Define the system.