Skip to content

Commit 35eb302

Browse files
committed
add: [CartesianIndex...] sparse method
A method for specifying the indexes to fill with values directly as CartesianIndices.
1 parent ce852af commit 35eb302

2 files changed

Lines changed: 6 additions & 0 deletions

File tree

src/sparsematrix.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,11 @@ 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}
1115+
IJ′ = reinterpret(Int, reshape(IJ, 1, :))
1116+
return sparse(view(IJ′, 1, :), view(IJ′, 2, :), V, m, n)
1117+
end
1118+
11141119
"""
11151120
sparse!(I::AbstractVector{Ti}, J::AbstractVector{Ti}, V::AbstractVector{Tv},
11161121
m::Integer, n::Integer, combine, klasttouch::Vector{Ti},

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 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))
5556
# with combine
5657
@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)
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, 1.0], 2, 2)

0 commit comments

Comments
 (0)