Skip to content

Commit

Permalink
Fix return type of +/-(::Symmetric, ::Hermitian) for JuMP types
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 11, 2024
1 parent 52bb94f commit 1ae61fd
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,55 @@ function Base.complex(
)
return r + im * i
end

function Base.:+(
A::LinearAlgebra.Symmetric{V},
B::LinearAlgebra.Hermitian,
) where {
V<:Union{
GenericVariableRef{<:Real},
GenericAffExpr{<:Real},
GenericQuadExpr{<:Real},
},
}
return LinearAlgebra.Hermitian(A) + B
end

function Base.:+(
A::LinearAlgebra.Hermitian,
B::LinearAlgebra.Symmetric{V},
) where {
V<:Union{
GenericVariableRef{<:Real},
GenericAffExpr{<:Real},
GenericQuadExpr{<:Real},
},
}
return A + LinearAlgebra.Hermitian(B)
end

function Base.:-(
A::LinearAlgebra.Symmetric{V},
B::LinearAlgebra.Hermitian,
) where {
V<:Union{
GenericVariableRef{<:Real},
GenericAffExpr{<:Real},
GenericQuadExpr{<:Real},
},
}
return LinearAlgebra.Hermitian(A) - B
end

function Base.:-(
A::LinearAlgebra.Hermitian,
B::LinearAlgebra.Symmetric{V},
) where {
V<:Union{
GenericVariableRef{<:Real},
GenericAffExpr{<:Real},
GenericQuadExpr{<:Real},
},
}
return A - LinearAlgebra.Hermitian(B)
end
22 changes: 22 additions & 0 deletions test/test_operator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -689,4 +689,26 @@ function test_base_complex()
return
end

function test_hermitian_and_symmetric()
model = Model()
@variable(model, A[1:2, 1:2], Symmetric)
@variable(model, B[1:2, 1:2], Hermitian)
for (x, y) in (
(A, B),
(B, A),
(1.0 * A, B),
(B, 1.0 * A),
(1.0 * A, 1.0 * B),
(1.0 * B, 1.0 * A),
(1.0 * LinearAlgebra.Symmetric(A .* A), 1.0 * B),
(1.0 * B, 1.0 * LinearAlgebra.Symmetric(A .* A)),
)
@test x + y isa LinearAlgebra.Hermitian
@test x + y == x .+ y
@test x - y isa LinearAlgebra.Hermitian
@test x - y == x .- y
end
return
end

end

0 comments on commit 1ae61fd

Please sign in to comment.