Skip to content

Commit 05cb226

Browse files
authored
[Utilities] fix bug converting MutableSparseMatrixCSC to SparseMatrixCSC (#2929)
1 parent a693453 commit 05cb226

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/Utilities/sparse_matrix.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ _first_index(::Type{T}, ::ZeroBasedIndexing) where {T} = T(0)
3535
_first_index(::Type{T}, ::OneBasedIndexing) where {T} = T(1)
3636

3737
_shift(x, ::T, ::T) where {T<:AbstractIndexing} = x
38-
_shift(x::Integer, ::ZeroBasedIndexing, ::OneBasedIndexing) = x + 1
39-
_shift(x::Integer, ::OneBasedIndexing, ::ZeroBasedIndexing) = x - 1
40-
_shift(x::Array{<:Integer}, ::ZeroBasedIndexing, ::OneBasedIndexing) = x .+ 1
38+
_shift(x::Integer, ::ZeroBasedIndexing, ::OneBasedIndexing) = x + one(typeof(x))
39+
_shift(x::Integer, ::OneBasedIndexing, ::ZeroBasedIndexing) = x - one(typeof(x))
40+
function _shift(x::Array{<:Integer}, ::ZeroBasedIndexing, ::OneBasedIndexing)
41+
return x .+ one(eltype(x))
42+
end
4143

4244
"""
4345
mutable struct MutableSparseMatrixCSC{Tv,Ti<:Integer,I<:AbstractIndexing}

test/Utilities/sparse_matrix.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ function test_ScalarAffine_OneBased()
235235
return
236236
end
237237

238+
function test_MutableSparseMatrixCSC_convert_to_Cint()
239+
I = MOI.Utilities.ZeroBasedIndexing
240+
A = MOI.Utilities.MutableSparseMatrixCSC{Float32,Int32,I}()
241+
B = convert(SparseArrays.SparseMatrixCSC{Float32,Int32}, A)
242+
@test B isa SparseArrays.SparseMatrixCSC{Float32,Int32}
243+
@test isempty(B)
244+
return
245+
end
246+
238247
end
239248

240249
TestSparseMatrix.runtests()

0 commit comments

Comments
 (0)