简体   繁体   English

解决Matlab中的稀疏三角线性系统

[英]Solve the sparse triangular linear system in Matlab

I implement the LU decomposition algorithm in Matlab for some large sparse Matrices to solve the linear system. 我在Matlab中为一些大型稀疏矩阵实现了LU分解算法,以解决线性系统。 When I got the L,U matrix, I used the backward substitution and forward substitution algorithm to solve the triangular linear system: 当我得到L,U矩阵时,我使用了向后替换和正向替换算法来求解三角线性系统:

    %x = U\y;

for i = n : -1 : 1
    x(i,:) = (y(i,:)-U(i,:)*x)/U(i,i);
end

but I found this code is the bottleneck. 但是我发现这段代码是瓶颈。 Although I can use the A\\b to get the solution, but I want to know how can I implement a efficient algorithm to solve this problem in Matlab, For example, Can I write the matrix product to simulate the following action without for loop? 尽管我可以使用A \\ b来获得解决方案,但是我想知道如何在Matlab中实现一种有效的算法来解决此问题,例如,我可以编写矩阵乘积来模拟以下动作而无需for循环吗?

(I got some reference books and paper, but all of the code is not in Matlab, just for C++ or C code) (我有一些参考书和论文,但是所有代码都不在Matlab中,仅适用于C ++或C代码)

First off: correctness goes before speed ; 首先, 正确性要高于速度 the loop you posted produces results different from U\\y , so you might want to check that first :) 您发布的循环产生的结果不同于 U\\y ,因此您可能要先检查一下:)

AFAIK, the backslash does some checks on the input matrix, and calls the fastest algorithm accordingly. AFAIK,反斜杠会对输入矩阵进行一些检查,并相应地调用最快的算法。 When those checks indicate A is lower triangular, it will do exactly what you did (but then probably more efficient). 当这些检查表明A为较低的三角形时,它将完全按照您的操作进行操作(但可能会更有效)。

Anyway, to speed up your code: you should pre-allocate x , otherwise Matlab is forced to grow the vector at each iteration. 无论如何,为了加快代码速度:您应该预先分配x ,否则Matlab将在每次迭代时强制增大向量。 Also, call your loop variable ii -- i is the imaginary unit, and the name resolution at each iteration takes some time. 另外,将循环变量ii称为i是虚数单位,每次迭代的名称解析都需要一些时间。 So, in summary: 因此,总而言之:

x = zeros(size(y));
for ii = n : -1 : 1
    x(ii,:) = (y(ii,:)-U(ii,:)*x)/U(ii,ii);
end

Note that there is no 'vectorized' solution, as the next result depends on the previous one. 请注意,没有“矢量化”解决方案,因为下一个结果取决于上一个结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM