2

让我们采用以下方阵:

import numpy as np
A = np.array([[10.0, -498.0],
             [-2.0, 100.0]])

如果 A 的行列式 (A[0,0]*A[1,1]-A[0,1]*A[1,0]) 为零,则 A 将是奇异的。例如,如果 A[0,1] 取值 -500.0(其他所有内容不变),则 A 将是单数:

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

如何找到所有值(A[0,0],A[0,1],...),其中 A(或任何给定的方阵)有效地变为奇异(我使用大型矩阵)?提前谢谢了。

4

2 回答 2

4

诀窍是使用拉普拉斯展开来计算行列式。公式是

det(A) = sum (-1)^(i+j) * a_ij * M_ij

所以要使矩阵奇异,你只需要使用上面的公式,将主题更改为 a_ij 并设置 det(A) = 0。可以这样做:

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

测试:

>>> 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'
于 2021-08-19T08:04:36.843 回答
0

这东西适用于方阵......

它的作用是对矩阵中的每个项目进行暴力破解并检查其是否为单数,(所以有很多混乱的输出,如果你喜欢它,可以使用它)

同样非常重要的是,它是一个递归函数,如果它是奇异的,则返回一个矩阵。所以它递归地抛出 RecursiveError ....:|

这是我想出的代码,你可以使用它,如果它适合你

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

我认为这是您想要的代码,如果它不起作用,请告诉我

于 2021-08-19T08:13:15.443 回答