-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Add display mode capability #115
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9a80c10
Add rudimentary display mode capability
dpsanders 2aaeda3
Add mechanism to change output precision
dpsanders 5098abc
Faster mechanism for changing precision display. Uses type-instabilit…
dpsanders 64788b5
Export display_mode
dpsanders 7fa8552
Add decoration display
dpsanders c92e83a
Fix display of decorated intervals
dpsanders 07245f4
Added round_string to return string representation of number to certa…
dpsanders c5c6884
Use MPFR rounding for display. Rename display options
dpsanders 9f3f780
Remove output functions from intervals.jl
dpsanders 47a9a18
Add showall function that shows same as :full option
dpsanders 8cbfd6c
Add error if wrong format option given
dpsanders d777029
Add displaymode tests
dpsanders 279c8c5
Add display.jl tests
dpsanders File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
type DisplayParameters | ||
format::Symbol | ||
decorations::Bool | ||
sigfigs::Int | ||
end | ||
|
||
const display_params = DisplayParameters(:standard, false, 6) | ||
|
||
const display_options = [:standard, :full, :midpoint] | ||
|
||
doc""" | ||
displaymode(;kw) | ||
|
||
`displaymode` changes how intervals are displayed using keyword arguments. | ||
The following options are available: | ||
|
||
- `format`: interval output format | ||
|
||
- `:standard`: `[1, 2]` | ||
- `:full`: `Interval(1, 2)` | ||
- `:midpoint`: 1.5 ± 0.5 | ||
|
||
- `sigfigs`: number of significant figures to show in `standard` mode | ||
|
||
- `decorations` (boolean): show decorations or not | ||
""" | ||
function displaymode(;decorations=nothing, format=nothing, sigfigs=-1) | ||
if format != nothing | ||
|
||
if format in display_options | ||
display_params.format = format | ||
else | ||
throw(ArgumentError("Allowed format option is one of $display_options.")) | ||
end | ||
|
||
end | ||
|
||
if decorations != nothing | ||
display_params.decorations = decorations | ||
end | ||
|
||
if sigfigs >= 0 | ||
display_params.sigfigs = sigfigs | ||
end | ||
end | ||
|
||
|
||
## Output | ||
|
||
# round to given number of signficant digits | ||
# basic structure taken from base/mpfr.jl | ||
function round_string(x::BigFloat, digits::Int, r::RoundingMode) | ||
|
||
lng = digits + Int32(8) | ||
buf = Array(UInt8, lng + 1) | ||
|
||
lng = ccall((:mpfr_snprintf,:libmpfr), Int32, | ||
(Ptr{UInt8}, Culong, Ptr{UInt8}, Int32, Ptr{BigFloat}...), | ||
buf, lng + 1, "%.$(digits)R*g", Base.MPFR.to_mpfr(r), &x) | ||
|
||
return bytestring(pointer(buf)) | ||
end | ||
|
||
round_string(x::Real, digits::Int, r::RoundingMode) = round_string(big(x), digits, r) | ||
|
||
|
||
function representation(a::Interval, format=nothing) | ||
if isempty(a) | ||
return "∅" | ||
end | ||
|
||
if format == nothing | ||
format = display_params.format # default | ||
end | ||
|
||
sigfigs = display_params.sigfigs | ||
|
||
local output | ||
|
||
if format == :standard | ||
|
||
aa = round_string(a.lo, sigfigs, RoundDown) | ||
bb = round_string(a.hi, sigfigs, RoundUp) | ||
|
||
output = "[$aa, $bb]" | ||
output = replace(output, "inf", "∞") | ||
output = replace(output, "Inf", "∞") | ||
|
||
elseif format == :full | ||
output = "Interval($(a.lo), $(a.hi))" | ||
|
||
elseif format == :midpoint | ||
m = round_string(mid(a), sigfigs, RoundNearest) | ||
r = round_string(radius(a), sigfigs, RoundUp) | ||
output = "$m ± $r" | ||
end | ||
|
||
output | ||
end | ||
|
||
function representation(a::Interval{BigFloat}) | ||
if display_params.format == :standard | ||
string( invoke(representation, (Interval,), a), | ||
subscriptify(precision(a.lo)) ) | ||
|
||
elseif display_params.format == :full | ||
invoke(representation, (Interval,), a) | ||
end | ||
end | ||
|
||
function representation(a::DecoratedInterval) | ||
|
||
if display_params.format==:full | ||
return "DecoratedInterval($(interval_part(a)), $(decoration(a)))" | ||
end | ||
|
||
interval = representation(interval_part(a)) | ||
|
||
if display_params.decorations | ||
string(interval, "_", decoration(a)) | ||
else | ||
interval | ||
end | ||
|
||
end | ||
|
||
show(io::IO, a::Interval) = print(io, representation(a)) | ||
show(io::IO, a::DecoratedInterval) = print(io, representation(a)) | ||
|
||
showall(io::IO, a::Interval) = print(io, representation(a, :full)) | ||
|
||
function subscriptify(n::Int) | ||
subscript_digits = [c for c in "₀₁₂₃₄₅₆₇₈₉"] | ||
dig = reverse(digits(n)) | ||
join([subscript_digits[i+1] for i in dig]) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
using ValidatedNumerics | ||
using FactCheck | ||
|
||
setprecision(Interval, Float64) | ||
|
||
facts("displaymode tests") do | ||
|
||
context("Interval") do | ||
|
||
a = 1..2 | ||
b = -1.1..1.3 | ||
c = Interval(pi) | ||
d = @interval(π) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to add some tests with |
||
|
||
context("6 sig figs") do | ||
displaymode(format=:standard, sigfigs=6) | ||
|
||
@fact string(a) --> "[1, 2]" | ||
@fact string(b) --> "[-1.10001, 1.30001]" | ||
@fact string(c) --> "[3.14159, 3.1416]" | ||
@fact string(d) --> "[3.14159, 3.1416]" | ||
end | ||
|
||
context("10 sig figs") do | ||
displaymode(sigfigs=10) | ||
|
||
@fact string(a) --> "[1, 2]" | ||
@fact string(b) --> "[-1.100000001, 1.300000001]" | ||
@fact string(c) --> "[3.141592653, 3.141592654]" | ||
@fact string(d) --> "[3.141592653, 3.141592654]" | ||
end | ||
|
||
context("20 sig figs") do | ||
displaymode(sigfigs=20) | ||
|
||
@fact string(a) --> "[1, 2]" | ||
@fact string(b) --> "[-1.1000000000000000889, 1.3000000000000000445]" | ||
@fact string(c) --> "[3.1415926535897931159, 3.141592653589793116]" | ||
@fact string(d) --> "[3.1415926535897931159, 3.1415926535897935601]" | ||
end | ||
|
||
context("Full") do | ||
displaymode(format=:full) | ||
|
||
@fact string(a) --> "Interval(1.0, 2.0)" | ||
@fact string(b) --> "Interval(-1.1, 1.3)" | ||
@fact string(c) --> "Interval(3.141592653589793, 3.141592653589793)" | ||
@fact string(d) --> "Interval(3.141592653589793, 3.1415926535897936)" | ||
end | ||
|
||
context("Midpoint") do | ||
displaymode(format=:midpoint, sigfigs=6) | ||
|
||
@fact string(a) --> "1.5 ± 0.5" | ||
@fact string(b) --> "0.1 ± 1.20001" | ||
@fact string(c) --> "3.14159 ± 0" | ||
@fact string(d) --> "3.14159 ± 4.4409e-16" | ||
end | ||
|
||
|
||
end | ||
|
||
context("DecoratedInterval") do | ||
a = @decorated(1, 2) | ||
|
||
displaymode(format=:standard, decorations=false) | ||
|
||
@fact string(a) --> "[1, 2]" | ||
|
||
displaymode(format=:standard, decorations=true) | ||
|
||
@fact string(a) --> "[1, 2]_com" | ||
|
||
end | ||
|
||
context("IntervalBox") do | ||
displaymode(sigfigs=6) | ||
|
||
X = IntervalBox(1..2, 3..4) | ||
@fact string(X) --> "[1, 2] × [3, 4]" | ||
|
||
X = IntervalBox(1.1..1.2, 2.1..2.2) | ||
@fact string(X) --> "[1.09999, 1.20001] × [2.09999, 2.20001]" | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think using
Interval(0.1,0.2)
yields clearer examples.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... or any other which is not an
Interval
with integers.