Skip to content

Commit 26e9fa7

Browse files
committed
docs: Document the CartesianIndex method of sparse
Also fixed the methods to include all of the shortcuts (defaults) that the multiple vector method supports.
1 parent 0021a71 commit 26e9fa7

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/sparsematrix.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@ sparse(D::Diagonal) = SparseMatrixCSC(D)
10411041

10421042
"""
10431043
sparse(I, J, V,[ m, n, combine])
1044+
sparse(IJ, V,[ m, n, combine])
10441045
10451046
Create a sparse matrix `S` of dimensions `m x n` such that `S[I[k], J[k]] = V[k]`. The
10461047
`combine` function is used to combine duplicates. If `m` and `n` are not specified, they
@@ -1061,6 +1062,12 @@ julia> Js = [1; 2; 3];
10611062
julia> Vs = [1; 2; 3];
10621063
10631064
julia> sparse(Is, Js, Vs)
1065+
3×3 SparseMatrixCSC{Int64, Int64} with 3 stored entries:
1066+
1 ⋅ ⋅
1067+
⋅ 2 ⋅
1068+
⋅ ⋅ 3
1069+
1070+
julia> sparse([CartesianIndex(i,i) for i in 1:3], Vs)
10641071
3×3 SparseMatrixCSC{Int64, Int64} with 3 stored entries:
10651072
1 ⋅ ⋅
10661073
⋅ 2 ⋅
@@ -1111,9 +1118,9 @@ end
11111118
sparse(I::AbstractVector, J::AbstractVector, V::AbstractVector, m::Integer, n::Integer, combine) =
11121119
sparse(AbstractVector{Int}(I), AbstractVector{Int}(J), V, m, n, combine)
11131120

1114-
function sparse(IJ::AbstractVector{CartesianIndex{2}}, V::AbstractVector, m::Integer, n::Integer)
1121+
function sparse(IJ::AbstractVector{CartesianIndex{2}}, args...)
11151122
IJ′ = reinterpret(Int, reshape(IJ, 1, :))
1116-
return sparse(view(IJ′, 1, :), view(IJ′, 2, :), V, m, n)
1123+
return sparse(view(IJ′, 1, :), view(IJ′, 2, :), args...)
11171124
end
11181125

11191126
"""

test/sparsematrix_constructors_indexing.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ end
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)
5555
@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
5656
@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))
57+
@test sparse([CartesianIndex(1, 1), CartesianIndex(2, 2), CartesianIndex(3, 3)], [1, 2, 3]) == sparse([1, 2, 3], [1, 2, 3], [1, 2, 3])
5758
# with combine
5859
@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)
5960
@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)