Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python numpy.linalg.solve和numpy.linalg.lu_solve之间的差异_Python_Numpy - Fatal编程技术网

Python numpy.linalg.solve和numpy.linalg.lu_solve之间的差异

Python numpy.linalg.solve和numpy.linalg.lu_solve之间的差异,python,numpy,Python,Numpy,要求解线性矩阵方程,可以使用numpy.linalg.solve 它实现了LAPACK例程 根据文件 DGESV computes the solution to a real system of linear equations A * X = B, where A is an N-by-N matrix and X and B are N-by-NRHS matrices. The LU decomposition with partial pivoting and row i

要求解线性矩阵方程,可以使用
numpy.linalg.solve
它实现了LAPACK例程

根据文件

DGESV computes the solution to a real system of linear equations
    A * X = B,
 where A is an N-by-N matrix and X and B are N-by-NRHS matrices.

 The LU decomposition with partial pivoting and row interchanges is
 used to factor A as
    A = P * L * U,
 where P is a permutation matrix, L is unit lower triangular, and U is
 upper triangular.  The factored form of A is then used to solve the
 system of equations A * X = B.
但是,我们也可以使用
scipy.linalg.lu\u factor()
scipy.linalg.lu\u solve()
为了解决我们的问题,其中
lu\u factor()

Compute pivoted LU decomposition of a matrix.

The decomposition is:

A = P L U

where P is a permutation matrix, 
L lower triangular with unit diagonal elements, 
and U upper triangular.

所以这两种方法显然是多余的。这种冗余有什么意义吗?对我来说似乎很困惑。

事实上你是对的:链接scipy的
scipy.linalg.lu\u factor()
scipy.linalg.lu\u solve()
完全等同于numpy的
numpy.linalg.solve(),在实际情况下,访问LU分解是一个很大的优势。

首先,让我们证明等价性。声明:

使用LAPACK例程_gesv计算解

事实上,包含LAPACK的精简版本。 然后,让我们来看看LAPACK的来源。计算矩阵的LU分解并用于求解线性系统。实际上,该函数的源代码非常清楚:对于输入检查,它可以归结为调用
dgetrf
(LU分解)和
dgetrs
。最后,分别包装
dgetrf
dgetrs
,包括
getrf、=get_-lapack_函数(('getrf',)、(a1,)
getrs、=get_-lapack_函数('getrs',)、(lu,b1))

正如@Alexander Reynolds所注意到的,LU分解可用于计算矩阵的行列式和秩。事实上,关于行列式,
numpy.det
调用LU分解\u getrf
查看。然而,numpy使用SVD分解计算矩阵的秩

但是,使用LU计算秩并不是将接口公开给
dgetrf
dgetrs
的唯一原因。确实,在一些常见情况下,一次校准
dgetrf
,在内存中保留LU分解并多次调用
dgetrs
是一个决定性的优势。例如,看看。必须注意,计算LU分解比使用分解(N^2)求解线性系统花费更多的时间(N^3)。

让我们看一看求解耦合非线性方程组的F(x)=0,其中F:R^N->R^N。执行牛顿-拉斐逊迭代需要求解一个线性系统,其中矩阵是雅可比矩阵J:


其中x{i+1}是未知的。雅可比矩阵J(x_i)的计算通常是昂贵的,而不是对系统必须求解的事实进行量纲化。因此,通常考虑拟牛顿法,其中建立了雅可比矩阵的近似值。一个简单的想法是,不要每次迭代都更新雅可比矩阵,只要剩余范数减少,就保持迭代在这样的过程中,
dgetrf
会不时被调用,而
dgetrs
会在每次迭代中被调用一次。
有关方案,请参阅。让我们看一个例子:让我们试着从x_0=2开始解x^2=1。对于牛顿法的4次迭代,我们得到f(x_4)=9.2e-8和f(x_5)这是如何冗余的?并非每个LU分解都会立即进入解算器。类似地,如果你在使用LU矩阵,那么你有一个解算器可以用于当前的矩阵形式。如果不是为了解am方程组,为什么你要使用LU分解?你可以从LU分解中获得其他信息---行列式,秩,等等。但我看到它主要用于另一个方向——创建LU矩阵,然后用它解决系统。对于这些信息
numpy。linalg
提供了适当的函数。