python内置函数进行矩阵约简
python 是否有一个内置函数可以将矩阵转换为行梯形形式(也称为上三角矩阵)?
Does python have a built-in function that converts a matrix into row echelon form (also known as upper triangular)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您可以使用
sympy
,Matrix.rref()
可以做到:If you can use
sympy
,Matrix.rref()
can do it:是的。在 scipy.linalg 中,lu 进行 LU 分解,这本质上会得到行梯形形式。
如果您感兴趣,还有其他分解,例如 qr、rq、svd 等。
文档。
Yes. In
scipy.linalg
,lu
does LU decomposition which will essentially get you row-echelon form.There are other factorizations such as
qr
,rq
,svd
, and more, if you're interested.Documentation.
参见http://mail.scipy.org/pipermail/numpy- Discussion/2008-November/038705.html
基本上:不要这样做。
rref 算法在计算机上实现时会产生太多不准确性。因此,您要么想以另一种方式解决问题,要么使用 @aix 建议的符号。
see http://mail.scipy.org/pipermail/numpy-discussion/2008-November/038705.html
Basically: don't do it.
The rref algorithm produces too much inaccuracy when implemented on a computer. So you either want to solve the problem in another way, or use symbolics like @aix suggested.
我同意 @Mile 的评论 @WinstonEwert 的回答 计算机没有理由不能以给定的精度执行 RREF 。
RREF的实现应该不会很复杂,而且matlab以某种方式设法拥有这个功能,所以numpy也应该有。
我做了一个非常简单直接的实现,效率很低。但对于简单的矩阵,它工作得很好:
更新:
似乎,@JustMe 在这个
rref
实现中发现了一个错误,如果输入矩阵的第一列为零,则会出现该错误。所以这里有一个更新版本。这是一些简单的测试
I agree with a @Mile comment to @WinstonEwert answer There's no reason a computer could not perform RREF with given precision.
The realization of RREF shouldn't be very complicated, and matlab somehow managed to have this function, so numpy should have too.
I did a very simple and straightforward realization, which is very inefficient. But for simple matrices it works pretty well:
UPDATE:
Seems, @JustMe spotted a bug in this
rref
realization, which appeared if an input matrix has the first column of zeros. So here an updated version.Here are some simple tests
你可以自己定义它:
You can define it yourself:
这是一个工作版本,它几乎只是 MATLAB rref 函数的 numpy 版本:
示例:
Here's a working version that's pretty much just a numpy version of MATLAB's rref function:
Example:
对于任何矩阵(具有满秩或不足),您可以循环遍历行(或列,这是等效的)并查看哪一行对排名有贡献。
选择前 n 行,您将得到一个最多具有 n 阶的矩阵。
For any matrix (having full rank or deficiency), you can loop through the rows (or columns, this is equivalent) and see which row contributes to rank.
Selecting the first n rows, you get a matrix having at most rank n.