Skip to content

Commit e0333ae

Browse files
authored
Merge pull request #451 from lucaferranti/lf/displaying
fixed displaying issues
2 parents f0eebb6 + 14ceca4 commit e0333ae

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "IntervalArithmetic"
22
uuid = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
33
repo = "https://github.com/JuliaIntervals/IntervalArithmetic.jl.git"
4-
version = "0.17.8"
4+
version = "0.18.0"
55

66
[deps]
77
CRlibm = "96374032-68de-5a5b-8d9e-752f78720389"

src/display.jl

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ function Base.show(io::IO, params::DisplayParameters)
1111
print(io, "- significant figures: $(params.sigfigs)")
1212
end
1313

14-
Base.show(io::IO, x::Complex{<:Interval}) = print(io, x.re, " + ", x.im, "im")
15-
1614
const display_params = DisplayParameters(:standard, false, 6)
1715

1816
const display_options = (:standard, :full, :midpoint)
@@ -274,6 +272,11 @@ function subscriptify(n::Integer)
274272
join( [Char(subscript_0 + i) for i in dig])
275273
end
276274

275+
function superscriptify(n::Integer)
276+
exps = ['', '¹', '²', '³', '', '', '', '', '', '']
277+
dig = reverse(digits(n))
278+
return join([exps[d+1] for d in dig])
279+
end
277280

278281
# fall-back:
279282
representation(a::Interval{T}, format=nothing) where T =
@@ -319,29 +322,61 @@ function representation(a::DecoratedInterval{T}, format=nothing) where T
319322
end
320323

321324

322-
function representation(X::IntervalBox, format=nothing)
325+
function representation(X::IntervalBox{N, T}, format=nothing) where {N, T}
323326

324327
if format == nothing
325328
format = display_params.format # default
326329
end
327330

328-
if display_params.format == :full
329-
return string("IntervalBox(", join(X.v, ", "), ")")
331+
n = format == :full ? N : superscriptify(N)
332+
333+
if isempty(X)
334+
format == :full && return string("IntervalBox(∅, ", n, ")")
335+
return string("", n)
336+
end
337+
338+
x = first(X)
339+
if all(==(x), X)
340+
if format == :full
341+
return string("IntervalBox(", representation(x, format), ", ", n, ")")
342+
elseif format == :midpoint
343+
return string("(", representation(x, format), ")", n)
344+
else
345+
return string(representation(x, format), n)
346+
end
347+
end
330348

349+
if format == :full
350+
full_str = representation.(X.v, :full)
351+
return string("IntervalBox(", join(full_str, ", "), ")")
352+
elseif format == :midpoint
353+
return string("(", join(X.v, ") × ("), ")")
331354
else
332355
return join(X.v, " × ")
333356
end
334357

335358
end
336359

360+
function representation(x::Complex{<:Interval}, format=nothing)
361+
362+
if format == nothing
363+
format = display_params.format
364+
end
365+
366+
format == :midpoint && return string('(', x.re, ')', " + ", '(', x.im, ')', "im")
367+
368+
return string(x.re, " + ", x.im, "im")
369+
end
337370

338371
for T in (Interval, DecoratedInterval)
339372
@eval show(io::IO, a::$T{S}) where S = print(io, representation(a))
340373
@eval showfull(io::IO, a::$T{S}) where S = print(io, representation(a, :full))
341374
@eval showfull(a::$T{S}) where S = showfull(stdout, a)
342375
end
343376

344-
T = IntervalBox
345-
@eval show(io::IO, a::$T) = print(io, representation(a))
346-
@eval show(io::IO, ::MIME"text/plain", a::$T) = print(io, representation(a))
347-
@eval showfull(io::IO, a::$T) = print(io, representation(a, :full))
377+
for T in (IntervalBox, Complex{<:Interval})
378+
@eval show(io::IO, a::$T) = print(io, representation(a))
379+
@eval show(io::IO, ::MIME"text/plain", a::$T) = print(io, representation(a))
380+
@eval showfull(io::IO, a::$T) = print(io, representation(a, :full))
381+
@eval showfull(a::$T) = showfull(stdout, a)
382+
end

test/display_tests/display.jl

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ setprecision(Interval, Float64)
9090
@test typeof(a) == Complex{Interval{Float64}}
9191
setformat(:standard)
9292
@test string(a) == "[0, 2] + [1, 1]im"
93+
94+
setformat(:midpoint)
95+
@test string(a) == "(1 ± 1) + (1 ± 0)im"
9396
end
9497

9598
setprecision(Interval, 256)
@@ -169,11 +172,31 @@ setprecision(Interval, Float64)
169172
@test string(X) == "[1.09999, 1.20001] × [2.09999, 2.20001]"
170173

171174
X = IntervalBox(-Inf..Inf, -Inf..Inf)
172-
@test string(X) == "[-∞, ∞] × [-∞, ∞]"
175+
@test string(X) == "[-∞, ∞]²"
176+
177+
setformat(:full)
178+
@test string(X) == "IntervalBox(Interval(-Inf, Inf), 2)"
179+
180+
181+
setformat(:standard)
182+
a = IntervalBox(1..2, 2..3)
183+
@test string(a) == "[1, 2] × [2, 3]"
184+
185+
b = IntervalBox(emptyinterval(), 2)
186+
@test string(b) == "∅²"
187+
188+
c = IntervalBox(1..2, 1)
189+
@test string(c) == "[1, 2]¹"
173190

174191
setformat(:full)
175-
@test string(X) == "IntervalBox(Interval(-Inf, Inf), Interval(-Inf, Inf))"
192+
@test string(a) == "IntervalBox(Interval(1.0, 2.0), Interval(2.0, 3.0))"
193+
@test string(b) == "IntervalBox(∅, 2)"
194+
@test string(c) == "IntervalBox(Interval(1.0, 2.0), 1)"
176195

196+
setformat(:midpoint)
197+
@test string(a) == "(1.5 ± 0.5) × (2.5 ± 0.5)"
198+
@test string(b) == "∅²"
199+
@test string(c) == "(1.5 ± 0.5)¹"
177200
end
178201
end
179202

@@ -200,6 +223,14 @@ end
200223
setformat(decorations=true)
201224
@test string(x) == "[0, 1]₁₂₈_def"
202225

226+
a = IntervalBox(1..2, 2..3)
227+
b = IntervalBox(emptyinterval(), 2)
228+
c = IntervalBox(1..2, 1)
229+
230+
@test sprint(showfull, a) == "IntervalBox(Interval(1.0, 2.0), Interval(2.0, 3.0))"
231+
@test sprint(showfull, b) == "IntervalBox(∅, 2)"
232+
@test sprint(showfull, c) == "IntervalBox(Interval(1.0, 2.0), 1)"
233+
203234
end
204235

205236
@testset "@format tests" begin

0 commit comments

Comments
 (0)