-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
fix fastpaths for materializing transposes / adjoints of sparse matrices #28049
Conversation
@@ -397,6 +397,8 @@ function SparseMatrixCSC{Tv,Ti}(M::StridedMatrix) where {Tv,Ti} | |||
end | |||
return SparseMatrixCSC(size(M, 1), size(M, 2), colptr, rowval, nzval) | |||
end | |||
SparseMatrixCSC(M::LinearAlgebra.Adjoint{Tv,SparseMatrixCSC{Tv,Ti}}) where {Tv,Ti} = copy(M) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably relax the signature to Adjoint{<:Any,<:SparseMatrixCSC}
(which is also the one of the copy
method this should dispatch to); likewise below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm
julia> B = sprand(1,1,1.0);
julia> SparseMatrixCSC(M::Adjoint{<:Any,SparseMatrixCSC}) = copy(M)
SparseMatrixCSC
julia> @which SparseMatrixCSC(B')
(::Type{SparseMatrixCSC})(M::AbstractArray{Tv,2}) where Tv in SparseArrays at /Users/kristoffer/julia/usr/share/julia/stdlib/v0.7/SparseArrays/src/sparsematrix.jl:371
julia> SparseMatrixCSC(M::LinearAlgebra.Adjoint{Tv,SparseMatrixCSC{Tv,Ti}}) where {Tv,Ti} = copy(M)
SparseMatrixCSC
julia> @which SparseMatrixCSC(B')
(::Type{SparseMatrixCSC})(M::Adjoint{Tv,SparseMatrixCSC{Tv,Ti}}) where {Tv, Ti} in Main at REPL[7]:1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adjoint{<:Any,<:SparseMatrixCSC}
, not Adjoint{<:Any,SparseMatrixCSC}
:
julia> B = sprand(1,1,1.0);
julia> SparseMatrixCSC(M::Adjoint{<:Any,<:SparseMatrixCSC}) = copy(M)
SparseMatrixCSC
julia> @which SparseMatrixCSC(B')
(::Type{SparseMatrixCSC})(M::Adjoint{#s2,#s1} where #s1<:SparseMatrixCSC where #s2) in Main at REPL[19]:1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, sorry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
@@ -2242,4 +2242,10 @@ _length_or_count_or_five(x) = length(x) | |||
end | |||
end | |||
|
|||
@testset "sparse transpose adjoint" begin | |||
A = sprand(10, 10, 0.75) | |||
@test A' == SparseMatrixCSC(A') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to verify the return type, too, because the new definitions do not obviously return a SparseMatrixCSC
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
@test A' == SparseMatrixCSC(A') | ||
@test SparseMatrixCSC(A') isa SparseMatrixCSC | ||
@test transpose(A) == SparseMatrixCSC(transpose(A)) | ||
@test SparseMatrixCSC(transpose(A)) isa SparseMatrixCSC |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Alternatively @test A' == SparseMatrix(A')::SparseMatrixCSC
.)
Setup:
Before PR:
After PR:
Fixes #28012