Skip to content

Commit 5f3114d

Browse files
authored
Add special interval constructors (#446)
* Add special interval constructors * rename to zer
1 parent a591cc2 commit 5f3114d

File tree

4 files changed

+29
-2
lines changed

4 files changed

+29
-2
lines changed

src/IntervalArithmetic.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import Base:
2222
+, -, *, /, //, fma,
2323
<, >, ==, !=, , ^, <=,
2424
in, zero, one, eps, typemin, typemax, abs, abs2, real, min, max,
25-
sqrt, exp, log, sin, cos, tan, cot, inv, cbrt, csc, hypot, sec,
25+
sqrt, exp, log, sin, cos, tan, cot, inv, cbrt, csc, hypot, sec,
2626
exp2, exp10, log2, log10,
2727
asin, acos, atan,
2828
sinh, cosh, tanh, coth, csch, sech, asinh, acosh, atanh, sinpi, cospi,
@@ -86,7 +86,7 @@ import Base: rounding, setrounding, setprecision
8686

8787
## Multidimensional
8888
export
89-
IntervalBox
89+
IntervalBox, symmetric_box
9090

9191
## Decorations
9292
export

src/multidim/intervalbox.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,19 @@ end
140140

141141
hull(a::IntervalBox{N,T}, b::IntervalBox{N,T}) where {N,T} = IntervalBox(hull.(a[:], b[:]))
142142
hull(a::Vector{IntervalBox{N,T}}) where {N,T} = hull(a...)
143+
144+
"""
145+
zero(IntervalBox{N, T})
146+
147+
Return the zero interval box of dimension `N` in the numeric type `T`.
148+
"""
149+
zero(::Type{IntervalBox{N, T}}) where {N, T} = IntervalBox(zero(Interval{T}), N)
150+
zero(x::IntervalBox{N, T}) where {N, T} = zero(typeof(x))
151+
152+
"""
153+
symmetric_box(N, T)
154+
155+
Return the symmetric interval box of dimension `N` in the numeric type `T`,
156+
each side is `Interval(-1, 1)`.
157+
"""
158+
symmetric_box(N, ::Type{T}) where T<:Real = IntervalBox(Interval{T}(-1, 1), N)

test/interval_tests/construction.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,3 +370,8 @@ import IntervalArithmetic: force_interval
370370
@test force_interval(Inf, -Inf) == Interval(-Inf, Inf)
371371
@test_throws ArgumentError force_interval(NaN, 3)
372372
end
373+
374+
@testset "Zero interval" begin
375+
@test zero(Interval{Float64}) === Interval{Float64}(0, 0)
376+
@test zero(0 .. 1) === Interval{Float64}(0, 0)
377+
end

test/multidim_tests/multidim.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,4 +314,10 @@ end
314314
@test hull(vb4) == ib4
315315
end
316316

317+
@testset "Special box constructors" begin
318+
@test zero(IntervalBox{2, Float64}) === IntervalBox(0 .. 0, 2)
319+
@test zero((0..1) × (0..1)) === IntervalBox(0 .. 0, 2)
320+
@test symmetric_box(2, Float64) === IntervalBox(-1 .. 1, 2)
321+
end
322+
317323
end

0 commit comments

Comments
 (0)