forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added "Inverse of Matrix" Algorithm (TheAlgorithms#2209)
* Added "Inverse of Matrix" Algorithm * Small quotation marks change * Update inverse_of_matrix.py * Updated doctests * Update inverse_of_matrix.py * Add type hints * swaped --> swapped Co-authored-by: Christian Clauss <[email protected]>
- Loading branch information
1 parent
b395003
commit a823a86
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,39 @@ | ||
from decimal import Decimal | ||
from typing import 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. | ||
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 | ||
>>> inverse_of_matrix([[2, 5], [2, 0]]) | ||
[[0.0, 0.5], [0.2, -0.2]] | ||
>>> inverse_of_matrix([[2.5, 5], [1, 2]]) | ||
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]] | ||
""" | ||
|
||
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.") | ||
# 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] |