2

I'm trying to use numpy.linalg.lstsq to solve following homogenous system with following constraint:

  Ax = 0
  |x| = 1

If I just purely call:

   numpy.linalg.lstsq(A, np.zeros((A.shape[0],1), dtype=np.float))

The solution would be a zero column matrix. I wonder how to create the |x|=1 constraint so that I would get a different solution rather than a zero matrix.

Thanks.

stoney78us
  • 195
  • 2
  • 5
  • 17
  • Does notation `|x|=1` mean that the norm of `x` should be one? If this is the case, any normalized `x` in the nullspace of `A` is a valid solution. – Stelios Oct 10 '16 at 08:04
  • Possible duplicate of [Python (NumPy, SciPy), finding the null space of a matrix](http://stackoverflow.com/questions/5889142/python-numpy-scipy-finding-the-null-space-of-a-matrix) – P. Camilleri Oct 10 '16 at 08:16
  • You can't get anything other than zero matrix if your matrix is full rank – percusse Oct 10 '16 at 11:07

1 Answers1

2

You set the constraint by forcing a term of the solution to be 1, then solving the rest of the linear system. Once you have a solution you can normalize.

import numpy as np

a = np.arange(9).reshape(3, 3)
b = a[:, 0].copy()
# we impose a condition that first term be 1, 
x = np.linalg.lstsq(a[:, 1:], -b)[0]
x = np.r_[1, x]
x /= np.linalg.norm(x)
print(a.dot(np.r_[1, x])) # [0, 0, 0]
Elliot
  • 2,541
  • 17
  • 27
  • What if for all x s.t. Ax = 0, the first element of x is 0 ? Also, saying "we know the first is 1" should be "we impose that the first term is 1". – P. Camilleri Oct 10 '16 at 06:54
  • Fair point. I guess this makes this question a repeat of http://stackoverflow.com/questions/5889142/python-numpy-scipy-finding-the-null-space-of-a-matrix then. – Elliot Oct 10 '16 at 07:59
  • Yes, I'm flagging it as duplicate. Nice idea, though – P. Camilleri Oct 10 '16 at 08:16