Python LinAlgError: 数组的最后 2 个维度必须是正方形
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48909600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
LinAlgError: Last 2 dimensions of the array must be square
提问by K Hymanson
I need to solve a set of simultaneous equations of the form Ax = Bfor x. I've used the numpy.linalg.solve function, inputting A and B, but I get the error 'LinAlgError: Last 2 dimensions of the array must be square'. How do I fix this?
我需要为x求解一组形式为Ax = B的联立方程。我使用了 numpy.linalg.solve 函数,输入了 A 和 B,但出现错误“LinAlgError:数组的最后 2 个维度必须是正方形”。我该如何解决?
Here's my code:
这是我的代码:
A = matrix([[v1x, v2x], [v1y, v2y], [v1z, v2z]])
print A
B = [(p2x-p1x-nmag[0]), (p2y-p1y-nmag[1]), (p2z-p1z-nmag[2])]
print B
x = numpy.linalg.solve(A, B)
The values of the matrix/vector are calculated earlier in the code and this works fine, but the values are:
矩阵/向量的值是在代码的前面计算的,这很好用,但值是:
A=
一=
(-0.56666301, -0.52472909)
(0.44034147, 0.46768087)
(0.69641397, 0.71129036)
B=
乙=
(-0.38038602567630364, -24.092279373295057, 0.0)
x should have the form (x1,x2,0)
x 应具有 (x1,x2,0) 形式
回答by Keivan
In case you still haven't found an answer, or in case someone in the future has this question.
如果您仍然没有找到答案,或者将来有人有这个问题。
To solve Ax=b:
要解决Ax=b:
numpy.linalg.solve uses LAPACK gesv. As mentioned in the documentation of LAPACK, gesv requires Ato be square:
numpy.linalg.solve 使用 LAPACK gesv。正如LAPACK 的文档中提到的, gesv 要求A是正方形:
LA_GESV computes the solution to a real or complex linear system of equations AX = B, where A is a square matrix and X and B are rectangular matrices or vectors. Gaussian elimination with row interchanges is used to factor A as A = PL*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 above system.
LA_GESV 计算方程 A X = B的实数或复线性方程组的解,其中 A 是方阵,X 和 B 是矩形矩阵或向量。使用行交换的高斯消元法将 A分解为 A = PL*U ,其中 P 是置换矩阵,L 是单位下三角,U 是上三角。然后使用 A 的因式分解形式来求解上述系统。
If Amatrix is not square, it means that you either have more variables than your equations or the other way around. In these situations, you can have the cases of no solution or infinite number of solutions. What determines the solution space is the rank of the matrix compared to the number of columns. Therefore, you first have to check the rank of the matrix.
如果一个矩阵不是方形,这意味着你要么有比你的方程或周围的其他方法更多的变数。在这些情况下,您可能会遇到无解或无限多解的情况。决定解空间的是矩阵相对于列数的秩。因此,您首先必须检查矩阵的秩。
That being said, you can use another method to solve your system of linear equations. I suggest having a look at factorization methods like LU or QR or even SVD. In LAPACK you can use getrs
, in Python you can different things:
话虽如此,您可以使用另一种方法来求解线性方程组。我建议看看像 LU 或 QR 甚至 SVD 这样的分解方法。在 LAPACK 中你可以使用getrs
,在 Python 中你可以使用不同的东西:
- first do the factorization like QR and then feed the resulting matrices to a method like
scipy.linalg.solve_triangular
- solve the least-squares using
numpy.linalg.lstsq
- 首先像 QR 一样进行分解,然后将结果矩阵提供给类似的方法
scipy.linalg.solve_triangular
- 使用解决最小二乘法
numpy.linalg.lstsq
Also have a look herewhere a simple example is formulated and solved.
也看看这里有一个简单的例子被制定和解决。
回答by ashish trehan
A square matrix is a matrix with the same number of rows and columns. The matrix you are doing is a 3 by 2. Add a column of zeroes to fix this problem.
方阵是具有相同行数和列数的矩阵。您正在执行的矩阵是 3 x 2。添加一列零以解决此问题。