|
| 1 | +from typing import Optional, Union |
| 2 | + |
| 3 | +matrix = list[list[float]] |
| 4 | +dimensions = tuple[int, int] |
| 5 | + |
| 6 | + |
| 7 | +class MatrixOperations: |
| 8 | + class IncompatibleDimensionsError(Exception): |
| 9 | + def __str__(self) -> str: |
| 10 | + return "Matrices are not compatible.\n" \ |
| 11 | + "No. of Columns in Matrix 1 have to be the same as No. of Rows in Matrix 2." |
| 12 | + |
| 13 | + @staticmethod |
| 14 | + def __get_dimensions(matrix_1: matrix, matrix_2: Optional[matrix] = None) -> \ |
| 15 | + Union[dimensions, tuple[dimensions, dimensions, bool, bool]]: |
| 16 | + rows_1: int = len(matrix_1) |
| 17 | + cols_1: int = len(matrix_1[0]) |
| 18 | + dim_1: dimensions = (rows_1, cols_1) |
| 19 | + if matrix_2 is not None: |
| 20 | + rows_2: int = len(matrix_2) |
| 21 | + cols_2: int = len(matrix_2[0]) |
| 22 | + dim_2: dimensions = (rows_2, cols_2) |
| 23 | + same_dim: bool = dim_1 == dim_2 |
| 24 | + compatible_dim: bool = cols_1 == rows_2 |
| 25 | + return dim_1, dim_2, same_dim, compatible_dim |
| 26 | + else: |
| 27 | + return dim_1 |
| 28 | + |
| 29 | + # noinspection PyUnusedLocal |
| 30 | + @staticmethod |
| 31 | + def addition(matrix_1: matrix, matrix_2: matrix) -> matrix: |
| 32 | + dim_1: dimensions |
| 33 | + dim_2: dimensions |
| 34 | + same_dim: bool |
| 35 | + compatible_dim: bool |
| 36 | + dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2) |
| 37 | + if not same_dim: |
| 38 | + raise MatrixOperations.IncompatibleDimensionsError |
| 39 | + sum_m: matrix = [[0 for cols in range(dim_1[1])] for rows in range(dim_1[0])] |
| 40 | + for i in range(0, dim_1[0]): |
| 41 | + for j in range(0, dim_1[1]): |
| 42 | + sum_m[i][j] = matrix_1[i][j] + matrix_2[i][j] |
| 43 | + return sum_m |
| 44 | + |
| 45 | + # noinspection PyUnusedLocal |
| 46 | + @staticmethod |
| 47 | + def subtraction(matrix_1: matrix, matrix_2: matrix) -> matrix: |
| 48 | + dim_1: dimensions |
| 49 | + dim_2: dimensions |
| 50 | + same_dim: bool |
| 51 | + compatible_dim: bool |
| 52 | + dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2) |
| 53 | + if not same_dim: |
| 54 | + raise MatrixOperations.IncompatibleDimensionsError |
| 55 | + difference_m: matrix = [[0 for cols in range(dim_1[1])] for rows in range(dim_1[0])] |
| 56 | + for i in range(0, dim_1[0]): |
| 57 | + for j in range(0, dim_1[1]): |
| 58 | + difference_m[i][j] = matrix_1[i][j] - matrix_2[i][j] |
| 59 | + return difference_m |
| 60 | + |
| 61 | + # noinspection PyUnusedLocal |
| 62 | + @staticmethod |
| 63 | + def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix: |
| 64 | + dim_1: dimensions |
| 65 | + dim_2: dimensions |
| 66 | + same_dim: bool |
| 67 | + compatible_dim: bool |
| 68 | + dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2) |
| 69 | + if not compatible_dim: |
| 70 | + raise MatrixOperations.IncompatibleDimensionsError |
| 71 | + product_m: matrix = [[0 for cols in range(dim_2[1])] for rows in range(dim_1[0])] |
| 72 | + for i in range(0, dim_1[0]): |
| 73 | + for j in range(0, dim_2[1]): |
| 74 | + for k in range(0, dim_2[0]): |
| 75 | + product_m[i][j] += matrix_1[i][k] * matrix_2[k][j] |
| 76 | + return product_m |
| 77 | + |
| 78 | + # noinspection PyUnusedLocal |
| 79 | + @staticmethod |
| 80 | + def transpose(matrix_1: matrix) -> matrix: |
| 81 | + dim: dimensions = MatrixOperations.__get_dimensions(matrix_1) |
| 82 | + transpose_m: matrix = [[0 for cols in range(dim[0])] for rows in range(dim[1])] |
| 83 | + for i in range(0, dim[0]): |
| 84 | + for j in range(0, dim[1]): |
| 85 | + transpose_m[j][i] = matrix_1[i][j] |
| 86 | + return transpose_m |
| 87 | + |
| 88 | + @staticmethod |
| 89 | + def display(matrix_1: matrix) -> None: |
| 90 | + for row in matrix_1: |
| 91 | + print(row) |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == '__main__': |
| 95 | + m_1 = [[10, 20, 30], [40, 5, 6], [7, 8, 9]] |
| 96 | + m_2 = [[10, 20, 30], [40, 5, 6], [7, 8, 9]] |
| 97 | + m_3 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] |
| 98 | + m_4 = [[1, 2], [4, 5], [7, 8]] |
| 99 | + |
| 100 | + addition = MatrixOperations.addition(m_1, m_2) |
| 101 | + difference = MatrixOperations.subtraction(m_1, m_2) |
| 102 | + transpose = MatrixOperations.transpose(m_3) |
| 103 | + product = MatrixOperations.multiplication(m_3, m_4) |
| 104 | + print('Matrix 1:') |
| 105 | + MatrixOperations.display(m_1) |
| 106 | + print('Matrix 2:') |
| 107 | + MatrixOperations.display(m_2) |
| 108 | + print('Matrix 3:') |
| 109 | + MatrixOperations.display(m_3) |
| 110 | + print('Matrix 4:') |
| 111 | + MatrixOperations.display(m_4) |
| 112 | + print('Transpose of Matrix 3:') |
| 113 | + MatrixOperations.display(transpose) |
| 114 | + print('Addition of Matrix 1 & 2:') |
| 115 | + MatrixOperations.display(addition) |
| 116 | + print('Subtraction of Matrix 1 & 2:') |
| 117 | + MatrixOperations.display(difference) |
| 118 | + print('Multiplication of Matrix 3 & 4:') |
| 119 | + MatrixOperations.display(product) |
0 commit comments