左矩阵除法和 Numpy 求解

发布于 2024-12-01 02:50:21 字数 669 浏览 0 评论 0 原文

我正在尝试将包含 \ 运算符的代码从 Matlab (Octave) 转换为 Python。示例代码

B = [2;4]
b = [4;4]
B \ b

这有效并产生 1.2 作为答案。使用此网页

http://mathesaurus.sourceforge.net/matlab-numpy.html

我将其翻译为:

import numpy as np
import numpy.linalg as lin
B = np.array([[2],[4]])
b = np.array([[4],[4]])
print lin.solve(B,b)

这给了我一个错误:

numpy.linalg.linalg.LinAlgError: Array must be square

Matlab \ 如何与 B 的非方阵一起工作?

有什么解决办法吗?

I am trying to convert code that contains the \ operator from Matlab (Octave) to Python. Sample code

B = [2;4]
b = [4;4]
B \ b

This works and produces 1.2 as an answer. Using this web page

http://mathesaurus.sourceforge.net/matlab-numpy.html

I translated that as:

import numpy as np
import numpy.linalg as lin
B = np.array([[2],[4]])
b = np.array([[4],[4]])
print lin.solve(B,b)

This gave me an error:

numpy.linalg.linalg.LinAlgError: Array must be square

How come Matlab \ works with non square matrix for B?

Any solutions for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无畏 2024-12-08 02:50:21

来自左矩阵除法的 MathWorks 文档

如果 A 是一个 m×n 矩阵,其中 m ~= n,B 是一个列向量,其中 m
分量,或具有多个此类列的矩阵,则 X = A\B 是
欠定或超定的最小二乘解
方程组 AX = B。换句话说,X 最小化范数(A*X - B),
向量 AX - B 的长度。

。numpy 中的等效项是 np.linalg.lstsq

In [15]: B = np.array([[2],[4]])

In [16]: b = np.array([[4],[4]])

In [18]: x,resid,rank,s = np.linalg.lstsq(B,b)

In [19]: x
Out[19]: array([[ 1.2]])

From MathWorks documentation for left matrix division:

If A is an m-by-n matrix with m ~= n and B is a column vector with m
components, or a matrix with several such columns, then X = A\B is the
solution in the least squares sense to the under- or overdetermined
system of equations AX = B. In other words, X minimizes norm(A*X - B),
the length of the vector AX - B.

The equivalent in numpy is np.linalg.lstsq:

In [15]: B = np.array([[2],[4]])

In [16]: b = np.array([[4],[4]])

In [18]: x,resid,rank,s = np.linalg.lstsq(B,b)

In [19]: x
Out[19]: array([[ 1.2]])
瑕疵 2024-12-08 02:50:21

当使用 \ 运算符时,Matlab 实际上会执行许多不同的操作,具体取决于所涉及矩阵的形状(请参阅

import numpy as np
import numpy.linalg as lin
B = np.array([[2],[4]])
b = np.array([[4],[4]])
print np.linalg.lstsq(B,b)[0]

这应该为您提供与 Matlab 相同的解决方案。

Matlab will actually do a number of different operations when the \ operator is used, depending on the shape of the matrices involved (see here for more details). In you example, Matlab is returning a least squares solution, rather than solving the linear equation directly, as would happen with a square matrix. To get the same behaviour in numpy, do this:

import numpy as np
import numpy.linalg as lin
B = np.array([[2],[4]])
b = np.array([[4],[4]])
print np.linalg.lstsq(B,b)[0]

which should give you the same solution as Matlab.

情绪操控生活 2024-12-08 02:50:21

您可以形成左逆:

import numpy as np
import numpy.linalg as lin
B = np.array([[2],[4]])
b = np.array([[4],[4]])

B_linv = lin.solve(B.T.dot(B), B.T)
c = B_linv.dot(b)
print('c\n', c)

结果:

c
 [[ 1.2]]

实际上,我们可以简单地运行求解器一次,而不形成逆,如下所示:

c = lin.solve(B.T.dot(B), B.T.dot(b))
print('c\n', c)

结果:

c
 [[ 1.2]]

...像以前一样

为什么?因为:

我们有:

在此处输入图像描述

乘以 BT,得到:

在此处输入图像描述

现在,BTdot(B) 是正方形,满秩,确实有逆。因此,我们可以乘以 BTdot(B) 的倒数,或者使用求解器(如上所述)来得到 c

You can form the left inverse:

import numpy as np
import numpy.linalg as lin
B = np.array([[2],[4]])
b = np.array([[4],[4]])

B_linv = lin.solve(B.T.dot(B), B.T)
c = B_linv.dot(b)
print('c\n', c)

Result:

c
 [[ 1.2]]

Actually, we can simply run the solver once, without forming an inverse, like this:

c = lin.solve(B.T.dot(B), B.T.dot(b))
print('c\n', c)

Result:

c
 [[ 1.2]]

.... as before

Why? Because:

We have:

enter image description here

Multiply through by B.T, gives us:

enter image description here

Now, B.T.dot(B) is square, full rank, does have an inverse. And therefore we can multiply through by the inverse of B.T.dot(B), or use a solver, as above, to get c.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文