|
76 | 76 |
|
77 | 77 | Poly(n::Number, var::SymbolLike = :x) = Poly([n], var)
|
78 | 78 |
|
| 79 | + |
79 | 80 | # create a Poly object from its roots
|
80 | 81 | """
|
81 | 82 |
|
@@ -107,8 +108,9 @@ include("show.jl") # display polynomials.
|
107 | 108 |
|
108 | 109 | convert{T}(::Type{Poly{T}}, p::Poly{T}) = p
|
109 | 110 | 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) |
112 | 114 | promote_rule{T, S}(::Type{Poly{T}}, ::Type{Poly{S}}) = Poly{promote_type(T, S)}
|
113 | 115 | promote_rule{T, S<:Number}(::Type{Poly{T}}, ::Type{S}) = Poly{promote_type(T, S)}
|
114 | 116 | eltype{T}(::Poly{T}) = T
|
@@ -298,15 +300,20 @@ function *{T,S}(p1::Poly{T}, p2::Poly{S})
|
298 | 300 | Poly(a,p1.var)
|
299 | 301 | end
|
300 | 302 |
|
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 |
303 | 305 | @compat Base.:.+{T<:Number}(c::T, p::Poly) = +(p, c)
|
304 | 306 | @compat Base.:.+{T<:Number}(p::Poly, c::T) = +(p, c)
|
305 | 307 | @compat Base.:.-{T<:Number}(p::Poly, c::T) = +(p, -c)
|
306 | 308 | @compat Base.:.-{T<:Number}(c::T, p::Poly) = +(p, -c)
|
307 | 309 | @compat Base.:.*{T<:Number,S}(c::T, p::Poly{S}) = Poly(c * p.a, p.var)
|
308 | 310 | @compat Base.:.*{T<:Number,S}(p::Poly{S}, c::T) = Poly(p.a * c, p.var)
|
309 | 311 | 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))) |
310 | 317 |
|
311 | 318 | function divrem{T, S}(num::Poly{T}, den::Poly{S})
|
312 | 319 | if num.var != den.var
|
@@ -373,6 +380,7 @@ p(4) # 57 = 1 + 2*4 + 3*4^2
|
373 | 380 | """
|
374 | 381 | function polyval{T,S}(p::Poly{T}, x::S)
|
375 | 382 | R = promote_type(T,S)
|
| 383 | + |
376 | 384 | lenp = length(p)
|
377 | 385 | if lenp == 0
|
378 | 386 | return zero(R) * x
|
@@ -407,20 +415,20 @@ polyint{T}(p::Poly{T}) = polyint(p, 0)
|
407 | 415 |
|
408 | 416 | # if we have coefficients that have `NaN` representation
|
409 | 417 | 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) |
412 | 420 | end
|
413 | 421 |
|
414 | 422 | # if we have initial condition that can represent `NaN`
|
415 | 423 | 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) |
418 | 426 | end
|
419 | 427 |
|
420 | 428 | # if we have both coefficients and initial condition that can take `NaN`
|
421 | 429 | 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) |
424 | 432 | end
|
425 | 433 |
|
426 | 434 | # otherwise, catch all
|
@@ -450,12 +458,12 @@ polyder(Poly([1, 3, -1])) # Poly(3 - 2x)
|
450 | 458 | """
|
451 | 459 | # if we have coefficients that can represent `NaN`s
|
452 | 460 | 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) |
459 | 467 | end
|
460 | 468 |
|
461 | 469 | # otherwise
|
@@ -541,7 +549,7 @@ gcd(poly([1,1,2]), poly([1,2,3])) # returns (x-1)*(x-2)
|
541 | 549 | ```
|
542 | 550 | """
|
543 | 551 | 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)) |
545 | 553 | return a
|
546 | 554 | else
|
547 | 555 | s, r = divrem(a, b)
|
|
0 commit comments