英文:
finding the iterative Schultz method
问题
Sure, here's the translated portion of your text:
请帮助我解决使用迭代Schultz方法找到问题的方法。出现溢出,但我不知道如何修复它。
给出了错误消息
RuntimeWarning: overflow encountered in 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) # 矩阵数据类型转换
n = A.shape[0]
X = np.eye(n) # 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("在最大迭代次数之后,该方法未收敛")
# 用法示例
A = np.array([[4, 1], [2, 3]])
A_inv = schultz_inverse(A)
print(A_inv)
我尝试将float64更改为float32,减少迭代次数并更改错误,但没有帮助。
在退出时,要么是[[-inf -inf] [-inf -inf]],要么是一个错误。
英文:
Please help me solve problems with finding the iterative Schultz method. Overflow occurs, but I don't know how to fix it
Gives an error message
RuntimeWarning: overflow encountered in 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)
I tried to change float64 to float32 and set fewer iterations and change the error, but it didn't help
at the exit or
[[-inf -inf]
[-inf -inf]]
either an error
答案1
得分: 1
I think the problem is not the implementation.
It should be the init of X.
For case A=I, X=I, 2X-XAX = I. This is the simplest case that you code works.
You can see it diverges... quite fast tbh (faster than I expected).
Therefore, I did an effortless change. Which is start with a smaller X.
For something a little better... I suggest you init X as I/det(A)...
Which is np.eye(n)/np.linalg.det(A)
;
Or even... as np.eye(n)/np.sum(A)
英文:
I think the problem is not the implementation.
It should be the init of X.
For case A=I, X=I, 2X-XAX = I. This is the simplest case that you code works.
You can see it diverges... quite fast tbh (faster than I expected).
Therefore, I did an effortless change. Which is start with a smaller X.
For something a little better... I suggest you init X as I/det(A)...
Which is np.eye(n)/np.linalg.det(A)
;
Or even... as np.eye(n)/np.sum(A)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论