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

stdlib: make dot product of Hermitian matrices real #52318

Merged
merged 6 commits into from
Feb 3, 2024
Merged
Changes from 4 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
73 changes: 36 additions & 37 deletions stdlib/LinearAlgebra/src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -453,47 +453,46 @@ function triu(A::Symmetric, k::Integer=0)
end
end

for (T, trans, real) in [(:Symmetric, :transpose, :identity), (:(Hermitian{<:Union{Real,Complex}}), :adjoint, :real)]
@eval begin
function dot(A::$T, B::$T)
n = size(A, 2)
if n != size(B, 2)
throw(DimensionMismatch("A has dimensions $(size(A)) but B has dimensions $(size(B))"))
end
dot(A::Symmetric, B::Symmetric) = _dot_hermsym(A, B, transpose, identity)
dot(A::Hermitian{<:Union{Real,Complex}}, B::Hermitian{<:Union{Real,Complex}}) = _dot_hermsym(A, B, conj, real)

dotprod = zero(dot(first(A), first(B)))
@inbounds if A.uplo == 'U' && B.uplo == 'U'
for j in 1:n
for i in 1:(j - 1)
dotprod += 2 * $real(dot(A.data[i, j], B.data[i, j]))
end
dotprod += dot(A[j, j], B[j, j])
end
elseif A.uplo == 'L' && B.uplo == 'L'
for j in 1:n
dotprod += dot(A[j, j], B[j, j])
for i in (j + 1):n
dotprod += 2 * $real(dot(A.data[i, j], B.data[i, j]))
end
end
elseif A.uplo == 'U' && B.uplo == 'L'
for j in 1:n
for i in 1:(j - 1)
dotprod += 2 * $real(dot(A.data[i, j], $trans(B.data[j, i])))
end
dotprod += dot(A[j, j], B[j, j])
end
else
for j in 1:n
dotprod += dot(A[j, j], B[j, j])
for i in (j + 1):n
dotprod += 2 * $real(dot(A.data[i, j], $trans(B.data[j, i])))
end
end
function _dot_hermsym(A, B, trans, real)
n = size(A, 2)
if n != size(B, 2)
throw(DimensionMismatch("A has dimensions $(size(A)) but B has dimensions $(size(B))"))
end

dotprod = real(zero(dot(first(A), first(B))))
@inbounds if A.uplo == 'U' && B.uplo == 'U'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we stop using this? This function assumes but does not enforce 1-based indexing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parents of Symmetric and Hermitian are required to be one-based at construction (see the very top of this file), so this is safe.

for j in 1:n
for i in 1:(j-1)
dotprod += 2 * real(dot(A.data[i, j], B.data[i, j]))
end
dotprod += real(dot(A[j, j], B[j, j]))
end
elseif A.uplo == 'L' && B.uplo == 'L'
for j in 1:n
dotprod += real(dot(A[j, j], B[j, j]))
for i in (j+1):n
dotprod += 2 * real(dot(A.data[i, j], B.data[i, j]))
end
end
elseif A.uplo == 'U' && B.uplo == 'L'
for j in 1:n
for i in 1:(j-1)
dotprod += 2 * real(dot(A.data[i, j], trans(B.data[j, i])))
end
dotprod += real(dot(A[j, j], B[j, j]))
end
else
for j in 1:n
dotprod += real(dot(A[j, j], B[j, j]))
for i in (j+1):n
dotprod += 2 * real(dot(A.data[i, j], trans(B.data[j, i])))
end
return dotprod
end
end
return dotprod
end

(-)(A::Symmetric) = Symmetric(-A.data, sym_uplo(A.uplo))
Expand Down