Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

matrix inverse python without numpy

def transposeMatrix(m):
    return 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
Comment

PREVIOUS NEXT
Code Example
Python :: two dimensional array python 
Python :: how do i limit decimals to only two decimals in python 
Python :: showing specific columns pandas 
Python :: how to add two list by zip function in python 
Python :: python is folder or file 
Python :: how to declare a variable in python 
Python :: subtract from dataframe column 
Python :: yaxis on the right matplotlib 
Python :: how to run django in jupyter 
Python :: changing the port of django port 
Python :: run matlab code in python 
Python :: how to create an empty list of certain length in python 
Python :: pandas print a single row 
Python :: pytube 
Python :: turn off warning when import python 
Python :: how to run same function on multiple threads in pyhton 
Python :: django migrate model 
Python :: how to run python file 
Python :: create pandas dataframe from dictionary 
Python :: django slug int url mapping 
Python :: python thread function 
Python :: how to convert .ui file to .py 
Python :: find the highest id in model django 
Python :: read file csv in python 
Python :: open file in python directory 
Python :: how to for loop for amount of characters in string python 
Python :: python b string 
Python :: time date year python 
Python :: python get the app path 
Python :: how to define piecewise function i python 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =