Skip to content

Commit 8b9f4fd

Browse files
authored
Merge pull request #560 from OlivierHnt/1.0-constructors
1.0-dev: rework constructors
2 parents 3e2e99e + 5fb804e commit 8b9f4fd

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1740
-1925
lines changed

src/IntervalArithmetic.jl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export ×, dot
2121
import Base:
2222
+, -, *, /, //, fma,
2323
<, >, ==, !=, , ^, <=, >=,
24-
in, zero, one, eps, typemin, typemax, abs, abs2, real, min, max,
24+
in, zero, one, eps, typemin, typemax, abs, abs2, min, max,
2525
sqrt, exp, log, exp2, exp10, log2, log10, inv, cbrt, hypot,
2626
sin, cos, tan, cot, csc, sec, asin, acos, atan, acot, sinpi, cospi,
2727
sinh, cosh, tanh, coth, csch, sech, asinh, acosh, atanh, acoth,
@@ -50,8 +50,7 @@ import .Broadcast: broadcasted
5050

5151
export
5252
Interval, BooleanInterval,
53-
interval, checked_interval,
54-
@interval, @biginterval, @floatinterval,
53+
interval, ±, .., @I_str,
5554
diam, radius, mid, scaled_mid, mag, mig, hull,
5655
emptyinterval, ∅, ∞, isempty, isinterior, isdisjoint, ,
5756
precedes, strictprecedes, , , , , contains_zero, isthinzero,
@@ -65,7 +64,6 @@ export
6564
IntervalRounding,
6665
PointwisePolicy,
6766
cancelminus, cancelplus, isbounded, isunbounded,
68-
.., @I_str, ±,
6967
pow, extended_div, nthroot,
7068
setformat, @format
7169

@@ -87,7 +85,6 @@ export
8785

8886
## Decorations
8987
export
90-
@decorated,
9188
interval, decoration, DecoratedInterval,
9289
com, dac, def, trv, ill
9390

src/bisect.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ const where_bisect = 0.49609375
66
Split the interval `X` at position α; α=0.5 corresponds to the midpoint.
77
Returns a tuple of the new intervals.
88
"""
9-
function bisect(X::F, α=where_bisect) where {F<:Interval}
9+
function bisect(X::Interval{T}, α=where_bisect) where {T<:NumTypes}
1010
@assert 0 α 1
1111

1212
m = scaled_mid(X, α)
1313

14-
return (F(inf(X), m), F(m, sup(X)))
14+
return (unsafe_interval(T, inf(X), m), unsafe_interval(T, m, sup(X)))
1515
end
1616

1717
"""
@@ -43,18 +43,18 @@ end
4343
"""
4444
mince(x::Interval, n)
4545
46-
Splits `x` in `n` intervals of the same diameter, which are returned
46+
Split `x` in `n` intervals of the same diameter, which are returned
4747
as a vector.
4848
"""
49-
function mince(x::F, n) where {F<:Interval}
50-
nodes = range(inf(x), sup(x), length = n+1)
51-
return [F(nodes[i], nodes[i+1]) for i in 1:length(nodes)-1]
49+
function mince(x::Interval{T}, n) where {T<:NumTypes}
50+
nodes = LinRange(inf(x), sup(x), n+1)
51+
return [unsafe_interval(T, nodes[i], nodes[i+1]) for i in 1:n]
5252
end
5353

5454
"""
5555
mince(x::IntervalBox, n)
5656
57-
Splits `x` in `n` intervals in each dimension of the same diameter. These
57+
Split `x` in `n` intervals in each dimension of the same diameter. These
5858
intervals are combined in all possible `IntervalBox`-es, which are returned
5959
as a vector.
6060
"""

src/decorations/decorations.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
include("intervals.jl")
22
include("functions.jl")
33

4-
isnan(x::Interval) = false # NaI is always decorated
4+
isnan(::Interval) = false # NaI is always decorated
55

66
"""`NaI` not-an-interval: [NaN, NaN]."""
7-
nai(::Type{T}) where T = DecoratedInterval(convert(T, NaN), convert(T, NaN), ill)
8-
nai(::Type{F}) where {T, F<:Interval{T}} = nai(T)
9-
nai(::Interval{T}) where T<:Real = nai(T)
10-
nai(::DecoratedInterval{T}) where T<:Real = nai(T)
11-
nai() = nai(Interval{default_bound()})
7+
nai(::Type{Interval{T}}) where {T<:NumTypes} = nai(T)
8+
nai(::Type{T}) where {T<:NumTypes} = DecoratedInterval(convert(T, NaN), convert(T, NaN), ill)
9+
nai(::Type{<:Real}) = nai(default_numtype())
10+
nai() = nai(default_numtype())
11+
nai(::T) where {T} = nai(T)
1212

13-
isnai(x::Interval) = isnan(inf(x)) || isnan(sup(x)) #|| inf(x) > sup(x) || (isinf(inf(x)) && inf(x) == sup(x))
13+
isnai(x::Interval) = isnan(inf(x)) || isnan(sup(x)) # || inf(x) > sup(x) || (isinf(inf(x)) && inf(x) == sup(x))
1414
isnai(x::DecoratedInterval) = isnai(interval(x)) || x.decoration == ill

src/decorations/functions.jl

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ Base.literal_pow(::typeof(^), x::DecoratedInterval{T}, ::Val{p}) where {T,p} = x
44

55

66
# zero, one
7-
zero(a::DecoratedInterval{T}) where T<:Real = DecoratedInterval(zero(T))
8-
zero(::Type{DecoratedInterval{T}}) where T<:Real = DecoratedInterval(zero(T))
9-
one(a::DecoratedInterval{T}) where T<:Real = DecoratedInterval(one(T))
10-
one(::Type{DecoratedInterval{T}}) where T<:Real = DecoratedInterval(one(T))
7+
zero(::DecoratedInterval{T}) where {T<:NumTypes} = DecoratedInterval(zero(T))
8+
zero(::Type{DecoratedInterval{T}}) where {T<:NumTypes} = DecoratedInterval(zero(T))
9+
one(::DecoratedInterval{T}) where {T<:NumTypes} = DecoratedInterval(one(T))
10+
one(::Type{DecoratedInterval{T}}) where {T<:NumTypes} = DecoratedInterval(one(T))
1111

1212
## Bool functions
1313
const bool_functions = (
@@ -323,18 +323,18 @@ end
323323

324324
# The function is unbounded at the bounded edges of the domain
325325
restricted_functions1 = Dict(
326-
:log => Interval{Float64}(0.0, Inf),
327-
:log2 => Interval{Float64}(0.0, Inf),
328-
:log10 => Interval{Float64}(0.0, Inf),
329-
:atanh => Interval{Float64}(-1.0, 1.0)
326+
:log => unsafe_interval(Float64, 0.0, Inf),
327+
:log2 => unsafe_interval(Float64, 0.0, Inf),
328+
:log10 => unsafe_interval(Float64, 0.0, Inf),
329+
:atanh => unsafe_interval(Float64, -1.0, 1.0)
330330
)
331331

332332
# The function is bounded at the bounded edge(s) of the domain
333333
restricted_functions2 = Dict(
334-
:sqrt => Interval{Float64}(0.0, Inf),
335-
:asin => Interval{Float64}(-1.0, 1.0),
336-
:acos => Interval{Float64}(-1.0, 1.0),
337-
:acosh => Interval{Float64}(1.0, Inf)
334+
:sqrt => unsafe_interval(Float64, 0.0, Inf),
335+
:asin => unsafe_interval(Float64, -1.0, 1.0),
336+
:acos => unsafe_interval(Float64, -1.0, 1.0),
337+
:acosh => unsafe_interval(Float64, 1.0, Inf)
338338
)
339339

340340
# Define functions with restricted domains on DecoratedInterval's:

src/decorations/intervals.jl

Lines changed: 46 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,75 @@
55
"""
66
DECORATION
77
8-
Enumeration constant for the types of interval decorations.
9-
The nomenclature of the follows the IEEE-1788 (2015) standard
10-
(sect 11.2):
11-
12-
- `com -> 4`: common: bounded, non-empty
13-
- `dac -> 3`: defined (nonempty) and continuous
14-
- `def -> 2`: defined (nonempty)
15-
- `trv -> 1`: always true (no information)
16-
- `ill -> 0`: nai ("not an interval")
8+
Enumeration constant for the types of interval decorations. The nomenclature
9+
follows Section 11.2 of the IEEE Standard 1788-2015:
10+
- `com -> 4`: non-empty, continuous and bounded (common)
11+
- `dac -> 3`: non-empty and continuous (defined and continuous)
12+
- `def -> 2`: non-empty (defined)
13+
- `trv -> 1`: always true (trivial)
14+
- `ill -> 0`: not an interval (ill-formed)
1715
"""
1816
@enum DECORATION ill=0 trv=1 def=2 dac=3 com=4
19-
# Note that `isweaklyless`, and hence ``<` and `min`, are automatically defined for enums
17+
# Note that `isweaklyless`, and hence `<` and `min`, are automatically defined for enums
2018

2119
"""
22-
DecoratedInterval
20+
DecoratedInterval{T<:NumTypes}
2321
24-
A *decorated* interval is an interval, together with a *decoration*, i.e.
25-
a flag that records the status of the interval when thought of as the result
26-
of a previously executed sequence of functions acting on an initial interval.
22+
Wraps an `Interval` together with a `DECORATION`, i.e. a flag that records the
23+
status of the interval when thought of as the result of a previously executed
24+
sequence of functions acting on an initial interval.
2725
"""
28-
2926
struct DecoratedInterval{T<:NumTypes}
3027
interval::Interval{T}
3128
decoration::DECORATION
32-
end
3329

34-
DecoratedInterval(I::DecoratedInterval, dec::DECORATION) = DecoratedInterval(I.interval, dec)
30+
DecoratedInterval{T}(x::Interval{T}, d::DECORATION) where {T<:NumTypes} = new{T}(x, d)
31+
end
3532

36-
function DecoratedInterval(a::T, b::S, d::DECORATION) where {T<:Real, S<:Real}
37-
NumType = promote_numtype(T, S)
38-
is_valid_interval(a, b) || return DecoratedInterval(Interval{NumType}(a, b), ill)
39-
return DecoratedInterval(Interval{NumType}(a, b), d)
33+
function DecoratedInterval{T}(x::Interval{T}) where {T<:NumTypes}
34+
d = decoration(x)
35+
d trv && return DecoratedInterval{T}(x, d)
36+
d == ill && return DecoratedInterval{T}(nai(T), d)
37+
return DecoratedInterval{T}(x, d)
4038
end
4139

42-
DecoratedInterval(a::Real, d::DECORATION) = DecoratedInterval(a, a, d)
43-
DecoratedInterval(a::Tuple, d::DECORATION) = DecoratedInterval(a..., d)
40+
DecoratedInterval(x::Interval{T}, d::DECORATION) where {T<:NumTypes} = DecoratedInterval{T}(x, d)
4441

45-
function DecoratedInterval{T}(I::Interval) where {T}
46-
d = decoration(I)
47-
d <= trv && return DecoratedInterval{T}(I, d)
48-
d == ill && return DecoratedInterval{T}(nai(I), d)
49-
return DecoratedInterval{T}(I, d)
50-
end
42+
DecoratedInterval(x::Interval{T}) where {T<:NumTypes} = DecoratedInterval{T}(x)
43+
44+
DecoratedInterval(x::DecoratedInterval, d::DECORATION) = DecoratedInterval(x.interval, d) # do we want this behaviour?
5145

52-
DecoratedInterval(I::Interval{T}) where {T<:NumTypes} = DecoratedInterval{T}(I)
46+
DecoratedInterval(a, b, d::DECORATION) =
47+
DecoratedInterval(unsafe_interval(promote_numtype(numtype(a), numtype(b)), a, b),
48+
ifelse(is_valid_interval(a, b), d, ill))
5349

54-
function DecoratedInterval(a::T, b::S) where {T<:Real, S<:Real}
55-
NumType = promote_numtype(T, S)
56-
is_valid_interval(a, b) || return DecoratedInterval(Interval{NumType}(a, b), ill)
57-
return DecoratedInterval(Interval{NumType}(a, b))
50+
function DecoratedInterval(a, b)
51+
T = promote_numtype(numtype(a), numtype(b))
52+
is_valid_interval(a, b) || return DecoratedInterval(unsafe_interval(T, a, b), ill)
53+
return DecoratedInterval(unsafe_interval(T, a, b))
5854
end
5955

60-
DecoratedInterval(a::Real) = DecoratedInterval(a, a)
56+
DecoratedInterval(a, d::DECORATION) = DecoratedInterval(a, a, d)
57+
58+
DecoratedInterval(a) = DecoratedInterval(a, a)
59+
60+
DecoratedInterval(a::Tuple, d::DECORATION) = DecoratedInterval(a..., d)
61+
6162
DecoratedInterval(a::Tuple) = DecoratedInterval(a...)
6263

64+
#
65+
6366
interval(x::DecoratedInterval) = x.interval
6467
decoration(x::DecoratedInterval) = x.decoration
6568

66-
# Automatic decorations for an Interval
67-
function decoration(I::Interval)
68-
isnai(I) && return ill # nai()
69-
isempty(I) && return trv # emptyinterval
70-
isunbounded(I) && return dac # unbounded
71-
com # common
69+
function decoration(x::Interval)
70+
isnai(x) && return ill # nai()
71+
isempty(x) && return trv # emptyinterval
72+
isunbounded(x) && return dac # unbounded
73+
return com # common
7274
end
7375

74-
big(x::DecoratedInterval) = DecoratedInterval(big(interval(x)), decoration(x))
76+
#
7577

76-
macro decorated(ex...)
77-
if !(ex[1] isa String)
78-
if length(ex) == 1
79-
x = :(@interval($(esc(ex[1]))))
80-
lo = :(inf($x))
81-
hi = :(sup($x))
82-
else
83-
lo, hi = ex
84-
end
85-
86-
return :(DecoratedInterval($lo, $hi))
87-
else
88-
s = ex[1]
89-
parse(DecoratedInterval{Float64}, s)
90-
end
91-
end
78+
float(x::DecoratedInterval) = DecoratedInterval(float(interval(x)), decoration(x))
79+
big(x::DecoratedInterval) = DecoratedInterval(big(interval(x)), decoration(x))

src/intervals/arithmetic/absmax.jl

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,41 @@
1-
# This file is part of the IntervalArithmetic.jl package; MIT licensed
1+
# This file contains the functions described as "Absmax functions" in Section
2+
# 9.1 of the IEEE Standard 1788-2015 and required for set-based flavor in
3+
# Section 10.5.3
24

3-
#= This file contains the functions described as "Absmax functions"
4-
in the IEEE Std 1788-2015 (sections 9.1) and required for set-based flavor
5-
in section 10.5.3.
6-
=#
75
"""
86
abs(a::Interval)
97
10-
Implement the `abs` function of the IEEE Std 1788-2015 (Table 9.1).
8+
Implement the `abs` function of the IEEE Standard 1788-2015 (Table 9.1).
119
"""
12-
function abs(a::F) where {F<:Interval}
13-
isempty(a) && return emptyinterval(F)
14-
return F(mig(a), mag(a))
10+
function abs(a::Interval{T}) where {T<:NumTypes}
11+
isempty(a) && return a
12+
return unsafe_interval(T, mig(a), mag(a))
1513
end
1614

1715
abs2(a::Interval) = sqr(a)
1816

1917
"""
20-
min(a::Interval)
18+
min(a::Interval, b::Interval)
2119
22-
Implement the `min` function of the IEEE Std 1788-2015 (Table 9.1).
20+
Implement the `min` function of the IEEE Standard 1788-2015 (Table 9.1).
2321
"""
24-
function min(a::F, b::F) where {F<:Interval}
25-
(isempty(a) || isempty(b)) && return emptyinterval(F)
26-
return F(min(inf(a), inf(b)), min(sup(a), sup(b)))
22+
function min(a::Interval{T}, b::Interval{T}) where {T<:NumTypes}
23+
isempty(a) && return a
24+
isempty(b) && return b
25+
return unsafe_interval(T, min(inf(a), inf(b)), min(sup(a), sup(b)))
2726
end
2827

28+
min(a::Interval, b::Interval) = min(promote(a, b)...)
29+
2930
"""
30-
max(a::Interval)
31+
max(a::Interval, b::Interval)
3132
32-
Implement the `max` function of the IEEE Std 1788-2015 (Table 9.1).
33+
Implement the `max` function of the IEEE Standard 1788-2015 (Table 9.1).
3334
"""
34-
function max(a::F, b::F) where {F<:Interval}
35-
(isempty(a) || isempty(b)) && return emptyinterval(F)
36-
return F(max(inf(a), inf(b)), max(sup(a), sup(b)))
35+
function max(a::Interval{T}, b::Interval{T}) where {T<:NumTypes}
36+
isempty(a) && return a
37+
isempty(b) && return b
38+
return unsafe_interval(T, max(inf(a), inf(b)), max(sup(a), sup(b)))
3739
end
40+
41+
max(a::Interval, b::Interval) = max(promote(a, b)...)

0 commit comments

Comments
 (0)