Circshift of a SparseMatrix


#1

Right now, circshift falls back to the generic AbstractMatrix implementation when called with a SparseMatrix (being very slow and adding unnecessary structural zeros values in the output).

Adding something simple like the following seems to do the job. So… what is the best way to get it added?

function circshift!(O::SparseMatrixCSC, X::SparseMatrixCSC, (r,c)::Tuple{T,T}) where T<:Int
    I,J,V = findnz(X)
    O .= sparse(map(i->mod1(i + r, X.n), I), map(j->mod1(j + c, X.m), J), V, X.n, X.m)
end

#2

Submit a pull request: https://github.com/JuliaLang/julia/pulls.

You’d add the function to one of the files in https://github.com/JuliaLang/julia/tree/master/stdlib/SparseArrays/src.

You should also add tests to https://github.com/JuliaLang/julia/tree/master/stdlib/SparseArrays/test.


#3

Thanks, will try to do so!
A


#4

Cool, thanks! Post a link to your PR here.


#5

Here it is


#6

More obscure but more efficient version in this PR