def execute(this, arguments, WORKINGMATRIX): source = arguments[0] destination = arguments[2] sourceFormat = arguments[1] destinationFormat = arguments[3] MatrixUtils.convert(source, destination, sourceFormat, destinationFormat)
def execute(this, arguments, WORKINGMATRIX): first = 0 last = WORKINGMATRIX.nzentries - 1 if len(arguments) == 2: first = arguments[0] last = arguments[1] MatrixUtils.printCSR(WORKINGMATRIX, first, last)
def execute(this, arguments, WORKINGMATRIX): path = arguments[0] numlines = 10 if len(arguments) == 2: numlines = arguments[1] MatrixUtils.head(path, numlines)
def execute(this, arguments, WORKINGMATRIX): width = arguments[0] height = arguments[1] print("Setting dimensions of matrix to {0} columns by {1} rows..." .format(width, height)) MatrixUtils.setDims(height, width, WORKINGMATRIX) print("done")
def execute(this, arguments, WORKINGMATRIX): width = arguments[0] height = arguments[1] val = 0 if len(arguments) == 3: val = arguments[2] MatrixUtils.initialize(height, width, WORKINGMATRIX, val)
def execute(this, arguments, WORKINGMATRIX): begin = arguments[0] end = arguments[1] spread = arguments[2] val = arguments[3] offset = 0 if len(arguments) == 5: offset = arguments[4] MatrixUtils.paintDiagonal(begin, end, spread, val, WORKINGMATRIX, offset)
def execute(this, arguments, WORKINGMATRIX): col1 = arguments[0] row1 = arguments[1] col2 = arguments[2] row2 = arguments[3] val = 0 if len(arguments) == 5: val = arguments[4] MatrixUtils.paint(row1, row2, col1, col2, val, WORKINGMATRIX)
def seidel(self, k): h_l, h_r = IterativeSolverHelper.transform_h(self.h) e = np.eye(self.h.shape[0]) e_hl_inv = MatrixUtils.inverse(e - h_l) x_cur = np.matrix(np.zeros(self.matrix.shape[0])).T err = 0 for i in range(k): x_cur = e_hl_inv @ h_r @ x_cur + e_hl_inv @ self.g err = MatrixUtils.norm_vec(self.x_gauss - x_cur) return Result(x_cur, err)
def execute(this, arguments, WORKINGMATRIX): height = 10 width = 10 if len(arguments) == 2: height = arguments[0] width = arguments[1] try: MatrixUtils.displayMatrix(WORKINGMATRIX, height, width) except ValueError: print("ERROR: height and width must be even numbers!")
def execute(this, arguments, WORKINGMATRIX): col1 = arguments[0] row1 = arguments[1] col2 = arguments[2] row2 = arguments[3] try: MatrixUtils.printRange(row1, row2, col1, col2, WORKINGMATRIX) except IndexError as e: #print("ERROR: specified endpoint(s) are out of bounds") print(e) except ValueError as e: #print("ERROR: col1 > col2 or row1 > row2") print(e)
def upper_relaxation(self, k): q = 2 / (1 + np.sqrt(1 - MatrixUtils.spectral_radius(self.h)**2)) num_rows, num_cols = self.h.shape x_cur = np.matrix(np.zeros(self.matrix.shape[0])).T x_new = x_cur err = 0 for j in range(k): for i in range(num_rows): sum_1 = IterativeSolverHelper.sum1(self.h, x_cur, i) sum_2 = IterativeSolverHelper.sum2(self.h, x_new, i) x_cur[i] = x_cur[i] + q * (sum_1 + sum_2 - x_cur[i] + self.g[i]) x_new = x_cur err = MatrixUtils.norm_vec(self.x_gauss - x_new) return Result(x_new, err)
def get_h(matrix): d_diag = np.diag(matrix) d = np.diag(d_diag) d_inv = MatrixUtils.inverse(d) e = np.eye(matrix.shape[0]) h = e - d_inv @ matrix return h
def simple_k_iterations(self, iterations): x_cur = np.matrix(np.zeros(self.matrix.shape[0])).T err = 0 for i in range(iterations): x_cur = self.h @ x_cur + self.g err = MatrixUtils.norm_vec(self.x_gauss - x_cur) return Result(x_cur, err)
def simple_iteration(self, err): k = 0 x_cur = np.matrix(np.zeros(self.matrix.shape[0])).T err_k = 0 while self.a_priori_err_estimate(x_cur) > err: k += 1 x_cur = self.h @ x_cur + self.g err_k = MatrixUtils.norm_vec(self.x_gauss - x_cur) return Result(x_cur, err_k), k
def get_seidel_transition_matrix(h): h_l, h_r = transform_h(h) e = np.eye(h.shape[0]) e_hl_inv = MatrixUtils.inverse(e - h_l) return e_hl_inv
def execute(this, arguments, WORKINGMATRIX): MatrixUtils.printRaw(WORKINGMATRIX)
def execute(this, arguments, WORKINGMATRIX): MatrixUtils.cat(arguments[0])
lyricsSplit[i] = re.sub(r'\s+\W+|^\W+', " ", lyricsSplit[i]) lyricsSplit[i] = lyricsSplit[i].strip() if len(lyricsSplit[i]) == 0: lyricsSplit.remove(lyricsSplit[i]) continue blobs.append(tb(lyricsSplit[i])) for line in lyricsSplit: for word in line.split(): if word not in unique_words: unique_words.append(word) frequency_vectors = [] for entry in blobs: # Builds Frequency vectors for each line in the song this_line = [] for word in unique_words: this_line.append(MatrixUtils.tfidf(word, entry, blobs)) frequency_vectors.append(this_line) np_frequency_vectors = np.array(frequency_vectors) sim_matrix = [] for i, first in enumerate( np_frequency_vectors ): # Checks the cosine between every line's frequency vector row = [] for j, second in enumerate(np_frequency_vectors): row.append( np.dot(first, second) / (np.linalg.norm(first) * np.linalg.norm(second))) sim_matrix.append(row)
from IterativeSolver import IterativeSolver1 import MatrixSolver import MatrixUtils import IterativeSolverHelper import numpy as np if __name__ == '__main__': a = np.matrix([[12.785723, 1.534675, -3.947418], [1.534675, 9.709232, 0.918435], [-3.947418, 0.918435, 7.703946]]) b = np.matrix([[9.60565], [7.30777], [4.21575]]) solver = IterativeSolver1(a, b) print("1. Exact solution = \n{}\n".format(solver.x_gauss)) print("2. ||H|| = {}\n".format(MatrixUtils.norm_matrix(solver.h))) k = 7 print("3. A priori error estimation for k = " + str(k) + ": {}\n".format(solver.a_priori_err_estimate(k))) k_iterations = solver.simple_k_iterations(7) lusternik_app = solver.lusternik_approximation( solver.simple_k_iterations(7).res, solver.simple_k_iterations(6).res) print(("4. Simple iteration for k = 7: \n" + "x7 = \n{}\n" "Error = {}\n" + "A posteriori error = {}\n" "Lusternik approximation = \n{}\n" + "Lusternik approximation error = {}\n").format( k_iterations.res, k_iterations.err, solver.a_posteriori_err_estimate( solver.simple_k_iterations(7).res, solver.simple_k_iterations(6).res), lusternik_app.res, lusternik_app.err)) seidel = solver.seidel(7)
def execute(this, arguments, WORKINGMATRIX): MatrixUtils.printWorkingDirectory()
def a_priori_err_estimate(self, k): h_norm = MatrixUtils.norm_matrix(self.h) g_norm = MatrixUtils.norm_vec(self.g) err = (h_norm**k) / (1 - h_norm) * g_norm return err
def a_posteriori_err_estimate(self, x_k, x_k_1): h_norm = MatrixUtils.norm_matrix(self.h) err = h_norm / (1 - h_norm) * MatrixUtils.norm_vec(x_k - x_k_1) return err
def get_g(matrix, b): d_diag = np.diag(matrix) d = np.diag(d_diag) d_inv = MatrixUtils.inverse(d) g = d_inv @ b return np.asmatrix(g)
def lusternik_approximation(self, x_k, x_k_1): r = MatrixUtils.spectral_radius(self.h) x_lust = x_k_1 + (x_k - x_k_1) / (1 - r) err = MatrixUtils.norm_vec(x_lust - self.x_gauss) return Result(x_lust, err)
def execute(this, arguments, WORKINGMATRIX): MatrixUtils.changeDirectory(arguments[0])
def execute(this, arguments, WORKINGMATRIX): target = "." if len(arguments) == 1: target = arguments[0] MatrixUtils.printDirectoryListing(target)