0

I have to solve Ax=b . I believe that python library to solve under determined system / over determined system of equation is different. Is there a library function in python which will remove dependent row/column from a matrix so that from the rank of a matrix I could solve the equation appropriately

nantitv
  • 3,539
  • 4
  • 38
  • 61
  • Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it. – Christian Tapia Feb 27 '14 at 06:38
  • use numpy , pretty good for this kind of operation – drabo2005 Feb 27 '14 at 07:04
  • basically I need to solve Ax=b. But in my case the system can be either under determined or over determined. I found out that over determined system can be solve using a least square method and under determined using least norm method. Therefore I need to know the rank of matrix before solving the system of equations. Therefore my question was in this direction. I request everybody to correct me if I'm wrong – nantitv Feb 27 '14 at 08:39
  • 1
    Determination of a matrix rank is a rather ill-posed problem. The best you can do is to compute the SVD and determine from the progression of the singular values whether there is a sufficient magnitude drop that can be used to determine the rank. But better use the pseudo-inverse or least-square solver provided by numpy as in the answer of user3277291. – Lutz Lehmann Feb 27 '14 at 12:26

2 Answers2

2

You can use the pseudoinverse matrix of A denoted as A+. A solution exist if and only if AA+b = b and all solutions are given by x = A+b + (I - A+A)*u

This can be done with numpy.linalg

Example:

>>> A = np.array([[1, 2, 3], [4, 5, 6],[8, 10, 12]])
>>> b = np.array([22., 7., 14.])
>>> Ap = np.linalg.pinv(A)
# Check if a solution exist
>>> np.testing.assert_allclose(A.dot(Ap).dot(b),b,rtol=1e-5, atol=0)
>>> x = Ap.dot(b)
>>> print A.dot(x)
[ 22.,   7.,  14.]

Alternatively you can use numpy.linalg.lstsq

Example:

>>> A = np.array([[1, 2, 3], [4, 5, 6],[8, 10, 12]])
>>> b = np.array([22., 7., 14.])
>>> x = np.linalg.lstsq(A,b)[0]
>>> print np.dot(A,x)
[ 22.,   7.,  14.]
Ben
  • 322
  • 1
  • 4
1

You can use numpy to do scientific computing. E.g.:

In [23]: import numpy as np

In [24]: a=np.arange(16).reshape((4,4))

In [25]: a
Out[25]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15]])

In [26]: np.delete(a, 2, axis=1)  #delete the 3rd column
Out[26]: 
array([[ 0,  1,  3],
       [ 4,  5,  7],
       [ 8,  9, 11],
       [12, 13, 15]])

In [27]: np.rank(a) #this returns the number of dimensions of an array, 
                    #not the concept "rank" in linear algebra, 
Out[27]: 2

In [40]: np.linalg.matrix_rank(a) #this returns matrix rank of array using SVD method
Out[40]: 2

On windows, you can get the unofficial installer here.

Another post: Calculate Matrix Rank using scipy

Community
  • 1
  • 1
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108