Skip to content

Commit 2c4d018

Browse files
committed
Generalize 3-arg dot to HermOrSym
1 parent 1ffbf9d commit 2c4d018

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/symmetric.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -737,27 +737,28 @@ function mul(A::AdjOrTrans{<:BlasFloat,<:StridedMatrix}, B::HermOrSym{<:BlasFloa
737737
convert(AbstractMatrix{T}, B))
738738
end
739739

740-
function dot(x::AbstractVector, A::RealHermSymComplexHerm, y::AbstractVector)
740+
function dot(x::AbstractVector, A::HermOrSym, y::AbstractVector)
741741
require_one_based_indexing(x, y)
742742
n = length(x)
743743
(n == length(y) == size(A, 1)) || throw(DimensionMismatch())
744+
diagop = A isa Symmetric ? identity : real
744745
data = A.data
745746
r = dot(zero(eltype(x)), zero(eltype(A)), zero(eltype(y)))
746747
iszero(n) && return r
747748
if A.uplo == 'U'
748749
@inbounds for j = 1:length(y)
749-
r += dot(x[j], real(data[j,j]), y[j])
750+
r += dot(x[j], diagop(data[j,j]), y[j])
750751
@simd for i = 1:j-1
751752
Aij = data[i,j]
752-
r += dot(x[i], Aij, y[j]) + dot(x[j], adjoint(Aij), y[i])
753+
r += dot(x[i], Aij, y[j]) + dot(x[j], _conjugation(A)(Aij), y[i])
753754
end
754755
end
755756
else # A.uplo == 'L'
756757
@inbounds for j = 1:length(y)
757-
r += dot(x[j], real(data[j,j]), y[j])
758+
r += dot(x[j], diagop(data[j,j]), y[j])
758759
@simd for i = j+1:length(y)
759760
Aij = data[i,j]
760-
r += dot(x[i], Aij, y[j]) + dot(x[j], adjoint(Aij), y[i])
761+
r += dot(x[i], Aij, y[j]) + dot(x[j], _conjugation(A)(Aij), y[i])
761762
end
762763
end
763764
end

test/symmetric.jl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,21 @@ end
582582

583583
# bug identified in PR #52318: dot products of quaternionic Hermitian matrices,
584584
# or any number type where conj(a)*conj(b) ≠ conj(a*b):
585-
@testset "dot Hermitian quaternion #52318" begin
586-
A, B = [Quaternion.(randn(3,3), randn(3, 3), randn(3, 3), randn(3,3)) |> t -> t + t' for i in 1:2]
585+
@testset "dot Hermitian quaternion" begin
586+
A, B = [(randn(Quaternion{Float64},4,4)) |> t -> t + t' for i in 1:2]
587587
@test A == Hermitian(A) && B == Hermitian(B)
588588
@test dot(A, B) dot(Hermitian(A), Hermitian(B))
589-
A, B = [Quaternion.(randn(3,3), randn(3, 3), randn(3, 3), randn(3,3)) |> t -> t + transpose(t) for i in 1:2]
589+
A, B = [(randn(Quaternion{Float64},4,4)) |> t -> t + transpose(t) for i in 1:2]
590590
@test A == Symmetric(A) && B == Symmetric(B)
591591
@test dot(A, B) dot(Symmetric(A), Symmetric(B))
592+
A = randn(Quaternion{Float64}, 4, 4)
593+
x = randn(Quaternion{Float64}, 4)
594+
y = randn(Quaternion{Float64}, 4)
595+
for t in (Symmetric, Hermitian), uplo in (:U, :L)
596+
M = t(A, uplo)
597+
N = Matrix(M)
598+
@test dot(x, M, y) dot(x, N, y)
599+
end
592600
end
593601

594602
# let's make sure the analogous bug will not show up with kronecker products

0 commit comments

Comments
 (0)