From 27da7fad85176de3ab2e6fde2830381f20bbbe17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 18 Jun 2018 10:42:40 +0200 Subject: [PATCH 1/9] CartesianRange -> CartesianIndices --- src/JuMPArray.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/JuMPArray.jl b/src/JuMPArray.jl index 0419abdc5ef..260f813248a 100644 --- a/src/JuMPArray.jl +++ b/src/JuMPArray.jl @@ -108,7 +108,7 @@ Base.isassigned(A::JuMPArray{T,N}, idx...) where {T,N} = length(idx) == N && all # For ambiguity Base.isassigned(A::JuMPArray{T,N}, idx::Int...) where {T,N} = length(idx) == N && all(t -> haskey(A.lookup[t[1]], t[2]), enumerate(idx)) -Base.eachindex(A::JuMPArray) = CartesianRange(size(A.data)) +Base.eachindex(A::JuMPArray) = CartesianIndices(size(A.data)) # TODO: similar @@ -160,7 +160,7 @@ function Base.show_nd(io::IO, a::JuMPArray, print_matrix::Function, label_slices end tailinds = Base.tail(Base.tail(indices(a.data))) nd = ndims(a)-2 - for I in CartesianRange(tailinds) + for I in CartesianIndices(tailinds) idxs = I.I if limit for i = 1:nd From ec6e1e705ee709b213e1fc025983211574924563 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 18 Jun 2018 10:44:13 +0200 Subject: [PATCH 2/9] shift! -> popfirst! --- src/macros.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/macros.jl b/src/macros.jl index 977f9eadb66..18a60cac0ac 100644 --- a/src/macros.jl +++ b/src/macros.jl @@ -21,13 +21,13 @@ function buildrefsets(expr::Expr, cname) # Creating an indexed set of refs refcall = Expr(:ref, cname) if isexpr(c, :typed_vcat) || isexpr(c, :ref) - shift!(c.args) + popfirst!(c.args) end condition = :() if isexpr(c, :vcat) || isexpr(c, :typed_vcat) if isexpr(c.args[1], :parameters) @assert length(c.args[1].args) == 1 - condition = shift!(c.args).args[1] + condition = popfirst!(c.args).args[1] else condition = pop!(c.args) end @@ -1046,7 +1046,7 @@ macro variable(args...) x = gensym() anon_singleton = true else - x = shift!(extra) + x = popfirst!(extra) if x in [:Int,:Bin,:PSD] _error("Ambiguous variable name $x detected. Use the \"category\" keyword argument to specify a category for an anonymous variable.") end From a87e1aae6eeaeeb4baa976f4cb96b3fddd3b6442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 18 Jun 2018 10:44:38 +0200 Subject: [PATCH 3/9] object_id -> objectid --- src/variables.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/variables.jl b/src/variables.jl index 45cb64270b3..edf0b155a3c 100644 --- a/src/variables.jl +++ b/src/variables.jl @@ -109,7 +109,7 @@ MOI.isvalid(m::Model, v::VariableRef) = (v.m === m) && MOI.isvalid(m.moibackend, # The default hash is slow. It's important for the performance of AffExpr to # define our own. # https://github.com/JuliaOpt/MathOptInterface.jl/issues/234#issuecomment-366868878 -Base.hash(v::VariableRef, h::UInt) = hash(object_id(v.m), hash(v.index.value, h)) +Base.hash(v::VariableRef, h::UInt) = hash(objectid(v.m), hash(v.index.value, h)) Base.isequal(v1::VariableRef, v2::VariableRef) = v1.m === v2.m && v1.index == v2.index From 0c4f4a4e78b5731e7cac96e048e56a47cc214ba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 18 Jun 2018 10:54:56 +0200 Subject: [PATCH 4/9] Add missing undef --- src/Derivatives/coloring.jl | 26 +++++++++++++------------- src/Derivatives/conversion.jl | 4 ++-- src/Derivatives/linearity.jl | 2 +- src/Derivatives/subexpressions.jl | 2 +- src/Derivatives/topological_sort.jl | 2 +- src/Derivatives/types.jl | 2 +- test/derivatives.jl | 2 +- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Derivatives/coloring.jl b/src/Derivatives/coloring.jl index 76ce34adea8..07f3138926d 100644 --- a/src/Derivatives/coloring.jl +++ b/src/Derivatives/coloring.jl @@ -82,16 +82,16 @@ function gen_adjlist(I,J,nel) adjcount[i] += 1 adjcount[j] += 1 end - offsets = Array{Int}(nel+1) + offsets = Array{Int}(undef, nel+1) offsets[1] = 1 for k in 1:nel offsets[k+1] = offsets[k] + adjcount[k] end fill!(adjcount,0) - edges = Array{MyPair{Int}}(n_edges) - adjlist = Array{Int}(offsets[nel+1]-1) - edgeindex = Array{Int}(length(adjlist)) + edges = Array{MyPair{Int}}(undef, n_edges) + adjlist = Array{Int}(undef, offsets[nel+1]-1) + edgeindex = Array{Int}(undef, length(adjlist)) edge_count = 0 for k in 1:length(I) @@ -176,7 +176,7 @@ function acyclic_coloring(g::UndirectedGraph) end num_colors = 0 forbiddenColors = Int[] - firstNeighbor = Array{Edge}(0) + firstNeighbor = Array{Edge}[] firstVisitToTree = fill(Edge(0,0,0),num_edges(g)) color = fill(0, num_vertices(g)) # disjoint set forest of edges in the graph @@ -270,7 +270,7 @@ function recovery_preprocess(g::UndirectedGraph,color,num_colors, local_indices) twocolorindex = zeros(Int32,num_colors, num_colors) seen_twocolors = 0 # count of edges in each subgraph - edge_count = Array{Int}(0) + edge_count = Array{Int}[] for k in 1:length(g.edges) e = g.edges[k] u = e.first @@ -303,10 +303,10 @@ function recovery_preprocess(g::UndirectedGraph,color,num_colors, local_indices) end # list of unique vertices in each twocolor subgraph - vertexmap = Array{Vector{Int}}(seen_twocolors) + vertexmap = Array{Vector{Int}}(undef, seen_twocolors) - postorder = Array{Vector{Int}}(seen_twocolors) - parents = Array{Vector{Int}}(seen_twocolors) + postorder = Array{Vector{Int}}(undef, seen_twocolors) + parents = Array{Vector{Int}}(undef, seen_twocolors) # temporary lookup map from global index to subgraph index revmap = zeros(Int,num_vertices(g)) @@ -342,7 +342,7 @@ function recovery_preprocess(g::UndirectedGraph,color,num_colors, local_indices) end # set up offsets for adjlist - offset = Array{Int}(length(vlist)+1) + offset = Array{Int}(undef, length(vlist)+1) offset[1] = 1 for k in 1:length(vlist) offset[k+1] = offset[k] + adjcount[vlist[k]] @@ -351,7 +351,7 @@ function recovery_preprocess(g::UndirectedGraph,color,num_colors, local_indices) # adjlist for node u in twocolor idx starts at # vec[offset[u]] # u has global index vlist[u] - vec = Array{Int}(offset[length(vlist)+1]-1) + vec = Array{Int}(undef, offset[length(vlist)+1]-1) # now fill in for k in 1:length(my_edges) @@ -471,7 +471,7 @@ end export hessian_color_preprocess # allocate a seed matrix -seed_matrix(rinfo::RecoveryInfo) = Array{Float64}(length(rinfo.local_indices),rinfo.num_colors) +seed_matrix(rinfo::RecoveryInfo) = Array{Float64}(undef,length(rinfo.local_indices),rinfo.num_colors) export seed_matrix @@ -512,7 +512,7 @@ function recover_from_matmat!(V, R, rinfo::RecoveryInfo, stored_values) vmap = rinfo.vertexmap[t] order = rinfo.postorder[t] parent = rinfo.parents[t] - stored_values[1:length(order)] = 0.0 + stored_values[1:length(order)] .= 0.0 @inbounds for z in 1:length(order) v = order[z] diff --git a/src/Derivatives/conversion.jl b/src/Derivatives/conversion.jl index a8827b3cb49..530eefdc69c 100644 --- a/src/Derivatives/conversion.jl +++ b/src/Derivatives/conversion.jl @@ -66,8 +66,8 @@ export expr_to_nodedata # edges leaving any node (i.e., the children) function adjmat(nd::Vector{NodeData}) len = length(nd) - I = Array{Int}(len) - J = Array{Int}(len) + I = Vector{Int}(undef, len) + J = Vector{Int}(undef, len) realnz = 0 for nz in 1:len par = nd[nz].parent diff --git a/src/Derivatives/linearity.jl b/src/Derivatives/linearity.jl index 549e09d5509..673fe07a111 100644 --- a/src/Derivatives/linearity.jl +++ b/src/Derivatives/linearity.jl @@ -8,7 +8,7 @@ export CONSTANT, LINEAR, PIECEWISE_LINEAR, NONLINEAR function classify_linearity(nd::Vector{NodeData},adj,subexpression_linearity) - linearity = Array{Linearity}(length(nd)) + linearity = Array{Linearity}(undef, length(nd)) # do a forward pass through the graph, which is reverse order of nd diff --git a/src/Derivatives/subexpressions.jl b/src/Derivatives/subexpressions.jl index 54308ac3795..57e645a0b36 100644 --- a/src/Derivatives/subexpressions.jl +++ b/src/Derivatives/subexpressions.jl @@ -55,7 +55,7 @@ function order_subexpressions(main_expressions::Vector{Vector{NodeData}},subexpr end N = nsub+length(main_expressions) sp = sparse(I,J,ones(length(I)),N,N) - cmap = Array{Int}(N) + cmap = Vector{Int}(undef, N) order = reverse(Coloring.reverse_topological_sort_by_dfs(sp.rowval,sp.colptr,N,cmap)[1]) # remove the subexpressions which never appear anywhere # and the indices of the main expressions diff --git a/src/Derivatives/topological_sort.jl b/src/Derivatives/topological_sort.jl index 4ff0eef01b5..d822b7a983b 100644 --- a/src/Derivatives/topological_sort.jl +++ b/src/Derivatives/topological_sort.jl @@ -31,7 +31,7 @@ mutable struct TopologicalSortVisitor parents::Vector{Int} function TopologicalSortVisitor(n::Int) - vs = Array{Int}(0) + vs = Vector{Int}[] sizehint!(vs, n) new(vs, zeros(Int,n)) end diff --git a/src/Derivatives/types.jl b/src/Derivatives/types.jl index 2dbe0a11008..9a969db6824 100644 --- a/src/Derivatives/types.jl +++ b/src/Derivatives/types.jl @@ -70,7 +70,7 @@ mutable struct UserOperatorRegistry univariate_operator_fprimeprime::Vector{Any} end -UserOperatorRegistry() = UserOperatorRegistry(Dict{Symbol,Int}(),Vector{MOI.AbstractNLPEvaluator}(0),Dict{Symbol,Int}(),[],[],[]) +UserOperatorRegistry() = UserOperatorRegistry(Dict{Symbol,Int}(),Vector{MOI.AbstractNLPEvaluator}[],Dict{Symbol,Int}(),[],[],[]) # we use the MathOptInterface NLPEvaluator interface, where the # operator takes the place of the objective function. diff --git a/test/derivatives.jl b/test/derivatives.jl index e201f5562c7..280e70be347 100644 --- a/test/derivatives.jl +++ b/test/derivatives.jl @@ -201,7 +201,7 @@ function test_linearity(ex,testval,IJ = [],indices=[]) linearity = classify_linearity(nd,adj,[]) @test linearity[1] == testval idxset = Coloring.IndexedSet(100) - edgelist = compute_hessian_sparsity(nd,adj,linearity,idxset,Array{Set{Tuple{Int,Int}}}(0), Array{Vector{Int}}(0)) + edgelist = compute_hessian_sparsity(nd,adj,linearity,idxset,Array{Set{Tuple{Int,Int}}}(undef, 0), Array{Vector{Int}}(undef, 0)) if linearity[1] != NONLINEAR @test length(edgelist) == 0 elseif length(IJ) > 0 From d979a1156ebbd1ab4be1a01801f322a4fe741b25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 18 Jun 2018 10:57:39 +0200 Subject: [PATCH 5/9] indices -> axes --- test/containers.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/containers.jl b/test/containers.jl index befc6a89f1b..84071880a43 100644 --- a/test/containers.jl +++ b/test/containers.jl @@ -25,7 +25,7 @@ function containermatches(c1::AbstractArray,c2::AbstractArray) end function containermatches(c1::JuMPArray,c2::JuMPArray) - return typeof(c1) == typeof(c2) && indices(c1) == indices(c2) + return typeof(c1) == typeof(c2) && axes(c1) == axes(c2) end containermatches(c1::Dict, c2::Dict) = (eltype(c1) == eltype(c2)) @@ -79,7 +79,7 @@ containermatches(c1, c2) = false @test_throws ErrorException @dummycontainer([i=1:10; iseven(i)], JuMPArray) @test containermatches(@dummycontainer([i=1:10; iseven(i)], Dict), Dict{Any,Bool}()) - # Dependent indices + # Dependent axes @test containermatches(@dummycontainer([i=1:10, j=1:i], Auto), Dict{Any,Bool}()) @test_throws ErrorException @dummycontainer([i=1:10, j=1:i], Array) @test_throws ErrorException @dummycontainer([i=1:10, j=1:i], JuMPArray) @@ -99,7 +99,7 @@ end @test A[3,1,1,1,1] == 2.0 @test isassigned(A, 2) @test !isassigned(A, 1) - @test length.(indices(A)) == (2,) + @test length.(axes(A)) == (2,) B = plus1.(A) @test B[2] == 2.0 @test B[3] == 3.0 @@ -113,7 +113,7 @@ And data, a 2-element Array{Float64,1}: A = @inferred JuMPArray([1.0,2.0], s2) @test @inferred A[:a] == 1.0 @test A[:b] == 2.0 - @test length.(indices(A)) == (2,) + @test length.(axes(A)) == (2,) B = plus1.(A) @test B[:a] == 2.0 @test B[:b] == 3.0 @@ -125,7 +125,7 @@ And data, a 2-element Array{Float64,1}: 3.0""" A = @inferred JuMPArray([1 2; 3 4], s1, s2) - @test length.(indices(A)) == (2,2) + @test length.(axes(A)) == (2,2) @test @inferred A[2,:a] == 1 @test A[3,:a] == 3 @test A[2,:b] == 2 From 33d25a6245514f2d9058293641a15200c94c98ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 18 Jun 2018 11:06:28 +0200 Subject: [PATCH 6/9] Add using Compat in coloring --- src/Derivatives/coloring.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Derivatives/coloring.jl b/src/Derivatives/coloring.jl index 07f3138926d..177b90615f2 100644 --- a/src/Derivatives/coloring.jl +++ b/src/Derivatives/coloring.jl @@ -1,5 +1,7 @@ module Coloring +using Compat + import DataStructures include("topological_sort.jl") From fdd2334a62458b6826f3cac4aa688eb16b1cebfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 18 Jun 2018 11:16:52 +0200 Subject: [PATCH 7/9] Fixes --- src/Derivatives/coloring.jl | 4 ++-- src/Derivatives/topological_sort.jl | 2 +- src/Derivatives/types.jl | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Derivatives/coloring.jl b/src/Derivatives/coloring.jl index 177b90615f2..857ff7aa3d6 100644 --- a/src/Derivatives/coloring.jl +++ b/src/Derivatives/coloring.jl @@ -178,7 +178,7 @@ function acyclic_coloring(g::UndirectedGraph) end num_colors = 0 forbiddenColors = Int[] - firstNeighbor = Array{Edge}[] + firstNeighbor = Edge[] firstVisitToTree = fill(Edge(0,0,0),num_edges(g)) color = fill(0, num_vertices(g)) # disjoint set forest of edges in the graph @@ -272,7 +272,7 @@ function recovery_preprocess(g::UndirectedGraph,color,num_colors, local_indices) twocolorindex = zeros(Int32,num_colors, num_colors) seen_twocolors = 0 # count of edges in each subgraph - edge_count = Array{Int}[] + edge_count = Int[] for k in 1:length(g.edges) e = g.edges[k] u = e.first diff --git a/src/Derivatives/topological_sort.jl b/src/Derivatives/topological_sort.jl index d822b7a983b..c63a5439f01 100644 --- a/src/Derivatives/topological_sort.jl +++ b/src/Derivatives/topological_sort.jl @@ -31,7 +31,7 @@ mutable struct TopologicalSortVisitor parents::Vector{Int} function TopologicalSortVisitor(n::Int) - vs = Vector{Int}[] + vs = Int[] sizehint!(vs, n) new(vs, zeros(Int,n)) end diff --git a/src/Derivatives/types.jl b/src/Derivatives/types.jl index 9a969db6824..9a4d34c0c77 100644 --- a/src/Derivatives/types.jl +++ b/src/Derivatives/types.jl @@ -70,7 +70,7 @@ mutable struct UserOperatorRegistry univariate_operator_fprimeprime::Vector{Any} end -UserOperatorRegistry() = UserOperatorRegistry(Dict{Symbol,Int}(),Vector{MOI.AbstractNLPEvaluator}[],Dict{Symbol,Int}(),[],[],[]) +UserOperatorRegistry() = UserOperatorRegistry(Dict{Symbol,Int}(),MOI.AbstractNLPEvaluator[],Dict{Symbol,Int}(),[],[],[]) # we use the MathOptInterface NLPEvaluator interface, where the # operator takes the place of the objective function. From d0b69d1c2c152a9ca2061a5989a92231e2500e32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 18 Jun 2018 11:19:10 +0200 Subject: [PATCH 8/9] More undef fixes --- src/Derivatives/coloring.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Derivatives/coloring.jl b/src/Derivatives/coloring.jl index 857ff7aa3d6..af2c4c02df0 100644 --- a/src/Derivatives/coloring.jl +++ b/src/Derivatives/coloring.jl @@ -288,9 +288,9 @@ function recovery_preprocess(g::UndirectedGraph,color,num_colors, local_indices) edge_count[idx] += 1 end # edges sorted by twocolor subgraph - sorted_edges = Array{Vector{MyPair{Int}}}(seen_twocolors) + sorted_edges = Array{Vector{MyPair{Int}}}(undef, seen_twocolors) for idx in 1:seen_twocolors - sorted_edges[idx] = Array{MyPair{Int}}(0) + sorted_edges[idx] = MyPair{Int}[] sizehint!(sorted_edges[idx],edge_count[idx]) end From 4e6e6534c94aee3fedb306a36e893944c6f7e2e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Mon, 18 Jun 2018 11:27:43 +0200 Subject: [PATCH 9/9] axes -> Compat.axes --- test/containers.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/containers.jl b/test/containers.jl index 84071880a43..c490b75422f 100644 --- a/test/containers.jl +++ b/test/containers.jl @@ -25,7 +25,7 @@ function containermatches(c1::AbstractArray,c2::AbstractArray) end function containermatches(c1::JuMPArray,c2::JuMPArray) - return typeof(c1) == typeof(c2) && axes(c1) == axes(c2) + return typeof(c1) == typeof(c2) && Compat.axes(c1) == Compat.axes(c2) end containermatches(c1::Dict, c2::Dict) = (eltype(c1) == eltype(c2)) @@ -99,7 +99,7 @@ end @test A[3,1,1,1,1] == 2.0 @test isassigned(A, 2) @test !isassigned(A, 1) - @test length.(axes(A)) == (2,) + @test length.(Compat.axes(A)) == (2,) B = plus1.(A) @test B[2] == 2.0 @test B[3] == 3.0 @@ -113,7 +113,7 @@ And data, a 2-element Array{Float64,1}: A = @inferred JuMPArray([1.0,2.0], s2) @test @inferred A[:a] == 1.0 @test A[:b] == 2.0 - @test length.(axes(A)) == (2,) + @test length.(Compat.axes(A)) == (2,) B = plus1.(A) @test B[:a] == 2.0 @test B[:b] == 3.0 @@ -125,7 +125,7 @@ And data, a 2-element Array{Float64,1}: 3.0""" A = @inferred JuMPArray([1 2; 3 4], s1, s2) - @test length.(axes(A)) == (2,2) + @test length.(Compat.axes(A)) == (2,2) @test @inferred A[2,:a] == 1 @test A[3,:a] == 3 @test A[2,:b] == 2