def relabel_graph(X): G = state.GRAPH n = len(G.nodes) for i in range(n): num = np.argmax(X[:,i]) G.nodes[num]['label'] = i solver.render()
import sys import numpy as np sys.path.append('..') import core.solver as solver from fft import fft_example from dct import dct_example from matmult import matmult, strassen_matmult # dct_example(3) # solver.render("dct.png") # matmult(2) # solver.render("matmult.png") # strassen_matmult(1) # solver.render("strassen.png") a = np.array(solver.transform_array([1, 2])) b = np.array(solver.transform_array([1, 2])) np.dot(a, b) solver.render("dot.png")
M2 = strassen_helper(A_mats[1][0] + A_mats[1][1], B_mats[0][0]) M3 = strassen_helper(A_mats[0][0], B_mats[0][1] - B_mats[1][1]) M4 = strassen_helper(A_mats[1][1], B_mats[1][0] - B_mats[0][0]) M5 = strassen_helper(A_mats[0][0] + A_mats[0][1], B_mats[1][1]) M6 = strassen_helper(A_mats[1][0] - A_mats[0][0], B_mats[0][0] + B_mats[0][1]) M7 = strassen_helper(A_mats[0][1] - A_mats[1][1], B_mats[1][0] + B_mats[1][1]) if alltogether: C11 = sum_matrices([M1, M4, M5, M7]) #((M1 + M4) - M5) + M7 C12 = M3 + M5 C21 = M2 + M4 C22 = sum_matrices([M1, M2, M3, M6]) # ((M1 - M2) + M3) + M6 else: C11 = ((M1 + M4) - M5) + M7 C12 = M3 + M5 C21 = M2 + M4 C22 = ((M1 - M2) + M3) + M6 if isinstance(C11, np.ndarray): first_row = np.concatenate([C11, C12], axis=1) second_row = np.concatenate([C21, C22], axis=1) return np.concatenate([first_row, second_row], axis=0) else: return np.array([[C11, C12], [C21, C22]]) if __name__ == "__main__": strassen_matmult(1, alltogether=True) solver.render("strassen_mamult_out.png")
import numpy as np import sys sys.path.append('..') import core.solver as solver from convolve import convolve import core.state as state from fft import fft_example from matmult import matmult def relabel_graph(X): G = state.GRAPH n = len(G.nodes) for i in range(n): num = np.argmax(X[:,i]) G.nodes[num]['label'] = i solver.render() filename = "results/matmult/5.gz" X = np.loadtxt(filename) print(X) matmult(2) # convolve(3) # fft_example(2) relabel_graph(X) solver.render("pics/matmult5.png")
neighbors.append(cp) neighboring_nodes = [nodes[stringify_binary(u)] for u in neighbors] if len(neighboring_nodes) > 0: new_node = solver.genop(neighboring_nodes, np.sum) # stand in for the DP step else: new_node = solver.DataNode(0) nodes[stringify_binary(curr_encoding)] = new_node def tsp(n): nodes = {} start_enc = np.zeros(n) curr_list = [start_enc] queued = set([stringify_binary(start_enc)]) while len(curr_list) > 0: curr_enc = curr_list.pop(0) tsp_helper(curr_enc, nodes) for i in range(len(curr_enc)): if curr_enc[i] == 0: cp = np.copy(curr_enc) cp[i] = 1 cp_str = stringify_binary(cp) if cp_str not in queued: curr_list.append(cp) queued.add(cp_str) assert len(queued) == 2**n if __name__ == "__main__": tsp(5) # strassen_matmult(1, alltogether=True) solver.render("hypercube.png")