AufsMaulwurf
AufsMaulwurf

Reputation: 41

LinAlgError: Last 2 dimensions of the array must be square, matrices cant be solved

I'm trying to solve a matrix with linalg.solve, but i always get the error LinAlgError: Last 2 dimensions of the array must be square. where is my mistake? Thanks for your help

Here is my code:

reaktionsgleichung=np.array([[3,1,0,0,0],[5,0,2,0,0],[3,0,0,-2,0],[9,-2,-1,0,-2]])

loesungsvektor=np.array([0,0,0,0,0])

np.linalg.solve(reaktionsgleichung, loesungsvektor)

Upvotes: 0

Views: 3878

Answers (1)

Z Li
Z Li

Reputation: 4318

Your matrix is a 4 by 5 matrix, which is not a square matrix.

According to the documentation of numpy.linalg.solve:

Computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix equation ax = b.

It only works with a square matrix.

Consider numpy.linalg.lstsq for non-square matrix

Upvotes: 1

Related Questions