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

Add dispatch for LinearAlgebra.dot for Symmetric and Hermitian matrices #248

Merged
merged 1 commit into from
Nov 30, 2023
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
26 changes: 7 additions & 19 deletions src/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,13 @@ function LinearAlgebra._dot_nonrecursive(
return fused_map_reduce(add_mul, lhs, rhs)
end

function LinearAlgebra.dot(
lhs::AbstractArray{<:AbstractMutable},
rhs::AbstractArray,
)
return operate(LinearAlgebra.dot, lhs, rhs)
end

function LinearAlgebra.dot(
lhs::AbstractArray,
rhs::AbstractArray{<:AbstractMutable},
)
return operate(LinearAlgebra.dot, lhs, rhs)
end

function LinearAlgebra.dot(
lhs::AbstractArray{<:AbstractMutable},
rhs::AbstractArray{<:AbstractMutable},
)
return operate(LinearAlgebra.dot, lhs, rhs)
for A in (LinearAlgebra.Symmetric, LinearAlgebra.Hermitian, AbstractArray)
B = A{<:AbstractMutable}
@eval begin
LinearAlgebra.dot(x::$A, y::$B) = operate(LinearAlgebra.dot, x, y)
LinearAlgebra.dot(x::$B, y::$A) = operate(LinearAlgebra.dot, x, y)
LinearAlgebra.dot(x::$B, y::$B) = operate(LinearAlgebra.dot, x, y)
end
end

# Special-case because the the base version wants to do
Expand Down
2 changes: 2 additions & 0 deletions test/dummy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Base.copy(x::DummyBigInt) = x
MA.mutable_copy(x::DummyBigInt) = DummyBigInt(MA.mutable_copy(x.data))
LinearAlgebra.symmetric_type(::Type{DummyBigInt}) = DummyBigInt
LinearAlgebra.symmetric(x::DummyBigInt, ::Symbol) = x
LinearAlgebra.hermitian_type(::Type{DummyBigInt}) = DummyBigInt
LinearAlgebra.hermitian(x::DummyBigInt, ::Symbol) = x
LinearAlgebra.dot(x::DummyBigInt, y::DummyBigInt) = x * y
function LinearAlgebra.dot(
x::DummyBigInt,
Expand Down
33 changes: 33 additions & 0 deletions test/rewrite.jl
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,36 @@ Base.getindex(x::_KwargRef; i) = x.data[i]
x = _KwargRef(Dict(i => i + 1 for i in 2:4))
@test MA.@rewrite(sum(x[i = j] for j in 2:4)) == 12
end

@testset "dispatch_dot" begin
# Symmetric
x = DummyBigInt[1 2; 2 3]
y = LinearAlgebra.Symmetric(x)
@test MA.isequal_canonical(
LinearAlgebra.dot(x, y),
MA.operate(LinearAlgebra.dot, x, y),
)
a = @allocated LinearAlgebra.dot(x, y)
b = @allocated MA.operate(LinearAlgebra.dot, x, y)
@test a == b
# Symmetric
x = DummyBigInt[1 2; 2 3]
y = LinearAlgebra.Hermitian(x)
@test MA.isequal_canonical(
LinearAlgebra.dot(x, y),
MA.operate(LinearAlgebra.dot, x, y),
)
a = @allocated LinearAlgebra.dot(x, y)
b = @allocated MA.operate(LinearAlgebra.dot, x, y)
@test a == b
# AbstractArray
x = DummyBigInt[1 2; 2 3]
y = x
@test MA.isequal_canonical(
LinearAlgebra.dot(x, y),
MA.operate(LinearAlgebra.dot, x, y),
)
a = @allocated LinearAlgebra.dot(x, y)
b = @allocated MA.operate(LinearAlgebra.dot, x, y)
@test a == b
end