From 70cc59d90aeb0f6a43734ede94e3ee1f410a7f3f Mon Sep 17 00:00:00 2001 From: VarunS2002 <30069380+VarunS2002@users.noreply.github.com> Date: Wed, 25 May 2022 21:43:59 +0530 Subject: [PATCH 1/6] Added function to get identity matrix --- matrix_operations.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/matrix_operations.py b/matrix_operations.py index 2012d37..add9bcf 100644 --- a/matrix_operations.py +++ b/matrix_operations.py @@ -26,6 +26,14 @@ def __get_dimensions(matrix_1: matrix, matrix_2: Optional[matrix] = None) -> \ else: return dim_1 + # noinspection PyUnusedLocal + @staticmethod + def identity(dimension: int) -> matrix: + identity_m: matrix = [[0 for cols in range(dimension)] for rows in range(dimension)] + for i in range(0, dimension): + identity_m[i][i] = 1 + return identity_m + # noinspection PyUnusedLocal @staticmethod def addition(matrix_1: matrix, matrix_2: matrix) -> matrix: @@ -117,3 +125,5 @@ def display(matrix_1: matrix) -> None: MatrixOperations.display(difference) print('Multiplication of Matrix 3 & 4:') MatrixOperations.display(product) + print('Identity Matrix:') + MatrixOperations.display(MatrixOperations.identity(3)) From 3a698025931aba7f18571f723d791ddcad6dd7ab Mon Sep 17 00:00:00 2001 From: VarunS2002 <30069380+VarunS2002@users.noreply.github.com> Date: Wed, 25 May 2022 21:45:02 +0530 Subject: [PATCH 2/6] get_dimensions() now returns if a single matrix is a square matrix --- matrix_operations.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/matrix_operations.py b/matrix_operations.py index add9bcf..307d4a4 100644 --- a/matrix_operations.py +++ b/matrix_operations.py @@ -12,7 +12,7 @@ def __str__(self) -> str: @staticmethod def __get_dimensions(matrix_1: matrix, matrix_2: Optional[matrix] = None) -> \ - Union[dimensions, tuple[dimensions, dimensions, bool, bool]]: + Union[tuple[dimensions, bool], tuple[dimensions, dimensions, bool, bool]]: rows_1: int = len(matrix_1) cols_1: int = len(matrix_1[0]) dim_1: dimensions = (rows_1, cols_1) @@ -24,7 +24,8 @@ def __get_dimensions(matrix_1: matrix, matrix_2: Optional[matrix] = None) -> \ compatible_dim: bool = cols_1 == rows_2 return dim_1, dim_2, same_dim, compatible_dim else: - return dim_1 + square: bool = rows_1 == cols_1 + return dim_1, square # noinspection PyUnusedLocal @staticmethod @@ -86,7 +87,9 @@ def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix: # noinspection PyUnusedLocal @staticmethod def transpose(matrix_1: matrix) -> matrix: - dim: dimensions = MatrixOperations.__get_dimensions(matrix_1) + dim: dimensions + square: bool + dim, square = MatrixOperations.__get_dimensions(matrix_1) transpose_m: matrix = [[0 for cols in range(dim[0])] for rows in range(dim[1])] for i in range(0, dim[0]): for j in range(0, dim[1]): From 673d4a78ff7d147b83b1e76dcf2531d59cc3d10f Mon Sep 17 00:00:00 2001 From: VarunS2002 <30069380+VarunS2002@users.noreply.github.com> Date: Wed, 25 May 2022 21:47:48 +0530 Subject: [PATCH 3/6] Fixed incorrect error messages for addition() and subtraction() Separated error message from Exception class and made it into a parameter --- matrix_operations.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/matrix_operations.py b/matrix_operations.py index 307d4a4..8e64339 100644 --- a/matrix_operations.py +++ b/matrix_operations.py @@ -6,9 +6,11 @@ class MatrixOperations: class IncompatibleDimensionsError(Exception): + def __init__(self, message: Optional[str] = None) -> None: + self.message = message if message else "Incompatible dimensions" + def __str__(self) -> str: - return "Matrices are not compatible.\n" \ - "No. of Columns in Matrix 1 have to be the same as No. of Rows in Matrix 2." + return self.message @staticmethod def __get_dimensions(matrix_1: matrix, matrix_2: Optional[matrix] = None) -> \ @@ -44,7 +46,9 @@ def addition(matrix_1: matrix, matrix_2: matrix) -> matrix: compatible_dim: bool dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2) if not same_dim: - raise MatrixOperations.IncompatibleDimensionsError + raise MatrixOperations.IncompatibleDimensionsError("Matrices are not compatible.\n" + "No. of Rows & Columns in Matrix 1 have to be the same " + "as No. of Rows & Columns in Matrix 2.") sum_m: matrix = [[0 for cols in range(dim_1[1])] for rows in range(dim_1[0])] for i in range(0, dim_1[0]): for j in range(0, dim_1[1]): @@ -60,7 +64,9 @@ def subtraction(matrix_1: matrix, matrix_2: matrix) -> matrix: compatible_dim: bool dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2) if not same_dim: - raise MatrixOperations.IncompatibleDimensionsError + raise MatrixOperations.IncompatibleDimensionsError("Matrices are not compatible.\n" + "No. of Rows & Columns in Matrix 1 have to be the same " + "as No. of Rows & Columns in Matrix 2.") difference_m: matrix = [[0 for cols in range(dim_1[1])] for rows in range(dim_1[0])] for i in range(0, dim_1[0]): for j in range(0, dim_1[1]): @@ -76,7 +82,9 @@ def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix: compatible_dim: bool dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2) if not compatible_dim: - raise MatrixOperations.IncompatibleDimensionsError + raise MatrixOperations.IncompatibleDimensionsError("Matrices are not compatible.\n" + "No. of Columns in Matrix 1 have to be the same as No. " + "of Rows in Matrix 2.") product_m: matrix = [[0 for cols in range(dim_2[1])] for rows in range(dim_1[0])] for i in range(0, dim_1[0]): for j in range(0, dim_2[1]): From cc04dd29d2548e958479a5f2c5096a9d6230acb6 Mon Sep 17 00:00:00 2001 From: VarunS2002 <30069380+VarunS2002@users.noreply.github.com> Date: Wed, 25 May 2022 22:07:01 +0530 Subject: [PATCH 4/6] Stripped all other functionality from get_dimensions() and split them into independent public functions Made get_dimensions() public --- matrix_operations.py | 55 ++++++++++++++++++++++++++------------------ 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/matrix_operations.py b/matrix_operations.py index 8e64339..4a12276 100644 --- a/matrix_operations.py +++ b/matrix_operations.py @@ -13,8 +13,8 @@ def __str__(self) -> str: return self.message @staticmethod - def __get_dimensions(matrix_1: matrix, matrix_2: Optional[matrix] = None) -> \ - Union[tuple[dimensions, bool], tuple[dimensions, dimensions, bool, bool]]: + def get_dimensions(matrix_1: matrix, matrix_2: Optional[matrix] = None) -> \ + Union[dimensions, tuple[dimensions, dimensions]]: rows_1: int = len(matrix_1) cols_1: int = len(matrix_1[0]) dim_1: dimensions = (rows_1, cols_1) @@ -22,12 +22,28 @@ def __get_dimensions(matrix_1: matrix, matrix_2: Optional[matrix] = None) -> \ rows_2: int = len(matrix_2) cols_2: int = len(matrix_2[0]) dim_2: dimensions = (rows_2, cols_2) - same_dim: bool = dim_1 == dim_2 - compatible_dim: bool = cols_1 == rows_2 - return dim_1, dim_2, same_dim, compatible_dim + return dim_1, dim_2 else: - square: bool = rows_1 == cols_1 - return dim_1, square + return dim_1 + + @staticmethod + def is_square(matrix_1: matrix) -> bool: + dim: dimensions = MatrixOperations.get_dimensions(matrix_1) + return dim[0] == dim[1] + + @staticmethod + def have_same_dimensions(matrix_1: matrix, matrix_2: matrix) -> bool: + dim_1: dimensions + dim_2: dimensions + dim_1, dim_2 = MatrixOperations.get_dimensions(matrix_1, matrix_2) + return dim_1 == dim_2 + + @staticmethod + def have_multipliable_dimensions(matrix_1: matrix, matrix_2: matrix) -> bool: + dim_1: dimensions + dim_2: dimensions + dim_1, dim_2 = MatrixOperations.get_dimensions(matrix_1, matrix_2) + return dim_1[1] == dim_2[0] # noinspection PyUnusedLocal @staticmethod @@ -42,10 +58,9 @@ def identity(dimension: int) -> matrix: def addition(matrix_1: matrix, matrix_2: matrix) -> matrix: dim_1: dimensions dim_2: dimensions - same_dim: bool - compatible_dim: bool - dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2) - if not same_dim: + dim_1, dim_2 = MatrixOperations.get_dimensions(matrix_1, matrix_2) + have_same_dimensions: bool = MatrixOperations.have_same_dimensions(matrix_1, matrix_2) + if not have_same_dimensions: raise MatrixOperations.IncompatibleDimensionsError("Matrices are not compatible.\n" "No. of Rows & Columns in Matrix 1 have to be the same " "as No. of Rows & Columns in Matrix 2.") @@ -60,10 +75,9 @@ def addition(matrix_1: matrix, matrix_2: matrix) -> matrix: def subtraction(matrix_1: matrix, matrix_2: matrix) -> matrix: dim_1: dimensions dim_2: dimensions - same_dim: bool - compatible_dim: bool - dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2) - if not same_dim: + dim_1, dim_2 = MatrixOperations.get_dimensions(matrix_1, matrix_2) + have_same_dimensions: bool = MatrixOperations.have_same_dimensions(matrix_1, matrix_2) + if not have_same_dimensions: raise MatrixOperations.IncompatibleDimensionsError("Matrices are not compatible.\n" "No. of Rows & Columns in Matrix 1 have to be the same " "as No. of Rows & Columns in Matrix 2.") @@ -78,10 +92,9 @@ def subtraction(matrix_1: matrix, matrix_2: matrix) -> matrix: def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix: dim_1: dimensions dim_2: dimensions - same_dim: bool - compatible_dim: bool - dim_1, dim_2, same_dim, compatible_dim = MatrixOperations.__get_dimensions(matrix_1, matrix_2) - if not compatible_dim: + dim_1, dim_2 = MatrixOperations.get_dimensions(matrix_1, matrix_2) + have_multipliable_dimensions: bool = MatrixOperations.have_multipliable_dimensions(matrix_1, matrix_2) + if not have_multipliable_dimensions: raise MatrixOperations.IncompatibleDimensionsError("Matrices are not compatible.\n" "No. of Columns in Matrix 1 have to be the same as No. " "of Rows in Matrix 2.") @@ -95,9 +108,7 @@ def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix: # noinspection PyUnusedLocal @staticmethod def transpose(matrix_1: matrix) -> matrix: - dim: dimensions - square: bool - dim, square = MatrixOperations.__get_dimensions(matrix_1) + dim: dimensions = MatrixOperations.get_dimensions(matrix_1) transpose_m: matrix = [[0 for cols in range(dim[0])] for rows in range(dim[1])] for i in range(0, dim[0]): for j in range(0, dim[1]): From 6ec561a9fafd9702ed408fbdcf8c7a6dfb0a541e Mon Sep 17 00:00:00 2001 From: VarunS2002 <30069380+VarunS2002@users.noreply.github.com> Date: Wed, 25 May 2022 22:07:51 +0530 Subject: [PATCH 5/6] Added function to perform scalar multiplication --- matrix_operations.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/matrix_operations.py b/matrix_operations.py index 4a12276..142fe95 100644 --- a/matrix_operations.py +++ b/matrix_operations.py @@ -87,6 +87,16 @@ def subtraction(matrix_1: matrix, matrix_2: matrix) -> matrix: difference_m[i][j] = matrix_1[i][j] - matrix_2[i][j] return difference_m + # noinspection PyUnusedLocal + @staticmethod + def scalar_multiplication(matrix_1: matrix, scalar: float) -> matrix: + dim: dimensions = MatrixOperations.get_dimensions(matrix_1) + product_m: matrix = [[0 for cols in range(dim[1])] for rows in range(dim[0])] + for i in range(0, dim[0]): + for j in range(0, dim[1]): + product_m[i][j] = matrix_1[i][j] * scalar + return product_m + # noinspection PyUnusedLocal @staticmethod def multiplication(matrix_1: matrix, matrix_2: matrix) -> matrix: @@ -126,6 +136,7 @@ def display(matrix_1: matrix) -> None: m_2 = [[10, 20, 30], [40, 5, 6], [7, 8, 9]] m_3 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] m_4 = [[1, 2], [4, 5], [7, 8]] + scalar_value = 0.5 addition = MatrixOperations.addition(m_1, m_2) difference = MatrixOperations.subtraction(m_1, m_2) @@ -147,5 +158,7 @@ def display(matrix_1: matrix) -> None: MatrixOperations.display(difference) print('Multiplication of Matrix 3 & 4:') MatrixOperations.display(product) + print(f'Scalar Multiplication of Matrix 1 by {scalar_value}:') + MatrixOperations.display(MatrixOperations.scalar_multiplication(m_1, scalar_value)) print('Identity Matrix:') MatrixOperations.display(MatrixOperations.identity(3)) From 57b57327c98d860827c25437b31d8d7e016ab499 Mon Sep 17 00:00:00 2001 From: VarunS2002 <30069380+VarunS2002@users.noreply.github.com> Date: Thu, 26 May 2022 16:50:01 +0530 Subject: [PATCH 6/6] Updated README.md for matrix_operations.py --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c61566d..17d3127 100644 --- a/README.md +++ b/README.md @@ -10,8 +10,10 @@ These programs can be imported and used as modules in your programs. - Mathematical operations on Matrices - Addition - Subtraction + - Scalar Multiplication - Multiplication - Transpose + - Identity 2. [stack.py](https://github.com/VarunS2002/Python-Data-Structures-Algorithms/blob/master/stack.py) - Stack Data Structure