Skip to content

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 7 commits into from
Jul 23, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions matrix/inverse_of_matrix.py
Original file line number Diff line number Diff line change
@@ -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'