Skip to content

Commit

Permalink
Add special interval constructors (#446)
Browse files Browse the repository at this point in the history
* Add special interval constructors

* rename to zer
  • Loading branch information
mforets committed Mar 31, 2021
1 parent a591cc2 commit 5f3114d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/IntervalArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Base:
+, -, *, /, //, fma,
<, >, ==, !=, , ^, <=,
in, zero, one, eps, typemin, typemax, abs, abs2, real, min, max,
sqrt, exp, log, sin, cos, tan, cot, inv, cbrt, csc, hypot, sec,
sqrt, exp, log, sin, cos, tan, cot, inv, cbrt, csc, hypot, sec,
exp2, exp10, log2, log10,
asin, acos, atan,
sinh, cosh, tanh, coth, csch, sech, asinh, acosh, atanh, sinpi, cospi,
Expand Down Expand Up @@ -86,7 +86,7 @@ import Base: rounding, setrounding, setprecision

## Multidimensional
export
IntervalBox
IntervalBox, symmetric_box

## Decorations
export
Expand Down
16 changes: 16 additions & 0 deletions src/multidim/intervalbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,19 @@ end

hull(a::IntervalBox{N,T}, b::IntervalBox{N,T}) where {N,T} = IntervalBox(hull.(a[:], b[:]))
hull(a::Vector{IntervalBox{N,T}}) where {N,T} = hull(a...)

"""
zero(IntervalBox{N, T})
Return the zero interval box of dimension `N` in the numeric type `T`.
"""
zero(::Type{IntervalBox{N, T}}) where {N, T} = IntervalBox(zero(Interval{T}), N)
zero(x::IntervalBox{N, T}) where {N, T} = zero(typeof(x))

"""
symmetric_box(N, T)
Return the symmetric interval box of dimension `N` in the numeric type `T`,
each side is `Interval(-1, 1)`.
"""
symmetric_box(N, ::Type{T}) where T<:Real = IntervalBox(Interval{T}(-1, 1), N)
5 changes: 5 additions & 0 deletions test/interval_tests/construction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,8 @@ import IntervalArithmetic: force_interval
@test force_interval(Inf, -Inf) == Interval(-Inf, Inf)
@test_throws ArgumentError force_interval(NaN, 3)
end

@testset "Zero interval" begin
@test zero(Interval{Float64}) === Interval{Float64}(0, 0)
@test zero(0 .. 1) === Interval{Float64}(0, 0)
end
6 changes: 6 additions & 0 deletions test/multidim_tests/multidim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,10 @@ end
@test hull(vb4) == ib4
end

@testset "Special box constructors" begin
@test zero(IntervalBox{2, Float64}) === IntervalBox(0 .. 0, 2)
@test zero((0..1) × (0..1)) === IntervalBox(0 .. 0, 2)
@test symmetric_box(2, Float64) === IntervalBox(-1 .. 1, 2)
end

end

0 comments on commit 5f3114d

Please sign in to comment.