Skip to content

Commit c944ac7

Browse files
authored
Merge pull request #152 from jverzani/show_unquoted
rework printcoefficient
2 parents d6e2ac1 + b3fd3e9 commit c944ac7

File tree

1 file changed

+48
-45
lines changed

1 file changed

+48
-45
lines changed

src/show.jl

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -46,25 +46,6 @@ hasneg(::Type{Poly{S}}) where {S} = false
4646
showone(::Type{Poly{S}}) where {S} = false
4747

4848

49-
### show parentheses?
50-
"""
51-
needsparens(pj::T, j::Int)
52-
53-
Add parentheses to coefficient `pj * x^j of type `T` when printing.
54-
Can be overridden by external types to control printing.
55-
"""
56-
function needsparens(pj::Complex{T}, j) where {T}
57-
hasreal = abs(real(pj)) > 0 || isnan(real(pj)) || isinf(real(pj))
58-
hasimag = abs(imag(pj)) > 0 || isnan(imag(pj)) || isinf(imag(pj))
59-
hasreal && hasimag && return true
60-
false
61-
end
62-
63-
# catchall
64-
# PR #147, a good idea?
65-
# needsparens(pj, j) = occursin(" + ", string(pj)) || contains(" - ", string(pj))
66-
needsparens(pj, j) = false
67-
6849

6950
#####
7051

@@ -122,7 +103,7 @@ function showterm(io::IO,p::Poly{T},j,first, mimetype) where {T}
122103
pj == zero(T) && return false
123104

124105
pj = printsign(io, pj, j, first, mimetype)
125-
printcoefficient(io, pj, j, mimetype)
106+
!(pj == one(T) && !(showone(T) || j == 0)) && printcoefficient(io, pj, j, mimetype)
126107
printproductsign(io, pj, j, mimetype)
127108
printexponent(io,p.var,j, mimetype)
128109
true
@@ -150,38 +131,69 @@ function printproductsign(io::IO, pj::T, j, mimetype) where {T}
150131
end
151132

152133
# show a single term
134+
# Other types can overload Polynomials.printcofficient with a mimetype
135+
# or Base.show_unquoted(io, pj, indent, prec)
136+
# For example
137+
138+
"""
139+
printcoefficient(io::IO, pj, j, mimetype)
140+
141+
Print coefficient pj of monomial pj * x^j with the given mimetype.
142+
143+
For pretty printing different number types, or for adding parentheses,
144+
methods can be added to this function. If no mimetype is desired,
145+
adding a method to `Base.show_unquoted` is suggested, as this will
146+
also be useful for the default `show` methods. The following example
147+
shows how `Dual` objects of `DualNumbers` may be printed with
148+
parentheses.
149+
150+
```
151+
using DualNumbers
152+
julia> Poly([Dual(1,2), Dual(3,4)])
153+
Poly(1 + 2ɛ + 3 + 4ɛ*x)
154+
julia> function Base.show_unquoted(io::IO, pj::Dual, indent::Int, prec::Int)
155+
if Base.operator_precedence(:+) <= prec
156+
print(io, "(")
157+
show(io, pj)
158+
print(io, ")")
159+
else
160+
show(io, pj)
161+
end
162+
end
163+
164+
julia> Poly([Dual(1,2), Dual(3,4)])
165+
Poly((1 + 2ɛ) + (3 + 4ɛ)*x)
166+
```
167+
"""
168+
printcoefficient(io::IO, pj::Any, j, mimetype) = Base.show_unquoted(io, pj, 0, Base.operator_precedence(:*))
169+
170+
# pretty print rational numbers in latex
171+
function printcoefficient(io::IO, a::Rational{T}, j, mimetype::MIME"text/latex") where {T}
172+
abs(a.den) == one(T) ? print(io, a.num) : print(io, "\\frac{$(a.num)}{$(a.den)}")
173+
end
174+
175+
# print complex numbers with parentheses as needed
153176
function printcoefficient(io::IO, pj::Complex{T}, j, mimetype) where {T}
154177

155178
hasreal = abs(real(pj)) > 0 || isnan(real(pj)) || isinf(real(pj))
156179
hasimag = abs(imag(pj)) > 0 || isnan(imag(pj)) || isinf(imag(pj))
157180

158-
if needsparens(pj, j)
159-
print(io, '(')
160-
_show(io, mimetype, pj)
161-
print(io, ')')
181+
if hasreal && hasimag
182+
Base.show_unquoted(io, pj, 0, Base.operator_precedence(:*))
162183
elseif hasreal
163184
a = real(pj)
164-
(j==0 || showone(T) || a != one(T)) && _show(io, mimetype, a)
185+
(j==0 || showone(T) || a != one(T)) && printcoefficient(io, a, j, mimetype)
165186
elseif hasimag
166187
b = imag(pj)
167-
(showone(T) || b != one(T)) && _show(io, mimetype, b)
188+
(showone(T) || b != one(T)) && printcoefficient(io, b, j, mimetype)
168189
(isnan(imag(pj)) || isinf(imag(pj))) && print(io, showop(mimetype, "*"))
169-
_show(io, mimetype, im)
190+
print(io, im)
170191
else
171192
return
172193
end
173194
end
174195

175196

176-
## show a single term
177-
function printcoefficient(io::IO, pj::T, j, mimetype) where {T}
178-
pj == one(T) && !(showone(T) || j == 0) && return
179-
if needsparens(pj, j)
180-
print(io, '('); _show(io, mimetype, pj); print(io, ')')
181-
else
182-
_show(io, mimetype, pj)
183-
end
184-
end
185197

186198
## show exponent
187199
function printexponent(io,var,i, mimetype::MIME"text/latex")
@@ -229,12 +241,3 @@ end
229241
function Base.show(io::IO, mimetype::MIME"text/html", p::Poly{T}) where {T}
230242
printpoly(io, p, mimetype)
231243
end
232-
233-
234-
## intercept show to allow prettier printing of rationals
235-
236-
function _show(io::IO, mimetype::MIME"text/latex", a::Rational{T}) where {T}
237-
abs(a.den) == one(T) ? print(io, a.num) : print(io, "\\frac{$(a.num)}{$(a.den)}")
238-
end
239-
240-
_show(io::IO, M, a::Any) = print(io,a)

0 commit comments

Comments
 (0)