@@ -46,25 +46,6 @@ 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
@@ -122,7 +103,7 @@ function showterm(io::IO,p::Poly{T},j,first, mimetype) where {T}
122
103
pj == zero (T) && return false
123
104
124
105
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)
126
107
printproductsign (io, pj, j, mimetype)
127
108
printexponent (io,p. var,j, mimetype)
128
109
true
@@ -150,38 +131,69 @@ function printproductsign(io::IO, pj::T, j, mimetype) where {T}
150
131
end
151
132
152
133
# 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
153
176
function printcoefficient (io:: IO , pj:: Complex{T} , j, mimetype) where {T}
154
177
155
178
hasreal = abs (real (pj)) > 0 || isnan (real (pj)) || isinf (real (pj))
156
179
hasimag = abs (imag (pj)) > 0 || isnan (imag (pj)) || isinf (imag (pj))
157
180
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 (:* ))
162
183
elseif hasreal
163
184
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 )
165
186
elseif hasimag
166
187
b = imag (pj)
167
- (showone (T) || b != one (T)) && _show (io, mimetype, b )
188
+ (showone (T) || b != one (T)) && printcoefficient (io, b, j, mimetype )
168
189
(isnan (imag (pj)) || isinf (imag (pj))) && print (io, showop (mimetype, " *" ))
169
- _show (io, mimetype , im)
190
+ print (io, im)
170
191
else
171
192
return
172
193
end
173
194
end
174
195
175
196
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
185
197
186
198
# # show exponent
187
199
function printexponent (io,var,i, mimetype:: MIME"text/latex" )
229
241
function Base. show (io:: IO , mimetype:: MIME"text/html" , p:: Poly{T} ) where {T}
230
242
printpoly (io, p, mimetype)
231
243
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