Skip to content

Commit 7f593c1

Browse files
committed
tweak show
2 parents 6cc4f3e + b5d46f9 commit 7f593c1

File tree

3 files changed

+70
-19
lines changed

3 files changed

+70
-19
lines changed

src/Polynomials.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ immutable Poly{T<:Number}
6969
return new(zeros(T,1), @compat Symbol(var))
7070
else
7171
# determine the last nonzero element and truncate a accordingly
72-
a_last = max(1,findlast(a))
72+
a_last = max(1,findlast(x->x!=zero(T), a))
7373
new(a[1:a_last], @compat Symbol(var))
7474
end
7575
end
@@ -115,6 +115,7 @@ convert{T, S<:Number,n}(::Type{Poly{T}}, x::Array{S,n}) = map(el->convert(Poly{p
115115
promote_rule{T, S}(::Type{Poly{T}}, ::Type{Poly{S}}) = Poly{promote_type(T, S)}
116116
promote_rule{T, S<:Number}(::Type{Poly{T}}, ::Type{S}) = Poly{promote_type(T, S)}
117117
eltype{T}(::Poly{T}) = T
118+
eltype{T}(::Type{Poly{T}}) = T
118119

119120
"""
120121
@@ -369,7 +370,7 @@ function polyval{T,S}(p::Poly{T}, x::S)
369370
if lenp == 0
370371
return zero(R) * x
371372
else
372-
y = convert(R, p[end]) + 0*x
373+
y = convert(R, p[end]) + zero(T)*x
373374
for i = (endof(p)-1):-1:0
374375
y = p[i] + x*y
375376
end

src/show.jl

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
## which can be modified by users for other Ts
66

77
"`hasneg(::T)` attribute is true if: `pj < zero(T)` is defined."
8-
hasneg{T}(::Type{T}) = true
8+
hasneg{T}(::Type{T}) = false
99

1010
"Could value possibly be negative and if so, is it?"
1111
isneg{T}(pj::T) = hasneg(T) && pj < zero(T)
@@ -20,19 +20,17 @@ showone{T}(::Type{T}) = true
2020
#####
2121

2222
## Numbers
23+
hasneg{T<:Real}(::Type{T}) = true
24+
25+
### Integer
2326
showone{T<:Integer}(::Type{T}) = false
2427
showone{T}(::Type{Rational{T}}) = false
2528

2629

27-
## Polynomials as coefficients
28-
hasneg{S}(::Type{Poly{S}}) = false
29-
showoone{S}(::Type{Poly{S}}) = false
30-
3130

3231

33-
## Complex coefficients
34-
## we say neg if real(z) < 0 || real(z) == 0 and imag(g) < 0
35-
hasneg{T}(::Type{Complex{T}}) = true
32+
### Complex coefficients
33+
hasneg{T}(::Type{Complex{T}}) = true ## we say neg if real(z) < 0 || real(z) == 0 and imag(g) < 0
3634

3735
function isneg{T}(pj::Complex{T})
3836
real(pj) < 0 && return true
@@ -43,10 +41,16 @@ end
4341
showone{T}(pj::Type{Complex{T}}) = showone(T)
4442

4543

44+
### Polynomials as coefficients
45+
hasneg{S}(::Type{Poly{S}}) = false
46+
showone{S}(::Type{Poly{S}}) = false
47+
48+
49+
#####
4650

4751
"Show different operations depending on mimetype. `l-` is leading minus sign."
48-
function showop(::MIME"text/html", op)
49-
d = Dict("*" => "&times;", "+" => " &#43; ", "-" => " &#45; ", "l-" => "&#45;")
52+
function showop(::MIME"text/plain", op)
53+
d = Dict("*" => "", "+" => " + ", "-" => " - ", "l-" => "-")
5054
d[op]
5155
end
5256

@@ -55,8 +59,8 @@ function showop(::MIME"text/latex", op)
5559
d[op]
5660
end
5761

58-
function showop(::MIME"text/plain", op)
59-
d = Dict("*" => "", "+" => " + ", "-" => " - ", "l-" => "-")
62+
function showop(::MIME"text/html", op)
63+
d = Dict("*" => "&#8729;", "+" => " &#43; ", "-" => " &#45; ", "l-" => "&#45;")
6064
d[op]
6165
end
6266

@@ -77,6 +81,7 @@ end
7781

7882
function showterm{T}(io::IO,p::Poly{T},j,first, mimetype)
7983
pj = p[j]
84+
8085
pj == zero(T) && return false
8186

8287
pj = printsign(io, pj, j, first, mimetype)
@@ -114,7 +119,7 @@ function printcoefficient{T}(io::IO, pj::Complex{T}, j, mimetype)
114119

115120
if hasreal & hasimag
116121
print(io, '(')
117-
showio(io, mimetype, pj)
122+
show(io, mimetype, pj)
118123
print(io, ')')
119124
elseif hasreal
120125
a = real(pj)
@@ -135,6 +140,7 @@ function printcoefficient{T}(io::IO, pj::T, j, mimetype)
135140
show(io, mimetype, pj)
136141
end
137142

143+
## show exponent
138144
function printexponent(io,var,i, mimetype::MIME"text/latex")
139145
if i == 0
140146
return
@@ -158,6 +164,7 @@ end
158164

159165
####
160166

167+
## text/plain
161168
@compat Base.show{T}(io::IO, p::Poly{T}) = show(io, MIME("text/plain"), p)
162169
@compat function Base.show{T}(io::IO, mimetype::MIME"text/plain", p::Poly{T})
163170
print(io,"Poly(")
@@ -166,6 +173,7 @@ end
166173

167174
end
168175

176+
## text/latex
169177
@compat function Base.show{T}(io::IO, mimetype::MIME"text/latex", p::Poly{T})
170178
print(io, "\$")
171179
printpoly(io, p, mimetype)
@@ -179,3 +187,13 @@ end
179187
@compat function Base.show{T<:Number}(io::IO, mimetype::MIME"text/latex", a::T)
180188
print(io, a)
181189
end
190+
191+
192+
## text/html
193+
@compat function Base.show{T}(io::IO, mimetype::MIME"text/html", p::Poly{T})
194+
printpoly(io, p, mimetype)
195+
end
196+
197+
@compat function Base.show{T<:Number}(io::IO, mimetype::MIME"text/html", a::T)
198+
print(io, a)
199+
end

test/runtests.jl

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ p1 = Poly([4,5,6])
184184
p1[0:1] = [7,8]
185185
@test all(p1[0:end] .== [7,8,6])
186186

187-
188187
## conjugate of poly (issue #59)
189188
as = [im, 1, 2]
190189
bs = [1, 1, 2]
@@ -198,14 +197,47 @@ p2[3] = 3
198197
@test p1[3] == 3
199198

200199

200+
## eltype of a Poly type
201+
types = [Int, UInt8, Float64]
202+
for t in types
203+
@test t == eltype(Poly{t})
204+
end
205+
206+
## Polynomials with non-Real type
207+
import Base: +, *, -
208+
immutable Mod2 <: Number
209+
v::Bool
210+
end
211+
+(x::Mod2,y::Mod2) = Mod2(x.v$y.v)
212+
*(x::Mod2,y::Mod2) = Mod2(x.v&y.v)
213+
-(x::Mod2,y::Mod2) = x+y
214+
-(x::Mod2) = x
215+
Base.one(::Type{Mod2}) = Mod2(true)
216+
Base.zero(::Type{Mod2}) = Mod2(false)
217+
Base.convert(::Type{Mod2},x::Integer) = Mod2(convert(Bool,x))
218+
Base.convert(::Type{Bool},x::Mod2) = x.v
219+
220+
# Test that none of this throws
221+
p = Poly([Mod2(true),Mod2(false), Mod2(true)])
222+
repr(p)
223+
@test p(Mod2(false)) == p[0]
224+
225+
201226
## changes to show
202227
p = Poly([1,2,3,1]) # leading coefficient of 1
203-
@test string(p) == "Poly(1 + 2⋅x + 3⋅x^2 + x^3)"
228+
@test repr(p) == "Poly(1 + 2⋅x + 3⋅x^2 + x^3)"
204229
p = Poly([1.0, 2.0, 3.0, 1.0])
205-
@test string(p) == "Poly(1.0 + 2.0⋅x + 3.0⋅x^2 + 1.0⋅x^3)"
230+
@test repr(p) == "Poly(1.0 + 2.0⋅x + 3.0⋅x^2 + 1.0⋅x^3)"
206231
p = Poly([1+im, 1-im, -1+im, -1 - im])# minus signs
207-
@test string(p) == "Poly((1 + 1im) + (1 - 1im)⋅x - (1 - 1im)⋅x^2 - (1 + 1im)⋅x^3)"
232+
@test repr(p) == "Poly((1 + 1im) + (1 - 1im)⋅x - (1 - 1im)⋅x^2 - (1 + 1im)⋅x^3)"
233+
234+
p = Poly([1,2,3])
235+
@test reprmime("text/latex", p) == "\$1 + 2\\cdot x + 3\\cdot x^{2}\$"
236+
p = Poly([1//2, 2//3, 1])
237+
@test reprmime("text/latex", p) == "\$\\frac{1}{2} + \\frac{2}{3}\\cdot x + x^{2}\$"
238+
208239

240+
## want to be able to copy and paste
209241
string_eval_poly(p,x) = eval(Expr(:function, Expr(:call, :f, :x), parse(string(p)[6:end-1])))(x)
210242
p = Poly([1,2,3]) # copy and paste
211243
q = Poly([1//1, 2//1, 3//1])

0 commit comments

Comments
 (0)