diff --git a/src/dual.jl b/src/dual.jl index 21f6ed5a..36d865d3 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -404,9 +404,8 @@ end ################### function Base.show{N}(io::IO, n::Dual{N}) - print(io, "Dual(", value(n)) - for i in 1:N - print(io, ",", partials(n, i)) - end - print(io, ")") + parts, val = partials(n), value(n) + compact = get(io, :compact, false) + show(io, val) + show(io, parts, true) end diff --git a/src/partials.jl b/src/partials.jl index e070275f..aac5e1c3 100644 --- a/src/partials.jl +++ b/src/partials.jl @@ -211,4 +211,30 @@ end # Pretty Printing # ################### -Base.show{N}(io::IO, p::Partials{N}) = print(io, "Partials", p.values) +const _subscripts = ["₀", "₁", "₂", "₃", "₄", "₅", "₆", "₇", "₈", "₉"] + +Base.show(io::IO, ::MIME"text/plain", p::Partials) = show(io, p) + +function Base.show{N}(io::IO, parts::Partials{N}, showing_dual = false) + compact = get(io, :compact, false) + for (i, p) in enumerate(parts) + if showing_dual == true || i != 1 + if signbit(p) && !isnan(p) + p = -p + print(io, compact ? "-" : " - ") + else + print(io, compact ? "+" : " + ") + end + end + show(io, p) + if !(isa(p, Integer) && !isa(p, Bool) || isa(p, AbstractFloat) && isfinite(p)) + print(io, "*") + end + print(io, "ɛ") + if length(parts) > 1 + for d in reverse(digits(i)) + print(io, _subscripts[d + 1]) + end + end + end +end