Skip to content

Commit 82cbdcd

Browse files
authored
Merge pull request #68 from rdeits/show-non-numeric
handle printing of non-number coefficients
2 parents 14f3f9a + 9a7a9ca commit 82cbdcd

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/show.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ function show(io::IO, m::AbstractMonomial)
2121
end
2222
end
2323

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

2729
function Base.show(io::IO, t::AbstractTerm)
2830
if isconstant(t)
2931
print_coefficient(io, coefficient(t))
3032
else
31-
if !isone(coefficient(t))
32-
if isone(-coefficient(t))
33+
if should_print_coefficient(coefficient(t))
34+
if !should_print_coefficient(-coefficient(t))
3335
print(io, '-')
3436
else
3537
print_coefficient(io, coefficient(t))

test/show.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
@test sprint(show, -(1.0 + 3.1im) * z*x) == "(-1.0 - 3.1im)xz"
2525
@test sprint(show, x^2 + (1.0 + 3.1im) * x) == "x^2 + (1.0 + 3.1im)x"
2626
@test sprint(show, x^2 - (1.0 + 3.1im) * x) == "x^2 + (-1.0 - 3.1im)x"
27+
@test sprint(show, [1.0, 2.0] * x) == "([1.0, 2.0])x"
2728
end

0 commit comments

Comments
 (0)