Skip to content

Commit e94b3ae

Browse files
authored
fixed eltype for interval (#496)
* fixed eltype for interval * updated Project.toml * added test for numtype and eltype * updated project.toml and added matrix * interval test * added numtype for interval boxes
1 parent 7bc379d commit e94b3ae

File tree

7 files changed

+35
-12
lines changed

7 files changed

+35
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
docs/build/
2+
Manifest.toml

src/IntervalArithmetic.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export
5858
precedes, strictprecedes, , , , , , contains_zero,
5959
entireinterval, isentire, nai, isnai, isthin, iscommon, isatomic,
6060
widen, inf, sup, bisect, mince,
61-
parameters, eps, dist,
61+
parameters, eps, dist, numtype,
6262
midpoint_radius, interval_from_midpoint_radius,
6363
RoundTiesToEven, RoundTiesToAway,
6464
cancelminus, cancelplus, isunbounded,

src/intervals/arithmetic.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,15 @@ end
284284
## Scalar functions on intervals (no directed rounding used)
285285

286286
function mag(a::Interval{T}) where T<:Real
287-
isempty(a) && return convert(eltype(a), NaN)
287+
isempty(a) && return convert(T, NaN)
288288
# r1, r2 = setrounding(T, RoundUp) do
289289
# abs(a.lo), abs(a.hi)
290290
# end
291291
max( abs(a.lo), abs(a.hi) )
292292
end
293293

294294
function mig(a::Interval{T}) where T<:Real
295-
isempty(a) && return convert(eltype(a), NaN)
295+
isempty(a) && return convert(T, NaN)
296296
zero(a.lo) a && return zero(a.lo)
297297
r1, r2 = setrounding(T, RoundDown) do
298298
abs(a.lo), abs(a.hi)
@@ -501,8 +501,8 @@ end
501501
Return the radius of the `Interval` `a`, such that
502502
`a ⊆ m ± radius`, where `m = mid(a)` is the midpoint.
503503
"""
504-
function radius(a::Interval)
505-
isempty(a) && return convert(eltype(a), NaN)
504+
function radius(a::Interval{T}) where {T<:Real}
505+
isempty(a) && return convert(T, NaN)
506506
m = mid(a)
507507
return max(m - a.lo, a.hi - m)
508508
end

src/intervals/functions.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,21 @@ end
1919
Base.literal_pow(::typeof(^), x::Interval{T}, ::Val{p}) where {T,p} = x^p
2020

2121

22-
Base.eltype(x::Interval{T}) where {T<:Real} = T
22+
Base.eltype(x::Interval{T}) where {T<:Real} = Interval{T}
2323

24+
"""
25+
numtype(::Interval{T}) where {T<:Real} = T
26+
27+
Returns the type of the bounds of the interval.
28+
29+
### Example
30+
31+
```julia
32+
julia> numtype(1..2)
33+
Float64
34+
```
35+
"""
36+
numtype(::Interval{T}) where {T<:Real} = T
2437

2538

2639
function ^(a::Interval{BigFloat}, n::Integer)
@@ -359,4 +372,4 @@ end
359372
function nthroot(a::Interval{T}, n::Integer) where T
360373
b = nthroot(bigequiv(a), n)
361374
return convert(Interval{T}, b)
362-
end
375+
end

src/intervals/hyperbolic.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end
3434

3535

3636
function acosh(a::Interval{BigFloat})
37-
domain = Interval(one(eltype(a)), Inf)
37+
domain = Interval(one(BigFloat), Inf)
3838
a = a domain
3939
isempty(a) && return a
4040

@@ -43,7 +43,7 @@ end
4343

4444

4545
function atanh(a::Interval{BigFloat})
46-
domain = Interval(-one(eltype(a)), one(eltype(a)))
46+
domain = Interval(-one(BigFloat), one(BigFloat))
4747
a = a domain
4848

4949
isempty(a) && return a

src/multidim/intervalbox.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ end
4040
eltype(::Type{IntervalBox{N,T}}) where {N,T} = Interval{T} # Note that this is defined for the type
4141

4242

43-
Base.eltype(x::IntervalBox{T}) where {T<:Real} = T
43+
Base.eltype(x::IntervalBox{N, T}) where {N, T<:Real} = Interval{T}
44+
numtype(x::IntervalBox{N, T}) where {N, T<:Real} = T
4445

4546
length(X::IntervalBox{N,T}) where {N,T} = N
4647

@@ -121,13 +122,13 @@ Splits `x` in `n` intervals in each dimension of the same diameter. These
121122
intervals are combined in all possible `IntervalBox`-es, which are returned
122123
as a vector.
123124
"""
124-
@inline mince(x::IntervalBox{N,T}, n::Int) where {N,T} =
125+
@inline mince(x::IntervalBox{N,T}, n::Int) where {N,T} =
125126
mince(x, ntuple(_ -> n, N))
126127

127128
"""
128129
mince(x::IntervalBox, ncuts::::NTuple{N,Int})
129130
130-
Splits `x[i]` in `ncuts[i]` intervals . These intervals are
131+
Splits `x[i]` in `ncuts[i]` intervals . These intervals are
131132
combined in all possible `IntervalBox`-es, which are returned
132133
as a vector.
133134
"""

test/interval_tests/construction.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,14 @@ end
223223
@test typeof(@interval 1 2) == Interval{Float64}
224224
@test typeof(@interval Float32 1 2) == Interval{Float32}
225225
@test typeof(@interval Float16 1 2) == Interval{Float16}
226+
227+
# PR 496
228+
@test eltype(Interval(1, 2)) == Interval{Float64}
229+
@test numtype(Interval(1, 2)) == Float64
230+
@test [1 2; 3 4] * Interval(-1, 1) == [-1..1 -2..2;-3..3 -4..4]
231+
232+
@test eltype(IntervalBox(1..2, 2..3)) == Interval{Float64}
233+
@test numtype(IntervalBox(1..2, 2..3)) == Float64
226234
end
227235

228236
@testset ".. tests" begin

0 commit comments

Comments
 (0)