Circshift of a SparseMatrix

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

Submit a pull request: Pull requests · JuliaLang/julia · GitHub.

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 Likes

Thanks, will try to do so!
A

1 Like

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

1 Like

Here it is
https://github.com/JuliaLang/julia/pull/30300

3 Likes

More obscure but more efficient version in this PR
https://github.com/JuliaLang/julia/pull/30317