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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,30 @@ function _MA.operate!!(
)
return _throw_operator_error(op, x)
end

_mult_upper(α, A) = parent(α * LinearAlgebra.UpperTriangular(parent(A)))
_mult_lower(α, A) = parent(α * LinearAlgebra.LowerTriangular(parent(A)))

function Base.:*(
x::Union{
GenericVariableRef{<:Real},
GenericAffExpr{<:Real},
GenericQuadExpr{<:Real},
},
A::LinearAlgebra.Hermitian,
)
c = LinearAlgebra.sym_uplo(A.uplo)
B = c == :U ? _mult_upper(x, A) : _mult_lower(x, A)
return LinearAlgebra.Hermitian(B, c)
odow marked this conversation as resolved.
Show resolved Hide resolved
end

function Base.:*(
A::LinearAlgebra.Hermitian,
x::Union{
GenericVariableRef{<:Real},
GenericAffExpr{<:Real},
GenericQuadExpr{<:Real},
},
)
return x * A
end
14 changes: 14 additions & 0 deletions test/test_complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,18 @@ function test_hermitian_jump_scalar()
return
end

function test_mul_real_hermitian()
A = [1+1im 1+1im; 1-2im 3+1im]
model = Model()
@variable(model, x)
for s in (:L, :U), f in (x, x + 1, x^2)
B = LinearAlgebra.Hermitian(A, s)
@test f * B isa LinearAlgebra.Hermitian
@test isequal_canonical(f * B, LinearAlgebra.Hermitian(f * A, s))
@test B * f isa LinearAlgebra.Hermitian
@test isequal_canonical(B * f, LinearAlgebra.Hermitian(A * f, s))
end
return
end

end # TestComplexNumberSupport
Loading