def test_create_guess(name, data, exception): inp_guess = IterativeInitialGuess.from_string(data.input.guess) inp_matA = create_matrix(data.input.matA) inp_matb = create_matrix(data.input.matb) inp_skip_build_interim = data.input.skip_build_interim with exception: actor = GaussSeidelSolver(matA=inp_matA, matb=inp_matb, guess_source=inp_guess) if not inp_skip_build_interim: actor._build_interim_matricies() act = actor._create_guess() if inp_guess != IterativeInitialGuess.RANDOM_MATRIX: exp = matops.reshape(create_matrix(data.expect.mat), (-1, 1)) assert act.shape == exp.shape assert matops.almost_equal(act, exp) else: act = np.fabs(act) exp_shape = actor._matC.shape exp_mat_zero = matops.create_zeros(exp_shape[0], exp_shape[1]) assert act.shape == exp_shape assert not matops.almost_equal(act, exp_mat_zero) assert (act <= 20.0).all()
def _create_guess(self): if not hasattr(self, "_matD"): self._build_interim_matricies() ret = (matops.create_random(matops.count_rows(self._matb), True) if self._guess_source == IterativeInitialGuess.RANDOM_MATRIX else matops.create_zeros(matops.count_rows(self._matb))) ret = matops.reshape(ret, (-1, 1)) return ret
def test_create_zeros(name, data, exception): inp_cols = data.input.columns inp_rows = data.input.rows with exception: act = matops.create_zeros(inp_rows, inp_cols) exp = create_matrix(data.expect.mat) assert act.shape == exp.shape assert matops.almost_equal(act, exp)
def _calculate_forward_solve_vector(self, matL): if not matops.is_in_crout_l_form(matL): raise ValueError("Matrix L is missing or not in propert form.") size = matops.count_columns(self._matb) ret = matops.create_zeros(size) for index in range(size): ret[index] = self._calculate_y_vector_value( matL, ret[:index], index) return ret
def _calculate_back_solve_vector(self): if matops.is_singular(self._mat): raise ValueError("Provided matrix is singular.") if not matops.is_in_reduced_row_echelon(self._mat): self._mat = matops.to_reduced_row_echelon(self._mat) ret = matops.create_zeros(self._mat.shape[0]) for r in range(ret.shape[0] - 1, -1, -1): ret[r] = self._calculate_back_solve_vector_row(ret, r) return ret
def _create_guess(self): if not hasattr(self, "_matC"): self._build_interim_matricies() rows = matops.count_rows(self._matb) creators = { IterativeInitialGuess.MATRIX_C: lambda: matops.deepcopy(self._matC), IterativeInitialGuess.RANDOM_MATRIX: lambda: matops.create_random( rows, True ), IterativeInitialGuess.MATRIX_OF_ZEROS: lambda: matops.create_zeros(rows, 1), } return creators.get(self._guess_source)()
def test_calc_forward_solve_vector(name, data, exception): inp_matb = create_matrix(data.input.matb) inp_matL = create_matrix(data.input.matL) inp_matA = matops.create_zeros(inp_matb.size, inp_matb.size) with exception: actor = LuDecompositionSolver(matA=inp_matA, matb=inp_matb) act = actor._calculate_forward_solve_vector(inp_matL) exp = create_matrix(data.expect) assert matops.almost_equal(act, exp)
def test_calc_y_vector_value(name, data, exception): inp_matb = create_matrix(data.input.matb) inp_matL = create_matrix(data.input.matL) inp_vecy = create_matrix(data.input.vecy) inp_index = data.input.index inp_matA = matops.create_zeros(inp_matb.size, inp_matb.size) with exception: actor = LuDecompositionSolver(matA=inp_matA, matb=inp_matb) act = actor._calculate_y_vector_value(inp_matL, inp_vecy, inp_index) exp = data.expect assert isclose(act, exp)