Aya Abdelsalam
Aya Abdelsalam

Reputation: 500

How to get the pseudo inverse of a huge diagonal matrix in python?

If I have a diagonal matrix with diagonal 100Kx1 and how can I to get its pseudo inverse? I won't be able to diagonalise the matrix and then get the inverse like I would do for small matrix so this won't work

np.linalg.pinv(np.diag(D))

Upvotes: 1

Views: 1322

Answers (2)

Paul Panzer
Paul Panzer

Reputation: 53039

Just take the reciprocals of the nonzero elements. You can check with a smaller diagonal matrix that this is what pinv does.

Upvotes: 2

abhinav
abhinav

Reputation: 1138

Suppose you have a vector of 100K x 1 named as A:

A = np.arange(100000) # will create a vector of 100k x 1
diagnoal_matrix = np.diag(A)

Then, you can find pseudo inverse of diagonal matrix as following:

np.linalg.pinv(diagnoal_matrix)

Sample code will look like:

import numpy as np
A = np.arange(100000)
diagnoal_matrix = np.diag(A)
B = np.linalg.pinv(diagnoal_matrix)
print B

It will print the output as following:

   [[    0     0     0 ...,     0     0     0]
 [    0     1     0 ...,     0     0     0]
 [    0     0     2 ...,     0     0     0]
 ..., 
 [    0     0     0 ..., 99997     0     0]
 [    0     0     0 ...,     0 99998     0]
 [    0     0     0 ...,     0     0 99999]]

Hope it helps!

Upvotes: -1

Related Questions