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

dispatch.jl: simplify overloads for SparseMatrix * AbstractArray #181

Merged
merged 2 commits into from
Nov 27, 2023
Merged
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
222 changes: 29 additions & 193 deletions src/dispatch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,199 +343,35 @@
# `LinearAlgebra.mul!` which prevents us from using mutability of the
# arithmetic. For this reason we intercept the calls and redirect them to `mul`.

# A few are overwritten below but many more need to be redirected to `mul` in
# `linalg.jl`.

Base.:*(A::_SparseMat{<:AbstractMutable}, x::StridedVector) = mul(A, x)

Base.:*(A::_SparseMat, x::StridedVector{<:AbstractMutable}) = mul(A, x)

function Base.:*(
A::_SparseMat{<:AbstractMutable},
x::StridedVector{<:AbstractMutable},
)
return mul(A, x)
end

# These six methods are needed on Julia v1.2 and earlier
function Base.:*(
A::LinearAlgebra.Adjoint{<:AbstractMutable,<:_SparseMat},
x::StridedVector,
)
return mul(A, x)
end

function Base.:*(
A::LinearAlgebra.Adjoint{<:Any,<:_SparseMat},
x::StridedVector{<:AbstractMutable},
)
return mul(A, x)
end

function Base.:*(
A::LinearAlgebra.Adjoint{<:AbstractMutable,<:_SparseMat},
x::StridedVector{<:AbstractMutable},
)
return mul(A, x)
end

function Base.:*(
A::LinearAlgebra.Transpose{<:AbstractMutable,<:_SparseMat},
x::StridedVector,
)
return mul(A, x)
end

function Base.:*(
A::LinearAlgebra.Transpose{<:Any,<:_SparseMat},
x::StridedVector{<:AbstractMutable},
)
return mul(A, x)
end

function Base.:*(
A::LinearAlgebra.Transpose{<:AbstractMutable,<:_SparseMat},
x::StridedVector{<:AbstractMutable},
)
return mul(A, x)
end

function Base.:*(
A::_SparseMat{<:AbstractMutable},
B::_SparseMat{<:AbstractMutable},
)
return mul(A, B)
end

Base.:*(A::_SparseMat{<:Any}, B::_SparseMat{<:AbstractMutable}) = mul(A, B)

Base.:*(A::_SparseMat{<:AbstractMutable}, B::_SparseMat{<:Any}) = mul(A, B)

function Base.:*(
A::_SparseMat{<:AbstractMutable},
B::LinearAlgebra.Adjoint{<:AbstractMutable,<:_SparseMat},
)
return mul(A, B)
end

function Base.:*(
A::_SparseMat{<:Any},
B::LinearAlgebra.Adjoint{<:AbstractMutable,<:_SparseMat},
)
return mul(A, B)
end

function Base.:*(
A::_SparseMat{<:AbstractMutable},
B::LinearAlgebra.Adjoint{<:Any,<:_SparseMat},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Adjoint{<:AbstractMutable,<:_SparseMat},
B::_SparseMat{<:AbstractMutable},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Adjoint{<:Any,<:_SparseMat},
B::_SparseMat{<:AbstractMutable},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Adjoint{<:AbstractMutable,<:_SparseMat},
B::_SparseMat{<:Any},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Transpose{<:AbstractMutable,<:_SparseMat},
B::_SparseMat{<:AbstractMutable},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Transpose{<:Any,<:_SparseMat},
B::_SparseMat{<:AbstractMutable},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Transpose{<:AbstractMutable,<:_SparseMat},
B::_SparseMat{<:Any},
)
return mul(A, B)
end

function Base.:*(
A::StridedMatrix{<:AbstractMutable},
B::_SparseMat{<:AbstractMutable},
)
return mul(A, B)
end

Base.:*(A::StridedMatrix{<:Any}, B::_SparseMat{<:AbstractMutable}) = mul(A, B)

Base.:*(A::StridedMatrix{<:AbstractMutable}, B::_SparseMat{<:Any}) = mul(A, B)

function Base.:*(
A::_SparseMat{<:AbstractMutable},
B::StridedMatrix{<:AbstractMutable},
)
return mul(A, B)
end

Base.:*(A::_SparseMat{<:Any}, B::StridedMatrix{<:AbstractMutable}) = mul(A, B)

Base.:*(A::_SparseMat{<:AbstractMutable}, B::StridedMatrix{<:Any}) = mul(A, B)

function Base.:*(
A::LinearAlgebra.Adjoint{<:AbstractMutable,<:_SparseMat},
B::StridedMatrix{<:AbstractMutable},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Adjoint{<:Any,<:_SparseMat},
B::StridedMatrix{<:AbstractMutable},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Adjoint{<:AbstractMutable,<:_SparseMat},
B::StridedMatrix{<:Any},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Transpose{<:AbstractMutable,<:_SparseMat},
B::StridedMatrix{<:AbstractMutable},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Transpose{<:Any,<:_SparseMat},
B::StridedMatrix{<:AbstractMutable},
)
return mul(A, B)
end

function Base.:*(
A::LinearAlgebra.Transpose{<:AbstractMutable,<:_SparseMat},
B::StridedMatrix{<:Any},
)
return mul(A, B)
const _LinearAlgebraWrappers = (
LinearAlgebra.Adjoint,
LinearAlgebra.Transpose,
# TODO(odow): we could expand these overloads to other LinearAlgebra types.
# LinearAlgebra.Symmetric,
# LinearAlgebra.Hermitian,
# LinearAlgebra.Diagonal,
# LinearAlgebra.LowerTriangular,
# LinearAlgebra.UpperTriangular,
# LinearAlgebra.UnitLowerTriangular,
# LinearAlgebra.UnitUpperTriangular,
)

const _MatrixLike = vcat(
Any[T -> LA{<:T,<:_SparseMat} for LA in _LinearAlgebraWrappers],
Any[T->_SparseMat{<:T}, T->StridedMatrix{<:T}],

Check warning on line 361 in src/dispatch.jl

View check run for this annotation

Codecov / codecov/patch

src/dispatch.jl#L360-L361

Added lines #L360 - L361 were not covered by tests
)

for f_A in _MatrixLike, f_B in vcat(_MatrixLike, T -> StridedVector{<:T})

Check warning on line 364 in src/dispatch.jl

View check run for this annotation

Codecov / codecov/patch

src/dispatch.jl#L364

Added line #L364 was not covered by tests
A, mut_A = f_A(Any), f_A(AbstractMutable)
B, mut_B = f_B(Any), f_B(AbstractMutable)
if A <: StridedMatrix && B <: StridedMatrix
continue
end
@eval begin
Base.:*(a::$(mut_A), b::$(B)) = mul(a, b)
Base.:*(a::$(A), b::$(mut_B)) = mul(a, b)
Base.:*(a::$(mut_A), b::$(mut_B)) = mul(a, b)
end
end

const StridedMaybeAdjOrTransMat{T} = Union{
Expand Down