Skip to content

Commit

Permalink
Fix direction of circshift (#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre authored Sep 15, 2022
1 parent ead48fe commit 4fb8f0e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/SparseArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ using Base.Sort: Forward
using LinearAlgebra
using LinearAlgebra: AdjOrTrans, matprod

# Temporary workaround for simplifying SparseArrays.jl upgrade in JuliaLang/julia
# to workaround circshift! bug, see https://github.com/JuliaLang/julia/pull/46759
const CIRCSHIFT_WRONG_DIRECTION = circshift!([1, 2, 3], 1) != circshift([1, 2, 3], 1)


import Base: +, -, *, \, /, &, |, xor, ==, zero
Expand Down
8 changes: 4 additions & 4 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4203,16 +4203,16 @@ function Base.swaprows!(A::AbstractSparseMatrixCSC, i, j)
rows[rr[iidx]] = j
jidx == 0 && continue
rotate_range = rr[iidx]:jrange[jidx]
circshift!(@view(vals[rotate_range]), -1)
circshift!(@view(rows[rotate_range]), -1)
circshift!(@view(vals[rotate_range]), CIRCSHIFT_WRONG_DIRECTION ? -1 : 1)
circshift!(@view(rows[rotate_range]), CIRCSHIFT_WRONG_DIRECTION ? -1 : 1)
else
# Same as i, but in the opposite direction
@assert has_j
rows[jrange[jidx]] = i
iidx > length(rr) && continue
rotate_range = rr[iidx]:jrange[jidx]
circshift!(@view(vals[rotate_range]), 1)
circshift!(@view(rows[rotate_range]), 1)
circshift!(@view(vals[rotate_range]), CIRCSHIFT_WRONG_DIRECTION ? 1 : -1)
circshift!(@view(rows[rotate_range]), CIRCSHIFT_WRONG_DIRECTION ? 1 : -1)
end
end
return nothing
Expand Down
4 changes: 2 additions & 2 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2223,8 +2223,8 @@ function subvector_shifter!(R::AbstractVector, V::AbstractVector, start::Integer
end
end
# ...but rowval should be sorted within columns
circshift!(@view(R[start:fin]), split-start+1)
circshift!(@view(V[start:fin]), split-start+1)
circshift!(@view(R[start:fin]), (CIRCSHIFT_WRONG_DIRECTION ? (+) : (-))(split-start+1))
circshift!(@view(V[start:fin]), (CIRCSHIFT_WRONG_DIRECTION ? (+) : (-))(split-start+1))
end

function circshift!(O::SparseVector, X::SparseVector, (r,)::Base.DimsInteger{1})
Expand Down

0 comments on commit 4fb8f0e

Please sign in to comment.