Skip to content
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

add prettier printing for duals #193

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 27 additions & 1 deletion src/partials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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