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

✨ Implement size for JuMPArray #1595

Merged
merged 3 commits into from
Nov 10, 2018
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
6 changes: 4 additions & 2 deletions src/JuMPArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ Base.setindex!(A::JuMPArray, v, idx::CartesianIndex) = A.data[idx] = v

# AbstractArray interface

# We don't define size because it causes 'end' to behave incorrectly. Better to error.
Base.size(A::JuMPArray) = error("JuMPArray does not define size().")
if VERSION < v"0.7-"
# We don't define size because it causes 'end' to behave incorrectly. Better
# to error.
Base.size(A::JuMPArray) = error("JuMPArray does not define size().")
Base.linearindices(A::JuMPArray) = error("JuMPArray does not support this operation.")
Base.indices(A::JuMPArray) = A.axes
else
Base.size(A::JuMPArray) = size(A.data)
Base.LinearIndices(A::JuMPArray) = error("JuMPArray does not support this operation.")
Base.axes(A::JuMPArray) = A.axes
end
Expand Down
23 changes: 23 additions & 0 deletions test/containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ end
@testset "JuMPArray" begin
@testset "Range index set" begin
A = @inferred JuMPArray([1.0,2.0], 2:3)
if VERSION >= v"0.7-"
@test size(A) == (2,)
@test size(A, 1) == 2
end
@test @inferred A[2] == 1.0
@test A[3] == 2.0
@test A[2,1] == 1.0
Expand All @@ -111,6 +115,10 @@ And data, a 2-element Array{Float64,1}:

@testset "Symbol index set" begin
A = @inferred JuMPArray([1.0,2.0], [:a, :b])
if VERSION >= v"0.7-"
@test size(A) == (2,)
@test size(A, 1) == 2
end
@test @inferred A[:a] == 1.0
@test A[:b] == 2.0
@test length.(Compat.axes(A)) == (2,)
Expand All @@ -128,6 +136,11 @@ And data, a 2-element Array{Float64,1}:

@testset "Mixed range/symbol index sets" begin
A = @inferred JuMPArray([1 2; 3 4], 2:3, [:a, :b])
if VERSION >= v"0.7-"
@test size(A) == (2, 2)
@test size(A, 1) == 2
@test size(A, 2) == 2
end
@test length.(Compat.axes(A)) == (2,2)
@test @inferred A[2,:a] == 1
@test A[3,:a] == 3
Expand Down Expand Up @@ -155,6 +168,13 @@ And data, a 2×2 Array{$Int,2}:
A = @inferred JuMPArray(zeros(2,2,2,2), 2:3, [:a, :b], -1:0,
["a","b"])
end
if VERSION >= v"0.7-"
@test size(A) == (2, 2, 2, 2)
@test size(A, 1) == 2
@test size(A, 2) == 2
@test size(A, 3) == 2
@test size(A, 4) == 2
end
A[2,:a,-1,"a"] = 1.0
f = 0.0
for I in eachindex(A)
Expand Down Expand Up @@ -217,6 +237,9 @@ And data, a 2×2×2×2 Array{Float64,4}:
a = Array{Int,0}(undef)
a[] = 10
A = JuMPArray(a)
if VERSION >= v"0.7-"
@test size(A) == tuple()
end
@test A[] == 10
A[] = 1
@test sprint(show, A) == """
Expand Down
5 changes: 3 additions & 2 deletions test/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,9 @@ function test_variable_end_indexing(ModelType)
@test x[end,1] == x[2, 1]
@test x[0, end-1] == x[0, 3]
@test z[end] == z[2]
# TODO: This probably isn't hard to make work.
@test_throws ErrorException x[end-1]
# TODO: It is redirected to x[11] as it is the 11th element but linear
# indexing is not supported
@test_throws KeyError x[end-1]
else
@test_throws ErrorException x[end,1]
@test_throws ErrorException x[end-1]
Expand Down