From 22e0bac8f094410575c9dc0292f7d8235a99f36e Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 14 Feb 2024 14:38:53 +1300 Subject: [PATCH] Fix multiplying Symmetric and Hermitian matrices --- src/dispatch.jl | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/dispatch.jl b/src/dispatch.jl index c1f4cf43..20f98e36 100644 --- a/src/dispatch.jl +++ b/src/dispatch.jl @@ -474,26 +474,27 @@ Base.:*(A::AbstractArray, α::AbstractMutable) = A .* α # 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) 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