LinAlgError:数组的最后2个尺寸必须为正方形 [英] LinAlgError: Last 2 dimensions of the array must be square

查看:122
本文介绍了LinAlgError:数组的最后2个尺寸必须为正方形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要求解x的一组形式为 A x = B 的联立方程.我使用了numpy.linalg.solve函数,输入了A和B,但出现错误"LinAlgError:数组的最后2个维必须是正方形".我该如何解决?

I need to solve a set of simultaneous equations of the form Ax = B for 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?

这是我的代码:

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的格式应为(x1,x2,0)

x should have the form (x1,x2,0)

推荐答案

如果您仍然找不到答案,或者将来有人遇到这个问题.

In case you still haven't found an answer, or in case someone in the future has this question.

要解决 Ax = b :

To solve Ax=b:

numpy.linalg.solve使用LAPACK gesv.如LAPACK文档中 所述,gesv需要 A 变成正方形:

numpy.linalg.solve uses LAPACK gesv. As mentioned in the documentation of LAPACK, gesv requires A to be square:

LA_GESV计算方程A X = B的实数或复数线性系统的解,其中A是一个正方形矩阵,而X和B是一个矩形矩阵或向量.使用行互换的高斯消除将A分解为A = P L * U,其中P是置换矩阵,L是单位下三角,U是上三角.然后使用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.

如果 A 矩阵不是正方形,则意味着您的变量多于方程式,反之亦然.在这些情况下,您可能会遇到无解或无数个解的情况.决定解决方案空间的是与列数相比的矩阵等级.因此,您首先必须检查矩阵的等级.

If A matrix 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.

也就是说,您可以使用另一种方法来求解线性方程组.我建议看一下LU或QR甚至SVD的分解方法.在LAPACK中,您可以使用getrs;在Python中,您可以使用不同的东西:

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:

  • 首先像QR一样进行因式分解,然后将结果矩阵馈给scipy.linalg.solve_triangular
  • 之类的方法.
  • 使用numpy.linalg.lstsq
  • 求解最小二乘
  • 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

还可以查看

Also have a look here where a simple example is formulated and solved.

这篇关于LinAlgError:数组的最后2个尺寸必须为正方形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆