You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中可求逆的上三角矩阵最大尺寸问题求助

上三角方阵求逆的数值稳定性问题

我需要在Python中对一个401x401的上三角方阵求逆,但遇到了问题:当矩阵尺寸小于等于153x153时,计算结果完全正常;但尺寸超过这个值后,逆矩阵与原矩阵的乘积不再是单位矩阵。

以下是我的代码:

import numpy as np
import math
from scipy import special

N=1
check = np.identity(N)

def calculate_matrix(N):
    matrix = np.zeros( (N,N) ) 
    matrix[0]=[1/(2**(j))*special.binom(j,j/2) if j%2==0 else 0 for j in range(N)]
    for p in range(0,math.floor((N-2)/2)+1):
        i=2*p+1
        matrix[i]=[(-1)**(2*q-p)/(4**q)*special.binom(2*q+1,(q-p)) if (q-i>=0 and q%2==1) else 0 for q in range(N)]
    for p in range(1,math.floor((N-1)/2)+1):
        i=2*p
        matrix[i]=[(-1)**(2*q-p)/(2**(2*q-1))*special.binom(2*q,(q-p)) if (q-i>=0 and q%2==0) else 0 for q in range(N)]
    return matrix

while (np.allclose(check, np.identity(N))==True and N<=402):
    N=N+1
    matrix = calculate_matrix(N)
    inverse_matrix = np.linalg.inv(matrix)
    check=np.matmul(inverse_matrix, matrix)

print("Maximum size of the matrix that can be inverted:", N-1)

经提醒,这个矩阵是病态矩阵,这应该是导致问题的核心原因。想请教有没有可行的解决方法,目前尝试过部分选主元法,但没有效果。


内容的提问来源于stack exchange,提问作者Jep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 10:12:40