From 94042ddfdad01118651e33f006d1d6ea13b22cea Mon Sep 17 00:00:00 2001 From: Antonio Caceres <acaceres@caltech.edu> Date: Thu, 14 Nov 2024 13:40:06 -0800 Subject: [PATCH] Add small pivot matrix tests to test_pivot_matrix.py. --- tests.json | 2 +- .../test_pivot_matrix.py | 42 ++++++++++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/tests.json b/tests.json index fee9ffc..0a75e7e 100644 --- a/tests.json +++ b/tests.json @@ -1 +1 @@ -{"0.elements": {"test_get_all_atoms.py": ["test_get_all_atoms"], "test_grams_to_mols.py": ["test_grams_to_mols"], "test_molar_mass.py": ["test_molar_mass"]}, "1.balancing": {"test_balance.py": ["test_check_balance_equation"], "test_make_atom_vector.py": ["test_make_atom_vector"], "test_make_compound_matrix.py": ["test_make_compound_matrix"], "test_make_dictionary_solution.py": ["test_make_dictionary_solution"], "test_make_dummy_equation.py": ["test_make_dummy_equation"], "test_make_system_of_equations.py": ["test_make_system_of_equations"], "test_scale_solution.py": ["test_scale_solution"]}, "2.matrix_operations": {"test_negate_matrix.py": ["test_negate_matrix"], "test_transpose_matrix.py": ["test_transpose_matrix"]}, "3.gaussian_elimination": {"test_back_substitute.py": ["test_back_substitute"], "test_gaussian_elimination.py": ["test_gaussian_elimination"], "test_pivot_matrix.py": ["test_pivot_matrix"], "test_ref.py": ["test_ref_zero_row", "test_ref_large_matrix"], "test_scale_matrix.py": ["test_scale_matrix"]}, "4.limiting_reactant": {"test_limiting_reactant.py": ["test_limiting_reactant"], "test_ratio.py": ["test_ratio"]}, "5.theoretical_yield": {"test_theoretical_yield.py": ["test_theoretical_yield"]}} +{"0.elements": {"test_get_all_atoms.py": ["test_get_all_atoms"], "test_grams_to_mols.py": ["test_grams_to_mols"], "test_molar_mass.py": ["test_molar_mass"]}, "1.balancing": {"test_balance.py": ["test_check_balance_equation"], "test_make_atom_vector.py": ["test_make_atom_vector"], "test_make_compound_matrix.py": ["test_make_compound_matrix"], "test_make_dictionary_solution.py": ["test_make_dictionary_solution"], "test_make_dummy_equation.py": ["test_make_dummy_equation"], "test_make_system_of_equations.py": ["test_make_system_of_equations"], "test_scale_solution.py": ["test_scale_solution"]}, "2.matrix_operations": {"test_negate_matrix.py": ["test_negate_matrix"], "test_transpose_matrix.py": ["test_transpose_matrix"]}, "3.gaussian_elimination": {"test_back_substitute.py": ["test_back_substitute"], "test_gaussian_elimination.py": ["test_gaussian_elimination"], "test_pivot_matrix.py": ["test_pivot_matrix_small", "test_pivot_matrix_large"], "test_ref.py": ["test_ref_zero_row", "test_ref_large_matrix"], "test_scale_matrix.py": ["test_scale_matrix"]}, "4.limiting_reactant": {"test_limiting_reactant.py": ["test_limiting_reactant"], "test_ratio.py": ["test_ratio"]}, "5.theoretical_yield": {"test_theoretical_yield.py": ["test_theoretical_yield"]}} diff --git a/tests/3.gaussian_elimination/test_pivot_matrix.py b/tests/3.gaussian_elimination/test_pivot_matrix.py index 76ef874..c756683 100644 --- a/tests/3.gaussian_elimination/test_pivot_matrix.py +++ b/tests/3.gaussian_elimination/test_pivot_matrix.py @@ -1,5 +1,5 @@ -import random import pytest +from fractions import Fraction from support.matrix import MutableRationalMatrix2D from tests.helpers.naming import apply_names from tests.helpers.matrix_helpers import generate_random_pivot_matrices @@ -10,8 +10,46 @@ ref_pivot_matrix = driver_import_reference('matrix_operations.pivot_matrix') from src.matrix_operations import pivot_matrix +_SMALL_PIVOTS = [ + [[1, 2, 3, 4], + [9, 8, 7, 6], + [7, 3, 4, 8], + ], + [[1, 2, 3, 4, 5], + [3, 2, 8, 6, 1], + [5, 7, 3, 2, 1], + [8, 2, 6, 4, 3], + ], + [[1, 2, 3, 4, 5, 6], + [8, 2, 8, 3, 6, 2], + [9, 4, 3, 7, 6, 3], + [0, 0, 0, 0, 0, 0], + [1, 2, 3, 4, 5, 6], + ] +] + + +def _generate_small_pivot_matrices(): + matrices = [] + for arr in _SMALL_PIVOTS: + mat = MutableRationalMatrix2D((len(arr), len(arr[0]))) + for i in range(mat.dimensions[0]): + for j in range(mat.dimensions[1]): + mat[i][j] = Fraction(arr[i][j]) + matrices.append([mat]) + return matrices + + +@pytest.mark.parametrize('matrix', apply_names('pivot_matrix', [False], _generate_small_pivot_matrices())) +def test_pivot_matrix_small(matrix: MutableRationalMatrix2D): + for col in range(2, len(matrix)): + correct = ref_pivot_matrix(matrix.mutable(), col) + out = pivot_matrix(matrix, col) + assert correct == out + + @pytest.mark.parametrize('matrix', apply_names('pivot_matrix', [False], generate_random_pivot_matrices())) -def test_pivot_matrix(matrix: MutableRationalMatrix2D): +def test_pivot_matrix_large(matrix: MutableRationalMatrix2D): for col in range(2, len(matrix)): correct = ref_pivot_matrix(matrix.mutable(), col) out = pivot_matrix(matrix, col) -- GitLab