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

Use eachindex/axes instead of 1:length/1:size #3464

Merged
merged 2 commits into from
Mar 19, 2024
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
4 changes: 2 additions & 2 deletions docs/src/man/reach_zonotopes_hybrid.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function reach_hybrid(As, Ts, init, δ, μ, T, max_order, instant_transitions)
println("currently in location $loc at time $t")
R = reach_continuous(As[loc], init, δ, μ, T-t, max_order)
found_transition = false
for i in 1:length(R)-1
for i in 1:(length(R)-1)
S = R[i]
push!(res, (S, loc))
for (guard, tgt_loc) in Ts[loc]
Expand Down Expand Up @@ -130,7 +130,7 @@ The following function does that for 2-mode models.
```@example example_reach_zonotopes_hybrid
function plot_res(res)
p = plot()
for i in 1:length(res)
for i in eachindex(res)
if res[i][2] == 1
c = "blue"
elseif res[i][2] == 2
Expand Down
2 changes: 1 addition & 1 deletion src/ConcreteOperations/convex_hull.jl
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ function monotone_chain!(points::Vector{VN}; sort::Bool=true) where {N,VN<:Abstr

# build lower hull
lower = Vector{VN}()
iterator = 1:length(points)
iterator = eachindex(points)
build_hull!(lower, iterator, points)

# build upper hull
Expand Down
2 changes: 1 addition & 1 deletion src/ConcreteOperations/intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ function intersection(P1::AbstractHPolygon, P2::AbstractHPolygon;
i1 = 1
i2 = 1
duplicates = 0
for i in 1:length(c)
for i in eachindex(c)
if c1[i1].a <= c2[i2].a
if c2[i2].a <= c1[i1].a
duplicates += 1
Expand Down
4 changes: 2 additions & 2 deletions src/ConcreteOperations/isdisjoint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ function isdisjoint(H1::HalfSpace, H2::HalfSpace, witness::Bool=false)
end
# compute witness
v = zeros(N, length(a1))
for i in 1:length(a1)
for i in eachindex(a1)
a_sum_i = a1[i] + a2[i]
if a_sum[i] != 0
v[i] = (H1.b + H2.b) / a_sum_i
Expand Down Expand Up @@ -1100,7 +1100,7 @@ function isdisjoint(X::CartesianProductArray, Y::CartesianProductArray,
@assert same_block_structure(array(X), array(Y)) "block structure has to " *
"be identical"

for i in 1:length(X.array)
for i in eachindex(X.array)
if isdisjoint(X.array[i], Y.array[i])
return _witness_result_empty(witness, true, X, Y)
end
Expand Down
4 changes: 2 additions & 2 deletions src/ConcreteOperations/issubset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1210,15 +1210,15 @@ function ⊆(X::CartesianProductArray, Y::CartesianProductArray,
end

N = promote_type(eltype(X), eltype(Y))
for i in 1:length(aX)
for i in eachindex(aX)
result = ⊆(aX[i], aY[i], witness)
if !witness && !result
return false
elseif witness && !result[1]
# construct a witness
w = Vector{N}(undef, dim(X))
k = 1
for j in 1:length(aX)
for j in eachindex(aX)
Xj = aX[j]
l = k + dim(Xj)
w[k:(l - 1)] = j == i ? result[2] : an_element(Xj)
Expand Down
4 changes: 2 additions & 2 deletions src/Interfaces/AbstractHyperrectangle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ function vertices_list(H::AbstractHyperrectangle; kwargs...)
vlist = Vector{typeof(c)}(undef, m)
vlist[1] = copy(v)
@inbounds for i in 2:m
for j in 1:length(v)
for j in eachindex(v)
if trivector[j] == Int8(-1)
trivector[j] = Int8(1)
v[j] = c[j] + radius_hyperrectangle(H, j)
Expand Down Expand Up @@ -750,7 +750,7 @@ A scalar representing the distance between point `x` and hyperrectangle `H`.
# compute closest point
y = similar(x)
outside = false
@inbounds for i in 1:length(x)
@inbounds for i in eachindex(x)
ci = center(H, i)
ri = radius_hyperrectangle(H, i)
d = x[i] - ci
Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/AbstractPolyhedron_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ function _affine_map_inverse_hrep(A::AbstractMatrix, P::LazySet,
end
if has_undefs # there were redundant constraints, so remove them
constraints_res = [constraints_res[i]
for i in 1:length(constraints_res)
for i in eachindex(constraints_res)
if isassigned(constraints_res, i)]
end
return constraints_res
Expand Down
10 changes: 5 additions & 5 deletions src/LazyOperations/CartesianProductArray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ function same_block_structure(x::AbstractVector{S1},
if length(x) != length(y)
return false
end
for i in 1:length(x)
for i in eachindex(x)
if dim(x[i]) != dim(y[i])
return false
end
Expand Down Expand Up @@ -577,7 +577,7 @@ function block_to_dimension_indices(cpa::CartesianProductArray{N},
constrained_blocks = 0
start_index, end_index = 1, 0
v_i = 1
@inbounds for i in 1:length(cpa.array)
@inbounds for i in eachindex(cpa.array)
end_index += dim(cpa.array[i])
if v_i <= length(vars) && vars[v_i] <= end_index
ranges[i] = (start_index, end_index)
Expand All @@ -599,7 +599,7 @@ function block_to_dimension_indices(cpa::CartesianProductArray{N}) where {N}
ranges = Vector{Tuple{Int,Int}}(undef, length(cpa.array))

start_index, end_index = 1, 0
@inbounds for i in 1:length(cpa.array)
@inbounds for i in eachindex(cpa.array)
end_index += dim(cpa.array[i])
ranges[i] = (start_index, end_index)
start_index = end_index + 1
Expand Down Expand Up @@ -633,8 +633,8 @@ function substitute_blocks(low_dim_cpa::CartesianProductArray{N},
blocks::Vector{Tuple{Int,Int}}) where {N}
array = Vector{LazySet{N}}(undef, length(orig_cpa.array))
index = 1
for bi in 1:length(orig_cpa.array)
start_ind, end_index = blocks[bi]
for bi in eachindex(orig_cpa.array)
start_ind, _ = blocks[bi]
if start_ind == -1
array[bi] = orig_cpa.array[bi]
else
Expand Down
2 changes: 1 addition & 1 deletion src/LazyOperations/Intersection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ function get_constrained_lowdimset(cpa::CartesianProductArray{N,S},
sizehint!(block_structure, non_empty_length)

last_var = 1
for i in 1:length(blocks)
for i in eachindex(blocks)
start_index, end_index = blocks[i]
block_end = last_var + end_index - start_index
if start_index != -1
Expand Down
27 changes: 12 additions & 15 deletions src/Plotting/plot_recipes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ julia> plot(Bs, 1e-3) # default accuracy value (explicitly given for clarity)
julia> plot(Bs, 1e-2) # faster but less accurate than the previous call
```
"""
@recipe function plot_list(list::AbstractVector{VN}, ε::Real=N(PLOT_PRECISION),
@recipe function plot_list(list::AbstractVector{VN}, ε::Real=N(PLOT_PRECISION), # COV_EXCL_LINE
Nφ::Int=PLOT_POLAR_DIRECTIONS;
same_recipe=false) where {N,VN<:LazySet{N}}
if same_recipe
Expand Down Expand Up @@ -227,7 +227,7 @@ function _plot_list_same_recipe(list::AbstractVector{VN}, ε::Real=N(PLOT_PRECIS
end

# recipe for vector of singletons
@recipe function plot_list(list::AbstractVector{SN}) where {N,SN<:AbstractSingleton{N}}
@recipe function plot_list(list::AbstractVector{SN}) where {N,SN<:AbstractSingleton{N}} # COV_EXCL_LINE
label --> DEFAULT_LABEL
grid --> DEFAULT_GRID
if DEFAULT_ASPECT_RATIO != :none
Expand All @@ -241,7 +241,7 @@ end
end

# plot recipe for the union of singletons
@recipe function plot_list(X::UnionSetArray{N,SN}) where {N,SN<:AbstractSingleton{N}}
@recipe function plot_list(X::UnionSetArray{N,SN}) where {N,SN<:AbstractSingleton{N}} # COV_EXCL_LINE
label --> DEFAULT_LABEL
grid --> DEFAULT_GRID
if DEFAULT_ASPECT_RATIO != :none
Expand Down Expand Up @@ -321,7 +321,7 @@ julia> plot(B, 1e-3) # default accuracy value (explicitly given for clarity her
julia> plot(B, 1e-2) # faster but less accurate than the previous call
```
"""
@recipe function plot_lazyset(X::LazySet{N}, ε::Real=N(PLOT_PRECISION)) where {N}
@recipe function plot_lazyset(X::LazySet{N}, ε::Real=N(PLOT_PRECISION)) where {N} # COV_EXCL_LINE
label --> DEFAULT_LABEL
grid --> DEFAULT_GRID
if DEFAULT_ASPECT_RATIO != :none
Expand Down Expand Up @@ -416,7 +416,7 @@ Plot a singleton.
julia> plot(Singleton([0.5, 1.0]))
```
"""
@recipe function plot_singleton(S::AbstractSingleton{N}, ε::Real=zero(N)) where {N}
@recipe function plot_singleton(S::AbstractSingleton{N}, ε::Real=zero(N)) where {N} # COV_EXCL_LINE
label --> DEFAULT_LABEL
grid --> DEFAULT_GRID
if DEFAULT_ASPECT_RATIO != :none
Expand Down Expand Up @@ -448,7 +448,7 @@ Plot an empty set.
- `∅` -- empty set
- `ε` -- (optional, default: `0`) ignored, used for dispatch
"""
@recipe function plot_emptyset(∅::EmptySet{N}, ε::Real=zero(N)) where {N}
@recipe function plot_emptyset(∅::EmptySet{N}, ::Real=zero(N)) where {N} # COV_EXCL_LINE
label --> DEFAULT_LABEL
grid --> DEFAULT_GRID
if DEFAULT_ASPECT_RATIO != :none
Expand Down Expand Up @@ -500,9 +500,8 @@ julia> plot(overapproximate(X, PolarDirections(100)))
julia> plot(X, 0.0, 100) # equivalent to the above line
```
"""
@recipe function plot_intersection(cap::Intersection{N},
ε::Real=zero(N),
Nφ::Int=PLOT_POLAR_DIRECTIONS) where {N}
@recipe function plot_intersection(cap::Intersection{N}, ε::Real=zero(N), # COV_EXCL_LINE
::Int=PLOT_POLAR_DIRECTIONS) where {N}
label --> DEFAULT_LABEL
grid --> DEFAULT_GRID
if DEFAULT_ASPECT_RATIO != :none
Expand Down Expand Up @@ -544,9 +543,8 @@ end

# non-convex sets

@recipe function plot_union(cup::Union{UnionSet{N},UnionSetArray{N}},
ε::Real=N(PLOT_PRECISION); same_recipe=false,
Nφ=PLOT_POLAR_DIRECTIONS) where {N}
@recipe function plot_union(cup::Union{UnionSet{N},UnionSetArray{N}}, ε::Real=N(PLOT_PRECISION); # COV_EXCL_LINE
same_recipe=false, Nφ=PLOT_POLAR_DIRECTIONS) where {N}
n = dim(cup)
# extract limits and extrema of already plotted sets
p = plotattributes[:plot_object]
Expand Down Expand Up @@ -593,9 +591,8 @@ end
end
end

@recipe function plot_polyzono(P::AbstractPolynomialZonotope{N},
ε::Real=N(PLOT_PRECISION); nsdiv=10,
partition=nothing) where {N}
@recipe function plot_polyzono(P::AbstractPolynomialZonotope{N}, ε::Real=N(PLOT_PRECISION); # COV_EXCL_LINE
nsdiv=10, partition=nothing) where {N}
label --> DEFAULT_LABEL
grid --> DEFAULT_GRID
if DEFAULT_ASPECT_RATIO != :none
Expand Down
2 changes: 1 addition & 1 deletion src/Sets/HPolyhedron.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ function rand(::Type{HPolyhedron};
P = rand(HPolytope; N=N, dim=dim, rng=rng)
constraints_P = constraints_list(P)
constraints_Q = Vector{eltype(constraints_P)}()
for i in 1:length(constraints_P)
for i in eachindex(constraints_P)
if rand(rng, Bool)
push!(constraints_Q, constraints_P[i])
end
Expand Down
2 changes: 1 addition & 1 deletion src/Sets/Hyperplane.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ to ``d``.
# not the zero vector, check if it is a normal vector
N = promote_type(eltype(d), eltype(a))
factor = zero(N)
for i in 1:length(a)
for i in eachindex(a)
if a[i] == 0
if d[i] != 0
unbounded = true
Expand Down
2 changes: 1 addition & 1 deletion src/Sets/VPolygon.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function VPolygon(vertices_matrix::MT; apply_convex_hull::Bool=true,
@assert size(vertices_matrix, 1) == 2 "the number of rows of the matrix " *
"of vertices should be 2, but it is $(size(vertices_matrix, 1))"

vertices = [vertices_matrix[:, j] for j in 1:size(vertices_matrix, 2)]
vertices = [vertices_matrix[:, j] for j in axes(vertices_matrix, 2)]
return VPolygon(vertices; apply_convex_hull=apply_convex_hull,
algorithm=algorithm)
end
Expand Down
2 changes: 1 addition & 1 deletion src/Sets/VPolytope.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ VPolytope() = VPolytope{Float64}()

# constructor from rectangular matrix
function VPolytope(vertices_matrix::MT) where {N,MT<:AbstractMatrix{N}}
vertices = [vertices_matrix[:, j] for j in 1:size(vertices_matrix, 2)]
vertices = [vertices_matrix[:, j] for j in axes(vertices_matrix, 2)]
return VPolytope(vertices)
end

Expand Down
2 changes: 1 addition & 1 deletion test/LazyOperations/UnionSet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ for N in [Float64, Rational{Int}, Float32]
@test isequivalent(Y.X, linear_map(A, UXY.X))
@test isequivalent(Y.Y, linear_map(A, UXY.Y))
Y = linear_map(A, Uarr)
for k in 1:length(array(Uarr))
for k in eachindex(array(Uarr))
@test isequivalent(array(Y)[k], linear_map(A, array(Uarr)[k]))
end

Expand Down
Loading