Skip to content

Commit 7887b14

Browse files
authored
Adjust printing (#281)
* fix complex printing * add adjustments for compact and multiplication_symbol
1 parent fe7232f commit 7887b14

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

src/polynomials/standard-basis.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@ abstract type StandardBasisPolynomial{T} <: AbstractPolynomial{T} end
33

44

55
function showterm(io::IO, ::Type{<:StandardBasisPolynomial}, pj::T, var, j, first::Bool, mimetype) where {T}
6+
67
if iszero(pj) return false end
8+
79
pj = printsign(io, pj, first, mimetype)
10+
811
if !(pj == one(T) && !(showone(T) || j == 0))
912
printcoefficient(io, pj, j, mimetype)
1013
end
14+
1115
printproductsign(io, pj, j, mimetype)
1216
printexponent(io, var, j, mimetype)
1317
return true

src/show.jl

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,17 @@ end
7272
"Show different operations depending on mimetype. `l-` is leading minus sign."
7373
function showop(::MIME"text/plain", op)
7474
d = Dict("*" => "*", "+" => " + ", "-" => " - ", "l-" => "-")
75-
d[op]
75+
get(d, op, "")
7676
end
7777

7878
function showop(::MIME"text/latex", op)
7979
d = Dict("*" => "\\cdot ", "+" => " + ", "-" => " - ", "l-" => "-")
80-
d[op]
80+
get(d, op, "")
8181
end
8282

8383
function showop(::MIME"text/html", op)
8484
d = Dict("*" => "&#8729;", "+" => " &#43; ", "-" => " &#45; ", "l-" => "&#45;")
85-
d[op]
85+
get(d, op, "")
8686
end
8787

8888

@@ -97,7 +97,8 @@ types "text/plain" (default), "text/latex", and "text/html" are supported. By
9797
default, the terms are in order of ascending powers, matching the order in
9898
`coeffs(p)`; specifying `descending_powers=true` reverses the order.
9999
`offset` allows for an integer number to be added to the exponent, just for printing.
100-
`var` allows for overriding the variable used for printing.
100+
`var` allows for overriding the variable used for printing. Setting multiplication symbol to `""`
101+
will avoid an operator being printed. Setting `compact=true` will use a compact style for floating point numbers.
101102
102103
# Examples
103104
```jldoctest show
@@ -119,11 +120,17 @@ julia> printpoly(stdout, Polynomial([-1, 0, 1], :z), offset=-1, descending_power
119120
x - x^-1
120121
```
121122
"""
122-
function printpoly(io::IO, p::P, mimetype=MIME"text/plain"(); descending_powers=false, offset::Int=0, var=p.var) where {T,P<:AbstractPolynomial{T}}
123+
function printpoly(io::IO, p::P, mimetype=MIME"text/plain"();
124+
descending_powers=false, offset::Int=0, var=p.var,
125+
compact=false, mulsymbol="*") where {T,P<:AbstractPolynomial{T}}
123126
first = true
124127
printed_anything = false
125128
for i in (descending_powers ? reverse(eachindex(p)) : eachindex(p))
126-
printed = showterm(io, P, p[i], var, i+offset, first, mimetype)
129+
ioc = IOContext(io,
130+
:compact=>get(io, :compact, compact),
131+
:multiplication_symbol => get(io, :multiplication_symbol, mulsymbol)
132+
)
133+
printed = showterm(ioc, P, p[i], var, i+offset, first, mimetype)
127134
first &= !printed
128135
printed_anything |= printed
129136
end
@@ -154,9 +161,11 @@ function printsign(io::IO, pj::T, first, mimetype) where {T}
154161
end
155162

156163
## print * or cdot, ...
164+
## pass `:multiplication_symbol => "" to IOContext have no sign
157165
function printproductsign(io::IO, pj::T, j, mimetype) where {T}
158166
j == 0 && return
159-
(showone(T) || pj != one(T)) && print(io, showop(mimetype, "*"))
167+
multiplication_symbol = showop(mimetype, get(io, :multiplication_symbol,"*"))
168+
(showone(T) || pj != one(T)) && print(io, multiplication_symbol)
160169
end
161170

162171
function printproductsign(io::IO, pj::T, j, mimetype) where {T<:Complex}
@@ -245,10 +254,9 @@ function printcoefficient(io::IO, pj::S, j, mimetype) where {T,S <: Complex{T}}
245254

246255
elseif hasreal
247256

248-
(iszero(j) || showone(T) || isone(a)) && printcoefficient(io, a, j, mimetype)
257+
(iszero(j) || showone(T) || !isone(a)) && printcoefficient(io, a, j, mimetype)
249258

250259
elseif hasimag
251-
252260
(showone(T) || !isone(b)) && printcoefficient(io, b, j, mimetype)
253261
(isnan(b) || isinf(b)) && print(io, showop(mimetype, "*"))
254262
print(io, imagsymbol(mimetype))

test/StandardBasis.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,13 @@ end
832832

833833
@testset "Showing" begin
834834

835+
# customized printing with printpoly
836+
function printpoly_to_string(args...; kwargs...)
837+
buf = IOBuffer()
838+
printpoly(buf, args...; kwargs...)
839+
return String(take!(buf))
840+
end
841+
835842
p = Polynomial{Rational}([1, 4])
836843
@test sprint(show, p) == "Polynomial(1//1 + 4//1*x)"
837844

@@ -868,18 +875,22 @@ end
868875
p = P([complex(1,1),complex(0,1),complex(1,0),complex(1,1)])
869876
@test repr("text/latex", p) == "\$1 + i + i\\cdot x + x^{2} + (1 + i)x^{3}\$"
870877

871-
# customized printing with printpoly
872-
function printpoly_to_string(args...; kwargs...)
873-
buf = IOBuffer()
874-
printpoly(buf, args...; kwargs...)
875-
return String(take!(buf))
876-
end
877878
@test printpoly_to_string(P([1,2,3], "y")) == "1 + 2*y + 3*y^2"
878879
@test printpoly_to_string(P([1,2,3], "y"), descending_powers = true) == "3*y^2 + 2*y + 1"
879880
@test printpoly_to_string(P([2, 3, 1], :z), descending_powers = true, offset = -2) == "1 + 3*z^-1 + 2*z^-2"
880881
@test printpoly_to_string(P([-1, 0, 1], :z), offset = -1, descending_powers = true) == "z - z^-1"
881882
@test printpoly_to_string(P([complex(1,1),complex(1,-1)]),MIME"text/latex"()) == "1 + i + (1 - i)x"
882883
end
884+
885+
## closed issues
886+
## issue 275 with compact mult symbol
887+
p = Polynomial([1.234567890, 2.34567890])
888+
io=IOBuffer(); printpoly(io, p, compact=true); @test String(take!(io)) == "1.23457 + 2.34568*x"
889+
io=IOBuffer(); printpoly(io, p, compact=true, mulsymbol=""); @test String(take!(io)) == "1.23457 + 2.34568x"
890+
891+
## issue 278 with complex
892+
@test printpoly_to_string(Polynomial([1 + im, 1, 2, im, 2im, 1+im, 1-im])) == "1 + im + x + 2*x^2 + im*x^3 + 2im*x^4 + (1 + im)x^5 + (1 - im)x^6"
893+
883894
end
884895

885896
@testset "Plotting" begin

0 commit comments

Comments
 (0)