Skip to content

Commit

Permalink
Use inf, sup (or bounds) instead of .lo and .hi (#531)
Browse files Browse the repository at this point in the history
* Use inf, sup (or bounds) instead of .lo and .hi

* Remove left instances of .lo and .hi in trigonometric.jl

Co-authored-by: Benoît Richard <[email protected]>

Co-authored-by: Benoît Richard <[email protected]>
  • Loading branch information
lbenet and Kolaru authored May 29, 2022
1 parent 49f4806 commit cafc327
Show file tree
Hide file tree
Showing 25 changed files with 360 additions and 318 deletions.
4 changes: 3 additions & 1 deletion examples/draw_function_image.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ function draw(box_list::Vector{T}, color="grey", alpha=0.5) where T<:IntervalBox
patch_list = []
for box in box_list
x, y = box
xlo, xhi = bounds(x)
ylo, yhi = bounds(y)
push!(patch_list,
make_rectangle(x.lo, y.lo, x.hi-x.lo, y.hi-y.lo, color, alpha))
make_rectangle(xlo, ylo, xhi-xlo, yhi-ylo, color, alpha))
end

ax = gca()
Expand Down
4 changes: 2 additions & 2 deletions src/bisect.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function bisect(X::Interval, α=where_bisect)

m = scaled_mid(X, α)

return (Interval(X.lo, m), Interval(m, X.hi))
return (Interval(inf(X), m), Interval(m, sup(X)))
end

"""
Expand Down Expand Up @@ -48,7 +48,7 @@ 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)
nodes = range(inf(x), sup(x), length = n+1)
return [Interval(nodes[i], nodes[i+1]) for i in 1:length(nodes)-1]
end

Expand Down
2 changes: 1 addition & 1 deletion src/decorations/decorations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ nai(::Interval{T}) where T<:Real = nai(T)
nai(::DecoratedInterval{T}) where T<:Real = nai(T)
nai() = nai(Interval{default_bound()})

isnai(x::Interval) = isnan(x.lo) || isnan(x.hi) #|| x.lo > x.hi || (isinf(x.lo) && x.lo == x.hi)
isnai(x::Interval) = isnan(inf(x)) || isnan(sup(x)) #|| inf(x) > sup(x) || (isinf(inf(x)) && inf(x) == sup(x))
isnai(x::DecoratedInterval) = isnai(interval(x)) || x.decoration == ill
24 changes: 12 additions & 12 deletions src/decorations/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function ^(xx::DecoratedInterval{T}, q::AbstractFloat) where T
x = interval(xx)
r = x^q
d = min(decoration(xx), decoration(r))
if x.lo > zero(T) || (x.lo zero(T) && q > zero(T)) ||
if inf(x) > zero(T) || (inf(x) zero(T) && q > zero(T)) ||
(isinteger(q) && q > zero(q)) || (isinteger(q) && zero(T) x)
return DecoratedInterval(r, d)
end
Expand All @@ -118,7 +118,7 @@ function ^(xx::DecoratedInterval{T}, q::Rational{S}) where {T, S<:Integer}
x = interval(xx)
r = x^q
d = min(decoration(xx), decoration(r))
if x.lo > zero(T) || (x.lo zero(T) && q > zero(T)) ||
if inf(x) > zero(T) || (inf(x) zero(T) && q > zero(T)) ||
(isinteger(q) && q > zero(q)) || (isinteger(q) && zero(T) x)
return DecoratedInterval(r, d)
end
Expand All @@ -130,9 +130,9 @@ function ^(xx::DecoratedInterval{T}, qq::DecoratedInterval{S}) where {T,S}
q = interval(qq)
r = x^q
d = min(decoration(xx), decoration(qq), decoration(r))
if x.lo > zero(T) || (x.lo zero(T) && q.lo > zero(T)) ||
(isthin(q) && isinteger(q.lo) && q.lo > zero(q)) ||
(isthin(q) && isinteger(q.lo) && zero(T) x)
if inf(x) > zero(T) || (inf(x) zero(T) && inf(q) > zero(T)) ||
(isthin(q) && isinteger(inf(q)) && inf(q) > zero(q)) ||
(isthin(q) && isinteger(inf(q)) && zero(T) x)
return DecoratedInterval(r, d)
end
DecoratedInterval(r, trv)
Expand All @@ -152,7 +152,7 @@ function ceil(xx::DecoratedInterval{T}) where T
x = interval(xx)
r = ceil(x)
d = decoration(xx)
if isinteger(x.hi)
if isinteger(sup(x))
d = min(d, dac)
end
isthin(r) && return DecoratedInterval(r, d)
Expand All @@ -162,7 +162,7 @@ function floor(xx::DecoratedInterval{T}) where T
x = interval(xx)
r = floor(x)
d = decoration(xx)
if isinteger(x.lo)
if isinteger(inf(x))
d = min(d, dac)
end
isthin(r) && return DecoratedInterval(r, d)
Expand All @@ -172,7 +172,7 @@ function trunc(xx::DecoratedInterval{T}) where T
x = interval(xx)
r = trunc(x)
d = decoration(xx)
if (isinteger(x.lo) && x.lo < zero(T)) || (isinteger(x.hi) && x.hi > zero(T))
if (isinteger(inf(x)) && inf(x) < zero(T)) || (isinteger(sup(x)) && sup(x) > zero(T))
d = min(d, dac)
end
isthin(r) && return DecoratedInterval(r, d)
Expand All @@ -183,7 +183,7 @@ function round(xx::DecoratedInterval, ::RoundingMode{:Nearest})
x = interval(xx)
r = round(x)
d = decoration(xx)
if isinteger(2*x.lo) || isinteger(2*x.hi)
if isinteger(2*inf(x)) || isinteger(2*sup(x))
d = min(d, dac)
end
isthin(r) && return DecoratedInterval(r, d)
Expand All @@ -193,7 +193,7 @@ function round(xx::DecoratedInterval, ::RoundingMode{:NearestTiesAway})
x = interval(xx)
r = round(x,RoundNearestTiesAway)
d = decoration(xx)
if isinteger(2*x.lo) || isinteger(2*x.hi)
if isinteger(2*inf(x)) || isinteger(2*sup(x))
d = min(d, dac)
end
isthin(r) && return DecoratedInterval(r, d)
Expand Down Expand Up @@ -309,8 +309,8 @@ function atan(yy::DecoratedInterval{T}, xx::DecoratedInterval{T}) where T
# Check cases when decoration is trv and decays (from com or dac)
if zero(T) y
zero(T) x && return DecoratedInterval(r, trv)
if x.hi < zero(T)
y.lo < zero(T) && return DecoratedInterval(r, min(d, def))
if sup(x) < zero(T)
inf(y) < zero(T) && return DecoratedInterval(r, min(d, def))
return DecoratedInterval(r, min(d, dac))
end
end
Expand Down
4 changes: 2 additions & 2 deletions src/decorations/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ macro decorated(ex...)
if !(ex[1] isa String)
if length(ex) == 1
x = :(@interval($(esc(ex[1]))))
lo = :($x.lo)
hi = :($x.hi)
lo = :(inf($x))
hi = :(sup($x))
else
lo, hi = ex
end
Expand Down
24 changes: 15 additions & 9 deletions src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -185,16 +185,18 @@ function basic_representation(a::Interval, format=nothing)

local output

alo, ahi = bounds(a)

if format == :standard
aa = round_string(a.lo, sigfigs, RoundDown)
bb = round_string(a.hi, sigfigs, RoundUp)
aa = round_string(alo, sigfigs, RoundDown)
bb = round_string(ahi, sigfigs, RoundUp)

output = "[$aa, $bb]"
output = replace(output, "inf" => "")
output = replace(output, "Inf" => "")

elseif format == :full
output = "Interval($(a.lo), $(a.hi))"
output = "Interval($(alo), $(ahi))"

elseif format == :midpoint
m = round_string(mid(a), sigfigs, RoundNearest)
Expand All @@ -218,15 +220,17 @@ function basic_representation(a::Interval{Float32}, format=nothing)

local output

alo, ahi = bounds(a)

if format == :standard

aa = round_string(a.lo, sigfigs, RoundDown)
bb = round_string(a.hi, sigfigs, RoundUp)
aa = round_string(alo, sigfigs, RoundDown)
bb = round_string(ahi, sigfigs, RoundUp)

output = "[$(aa)f0, $(bb)f0]"

elseif format == :full
output = "Interval($(a.lo)f0, $(a.hi)f0)"
output = "Interval($(alo)f0, $(ahi)f0)"

elseif format == :midpoint
m = round_string(mid(a), sigfigs, RoundNearest)
Expand All @@ -252,11 +256,13 @@ function basic_representation(a::Interval{Rational{T}}, format=nothing) where

local output

alo, ahi = bounds(a)

if format == :standard
output = "[$(a.lo), $(a.hi)]"
output = "[$(alo), $(ahi)]"

elseif format == :full
output = "Interval($(a.lo), $(a.hi))"
output = "Interval($(alo), $(ahi))"

elseif format == :midpoint
m = mid(a)
Expand Down Expand Up @@ -289,7 +295,7 @@ function representation(a::Interval{BigFloat}, format=nothing)
end

if format == :standard
return string(basic_representation(a, format), subscriptify(precision(a.lo)))
return string(basic_representation(a, format), subscriptify(precision(sup(a))))
else
return basic_representation(a, format)
end
Expand Down
4 changes: 2 additions & 2 deletions src/intervals/arithmetic/absmax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Implement the `min` function of the IEEE Std 1788-2015 (Table 9.1).
"""
function min(a::F, b::F) where {F<:Interval}
(isempty(a) || isempty(b)) && return emptyinterval(F)
return F( min(a.lo, b.lo), min(a.hi, b.hi))
return F( min(inf(a), inf(b)), min(sup(a), sup(b)))
end

"""
Expand All @@ -33,5 +33,5 @@ Implement the `max` function of the IEEE Std 1788-2015 (Table 9.1).
"""
function max(a::F, b::F) where {F<:Interval}
(isempty(a) || isempty(b)) && return emptyinterval(F)
return F( max(a.lo, b.lo), max(a.hi, b.hi))
return F( max(inf(a), inf(b)), max(sup(a), sup(b)))
end
Loading

0 comments on commit cafc327

Please sign in to comment.