def test_iterate_until_solved(name, data, exception): def assert_result_vector_equality(act_vec, data_exp_raw): exp_vec = matops.reshape(create_matrix(data_exp_raw), (-1, 1)) assert matops.almost_equal(act_vec, exp_vec) inp_guess = IterativeInitialGuess.from_string(data.input.guess) inp_matA = create_matrix(data.input.matA) inp_matb = create_matrix(data.input.matb) with exception: actor = GaussSeidelSolver(matA=inp_matA, matb=inp_matb, guess_source=inp_guess) init_guess = actor._create_guess() act_vec, act_iter_count = actor._iterate_until_solved(init_guess) exp_iter_count = data.expect.iter_count print(act_iter_count) if isinstance(exp_iter_count, int): assert act_iter_count == exp_iter_count if exp_iter_count <= _ITER_MAX: assert_result_vector_equality(act_vec, data.expect.mat) elif isinstance(exp_iter_count, list): assert exp_iter_count[0] <= act_iter_count <= exp_iter_count[1] assert_result_vector_equality(act_vec, data.expect.mat) else: pytest.fail("expect.iter_count should be int or list of ints.")
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 test_iterate_until_solved(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_matC = create_matrix(data.input.matC) inp_matD = create_matrix(data.input.matD) with exception: actor = JacobiSolver(matA=inp_matA, matb=inp_matb, guess_source=inp_guess) actor._matC = inp_matC actor._matD = inp_matD init_guess = actor._create_guess() act_vec, act_iter_count = actor._iterate_until_solved(init_guess) exp_vec = matops.reshape(create_matrix(data.expect.mat), (-1, 1)) exp_iter_count = data.expect.iter_count assert matops.almost_equal(act_vec, exp_vec) print(act_iter_count) if isinstance(data.expect.iter_count, int): assert act_iter_count == exp_iter_count elif isinstance(data.expect.iter_count, list): assert exp_iter_count[0] <= act_iter_count <= exp_iter_count[1] else: pytest.fail("expect.iter_count should be int or list of ints.")
def test_init(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_omega = data.input.omega with exception: assert inp_guess is not None actor = SORSolver(matA=inp_matA, matb=inp_matb, guess_source=inp_guess, omega=inp_omega) act_guess = actor._guess_source act_matA = actor._matA act_matb = actor._matb act_omega = actor._omega exp_guess = IterativeInitialGuess.from_string(data.expect.guess) exp_matA = create_matrix(data.expect.matA) exp_matb = matops.reshape(create_matrix(data.expect.matb), (-1, 1)) exp_omega = data.expect.omega assert exp_guess is not None assert act_guess == exp_guess assert matops.almost_equal(act_matA, exp_matA) assert matops.almost_equal(act_matb, exp_matb) assert isclose(act_omega, exp_omega)
def test_create_based_on_l_component(name, data, exception): inp = create_matrix(data.input.mat) with exception: act = matops.create_based_on_l_component(inp) exp = create_matrix(data.expect.mat) assert matops.almost_equal(act, exp)
def test_create_based_on_non_diagonal_terms(name, data, exception): inp = create_matrix(data.input.mat) with exception: act = matops.create_based_on_non_diagonal_terms(inp) exp = create_matrix(data.expect) assert matops.almost_equal(act, exp)
def test_create_inverted(name, data, exception): inp = create_matrix(data.input.mat) with exception: act = matops.create_inverted(inp) exp = create_matrix(data.expect) assert matops.almost_equal(act, exp)
def test_subtract(name, data, exception): inp_mat1 = create_matrix(data.input.mat1) inp_mat2 = create_matrix(data.input.mat2) with exception: act = matops.subtract(inp_mat1, inp_mat2) exp = create_matrix(data.expect.mat) assert matops.almost_equal(act, exp)
def test_add(name, data, exception): inp_matA = create_matrix(data.input.matA) inp_matB = create_matrix(data.input.matB) with exception: act = matops.add(inp_matA, inp_matB) exp = create_matrix(data.expect.mat) assert matops.almost_equal(act, exp)
def test_create_based_on_u_component(name, data, exception): inp_mat = create_matrix(data.input.mat) inp_diag_zero = data.input.diag_zero with exception: act = matops.create_based_on_u_component(inp_mat, inp_diag_zero) exp = create_matrix(data.expect.mat) assert matops.almost_equal(act, exp)
def test_two_norm_of_error(name, data, exception): inpA = create_matrix(data.input.matA) inpb = create_matrix(data.input.matb) inpx = create_matrix(data.input.matx) with exception: act = matops.two_norm_of_error(inpA, inpb, inpx) exp = data.expect assert isclose(act, exp, abs_tol=0.000_01)
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_reshape(name, data, exception): inp_mat = create_matrix(data.input.mat) inp_newshape = (tuple(data.input.newshape.value) if data.input.newshape.as_tuple else data.input.newshape.value) with exception: act = matops.reshape(inp_mat, inp_newshape) exp = create_matrix(data.expect.mat) assert matops.almost_equal(act, exp)
def test_multiply(name, data, exception): inp_matA = create_matrix(data.input.a) inp_b = data.input.b if isinstance(inp_b, list): inp_b = create_matrix(inp_b) with exception: act = matops.multiply(inp_matA, inp_b) exp = create_matrix(data.expect) assert matops.almost_equal(act, exp)
def test_set_rows_below_to_zero(name, data, exception): inp_inplace = data.input.inplace inp_mat = create_matrix(data.input.mat) inp_row = data.input.source_row with exception: act = matops.set_rows_below_to_zero(inp_mat, inp_row, inp_inplace) exp = create_matrix(data.expect.mat) assert matops.almost_equal(act, exp) # make sure inp matrix was not mutated assert matops.almost_equal(act, inp_mat) == data.expect.inp_match_act
def test_calc_back_solve_vector(name, data, exception): inp_matA = create_matrix(data.input.matA) inp_matb = create_matrix(data.input.matb) with exception: actor = GaussianSolver(matA=inp_matA, matb=inp_matb) act = actor._calculate_back_solve_vector() exp = create_matrix(data.expect) # print(exp) # print(act) assert almost_equal(act, exp)
def test_to_reduced_row_echelon(name, data, exception): inp = create_matrix(data.input) with exception: act = matops.to_reduced_row_echelon(inp) exp = create_matrix(data.expect) # print('') # print(inp) # print(exp) # print(act) assert matops.almost_equal(act, exp)
def test_create_augmented(name, data, exception): inp_matA = create_matrix(data.input.A) inp_matb = create_matrix(data.input.b) orig_matA = matops.deepcopy(inp_matA) orig_matb = matops.deepcopy(inp_matb) with exception: act = matops.create_augmented(inp_matA, inp_matb) exp = create_matrix(data.expect) assert matops.almost_equal(act, exp) assert inp_matA.shape == orig_matA.shape assert inp_matb.shape == orig_matb.shape
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)
def test_calc_back_solve_vector_row(name, data, exception): inp_matA = create_matrix(data.input.matA) inp_matb = create_matrix(data.input.matb) inp_vec = create_matrix(data.input.bsvec) inp_row = data.input.calc_row with exception: actor = GaussianSolver(matA=inp_matA, matb=inp_matb) act = actor._calculate_back_solve_vector_row(inp_vec, inp_row) exp = data.expect # print(exp) # print(act) assert isclose(act, exp, abs_tol=0.000_01)
def test_init(name, data, exception): inp_matA = create_matrix(data.input.matA) inp_matb = create_matrix(data.input.matb) with exception: actor = LuDecompositionSolver(matA=inp_matA, matb=inp_matb) act_matA = actor._matA act_matb = actor._matb exp_matA = create_matrix(data.expect.matA) exp_matb = create_matrix(data.expect.matb) exp_mat = create_matrix(data.expect.mat) assert matops.almost_equal(act_matA, exp_matA) assert matops.almost_equal(act_matb, exp_matb)
def test_is_in_reduced_row_echelon(name, data, exception): inp = create_matrix(data.input) with exception: act = matops.is_in_reduced_row_echelon(inp) assert act == data.expect
def test_percent_error(name, data, exception): # print('') # print(data.name) inp_act = create_matrix(data.input.act).reshape(-1, 1) inp_exp = create_matrix(data.input.exp).reshape(-1, 1) with exception: act = matops.percent_error(inp_act, inp_exp) exp = data.expect if data.expect != "np.inf" else np.inf # print(exp) # print(act) assert act >= 0.0 assert isclose(act, exp, abs_tol=0.000_01)
def test_is_square(name, data, exception): inp = create_matrix(data.input) with exception: act = matops.is_square(inp) assert act == data.expect
def test_calc_u_row(name, data, exception): inp_matA = create_matrix(data.input.matA) inp_matL = create_matrix(data.input.matL) inp_matU = create_matrix(data.input.matU) inp_column = data.input.row inp_limit = data.input.limit inp_matb = matops.create_ones(inp_limit) with exception: actor = LuDecompositionSolver(matA=inp_matA, matb=inp_matb) act = actor._calculate_u_row(inp_matL, inp_matU, inp_column, inp_limit) exp = create_matrix(data.expect) # print("Expected:\n{}".format(exp)) # print("Actual:\n{}".format(act)) assert matops.almost_equal(act, exp)
def test_init(name, data, exception): inp_matA = create_matrix(data.input.matA) inp_matb = create_matrix(data.input.matb) with exception: actor = GaussianSolver(matA=inp_matA, matb=inp_matb) act_matA = actor._matA act_matb = actor._matb act_mat = actor._mat exp_matA = create_matrix(data.expect.matA) exp_matb = create_matrix(data.expect.matb) exp_mat = create_matrix(data.expect.mat) assert almost_equal(act_matA, exp_matA) assert almost_equal(act_matb, exp_matb) assert almost_equal(act_mat, exp_mat)
def test_is_in_crout_l_form(name, data, exception): inp = create_matrix(data.input) with exception: act = matops.is_in_crout_l_form(inp) exp = data.expect assert act == exp
def test_count_rows(name, data, exception): inp = create_matrix(getattr(data.input, "mat", None)) with exception: act = matops.count_rows(inp) exp = data.expect assert act == exp
def test_is_singular(name, data, exception): # print('') # print(data.name) inp = create_matrix(data.input) with exception: act = matops.is_singular(inp) assert act == data.expect
def test_swap_largest_pivot_to_top(name, data, exception): inp_inplace = data.input.inplace inp_mat = create_matrix(data.input.mat) inp_pivot = data.input.pivot with exception: act = matops.swap_largest_pivot_to_top(inp_mat, inp_pivot, inp_inplace) exp = create_matrix(data.expect.mat) # print('') # print(inp) # print(exp) # print(act) assert matops.almost_equal(act, exp) # make sure inp matrix was not mutated assert matops.almost_equal(act, inp_mat) == data.expect.inp_match_act