-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added "Inverse of Matrix" Algorithm #2209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
51a05cb
Added "Inverse of Matrix" Algorithm
RobotGuy999 2230b46
Small quotation marks change
RobotGuy999 d610d68
Update inverse_of_matrix.py
RobotGuy999 69f8ee9
Updated doctests
RobotGuy999 2c4b363
Update inverse_of_matrix.py
RobotGuy999 007c8b6
Add type hints
cclauss 667ab6c
swaped --> swapped
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
from decimal import * | ||
|
||
|
||
def inverse_of_matrix(matrix): | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
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 | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> 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' | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> 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 = [] | ||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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' |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.