Find values for which matrix becomes singular in Python

Viewed 275

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.

2 Answers

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

Related