Skip to content

Commit a3dc27f

Browse files
authored
Merge pull request #467 from JuliaIntervals/lb/mince
Improve mince and add method for non-uniform partition of IntervalBox
2 parents c7602f4 + 430ed81 commit a3dc27f

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

src/intervals/arithmetic.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ convert(::Type{Integer}, a::Interval) = isinteger(a) ?
575575
Splits `x` in `n` intervals of the same diameter, which are returned
576576
as a vector.
577577
"""
578-
function mince(x::Interval, n)
578+
@inline function mince(x::Interval, n)
579579
nodes = range(x.lo, x.hi, length = n+1)
580-
return [Interval(nodes[i], nodes[i+1]) for i in 1:length(nodes)-1]
580+
return Interval.(nodes[1:n], nodes[2:n+1])
581581
end

src/multidim/intervalbox.jl

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,29 +115,33 @@ Base.:(==)(x::IntervalBox, y::IntervalBox) = x.v == y.v
115115

116116

117117
"""
118-
mince(x::IntervalBox, n)
118+
mince(x::IntervalBox, n::Int)
119119
120120
Splits `x` in `n` intervals in each dimension of the same diameter. These
121121
intervals are combined in all possible `IntervalBox`-es, which are returned
122122
as a vector.
123123
"""
124-
@generated function mince(x::IntervalBox{N,T}, n) where {N,T}
125-
quote
126-
nodes_matrix = Array{Interval{T},2}(undef, n, N)
127-
for i in 1:N
128-
nodes_matrix[1:n,i] .= mince(x[i], n)
129-
end
130-
131-
nodes = IntervalBox{$N,T}[]
132-
Base.Cartesian.@nloops $N i _->(1:n) begin
133-
Base.Cartesian.@nextract $N ival d->nodes_matrix[i_d, d]
134-
ibox = Base.Cartesian.@ncall $N IntervalBox ival
135-
push!(nodes, ibox)
136-
end
137-
nodes
124+
@inline mince(x::IntervalBox{N,T}, n::Int) where {N,T} =
125+
mince(x, ntuple(_ -> n, N))
126+
127+
"""
128+
mince(x::IntervalBox, ncuts::::NTuple{N,Int})
129+
130+
Splits `x[i]` in `ncuts[i]` intervals . These intervals are
131+
combined in all possible `IntervalBox`-es, which are returned
132+
as a vector.
133+
"""
134+
@inline function mince(x::IntervalBox{N,T}, ncuts::NTuple{N,Int}) where {N,T}
135+
minced_intervals = [mince(x[i], ncuts[i]) for i in 1:N]
136+
minced_boxes = Vector{IntervalBox{N,T}}(undef, prod(ncuts))
137+
138+
for (k, cut_indices) in enumerate(CartesianIndices(ncuts))
139+
minced_boxes[k] = IntervalBox([minced_intervals[i][cut_indices[i]] for i in 1:N])
138140
end
141+
return minced_boxes
139142
end
140143

144+
141145
hull(a::IntervalBox{N,T}, b::IntervalBox{N,T}) where {N,T} = IntervalBox(hull.(a[:], b[:]))
142146
hull(a::Vector{IntervalBox{N,T}}) where {N,T} = hull(a...)
143147

test/multidim_tests/multidim.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,18 +306,28 @@ end
306306
@test vb2 == vv
307307
@test hull(vb2...) == ib2
308308
@test hull(vb2) == ib2
309+
@test mince(ib2, (4,4)) == vb2
310+
@test mince(ib2, (1,4)) == [ (-1 .. 1)×(-1 .. -0.5), (-1 .. 1)×(-0.5 .. 0),
311+
(-1 .. 1)×(0 .. 0.5), (-1 .. 1)×(0.5 .. 1)]
312+
@test hull(mince(ib2, (1,4))) == ib2
309313

310314
ib3 = IntervalBox(-1..1, 3)
311315
vb3 = mince(ib3, 4)
312316
@test length(vb3) == 4^3
313317
@test hull(vb3...) == ib3
314318
@test hull(vb3) == ib3
319+
@test mince(ib3, (4,4,4)) == vb3
320+
@test mince(ib3, (2,1,1)) == [(-1 .. 0)×(-1 .. 1)×(-1 .. 1),
321+
(0 .. 1)×(-1 .. 1)×(-1 .. 1)]
322+
@test hull(mince(ib3, (2,1,1))) == ib3
315323

316324
ib4 = IntervalBox(-1..1, 4)
317325
vb4 = mince(ib4, 4)
318326
@test length(vb4) == 4^4
319327
@test hull(vb4...) == ib4
320328
@test hull(vb4) == ib4
329+
@test mince(ib4,(4,4,4,4)) == vb4
330+
@test mince(ib4,(1,1,1,1)) == [ib4]
321331
end
322332

323333
@testset "Special box constructors" begin

0 commit comments

Comments
 (0)