Skip to content

Commit 0021a71

Browse files
committed
fix: Only allow 2D Cartesian indices in constructor
NOTE: `CartesianIndex{2}` is no longer an abstract type so the function no longer needs to be parametric.
1 parent 35eb302 commit 0021a71

2 files changed

Lines changed: 2 additions & 1 deletion

File tree

src/sparsematrix.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ end
11111111
sparse(I::AbstractVector, J::AbstractVector, V::AbstractVector, m::Integer, n::Integer, combine) =
11121112
sparse(AbstractVector{Int}(I), AbstractVector{Int}(J), V, m, n, combine)
11131113

1114-
function sparse(IJ::AbstractVector{Ci}, V::AbstractVector, m::Integer, n::Integer) where {Ci<:CartesianIndex}
1114+
function sparse(IJ::AbstractVector{CartesianIndex{2}}, V::AbstractVector, m::Integer, n::Integer)
11151115
IJ′ = reinterpret(Int, reshape(IJ, 1, :))
11161116
return sparse(view(IJ′, 1, :), view(IJ′, 2, :), V, m, n)
11171117
end

test/sparsematrix_constructors_indexing.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ end
5252
SparseMatrixCSC(6, 6, big.([1,2,4,7,8,9,9]), big.([1,1,2,1,2,3,4,5]), big.([1,2,3,4,5,6,7,8])))
5353
@test sparse(Any[1,2,3], Any[1,2,3], Any[1,1,1]) == sparse([1,2,3], [1,2,3], [1,1,1])
5454
@test sparse(Any[1,2,3], Any[1,2,3], Any[1,1,1], 5, 4) == sparse([1,2,3], [1,2,3], [1,1,1], 5, 4)
55+
@test_throws MethodError sparse([CartesianIndex(1, 1, 1), CartesianIndex(2, 2, 2), CartesianIndex(3, 3, 3)], [1, 2, 4], [1, 2, 3], 3, 3) # Only 2D indices should work
5556
@test isequal(sparse([CartesianIndex(1, 1), CartesianIndex(2, 2), CartesianIndex(3, 3)], [1.0, 2.0, 3.0], 3, 3), sparse([1, 2, 3], [1, 2, 3], [1.0, 2.0, 3.0], 3, 3))
5657
# with combine
5758
@test sparse([1, 1, 2, 2, 2], [1, 2, 1, 2, 2], 1.0, 2, 2, +) == sparse([1, 1, 2, 2], [1, 2, 1, 2], [1.0, 1.0, 1.0, 2.0], 2, 2)

0 commit comments

Comments
 (0)