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

Fix *(::AbstractJuMPScalar{<:Real}, ::Hermitian) to return Hermitian #3695

Merged
merged 4 commits into from
Mar 4, 2024

Conversation

odow
Copy link
Member

@odow odow commented Feb 29, 2024

Closes #3694

Copy link

codecov bot commented Feb 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.35%. Comparing base (a9b5ff9) to head (9b0eaee).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3695      +/-   ##
==========================================
- Coverage   98.36%   98.35%   -0.02%     
==========================================
  Files          43       43              
  Lines        5699     5707       +8     
==========================================
+ Hits         5606     5613       +7     
- Misses         93       94       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@araujoms
Copy link
Contributor

araujoms commented Mar 1, 2024

Could you also do the Symmetric case? It's actually much easier because you don't need to test whether the scalar is real.

test/test_complex.jl Outdated Show resolved Hide resolved
Co-authored-by: Mateus Araújo <maltusan@gmail.com>
@odow
Copy link
Member Author

odow commented Mar 2, 2024

This one we can fix in MA: jump-dev/MutableArithmetics.jl#268

@araujoms
Copy link
Contributor

araujoms commented Mar 2, 2024

I've just realized that with this definition of multiplication you are generating hidden undefs in the lower triangular, that is, you are relying on the bug JuliaLang/julia#52895 being fixed, which I think is only the case for Julia 1.11 onwards. So the following code will fail for all previous version of Julia:

using JuMP
prob = Model()
@variable(prob,x)
m = x*LinearAlgebra.Hermitian(randn(2,2))
m+m

@odow
Copy link
Member Author

odow commented Mar 2, 2024

Perhaps we should close this then. The currently behavior is correct. The new PR would introduce the risk of incorrect behavior due to bugs in Base.

If JuMP generates a Matrix, you can always explicitly wrap it in Hermitian.

It's really hard to uniformly support all of LinearAlgebra, SparseArrays, MutableArithmetics, and JuMP.

@araujoms
Copy link
Contributor

araujoms commented Mar 2, 2024

Or you could just return instead

LinearAlgebra.Hermitian(Matrix(LinearAlgebra.Hermitian(B, c)),c)

This gets rids of the undefs, so no bugs are possible, you keep the performance gain, and don't require the users to always rewrap everything in Hermitian.

src/operators.jl Outdated Show resolved Hide resolved
@odow odow requested a review from blegat March 3, 2024 23:06
@odow odow merged commit ecaf80c into master Mar 4, 2024
10 of 11 checks passed
@odow odow deleted the od/mul-hermitian branch March 4, 2024 21:12
@araujoms
Copy link
Contributor

araujoms commented Mar 4, 2024

Thanks a lot for solving all of the problems I pointed out! I'm always amazed by how quick you and @blegat are.

@odow
Copy link
Member Author

odow commented Mar 4, 2024

No problem. A lot of the problems are because you might be the only person actively using JuMP for complex Hermitian stuff... 😆

@araujoms
Copy link
Contributor

araujoms commented Mar 4, 2024

Well, one might say only, but I'd rather say first. 😉

There are also these guys, but they invented their own reformulation of the complex Hermitian PSD cone, so they are only using JuMP for the real one.

@odow
Copy link
Member Author

odow commented Mar 4, 2024

There are also these guys

You might recognize an author

@araujoms
Copy link
Contributor

araujoms commented Mar 4, 2024

Well, I know some of them personally, but there is one that I'm sure you do. 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

*(::Real, ::Hermitian) is not hermitian
2 participants