Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Takagi tol #393

Closed
wants to merge 5 commits into from
Closed

Takagi tol #393

wants to merge 5 commits into from

Conversation

RyosukeNORO
Copy link
Collaborator

@RyosukeNORO RyosukeNORO commented Jul 4, 2024

Context:
There were issues with Takagi decomposition, specifically sqrtm, with some matrix.

Description of the Change:
Implemented the tolerance parameter to kill small values in the elements of the matrix to be Takagi-decomposed.
 
Benefits:
Takagi decomposition works well.

Possible Drawbacks:
Takagi decomposition does not work well in other matrices because I have not fully understood why the decomposition have not done well before the change.

Related GitHub Issues:

@RyosukeNORO RyosukeNORO requested a review from elib20 July 4, 2024 14:26
thewalrus/decompositions.py Outdated Show resolved Hide resolved
@nquesada
Copy link
Collaborator

nquesada commented Jul 4, 2024

Could you provide the matrix for which takagi fails @RyosukeNORO ?

@RyosukeNORO
Copy link
Collaborator Author

RyosukeNORO commented Jul 4, 2024

Could you provide the matrix for which takagi fails @RyosukeNORO ?

Here you are.
matrix_inaccurate_Takagi.zip

The zip file includes .npy file of a matrix for which takagi fails. You can simply load the .npy file A = np.load('matrix_inaccurate_Takagi.npy') and run takagi(A).
When I printed the matrix and copied it to create a new matrix, takagi with the new matrix works well.

@nquesada
Copy link
Collaborator

nquesada commented Jul 4, 2024

Thanks. That is a very interesting edge case you found. I suggest you use tol to check if your matrix is diagonal:

np.allclose(A, np.diag(np.diag(A)), rtol, atol) is True

If this the case, then the Takagi is trivial:

d = np.diag(A)
U = np.diag(np.exp(1j*0.5*np.angle(d)))
l = np.abs(d)

@nquesada
Copy link
Collaborator

nquesada commented Jul 4, 2024

The issue seems to be related to sqrtm having problems with diagonal matrices :| .

image

You might want to check if an issue exists for this on the scipy repo and if not make one.

@elib20
Copy link
Collaborator

elib20 commented Jul 4, 2024

Thanks Nico! I think it would be fine if we can just hardcode the diagonal edge case rather than suppress these numerical zeros. There are already multiple different cases that are treated differently in the function. What do you think?

@nquesada
Copy link
Collaborator

nquesada commented Jul 4, 2024

Agreed! I don't like the idea of suppressing things. One thing that requires some thinking is whether we need both an rtol and atol. I think you don't need rtol if you are doing np.allclose(something, 0) : https://numpy.org/doc/stable/reference/generated/numpy.allclose.html `

I do suggest to call the function input rtol following the usage from np.allclose. Also, please add this matrix as a test into into test_decompositions. That way we know we are covering this edge case and the tests bot won't yell at you.

@nquesada
Copy link
Collaborator

nquesada commented Jul 4, 2024

Also, please make sure to order the diagonal elements based on their absolute value (in increasing or decreasing order, following svd_order)

@nquesada
Copy link
Collaborator

nquesada commented Jul 4, 2024

And also, extra also, why don't you make a PR directly into main?

@RyosukeNORO RyosukeNORO mentioned this pull request Jul 5, 2024
@RyosukeNORO RyosukeNORO closed this Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants