Skip to content

Commit

Permalink
Finish fixing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Kolaru committed Jul 28, 2020
1 parent 2fcb8ca commit 9b46da7
Show file tree
Hide file tree
Showing 16 changed files with 451 additions and 431 deletions.
2 changes: 1 addition & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Review intervals folder:
☐ {F <: Type} -> {F<:Type} (remove spaces)
✔ Replace "IEEE standard" -> "IEEE Std 1788-2015" @done(19-08-22 16:13)
✔ "Corresponds to" -> "Implement" in doc @done(19-08-22 16:13)
✔ Rename `entireinterval` -> `RR` @done(19-08-19 19:14)
✔ Rename `RR` -> `RR` @done(19-08-19 19:14)
☐ Investigate compile time warnings
✔ Check if intervals of different flavor type can/should be equal in the sense of `isidentical` @done(19-12-08 01:12)
☐ Operation on different flavors of interval should always error.
Expand Down
2 changes: 1 addition & 1 deletion src/IntervalArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export
precedes, strictprecedes, , , , , contains_zero,
isidentical, isdistinct,
RR, isentire, nai, isnai, isthin, iscommon, isatomic,
widen, inf, sup, bisect,
widen, inf, sup, bisect, mince,
eps, dist,
midpoint_radius, interval_from_midpoint_radius,
RoundTiesToEven, RoundTiesToAway,
Expand Down
35 changes: 35 additions & 0 deletions src/bisect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,38 @@ function bisect(X::IntervalBox, i::Integer, α=where_bisect)

return (X1, X2)
end

"""
mince(x::Interval, n)
Splits `x` in `n` intervals of the same diameter, which are returned
as a vector.
"""
function mince(x::Interval, n)
nodes = range(x.lo, x.hi, length = n+1)
return [Interval(nodes[i], nodes[i+1]) for i in 1:length(nodes)-1]
end

"""
mince(x::IntervalBox, n)
Splits `x` in `n` intervals in each dimension of the same diameter. These
intervals are combined in all possible `IntervalBox`-es, which are returned
as a vector.
"""
@generated function mince(x::IntervalBox{N,T}, n) where {N,T}
quote
nodes_matrix = Array{Interval{T},2}(undef, n, N)
for i in 1:N
nodes_matrix[1:n,i] .= mince(x[i], n)
end

nodes = IntervalBox{$N,T}[]
Base.Cartesian.@nloops $N i _->(1:n) begin
Base.Cartesian.@nextract $N ival d->nodes_matrix[i_d, d]
ibox = Base.Cartesian.@ncall $N IntervalBox ival
push!(nodes, ibox)
end
nodes
end
end
2 changes: 1 addition & 1 deletion src/intervals/common/arithmetic/power.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ for f in (:exp, :expm1)
end
end

for f in (:exp2, :exp10)
for f in (:exp2, :exp10, :cbrt)
@eval function ($f)(x::BigFloat, r::RoundingMode) # add BigFloat functions with rounding:
setrounding(BigFloat, r) do
($f)(x)
Expand Down
12 changes: 12 additions & 0 deletions src/intervals/common/arithmetic/trigonometric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ function sin(a::F) where {F<:AbstractFlavor{Float64}}
end
end

function sinpi(a::AbstractFlavor{T}) where T
isempty(a) && return a
w = a * Interval{T}(π)
return(sin(w))
end

"""
cos(a::AbstractFlavor)
Expand Down Expand Up @@ -223,6 +229,12 @@ function cos(a::F) where {F<:AbstractFlavor{Float64}}
end
end

function cospi(a::AbstractFlavor{T}) where T
isempty(a) && return a
w = a * Interval{T}(π)
return(cos(w))
end

"""
tan(a::AbstractFlavor)
Expand Down
4 changes: 2 additions & 2 deletions src/intervals/common/constants.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ RR(::F) where {T, F <: AbstractFlavor{T}} = RR(F)
RR(::Type{T}) where T = Interval{T}(-Inf, Inf)
RR() = RR(Float64)

function entireinterval(args...)
@warn "entireinterval is deprecated. Use RR or ℝ instead."
function RR(args...)
@warn "RR is deprecated. Use RR or ℝ instead."
return RR(args...)
end
2 changes: 2 additions & 0 deletions src/intervals/common/set_operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ hull(a::F, b::G) where {F<:AbstractFlavor, G<:AbstractFlavor} =
promote_type(F, G)(min(a.lo, b.lo), max(a.hi, b.hi))
hull(a::Complex{F},b::Complex{F}) where {F <: AbstractFlavor} =
complex(hull(real(a), real(b)), hull(imag(a), imag(b)))
hull(a...) = reduce(hull, a)
hull(a::Vector{F}) where {F<:AbstractFlavor} = reduce(hull, a)

"""
union(a, b)
Expand Down
2 changes: 1 addition & 1 deletion src/intervals/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function ^(a::Interval{BigFloat}, n::Integer)
a.hi == 0 && return @round(-Inf, a.lo^n)
return @round(a.hi^n, a.lo^n)
else
return entireinterval(a)
return RR(a)
end
end

Expand Down
7 changes: 7 additions & 0 deletions src/intervals/rounding_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ macro round(F, ex1, ex2)
:($(esc(F))($(round_expr(ex1, RoundDown)), $(round_expr(ex2, RoundUp))))
end

# TODO Remove this method to make sure correct type is always used in @round
# Kept there for know to avoid having to patch all @round in the code
macro round(ex1, ex2)
F = Interval{DefaultBound}
:($(esc(F))($(round_expr(ex1, RoundDown)), $(round_expr(ex2, RoundUp))))
end

macro round_down(ex1)
round_expr(ex1, RoundDown)
end
Expand Down
27 changes: 8 additions & 19 deletions src/intervals/trigonometric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,6 @@ function sin(a::Interval{Float64})
end
end

function sinpi(a::Interval{T}) where T
isempty(a) && return a
w = a * Interval{T}(π)
return(sin(w))
end

function cos(a::Interval{T}) where T
isempty(a) && return a

Expand Down Expand Up @@ -226,11 +220,6 @@ function cos(a::Interval{Float64})
end
end

function cospi(a::Interval{T}) where T
isempty(a) && return a
w = a * Interval{T}(π)
return(cos(w))
end

function find_quadrants_tan(x::T) where {T}
temp = atomic(Interval{T}, x) / half_pi(x)
Expand All @@ -241,7 +230,7 @@ end
function tan(a::Interval{T}) where T
isempty(a) && return a

diam(a) > Interval{T}(π).lo && return entireinterval(a)
diam(a) > Interval{T}(π).lo && return RR(a)

lo_quadrant = minimum(find_quadrants_tan(a.lo))
hi_quadrant = maximum(find_quadrants_tan(a.hi))
Expand All @@ -252,12 +241,12 @@ function tan(a::Interval{T}) where T
if lo_quadrant_mod == 0 && hi_quadrant_mod == 1
# check if really contains singularity:
if hi_quadrant * half_pi(T) a
return entireinterval(a) # crosses singularity
return RR(a) # crosses singularity
end

elseif lo_quadrant_mod == hi_quadrant_mod && hi_quadrant > lo_quadrant
# must cross singularity
return entireinterval(a)
return RR(a)

end

Expand All @@ -273,7 +262,7 @@ function tan(a::Interval{Float64})

isempty(a) && return a

diam(a) > Interval{T}(π).lo && return entireinterval(a)
diam(a) > Interval{T}(π).lo && return RR(a)

lo_quadrant, lo = quadrant(a.lo)
hi_quadrant, hi = quadrant(a.hi)
Expand All @@ -282,11 +271,11 @@ function tan(a::Interval{Float64})
hi_quadrant_mod = mod(hi_quadrant, 2)

if lo_quadrant_mod == 0 && hi_quadrant_mod == 1
return entireinterval(a) # crosses singularity
return RR(a) # crosses singularity

elseif lo_quadrant_mod == hi_quadrant_mod && hi_quadrant != lo_quadrant
# must cross singularity
return entireinterval(a)
return RR(a)

end

Expand Down Expand Up @@ -398,7 +387,7 @@ end
function cot(a::Interval{BigFloat})
isempty(a) && return a

diam(a) > Interval{BigFloat}(π).lo && return entireinterval(a)
diam(a) > Interval{BigFloat}(π).lo && return RR(a)

lo_quadrant, lo = quadrant(Float64(a.lo))
hi_quadrant, hi = quadrant(Float64(a.hi))
Expand All @@ -410,7 +399,7 @@ function cot(a::Interval{BigFloat})

if(lo_quadrant_mod == 1 && hi_quadrant_mod == 0)
if(lo_quadrant < hi_quadrant)
return entireinterval(a)
return RR(a)
else
return @round(-Inf, cot(a.lo))
end
Expand Down
25 changes: 0 additions & 25 deletions src/multidim/intervalbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,30 +113,5 @@ dot(x::IntervalBox, y::IntervalBox) = dot(x.v, y.v)

Base.:(==)(x::IntervalBox, y::IntervalBox) = x.v == y.v


"""
mince(x::IntervalBox, n)
Splits `x` in `n` intervals in each dimension of the same diameter. These
intervals are combined in all possible `IntervalBox`-es, which are returned
as a vector.
"""
@generated function mince(x::IntervalBox{N,T}, n) where {N,T}
quote
nodes_matrix = Array{Interval{T},2}(undef, n, N)
for i in 1:N
nodes_matrix[1:n,i] .= mince(x[i], n)
end

nodes = IntervalBox{$N,T}[]
Base.Cartesian.@nloops $N i _->(1:n) begin
Base.Cartesian.@nextract $N ival d->nodes_matrix[i_d, d]
ibox = Base.Cartesian.@ncall $N IntervalBox ival
push!(nodes, ibox)
end
nodes
end
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...)
4 changes: 2 additions & 2 deletions src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function parse(::Type{F}, s::AbstractString) where {T, F<:AbstractFlavor{T}}

if m!= nothing
if m.captures[2] == "" # strings of the form "10??"
return entireinterval(T)
return RR(T)
end
if m.captures[2] == "u" # strings of the form "10??u"
lo = parse(Float64, m.captures[1])
Expand Down Expand Up @@ -243,7 +243,7 @@ function parse(::Type{F}, s::AbstractString) where {T, F<:AbstractFlavor{T}}
return emptyinterval(T)
end
if m.captures[1] == "entire"
return entireinterval(T)
return RR(T)
end

lo = m.captures[1]
Expand Down
10 changes: 5 additions & 5 deletions test/ITF1788_tests/ieee1788-constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ setrounding(Interval, :tight)

# According to the examples in Section 7.4.2, unbounded intervals can be constructed with non-common inputs.
@testset "IEEE1788.a" begin
@test Interval(-Inf, Inf) == entireinterval(Float64)
@test Interval(-Inf, Inf) == RR(Float64)
end

# Examples from Sections 9.7.1 and 9.8
Expand Down Expand Up @@ -77,8 +77,8 @@ end
@test decoration(@decorated("[-Inf, 2/3]")) == decoration(DecoratedInterval(Interval(-Inf, +0xa.aaaaaaaaaaab0p-4), dac))
@test @decorated("[0x1.3p-1,]") == DecoratedInterval(Interval(0x1.3p-1, Inf), dac)
@test decoration(@decorated("[0x1.3p-1,]")) == decoration(DecoratedInterval(Interval(0x1.3p-1, Inf), dac))
@test @decorated("[,]") == DecoratedInterval(entireinterval(Float64), dac)
@test decoration(@decorated("[,]")) == decoration(DecoratedInterval(entireinterval(Float64), dac))
@test @decorated("[,]") == DecoratedInterval(RR(Float64), dac)
@test decoration(@decorated("[,]")) == decoration(DecoratedInterval(RR(Float64), dac))
@test @decorated("3.56?1") == DecoratedInterval(Interval(0x3.8ccccccccccccp+0, 0x3.91eb851eb8520p+0), com)
@test decoration(@decorated("3.56?1")) == decoration(DecoratedInterval(Interval(0x3.8ccccccccccccp+0, 0x3.91eb851eb8520p+0), com))
@test @decorated("3.56?1e2") == DecoratedInterval(Interval(355.0, 357.0), com)
Expand Down Expand Up @@ -109,7 +109,7 @@ end
@test @interval("[]") ==
@test @interval("[empty]") ==
@test @interval("[ empty ]") ==
@test @interval("[,]") == entireinterval(Float64)
@test @interval("[ entire ]") == entireinterval(Float64)
@test @interval("[,]") == RR(Float64)
@test @interval("[ entire ]") == RR(Float64)
end
# FactCheck.exitstatus()

0 comments on commit 9b46da7

Please sign in to comment.