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 multiplying Symmetric and Hermitian matrices #261

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
25 changes: 13 additions & 12 deletions src/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -474,26 +474,27 @@

# Needed for Julia v1.0, otherwise, `broadcast(*, α, A)` gives a `Array` and
# not a `Symmetric`.

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

function Base.:*(α::Number, A::LinearAlgebra.Symmetric{<:AbstractMutable})
return LinearAlgebra.Symmetric(
α * parent(A),
LinearAlgebra.sym_uplo(A.uplo),
)
c = LinearAlgebra.sym_uplo(A.uplo)
B = c == :U ? _mult_upper(α, A) : _mult_lower(α, A)
return LinearAlgebra.Symmetric(B, c)
end

function Base.:*(α::Number, A::LinearAlgebra.Hermitian{<:AbstractMutable})
return LinearAlgebra.Hermitian(
α * parent(A),
LinearAlgebra.sym_uplo(A.uplo),
)
c = LinearAlgebra.sym_uplo(A.uplo)
B = c == :U ? _mult_upper(α, A) : _mult_lower(α, A)
return LinearAlgebra.Hermitian(B, c)

Check warning on line 490 in src/dispatch.jl

View check run for this annotation

Codecov / codecov/patch

src/dispatch.jl#L488-L490

Added lines #L488 - L490 were not covered by tests
end

# Fix ambiguity identified by Aqua.jl.
function Base.:*(α::Real, A::LinearAlgebra.Hermitian{<:AbstractMutable})
return LinearAlgebra.Hermitian(
α * parent(A),
LinearAlgebra.sym_uplo(A.uplo),
)
c = LinearAlgebra.sym_uplo(A.uplo)
B = c == :U ? _mult_upper(α, A) : _mult_lower(α, A)
return LinearAlgebra.Hermitian(B, c)
end

# These three have specific methods that just redirect to `Matrix{T}` which
Expand Down
12 changes: 12 additions & 0 deletions test/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ end
@test MA.operate(LinearAlgebra.dot, z, z) == LinearAlgebra.dot(z, z)
end
end

@testset "*(::Real, ::Union{Hermitian,Symmetric})" begin
A = DummyBigInt[1 2; 2 3]
B = DummyBigInt[2 4; 4 6]
@test MA.isequal_canonical(2 * A, B)
C = LinearAlgebra.Symmetric(B)
@test MA.isequal_canonical(2 * LinearAlgebra.Symmetric(A, :U), C)
@test MA.isequal_canonical(2 * LinearAlgebra.Symmetric(A, :L), C)
D = LinearAlgebra.Hermitian(B)
@test all(MA.isequal_canonical.(2 * LinearAlgebra.Hermitian(A, :L), D))
@test all(MA.isequal_canonical.(2 * LinearAlgebra.Hermitian(A, :U), D))
end
Loading