Skip to content

Commit 347fbaa

Browse files
committed
handle printing of non-number coefficients
1 parent 14f3f9a commit 347fbaa

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/show.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ if VERSION < v"0.7.0-DEV.1144" # Define `isone` for base types JuliaLang/julia#2
66
isone(x::T) where T = x == one(T)
77
end
88

9+
910
function show(io::IO, m::AbstractMonomial)
1011
if isconstant(m)
1112
print(io, '1')
@@ -21,15 +22,17 @@ function show(io::IO, m::AbstractMonomial)
2122
end
2223
end
2324

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

2730
function Base.show(io::IO, t::AbstractTerm)
2831
if isconstant(t)
2932
print_coefficient(io, coefficient(t))
3033
else
31-
if !isone(coefficient(t))
32-
if isone(-coefficient(t))
34+
if should_print_coefficient(coefficient(t))
35+
if !should_print_coefficient(-coefficient(t))
3336
print(io, '-')
3437
else
3538
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)