Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linear algebra on python

Tags:

python

numpy

i would like to use numpy.linalg.solve to solve a linear algebra equation, but i got an error message saying 'Last 2 dimensions of the array must be square'. Please shed some light thanks a lot !! here's my code:

import numpy as np
from numpy. linalg import solve

A = np.array([[3,-1,-1,0,0,0], [-1,4,-1,-1,0,0], [0,0,-1,-1,4,-1], [0,0,0,-1,-1,3]],float)

w = np.array([5,5,0,0],float)

v = solve(A,w)

print(v)
like image 557
tchengaa Avatar asked Mar 13 '15 17:03

tchengaa


1 Answers

As igavriil already wrote numpy.linalg.solve can only be used to find (the exact) solution for a well-determined system (i.e sqare coefficient matrix). If your system is under- or over-determined, there is usually no exact solution.

If you want to find an approximate solution, you can use numpy.linalg.lstsq. It uses a method called "least-squares-fitting" to find a solution that minimizes the overall error.

like image 57
jandob Avatar answered Sep 27 '22 22:09

jandob