寻找迭代 Schultz 方法

huangapple go评论51阅读模式
英文:

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.

For your test case....寻找迭代 Schultz 方法

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.寻找迭代 Schultz 方法

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.

For your test case....寻找迭代 Schultz 方法

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.寻找迭代 Schultz 方法

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)

huangapple
  • 本文由 发表于 2023年4月11日 16:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983923.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定