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_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_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_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_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_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_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_create_identity(name, data, exception): inp = data.input with exception: act = matops.create_identity(inp) exp = create_matrix(data.expect) assert matops.almost_equal(act, exp)
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
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_build_interim_matricies(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) with exception: actor = JacobiSolver(matA=inp_matA, matb=inp_matb, guess_source=inp_guess) actor._build_interim_matricies() act_matC = actor._matC act_matD = actor._matD exp_matC = create_matrix(data.expect.matC) exp_matD = create_matrix(data.expect.matD) assert matops.almost_equal(act_matC, exp_matC) assert matops.almost_equal(act_matD, exp_matD)
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_set_row_diagonal_to_one(name, data, exception): inp_inplace = data.input.inplace inp_mat = create_matrix(data.input.mat) inp_row = data.input.row with exception: act = matops.set_row_diagonal_to_one(inp_mat, inp_row, inp_inplace) exp = create_matrix(data.expect.mat) # print('') # print(data.name) # 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
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_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 test_subtract_scalar_row_from_row(name, data, exception): inp_inplace = data.input.inplace inp_mat = create_matrix(data.input.mat) inp_aff_row = data.input.affect_row inp_src_row = data.input.source_row with exception: act = matops.subtract_scalar_row_from_row(inp_mat, inp_src_row, inp_aff_row, 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
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_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_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_row_by_scalar(name, data, exception): # print('') # print(data.name) inp_inplace = data.input.inplace inp_mat = create_matrix(data.input.mat) inp_row = data.input.row inp_scalar = data.input.scalar with exception: act = matops.multiply_row_by_scalar(inp_mat, inp_row, inp_scalar, inp_inplace) exp = create_matrix(data.expect.mat) # 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
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_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) with exception: assert inp_guess is not None actor = GaussSeidelSolver(matA=inp_matA, matb=inp_matb, guess_source=inp_guess) act_guess = actor._guess_source act_matA = actor._matA act_matb = actor._matb 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)) 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)
def test_build_interim_matricies(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) with exception: actor = SORSolver(matA=inp_matA, matb=inp_matb, guess_source=inp_guess, omega=0.5) actor._build_interim_matricies() act_matD = actor._matD act_matL = actor._matL act_matU = actor._matU exp_matD = create_matrix(data.expect.matD) exp_matL = create_matrix(data.expect.matL) exp_matU = create_matrix(data.expect.matU) assert matops.almost_equal(act_matD, exp_matD) assert matops.almost_equal(act_matL, exp_matL) assert matops.almost_equal(act_matU, exp_matU)
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_create_random_diag_dominate(name, data, exception): inp_size = data.input.size with exception: act = matops.create_random_diagonal_dominate(inp_size) exp_size = data.expect.size assert act.shape == (exp_size, exp_size) diag = act.diagonal() bigs = act.max(1) print(act) assert matops.almost_equal(diag, bigs)
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_calc_iteration_result(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) inp_matguess = matops.reshape(create_matrix(data.input.mat_guess), (-1, 1)) with exception: actor = JacobiSolver(matA=inp_matA, matb=inp_matb, guess_source=inp_guess) actor._matC = inp_matC actor._matD = inp_matD act = actor._calculate_iteration_result(inp_matguess) exp = matops.reshape(create_matrix(data.expect.mat), (-1, 1)) assert matops.almost_equal(act, exp)