Skip to content

Commit aafe0c6

Browse files
authored
Merge pull request #93 from jverzani/constructor
Minor tweaks
2 parents 7c0cfec + 820d8a5 commit aafe0c6

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
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: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ immutable Poly{T}
7575
end
7676

7777
Poly(n::Number, var::SymbolLike = :x) = Poly([n], var)
78+
@compat (::Type{Poly{T}}){T,S}(x::Vector{S}, var::SymbolLike = :x) =
79+
Poly(convert(Vector{T}, x), var)
7880

7981
# create a Poly object from its roots
8082
"""
@@ -107,8 +109,9 @@ include("show.jl") # display polynomials.
107109

108110
convert{T}(::Type{Poly{T}}, p::Poly{T}) = p
109111
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)
112+
convert{T, S<:Number}(::Type{Poly{T}}, x::S, var::SymbolLike=:x) = Poly(promote_type(T, S)[x], var)
113+
convert{T, S<:Number}(::Type{Poly{T}}, x::Vector{S}, var::SymbolLike=:x) = (R = promote_type(T,S); Poly(convert(Vector{R},x), var))
114+
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)
112115
promote_rule{T, S}(::Type{Poly{T}}, ::Type{Poly{S}}) = Poly{promote_type(T, S)}
113116
promote_rule{T, S<:Number}(::Type{Poly{T}}, ::Type{S}) = Poly{promote_type(T, S)}
114117
eltype{T}(::Poly{T}) = T
@@ -298,15 +301,20 @@ function *{T,S}(p1::Poly{T}, p2::Poly{S})
298301
Poly(a,p1.var)
299302
end
300303

301-
## older . operators
302-
if VERSION < v"0.6.0-dev"
304+
## older . operators, hack to avoid warning on v0.6
305+
dot_operators = quote
303306
@compat Base.:.+{T<:Number}(c::T, p::Poly) = +(p, c)
304307
@compat Base.:.+{T<:Number}(p::Poly, c::T) = +(p, c)
305308
@compat Base.:.-{T<:Number}(p::Poly, c::T) = +(p, -c)
306309
@compat Base.:.-{T<:Number}(c::T, p::Poly) = +(p, -c)
307310
@compat Base.:.*{T<:Number,S}(c::T, p::Poly{S}) = Poly(c * p.a, p.var)
308311
@compat Base.:.*{T<:Number,S}(p::Poly{S}, c::T) = Poly(p.a * c, p.var)
309312
end
313+
VERSION < v"0.6.0-dev" && eval(dot_operators)
314+
315+
316+
# are any values NaN
317+
hasnan(p::Poly) = reduce(|, (@compat isnan.(p.a)))
310318

311319
function divrem{T, S}(num::Poly{T}, den::Poly{S})
312320
if num.var != den.var
@@ -373,6 +381,7 @@ p(4) # 57 = 1 + 2*4 + 3*4^2
373381
"""
374382
function polyval{T,S}(p::Poly{T}, x::S)
375383
R = promote_type(T,S)
384+
376385
lenp = length(p)
377386
if lenp == 0
378387
return zero(R) * x
@@ -407,20 +416,20 @@ polyint{T}(p::Poly{T}) = polyint(p, 0)
407416

408417
# if we have coefficients that have `NaN` representation
409418
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)
419+
hasnan(p) && return Poly(promote_type(T,S)[NaN])
420+
_polyint(p, k)
412421
end
413422

414423
# if we have initial condition that can represent `NaN`
415424
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)
425+
isnan(k) && return Poly(promote_type(T,S)[NaN])
426+
_polyint(p, k)
418427
end
419428

420429
# if we have both coefficients and initial condition that can take `NaN`
421430
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)
431+
hasnan(p) || isnan(k) && return Poly(promote_type(T,S)[NaN])
432+
_polyint(p, k)
424433
end
425434

426435
# otherwise, catch all
@@ -450,12 +459,12 @@ polyder(Poly([1, 3, -1])) # Poly(3 - 2x)
450459
"""
451460
# if we have coefficients that can represent `NaN`s
452461
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)
462+
n = length(p)
463+
order < 0 && error("Order of derivative must be non-negative")
464+
order == 0 && return p
465+
hasnan(p) && return Poly(T[NaN], p.var)
466+
n <= order && return Poly(T[], p.var)
467+
_polyder(p, order)
459468
end
460469

461470
# otherwise
@@ -520,7 +529,7 @@ function roots{T}(p::Poly{T})
520529
companion = diagm(ones(R, n-1), -1)
521530
an = p[end-num_trailing_zeros]
522531
companion[1,:] = -p[(end-num_trailing_zeros-1):-1:num_leading_zeros] / an
523-
532+
524533
D = eigvals(companion)
525534
r = zeros(eltype(D),length(p)-num_trailing_zeros-1)
526535
r[1:n] = D
@@ -541,7 +550,7 @@ gcd(poly([1,1,2]), poly([1,2,3])) # returns (x-1)*(x-2)
541550
```
542551
"""
543552
function gcd{T, S}(a::Poly{T}, b::Poly{S})
544-
if all(map(abs,b.a).<=2*eps(S))
553+
if reduce(&, (@compat abs.(b.a)) .<=2*eps(S))
545554
return a
546555
else
547556
s, r = divrem(a, b)

src/show.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ end
9494

9595

9696
## print the sign
97-
## returns aspos(pj)
97+
## returns aspos(pj)
9898
function printsign{T}(io::IO, pj::T, j, first, mimetype)
9999
neg = isneg(pj)
100100
if first
@@ -111,12 +111,12 @@ function printproductsign{T}(io::IO, pj::T, j, mimetype)
111111
j == 0 && return
112112
(showone(T) || pj != one(T)) && print(io, showop(mimetype, "*"))
113113
end
114-
114+
115115
function printcoefficient{T}(io::IO, pj::Complex{T}, j, mimetype)
116116

117-
hasreal = abs(real(pj)) > 0
118-
hasimag = abs(imag(pj)) > 0
119-
117+
hasreal = abs(real(pj)) > 0 || isnan(real(pj)) || isinf(real(pj))
118+
hasimag = abs(imag(pj)) > 0 || isnan(imag(pj)) || isinf(imag(pj))
119+
120120
if hasreal & hasimag
121121
print(io, '(')
122122
show(io, mimetype, pj)
@@ -127,14 +127,15 @@ 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
133134
end
134135
end
135136

136-
137-
## show a single term
137+
138+
## show a single term
138139
function printcoefficient{T}(io::IO, pj::T, j, mimetype)
139140
pj == one(T) && !(showone(T) || j == 0) && return
140141
show(io, mimetype, pj)

test/runtests.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ p5 = Poly([1,4,6,4,1,0,0,0,0,0,0,0,0,0,0,0,0,0])
1313
pN = Poly([276,3,87,15,24,0])
1414
pR = Poly([3//4, -2//1, 1//1])
1515
X = Poly([0.0, 1.0])
16+
T = Int64
17+
Poly{T}([zero(T), one(T)])
18+
Poly{T}([zero(T), one(T)], :y)
19+
1620
p1000 = Poly(randn(1000))
1721

1822
@test length(pNULL) == 1
@@ -231,6 +235,8 @@ p = Poly([1, im])
231235
@test repr(p) == "Poly(1 + im⋅x)"
232236
p = Poly([1+im, 1-im, -1+im, -1 - im])# minus signs
233237
@test repr(p) == "Poly((1 + 1im) + (1 - 1im)⋅x - (1 - 1im)⋅x^2 - (1 + 1im)⋅x^3)"
238+
p = Poly([1.0, 0 + NaN*im, NaN, Inf, 0 - Inf*im]) # handle NaN or Inf appropriately
239+
@test repr(p) == "Poly(1.0 + NaN⋅im⋅x + NaN⋅x^2 + Inf⋅x^3 - Inf⋅im⋅x^4)"
234240

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

0 commit comments

Comments
 (0)