Skip to content
Merged
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
6 changes: 4 additions & 2 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ function show(io::IO, m::AbstractMonomial)
end
end

should_print_coefficient(x) = true # By default, just print all coefficients
should_print_coefficient(x::Number) = !isone(x) # For numbers, we omit any "one" coefficients
print_coefficient(io::IO, coeff::Real) = print(io, coeff)
print_coefficient(io::IO, coeff) = print(io, "(", coeff, ")")

function Base.show(io::IO, t::AbstractTerm)
if isconstant(t)
print_coefficient(io, coefficient(t))
else
if !isone(coefficient(t))
if isone(-coefficient(t))
if should_print_coefficient(coefficient(t))
if !should_print_coefficient(-coefficient(t))
print(io, '-')
else
print_coefficient(io, coefficient(t))
Expand Down
1 change: 1 addition & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
@test sprint(show, -(1.0 + 3.1im) * z*x) == "(-1.0 - 3.1im)xz"
@test sprint(show, x^2 + (1.0 + 3.1im) * x) == "x^2 + (1.0 + 3.1im)x"
@test sprint(show, x^2 - (1.0 + 3.1im) * x) == "x^2 + (-1.0 - 3.1im)x"
@test sprint(show, [1.0, 2.0] * x) == "([1.0, 2.0])x"
end