Last active
September 24, 2017 02:23
-
-
Save HorlogeSkynet/0a79fdd8304b7af9f011 to your computer and use it in GitHub Desktop.
Simple Python matrix inversion script
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
#!/usr/bin/env python3 | |
# -*-coding:utf-8 -* | |
""" | |
@author: HorlogeSkynet | |
""" | |
import os | |
import numpy as np | |
def clearScreen(): | |
os.system('cls' if os.name == 'nt' else 'clear') | |
def printPartialMatrix(matrice, n, i, j): | |
clearScreen() | |
print("\n\nPlease enter the values in the matrice:\n") | |
for _y in range(1, n + 1, 1): | |
print("\t[{}]".format(_y), end='') | |
print() | |
for _x in range(0, i + 1, 1): | |
print("\n[{}]".format(_x + 1), end='\t') | |
if _x == i: | |
for _y in range(0, j, 1): | |
print("{}".format(matrice[_x][_y]), end='\t') | |
else: | |
for _y in range(0, n, 1): | |
print("{}".format(matrice[_x][_y]), end='\t') | |
def promptMatrix(matrice, n): | |
for _i in range(0, n, 1): | |
for _j in range(0, n, 1): | |
printPartialMatrix(matrice, n, _i, _j) | |
matrice[_i][_j] = input() | |
def printMatrix(matrice, n, param): | |
if param: | |
print("\t\t", end = '') | |
for _y in range(1, n + 1, 1): | |
print("\t[{}]".format(_y), end='') | |
print() | |
for _x in range(0, n, 1): | |
if param: | |
if _x == n // 2: | |
print("\n(1 / {}) * ".format(param), end='\t') | |
else: | |
print("\n", end='\t\t') | |
else: | |
print() | |
print("[{}]".format(_x + 1), end='\t') | |
for _y in range(0, n, 1): | |
print("{}".format(matrice[_x][_y]), end='\t') | |
def zerosFinder(matrice, n): | |
# Let's find the line and the column which got the less '0' than others | |
line = 0 | |
column = 0 | |
maxZero_l = -1 | |
maxZero_c = -1 | |
for _i in range(0, n, 1): | |
zero_l = 0 | |
zero_c = 0 | |
for _j in range(0, n, 1): | |
if matrice[_i][_j] == 0: | |
zero_l += 1 | |
if matrice[_j][_i] == 0: | |
zero_c += 1 | |
if zero_l > maxZero_l: | |
maxZero_l = zero_l | |
line = _i | |
if zero_c > maxZero_c: | |
maxZero_c = zero_c | |
column = _i | |
return line, column | |
def determinant(matrice, n): | |
_value = 0 | |
if n == 2: | |
# Here: let's just return 'det(M) = (a * d) - (b * c)' | |
return (matrice[0][0] * matrice[1][1]) - (matrice[1][0] * matrice[0][1]) | |
else: | |
line, column = zerosFinder(matrice, n) | |
newMatrice = np.zeros((n - 1, n - 1)) | |
if line > column: | |
for _y in range(0, n, 1): | |
_u = 0 | |
_v = 0 | |
for _i in range(0, n, 1): | |
for _j in range(0, n, 1): | |
if _i != line and _j != _y: | |
newMatrice[_u][_v] = matrice[_i][_j] | |
if _v == (n - 1) - 1: | |
_u += 1 | |
_v = 0 | |
else: | |
_v += 1 | |
_value += matrice[line][_y] * (-1)**(line + _y) * determinant(newMatrice, n - 1) | |
else: | |
for _x in range(0, n, 1): | |
_u = 0 | |
_v = 0 | |
for _i in range(0, n, 1): | |
for _j in range(0, n, 1): | |
if _j != column and _i != _x: | |
newMatrice[_u][_v] = matrice[_i][_j] | |
if _v == (n - 1) - 1: | |
_u += 1 | |
_v = 0 | |
else: | |
_v += 1 | |
_value += matrice[_x][column] * (-1)**(_x + column) * determinant(newMatrice, n - 1) | |
return _value | |
def inverse(matrice, n, determinant): | |
matrice = transposee(comatrice(matrice, n), n) | |
printMatrix(matrice, n, determinant) | |
def comatrice(matrice, n): | |
coMatrice = np.zeros((n, n)) | |
if n > 2: | |
newMatrice = np.zeros((n - 1, n - 1)) | |
for _i in range(0, n, 1): | |
for _j in range(0, n, 1): | |
_u = 0 | |
_v = 0 | |
for _x in range(0, n, 1): | |
for _y in range(0, n, 1): | |
if _x != _i and _y != _j: | |
newMatrice[_u][_v] = matrice[_x][_y] | |
if _v == (n - 1) - 1: | |
_u += 1 | |
_v = 0 | |
else: | |
_v += 1 | |
coMatrice[_i][_j] = (-1)**(_i + _j) * determinant(newMatrice, n - 1) | |
else: | |
coMatrice[0][0], coMatrice[1][1] = matrice[1][1], matrice[0][0] | |
coMatrice[0][1], coMatrice[1][0] = (-1) * matrice[1][0], (-1) * matrice[0][1] | |
return coMatrice | |
def transposee(matrice, n): | |
for _i in range(0, n, 1): | |
for _j in range(_i + 1, n, 1): | |
matrice[_i][_j], matrice[_j][_i] = matrice[_j][_i], matrice[_i][_j] | |
return matrice | |
def trace(matrice, n): | |
trace = 0 | |
for _i in range(0, n, 1): | |
trace += matrice[_i][_i] | |
return trace | |
if __name__ == '__main__': | |
clearScreen() | |
n = 0 | |
while n <= 1: | |
n = abs(int(input("\nSquare matrice dimension: "))) | |
matrice = np.zeros((n, n)) | |
promptMatrix(matrice, n) | |
clearScreen() | |
print("\n-> M:\n") | |
printMatrix(matrice, n, False) | |
value = determinant(matrice, n) | |
print("\n\n\n-> Det(M) = {}.\n".format(value)) | |
if value != 0: | |
print("\n-> M^⁻¹:\n") | |
inverse(matrice, n, value) | |
else: | |
print("\n-> Indeed, M is not invertible.", end='') | |
print("\n\n\n-> tr(M): {}.\n\n\n".format(trace(matrice, n))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment