0

I want to slove a set of linear equation of 10 variable.

I created the first array like this:

A=np.random.random_integers(15, size=(10,10))

and i want the values after the equal to be 0

(A.x + d.y + .... + N = 0)

so i did something like that:

b=np.zeros(shape=(10))

but when i apply the linear algebra function

print linalg.solve(A, b)

i just get as a result an array of 10 zeros.

[ 0. 0. 0. 0. -0. -0. -0. -0. 0. 0.]

anyone can help??

Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
ifreak
  • 1,726
  • 4
  • 27
  • 45
  • 1
    Your problem is that the zero-vector is (obviously) a correct solution to the problem. Likely you are looking for the [null space](https://en.wikipedia.org/wiki/Kernel_(matrix)) of the matrix `A`. [This question](http://stackoverflow.com/questions/5889142/python-numpy-scipy-finding-the-null-space-of-a-matrix) may help. – aganders3 Oct 09 '12 at 14:55
  • thank you, but honestly i did not get the idea completely. but i understand that `0-vector` is a solution and the other solutions does not appear because of rounding problems. so i tried to use the function `null(A)` but i got an empty array `[]` even thought I've added a row of `0` to the array `A` as suggested in the answer .. so i don't know what to do now – ifreak Oct 09 '12 at 15:37
  • 1
    @ifreak, your `A` simply has an empty null space since it is (normally at least) a full rank matrix. Try adding `A[9,:] = 0` then you will reduce the rank by one and get a null space with one vector in it. If you do `A[8:,:] = 0` you get two vectors, etc. – seberg Oct 09 '12 at 16:11
  • @seberg is correct. Consider the [Rank-nullity theorem](https://en.wikipedia.org/wiki/Rank-nullity_theorem). There is a chance your random matrix generation will produce a matrix of less than full rank, but that chance is quite low. – aganders3 Oct 09 '12 at 16:17
  • when i try to add `A[9,:] = 0` to the matrix, the `linalg.solve` gives me an error that this matrix is `singular` but why does replacing a row in the matrix with 0 makes the matrix non invertible? it's still the same nXn matrix. – ifreak Oct 10 '12 at 08:57
  • 1
    @sau if you make an edit please fix *all* the issues in the post whilst you're at it. – Flexo May 13 '13 at 15:56

1 Answers1

0

I do not understand the meaning of the second line of code.

Though, with this:

A=np.random.random_integers(15, size=(10,10))
b=np.zeros(shape=(10))

you are solving the system:

A * x = b

which means that you have:

A[1,1] * x_1 + A[1,2] * x_2 + ... + A[1,10] * x_10 = 0
A[2,1] * x_1 + A[2,2] * x_2 + ... + A[2,10] * x_10 = 0
...

So that the x = zero vector is always a perfect solution = you are looking for such x that A x = 0, so x is zero. Try

b = np.random.random_integers(15, size=(10,1))

and x resulting from linalg.solve(A,b) will specify a linear combination of columns from A to sum up to the random b vector.

In https://stackoverflow.com/questions/12910513/how-to-verify-the-results-of-a-linear-equation-system you tried numpy.svd (which is singular value decmposition, which I think you do not want) and numpy.lstsq which tries to find inexact solution that minimizes the least square distance (e.g. for overdetermined matrices).

I might not have understood what you are looking for - please clarify the line specifying what exactly are you looking for.

Community
  • 1
  • 1
Joe M.
  • 609
  • 3
  • 11
  • yes, what you said is correct .. i'm trying to solve this : `A[1,1] * x_1 + A[1,2] * x_2 + ... + A[1,10] * x_10 = 0` `A[2,1] * x_1 + A[2,2] * x_2 + ... + A[2,10] * x_10 = 0` but not for this random matrix, i want a general method to do this. and why the methods i used in the other question will not work ?? – ifreak Oct 16 '12 at 09:05
  • Ah, ok, now I get it. The method you use works perfectly. The problem here is, that for this problem X= 0 is always a correct solution and there is infinitely many other solutions since you have underdeterminded matrix that has one or more degrees of freedom. So if you are looking for one solution to all of your matrices, you do not need to run any algebra at all, just return a zero vector. I think you should think if this is REALLY what you want, say, you do not want for example this: A[1,1] * x_1 + A[1,2] * x_2 + ... + A[1,9] * x_9 = A[1, 10] – Joe M. Oct 16 '12 at 09:20
  • but i don't want the 0 solution, i need the other possible solutions. and i don't want this : `A[1,1] * x_1 + A[1,2] * x_2 + ... + A[1,9] * x_9 = A[1, 10] ` because i will lose a part of the unknown..is there anyway to get the most acceptable solution?? – ifreak Oct 16 '12 at 11:47
  • Ok, see http://en.wikipedia.org/wiki/System_of_linear_equations#Homogeneous_systems . If the A matrix is not singular, the only solution to this Ax = 0 is the 0 vector that linalg.solve correctly returns. Otw. the linalg.solve raises an exception, and now you know you have a singular matrix, then I think you should use the null function from the link from aganders3 on the top. You should not add any row of zeros, since your matrix is already a square. Note: if you have random matrix, the probability that the matrix is singular is small and zeros will likely be only solution. – Joe M. Oct 16 '12 at 13:47
  • I've tried to use the null function from the link, but it's giving me weird results(a list containing 25 list of 25X25)) but in my case it should give only 25 value because i have 25 unknown.. – ifreak Oct 18 '12 at 08:20