@@ -46,6 +46,26 @@ hasneg(::Type{Poly{S}}) where {S} = false
46
46
showone (:: Type{Poly{S}} ) where {S} = false
47
47
48
48
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
+
49
69
# ####
50
70
51
71
" 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}
129
149
(showone (T) || pj != one (T)) && print (io, showop (mimetype, " *" ))
130
150
end
131
151
152
+ # show a single term
132
153
function printcoefficient (io:: IO , pj:: Complex{T} , j, mimetype) where {T}
133
154
134
155
hasreal = abs (real (pj)) > 0 || isnan (real (pj)) || isinf (real (pj))
135
156
hasimag = abs (imag (pj)) > 0 || isnan (imag (pj)) || isinf (imag (pj))
136
157
137
- if hasreal & hasimag
158
+ if needsparens (pj, j)
138
159
print (io, ' (' )
139
- show (io, mimetype, pj)
160
+ _show (io, mimetype, pj)
140
161
print (io, ' )' )
141
162
elseif hasreal
142
163
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)
144
165
elseif hasimag
145
166
b = imag (pj)
146
- (showone (T) || b != one (T)) && show (io, mimetype, b)
167
+ (showone (T) || b != one (T)) && _show (io, mimetype, b)
147
168
(isnan (imag (pj)) || isinf (imag (pj))) && print (io, showop (mimetype, " *" ))
148
- show (io, mimetype, im)
169
+ _show (io, mimetype, im)
149
170
else
150
171
return
151
172
end
155
176
# # show a single term
156
177
function printcoefficient (io:: IO , pj:: T , j, mimetype) where {T}
157
178
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
159
184
end
160
185
161
186
# # show exponent
184
209
185
210
# # text/plain
186
211
Base. show (io:: IO , p:: Poly{T} ) where {T} = show (io, MIME (" text/plain" ), p)
212
+
187
213
function Base. show (io:: IO , mimetype:: MIME"text/plain" , p:: Poly{T} ) where {T}
188
214
print (io," Poly(" )
189
215
printpoly (io, p, mimetype)
@@ -198,20 +224,17 @@ function Base.show(io::IO, mimetype::MIME"text/latex", p::Poly{T}) where {T}
198
224
print (io, " \$ " )
199
225
end
200
226
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
-
209
227
210
228
# # text/html
211
229
function Base. show (io:: IO , mimetype:: MIME"text/html" , p:: Poly{T} ) where {T}
212
230
printpoly (io, p, mimetype)
213
231
end
214
232
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) }" )
217
238
end
239
+
240
+ _show (io:: IO , M, a:: Any ) = print (io,a)
0 commit comments