def main(): A = matrix.create_matrix(stdin.readline().split()) B = matrix.create_matrix(stdin.readline().split()) pi = matrix.create_matrix(stdin.readline().split()) emissions = matrix.create_vector(stdin.readline().split()) T = len(emissions) # nb of observations N = len(A) delta_matrix = [[0.0 for _ in range(N)] for _ in range(T)] # remember that delta_id_matrix always have the index written as actual index - 2 (instead of -1) # since t = 1 has no predecessor delta_id_matrix = [[0 for _ in range(N)] for _ in range(T - 1)] for i in range(N): delta_matrix[0][i] = B[i][emissions[0]] * pi[0][i] for t in range(1, T): for i in range(N): temp = [] for j in range(N): temp.append(A[j][i] * delta_matrix[t - 1][j] * B[i][emissions[t]]) delta_matrix[t][i] = max(temp) delta_id_matrix[t - 1][i] = temp.index(delta_matrix[t][i]) seq = [delta_matrix[T - 1].index(max(delta_matrix[T - 1]))] for t in reversed(range(T - 1)): seq.append(delta_id_matrix[t][seq[-1]]) seq = reversed(seq) result = "" for state in seq: result += str(state) + " " print(result[:-1]) return result[:-1]
def main(): A = matrix.create_matrix(stdin.readline().split()) B = matrix.create_matrix(stdin.readline().split()) pi = matrix.create_matrix(stdin.readline().split()) v1 = matrix.matrix_vec_mul(pi, A) v2 = matrix.matrix_vec_mul(v1, B) n = len(v2[0]) result = "1 " + str(n) for i in range(0, n): result += " " + str(v2[0][i]) print(result) return result
def linkingnumber(overstrandlist, signlist, colourlist, p): dln_mat = [] for k in range((p - 1) // 2 + 1): coeff_matrix = matrix.create_matrix(overstrandlist, colourlist, signlist, p, k) coeff_dict = rrematrix_to_dict(coeff_matrix, p, k) # print("k =", k) # print("Coefficients:", coeff_dict) # print() dln = matrix_to_dln(colourlist, overstrandlist, signlist, coeff_dict, p, k) dln_mat.append(dln) return dln_mat
def main(): A = matrix.create_matrix(stdin.readline().split()) B = matrix.create_matrix(stdin.readline().split()) pi = matrix.create_matrix(stdin.readline().split()) emissions = matrix.create_vector(stdin.readline().split()) T = len(emissions) # nb of observations N = len(A) alpha_matrix = [[0.0 for _ in range(N)] for _ in range(T)] for i in range(N): alpha_matrix[0][i] = B[i][emissions[0]] * pi[0][i] for t in range(1, T): for i in range(N): s = 0 for j in range(N): s += A[j][i] * alpha_matrix[t - 1][j] alpha_matrix[t][i] = B[i][emissions[t]] * s result = 0 for i in range(N): result += alpha_matrix[T - 1][i] print(result) return result
def gauss_to_dln(gauss, p): overstrand = gauss_to_overstrand.create_overstrand_list(gauss) signlist = gauss_to_signlist.SignList(gauss) colourlists = rrematrix_to_colourlist.overstrand_to_colourlist(overstrand, p) # print(signlist, overstrand, colourlists) complete_dln = [] for i in range(len(colourlists)): colourlist = colourlists[i] if len(colourlist) % 2 == 1: colourlist.append(colourlist[0]) if i == 0: signlist.append(1) overstrand.append(len(overstrand)) # print(signlist, overstrandlist, colourlist) dln_mat = [] for k in range(((p - 1) // 2) + 1): # print(k) mat = matrix.create_matrix(overstrand, colourlist, signlist, p, k) coeffs = rrematrix_to_dict(mat, p, k) # print("Coefficients: k = ", k) # print(coeffs) # print() dlns = matrix_to_dln(colourlist, overstrand, signlist, coeffs, p, k) dln_mat.append(dlns) complete_dln.append(dln_mat) # print(complete_dln) error_list = errors(complete_dln, p) if error_list == []: return complete_dln else: return ["Error:", error_list]
import matrix a = matrix.rand_matrix(4, 4) b = matrix.rand_matrix(4, 4) c = matrix.create_matrix(4, 4) matrix.mult(c, a, b) print c a = matrix.rand_matrix(4, 2) b = matrix.rand_matrix(2, 4) c = matrix.create_matrix(4, 4) c = matrix.mult(c, a, b) print c r = matrix.create_matrix(2, 2) print matrix.mult(r, [[1, 0], [0, 1]], [[1, 0], [0, 1]]) print matrix.mult(r, [[2, 0], [0, 2]], [[0, 2], [2, 0]])
import sys import matrix if __name__ == "__main__": #exec(open("dataPreProcessing.py").read()) kmin = int(sys.argv[1]) kmax = int(sys.argv[2]) for k in range(kmin, kmax + 1): print("Run for k = " + str(k)) matrix.create_matrix(k)
from gauss import gauss, residuo from gauss_seidel import gauss_seidel, converges from matrix import create_matrix, n import numpy as np A, b = create_matrix() g_res = gauss(A, b) g_mres = max(abs(residuo(A, b, g_res))) print('A1:') print('Primeira:', g_res[0]) print('Última:', g_res[-1]) print('Resíduo máximo:', g_mres) print('\nA2:') print('Operações em ponto flutuante:', (4 * n**3 + 9 * n**2 - n - 6) // 6) criteria = 1e-4 print('\nB1:') if converges(A): print('O sistema possui diagonal dominante e sempre irá convergir. ' 'Podemos acelerar o cálculo usando um fator de sobre-relaxação.') else: print('O sistema não possui diagonal dominante e ' 'talvez não venha a convergir')
def matrix_to_dln(colourlist, overstrandlist, signlist, coeff_dict, p, k): universes_list = i2l.universe_lists(colourlist, overstrandlist, p) wheres = i2l.where_lists(colourlist, overstrandlist, p) vert_order = i2l.vertical_order(colourlist, overstrandlist, p) dlns = [] dln = 0 # print('K = ', k) if coeff_dict == "X": # No DLN Exists for i in range(((p - 1) // 2) + 1): dlns.append("x") else: # Linking number of Index 1 knot with surface zero_2chain = rrematrix_to_dict(matrix.create_matrix(overstrandlist, colourlist, signlist, p, 0), p, 0) if zero_2chain == "X": dln = "x" else: for i in range(len(colourlist)): if colourlist[i] == colourlist[overstrandlist[i]]: # print("homogeneous => no change. DLN = ", dln) dln += 0 else: over_index = vert_order[i][-1] # Need bottom overstrand where_over = wheres[over_index - 1][overstrandlist[i]] if where_over == colourlist[i]: epsilon1 = 1 else: epsilon1 = -1 if signlist[i] * epsilon1 == 1: # Walk through R wall dln += signlist[i] * coeff_dict[(over_index, overstrandlist[i])] else: # Walk through L wall dln -= signlist[i] * coeff_dict[(over_index, overstrandlist[i])] if k == over_index: dln += signlist[i] dlns.append(dln) # Linking numbers of Index 2 knots with surface for j in range(len(wheres)): # Go through each of the index 2 knots where_list = wheres[j] dln = 0 j_2chain = rrematrix_to_dict(matrix.create_matrix(overstrandlist, colourlist, signlist, p, j + 1), p, j + 1) if j_2chain == "X": dln = "x" else: for i in range(len(where_list) - 1): # Go through each crossing where_under = where_list[i] universes = universes_list[i] # print("crossing:", i) if colourlist[i] == colourlist[overstrandlist[i]]: for x in range(len(universes)): # list of pairs of universes if where_under in universes[x]: over_index = vert_order[i][x] else: if where_under in universes[0] and where_under in universes[1]: # Must be on the top level, so a(j) = 0 over_index = 0 else: if where_under in universes[0]: level = i2l.index(universes[0], where_under) else: level = i2l.index(universes[1], where_under) over_index = vert_order[i][level - 1] if over_index == 0: where_over = colourlist[overstrandlist[i]] # print("adding index 1 wall") # print("coefficient:", coeff_dict[(0, overstrandlist[i])]) dln += signlist[i] * coeff_dict[(0, overstrandlist[i])] # print() # print(dln) else: where_over = wheres[over_index - 1][overstrandlist[i]] # print(where_under, where_over) reflections = [dln5.reflect(where_over, colourlist[i], p), dln5.reflect(where_over, colourlist[overstrandlist[i]], p)] if where_under in reflections: epsilon1 = 1 else: epsilon1 = -1 # print("adding wall", (over_index, overstrandlist[i])) # if epsilon1 * signlist[i] == -1: # print("right") # else: # print("left") # print("coefficient:", -epsilon1 * signlist[i] * coeff_dict[(over_index, overstrandlist[i])]) dln -= epsilon1 * coeff_dict[(over_index, overstrandlist[i])] if k == over_index: # print("since over_index = k, add", (signlist[i] + epsilon1) // 2) dln += (signlist[i] + epsilon1) // 2 # print() # print("Final DLN:", dln) # print() dlns.append(dln) return dlns
# Print the time.txt in sort order results = [] # This opens a handle to your file, in 'r' read mode with open('time.txt') as fp: # Read in all the lines of your file into a list of lines for line in fp: results.append(line) float_results = [float(x) for x in results] data = float_results[1::2] y = np.array(float_results[0::2]) y = y.astype(int) dictionary = {} dictionary = dict(zip(y, data)) sort_dictionary = dict(sorted(dictionary.items(), key=operator.itemgetter(0))) data_x = np.cumsum(np.array(list(sort_dictionary.values()))) # Plot time of k files plt.plot(data_x, sort_dictionary.keys(), color="blue") plt.ylabel('number of k files') plt.xlabel('time(sec)') plt.title('Time plot') plt.savefig('time.png') plt.show() matrix.create_matrix(sys.argv[2], best_k)
def main(): A = matrix.create_matrix(stdin.readline().split()) B = matrix.create_matrix(stdin.readline().split()) pi = matrix.create_matrix(stdin.readline().split()) emissions = matrix.create_vector(stdin.readline().split()) T = len(emissions) N = len(A) K = len(B[0]) alpha = [[0.0 for _ in range(N)] for _ in range(T)] beta = [[0.0 for _ in range(N)] for _ in range(T)] di_gamma = [[[0.0 for _ in range(N)] for _ in range(N)] for _ in range(T - 1)] gamma = [[0.0 for _ in range(N)] for _ in range(T)] scaling_factors = [0.0 for _ in range(T)] max_its = 30 its = 0 old_log_prob = -float("inf") range_n = range(N) while its < max_its: # its < max_its: scaling_factors[0] = 0 for i in range_n: temp = B[i][emissions[0]] * pi[0][i] alpha[0][i] = temp scaling_factors[0] += temp scaling_factors[0] = 1 / scaling_factors[0] alpha[0] = [x * scaling_factors[0] for x in alpha[0]] log_prob = log(scaling_factors[0]) for t in range(1, T): scaling_factors[t] = 0 for i in range_n: s = 0 for j in range_n: s += A[j][i] * alpha[t - 1][j] alpha[t][i] = B[i][emissions[t]] * s scaling_factors[t] += alpha[t][i] scaling_factors[t] = 1 / scaling_factors[t] alpha[t] = [x * scaling_factors[t] for x in alpha[t]] log_prob += log(scaling_factors[t]) log_prob = -log_prob if log_prob > old_log_prob: old_log_prob = log_prob # its += its else: break beta[T - 1] = [scaling_factors[T - 1] for _ in range_n] # 1 scaled by cT-1 for t in reversed(range(T - 1)): for i in range(N): s = 0 for j in range(N): s += beta[t + 1][j] * B[j][emissions[t + 1]] * A[i][j] beta[t][i] = s * scaling_factors[t] for t in range(T - 1): for i in range_n: di_gamma_sum = 0 for j in range_n: temp = alpha[t][i] * A[i][j] * B[j][emissions[ t + 1]] * beta[t + 1][j] di_gamma[t][i][j] = temp di_gamma_sum += temp gamma[t][i] = di_gamma_sum gamma[T - 1] = alpha[T - 1] for i in range_n: pi[0][i] = gamma[0][i] gamma_sum = 0 for t in range(T - 1): gamma_sum += gamma[t][i] for k in range(K): ind_gamma_sum = 0 for t in range(T): if emissions[t] == k: ind_gamma_sum += gamma[t][i] B[i][k] = ind_gamma_sum / gamma_sum for j in range_n: di_gamma_sum = 0 for t in range(T - 1): di_gamma_sum += di_gamma[t][i][j] A[i][j] = di_gamma_sum / gamma_sum its += 1 result = str(N) + " " + str(N) for row in A: for val in row: result += " " + str(round(val, 6)) result += "\n" + str(N) + " " + str(K) for row in B: for val in row: result += " " + str(round(val, 6)) print(result) return result
from graph import Graph from binary_heap import BinaryHeap from binomial_heap import BinomialHeap from dijkstra import dijkstra #(List[int],float) or Map from prima import prima #(List[Tuple],float) from alfa_star import a_star #(List[int],float) from bellman_ford import bellman_ford #(List[int],float) or Map from floyd_warshall import floyd_warshall #(List[int],float) or Map or #(List[float],List[int]) from kruskal import kruskal #(List[Tuple],float) # bubble: 1.23 G = Graph(*create_matrix(25,25,20,dst=7)) G.draw() H = Graph(G.H()) q=H.get_queue() lst=[] while q: lst.append(q.get()) def test(constructor, lst): n=len(lst) q=constructor() b=constructor() # ============================================================================= # try: # asd = q._lst