Skip to content

Commit 9049ca7

Browse files
committed
Cleanup
1 parent 7c46049 commit 9049ca7

File tree

6 files changed

+105
-99
lines changed

6 files changed

+105
-99
lines changed

src/intervals/arithmetic/basic.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ function Base.inv(x::BareInterval{T}) where {T<:NumTypes}
127127
inf(x) < 0 == sup(x) && return @round(T, typemin(T), inv(inf(x)))
128128
inf(x) == 0 < sup(x) && return @round(T, inv(sup(x)), typemax(T))
129129
inf(x) < 0 < sup(x) && return entireinterval(BareInterval{T})
130-
isthinzero(x) && return div_by_thin_zero(_unsafe_bareinterval(T, one(T), one(T)))
130+
# isthinzero(x)
131+
return div_by_thin_zero(_unsafe_bareinterval(T, one(T), one(T)))
131132
end
132133
return @round(T, inv(sup(x)), inv(inf(x)))
133134
end

src/intervals/arithmetic/power.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,8 @@ for f ∈ (:cbrt, :exp, :exp2, :exp10, :expm1)
406406
Base.$f(x::BareInterval{<:Rational}) = $f(float(x))
407407

408408
function Base.$f(x::Interval)
409-
bx = bareinterval(x)
410-
r = $f(bx)
411-
d = min(decoration(r), decoration(x))
409+
r = $f(bareinterval(x))
410+
d = min(decoration(x), decoration(r))
412411
return _unsafe_interval(r, d, isguaranteed(x))
413412
end
414413
end

src/intervals/interval_operations/bisect.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ function bisect(x::BareInterval{T}, α::Real = 0.5) where {T<:NumTypes}
1616
end
1717

1818
function bisect(x::Interval, α::Real = 0.5)
19-
bx = bareinterval(x)
20-
r₁, r₂ = bisect(bx, α)
19+
r₁, r₂ = bisect(bareinterval(x), α)
2120
d₁, d₂ = min(decoration(x), decoration(r₁)), min(decoration(x), decoration(r₂))
2221
t = isguaranteed(x)
2322
return (_unsafe_interval(r₁, d₁, t), _unsafe_interval(r₂, d₂, t))

src/intervals/interval_operations/boolean.jl

Lines changed: 86 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,18 @@ function isequal_interval(x::Interval, y::Interval)
2020
isnai(x) | isnai(y) && return false
2121
return isequal_interval(bareinterval(x), bareinterval(y))
2222
end
23+
isequal_interval(x::Complex{<:Interval}, y::Complex{<:Interval}) = isequal_interval(real(x), real(y)) & isequal_interval(imag(x), imag(y))
24+
isequal_interval(x::Complex{<:Interval}, y::Interval) = isequal_interval(real(x), y) & isthinzero(imag(x))
25+
isequal_interval(x::Interval, y::Complex{<:Interval}) = isequal_interval(x, real(y)) & isthinzero(imag(y))
26+
27+
function isequal_interval(x::AbstractVector, y::AbstractVector)
28+
n = length(x)
29+
m = length(y)
30+
n == m || return throw(DimensionMismatch("dimensions must match: x has length $n, y has length $m"))
31+
return all(t -> isequal_interval(t[1], t[2]), zip(x, y))
32+
end
2333

2434
isequal_interval(x, y, z, w...) = isequal_interval(x, y) & isequal_interval(y, z, w...)
25-
isequal_interval(x::Complex, y::Complex) = isequal_interval(real(x), real(y)) & isequal_interval(imag(x), imag(y))
26-
isequal_interval(x::Complex, y::Real) = isequal_interval(real(x), y) & isthinzero(imag(x))
27-
isequal_interval(x::Real, y::Complex) = isequal_interval(x, real(y)) & isthinzero(imag(y))
2835

2936
isequal_interval(x) = Base.Fix2(isequal_interval, x)
3037

@@ -41,16 +48,46 @@ function issubset_interval(x::Interval, y::Interval)
4148
isnai(x) | isnai(y) && return false
4249
return issubset_interval(bareinterval(x), bareinterval(y))
4350
end
51+
issubset_interval(x::Complex{<:Interval}, y::Complex{<:Interval}) = issubset_interval(real(x), real(y)) & issubset_interval(imag(x), imag(y))
52+
issubset_interval(x::Complex{<:Interval}, y::Interval) = issubset_interval(real(x), y) & isthinzero(imag(x))
53+
issubset_interval(x::Interval, y::Complex{<:Interval}) = issubset_interval(x, real(y)) & in_interval(0, imag(y))
54+
55+
function issubset_interval(x::AbstractVector, y::AbstractVector)
56+
n = length(x)
57+
m = length(y)
58+
n == m || return throw(DimensionMismatch("dimensions must match: x has length $n, y has length $m"))
59+
return all(t -> issubset_interval(t[1], t[2]), zip(x, y))
60+
end
4461

4562
issubset_interval(x, y, z, w...) = issubset_interval(x, y) & issubset_interval(y, z, w...)
46-
issubset_interval(x::Complex, y::Complex) = issubset_interval(real(x), real(y)) & issubset_interval(imag(x), imag(y))
47-
issubset_interval(x::Complex, y::Real) = issubset_interval(real(x), y) & isthinzero(imag(x))
48-
issubset_interval(x::Real, y::Complex) = issubset_interval(x, real(y)) & in_interval(0, imag(y))
63+
64+
"""
65+
isstrictsubset(x, y)
66+
67+
Test whether `x` is a strict subset of `y`. If `x` and `y` are intervals, this
68+
is semantically equivalent to `isinterior(x, y)`. If `x` and `y` are vectors,
69+
`x` must be a subset of `y` with at least one of their component being a strict
70+
subset.
71+
72+
See also: [`isinterior`](@ref).
73+
"""
74+
isstrictsubset(x::BareInterval, y::BareInterval) = isinterior(x, y)
75+
76+
isstrictsubset(x::Interval, y::Interval) = isinterior(x, y)
77+
isstrictsubset(x::Complex{<:Interval}, y::Complex{<:Interval}) =
78+
(isstrictsubset(real(x), real(y)) & issubset_interval(imag(x), imag(y))) | (issubset_interval(real(x), real(y)) & isstrictsubset(imag(x), imag(y)))
79+
isstrictsubset(x::Complex{<:Interval}, y::Interval) = isstrictsubset(real(x), y) & isthinzero(imag(x))
80+
isstrictsubset(x::Interval, y::Complex{<:Interval}) = isstrictsubset(x, real(y)) & in_interval(0, imag(y))
81+
82+
isstrictsubset(x::AbstractVector, y::AbstractVector) = issubset_interval(x, y) & any(t -> isstrictsubset(t[1], t[2]), zip(x, y))
83+
84+
isstrictsubset(x, y, z, w...) = isstrictsubset(x, y) & isstrictsubset(y, z, w...)
4985

5086
"""
5187
isinterior(x, y)
5288
53-
Test whether `x` is in the interior of `y`.
89+
Test whether `x` is in the interior of `y`. If `x` and `y` are intervals, this
90+
is semantically equivalent to `isstrictsubset(x, y)`.
5491
5592
Implement the `interior` function of the IEEE Standard 1788-2015 (Table 9.3).
5693
@@ -63,6 +100,9 @@ function isinterior(x::Interval, y::Interval)
63100
isnai(x) | isnai(y) && return false
64101
return isinterior(bareinterval(x), bareinterval(y))
65102
end
103+
isinterior(x::Complex{<:Interval}, y::Complex{<:Interval}) = isinterior(real(x), real(y)) & isinterior(imag(x), imag(y))
104+
isinterior(::Complex{<:Interval}, ::Interval) = false
105+
isinterior(x::Interval, y::Complex{<:Interval}) = isinterior(x, real(y)) & isinterior(zero(x), imag(y))
66106

67107
function isinterior(x::AbstractVector, y::AbstractVector)
68108
n = length(x)
@@ -72,33 +112,33 @@ function isinterior(x::AbstractVector, y::AbstractVector)
72112
end
73113

74114
isinterior(x, y, z, w...) = isinterior(x, y) & isinterior(y, z, w...)
75-
isinterior(x::Complex, y::Complex) =
76-
(isinterior(real(x), real(y)) & issubset_interval(imag(x), imag(y))) |
77-
(issubset_interval(real(x), real(y)) & isinterior(imag(x), imag(y)))
78-
isinterior(x::Complex, y::Real) = isinterior(real(x), y) & isthinzero(imag(x))
79-
isinterior(x::Real, y::Complex) = isinterior(x, real(y)) & in_interval(0, imag(y))
80115

81116
"""
82-
isstrictsubset(x, y)
117+
isdisjoint_interval(x, y)
83118
84-
Test whether `x` is a strict subset of `y`. If `x` and `y` are intervals, this
85-
is semantically equivalent to `isinterior(x, y)`. If `x` and `y` are vectors, at
86-
least one component of `x` must be in the interior of `y`.
119+
Test whether `x` and `y` have no common elements.
87120
88-
See also: [`isinterior`](@ref).
121+
Implement the `disjoint` function of the IEEE Standard 1788-2015 (Table 9.3).
89122
"""
90-
isstrictsubset(x::BareInterval, y::BareInterval) = isinterior(x, y)
123+
isdisjoint_interval(x::BareInterval, y::BareInterval) =
124+
isempty_interval(x) | isempty_interval(y) | _strictlessprime(sup(y), inf(x)) | _strictlessprime(sup(x), inf(y))
91125

92-
isstrictsubset(x::Interval, y::Interval) = isinterior(x, y)
126+
function isdisjoint_interval(x::Interval, y::Interval)
127+
isnai(x) | isnai(y) && return false
128+
return isdisjoint_interval(bareinterval(x), bareinterval(y))
129+
end
130+
isdisjoint_interval(x::Complex{<:Interval}, y::Complex{<:Interval}) = isdisjoint_interval(real(x), real(y)) | isdisjoint_interval(imag(x), imag(y))
131+
isdisjoint_interval(x::Complex{<:Interval}, y::Interval) = isdisjoint_interval(real(x), y) | !in_interval(0, imag(x))
132+
isdisjoint_interval(x::Interval, y::Complex{<:Interval}) = isdisjoint_interval(x, real(y)) | !in_interval(0, imag(y))
93133

94-
isstrictsubset(x::AbstractVector, y::AbstractVector) = isinterior(x, y) & any(t -> isinterior(t[1], t[2]), zip(x, y))
134+
function isdisjoint_interval(x::AbstractVector, y::AbstractVector)
135+
n = length(x)
136+
m = length(y)
137+
n == m || return throw(DimensionMismatch("dimensions must match: x has length $n, y has length $m"))
138+
return any(t -> isdisjoint_interval(t[1], t[2]), zip(x, y))
139+
end
95140

96-
isstrictsubset(x, y, z, w...) = isstrictsubset(x, y) & isstrictsubset(y, z, w...)
97-
isstrictsubset(x::Complex, y::Complex) =
98-
(isstrictsubset(real(x), real(y)) & issubset_interval(imag(x), imag(y))) |
99-
(issubset_interval(real(x), real(y)) & isstrictsubset(imag(x), imag(y)))
100-
isstrictsubset(x::Complex, y::Real) = isstrictsubset(real(x), y) & isthinzero(imag(x))
101-
isstrictsubset(x::Real, y::Complex) = isstrictsubset(x, real(y)) & in_interval(0, imag(y))
141+
isdisjoint_interval(x, y, z, w...) = isdisjoint_interval(x, y) & isdisjoint_interval(y, z, w...)
102142

103143
"""
104144
isweakless(x, y)
@@ -159,24 +199,6 @@ function strictprecedes(x::Interval, y::Interval)
159199
return strictprecedes(bareinterval(x), bareinterval(y))
160200
end
161201

162-
"""
163-
isdisjoint_interval(x, y)
164-
165-
Test whether `x` and `y` have no common elements.
166-
167-
Implement the `disjoint` function of the IEEE Standard 1788-2015 (Table 9.3).
168-
"""
169-
isdisjoint_interval(a::BareInterval, b::BareInterval) =
170-
isempty_interval(a) | isempty_interval(b) | _strictlessprime(sup(b), inf(a)) | _strictlessprime(sup(a), inf(b))
171-
172-
function isdisjoint_interval(x::Interval, y::Interval)
173-
isnai(x) | isnai(y) && return false
174-
return isdisjoint_interval(bareinterval(x), bareinterval(y))
175-
end
176-
177-
isdisjoint_interval(x::Complex, y::Complex) =
178-
isdisjoint_interval(real(x), real(y)) | isdisjoint_interval(imag(x), imag(y))
179-
180202
"""
181203
in_interval(x, y)
182204
@@ -189,22 +211,19 @@ function in_interval(x::Number, y::BareInterval)
189211
return inf(y) x sup(y)
190212
end
191213
in_interval(x::Complex, y::BareInterval) = in_interval(real(x), y) & iszero(imag(x))
214+
in_interval(x::Interval, y::BareInterval) = throw(MethodError(in_interval, (x, y)))
215+
in_interval(::BareInterval, ::BareInterval) =
216+
throw(ArgumentError("`in_interval` is purposely not supported for two interval arguments. See instead `issubset_interval`"))
192217

193218
function in_interval(x::Number, y::Interval)
194219
isnai(y) && return false
195220
return in_interval(x, bareinterval(y))
196221
end
197-
198-
in_interval(::BareInterval, ::BareInterval) =
199-
throw(ArgumentError("`in_interval` is purposely not supported for two interval arguments. See instead `issubset_interval`"))
200-
222+
in_interval(x::Number, y::Complex{<:Interval}) = in_interval(x, real(y)) & in_interval(0, imag(y))
223+
in_interval(x::Complex, y::Complex{<:Interval}) = in_interval(real(x), real(y)) & in_interval(imag(x), imag(y))
201224
in_interval(::Interval, ::Interval) =
202225
throw(ArgumentError("`in_interval` is purposely not supported for two interval arguments. See instead `issubset_interval`"))
203226

204-
in_interval(x::Complex, y::Complex) = in_interval(real(x), real(y)) & in_interval(imag(x), imag(y))
205-
in_interval(x::Complex, y::Number) = in_interval(real(x), y) & iszero(imag(x))
206-
in_interval(x::Number, y::Complex) = in_interval(x, real(y)) & in_interval(0, imag(y))
207-
208227
in_interval(x) = Base.Fix2(in_interval, x)
209228

210229
"""
@@ -220,8 +239,9 @@ function isempty_interval(x::Interval)
220239
isnai(x) && return false
221240
return isempty_interval(bareinterval(x))
222241
end
242+
isempty_interval(x::Complex{<:Interval}) = isempty_interval(real(x)) | isempty_interval(imag(x))
223243

224-
isempty_interval(x::Complex) = isempty_interval(real(x)) & isempty_interval(imag(x))
244+
isempty_interval(x::AbstractVector) = any(isempty_interval, x)
225245

226246
"""
227247
isentire_interval(x)
@@ -236,8 +256,7 @@ function isentire_interval(x::Interval)
236256
isnai(x) && return false
237257
return isentire_interval(bareinterval(x))
238258
end
239-
240-
isentire_interval(x::Complex) = isentire_interval(real(x)) & isentire_interval(imag(x))
259+
isentire_interval(x::Complex{<:Interval}) = isentire_interval(real(x)) & isentire_interval(imag(x))
241260

242261
"""
243262
isnai(x)
@@ -247,8 +266,7 @@ Test whether `x` is an NaI (Not an Interval).
247266
isnai(::BareInterval) = false
248267

249268
isnai(x::Interval) = decoration(x) == ill
250-
251-
isnai(x::Complex) = isnai(real(x)) & isnai(imag(x))
269+
isnai(x::Complex{<:Interval}) = isnai(real(x)) & isnai(imag(x))
252270

253271
"""
254272
isbounded(x)
@@ -261,8 +279,7 @@ function isbounded(x::Interval)
261279
isnai(x) && return false
262280
return isbounded(bareinterval(x))
263281
end
264-
265-
isbounded(x::Complex) = isbounded(real(x)) & isbounded(imag(x))
282+
isbounded(x::Complex{<:Interval}) = isbounded(real(x)) & isbounded(imag(x))
266283

267284
"""
268285
isunbounded(x)
@@ -275,8 +292,7 @@ function isunbounded(x::Interval)
275292
isnai(x) && return false
276293
return isunbounded(bareinterval(x))
277294
end
278-
279-
isunbounded(x::Complex) = isunbounded(real(x)) | isunbounded(imag(x))
295+
isunbounded(x::Complex{<:Interval}) = isunbounded(real(x)) | isunbounded(imag(x))
280296

281297
"""
282298
iscommon(x)
@@ -292,8 +308,7 @@ function iscommon(x::Interval)
292308
isnai(x) && return false
293309
return iscommon(bareinterval(x))
294310
end
295-
296-
iscommon(x::Complex) = iscommon(real(x)) & iscommon(imag(x))
311+
iscommon(x::Complex{<:Interval}) = iscommon(real(x)) & iscommon(imag(x))
297312

298313
"""
299314
isatomic(x)
@@ -309,8 +324,7 @@ function isatomic(x::Interval)
309324
isnai(x) && return false
310325
return isatomic(bareinterval(x))
311326
end
312-
313-
isatomic(x::Complex) = isatomic(real(x)) & isatomic(imag(x))
327+
isatomic(x::Complex{<:Interval}) = isatomic(real(x)) & isatomic(imag(x))
314328

315329
"""
316330
isthin(x)
@@ -325,8 +339,7 @@ function isthin(x::Interval)
325339
isnai(x) && return false
326340
return isthin(bareinterval(x))
327341
end
328-
329-
isthin(x::Complex) = isthin(real(x)) & isthin(imag(x))
342+
isthin(x::Complex{<:Interval}) = isthin(real(x)) & isthin(imag(x))
330343

331344
"""
332345
isthin(x, y)
@@ -335,17 +348,14 @@ Test whether `x` contains only `y`.
335348
"""
336349
isthin(x::BareInterval, y::Number) = inf(x) == sup(x) == y
337350
isthin(x::BareInterval, y::Complex) = isthin(x, real(y)) & iszero(imag(y))
351+
isthin(x::BareInterval, y::Interval) = throw(MethodError(isthin, (x, y)))
338352

339353
function isthin(x::Interval, y::Number)
340354
isnai(x) && return false
341355
return isthin(bareinterval(x), y)
342356
end
343-
344-
isthin(x::Complex, y::Complex) = isthin(real(x), real(y)) & isthin(imag(x), imag(y))
345-
isthin(x::Complex, y::Number) = isthin(real(x), real(y)) & isthinzero(imag(x))
346-
isthin(x::Number, y::Complex) = isthin(real(x), real(y)) & iszero(imag(y))
347-
348-
isthin(x::BareInterval, y::Interval) = throw(MethodError(isthin, (x, y)))
357+
isthin(x::Complex{<:Interval}, y::Number) = isthin(real(x), y) & isthinzero(imag(x))
358+
isthin(x::Complex{<:Interval}, y::Complex) = isthin(real(x), real(y)) & isthin(imag(x), imag(y))
349359

350360
"""
351361
isthinzero(x)
@@ -358,8 +368,7 @@ function isthinzero(x::Interval)
358368
isnai(x) && return false
359369
return isthinzero(bareinterval(x))
360370
end
361-
362-
isthinzero(x::Complex) = isthinzero(real(x)) & isthinzero(imag(x))
371+
isthinzero(x::Complex{<:Interval}) = isthinzero(real(x)) & isthinzero(imag(x))
363372

364373
"""
365374
isthinone(x)
@@ -372,8 +381,7 @@ function isthinone(x::Interval)
372381
isnai(x) && return false
373382
return isthinone(bareinterval(x))
374383
end
375-
376-
isthinone(x::Complex) = isthinone(real(x)) & isthinzero(imag(x))
384+
isthinone(x::Complex{<:Interval}) = isthinone(real(x)) & isthinzero(imag(x))
377385

378386
"""
379387
isthininteger(x)
@@ -386,5 +394,4 @@ function isthininteger(x::Interval)
386394
isnai(x) && return false
387395
return isthininteger(bareinterval(x))
388396
end
389-
390-
isthininteger(x::Complex) = isthininteger(real(x)) & isthinzero(imag(x))
397+
isthininteger(x::Complex{<:Interval}) = isthininteger(real(x)) & isthinzero(imag(x))

src/intervals/interval_operations/extended_div.jl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ Implement the `mulRevToPair` function of the IEEE Standard 1788-2015 (Section 10
1010
"""
1111
function extended_div(x::BareInterval{T}, y::BareInterval{T}) where {T<:NumTypes}
1212
ylo, yhi = bounds(y)
13-
if 0 < yhi && 0 > ylo && !in_interval(0, x)
14-
if sup(x) < 0
15-
return (x / _unsafe_bareinterval(T, zero(T), yhi), x / _unsafe_bareinterval(T, ylo, zero(T)))
16-
elseif inf(x) > 0
17-
return (x / _unsafe_bareinterval(T, ylo, zero(T)), x / _unsafe_bareinterval(T, zero(T), yhi))
18-
end
13+
if ylo < 0 < yhi && !in_interval(0, x)
14+
sup(x) < 0 && return (x / _unsafe_bareinterval(T, zero(T), yhi), x / _unsafe_bareinterval(T, ylo, zero(T)))
15+
# inf(x) > 0
16+
return (x / _unsafe_bareinterval(T, ylo, zero(T)), x / _unsafe_bareinterval(T, zero(T), yhi))
1917
elseif in_interval(0, x) && in_interval(0, y)
2018
return (entireinterval(BareInterval{T}), emptyinterval(BareInterval{T}))
2119
else
@@ -26,10 +24,10 @@ end
2624
function extended_div(x::Interval, y::Interval)
2725
bx = bareinterval(x)
2826
by = bareinterval(y)
29-
r1, r2 = extended_div(bx, by)
27+
r₁, r₂ = extended_div(bx, by)
3028
d = min(decoration(x), decoration(y))
31-
d1 = min(d, decoration(r1), ifelse(!isempty_interval(bx) & !isempty_interval(by) & !in_interval(0, x), d, trv))
32-
d2 = min(d, decoration(r2), trv)
29+
d₁ = min(d, decoration(r₁), ifelse(!isempty_interval(bx) & !isempty_interval(by) & !in_interval(0, bx), d, trv))
30+
d₂ = min(d, decoration(r₂), trv)
3331
t = isguaranteed(x) & isguaranteed(y)
34-
return _unsafe_interval(r1, d1, t), _unsafe_interval(r2, d2, t)
32+
return _unsafe_interval(r₁, d₁, t), _unsafe_interval(r₂, d₂, t)
3533
end

src/intervals/intervals.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ include("interval_operations/constants.jl")
2828
include("interval_operations/extended_div.jl")
2929
export extended_div
3030
include("interval_operations/boolean.jl")
31-
export isempty_interval, isentire_interval, isequal_interval, in_interval,
32-
issubset_interval, isinterior, precedes, strictprecedes,
33-
isdisjoint_interval, isweakless, isstrictless, isthin, isthinzero, isthinone,
34-
isbounded, isunbounded, isnai, iscommon, isatomic
31+
export isequal_interval, issubset_interval, isstrictsubset, isinterior,
32+
isdisjoint_interval, isweakless, isstrictless, precedes, strictprecedes,
33+
in_interval, isempty_interval, isentire_interval, isnai, isbounded,
34+
isunbounded, iscommon, isatomic, isthin, isthinzero, isthinone,
35+
isthininteger
36+
3537
include("interval_operations/overlap.jl")
3638
export Overlap, overlap
3739
include("interval_operations/numeric.jl")

0 commit comments

Comments
 (0)