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]))
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))
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)))
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, 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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])
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])
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)
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)) print("Initializing") grob.initialize_np_matrix() print(grob.np_matrix.shape) print("ADDING PHI's") grob.add_phi_to_matrix(False) print(grob.np_matrix.shape) print("ADDING r's") grob.add_r_to_matrix() print(grob2.np_matrix.shape) polys = grob.reduce_matrix() i += 1 print("WE WIN") return grob.reduce_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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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]], [[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], [ 0, 0, 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])