scipy.linalg.orth orthogonalizes matrices with SVD. After orthogonalization, the vectors are put in the descending order of singular values. However, for some reasons, I need the orthogonalized vectors to be as similar to the original ones as possible, both in shapes and in orders.
For example, for the matrix,
>>> import scipy.linalg as sl
>>> import numpy as np
>>> A=np.array([[5,0,0],[0,2,0],[0,0,4]])
>>> A
array([[5, 0, 0],
[0, 2, 0],
[0, 0, 4]])
instead of getting the orthogonalized result with the descending order of the singular values 5, 4, 2,
>>> sl.orth(A)
array([[1., 0., 0.],
[0., 0., 1.],
[0., 1., 0.]])
I want the result to be
1 0 0
0 1 0
0 0 1
which keeps the order of the original vectors. Is there any way I can realize this? Thanks for any help!
You can't directly ask SciPy to do this. SciPy internally uses a LAPACK function called gesdd to do this, which already does this sorting, and does not have a way to disable it.
However, what you could do instead is to compare the cosine similarity of every orthogonal vector to every input vector, and sort the orthogonal vectors by similarity to the input vectors.
Example:
Output: