From 64cef2effd49b160bd18d245829f5e42f3d16000 Mon Sep 17 00:00:00 2001 From: karthik Date: Mon, 10 Oct 2022 23:54:43 +0530 Subject: [PATCH 1/6] Modified - Matrix/inverse_of_matrix.py - added implementation for inverse of 3x3 matrix --- matrix/inverse_of_matrix.py | 76 ++++++++++++++++++++++++++++++------- 1 file changed, 63 insertions(+), 13 deletions(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index 92780e656ea1..bb1caf0fa9db 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -6,13 +6,14 @@ 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. + This function finds the inverse of a 2x2 and 3x3 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/3/library/decimal.html + Demo for 2x2 >>> inverse_of_matrix([[2, 5], [2, 0]]) [[0.0, 0.5], [0.2, -0.2]] >>> inverse_of_matrix([[2.5, 5], [1, 2]]) @@ -25,24 +26,73 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: [[0.16666666666666666, -0.0625], [-0.3333333333333333, 0.25]] >>> inverse_of_matrix([[10, 5], [3, 2.5]]) [[0.25, -0.5], [-0.3, 1.0]] + + Demo for 3x3 + >>> inverse_of_matrix([[2, 5, 7], [2, 0, 1], [1, 2, 3]]) + [[2, 5, -4], [5, 1, -1], [4, 1, -10]] + >>> inverse_of_matrix([[1, 2, 2], [1, 2, 2], [3, 2, -1]) + Traceback (most recent call last): + ... + ValueError: This matrix has no inverse. """ D = Decimal # An abbreviation for conciseness # Check if the provided matrix has 2 rows and 2 columns # since this implementation only works for 2x2 matrices - if len(matrix) != 2 or len(matrix[0]) != 2 or len(matrix[1]) != 2: - raise ValueError("Please provide a matrix of size 2x2.") + if len(matrix) == 2 and len(matrix[0]) == 2 and len(matrix[1]) == 2: + # 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.") + + # 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] + + # Calculate the inverse of the matrix + return [[float(D(n) / determinant) or 0.0 for n in row] for row in swapped_matrix] + elif len(matrix) == 3 and len(matrix[0]) == 3 and len(matrix[1]) == 3 and len(matrix[2]) == 3: + # Calculate the determinant of the matrix using sarrus rule + determinant = ((D(matrix[0][0]) * D(matrix[1][1]) * D(matrix[2][2])) + ( + D(matrix[0][1]) * D(matrix[1][2]) * D(matrix[2][0])) + ( + D(matrix[0][2]) * D(matrix[1][0]) * D(matrix[2][1]))) - ( + (D(matrix[0][2]) * D(matrix[1][1]) * D(matrix[2][0])) + ( + D(matrix[0][1]) * D(matrix[1][0]) * D(matrix[2][2])) + ( + D(matrix[0][0]) * D(matrix[1][2]) * D(matrix[2][1]))) + if determinant == 0: + raise ValueError("This matrix has no inverse.") + + # Creating cofactor matrix + cofactor_matrix = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] + cofactor_matrix[0][0] = (D(matrix[1][1]) * D(matrix[2][2])) - (D(matrix[1][2]) * D(matrix[2][1])) + cofactor_matrix[0][1] = -((D(matrix[1][0]) * D(matrix[2][2])) - (D(matrix[1][2]) * D(matrix[2][0]))) + cofactor_matrix[0][2] = (D(matrix[1][0]) * D(matrix[2][1])) - (D(matrix[1][1]) * D(matrix[2][0])) + cofactor_matrix[1][0] = -((D(matrix[0][1]) * D(matrix[2][2])) - (D(matrix[0][2]) * D(matrix[2][1]))) + cofactor_matrix[1][1] = (D(matrix[0][0]) * D(matrix[2][2])) - (D(matrix[0][2]) * D(matrix[2][0])) + cofactor_matrix[1][2] = -((D(matrix[0][0]) * D(matrix[2][1])) - (D(matrix[0][1]) * D(matrix[2][0]))) + cofactor_matrix[2][0] = (D(matrix[0][1]) * D(matrix[1][2])) - (D(matrix[0][2]) * D(matrix[1][1])) + cofactor_matrix[2][1] = -((D(matrix[0][0]) * D(matrix[1][2])) - (D(matrix[0][2]) * D(matrix[1][0]))) + cofactor_matrix[2][2] = (D(matrix[0][0]) * D(matrix[1][1])) - (D(matrix[0][1]) * D(matrix[1][0])) + + # Transpose the cofactor matrix (Adjoint matrix) + adjoint_matrix = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] + for i in range(3): + for j in range(3): + adjoint_matrix[i][j] = cofactor_matrix[j][i] + + # Inverse of the matrix using the formula (1/determinant) * adjoint matrix = Inverse of matrix + inverse_matrix = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] + for i in range(3): + for j in range(3): + inverse_matrix[i][j] = (adjoint_matrix[i][j]) / (determinant) + + # Calculate the inverse of the matrix + return [[float(D(n)) or 0.0 for n in row] for row in inverse_matrix] + else: + raise ValueError("Please provide a matrix of size 2x2 or 3x3.") + - # 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.") - # 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] - # Calculate the inverse of the matrix - return [[float(D(n) / determinant) or 0.0 for n in row] for row in swapped_matrix] From 5818223691d9179a18ae1726f54b5b77db290aae Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Oct 2022 18:45:22 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- matrix/inverse_of_matrix.py | 70 +++++++++++++++++++++++++------------ 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index bb1caf0fa9db..66d7b2c301ce 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -42,7 +42,9 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: # since this implementation only works for 2x2 matrices if len(matrix) == 2 and len(matrix[0]) == 2 and len(matrix[1]) == 2: # Calculate the determinant of the matrix - determinant = D(matrix[0][0]) * D(matrix[1][1]) - D(matrix[1][0]) * D(matrix[0][1]) + 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.") @@ -52,29 +54,57 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: 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] - elif len(matrix) == 3 and len(matrix[0]) == 3 and len(matrix[1]) == 3 and len(matrix[2]) == 3: + return [ + [float(D(n) / determinant) or 0.0 for n in row] for row in swapped_matrix + ] + elif ( + len(matrix) == 3 + and len(matrix[0]) == 3 + and len(matrix[1]) == 3 + and len(matrix[2]) == 3 + ): # Calculate the determinant of the matrix using sarrus rule - determinant = ((D(matrix[0][0]) * D(matrix[1][1]) * D(matrix[2][2])) + ( - D(matrix[0][1]) * D(matrix[1][2]) * D(matrix[2][0])) + ( - D(matrix[0][2]) * D(matrix[1][0]) * D(matrix[2][1]))) - ( - (D(matrix[0][2]) * D(matrix[1][1]) * D(matrix[2][0])) + ( - D(matrix[0][1]) * D(matrix[1][0]) * D(matrix[2][2])) + ( - D(matrix[0][0]) * D(matrix[1][2]) * D(matrix[2][1]))) + determinant = ( + (D(matrix[0][0]) * D(matrix[1][1]) * D(matrix[2][2])) + + (D(matrix[0][1]) * D(matrix[1][2]) * D(matrix[2][0])) + + (D(matrix[0][2]) * D(matrix[1][0]) * D(matrix[2][1])) + ) - ( + (D(matrix[0][2]) * D(matrix[1][1]) * D(matrix[2][0])) + + (D(matrix[0][1]) * D(matrix[1][0]) * D(matrix[2][2])) + + (D(matrix[0][0]) * D(matrix[1][2]) * D(matrix[2][1])) + ) if determinant == 0: raise ValueError("This matrix has no inverse.") # Creating cofactor matrix cofactor_matrix = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] - cofactor_matrix[0][0] = (D(matrix[1][1]) * D(matrix[2][2])) - (D(matrix[1][2]) * D(matrix[2][1])) - cofactor_matrix[0][1] = -((D(matrix[1][0]) * D(matrix[2][2])) - (D(matrix[1][2]) * D(matrix[2][0]))) - cofactor_matrix[0][2] = (D(matrix[1][0]) * D(matrix[2][1])) - (D(matrix[1][1]) * D(matrix[2][0])) - cofactor_matrix[1][0] = -((D(matrix[0][1]) * D(matrix[2][2])) - (D(matrix[0][2]) * D(matrix[2][1]))) - cofactor_matrix[1][1] = (D(matrix[0][0]) * D(matrix[2][2])) - (D(matrix[0][2]) * D(matrix[2][0])) - cofactor_matrix[1][2] = -((D(matrix[0][0]) * D(matrix[2][1])) - (D(matrix[0][1]) * D(matrix[2][0]))) - cofactor_matrix[2][0] = (D(matrix[0][1]) * D(matrix[1][2])) - (D(matrix[0][2]) * D(matrix[1][1])) - cofactor_matrix[2][1] = -((D(matrix[0][0]) * D(matrix[1][2])) - (D(matrix[0][2]) * D(matrix[1][0]))) - cofactor_matrix[2][2] = (D(matrix[0][0]) * D(matrix[1][1])) - (D(matrix[0][1]) * D(matrix[1][0])) + cofactor_matrix[0][0] = (D(matrix[1][1]) * D(matrix[2][2])) - ( + D(matrix[1][2]) * D(matrix[2][1]) + ) + cofactor_matrix[0][1] = -( + (D(matrix[1][0]) * D(matrix[2][2])) - (D(matrix[1][2]) * D(matrix[2][0])) + ) + cofactor_matrix[0][2] = (D(matrix[1][0]) * D(matrix[2][1])) - ( + D(matrix[1][1]) * D(matrix[2][0]) + ) + cofactor_matrix[1][0] = -( + (D(matrix[0][1]) * D(matrix[2][2])) - (D(matrix[0][2]) * D(matrix[2][1])) + ) + cofactor_matrix[1][1] = (D(matrix[0][0]) * D(matrix[2][2])) - ( + D(matrix[0][2]) * D(matrix[2][0]) + ) + cofactor_matrix[1][2] = -( + (D(matrix[0][0]) * D(matrix[2][1])) - (D(matrix[0][1]) * D(matrix[2][0])) + ) + cofactor_matrix[2][0] = (D(matrix[0][1]) * D(matrix[1][2])) - ( + D(matrix[0][2]) * D(matrix[1][1]) + ) + cofactor_matrix[2][1] = -( + (D(matrix[0][0]) * D(matrix[1][2])) - (D(matrix[0][2]) * D(matrix[1][0])) + ) + cofactor_matrix[2][2] = (D(matrix[0][0]) * D(matrix[1][1])) - ( + D(matrix[0][1]) * D(matrix[1][0]) + ) # Transpose the cofactor matrix (Adjoint matrix) adjoint_matrix = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] @@ -92,7 +122,3 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: return [[float(D(n)) or 0.0 for n in row] for row in inverse_matrix] else: raise ValueError("Please provide a matrix of size 2x2 or 3x3.") - - - - From 8afba699b0fed650974d9e0974059194c895139d Mon Sep 17 00:00:00 2001 From: karthik Date: Tue, 11 Oct 2022 10:15:49 +0530 Subject: [PATCH 3/6] Modified matrix/inverse_of_matrix.py all test passing now MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [pre-commit.ci] pre-commit autoupdate (#6940) * [pre-commit.ci] pre-commit autoupdate updates: - [github.com/psf/black: 22.8.0 → 22.10.0](https://github.com/psf/black/compare/22.8.0...22.10.0) - [github.com/asottile/pyupgrade: v2.38.2 → v3.0.0](https://github.com/asottile/pyupgrade/compare/v2.38.2...v3.0.0) - [github.com/pre-commit/mirrors-mypy: v0.981 → v0.982](https://github.com/pre-commit/mirrors-mypy/compare/v0.981...v0.982) * updating DIRECTORY.md Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> --- .pre-commit-config.yaml | 6 +-- DIRECTORY.md | 1 + matrix/inverse_of_matrix.py | 92 +++++++++++++++++++++++++------------ 3 files changed, 67 insertions(+), 32 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index a2fcf12c9bbd..0abe647b017a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -14,7 +14,7 @@ repos: - id: requirements-txt-fixer - repo: https://github.com/psf/black - rev: 22.8.0 + rev: 22.10.0 hooks: - id: black @@ -26,7 +26,7 @@ repos: - --profile=black - repo: https://github.com/asottile/pyupgrade - rev: v2.38.2 + rev: v3.0.0 hooks: - id: pyupgrade args: @@ -42,7 +42,7 @@ repos: - --max-line-length=88 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.981 + rev: v0.982 hooks: - id: mypy args: diff --git a/DIRECTORY.md b/DIRECTORY.md index 668da4761f74..9ef72c403f32 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -927,6 +927,7 @@ ## Scheduling * [First Come First Served](scheduling/first_come_first_served.py) * [Highest Response Ratio Next](scheduling/highest_response_ratio_next.py) + * [Job Sequencing With Deadline](scheduling/job_sequencing_with_deadline.py) * [Multi Level Feedback Queue](scheduling/multi_level_feedback_queue.py) * [Non Preemptive Shortest Job First](scheduling/non_preemptive_shortest_job_first.py) * [Round Robin](scheduling/round_robin.py) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index bb1caf0fa9db..52433899e6c2 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -29,8 +29,8 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: Demo for 3x3 >>> inverse_of_matrix([[2, 5, 7], [2, 0, 1], [1, 2, 3]]) - [[2, 5, -4], [5, 1, -1], [4, 1, -10]] - >>> inverse_of_matrix([[1, 2, 2], [1, 2, 2], [3, 2, -1]) + [[2.0, 1.0, -5.0], [5.0, 1.0, -12.0], [-4.0, -1.0, 10.0]] + >>> inverse_of_matrix([[1, 2, 2], [1, 2, 2], [3, 2, -1]]) Traceback (most recent call last): ... ValueError: This matrix has no inverse. @@ -42,7 +42,9 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: # since this implementation only works for 2x2 matrices if len(matrix) == 2 and len(matrix[0]) == 2 and len(matrix[1]) == 2: # Calculate the determinant of the matrix - determinant = D(matrix[0][0]) * D(matrix[1][1]) - D(matrix[1][0]) * D(matrix[0][1]) + 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.") @@ -52,47 +54,79 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: 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] - elif len(matrix) == 3 and len(matrix[0]) == 3 and len(matrix[1]) == 3 and len(matrix[2]) == 3: - # Calculate the determinant of the matrix using sarrus rule - determinant = ((D(matrix[0][0]) * D(matrix[1][1]) * D(matrix[2][2])) + ( - D(matrix[0][1]) * D(matrix[1][2]) * D(matrix[2][0])) + ( - D(matrix[0][2]) * D(matrix[1][0]) * D(matrix[2][1]))) - ( - (D(matrix[0][2]) * D(matrix[1][1]) * D(matrix[2][0])) + ( - D(matrix[0][1]) * D(matrix[1][0]) * D(matrix[2][2])) + ( - D(matrix[0][0]) * D(matrix[1][2]) * D(matrix[2][1]))) + return [ + [float(D(n) / determinant) or 0.0 for n in row] for row in swapped_matrix + ] + elif ( + len(matrix) == 3 + and len(matrix[0]) == 3 + and len(matrix[1]) == 3 + and len(matrix[2]) == 3 + ): + # Calculate the determinant of the matrix using Sarrus rule + determinant = ( + (D(matrix[0][0]) * D(matrix[1][1]) * D(matrix[2][2])) + + (D(matrix[0][1]) * D(matrix[1][2]) * D(matrix[2][0])) + + (D(matrix[0][2]) * D(matrix[1][0]) * D(matrix[2][1])) + ) - ( + (D(matrix[0][2]) * D(matrix[1][1]) * D(matrix[2][0])) + + (D(matrix[0][1]) * D(matrix[1][0]) * D(matrix[2][2])) + + (D(matrix[0][0]) * D(matrix[1][2]) * D(matrix[2][1])) + ) if determinant == 0: raise ValueError("This matrix has no inverse.") # Creating cofactor matrix - cofactor_matrix = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] - cofactor_matrix[0][0] = (D(matrix[1][1]) * D(matrix[2][2])) - (D(matrix[1][2]) * D(matrix[2][1])) - cofactor_matrix[0][1] = -((D(matrix[1][0]) * D(matrix[2][2])) - (D(matrix[1][2]) * D(matrix[2][0]))) - cofactor_matrix[0][2] = (D(matrix[1][0]) * D(matrix[2][1])) - (D(matrix[1][1]) * D(matrix[2][0])) - cofactor_matrix[1][0] = -((D(matrix[0][1]) * D(matrix[2][2])) - (D(matrix[0][2]) * D(matrix[2][1]))) - cofactor_matrix[1][1] = (D(matrix[0][0]) * D(matrix[2][2])) - (D(matrix[0][2]) * D(matrix[2][0])) - cofactor_matrix[1][2] = -((D(matrix[0][0]) * D(matrix[2][1])) - (D(matrix[0][1]) * D(matrix[2][0]))) - cofactor_matrix[2][0] = (D(matrix[0][1]) * D(matrix[1][2])) - (D(matrix[0][2]) * D(matrix[1][1])) - cofactor_matrix[2][1] = -((D(matrix[0][0]) * D(matrix[1][2])) - (D(matrix[0][2]) * D(matrix[1][0]))) - cofactor_matrix[2][2] = (D(matrix[0][0]) * D(matrix[1][1])) - (D(matrix[0][1]) * D(matrix[1][0])) + cofactor_matrix = [ + [D(0.0), D(0.0), D(0.0)], + [D(0.0), D(0.0), D(0.0)], + [D(0.0), D(0.0), D(0.0)], + ] + cofactor_matrix[0][0] = (D(matrix[1][1]) * D(matrix[2][2])) - ( + D(matrix[1][2]) * D(matrix[2][1]) + ) + cofactor_matrix[0][1] = -( + (D(matrix[1][0]) * D(matrix[2][2])) - (D(matrix[1][2]) * D(matrix[2][0])) + ) + cofactor_matrix[0][2] = (D(matrix[1][0]) * D(matrix[2][1])) - ( + D(matrix[1][1]) * D(matrix[2][0]) + ) + cofactor_matrix[1][0] = -( + (D(matrix[0][1]) * D(matrix[2][2])) - (D(matrix[0][2]) * D(matrix[2][1])) + ) + cofactor_matrix[1][1] = (D(matrix[0][0]) * D(matrix[2][2])) - ( + D(matrix[0][2]) * D(matrix[2][0]) + ) + cofactor_matrix[1][2] = -( + (D(matrix[0][0]) * D(matrix[2][1])) - (D(matrix[0][1]) * D(matrix[2][0])) + ) + cofactor_matrix[2][0] = (D(matrix[0][1]) * D(matrix[1][2])) - ( + D(matrix[0][2]) * D(matrix[1][1]) + ) + cofactor_matrix[2][1] = -( + (D(matrix[0][0]) * D(matrix[1][2])) - (D(matrix[0][2]) * D(matrix[1][0])) + ) + cofactor_matrix[2][2] = (D(matrix[0][0]) * D(matrix[1][1])) - ( + D(matrix[0][1]) * D(matrix[1][0]) + ) # Transpose the cofactor matrix (Adjoint matrix) - adjoint_matrix = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] + adjoint_matrix = [ + [D(0.0), D(0.0), D(0.0)], + [D(0.0), D(0.0), D(0.0)], + [D(0.0), D(0.0), D(0.0)], + ] for i in range(3): for j in range(3): adjoint_matrix[i][j] = cofactor_matrix[j][i] - # Inverse of the matrix using the formula (1/determinant) * adjoint matrix = Inverse of matrix + # Inverse of the matrix using the formula (1/determinant) * adjoint matrix inverse_matrix = [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]] for i in range(3): for j in range(3): - inverse_matrix[i][j] = (adjoint_matrix[i][j]) / (determinant) + inverse_matrix[i][j] = float(adjoint_matrix[i][j]) / float(determinant) # Calculate the inverse of the matrix return [[float(D(n)) or 0.0 for n in row] for row in inverse_matrix] else: raise ValueError("Please provide a matrix of size 2x2 or 3x3.") - - - - From 9f726f33d5126d45f1b2ec0a374d463c3c57fb5b Mon Sep 17 00:00:00 2001 From: karthik Date: Tue, 11 Oct 2022 15:36:26 +0530 Subject: [PATCH 4/6] Improved docstring with more examples --- matrix/inverse_of_matrix.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index 52433899e6c2..5821dce2d10e 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -34,6 +34,31 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: Traceback (most recent call last): ... ValueError: This matrix has no inverse. + + More examples: + >>> inverse_of_matrix([]) + Traceback (most recent call last): + ... + ValueError: Please provide a matrix of size 2x2 or 3x3. + >>> inverse_of_matrix([[],[]]) + Traceback (most recent call last): + ... + ValueError: Please provide a matrix of size 2x2 or 3x3. + >>> inverse_of_matrix([[1, 2], [3, 4], [5, 6]]) + Traceback (most recent call last): + ... + ValueError: Please provide a matrix of size 2x2 or 3x3. + >>> inverse_of_matrix([[1, 2, 1], [0,3, 4]]) + Traceback (most recent call last): + ... + ValueError: Please provide a matrix of size 2x2 or 3x3. + + >>> inverse_of_matrix([[1, 2, 3], [7, 8, 9], [7, 8, 9]]) + Traceback (most recent call last): + ... + ValueError: This matrix has no inverse. + >>> inverse_of_matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) + [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] """ D = Decimal # An abbreviation for conciseness From 89bfd2450b5542f187fe6eba027d30e2e11cfdc5 Mon Sep 17 00:00:00 2001 From: karthik Date: Tue, 11 Oct 2022 16:10:10 +0530 Subject: [PATCH 5/6] Improved docstring --- matrix/inverse_of_matrix.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index 5821dce2d10e..36a9adc21a4b 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -36,18 +36,22 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: ValueError: This matrix has no inverse. More examples: + >>> inverse_of_matrix([]) Traceback (most recent call last): ... ValueError: Please provide a matrix of size 2x2 or 3x3. + >>> inverse_of_matrix([[],[]]) Traceback (most recent call last): ... ValueError: Please provide a matrix of size 2x2 or 3x3. + >>> inverse_of_matrix([[1, 2], [3, 4], [5, 6]]) Traceback (most recent call last): ... ValueError: Please provide a matrix of size 2x2 or 3x3. + >>> inverse_of_matrix([[1, 2, 1], [0,3, 4]]) Traceback (most recent call last): ... @@ -57,8 +61,14 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: Traceback (most recent call last): ... ValueError: This matrix has no inverse. + >>> inverse_of_matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] + + >>> inverse_of_matrix([1, 2, 3], [4, 5, 6]) + Trackback (most recent call last): + ... + TypeError: inverse_of_matrix() takes 1 positional argument but 2 were given """ D = Decimal # An abbreviation for conciseness From d2b54eb27e92a86c71f3360643da7f3cd5b62fca Mon Sep 17 00:00:00 2001 From: karthik Date: Tue, 11 Oct 2022 16:44:38 +0530 Subject: [PATCH 6/6] deleted one example from docstring --- matrix/inverse_of_matrix.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/matrix/inverse_of_matrix.py b/matrix/inverse_of_matrix.py index 36a9adc21a4b..8086b4bb5f17 100644 --- a/matrix/inverse_of_matrix.py +++ b/matrix/inverse_of_matrix.py @@ -64,11 +64,6 @@ def inverse_of_matrix(matrix: list[list[float]]) -> list[list[float]]: >>> inverse_of_matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0], [0.0, 0.0, 1.0]] - - >>> inverse_of_matrix([1, 2, 3], [4, 5, 6]) - Trackback (most recent call last): - ... - TypeError: inverse_of_matrix() takes 1 positional argument but 2 were given """ D = Decimal # An abbreviation for conciseness