Finding Inverse of Matrix using Cramer's Rule - python

I have created a function determinant which outputs a determinant of a 3x3 matrix. I also need to create a function to invert that matrix however the code doesn't seem to work and I can't figure out why.
M = np.array([
[4.,3.,9.],
[2.,1.,8.],
[10.,7.,5.]
])
def inverse(M):
'''
This function finds the inverse of a matrix using the Cramers rule.
Input: Matrix - M
Output: The inverse of the Matrix - M.
'''
d = determinant(M) # Simply returns the determinant of the matrix M.
counter = 1
array = []
for line in M: # This for loop simply creates a co-factor of Matrix M and puts it in a list.
y = []
for item in line:
if counter %2 == 0:
x = -item
else:
x = item
counter += 1
y.append(x)
array.append(y)
cf = np.matrix(array) # Translating the list into a matrix.
adj = np.matrix.transpose(cf) # Transposing the matrix.
inv = (1/d) * adj
return inv
OUTPUT:
via inverse(M):
[[ 0.0952381 -0.04761905 0.23809524],
[-0.07142857 0.02380952 -0.16666667],
[ 0.21428571 -0.19047619 0.11904762]]
via built-in numpy inverse function:
[[-1.21428571 1.14285714 0.35714286]
[ 1.66666667 -1.66666667 -0.33333333]
[ 0.0952381 0.04761905 -0.04761905]]
As you can see some of the numbers match and I'm just not sure why the answer isn't exact as I'm using the formula correctly.

You co-factor matrix calculation isn't correct.
def inverse(M):
d = np.linalg.det(M)
cf_mat = []
for i in range(M.shape[0]):
for j in range(M.shape[1]):
# for each position we need to calculate det
# of submatrix without current row and column
# and multiply it on position coefficient
coef = (-1) ** (i + j)
new_mat = []
for i1 in range(M.shape[0]):
for j1 in range(M.shape[1]):
if i1 != i and j1 != j:
new_mat.append(M[i1, j1])
new_mat = np.array(new_mat).reshape(
(M.shape[0] - 1, M.shape[1] - 1))
new_mat_det = np.linalg.det(new_mat)
cf_mat.append(new_mat_det * coef)
cf_mat = np.array(cf_mat).reshape(M.shape)
adj = np.matrix.transpose(cf_mat)
inv = (1 / d) * adj
return inv
This code isn't very effective, but here you can see, how it should be calculated. More information and clear formula you can find at Wiki.
Output matrix:
[[-1.21428571 1.14285714 0.35714286]
[ 1.66666667 -1.66666667 -0.33333333]
[ 0.0952381 0.04761905 -0.04761905]]

Related

Make an array of matrices in Python (numpy)

I'm trying to create an undefined length array of matrices for a neural network, but, when i append the second matrix to the array, the format is messed up.
def createRandomWeights(X):
initialW = generateWeights(X.shape[1], S[0]) # first weight matrix
w = np.array([initialW]) # array of weight matrices
for i in range(0, L - 1):
layerW = np.random.uniform(-1, 1, (S[i], S[i + 1]))
w = np.append(w, [layerW])
return w
The function generateWeights only creates an NxM size np.matrix of random numbers between -1 and 1.
S is an array of numbers
L is the lenght of S
Example:
S = [2,3]
L = len(s)
X = [[1,1,1],[1,-1,1],[-1,1,1],[-1,-1,1]]
Expected output example (random numbers wrote as 'rn'):
matrix1 = [[rn, rn],[rn, rn],[rn, rn]] # 3x2 matrix
matrix2 = [[rn, rn, rn],[rn, rn, rn]] # 2x3 matrix
output = [matrix1, matrix2] # 2 matrix elements array
Real output:
output = [rn, rn, rn, rn, rn...] #12 times
The problem is that you are using np.append instead of using the append method for list in Python.
def createRandomWeights(X):
initialW = generateWeights(X.shape[1], S[0]) # first weight matrix
w = np.array([initialW]) # array of weight matrices
for i in range(0, L - 1):
layerW = np.random.uniform(-1, 1, (S[i], S[i + 1]))
w.append(layerW)
return w
The code above should do the job. If you check the docs on np.append you will see that it will turn the arguments into a 1 dimensional array if no other params are specified.

Find values for which matrix becomes singular in Python

Let's take the following square matrix:
import numpy as np
A = np.array([[10.0, -498.0],
[-2.0, 100.0]])
A will be singular if its determinant (A[0,0]*A[1,1]-A[0,1]*A[1,0]) is zero. For example, A will be singular if A[0,1] takes the value -500.0 (all else unchanged):
from sympy import symbols, Eq, solve
y = symbols('y')
eq = Eq(A[0,0]*A[1,1]-y*A[1,0])
sol = solve(eq)
sol
How to find all values (A[0,0],A[0,1],...) for which A (or any given square matrix) becomes singular efficiently (I work with large matrices)? Many thanks in advance.
The trick is to use Laplace expansion to calculate the determinant. The formula is
det(A) = sum (-1)^(i+j) * a_ij * M_ij
So to make a matrix singular, you just need to use the above formula, change the subject to a_ij and set det(A) = 0. It can be done like this:
import numpy as np
def cofactor(A, i, j):
A = np.delete(A, (i), axis=0)
A = np.delete(A, (j), axis=1)
return (-1)**(i+j) * np.linalg.det(A)
def make_singular(A, I, J):
n = A.shape[0]
s = 0
for i in range(n):
if i != J:
s += A[I, i] * cofactor(A, I, i)
M = cofactor(A, I, J)
if M == 0:
return 'No solution'
else:
return -s / M
Testing:
>>> M = np.array([[10.0, -498.0],
[-2.0, 100.0]])
>>> make_singular(M, 0, 1)
-500.0000000000002
>>> M = np.array([[10.0, -498.0],
[0, 100.0]])
>>> make_singular(M, 0, 1)
'No solution'
This thing works for square matrices...
What it does is it bruteforces through every item in the matrix and check if its singular, (so theres a lot of messy output, ue it if you like it tho)
And also very important, it is a Recursive function that returns a matrix if it is singular. So it throws RecursiveError recursively....:|
This is the code i have come up with, you can use it if its okay for you
import numpy as np
def is_singular(_temp_int:str, matrix_size:int):
kwargs = [int(i) for i in _temp_int]
arr = [] # Creates the matrix from the given size
temp_count = 0
for i in range(matrix_size):
arr.append([])
m = arr[i]
for j in range(matrix_size):
m.append(int(_temp_int[temp_count]))
temp_count += 1
n_array = np.array(arr)
if int(np.linalg.det(n_array)) == 0:
print(n_array) # print(n_array) for a pretty output or print(arr) for single line output of the determinant matrix
_temp_int = str(_temp_int[:-len(str(int(_temp_int)+1))] + str(int(_temp_int)+1))
is_singular(_temp_int, matrix_size)
# Only square matrices, so only one-digit integer as input
print("List of singular matrices in the size of '3x3': ")
is_singular('112278011', 3)
# Just give a temporary integer string which will be converted to matrix like [[1, 1, 2], [2, 7, 8], [0, 1, 1]]
# From the provided integer string, it adds up 1 after every iteration
I think this is the code you want, let me know if its not working

Heuristic algorithm in subarrays

ORIGINAL PROBLEM:
Given a set A={a1, . . . , an} and the matrix D of distances between the elements of A, we want to select the subset A*⊂ A of cardinal p with minimum diameter δ(A∗
) with δ(A∗) =max{d(a, a0) : a, a0 ∈ A∗}.
Write a python code that solve heuristically the particular case of n=8, p=4.
WHAT I HAVE UNDERSTOOD:
Given a matrix mxn (in this case 8x8) I am trying to look through a heuristic algorithm the max value of each sub-array of size 4x4, and store these values in a final matrix
For example:
Given the C matrix of euclidean distances 8x8:
What is the max value of a each possible sub-array 4x4?
and then store this max value in the final matrix mxn..
I have tried this but only returns one max value in the matrix.
# Python 3 Program to find the maximum
# value in a matrix which contain
# intersecting concentric submatrix
MAXN = 64
# Return the maximum value in intersecting
# concentric submatrix.
def maxValue( n, m, x, y, a):
c = [[0 for x in range(MAXN)]
for y in range(MAXN)]
# For each center of concentric sub-matrix.
for i in range( m):
# for each row
for p in range(n) :
# for each column
for q in range( n) :
# finding x distance.
dx = abs(p - x[i])
# finding y distance.
dy = abs(q - y[i])
# maximum of x distance and y distance
d = max(dx, dy)
# assigning the value.
c[p][q] += max(0, a[i] - d)
# Finding the maximum value in
# the formed matrix.
res = 0
for i in range(n) :
for j in range(n) :
res = max(res, c[i][j])
return res
# Driver Code
if __name__ == "__main__":
n = 10
m = 2
x = [ 3, 7 ]
y = [ 3, 7 ]
a = [ 4, 3 ]
print(maxValue(n, m, x, y, a))

Mahalanobis distance returns NaN in Pytorch

I'm testing new metrics to measure distance between weight matrices in Pytorch, right now I'm trying to use Mahalanobis. For that I reshape every matrix into a vector and concat then into one matrix and then use this matrix to calculate the mahalanobis distance between any two rows of this matrix. Problem is some of those are getting me negative results and the square root of a negative throws me NaN.
I know the covariance matrix has to be Positive Defined, I guess I'm messing up there, or maybe my ideia of using mahalanobis in this case is not possible?
Here's the code I'm using, where I'm passing to it a X with shape (64,121), that being 64 (11x11) matrices
def _mahalanobis(X):
VI = torch.inverse(cov(X)) #covariance matrix
total_dist = 0
for i,v in enumerate(X):
dist = 0
for j,u in enumerate(X):
if i == j:
continue
x = (v-u).unsqueeze(0).t()
y = (v - u).unsqueeze(0)
dist = torch.sqrt(torch.mm(torch.mm(y,VI),x))
total_dist +=dist
print(dist)
return total_dist
## Returns the covariance matrix of m
def cov(m, rowvar=False):
if m.dim() > 2:
raise ValueError('m has more than 2 dimensions')
if m.dim() < 2:
m = m.view(1, -1)
if not rowvar and m.size(0) != 1:
m = m.t()
# m = m.type(torch.double) # uncomment this line if desired
fact = 1.0 / (m.size(1) - 1)
m -= torch.mean(m, dim=1, keepdim=True)
mt = m.t() # if complex: mt = m.t().conj()
return fact * m.matmul(mt).squeeze()

Getting the adjugate of matrix in python

i having some problems in solving the question finding the adjugate of a matrix by given the formula of cofactor matrix
c[i][j] = (-1)**(i+j)*m[i][j]
where m stand for determinant of matrix.
x = np.array([[1,3,5],[-2,-4,-5],[3,6,1]] , dtype = 'int')
i only able to do this and don't know how to continue , please help
to find the cofactor i have this hint
def COF(C)
create an empty matrix CO
for row
for col
sel_rows = all rows except current row
sel_columns = all cols except current col
MATij = [selected rows and selected columns]
compute COij
return CO
import numpy as np
x = np.array([[1,3,5],[-2,-4,-5],[3,6,1]] , dtype = 'int')
m = np.linalg.det(x)
c =[[i for i in range(3)] for j in range(3)]
for i in range(3):
for j in range(3):
c[i][j] = (-1)*(i+j)*m
The c.T to work without any errors, the array c should be a numpy array. Here the array c that #TaohidulIslam declared is a Python List. So you are getting an error.
Declare c as follows:
c =np.array([[i for i in range(3)] for j in range(3)])
You can calculate the adjugate matrix by the transposal of the cofactor matrix
with the method below which is suitable for non singular matrices.
First, find the cofactor matrix, as follows:
https://www.geeksforgeeks.org/how-to-find-cofactor-of-a-matrix-using-numpy/
Then, find the transposal of the cofactor matrix.
import numpy as np
import math as mth
# get cofactors matrix
def getcofat(x):
eps = 1e-6
detx = np.linalg.det(x)
if (mth.fabs(detx) < eps):
print("No possible to get cofactors for singular matrix with this method")
x = None
return x
invx = np.linalg.pinv(x)
invxT = invx.T
x = invxT * detx
return x
# get adj matrix
def getadj(x):
eps = 1e-6
detx = np.linalg.det(x)
if (mth.fabs(detx) < eps):
print("No possible to get adj matrix for singular matrix with this method")
adjx = None
return adjx
cofatx = getcofat(x)
adjx = cofatx.T
return adjx
A = np.array([[1, 3, 5], [-2, -4, -5], [3, 6, 1]])
print(A)
print(np.linalg.det(A))
Acofat = getcofat(A)
print(Acofat)
Aadj = getadj(A)
print(Aadj)

Categories