Skip to content

Commit

Permalink
Completes ieee1788-constructors.jl (#386)
Browse files Browse the repository at this point in the history
* Completes ieee1788-constructors.jl

* Fix tests

* Deprecate interval_part

* change variable name(interval) to fix test

* Bump patch version

* Remove unnecessary lines
  • Loading branch information
krish8484 committed Jun 27, 2020
1 parent b42eee9 commit d3a318c
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 78 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "IntervalArithmetic"
uuid = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
repo = "https://github.com/JuliaIntervals/IntervalArithmetic.jl.git"
version = "0.17.4"
version = "0.17.5"

[deps]
CRlibm = "96374032-68de-5a5b-8d9e-752f78720389"
Expand Down
2 changes: 1 addition & 1 deletion src/IntervalArithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export
## Decorations
export
@decorated,
interval_part, decoration, DecoratedInterval,
interval, decoration, DecoratedInterval,
com, dac, def, trv, ill

## Union type
Expand Down
80 changes: 47 additions & 33 deletions src/decorations/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,64 @@ zero(::Type{DecoratedInterval{T}}) where T<:Real = DecoratedInterval(zero(T))
one(a::DecoratedInterval{T}) where T<:Real = DecoratedInterval(one(T))
one(::Type{DecoratedInterval{T}}) where T<:Real = DecoratedInterval(one(T))

# NaI: not-an-interval
"""`NaI` not-an-interval: [NaN, NaN]."""
nai(::Type{T}) where T<:Real = DecoratedInterval(Interval{T}(convert(T, NaN), convert(T, NaN)), ill)
nai(x::Interval{T}) where T<:Real = nai(T)
nai(x::DecoratedInterval{T}) where T<:Real = nai(T)
nai() = nai(precision(Interval)[1])
isnai(x::Interval) = isnan(x.lo) || isnan(x.hi)
isnai(x::DecoratedInterval) = isnai(interval(x)) && x.decoration == ill
isnan(x::Interval) = isnai(x)

## Bool functions
bool_functions = (
:isempty, :isentire, :isunbounded,
:isfinite, :isnai, :isnan,
:isfinite, :isnan,
:isthin, :iscommon
)

bool_binary_functions = (
:<, :>, :(==), :!=, :, :<=,
:<, :>, :!=, :, :<=,
:isinterior, :isdisjoint, :precedes, :strictprecedes
)

for f in bool_functions
@eval $(f)(xx::DecoratedInterval) = $(f)(interval_part(xx))
@eval $(f)(xx::DecoratedInterval) = $(f)(interval(xx))
end

for f in bool_binary_functions
@eval $(f)(xx::DecoratedInterval, yy::DecoratedInterval) =
$(f)(interval_part(xx), interval_part(yy))
$(f)(interval(xx), interval(yy))
end

in(x::T, a::DecoratedInterval) where T<:Real = in(x, interval_part(a))
in(x::T, a::DecoratedInterval) where T<:Real = in(x, interval(a))


function ==(x::DecoratedInterval, y::DecoratedInterval)
isnai(x) && isnai(y) && return true
return (==(interval(x), interval(y)))
end

## scalar functions: mig, mag and friends
scalar_functions = (
:mig, :mag, :inf, :sup, :mid, :diam, :radius, :dist, :eps
)

for f in scalar_functions
@eval $(f)(xx::DecoratedInterval{T}) where T = $f(interval_part(xx))
@eval $(f)(xx::DecoratedInterval{T}) where T = $f(interval(xx))
end


## Arithmetic function; / is treated separately
arithm_functions = ( :+, :-, :* )

+(xx::DecoratedInterval) = xx
-(xx::DecoratedInterval) = DecoratedInterval(-interval_part(xx), decoration(xx))
-(xx::DecoratedInterval) = DecoratedInterval(-interval(xx), decoration(xx))
for f in arithm_functions
@eval function $(f)(xx::DecoratedInterval{T}, yy::DecoratedInterval{T}) where T
x = interval_part(xx)
y = interval_part(yy)
x = interval(xx)
y = interval(yy)
r = $f(x, y)
dec = min(decoration(xx), decoration(yy), decoration(r))
DecoratedInterval(r, dec)
Expand All @@ -61,16 +75,16 @@ end

# Division
function inv(xx::DecoratedInterval{T}) where T
x = interval_part(xx)
x = interval(xx)
dx = decoration(xx)
dx = zero(T) x ? min(dx,trv) : dx
r = inv(x)
dx = min(decoration(r), dx)
DecoratedInterval( r, dx )
end
function /(xx::DecoratedInterval{T}, yy::DecoratedInterval{T}) where T
x = interval_part(xx)
y = interval_part(yy)
x = interval(xx)
y = interval(yy)
r = x / y
dy = decoration(yy)
dy = zero(T) y ? min(dy, trv) : dy
Expand All @@ -80,7 +94,7 @@ end

## fma
function fma(xx::DecoratedInterval{T}, yy::DecoratedInterval{T}, zz::DecoratedInterval{T}) where T
r = fma(interval_part(xx), interval_part(yy), interval_part(zz))
r = fma(interval(xx), interval(yy), interval(zz))
d = min(decoration(xx), decoration(yy), decoration(zz))
d = min(decoration(r), d)
DecoratedInterval(r, d)
Expand All @@ -89,15 +103,15 @@ end

# power function must be defined separately and carefully
function ^(xx::DecoratedInterval{T}, n::Integer) where T
x = interval_part(xx)
x = interval(xx)
r = x^n
d = min(decoration(xx), decoration(r))
n < 0 && zero(T) x && return DecoratedInterval(r, trv)
DecoratedInterval(r, d)
end

function ^(xx::DecoratedInterval{T}, q::AbstractFloat) where T
x = interval_part(xx)
x = interval(xx)
r = x^q
d = min(decoration(xx), decoration(r))
if x > zero(T) || (x.lo zero(T) && q > zero(T)) ||
Expand All @@ -108,7 +122,7 @@ function ^(xx::DecoratedInterval{T}, q::AbstractFloat) where T
end

function ^(xx::DecoratedInterval{T}, q::Rational{S}) where {T, S<:Integer}
x = interval_part(xx)
x = interval(xx)
r = x^q
d = min(decoration(xx), decoration(r))
if x > zero(T) || (x.lo zero(T) && q > zero(T)) ||
Expand All @@ -119,8 +133,8 @@ function ^(xx::DecoratedInterval{T}, q::Rational{S}) where {T, S<:Integer}
end

function ^(xx::DecoratedInterval{T}, qq::DecoratedInterval{S}) where {T,S}
x = interval_part(xx)
q = interval_part(qq)
x = interval(xx)
q = interval(qq)
r = x^q
d = min(decoration(xx), decoration(qq), decoration(r))
if x > zero(T) || (x.lo zero(T) && q.lo > zero(T)) ||
Expand All @@ -136,13 +150,13 @@ end

## Discontinuous functions (sign, ceil, floor, trunc) and round
function sign(xx::DecoratedInterval{T}) where T
r = sign(interval_part(xx))
r = sign(interval(xx))
d = decoration(xx)
isthin(r) && return DecoratedInterval(r, d)
DecoratedInterval(r, min(d,def))
end
function ceil(xx::DecoratedInterval{T}) where T
x = interval_part(xx)
x = interval(xx)
r = ceil(x)
d = decoration(xx)
if isinteger(x.hi)
Expand All @@ -152,7 +166,7 @@ function ceil(xx::DecoratedInterval{T}) where T
DecoratedInterval(r, min(d,def))
end
function floor(xx::DecoratedInterval{T}) where T
x = interval_part(xx)
x = interval(xx)
r = floor(x)
d = decoration(xx)
if isinteger(x.lo)
Expand All @@ -162,7 +176,7 @@ function floor(xx::DecoratedInterval{T}) where T
DecoratedInterval(r, min(d,def))
end
function trunc(xx::DecoratedInterval{T}) where T
x = interval_part(xx)
x = interval(xx)
r = trunc(x)
d = decoration(xx)
if (isinteger(x.lo) && x.lo < zero(T)) || (isinteger(x.hi) && x.hi > zero(T))
Expand All @@ -173,7 +187,7 @@ function trunc(xx::DecoratedInterval{T}) where T
end

function round(xx::DecoratedInterval, ::RoundingMode{:Nearest})
x = interval_part(xx)
x = interval(xx)
r = round(x)
d = decoration(xx)
if isinteger(2*x.lo) || isinteger(2*x.hi)
Expand All @@ -183,7 +197,7 @@ function round(xx::DecoratedInterval, ::RoundingMode{:Nearest})
DecoratedInterval(r, min(d,def))
end
function round(xx::DecoratedInterval, ::RoundingMode{:NearestTiesAway})
x = interval_part(xx)
x = interval(xx)
r = round(x,RoundNearestTiesAway)
d = decoration(xx)
if isinteger(2*x.lo) || isinteger(2*x.hi)
Expand All @@ -203,23 +217,23 @@ binary_functions = ( :min, :max )

for f in binary_functions
@eval function $(f)(xx::DecoratedInterval{T}, yy::DecoratedInterval{T}) where T
r = $f(interval_part(xx), interval_part(yy))
r = $f(interval(xx), interval(yy))
d = min(decoration(r), decoration(xx), decoration(yy))
DecoratedInterval(r, d)
end
end

## abs
abs(xx::DecoratedInterval{T}) where T =
DecoratedInterval(abs(interval_part(xx)), decoration(xx))
DecoratedInterval(abs(interval(xx)), decoration(xx))


## Other (cancel and set) functions
other_functions = ( :cancelplus, :cancelminus, :intersect, :hull, :union )

for f in other_functions
@eval $(f)(xx::DecoratedInterval{T}, yy::DecoratedInterval{T}) where T =
DecoratedInterval($(f)(interval_part(xx), interval_part(yy)), trv)
DecoratedInterval($(f)(interval(xx), interval(yy)), trv)
end

@doc """
Expand Down Expand Up @@ -268,15 +282,15 @@ unrestricted_functions =(

for f in unrestricted_functions
@eval function $(f)(xx::DecoratedInterval{T}) where T
x = interval_part(xx)
x = interval(xx)
r = $f(x)
d = min(decoration(r), decoration(xx))
DecoratedInterval(r, d)
end
end

function tan(xx::DecoratedInterval{T}) where T
x = interval_part(xx)
x = interval(xx)
r = tan(x)
d = min(decoration(r), decoration(xx))
if isunbounded(r)
Expand All @@ -294,8 +308,8 @@ function decay(a::DECORATION)
end

function atan(yy::DecoratedInterval{T}, xx::DecoratedInterval{T}) where T
x = interval_part(xx)
y = interval_part(yy)
x = interval(xx)
y = interval(yy)
r = atan(y, x)
d = decoration(r)
d = min(d, decoration(xx), decoration(yy))
Expand Down Expand Up @@ -333,7 +347,7 @@ restricted_functions2 = Dict(
for (f, domain) in restricted_functions1
domain = Interval(domain...)
@eval function Base.$(f)(xx::DecoratedInterval{T}) where T
x = interval_part(xx)
x = interval(xx)
r = $(f)(x)
d = min(decoration(xx), decoration(r))
x $(domain) && return DecoratedInterval(r, d)
Expand All @@ -344,7 +358,7 @@ end
for (f, domain) in restricted_functions2
domain = Interval(domain...)
@eval function Base.$(f)(xx::DecoratedInterval{T}) where T
x = interval_part(xx)
x = interval(xx)
r = $(f)(x)
d = min(decoration(xx), decoration(r))
x $(domain) && return DecoratedInterval(r, d)
Expand Down
29 changes: 17 additions & 12 deletions src/decorations/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct DecoratedInterval{T<:Real} <: AbstractInterval{T}
function DecoratedInterval{T}(I::Interval, d::DECORATION) where T
dd = decoration(I)
dd <= trv && return new{T}(I, dd)
d == ill && return new{T}(nai(I), d)
d == ill && return new{T}(nai(I))
return new{T}(I, d)
end
end
Expand All @@ -47,7 +47,7 @@ DecoratedInterval(I::Interval{T}, d::DECORATION) where T<:AbstractFloat =
DecoratedInterval{T}(I, d)

function DecoratedInterval(a::T, b::T, d::DECORATION) where T<:Real
a > b && return DecoratedInterval(nai(T), ill)
a > b && return nai(T)
DecoratedInterval(Interval(a,b), d)
end

Expand All @@ -63,7 +63,7 @@ DecoratedInterval(a::T, b::S, d::DECORATION) where {T<:Real, S<:Real} =
DecoratedInterval(I::Interval) = DecoratedInterval(I, decoration(I))

function DecoratedInterval(a::T, b::T) where T<:Real
a > b && return DecoratedInterval(nai(T), ill)
a > b && return nai(T)
DecoratedInterval(Interval(a,b))
end

Expand All @@ -79,7 +79,7 @@ DecoratedInterval(a::Tuple) = DecoratedInterval(a...)

DecoratedInterval(I::DecoratedInterval, dec::DECORATION) = DecoratedInterval(I.interval, dec)

interval_part(x::DecoratedInterval) = x.interval
interval(x::DecoratedInterval) = x.interval

decoration(x::DecoratedInterval) = x.decoration

Expand Down Expand Up @@ -111,25 +111,30 @@ convert(::Type{DecoratedInterval{T}}, x::S) where {T<:Real, S<:Integer} =
# convert(DecoratedInterval{T}, rationalize(x))
# end
function convert(::Type{DecoratedInterval{T}}, xx::DecoratedInterval) where T<:Real
x = interval_part(xx)
x = interval(xx)
x = atomic(Interval{T},x)
DecoratedInterval( x, decoration(xx) )
end

convert(::Type{DecoratedInterval{T}}, x::AbstractString) where T<:AbstractFloat =
parse(DecoratedInterval{T}, x)

big(x::DecoratedInterval) = DecoratedInterval(big(interval_part(x)),
big(x::DecoratedInterval) = DecoratedInterval(big(interval(x)),
decoration(x))

macro decorated(ex...)
local x
if(!(ex[1] isa String))
local x

if length(ex) == 1
x = :(@interval($(esc(ex[1]))))
if length(ex) == 1
x = :(@interval($(esc(ex[1]))))
else
x = :($(esc(ex[1])), $(esc(ex[2])))
end

:(DecoratedInterval($x))
else
x = :($(esc(ex[1])), $(esc(ex[2])))
s = ex[1]
parse(DecoratedInterval{Float64}, s)
end

:(DecoratedInterval($x))
end
1 change: 1 addition & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ end

@deprecate infimum inf
@deprecate supremum sup
@deprecate interval_part interval
8 changes: 4 additions & 4 deletions src/display.jl
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,15 @@ function representation(a::DecoratedInterval{T}, format=nothing) where T
end

if format==:full
return "DecoratedInterval($(representation(interval_part(a), format)), $(decoration(a)))"
return "DecoratedInterval($(representation(interval(a), format)), $(decoration(a)))"
end

interval = representation(interval_part(a), format)
var_interval = representation(interval(a), format)

if display_params.decorations
string(interval, "_", decoration(a))
string(var_interval, "_", decoration(a))
else
interval
var_interval
end

end
Expand Down
Loading

2 comments on commit d3a318c

@dpsanders
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/17101

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.17.5 -m "<description of version>" d3a318c6356515ee2d2a0c47ab7af419dea106ea
git push origin v0.17.5

Please sign in to comment.