matlab数据方块里面反斜杠,如何實現Matlab的mlb(即反斜杠運算符“\”)

33

For x = A\b, the backslash operator encompasses a number of algorithms to handle different kinds of input matrices. So the matrix A is diagnosed and an execution path is selected according to its characteristics.

對於x = A\b,反斜杠運算符包含許多算法來處理不同類型的輸入矩陣。因此矩陣A被診斷出來,根據其特征選擇執行路徑。

The following page describes in pseudo-code when A is a full matrix:

當A是一個完整的矩陣時,下面的頁面用偽代碼描述:

if size(A,1) == size(A,2) % A is square

if isequal(A,tril(A)) % A is lower triangular

x = A \ b; % This is a simple forward substitution on b

elseif isequal(A,triu(A)) % A is upper triangular

x = A \ b; % This is a simple backward substitution on b

else

if isequal(A,A') % A is symmetric

[R,p] = chol(A);

if (p == 0) % A is symmetric positive definite

x = R \ (R' \ b); % a forward and a backward substitution

return

end

end

[L,U,P] = lu(A); % general, square A

x = U \ (L \ (P*b)); % a forward and a backward substitution

end

else % A is rectangular

[Q,R] = qr(A);

x = R \ (Q' * b);

end

For non-square matrices, QR decomposition is used. For square triangular matrices, it performs a simple forward/backward substitution. For square symmetric positive-definite matrices, Cholesky decomposition is used. Otherwise LU decomposition is used for general square matrices.

對於非方陣,使用QR分解。對於正方形三角形矩陣,它執行一個簡單的前向/向后替換。對於正方形對稱正定矩陣,使用Cholesky分解。對於一般的方陣,則采用LU分解。

Update: MathWorks has updated the algorithm section in the doc page of mldivide with some nice flow charts. See here and here (full and sparse cases).

更新:MathWorks已經在mlb的doc頁面中更新了算法部分,並提供了一些不錯的流程圖。看這里和這里(完整和稀疏的情況)。

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9rVnN5ai5wbmc=

All of these algorithms have corresponding methods in LAPACK, and in fact it's probably what MATLAB is doing (note that recent versions of MATLAB ship with the optimized Intel MKL implementation).

所有這些算法在LAPACK中都有相應的方法,實際上,這可能是MATLAB所做的(注意,最近的MATLAB版本的ship使用了優化的Intel MKL實現)。

The reason for having different methods is that it tries to use the most specific algorithm to solve the system of equations that takes advantage of all the characteristics of the coefficient matrix (either because it would be faster or more numerically stable). So you could certainly use a general solver, but it wont be the most efficient.

使用不同方法的原因是,它嘗試使用最具體的算法來解決那些利用系數矩陣的所有特征的方程組(要么是因為它更快,要么更穩定)。所以你當然可以使用一個通用求解器,但它不會是最有效的。

In fact if you know what A is like beforehand, you could skip the extra testing process by calling linsolve and specifying the options directly.

事實上,如果您事先知道A是什么樣子,您可以通過調用linsolve和直接指定選項來跳過額外的測試過程。

if A is rectangular or singular, you could also use PINV to find a minimal norm least-squares solution (implemented using SVD decomposition):

如果A是矩形的或者是單數的,你也可以使用PINV來找到最小范數最小二乘解(用SVD分解實現):

x = pinv(A)*b

All of the above applies to dense matrices, sparse matrices are a whole different story. Usually iterative solvers are used in such cases. I believe MATLAB uses UMFPACK and other related libraries from the SuiteSpase package for direct solvers.

以上這些都適用於稠密矩陣,稀疏矩陣是一個完全不同的故事。通常在這種情況下使用迭代求解器。我相信MATLAB使用UMFPACK和其他相關的庫來直接求解。

When working with sparse matrices, you can turn on diagnostic information and see the tests performed and algorithms chosen using spparms:

當使用稀疏矩陣時,您可以打開診斷信息,並查看所執行的測試和使用spparms選擇的算法:

spparms('spumoni',2)

x = A\b;

What's more, the backslash operator also works on gpuArray's, in which case it relies on cuBLAS and MAGMA to execute on the GPU.

更重要的是,反斜杠運算符也適用於gpuArray,在這種情況下,它依賴於cuBLAS和岩漿在GPU上執行。

It is also implemented for distributed arrays which works in a distributed computing environment (work divided among a cluster of computers where each worker has only part of the array, possibly where the entire matrix cannot be stored in memory all at once). The underlying implementation is using ScaLAPACK.

它也適用於分布式計算環境(在一個計算機集群中,每個工作人員只有一個數組的一部分,可能整個矩陣不能同時存儲在內存中)。底層實現是使用ScaLAPACK。

That's a pretty tall order if you want to implement all of that yourself :)

如果你想自己實現所有這些,這是一個非常艱巨的任務

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值