From 51a05cbd86746657d1a1539011c2854a665046e3 Mon Sep 17 00:00:00 2001 From: RobotGuy999 <68413067+RobotGuy999@users.noreply.github.com> Date: Fri, 17 Jul 2020 23:19:41 +0800 Subject: [PATCH 1/7] Added "Inverse of Matrix" Algorithm --- matrix/inverse_of_matrix.py | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 matrix/inverse_of_matrix.py diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py new file mode 100644 index 000000000000..b0235c637336 --- /dev/null +++ b/matrix/inverse_of_matrix.py @@ -0,0 +1,54 @@ +from decimal import * + + +def inverse_of_matrix(matrix): + """ + A matrix multiplied with its inverse gives the identity matrix. + This function finds the inverse of a 2x2 matrix. + If the determinant of a matrix is 0, its inverse does not exist. + + Sources for fixing inaccurate float arithmetic: + https://stackoverflow.com/questions/6563058/how-do-i-use-accurate-float-arithmetic-in-python + https://docs.python.org/release/2.7/library/decimal.html#module-decimal + + >>> inverse_of_matrix([[2, 5], [2, 0]]) + [[0.0, 0.5], [0.2, -0.2]] + >>> inverse_of_matrix([[2.5, 5], [1, 2]]) + 'Does not exist' + >>> inverse_of_matrix([[12, -16], [-9, 0]]) + [[0.0, -0.1111111111111111], [-0.0625, -0.08333333333333333]] + >>> inverse_of_matrix([[12, 3], [16, 8]]) + [[0.16666666666666666, -0.0625], [-0.3333333333333333, 0.25]] + >>> inverse_of_matrix([[10, 9], [2, 2.5]]) + [[1.0, -4.5], [-1.0, 5.0]] + """ + + determinant = Decimal(matrix[0][0]) * Decimal(matrix[1][1]) - Decimal( + matrix[1][0] + ) * Decimal( + matrix[0][1] + ) # Finds the determinant of the matrix + if determinant != 0: + inverse = [] + matrix_with_swapped_pos = ( + [] + matrix + ) # Creates a copy of the matrix and swaps the positions of the elements + matrix_with_swapped_pos[0][0], matrix_with_swapped_pos[1][1] = ( + matrix[1][1], + matrix[0][0], + ) + matrix_with_swapped_pos[1][0], matrix_with_swapped_pos[0][1] = ( + 0 - matrix[1][0], + 0 - matrix[0][1], + ) + for row1 in matrix_with_swapped_pos: + inverse.append( + [float(Decimal(num) / determinant) for num in row1] + ) # Finds the inverse of the matrix + for row2 in inverse: + for index in range(len(row2)): # Replaces the -0.0's with 0.0's + if row2[index] == -0.0: + row2[index] = 0.0 + return inverse + else: + return "Does not exist" From 2230b460a62a160598598166510d334361925c15 Mon Sep 17 00:00:00 2001 From: RobotGuy999 <68413067+RobotGuy999@users.noreply.github.com> Date: Fri, 17 Jul 2020 23:29:37 +0800 Subject: [PATCH 2/7] Small quotation marks change --- matrix/inverse_of_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index b0235c637336..eda16927835c 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -51,4 +51,4 @@ def inverse_of_matrix(matrix): row2[index] = 0.0 return inverse else: - return "Does not exist" + return 'Does not exist' From d610d689da257527329212980bb29fb531afabfb Mon Sep 17 00:00:00 2001 From: RobotGuy999 <68413067+RobotGuy999@users.noreply.github.com> Date: Mon, 20 Jul 2020 21:16:34 +0800 Subject: [PATCH 3/7] Update inverse_of_matrix.py --- matrix/inverse_of_matrix.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index eda16927835c..f2de96fb2b67 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -1,7 +1,7 @@ -from decimal import * +from decimal import Decimal -def inverse_of_matrix(matrix): +def inverse_of_matrix(matrix: list): """ A matrix multiplied with its inverse gives the identity matrix. This function finds the inverse of a 2x2 matrix. @@ -9,7 +9,7 @@ def inverse_of_matrix(matrix): Sources for fixing inaccurate float arithmetic: https://stackoverflow.com/questions/6563058/how-do-i-use-accurate-float-arithmetic-in-python - https://docs.python.org/release/2.7/library/decimal.html#module-decimal + https://docs.python.org/3/library/decimal.html >>> inverse_of_matrix([[2, 5], [2, 0]]) [[0.0, 0.5], [0.2, -0.2]] From 69f8ee9fd3243eda3bb56744320882f206981ef9 Mon Sep 17 00:00:00 2001 From: RobotGuy999 <68413067+RobotGuy999@users.noreply.github.com> Date: Mon, 20 Jul 2020 23:53:18 +0800 Subject: [PATCH 4/7] Updated doctests --- matrix/inverse_of_matrix.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index f2de96fb2b67..5dd1db5c7483 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -19,8 +19,8 @@ def inverse_of_matrix(matrix: list): [[0.0, -0.1111111111111111], [-0.0625, -0.08333333333333333]] >>> inverse_of_matrix([[12, 3], [16, 8]]) [[0.16666666666666666, -0.0625], [-0.3333333333333333, 0.25]] - >>> inverse_of_matrix([[10, 9], [2, 2.5]]) - [[1.0, -4.5], [-1.0, 5.0]] + >>> inverse_of_matrix([[10, 5], [3, 2.5]]) + [[0.25, -0.5], [-0.3, 1]] """ determinant = Decimal(matrix[0][0]) * Decimal(matrix[1][1]) - Decimal( From 2c4b3637903df2a868472900740685748397df26 Mon Sep 17 00:00:00 2001 From: RobotGuy999 <68413067+RobotGuy999@users.noreply.github.com> Date: Tue, 21 Jul 2020 09:24:44 +0800 Subject: [PATCH 5/7] Update inverse_of_matrix.py --- matrix/inverse_of_matrix.py | 53 +++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index 5dd1db5c7483..434c9c02cfb3 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -14,13 +14,15 @@ def inverse_of_matrix(matrix: list): >>> inverse_of_matrix([[2, 5], [2, 0]]) [[0.0, 0.5], [0.2, -0.2]] >>> inverse_of_matrix([[2.5, 5], [1, 2]]) - 'Does not exist' + Traceback (most recent call last): + ... + ValueError: This matrix has no inverse. >>> inverse_of_matrix([[12, -16], [-9, 0]]) [[0.0, -0.1111111111111111], [-0.0625, -0.08333333333333333]] >>> inverse_of_matrix([[12, 3], [16, 8]]) [[0.16666666666666666, -0.0625], [-0.3333333333333333, 0.25]] >>> inverse_of_matrix([[10, 5], [3, 2.5]]) - [[0.25, -0.5], [-0.3, 1]] + [[0.25, -0.5], [-0.3, 1.0]] """ determinant = Decimal(matrix[0][0]) * Decimal(matrix[1][1]) - Decimal( @@ -28,27 +30,26 @@ def inverse_of_matrix(matrix: list): ) * Decimal( matrix[0][1] ) # Finds the determinant of the matrix - if determinant != 0: - inverse = [] - matrix_with_swapped_pos = ( - [] + matrix - ) # Creates a copy of the matrix and swaps the positions of the elements - matrix_with_swapped_pos[0][0], matrix_with_swapped_pos[1][1] = ( - matrix[1][1], - matrix[0][0], - ) - matrix_with_swapped_pos[1][0], matrix_with_swapped_pos[0][1] = ( - 0 - matrix[1][0], - 0 - matrix[0][1], - ) - for row1 in matrix_with_swapped_pos: - inverse.append( - [float(Decimal(num) / determinant) for num in row1] - ) # Finds the inverse of the matrix - for row2 in inverse: - for index in range(len(row2)): # Replaces the -0.0's with 0.0's - if row2[index] == -0.0: - row2[index] = 0.0 - return inverse - else: - return 'Does not exist' + if determinant == 0: + raise ValueError("This matrix has no inverse.") + inverse = [] + matrix_with_swapped_pos = ( + [] + matrix + ) # Creates a copy of the matrix and swaps the positions of the elements + matrix_with_swapped_pos[0][0], matrix_with_swapped_pos[1][1] = ( + matrix[1][1], + matrix[0][0], + ) + matrix_with_swapped_pos[1][0], matrix_with_swapped_pos[0][1] = ( + 0 - matrix[1][0], + 0 - matrix[0][1], + ) + for row1 in matrix_with_swapped_pos: + inverse.append( + [float(Decimal(num) / determinant) for num in row1] + ) # Finds the inverse of the matrix + for row2 in inverse: + for index in range(len(row2)): # Replaces the -0.0's with 0.0's + if row2[index] == -0.0: + row2[index] = 0.0 + return inverse From 007c8b6eb618ff23c6be1ddab6f4356035d7ae44 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 23 Jul 2020 13:06:31 +0200 Subject: [PATCH 6/7] Add type hints --- matrix/inverse_of_matrix.py | 38 +++++++++++-------------------------- 1 file changed, 11 insertions(+), 27 deletions(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index 434c9c02cfb3..74af90241846 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -1,7 +1,8 @@ from decimal import Decimal +from typing import List -def inverse_of_matrix(matrix: list): +def inverse_of_matrix(matrix: List[List[float]]) -> List[List[float]]: """ A matrix multiplied with its inverse gives the identity matrix. This function finds the inverse of a 2x2 matrix. @@ -25,31 +26,14 @@ def inverse_of_matrix(matrix: list): [[0.25, -0.5], [-0.3, 1.0]] """ - determinant = Decimal(matrix[0][0]) * Decimal(matrix[1][1]) - Decimal( - matrix[1][0] - ) * Decimal( - matrix[0][1] - ) # Finds the determinant of the matrix + D = Decimal # An abbreviation to be conciseness + # Calculate the determinant of the matrix + determinant = D(matrix[0][0]) * D(matrix[1][1]) - D(matrix[1][0]) * D(matrix[0][1]) if determinant == 0: raise ValueError("This matrix has no inverse.") - inverse = [] - matrix_with_swapped_pos = ( - [] + matrix - ) # Creates a copy of the matrix and swaps the positions of the elements - matrix_with_swapped_pos[0][0], matrix_with_swapped_pos[1][1] = ( - matrix[1][1], - matrix[0][0], - ) - matrix_with_swapped_pos[1][0], matrix_with_swapped_pos[0][1] = ( - 0 - matrix[1][0], - 0 - matrix[0][1], - ) - for row1 in matrix_with_swapped_pos: - inverse.append( - [float(Decimal(num) / determinant) for num in row1] - ) # Finds the inverse of the matrix - for row2 in inverse: - for index in range(len(row2)): # Replaces the -0.0's with 0.0's - if row2[index] == -0.0: - row2[index] = 0.0 - return inverse + # Creates a copy of the matrix with swaped positions of the elements + swapped_matrix = [[0.0, 0.0], [0.0, 0.0]] + swapped_matrix[0][0], swapped_matrix[1][1] = matrix[1][1], matrix[0][0] + swapped_matrix[1][0], swapped_matrix[0][1] = -matrix[1][0], -matrix[0][1] + # Calculate the inverse of the matrix + return [[float(D(n) / determinant) or 0.0 for n in row] for row in swapped_matrix] From 667ab6cc339c957b73180355916ffa3d0613c3bf Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 23 Jul 2020 13:09:00 +0200 Subject: [PATCH 7/7] swaped --> swapped --- matrix/inverse_of_matrix.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index 74af90241846..abbeb79ddbdb 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -31,7 +31,7 @@ def inverse_of_matrix(matrix: List[List[float]]) -> List[List[float]]: determinant = D(matrix[0][0]) * D(matrix[1][1]) - D(matrix[1][0]) * D(matrix[0][1]) if determinant == 0: raise ValueError("This matrix has no inverse.") - # Creates a copy of the matrix with swaped positions of the elements + # Creates a copy of the matrix with swapped positions of the elements swapped_matrix = [[0.0, 0.0], [0.0, 0.0]] swapped_matrix[0][0], swapped_matrix[1][1] = matrix[1][1], matrix[0][0] swapped_matrix[1][0], swapped_matrix[0][1] = -matrix[1][0], -matrix[0][1]