Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error instead of widening index types in sparse #33083

Merged
merged 1 commit into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -631,9 +631,8 @@ function sparse(I::AbstractVector{Ti}, J::AbstractVector{Ti}, V::AbstractVector{
"length(I) (=$(length(I))) == length(J) (= $(length(J))) == length(V) (= ",
"$(length(V)))")))
end
Tj = Ti
while isbitstype(Tj) && coolen >= typemax(Tj)
Tj = widen(Tj)
if Base.hastypemax(Ti) && coolen >= typemax(Ti)
throw(ArgumentError("the index type $Ti cannot hold $coolen elements; use a larger index type"))
end
if m == 0 || n == 0 || coolen == 0
if coolen != 0
Expand All @@ -646,13 +645,13 @@ function sparse(I::AbstractVector{Ti}, J::AbstractVector{Ti}, V::AbstractVector{
SparseMatrixCSC(m, n, fill(one(Ti), n+1), Vector{Ti}(), Vector{Tv}())
else
# Allocate storage for CSR form
csrrowptr = Vector{Tj}(undef, m+1)
csrrowptr = Vector{Ti}(undef, m+1)
csrcolval = Vector{Ti}(undef, coolen)
csrnzval = Vector{Tv}(undef, coolen)

# Allocate storage for the CSC form's column pointers and a necessary workspace
csccolptr = Vector{Ti}(undef, n+1)
klasttouch = Vector{Tj}(undef, n)
klasttouch = Vector{Ti}(undef, n)

# Allocate empty arrays for the CSC form's row and nonzero value arrays
# The parent method called below automagically resizes these arrays
Expand Down
6 changes: 3 additions & 3 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2664,14 +2664,14 @@ end
J1 = Int8.(rand(1:10, 500))
V1 = ones(500)
# m * n < typemax(Ti) and length(I) >= typemax(Ti) - combining values
@test sparse(I1, J1, V1, 10, 10) !== nothing
@test_throws ArgumentError sparse(I1, J1, V1, 10, 10)
# m * n >= typemax(Ti) and length(I) >= typemax(Ti)
@test sparse(I1, J1, V1, 12, 13) !== nothing
@test_throws ArgumentError sparse(I1, J1, V1, 12, 13)
I1 = Int8.(rand(1:10, 126))
J1 = Int8.(rand(1:10, 126))
V1 = ones(126)
# m * n >= typemax(Ti) and length(I) < typemax(Ti)
@test sparse(I1, J1, V1, 100, 100) !== nothing
@test size(sparse(I1, J1, V1, 100, 100)) == (100,100)
end

@testset "Typecheck too strict #31435" begin
Expand Down