Skip to content

Commit cbf5b77

Browse files
committed
v0.6 fixes; fix issue with show and NaN,Inf over C[x]
Address issues with Poly{T} constructor
1 parent 7c0cfec commit cbf5b77

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
lines changed

REQUIRE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
julia 0.4
2-
Compat 0.7.15
2+
Compat 0.9.4

src/Polynomials.jl

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ end
7676

7777
Poly(n::Number, var::SymbolLike = :x) = Poly([n], var)
7878

79+
7980
# create a Poly object from its roots
8081
"""
8182
@@ -107,8 +108,9 @@ include("show.jl") # display polynomials.
107108

108109
convert{T}(::Type{Poly{T}}, p::Poly{T}) = p
109110
convert{T}(::Type{Poly{T}}, p::Poly) = Poly(convert(Vector{T}, p.a), p.var)
110-
convert{T, S<:Number}(::Type{Poly{T}}, x::S) = Poly(promote_type(T, S)[x])
111-
convert{T, S<:Number,n}(::Type{Poly{T}}, x::Array{S,n}) = map(el->convert(Poly{promote_type(T,S)},el),x)
111+
convert{T, S<:Number}(::Type{Poly{T}}, x::S, var::SymbolLike=:x) = Poly(promote_type(T, S)[x], var)
112+
convert{T, S<:Number}(::Type{Poly{T}}, x::Vector{S}, var::SymbolLike=:x) = (R = promote_type(T,S); Poly(convert(Vector{R},x), var))
113+
convert{T, S<:Number,n}(::Type{Poly{T}}, x::Array{S,n}, var::SymbolLike=:x) = map(el->convert(Poly{promote_type(T,S)},el,var),x)
112114
promote_rule{T, S}(::Type{Poly{T}}, ::Type{Poly{S}}) = Poly{promote_type(T, S)}
113115
promote_rule{T, S<:Number}(::Type{Poly{T}}, ::Type{S}) = Poly{promote_type(T, S)}
114116
eltype{T}(::Poly{T}) = T
@@ -298,15 +300,20 @@ function *{T,S}(p1::Poly{T}, p2::Poly{S})
298300
Poly(a,p1.var)
299301
end
300302

301-
## older . operators
302-
if VERSION < v"0.6.0-dev"
303+
## older . operators, hack to avoid warning on v0.6
304+
dot_operators = quote
303305
@compat Base.:.+{T<:Number}(c::T, p::Poly) = +(p, c)
304306
@compat Base.:.+{T<:Number}(p::Poly, c::T) = +(p, c)
305307
@compat Base.:.-{T<:Number}(p::Poly, c::T) = +(p, -c)
306308
@compat Base.:.-{T<:Number}(c::T, p::Poly) = +(p, -c)
307309
@compat Base.:.*{T<:Number,S}(c::T, p::Poly{S}) = Poly(c * p.a, p.var)
308310
@compat Base.:.*{T<:Number,S}(p::Poly{S}, c::T) = Poly(p.a * c, p.var)
309311
end
312+
VERSION < v"0.6.0-dev" && eval(dot_operators)
313+
314+
315+
# are any values NaN
316+
hasnan(p::Poly) = reduce(|, (@compat isnan.(p.a)))
310317

311318
function divrem{T, S}(num::Poly{T}, den::Poly{S})
312319
if num.var != den.var
@@ -373,6 +380,7 @@ p(4) # 57 = 1 + 2*4 + 3*4^2
373380
"""
374381
function polyval{T,S}(p::Poly{T}, x::S)
375382
R = promote_type(T,S)
383+
376384
lenp = length(p)
377385
if lenp == 0
378386
return zero(R) * x
@@ -407,20 +415,20 @@ polyint{T}(p::Poly{T}) = polyint(p, 0)
407415

408416
# if we have coefficients that have `NaN` representation
409417
function polyint{T<:Union{Real,Complex},S<:Number}(p::Poly{T}, k::S)
410-
any(map(isnan,p.a)) && return Poly(promote_type(T,S)[NaN])
411-
_polyint(p, k)
418+
hasnan(p) && return Poly(promote_type(T,S)[NaN])
419+
_polyint(p, k)
412420
end
413421

414422
# if we have initial condition that can represent `NaN`
415423
function polyint{T,S<:Union{Real,Complex}}(p::Poly{T}, k::S)
416-
isnan(k) && return Poly(promote_type(T,S)[NaN])
417-
_polyint(p, k)
424+
isnan(k) && return Poly(promote_type(T,S)[NaN])
425+
_polyint(p, k)
418426
end
419427

420428
# if we have both coefficients and initial condition that can take `NaN`
421429
function polyint{T<:Union{Real,Complex},S<:Union{Real,Complex}}(p::Poly{T}, k::S)
422-
(any(map(isnan,p.a)) || isnan(k)) && return Poly(promote_type(T,S)[NaN])
423-
_polyint(p, k)
430+
hasnan(p) || isnan(k) && return Poly(promote_type(T,S)[NaN])
431+
_polyint(p, k)
424432
end
425433

426434
# otherwise, catch all
@@ -450,12 +458,12 @@ polyder(Poly([1, 3, -1])) # Poly(3 - 2x)
450458
"""
451459
# if we have coefficients that can represent `NaN`s
452460
function polyder{T<:Union{Real,Complex}}(p::Poly{T}, order::Int=1)
453-
n = length(p)
454-
order < 0 && error("Order of derivative must be non-negative")
455-
order == 0 && return p
456-
any(map(isnan,p.a)) && return Poly(T[NaN], p.var)
457-
n <= order && return Poly(T[], p.var)
458-
_polyder(p, order)
461+
n = length(p)
462+
order < 0 && error("Order of derivative must be non-negative")
463+
order == 0 && return p
464+
hasnan(p) && return Poly(T[NaN], p.var)
465+
n <= order && return Poly(T[], p.var)
466+
_polyder(p, order)
459467
end
460468

461469
# otherwise
@@ -541,7 +549,7 @@ gcd(poly([1,1,2]), poly([1,2,3])) # returns (x-1)*(x-2)
541549
```
542550
"""
543551
function gcd{T, S}(a::Poly{T}, b::Poly{S})
544-
if all(map(abs,b.a).<=2*eps(S))
552+
if reduce(&, (@compat abs.(b.a)) .<=2*eps(S))
545553
return a
546554
else
547555
s, r = divrem(a, b)

src/show.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ end
114114

115115
function printcoefficient{T}(io::IO, pj::Complex{T}, j, mimetype)
116116

117-
hasreal = abs(real(pj)) > 0
118-
hasimag = abs(imag(pj)) > 0
117+
hasreal = abs(real(pj)) > 0 || isnan(real(pj)) || isinf(real(pj))
118+
hasimag = abs(imag(pj)) > 0 || isnan(imag(pj)) || isinf(imag(pj))
119119

120120
if hasreal & hasimag
121121
print(io, '(')
@@ -127,6 +127,7 @@ function printcoefficient{T}(io::IO, pj::Complex{T}, j, mimetype)
127127
elseif hasimag
128128
b = imag(pj)
129129
(showone(T) || b != one(T)) && show(io, mimetype, b)
130+
(isnan(imag(pj)) || isinf(imag(pj))) && print(io, showop(mimetype, "*"))
130131
show(io, mimetype, im)
131132
else
132133
return

test/runtests.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ p = Poly([1, im])
231231
@test repr(p) == "Poly(1 + im⋅x)"
232232
p = Poly([1+im, 1-im, -1+im, -1 - im])# minus signs
233233
@test repr(p) == "Poly((1 + 1im) + (1 - 1im)⋅x - (1 - 1im)⋅x^2 - (1 + 1im)⋅x^3)"
234+
p = Poly([1.0, 0 + NaN*im, NaN, Inf, 0 - Inf*im]) # handle NaN or Inf appropriately
235+
@test repr(p) == "Poly(1.0 + NaN⋅im⋅x + NaN⋅x^2 + Inf⋅x^3 - Inf⋅im⋅x^4)"
234236

235237
p = Poly([1,2,3])
236238
@test reprmime("text/latex", p) == "\$1 + 2\\cdot x + 3\\cdot x^{2}\$"

0 commit comments

Comments
 (0)