Skip to content

Commit d6e2ac1

Browse files
authored
Merge pull request #151 from jverzani/dehoist
address type piracy (close issue #150); add idea from PR #147
2 parents a94fed9 + 33f1876 commit d6e2ac1

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

src/show.jl

+39-16
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ 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+
68+
4969
#####
5070

5171
"Show different operations depending on mimetype. `l-` is leading minus sign."
@@ -129,23 +149,24 @@ function printproductsign(io::IO, pj::T, j, mimetype) where {T}
129149
(showone(T) || pj != one(T)) && print(io, showop(mimetype, "*"))
130150
end
131151

152+
# show a single term
132153
function printcoefficient(io::IO, pj::Complex{T}, j, mimetype) where {T}
133154

134155
hasreal = abs(real(pj)) > 0 || isnan(real(pj)) || isinf(real(pj))
135156
hasimag = abs(imag(pj)) > 0 || isnan(imag(pj)) || isinf(imag(pj))
136157

137-
if hasreal & hasimag
158+
if needsparens(pj, j)
138159
print(io, '(')
139-
show(io, mimetype, pj)
160+
_show(io, mimetype, pj)
140161
print(io, ')')
141162
elseif hasreal
142163
a = real(pj)
143-
(j==0 || showone(T) || a != one(T)) && show(io, mimetype, a)
164+
(j==0 || showone(T) || a != one(T)) && _show(io, mimetype, a)
144165
elseif hasimag
145166
b = imag(pj)
146-
(showone(T) || b != one(T)) && show(io, mimetype, b)
167+
(showone(T) || b != one(T)) && _show(io, mimetype, b)
147168
(isnan(imag(pj)) || isinf(imag(pj))) && print(io, showop(mimetype, "*"))
148-
show(io, mimetype, im)
169+
_show(io, mimetype, im)
149170
else
150171
return
151172
end
@@ -155,7 +176,11 @@ end
155176
## show a single term
156177
function printcoefficient(io::IO, pj::T, j, mimetype) where {T}
157178
pj == one(T) && !(showone(T) || j == 0) && return
158-
show(io, mimetype, pj)
179+
if needsparens(pj, j)
180+
print(io, '('); _show(io, mimetype, pj); print(io, ')')
181+
else
182+
_show(io, mimetype, pj)
183+
end
159184
end
160185

161186
## show exponent
@@ -184,6 +209,7 @@ end
184209

185210
## text/plain
186211
Base.show(io::IO, p::Poly{T}) where {T} = show(io, MIME("text/plain"), p)
212+
187213
function Base.show(io::IO, mimetype::MIME"text/plain", p::Poly{T}) where {T}
188214
print(io,"Poly(")
189215
printpoly(io, p, mimetype)
@@ -198,20 +224,17 @@ function Base.show(io::IO, mimetype::MIME"text/latex", p::Poly{T}) where {T}
198224
print(io, "\$")
199225
end
200226

201-
function Base.show(io::IO, mimetype::MIME"text/latex", a::Rational{T}) where {T}
202-
abs(a.den) == one(T) ? print(io, a.num) : print(io, "\\frac{$(a.num)}{$(a.den)}")
203-
end
204-
205-
function Base.show(io::IO, mimetype::MIME"text/latex", a::T) where {T<:Number}
206-
print(io, a)
207-
end
208-
209227

210228
## text/html
211229
function Base.show(io::IO, mimetype::MIME"text/html", p::Poly{T}) where {T}
212230
printpoly(io, p, mimetype)
213231
end
214232

215-
function Base.show(io::IO, mimetype::MIME"text/html", a::T) where {T<:Number}
216-
print(io, a)
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)}")
217238
end
239+
240+
_show(io::IO, M, a::Any) = print(io,a)

0 commit comments

Comments
 (0)