[Numpy-discussion] linear algebra help

Nils Wagner nwagner at iam.uni-stuttgart.de
Sat May 16 09:15:46 EDT 2009


On Sat, 16 May 2009 16:01:00 +0300
  Quilby <quilby at gmail.com> wrote:
> Hi-
> This is what I need to do-
> 
> I have this equation-
> 
> Ax = y
> 
> Where A is a rational m*n matrix (m<=n), and x and y are 
>vectors of
> the right size. I know A and y, I don't know what x is 
>equal to. I
> also know that there is no x where Ax equals exactly y. 
>I want to find
> the vector x' such that Ax' is as close as possible to 
>y. Meaning that
> (Ax' - y) is as close as possible to (0,0,0,...0).
> 
> I know that I need to use either the lstsq function:
> http://www.scipy.org/doc/numpy_api_docs/numpy.linalg.linalg.html#lstsq
> 
> or the svd function:
> http://www.scipy.org/doc/numpy_api_docs/numpy.linalg.linalg.html#svd
> 
> I don't understand the documentation at all. Can someone 
>please show
> me how to use these functions to solve my problem.
> 
> Thanks a lot!!!
> 
> -quilby
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
  
I guess you meant a rectangular matrix
http://mathworld.wolfram.com/RectangularMatrix.html

from numpy.random import rand, seed
from numpy import dot, shape
from numpy.linalg import lstsq, norm
seed(1)
m = 10
n = 20
A = rand(m,n) # random matrix
b = rand(m)   # rhs
x,residues,rank,s = lstsq(A,b)

print 'Singular values',s
print 'Numerical rank of A',rank
print 'Solution',x

r=dot(A,x)-b
print 'residual',norm(r)

Cheers,
             Nils



More information about the NumPy-Discussion mailing list