import numpy as np
def is_diagonally_dominant(A):
"""
Check if the matrix A is strictly diagonally dominant.
Parameters:
A : numpy array
Coefficient matrix
Returns:
bool
True if A is strictly diagonally dominant, False otherwise
"""
n = A.shape[0]
for i in range(n):
row_sum = np.sum(np.abs(A[i, :])) - np.abs(A[i, i])
if np.abs(A[i, i]) <= row_sum:
return False
return True
2. 判断谱半径条件
谱半径条件:对于矩阵 A 的迭代矩阵 M,如果其谱半径 ho(M) 小于 1,则迭代法收敛。这里的谱半径定义为矩阵的特征值的最大绝对值。
def spectral_radius(A):
"""
Compute the spectral radius of the matrix A.
Parameters:
A : numpy array
Coefficient matrix
Returns:
float
Spectral radius of A
"""
eigenvalues = np.linalg.eigvals(A)
spectral_radius = np.max(np.abs(eigenvalues))
return spectral_radius
示例用法
现在我们可以使用这些函数来判断一个给定的系数矩阵是否满足迭代法的收敛性条件。例如:
# Example usage
A = np.array([[4, -1, 0, 0],
[-1, 4, -1, 0],
[0, -1, 4, -1],
[0, 0, -1, 3]])
# Check if A is strictly diagonally dominant
if is_diagonally_dominant(A):
print("Matrix A is strictly diagonally dominant. Jacobi and Gauss-Seidel methods will converge.")
# Compute the spectral radius of A
rho = spectral_radius(A)
if rho < 1:
print(f"Spectral radius of A is {rho}, which is less than 1. Iterative methods are likely to converge.")
else:
print(f"Spectral radius of A is {rho}, which is greater than or equal to 1. Iterative methods may not co