From 347fbaa34fb7d29b5d8232fd2753dba006d378de Mon Sep 17 00:00:00 2001 From: Robin Deits Date: Fri, 2 Mar 2018 13:41:18 -0500 Subject: [PATCH 1/3] handle printing of non-number coefficients --- src/show.jl | 7 +++++-- test/show.jl | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/show.jl b/src/show.jl index b722689b..af5711b6 100644 --- a/src/show.jl +++ b/src/show.jl @@ -6,6 +6,7 @@ if VERSION < v"0.7.0-DEV.1144" # Define `isone` for base types JuliaLang/julia#2 isone(x::T) where T = x == one(T) end + function show(io::IO, m::AbstractMonomial) if isconstant(m) print(io, '1') @@ -21,6 +22,8 @@ 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, ")") @@ -28,8 +31,8 @@ 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)) diff --git a/test/show.jl b/test/show.jl index 163ebdb4..bcba2084 100644 --- a/test/show.jl +++ b/test/show.jl @@ -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 From 1f61dd8717a07f2078c9900d238e411da390ee0c Mon Sep 17 00:00:00 2001 From: Robin Deits Date: Fri, 2 Mar 2018 13:42:13 -0500 Subject: [PATCH 2/3] cleanup --- src/show.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/show.jl b/src/show.jl index af5711b6..e23e2194 100644 --- a/src/show.jl +++ b/src/show.jl @@ -6,7 +6,6 @@ if VERSION < v"0.7.0-DEV.1144" # Define `isone` for base types JuliaLang/julia#2 isone(x::T) where T = x == one(T) end - function show(io::IO, m::AbstractMonomial) if isconstant(m) print(io, '1') From 9a7a9ca2dfda1d6d7d8b2831df5418e0c717d20c Mon Sep 17 00:00:00 2001 From: Robin Deits Date: Sat, 3 Mar 2018 19:21:29 -0500 Subject: [PATCH 3/3] fix test --- test/show.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/show.jl b/test/show.jl index bcba2084..e6f092e3 100644 --- a/test/show.jl +++ b/test/show.jl @@ -24,5 +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") + @test sprint(show, [1.0, 2.0] * x) == "([1.0, 2.0])x" end