Skip to content

Commit 7575879

Browse files
authored
Merge pull request #222 from jverzani/v1.1.0
V1.1.0: change eltype(::Type{Polynomial}) (breaks non documented feature) improve ImmutablePolynomial (breaks non documented order of type parameters) clean up
2 parents 32af155 + ba751dd commit 7575879

File tree

12 files changed

+355
-226
lines changed

12 files changed

+355
-226
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name = "Polynomials"
33
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
44
license = "MIT"
55
author = "JuliaMath"
6-
version = "1.0.6"
6+
version = "1.1.0"
77

88
[deps]
99
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"

src/abstract.jl

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ An abstract container for various polynomials.
1313
"""
1414
abstract type AbstractPolynomial{T} end
1515

16+
# We want ⟒(P{α…,T}) = P{α…}; this default
17+
# works for most cases
18+
(P::Type{<:AbstractPolynomial}) = constructorof(P)
1619

1720
"""
1821
Polynomials.@register(name)
@@ -36,17 +39,18 @@ macro register(name)
3639
quote
3740
Base.convert(::Type{P}, p::P) where {P<:$poly} = p
3841
Base.convert(P::Type{<:$poly}, p::$poly{T}) where {T} = P(coeffs(p), p.var)
39-
Base.promote_rule(::Type{$poly{T}}, ::Type{$poly{S}}) where {T,S} =
42+
Base.promote(p::P, q::Q) where {T, P <:$poly{T}, Q <: $poly{T}} = p,q
43+
Base.promote_rule(::Type{<:$poly{T}}, ::Type{<:$poly{S}}) where {T,S} =
4044
$poly{promote_type(T, S)}
41-
Base.promote_rule(::Type{$poly{T}}, ::Type{S}) where {T,S<:Number} =
45+
Base.promote_rule(::Type{<:$poly{T}}, ::Type{S}) where {T,S<:Number} =
4246
$poly{promote_type(T, S)}
4347
$poly(coeffs::AbstractVector{T}, var::SymbolLike = :x) where {T} =
4448
$poly{T}(coeffs, Symbol(var))
45-
$poly{T}(x::AbstractVector{S}, var = :x) where {T,S<:Number} =
46-
$poly(T.(x), var)
47-
$poly{T}(n::S, var = :x) where {T, S<:Number} =
48-
$poly(T[n], var)
49-
$poly(n::Number, var = :x) = $poly([n], var)
49+
$poly{T}(x::AbstractVector{S}, var::SymbolLike = :x) where {T,S<:Number} =
50+
$poly(T.(x), Symbol(var))
51+
$poly{T}(n::S, var::SymbolLike = :x) where {T, S<:Number} =
52+
$poly(T[n], Symbol(var))
53+
$poly(n::Number, var::SymbolLike = :x) = $poly([n], Symbol(var))
5054
$poly{T}(var::SymbolLike=:x) where {T} = variable($poly{T}, Symbol(var))
5155
$poly(var::SymbolLike=:x) = variable($poly, Symbol(var))
5256
end
@@ -58,19 +62,20 @@ macro register1(name)
5862
poly = esc(name)
5963
quote
6064
Base.convert(::Type{P}, p::P) where {P<:$poly} = p
61-
Base.promote_rule(::Type{$poly{α,T}}, ::Type{$poly{α,S}}) where {α,T,S} =
65+
Base.promote(p::P, q::Q) where {α,T, P <:$poly{α,T}, Q <: $poly{α,T}} = p,q
66+
Base.promote_rule(::Type{<:$poly{α,T}}, ::Type{<:$poly{α,S}}) where {α,T,S} =
6267
$poly{α,promote_type(T, S)}
63-
Base.promote_rule(::Type{$poly{α,T}}, ::Type{S}) where {α,T,S<:Number} =
68+
Base.promote_rule(::Type{<:$poly{α,T}}, ::Type{S}) where {α,T,S<:Number} =
6469
$poly{α,promote_type(T,S)}
65-
function $poly{α,T}(x::AbstractVector{S}, var::Polynomials.SymbolLike = :x) where {α,T,S}
70+
function $poly{α,T}(x::AbstractVector{S}, var::SymbolLike = :x) where {α,T,S}
6671
$poly{α,T}(T.(x), Symbol(var))
6772
end
68-
$poly{α}(coeffs::AbstractVector{T}, var::Polynomials.SymbolLike=:x) where {α,T} =
73+
$poly{α}(coeffs::AbstractVector{T}, var::SymbolLike=:x) where {α,T} =
6974
$poly{α,T}(coeffs, Symbol(var))
70-
$poly{α,T}(n::Number, var::Polynomials.SymbolLike = :x) where {α,T} = n*one($poly{α,T}, Symbol(var))
71-
$poly{α}(n::Number, var::Polynomials.SymbolLike = :x) where {α} = n*one($poly{α}, Symbol(var))
72-
$poly{α,T}(var::Polynomials.SymbolLike=:x) where {α, T} = variable($poly{α,T}, Symbol(var))
73-
$poly{α}(var::Polynomials.SymbolLike=:x) where {α} = variable($poly{α}, Symbol(var))
75+
$poly{α,T}(n::Number, var::SymbolLike = :x) where {α,T} = n*one($poly{α,T}, Symbol(var))
76+
$poly{α}(n::Number, var::SymbolLike = :x) where {α} = n*one($poly{α}, Symbol(var))
77+
$poly{α,T}(var::SymbolLike=:x) where {α, T} = variable($poly{α,T}, Symbol(var))
78+
$poly{α}(var::SymbolLike=:x) where {α} = variable($poly{α}, Symbol(var))
7479
end
7580
end
7681

@@ -80,17 +85,18 @@ macro register2(name)
8085
poly = esc(name)
8186
quote
8287
Base.convert(::Type{P}, p::P) where {P<:$poly} = p
83-
Base.promote_rule(::Type{$poly{α,β,T}}, ::Type{$poly{α,β,S}}) where {α,β,T,S} =
88+
Base.promote(p::P, q::Q) where {α,β,T, P <:$poly{α,β,T}, Q <: $poly{α,β,T}} = p,q
89+
Base.promote_rule(::Type{<:$poly{α,β,T}}, ::Type{<:$poly{α,β,S}}) where {α,β,T,S} =
8490
$poly{α,β,promote_type(T, S)}
85-
Base.promote_rule(::Type{$poly{α,β,T}}, ::Type{S}) where {α,β,T,S<:Number} =
91+
Base.promote_rule(::Type{<:$poly{α,β,T}}, ::Type{S}) where {α,β,T,S<:Number} =
8692
$poly{α,β,promote_type(T, S)}
87-
$poly{α,β}(coeffs::AbstractVector{T}, var::Polynomials.SymbolLike = :x) where {α,β,T} =
93+
$poly{α,β}(coeffs::AbstractVector{T}, var::SymbolLike = :x) where {α,β,T} =
8894
$poly{α,β,T}(coeffs, Symbol(var))
89-
$poly{α,β,T}(x::AbstractVector{S}, var = :x) where {α,β,T,S<:Number} = $poly{α,β,T}(T.(x), var)
90-
$poly{α,β,T}(n::Number, var = :x) where {α,β,T} = n*one($poly{α,β,T}, var)
91-
$poly{α,β}(n::Number, var = :x) where {α,β} = n*one($poly{α,β}, var)
92-
$poly{α,β,T}(var=:x) where {α,β, T} = variable($poly{α,β,T}, var)
93-
$poly{α,β}(var=:x) where {α,β} = variable($poly{α,β}, var)
95+
$poly{α,β,T}(x::AbstractVector{S}, var::SymbolLike = :x) where {α,β,T,S<:Number} = $poly{α,β,T}(T.(x), var)
96+
$poly{α,β,T}(n::Number, var::SymbolLike = :x) where {α,β,T} = n*one($poly{α,β,T}, Symbol(var))
97+
$poly{α,β}(n::Number, va::SymbolLiker = :x) where {α,β} = n*one($poly{α,β}, Symbol(var))
98+
$poly{α,β,T}(var::SymbolLike=:x) where {α,β, T} = variable($poly{α,β,T}, Symbol(var))
99+
$poly{α,β}(var::SymbolLike=:x) where {α,β} = variable($poly{α,β}, Symbol(var))
94100
end
95101
end
96102

src/common.jl

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ In-place version of [`truncate`](@ref)
172172
"""
173173
function truncate!(p::AbstractPolynomial{T};
174174
rtol::Real = Base.rtoldefault(real(T)),
175-
atol::Real = 0,) where {T}
175+
atol::Real = 0,) where {T}
176176
max_coeff = maximum(abs, coeffs(p))
177177
thresh = max_coeff * rtol + atol
178178
map!(c->abs(c) <= thresh ? zero(T) : c, coeffs(p), coeffs(p))
@@ -225,33 +225,7 @@ function Base.chop(p::AbstractPolynomial{T};
225225
end
226226

227227

228-
"""
229-
variable(var=:x)
230-
variable(::Type{<:AbstractPolynomial}, var=:x)
231-
variable(p::AbstractPolynomial, var=p.var)
232-
233-
Return the monomial `x` in the indicated polynomial basis. If no type is give, will default to [`Polynomial`](@ref). Equivalent to `P(var)`.
234-
235-
# Examples
236-
```jldoctest common
237-
julia> using Polynomials
238-
239-
julia> x = variable()
240-
Polynomial(x)
241-
242-
julia> p = 100 + 24x - 3x^2
243-
Polynomial(100 + 24*x - 3*x^2)
244-
245-
julia> roots((x - 3) * (x + 2))
246-
2-element Array{Float64,1}:
247-
-2.0
248-
3.0
249228

250-
```
251-
"""
252-
variable(::Type{P}, var::SymbolLike = :x) where {P <: AbstractPolynomial} = P([0, 1], var)
253-
variable(p::AbstractPolynomial, var::SymbolLike = p.var) = variable(typeof(p), var)
254-
variable(var::SymbolLike = :x) = variable(Polynomial{Int})
255229

256230
"""
257231
check_same_variable(p::AbstractPolynomial, q::AbstractPolynomial)
@@ -303,7 +277,10 @@ Returns the size of the polynomials coefficients, along axis `i` if provided.
303277
Base.size(p::AbstractPolynomial) = size(coeffs(p))
304278
Base.size(p::AbstractPolynomial, i::Integer) = size(coeffs(p), i)
305279
Base.eltype(p::AbstractPolynomial{T}) where {T} = T
306-
Base.eltype(::Type{P}) where {P <: AbstractPolynomial} = P
280+
# in analogy with polynomial as a Vector{T} with different operations defined.
281+
Base.eltype(::Type{<:AbstractPolynomial}) = Float64
282+
Base.eltype(::Type{<:AbstractPolynomial{T}}) where {T} = T
283+
#Base.eltype(::Type{P}) where {P <: AbstractPolynomial} = P # changed in v1.1.0
307284
function Base.iszero(p::AbstractPolynomial)
308285
if length(p) == 0
309286
return true
@@ -381,18 +358,6 @@ Base.lastindex(p::AbstractPolynomial) = length(p) - 1
381358
Base.eachindex(p::AbstractPolynomial) = 0:length(p) - 1
382359
Base.broadcastable(p::AbstractPolynomial) = Ref(p)
383360

384-
# basis
385-
# return the kth basis polynomial for the given polynomial type, e.g. x^k for Polynomial{T}
386-
function basis(p::P, k::Int; var=:x) where {P<:AbstractPolynomial}
387-
basis(P, k, var=var)
388-
end
389-
390-
function basis(::Type{P}, k::Int; var=:x) where {P <: AbstractPolynomial}
391-
zs = zeros(Int, k+1)
392-
zs[end] = 1
393-
P(zs, var)
394-
end
395-
396361
# iteration
397362
# iteration occurs over the basis polynomials
398363
Base.iterate(p::AbstractPolynomial) = (p[0] * one(typeof(p)), 1)
@@ -439,26 +404,70 @@ Base.setindex!(p::AbstractPolynomial, values, ::Colon) =
439404
identity =#
440405
Base.copy(p::P) where {P <: AbstractPolynomial} = P(copy(coeffs(p)), p.var)
441406
Base.hash(p::AbstractPolynomial, h::UInt) = hash(p.var, hash(coeffs(p), h))
407+
408+
#=
409+
zero, one, variable, basis =#
442410
"""
443411
zero(::Type{<:AbstractPolynomial})
444412
zero(::AbstractPolynomial)
445413
446414
Returns a representation of 0 as the given polynomial.
447415
"""
448-
Base.zero(::Type{P}, var=:x) where {T, P <: AbstractPolynomial{T}} = P(zeros(T, 1), var)
449-
Base.zero(::Type{P}, var=:x) where {P <: AbstractPolynomial} = P(zeros(1), var)
416+
Base.zero(::Type{P}, var=:x) where {P <: AbstractPolynomial} = (P)(zeros(eltype(P), 1), var)
450417
Base.zero(p::P) where {P <: AbstractPolynomial} = zero(P, p.var)
451418
"""
452419
one(::Type{<:AbstractPolynomial})
453420
one(::AbstractPolynomial)
454421
455422
Returns a representation of 1 as the given polynomial.
456423
"""
457-
Base.one(::Type{P}, var=:x) where {T, P <: AbstractPolynomial{T}} = P(ones(T, 1), var)
458-
Base.one(::Type{P}, var=:x) where {P <: AbstractPolynomial} = P(ones(1), var)
424+
Base.one(::Type{P}, var=:x) where {P <: AbstractPolynomial} = (P)(ones(eltype(P),1), var)
459425
Base.one(p::P) where {P <: AbstractPolynomial} = one(P, p.var)
460-
Base.oneunit(p::P, args...) where {P <: AbstractPolynomial} = one(p, args...)
426+
461427
Base.oneunit(::Type{P}, args...) where {P <: AbstractPolynomial} = one(P, args...)
428+
Base.oneunit(p::P, args...) where {P <: AbstractPolynomial} = one(p, args...)
429+
430+
431+
"""
432+
variable(var=:x)
433+
variable(::Type{<:AbstractPolynomial}, var=:x)
434+
variable(p::AbstractPolynomial, var=p.var)
435+
436+
Return the monomial `x` in the indicated polynomial basis. If no type is give, will default to [`Polynomial`](@ref). Equivalent to `P(var)`.
437+
438+
# Examples
439+
```jldoctest common
440+
julia> using Polynomials
441+
442+
julia> x = variable()
443+
Polynomial(x)
444+
445+
julia> p = 100 + 24x - 3x^2
446+
Polynomial(100 + 24*x - 3*x^2)
447+
448+
julia> roots((x - 3) * (x + 2))
449+
2-element Array{Float64,1}:
450+
-2.0
451+
3.0
452+
453+
```
454+
"""
455+
variable(::Type{P}, var::SymbolLike = :x) where {P <: AbstractPolynomial} = MethodError()
456+
variable(p::AbstractPolynomial, var::SymbolLike = p.var) = variable(typeof(p), var)
457+
variable(var::SymbolLike = :x) = variable(Polynomial{Int}, var)
458+
459+
# basis
460+
# var is a positional argument, not a keyword; can't deprecate so we do `_var; var=_var`
461+
#@deprecate basis(p::P, k::Int; var=:x) where {P<:AbstractPolynomial} basis(p, k, var)
462+
#@deprecate basis(::Type{P}, k::Int; var=:x) where {P <: AbstractPolynomial} basis(P, k,var)
463+
# return the kth basis polynomial for the given polynomial type, e.g. x^k for Polynomial{T}
464+
function basis(::Type{P}, k::Int, _var::SymbolLike=:x; var=_var) where {P <: AbstractPolynomial}
465+
zs = zeros(Int, k+1)
466+
zs[end] = 1
467+
(P){eltype(P)}(zs, var)
468+
end
469+
basis(p::P, k::Int, _var::SymbolLike=:x; var=_var) where {P<:AbstractPolynomial} = basis(P, k, var)
470+
462471
#=
463472
arithmetic =#
464473
Base.:-(p::P) where {P <: AbstractPolynomial} = P(-coeffs(p), p.var)

src/contrib.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ end
3333
## Slight modification when `x` is a matrix
3434
## Remove once dependencies for Julia 1.0.0 are dropped
3535
function evalpoly(x::S, p::Tuple) where {S}
36+
p == () && return zero(S)
3637
if @generated
3738
N = length(p.parameters)
3839
ex = :(p[end]*_one(S))

src/polynomials/ChebyshevT.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function Base.convert(C::Type{<:ChebyshevT}, p::Polynomial)
5959
end
6060

6161
domain(::Type{<:ChebyshevT}) = Interval(-1, 1)
62-
62+
variable(P::Type{<:ChebyshevT}, var::SymbolLike=:x ) = P([0,1], var)
6363
"""
6464
(::ChebyshevT)(x)
6565

0 commit comments

Comments
 (0)