Python Forum
Inverse of singular matrix
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Inverse of singular matrix
#1
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,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0 0 1 1 1 0 0 0 1 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 0 0 0]]
Reply
#2
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).
Reply
#3
(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.
Reply
#4
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.
Reply
#5
I made a mistake in my previous post, np.ones(...) should be replaced with np.eye(20).
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Differencing Time series and Inverse after Training donnertrud 0 4,077 May-27-2020, 06:11 AM
Last Post: donnertrud
  Help with Maths (inverse squares) michael1789 5 2,912 Dec-11-2019, 07:30 AM
Last Post: Gribouillis
  Beta Inverse Function ankur2207 5 8,026 Jul-23-2018, 10:07 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020