diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7bfcd7a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__/ +*.pyc \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4b5a294 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "python-envs.defaultEnvManager": "ms-python.python:conda", + "python-envs.defaultPackageManager": "ms-python.python:conda" +} \ No newline at end of file diff --git a/__pycache__/fraction.cpython-312.pyc b/__pycache__/fraction.cpython-312.pyc deleted file mode 100644 index 29247b0..0000000 Binary files a/__pycache__/fraction.cpython-312.pyc and /dev/null differ diff --git a/__pycache__/test_fraction.cpython-312.pyc b/__pycache__/test_fraction.cpython-312.pyc deleted file mode 100644 index 4128782..0000000 Binary files a/__pycache__/test_fraction.cpython-312.pyc and /dev/null differ diff --git a/matrix.py b/matrix.py new file mode 100644 index 0000000..e3458d0 --- /dev/null +++ b/matrix.py @@ -0,0 +1,113 @@ +import ast + +class Matrix: + + def parse_matrix(self, mat_str): + try: + mat = ast.literal_eval(mat_str) + + if not isinstance(mat, list) or not mat: + raise ValueError("Invalid matrix") + + row_len = len(mat[0]) + for row in mat: + if not isinstance(row, list) or len(row) != row_len: + raise ValueError("Irregular matrix") + + return mat + + except: + raise ValueError("Invalid matrix format") + + + def matrix_add(self, A, B): + A = self.parse_matrix(A) + B = self.parse_matrix(B) + + if not A or not B: + raise ValueError("Empty matrix") + + rows = len(A) + cols = len(A[0]) + + if rows != len(B) or cols != len(B[0]): + raise ValueError("Matrix dimensions must match") + + result = [] + for i in range(rows): + row = [] + for j in range(cols): + row.append(A[i][j] + B[i][j]) + result.append(row) + + return str(result) + + + def matrix_subtract(self, A, B): + A = self.parse_matrix(A) + B = self.parse_matrix(B) + + if not A or not B: + raise ValueError("Empty matrix") + + rows = len(A) + cols = len(A[0]) + + if rows != len(B) or cols != len(B[0]): + raise ValueError("Matrix dimensions must match") + + result = [] + for i in range(rows): + row = [] + for j in range(cols): + row.append(A[i][j] - B[i][j]) + result.append(row) + + return str(result) + + + def matrix_multiply(self, A, B): + A = self.parse_matrix(A) + B = self.parse_matrix(B) + + if not A or not B: + raise ValueError("Empty matrix") + + rows_A = len(A) + cols_A = len(A[0]) + rows_B = len(B) + cols_B = len(B[0]) + + if cols_A != rows_B: + raise ValueError("Invalid dimensions for multiplication") + + result = [] + for i in range(rows_A): + row = [] + for j in range(cols_B): + val = 0 + for k in range(cols_A): + val += A[i][k] * B[k][j] + row.append(val) + result.append(row) + + return str(result) + + + def matrix_transpose(self, A): + A = self.parse_matrix(A) + + if not A: + raise ValueError("Empty matrix") + + rows = len(A) + cols = len(A[0]) + + result = [] + for j in range(cols): + row = [] + for i in range(rows): + row.append(A[i][j]) + result.append(row) + + return str(result) \ No newline at end of file diff --git a/test_matrix.py b/test_matrix.py new file mode 100644 index 0000000..393ac1a --- /dev/null +++ b/test_matrix.py @@ -0,0 +1,85 @@ +def test_add(): + from matrix import Matrix + m = Matrix() + assert m.matrix_add('[[1,2],[3,4]]', '[[5,6],[7,8]]') == '[[6, 8], [10, 12]]' + +def test_multiply(): + from matrix import Matrix + m = Matrix() + assert m.matrix_multiply('[[1,2],[3,4]]', '[[5,6],[7,8]]') == '[[19, 22], [43, 50]]' + +def test_transpose(): + from matrix import Matrix + m = Matrix() + assert m.matrix_transpose('[[1,2],[3,4]]') == '[[1, 3], [2, 4]]' + +def test_subtract(): + from matrix import Matrix + m = Matrix() + assert m.matrix_subtract('[[5,6],[7,8]]', '[[1,2],[3,4]]') == '[[4, 4], [4, 4]]' + +def test_add_dimension_mismatch(): + from matrix import Matrix + m = Matrix() + try: + m.matrix_add('[[1,2]]', '[[1,2],[3,4]]') + assert False + except ValueError: + assert True +def test_empty_matrix(): + from matrix import Matrix + m = Matrix() + try: + m.matrix_add('[]', '[]') + assert False + except ValueError: + assert True + +def test_invalid_format(): + from matrix import Matrix + m = Matrix() + try: + m.matrix_add('abc', '[[1,2]]') + assert False + except ValueError: + assert True + +def test_irregular_matrix(): + from matrix import Matrix + m = Matrix() + try: + m.matrix_add('[[1,2],[3]]', '[[1,2],[3,4]]') + assert False + except ValueError: + assert True + +def test_multiply_dimension_mismatch(): + from matrix import Matrix + m = Matrix() + try: + m.matrix_multiply('[[1,2]]', '[[1,2]]') + assert False + except ValueError: + assert True + +def test_single_element(): + from matrix import Matrix + m = Matrix() + assert m.matrix_add('[[5]]', '[[3]]') == '[[8]]' + +def test_negative_values(): + from matrix import Matrix + m = Matrix() + assert m.matrix_add('[[-1,-2],[-3,-4]]', '[[1,2],[3,4]]') == '[[0, 0], [0, 0]]' + +def test_transpose_rectangular(): + from matrix import Matrix + m = Matrix() + assert m.matrix_transpose('[[1,2,3],[4,5,6]]') == '[[1, 4], [2, 5], [3, 6]]' + +def test_large_matrix(): + from matrix import Matrix + m = Matrix() + A = '[[1,1],[1,1]]' + B = '[[1,1],[1,1]]' + assert m.matrix_add(A, B) == '[[2, 2], [2, 2]]' \ No newline at end of file