Created
April 20, 2018 12:37
-
-
Save samehmohamed88/7ebeca84b6c435dd5d8866582d843f7e to your computer and use it in GitHub Desktop.
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
lines = open('/proc/meminfo', 'r').read().split('\n') | |
def get_mem_free(): | |
for line in lines: | |
if 'MemFree' in line: | |
return int(line.split(':')[1].strip().split(' ')[0]) | |
def transposeMatrix(m): | |
return list(map(list,zip(*m))) | |
def getMatrixMinor(m,i,j): | |
return [row[:j] + row[j+1:] for row in (m[:i]+m[i+1:])] | |
def getMatrixDeternminant(m): | |
#base case for 2x2 matrix | |
if len(m) == 2: | |
return m[0][0]*m[1][1]-m[0][1]*m[1][0] | |
determinant = 0 | |
for c in range(len(m)): | |
determinant += ((-1)**c)*m[0][c]*getMatrixDeternminant(getMatrixMinor(m,0,c)) | |
return determinant | |
def getMatrixInverse(m): | |
determinant = getMatrixDeternminant(m) | |
#special case for 2x2 matrix: | |
if len(m) == 2: | |
return [[m[1][1]/determinant, -1*m[0][1]/determinant], | |
[-1*m[1][0]/determinant, m[0][0]/determinant]] | |
#find matrix of cofactors | |
cofactors = [] | |
for r in range(len(m)): | |
cofactorRow = [] | |
for c in range(len(m)): | |
minor = getMatrixMinor(m,r,c) | |
cofactorRow.append(((-1)**(r+c)) * getMatrixDeternminant(minor)) | |
cofactors.append(cofactorRow) | |
cofactors = transposeMatrix(cofactors) | |
for r in range(len(cofactors)): | |
for c in range(len(cofactors)): | |
cofactors[r][c] = cofactors[r][c]/determinant | |
return cofactors | |
def generate_matrix(): | |
import random | |
M = [] | |
for i in range(9): | |
n = [] | |
for j in range(9): | |
n.append(random.randint(1, 100)) | |
M.append(n) | |
return M | |
# print(generate_matrix()) | |
print(getMatrixInverse(generate_matrix())) | |
# print(get_mem_free()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment