Python Forum

Full Version: Inverse of singular matrix
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How one can invert this singular matrix?

m1 = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0], [0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0], [0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1], [1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1], [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1], [0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 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 0 0] [1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
This matrix is of shape (30, 20). Therefore, it couldn't be inverted in traditional sense. The main question here is why do you need to invert such matrix? May be you need to solve a system of linear equation with that matrix, e.g. Ax = b. In this case, your system is probably overdetermined and has no solution in classical sense, but it could be solved in least squares sense.
Even if the matrix has insufficient rank, e.g. rank(A) < 20, you can obtain least squares solution using Moore-Penrose inversion. It is implemented in numpy: np.linalg.pinv.
So, least squares solution could be obtained as follows:

x = np.linalg.pinv(A) @ b, where
b is known vector of shape (30, 1); you can use np.dot(np.linalg.pinv(A), b) instead of @ (if you work with Py < 3.5).
(May-04-2019, 10:42 AM)scidam Wrote: [ -> ]This matrix is of shape (30, 20). Therefore, it couldn't be inverted in traditional sense. The main question here is why do you need to invert such matrix? May be you need to solve a system of linear equation with that matrix, e.g. Ax = b. In this case, your system is probably overdetermined and has no solution in classical sense, but it could be solved in least squares sense.
Even if the matrix has insufficient rank, e.g. rank(A) < 20, you can obtain least squares solution using Moore-Penrose inversion. It is implemented in numpy: np.linalg.pinv.
So, least squares solution could be obtained as follows:

x = np.linalg.pinv(A) @ b, where
b is known vector of shape (30, 1); you can use np.dot(np.linalg.pinv(A), b) instead of @ (if you work with Py < 3.5).

Yes I need to solve a system of linear equation, and I tried with Moore-Penrose inversion, but the solution it is not sufficient. Therefore I am looking for another was of inverting this matrix.
Pseudoinverse gives you a solution of minimum norm; In your case matrix A has rank=15, so you have "quite large subspace of freedom" to choose another solution. From the following post you can find how to get any solution:
any_solution = np.linalg.pinv(A) @ b + (np.ones((20, 20)) - np.linalg.pinv(A)@A)@ y  # y.shape = (20, 1)
y is arbitrary vector. Choosing appropriate y probably lead you to desirable solution.
I made a mistake in my previous post, np.ones(...) should be replaced with np.eye(20).