Skip to content

Commit 6a9739d

Browse files
committed
handle compatability differently
1 parent 0e7a1f8 commit 6a9739d

File tree

7 files changed

+84
-40
lines changed

7 files changed

+84
-40
lines changed

src/Polynomials.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ include("contrib.jl")
1010

1111
# Polynomials
1212
include("polynomials/Polynomial.jl")
13-
include("polynomials/ChebyshevT.jl")
14-
#include("polynomials/ChebyshevU.jl")
1513

14+
include("polynomials/ChebyshevT.jl")
1615

17-
include("polynomials/Poly.jl") # to be deprecated, then removed
16+
# to be deprecated, then removed
17+
include("polynomials/Poly.jl")
1818
include("pade.jl")
19-
include("compat.jl") # Where we keep deprecations
2019

2120
# Interface for all AbstractPolynomials
2221
include("common.jl")

src/common.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,17 @@ end
388388

389389
Base.collect(p::P) where {P <: AbstractPolynomial} = collect(P, p)
390390

391+
function Base.getproperty(p::AbstractPolynomial, nm::Symbol)
392+
if nm == :a
393+
Base.depwarn("AbstractPolynomial.a is deprecated, use AbstractPolynomial.coeffs or coeffs(AbstractPolynomial) instead.",
394+
Symbol("Base.getproperty"),
395+
)
396+
return getfield(p, :coeffs)
397+
end
398+
return getfield(p, nm)
399+
end
400+
401+
391402
# getindex
392403
function Base.getindex(p::AbstractPolynomial{T}, idx::Int) where {T <: Number}
393404
idx < 0 && throw(BoundsError(p, idx))

src/compat.jl

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,21 @@
22
## then deprecate these
33
## then release v1.0
44

5-
poly(r, var = :x) = fromroots(Polynomial, r; var = var)
6-
polyval(p::AbstractPolynomial, x::Number) = p(x)
7-
polyval(p::AbstractPolynomial, x) = p.(x)
8-
9-
function Base.getproperty(p::AbstractPolynomial, nm::Symbol)
10-
if nm == :a
11-
#Base.depwarn("AbstracPolynomial.a is deprecated, use AbstracPolynomial.coeffs or coeffs(AbstractPolynomial) instead.",
12-
# Symbol("Base.getproperty"),
13-
#)
14-
return getfield(p, :coeffs)
15-
end
16-
return getfield(p, nm)
17-
end
18-
19-
polyint(p::AbstractPolynomial, C = 0) = integrate(p, C)
20-
polyint(p::AbstractPolynomial, a, b) = integrate(p, a, b)
21-
polyder(p::AbstractPolynomial, ord = 1) = derivative(p, ord)
22-
polyfit(x, y, n = length(x) - 1, sym=:x) = fit(Polynomial, x, y, n; var = sym)
23-
polyfit(x, y, sym::Symbol) = fit(Polynomial, x, y, var = sym)
24-
25-
padeval(PQ::Pade, x::Number) = PQ(x)
26-
padeval(PQ::Pade, x) = PQ.(x)
27-
28-
export poly, polyval, polyint, polyder, polyfit, padeval
5+
## poly(r, var = :x) = fromroots(Polynomial, r; var = var)
6+
## polyval(p::AbstractPolynomial, x::Number) = p(x)
7+
## polyval(p::AbstractPolynomial, x) = p.(x)
8+
9+
10+
## polyint(p::AbstractPolynomial, C = 0) = integrate(p, C)
11+
## polyint(p::AbstractPolynomial, a, b) = integrate(p, a, b)
12+
## polyder(p::AbstractPolynomial, ord = 1) = derivative(p, ord)
13+
## polyfit(x, y, n = length(x) - 1, sym=:x) = fit(Polynomial, x, y, n; var = sym)
14+
## polyfit(x, y, sym::Symbol) = fit(Polynomial, x, y, var = sym)
15+
16+
## padeval(PQ::Pade, x::Number) = PQ(x)
17+
## padeval(PQ::Pade, x) = PQ.(x)
18+
19+
## export poly, polyval, polyint, polyder, polyfit, padeval
2920

3021

3122
## @deprecate poly(r, var = :x) fromroots(Polynomial, r; var = var)

src/pade.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,10 @@ true
9494
```
9595
"""
9696
(PQ::Pade)(x) = PQ.p(x) / PQ.q(x)
97+
98+
99+
## Compat
100+
padeval(PQ::Pade, x::Number) = PQ(x)
101+
padeval(PQ::Pade, x) = PQ.(x)
102+
103+
export padeval

src/polynomials/Poly.jl

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
module PolyCompat
2+
3+
using ..Polynomials
14
#=
25
This type is only here to provide stability while deprecating. This will eventually be removed in favor
36
of `Polynomial` =#
@@ -7,7 +10,7 @@ export Poly
710
struct Poly{T <: Number} <: AbstractPolynomial{T}
811
coeffs::Vector{T}
912
var::Symbol
10-
function Poly(a::AbstractVector{T}, var::SymbolLike = :x) where {T <: Number}
13+
function Poly(a::AbstractVector{T}, var::Polynomials.SymbolLike = :x) where {T <: Number}
1114
# if a == [] we replace it with a = [0]
1215
## Base.depwarn("Poly is deprecated and will be removed in a future release. Please use Polynomial instead", :Poly)
1316
if length(a) == 0
@@ -21,12 +24,16 @@ struct Poly{T <: Number} <: AbstractPolynomial{T}
2124
end
2225
end
2326

24-
@register Poly
27+
Polynomials.@register Poly
28+
29+
30+
31+
2532

2633
Base.convert(P::Type{<:Polynomial}, p::Poly{T}) where {T} = P(p.coeffs, p.var)
2734

28-
domain(::Type{<:Poly}) = Interval(-Inf, Inf)
29-
mapdomain(::Type{<:Poly}, x::AbstractArray) = x
35+
Polynomials.domain(::Type{<:Poly}) = Interval(-Inf, Inf)
36+
Polynomials.mapdomain(::Type{<:Poly}, x::AbstractArray) = x
3037

3138
function (p::Poly{T})(x::S) where {T,S}
3239
oS = one(x)
@@ -39,7 +46,7 @@ function (p::Poly{T})(x::S) where {T,S}
3946
end
4047

4148

42-
function fromroots(P::Type{<:Poly}, r::AbstractVector{T}; var::SymbolLike = :x) where {T <: Number}
49+
function Polynomials.fromroots(P::Type{<:Poly}, r::AbstractVector{T}; var::Polynomials.SymbolLike = :x) where {T <: Number}
4350
n = length(r)
4451
c = zeros(T, n + 1)
4552
c[1] = one(T)
@@ -50,7 +57,7 @@ function fromroots(P::Type{<:Poly}, r::AbstractVector{T}; var::SymbolLike = :x)
5057
end
5158

5259

53-
function vander(P::Type{<:Poly}, x::AbstractVector{T}, n::Integer) where {T <: Number}
60+
function Polynomials.vander(P::Type{<:Poly}, x::AbstractVector{T}, n::Integer) where {T <: Number}
5461
A = Matrix{T}(undef, length(x), n + 1)
5562
A[:, 1] .= one(T)
5663
@inbounds for i in 1:n
@@ -60,7 +67,7 @@ function vander(P::Type{<:Poly}, x::AbstractVector{T}, n::Integer) where {T <: N
6067
end
6168

6269

63-
function integrate(p::Poly{T}, k::S) where {T,S <: Number}
70+
function Polynomials.integrate(p::Poly{T}, k::S) where {T,S <: Number}
6471
R = promote_type(eltype(one(T) / 1), S)
6572
if hasnan(p) || isnan(k)
6673
return Poly([NaN])
@@ -75,7 +82,7 @@ function integrate(p::Poly{T}, k::S) where {T,S <: Number}
7582
end
7683

7784

78-
function derivative(p::Poly{T}, order::Integer = 1) where {T}
85+
function Polynomials.derivative(p::Poly{T}, order::Integer = 1) where {T}
7986
order < 0 && error("Order of derivative must be non-negative")
8087
order == 0 && return p
8188
hasnan(p) && return Poly(T[NaN], p.var)
@@ -90,7 +97,7 @@ function derivative(p::Poly{T}, order::Integer = 1) where {T}
9097
end
9198

9299

93-
function companion(p::Poly{T}) where T
100+
function Polynomials.companion(p::Poly{T}) where T
94101
d = length(p) - 1
95102
d < 1 && error("Series must have degree greater than 1")
96103
d == 1 && return diagm(0 => [-p[0] / p[1]])
@@ -146,4 +153,33 @@ function Base.divrem(num::Poly{T}, den::Poly{S}) where {T,S}
146153
return P(q_coeff, num.var), P(r_coeff, num.var)
147154
end
148155

149-
showterm(io::IO, ::Type{Poly{T}}, pj::T, var, j, first::Bool, mimetype) where {T} = showterm(io, Polynomial{T}, pj, var, j, first, mimetype)
156+
Polynomials.showterm(io::IO, ::Type{Poly{T}}, pj::T, var, j, first::Bool, mimetype) where {T} = showterm(io, Polynomial{T}, pj, var, j, first, mimetype)
157+
158+
159+
160+
## Compat
161+
poly(r, var = :x) = fromroots(Poly, r; var = var)
162+
polyval(p::Poly, x::Number) = p(x)
163+
polyval(p::Poly, x) = p.(x)
164+
165+
function Base.getproperty(p::Poly, nm::Symbol)
166+
if nm == :a
167+
return getfield(p, :coeffs)
168+
end
169+
return getfield(p, nm)
170+
end
171+
172+
polyint(p::Poly, C = 0) = integrate(p, C)
173+
polyint(p::Poly, a, b) = integrate(p, a, b)
174+
polyder(p::Poly, ord = 1) = derivative(p, ord)
175+
polyfit(x, y, n = length(x) - 1, sym=:x) = fit(Poly, x, y, n; var = sym)
176+
polyfit(x, y, sym::Symbol) = fit(Poly, x, y, var = sym)
177+
178+
export poly, polyval, polyint, polyder, polyfit
179+
180+
end
181+
182+
## Ensure compatability for now
183+
using .PolyCompat
184+
export Poly
185+
export poly, polyval, polyint, polyder, polyfit, padeval

src/polynomials/Polynomial.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ function Base.divrem(num::Polynomial{T}, den::Polynomial{S}) where {T,S}
226226
return zero(P), convert(P, num)
227227
end
228228
q_coeff = zeros(R, deg)
229-
r_coeff = R[ num.a[i] for i in 1:n+1 ]
229+
r_coeff = R[ num[i-1] for i in 1:n+1 ]
230230
@inbounds for i in n:-1:m
231231
q = r_coeff[i + 1] / den[m]
232232
q_coeff[i - m + 1] = q

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ import SparseArrays: sparse, nnz
1010
@testset "Polynomial" begin include("Polynomial.jl") end
1111
@testset "ChebyshevT" begin include("ChebyshevT.jl") end
1212
#@testset "Deprecations" begin include("deprecated.jl") end
13-
@testset "Poly (to be deprecated)" begin include("Poly.jl") end
13+
@testset "Poly (compatability)" begin include("Poly.jl") end

0 commit comments

Comments
 (0)