Skip to content

Commit 5cbf7a8

Browse files
committed
Only allow Interval{T} as a raw constructor
1 parent 466606b commit 5cbf7a8

File tree

17 files changed

+202
-184
lines changed

17 files changed

+202
-184
lines changed

src/bisect.jl

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

1313
m = scaled_mid(X, α)
1414

15-
return (Interval(inf(X), m), Interval(m, sup(X)))
15+
return (F(inf(X), m), F(m, sup(X)))
1616
end
1717

1818
"""
@@ -47,9 +47,9 @@ end
4747
Splits `x` in `n` intervals of the same diameter, which are returned
4848
as a vector.
4949
"""
50-
function mince(x::Interval, n)
50+
function mince(x::F, n) where {F<:Interval}
5151
nodes = range(inf(x), sup(x), length = n+1)
52-
return [Interval(nodes[i], nodes[i+1]) for i in 1:length(nodes)-1]
52+
return [F(nodes[i], nodes[i+1]) for i in 1:length(nodes)-1]
5353
end
5454

5555
"""
@@ -74,4 +74,4 @@ as a vector.
7474
end
7575
nodes
7676
end
77-
end
77+
end

src/decorations/functions.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ restricted_functions2 = Dict(
339339

340340
# Define functions with restricted domains on DecoratedInterval's:
341341
for (f, domain) in restricted_functions1
342-
domain = Interval(domain...)
342+
domain = interval(domain...)
343343
@eval function Base.$(f)(xx::DecoratedInterval{T}) where T
344344
x = interval(xx)
345345
r = $(f)(x)
@@ -350,7 +350,7 @@ for (f, domain) in restricted_functions1
350350
end
351351

352352
for (f, domain) in restricted_functions2
353-
domain = Interval(domain...)
353+
domain = interval(domain...)
354354
@eval function Base.$(f)(xx::DecoratedInterval{T}) where T
355355
x = interval(xx)
356356
r = $(f)(x)

src/decorations/intervals.jl

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,25 @@ end
3333

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

36-
function DecoratedInterval(a::Real, b::Real, d::DECORATION)
37-
is_valid_interval(a, b) || return DecoratedInterval(Interval(a,b), ill)
38-
return DecoratedInterval(Interval(a,b), d)
36+
function DecoratedInterval(a::T, b::S, d::DECORATION) where {T<:Real, S<:Real}
37+
BoundsType = promote_type(default_bound(), T, S)
38+
is_valid_interval(a, b) || return DecoratedInterval(Interval{BoundsType}(a, b), ill)
39+
return DecoratedInterval(Interval{BoundsType}(a, b), d)
40+
end
41+
function DecoratedInterval(a::T, b::S, d::DECORATION) where {T<:AbstractFloat, S<:Real}
42+
BoundsType = promote_type(T, S)
43+
is_valid_interval(a, b) || return DecoratedInterval(Interval{BoundsType}(a, b), ill)
44+
return DecoratedInterval(Interval{BoundsType}(a, b), d)
45+
end
46+
function DecoratedInterval(a::T, b::S, d::DECORATION) where {T<:Real, S<:AbstractFloat}
47+
BoundsType = promote_type(T, S)
48+
is_valid_interval(a, b) || return DecoratedInterval(Interval{BoundsType}(a, b), ill)
49+
return DecoratedInterval(Interval{BoundsType}(a, b), d)
50+
end
51+
function DecoratedInterval(a::T, b::S, d::DECORATION) where {T<:AbstractFloat, S<:AbstractFloat}
52+
BoundsType = promote_type(T, S)
53+
is_valid_interval(a, b) || return DecoratedInterval(Interval{BoundsType}(a, b), ill)
54+
return DecoratedInterval(Interval{BoundsType}(a, b), d)
3955
end
4056

4157
DecoratedInterval(a::Real, d::DECORATION) = DecoratedInterval(a, a, d)
@@ -50,9 +66,25 @@ end
5066

5167
DecoratedInterval(I::Interval) = DecoratedInterval{default_bound()}(I)
5268

53-
function DecoratedInterval(a::Real, b::Real)
54-
is_valid_interval(a, b) || return DecoratedInterval(Interval(a,b), ill)
55-
return DecoratedInterval(Interval(a,b))
69+
function DecoratedInterval(a::T, b::S) where {T<:Real, S<:Real}
70+
BoundsType = promote_type(default_bound(), T, S)
71+
is_valid_interval(a, b) || return DecoratedInterval(Interval{BoundsType}(a, b), ill)
72+
return DecoratedInterval(Interval{BoundsType}(a, b))
73+
end
74+
function DecoratedInterval(a::T, b::S) where {T<:AbstractFloat, S<:Real}
75+
BoundsType = promote_type(T, S)
76+
is_valid_interval(a, b) || return DecoratedInterval(Interval{BoundsType}(a, b), ill)
77+
return DecoratedInterval(Interval{BoundsType}(a, b))
78+
end
79+
function DecoratedInterval(a::T, b::S) where {T<:Real, S<:AbstractFloat}
80+
BoundsType = promote_type(T, S)
81+
is_valid_interval(a, b) || return DecoratedInterval(Interval{BoundsType}(a, b), ill)
82+
return DecoratedInterval(Interval{BoundsType}(a, b))
83+
end
84+
function DecoratedInterval(a::T, b::S) where {T<:AbstractFloat, S<:AbstractFloat}
85+
BoundsType = promote_type(T, S)
86+
is_valid_interval(a, b) || return DecoratedInterval(Interval{BoundsType}(a, b), ill)
87+
return DecoratedInterval(Interval{BoundsType}(a, b))
5688
end
5789

5890
DecoratedInterval(a::Real) = DecoratedInterval(a, a)

src/intervals/arithmetic/absmax.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Implement the `min` function of the IEEE Std 1788-2015 (Table 9.1).
2323
"""
2424
function min(a::F, b::F) where {F<:Interval}
2525
(isempty(a) || isempty(b)) && return emptyinterval(F)
26-
return F( min(inf(a), inf(b)), min(sup(a), sup(b)))
26+
return F(min(inf(a), inf(b)), min(sup(a), sup(b)))
2727
end
2828

2929
"""
@@ -33,5 +33,5 @@ Implement the `max` function of the IEEE Std 1788-2015 (Table 9.1).
3333
"""
3434
function max(a::F, b::F) where {F<:Interval}
3535
(isempty(a) || isempty(b)) && return emptyinterval(F)
36-
return F( max(inf(a), inf(b)), max(sup(a), sup(b)))
37-
end
36+
return F(max(inf(a), inf(b)), max(sup(a), sup(b)))
37+
end

src/intervals/arithmetic/basic.jl

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ function +(a::F, b::T) where {T, F<:Interval{T}}
1919
isempty(a) && return emptyinterval(F)
2020
return @round(F, inf(a) + b, sup(a) + b)
2121
end
22-
+(a::Interval{T}, b::S) where {T, S<:Real} = a + Interval{T}(b)
23-
+(b::Real, a::Interval) = a + b
22+
+(a::Interval{T}, b::Real) where {T} = a + interval(T, b)
23+
+(a::Real, b::Interval{T}) where {T} = b + a
2424

2525
function +(a::F, b::F) where {F<:Interval}
2626
(isempty(a) || isempty(b)) && return emptyinterval(F)
@@ -50,8 +50,8 @@ function -(b::T, a::F) where {T, F<:Interval{T}}
5050
isempty(a) && return emptyinterval(F)
5151
return @round(F, b - sup(a), b - inf(a))
5252
end
53-
-(a::F, b::Real) where {F<:Interval} = a - F(b)
54-
-(a::Real, b::F) where {F<:Interval} = F(a) - b
53+
-(a::Interval{T}, b::Real) where {T} = a - interval(T, b)
54+
-(a::Real, b::Interval{T}) where {T} = interval(T, a) - b
5555

5656
function -(a::F, b::F) where {F<:Interval}
5757
(isempty(a) || isempty(b)) && return emptyinterval(F)
@@ -77,18 +77,18 @@ Implement the `mul` function of the IEEE Std 1788-2015 (Table 9.1).
7777
7878
Note: the behavior of the multiplication is flavor dependent for some edge cases.
7979
"""
80-
function *(x::T, a::F) where {T<:Real, F<:Interval{T}}
80+
function *(a::F, b::T) where {T<:Real, F<:Interval{T}}
8181
isempty(a) && return emptyinterval(F)
82-
(isthinzero(a) || iszero(x)) && return zero(F)
82+
(isthinzero(a) || iszero(b)) && return zero(F)
8383

84-
if x 0.0
85-
return @round(F, inf(a)*x, sup(a)*x)
84+
if b 0.0
85+
return @round(F, inf(a)*b, sup(a)*b)
8686
else
87-
return @round(F, sup(a)*x, inf(a)*x)
87+
return @round(F, sup(a)*b, inf(a)*b)
8888
end
8989
end
90-
*(x::T, a::F) where {T<:Real, S, F<:Interval{S}} = Interval{S}(x) * a
91-
*(a::F, x::T) where {T<:Real, S, F<:Interval{S}} = x*a
90+
*(a::Interval{T}, b::Real) where {T} = a * interval(T, b)
91+
*(a::Real, b::Interval{T}) where {T} = b * a
9292

9393
function *(a::F, b::F) where {F<:Interval}
9494
(isempty(a) || isempty(b)) && return emptyinterval(F)
@@ -131,18 +131,18 @@ Implement the `div` function of the IEEE Std 1788-2015 (Table 9.1).
131131
132132
Note: the behavior of the division is flavor dependent for some edge cases.
133133
"""
134-
function /(a::F, x::Real) where {F<:Interval}
134+
function /(a::F, b::Real) where {F<:Interval}
135135
isempty(a) && return emptyinterval(T)
136-
iszero(x) && return div_by_thin_zero(a)
136+
iszero(b) && return div_by_thin_zero(a)
137137

138-
if x 0.0
139-
return @round(F, inf(a)/x, sup(a)/x)
138+
if b 0
139+
return @round(F, inf(a)/b, sup(a)/b)
140140
else
141-
return @round(F, sup(a)/x, inf(a)/x)
141+
return @round(F, sup(a)/b, inf(a)/b)
142142
end
143143
end
144144

145-
/(x::Real, a::Interval) = x*inv(a)
145+
/(a::Real, b::Interval) = a * inv(b)
146146

147147
function /(a::F, b::F) where {T, F<:Interval{T}}
148148
(isempty(a) || isempty(b)) && return emptyinterval(F)

src/intervals/arithmetic/power.jl

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function ^(a::F, n::Integer) where {F<:Interval{BigFloat}}
4040
isempty(a) && return a
4141
iszero(n) && return one(F)
4242
n == 1 && return a
43-
n < 0 && isthinzero(a) && return emptyinterval(F)
43+
(n < 0 && isthinzero(a)) && return emptyinterval(F)
4444

4545
if isodd(n) # odd power
4646
isentire(a) && return a
@@ -98,7 +98,7 @@ function ^(a::F, x::BigFloat) where {F<:Interval{BigFloat}}
9898
a = a domain
9999
(isempty(x) || isempty(a)) && return emptyinterval(F)
100100

101-
xx = F(x)
101+
xx = F(x, x)
102102

103103
lo = @round(F, inf(a)^inf(xx), inf(a)^inf(xx))
104104
lo = (inf(lo) == Inf) ? F(prevfloat(Inf), Inf) : lo
@@ -167,20 +167,10 @@ function ^(a::F, x::Interval) where {F<:Interval{BigFloat}}
167167

168168
(isempty(x) || isempty(a)) && return emptyinterval(F)
169169

170-
lo1 = a^inf(x)
171-
lo2 = a^sup(x)
172-
lo1 = hull(lo1, lo2)
173-
174-
hi1 = a^inf(x)
175-
hi2 = a^sup(x)
176-
hi1 = hull(hi1, hi2)
177-
178-
return hull(lo1, hi1)
170+
return hull(a^inf(x), a^sup(x))
179171
end
180172

181-
function sqr(a::Interval)
182-
return a^2
183-
end
173+
sqr(a::Interval) = a^2
184174

185175
"""
186176
hypot(x::Interval, n::Integer)
@@ -203,13 +193,16 @@ function pow(x::F, n::Integer) where {F<:Interval}
203193
isempty(x) && return x
204194

205195
if iseven(n) && 0 x
196+
xmig = mig(x)
197+
xmag = mag(x)
206198
return hull(zero(x),
207-
Base.power_by_squaring(F(mig(x)), n),
208-
Base.power_by_squaring(F(mag(x)), n)
209-
)
199+
Base.power_by_squaring(F(xmig, xmig), n),
200+
Base.power_by_squaring(F(xmag, xmag), n))
210201
else
211-
return hull( Base.power_by_squaring(F(inf(x)), n),
212-
Base.power_by_squaring(F(sup(x)), n) )
202+
xinf = inf(x)
203+
xsup = sup(x)
204+
return hull(Base.power_by_squaring(F(xinf, xinf), n),
205+
Base.power_by_squaring(F(xsup, xsup), n))
213206
end
214207
end
215208

@@ -262,7 +255,7 @@ for f in (:log, :log2, :log10)
262255
end
263256

264257
function log1p(a::F) where {T, F<:Interval{T}}
265-
domain = Interval{T}(-1, Inf)
258+
domain = F(-1, Inf)
266259
a = a domain
267260

268261
(isempty(a) || sup(a) -one(T)) && return emptyinterval(a)

0 commit comments

Comments
 (0)