请帮我解决寻找迭代 Schultz 方法的问题。发生溢出,但我不知道如何解决
给出错误信息 RuntimeWarning:matmul 遇到溢出 B=2*X-X@A@X
import numpy as np
def schultz_inverse(A, tolerance=1e-6, max_iterations=100):
A = A.astype(float) # matrix data type conversion
n = A.shape[0]
X = np.eye(n) # initial approximation for X
for i in range(max_iterations):
B = 2 * X - X @ A @ X
if np.allclose(B, X, rtol=tolerance):
return B
X = B
raise Exception("The method did not converge after the maximum number of iterations")
# Usage example
A = np.array([['4', '1'], ['2', '3']])
A_inv = schultz_inverse(A)
print(A_inv)
我试过把float64改成float32,设置更少的迭代次数,改了错误,但是没用
在出口或 [[-信息-信息] [-inf -inf]] 要么是错误