如何在Python中实现部分旋转的LU分解?

时间:2022-09-13 15:35:21

I want to implement my own LU decomposition P,L,U = my_lu(A), so that given a matrix A, computes the LU decomposition with partial pivoting. But I only know how to do it without pivoting. Can anyone help to do the partial pivoting?

我想实现我自己的LU分解P,L,U = my_lu(A),所以给定一个矩阵A,用部分枢轴计算LU分解。但我只知道怎么做,没有旋转。有人能帮忙做部分旋转吗?

def lu(A):

    import numpy as np

    # Return an error if matrix is not square
    if not A.shape[0]==A.shape[1]:
        raise ValueError("Input matrix must be square")

    n = A.shape[0] 

    L = np.zeros((n,n),dtype='float64') 
    U = np.zeros((n,n),dtype='float64') 
    U[:] = A 
    np.fill_diagonal(L,1) # fill the diagonal of L with 1

    for i in range(n-1):
        for j in range(i+1,n):
            L[j,i] = U[j,i]/U[i,i]
            U[j,i:] = U[j,i:]-L[j,i]*U[i,i:]
            U[j,i] = 0
    return (L,U)

1 个解决方案

#1


-1  

You can use Scipy's scipy.linalg.lu for this.

你可以用Scipy的剪刀。linalg。陆。

http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.linalg.lu.html

http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.linalg.lu.html

Please check this link also:

请同时查看此链接:

http://www.quantstart.com/articles/LU-Decomposition-in-Python-and-NumPy

http://www.quantstart.com/articles/LU-Decomposition-in-Python-and-NumPy

#1


-1  

You can use Scipy's scipy.linalg.lu for this.

你可以用Scipy的剪刀。linalg。陆。

http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.linalg.lu.html

http://docs.scipy.org/doc/scipy-0.13.0/reference/generated/scipy.linalg.lu.html

Please check this link also:

请同时查看此链接:

http://www.quantstart.com/articles/LU-Decomposition-in-Python-and-NumPy

http://www.quantstart.com/articles/LU-Decomposition-in-Python-and-NumPy