Skip to content

Commit 4ace824

Browse files
authored
V0.9 (#200)
* make deprecations errors * run doctests; fix bug in printpoly * adjust one, zero and handling of var * adjust documentation, move Pade tests to compatability file * remove round function, caused issues (no deprecation phase). Close #201
1 parent aa33559 commit 4ace824

21 files changed

+265
-254
lines changed

Project.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ name = "Polynomials"
22
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
33
license = "MIT"
44
author = "JuliaMath"
5-
version = "0.7.0"
5+
version = "0.9.0"
66

77
[deps]
88
Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5"
99
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1010
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
1111

1212
[compat]
13-
Intervals = "0.5.0, 1.0"
14-
RecipesBase = "0.7, 0.8, 1.0"
13+
Intervals = "0.5, 1"
14+
RecipesBase = "0.7, 0.8, 1"
1515
julia = "1"
1616

1717

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,15 +141,15 @@ julia> roots(Polynomial([0, 0, 1]))
141141

142142
#### Fitting arbitrary data
143143

144-
Fit a polynomial (of degree `deg`) to `x` and `y` using a least-squares approximation.
144+
Fit a polynomial (of degree `deg` or less) to `x` and `y` using a least-squares approximation.
145145

146146
```julia
147147
julia> xs = 0:4; ys = @. exp(-xs) + sin(xs);
148148

149-
julia> fit(xs, ys) |> x -> round(x, digits=4)
149+
julia> fit(xs, ys) |> p -> round.(coeffs(p), digits=4) |> Polynomial
150150
Polynomial(1.0 + 0.0593*x + 0.3959*x^2 - 0.2846*x^3 + 0.0387*x^4)
151151

152-
julia> fit(ChebyshevT, xs, ys, deg=2) |> x -> round(x, digits=4)
152+
julia> fit(ChebyshevT, xs, ys, 2) |> p -> round.(coeffs(p), digits=4) |> ChebyshevT
153153
ChebyshevT(0.5413T_0(x) - 0.8991T_1(x) - 0.4238T_2(x))
154154
```
155155

docs/src/index.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ resulting polynomial is one lower than the degree of `p`.
120120

121121
```jldoctest
122122
julia> derivative(Polynomial([1, 3, -1]))
123-
Polynomial(3 - 2*x)
123+
Polynomial(3.0 - 2.0*x)
124124
```
125125

126126
### Root-finding
@@ -132,18 +132,18 @@ the returned roots may be real or complex.
132132
```jldoctest
133133
julia> roots(Polynomial([1, 0, -1]))
134134
2-element Array{Float64,1}:
135-
1.0
136135
-1.0
136+
1.0
137137
138138
julia> roots(Polynomial([1, 0, 1]))
139139
2-element Array{Complex{Float64},1}:
140-
0.0 - 1.0im
141-
0.0 + 1.0im
140+
0.0 - 1.0im
141+
0.0 + 1.0im
142142
143143
julia> roots(Polynomial([0, 0, 1]))
144144
2-element Array{Float64,1}:
145-
0.0
146-
-0.0
145+
0.0
146+
0.0
147147
```
148148

149149
### Fitting arbitrary data
@@ -193,6 +193,8 @@ julia> convert(ChebyshevT, Polynomial([1.0, 2, 3]))
193193
ChebyshevT(2.5⋅T_0(x) + 2.0⋅T_1(x) + 1.5⋅T_2(x))
194194
```
195195

196+
!!! Note
197+
The older `Poly` type that this package used prior to `v0.7` is implemented as an alternate basis to provide support for older code bases. As of `v1.0`, this type will be only available by executing `using Polynomials.PolyCompat`.
196198

197199
### Iteration
198200

docs/src/polynomials/chebyshev.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ The [Chebyshev polynomials](https://en.wikipedia.org/wiki/Chebyshev_polynomials)
1414

1515
```@docs
1616
ChebyshevT
17-
ChebyshevT()
1817
```
1918

2019
The `ChebyshevT` type holds coefficients representing the polynomial `a_0 T_0 + a_1 T_1 + ... + a_n T_n`.

docs/src/polynomials/polynomial.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ end
88

99
```@docs
1010
Polynomial
11-
Polynomial()
1211
```
1312

1413
```@docs

docs/src/reference.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ domain
2323
mapdomain
2424
chop
2525
chop!
26-
round
2726
truncate
2827
truncate!
2928
```

src/abstract.jl

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ export AbstractPolynomial
33
const SymbolLike = Union{AbstractString,Char,Symbol}
44

55
"""
6-
AbstractPolynomial{<:Number}
6+
AbstractPolynomial{T}
77
88
An abstract container for various polynomials.
99
1010
# Properties
1111
- `coeffs` - The coefficients of the polynomial
1212
- `var` - The indeterminate of the polynomial
1313
"""
14-
abstract type AbstractPolynomial{T<:Number} end
14+
abstract type AbstractPolynomial{T} end
1515

1616

1717
"""
@@ -29,7 +29,7 @@ Polynomials.@register MyPolynomial
2929
# Implementations
3030
This will implement simple self-conversions like `convert(::Type{MyPoly}, p::MyPoly) = p` and creates two promote rules. The first allows promotion between two types (e.g. `promote(Polynomial, ChebyshevT)`) and the second allows promotion between parametrized types (e.g. `promote(Polynomial{T}, Polynomial{S})`).
3131
32-
For constructors, it implements the shortcut for `MyPoly(...) = MyPoly{T}(...)`, singleton constructor `MyPoly(x::Number, ...)`, and conversion constructor `MyPoly{T}(n::S, ...)`.
32+
For constructors, it implements the shortcut for `MyPoly(...) = MyPoly{T}(...)`, singleton constructor `MyPoly(x::Number, ...)`, conversion constructor `MyPoly{T}(n::S, ...)`, and `variable` alternative `MyPoly(var=:x)`.
3333
"""
3434
macro register(name)
3535
poly = esc(name)
@@ -40,19 +40,12 @@ macro register(name)
4040
$poly{promote_type(T, S)}
4141
Base.promote_rule(::Type{$poly{T}}, ::Type{S}) where {T,S<:Number} =
4242
$poly{promote_type(T, S)}
43-
44-
function (p::$poly)(x::AbstractVector)
45-
Base.depwarn(
46-
"Calling p(x::AbstractVector is deprecated. Use p.(x) instead.",
47-
Symbol("(p::AbstractPolynomial)"),
48-
)
49-
return p.(x)
50-
end
51-
5243
$poly(coeffs::AbstractVector{T}, var::SymbolLike = :x) where {T} =
5344
$poly{T}(coeffs, Symbol(var))
54-
$poly(n::Number, var = :x) = $poly([n], var)
55-
$poly{T}(n::S, var = :x) where {T,S<:Number} = $poly(T(n), var)
5645
$poly{T}(x::AbstractVector{S}, var = :x) where {T,S<:Number} = $poly(T.(x), var)
46+
$poly{T}(n::Number, var = :x) where {T} = $poly(T(n), var)
47+
$poly(n::Number, var = :x) = $poly([n], var)
48+
$poly{T}(var=:x) where {T} = variable($poly{T}, var)
49+
$poly(var=:x) = variable($poly, var)
5750
end
5851
end

src/common.jl

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ export fromroots,
2424
Construct a polynomial of the given type given the roots. If no type is given, defaults to `Polynomial`.
2525
2626
# Examples
27-
```jldoctest
27+
```jldoctest common
28+
julia> using Polynomials
29+
2830
julia> r = [3, 2]; # (x - 3)(x - 2)
2931
3032
julia> fromroots(r)
@@ -46,7 +48,9 @@ fromroots(r::AbstractVector{<:Number}; var::SymbolLike = :x) =
4648
Construct a polynomial of the given type using the eigenvalues of the given matrix as the roots. If no type is given, defaults to `Polynomial`.
4749
4850
# Examples
49-
```jldoctest
51+
```jldoctest common
52+
julia> using Polynomials
53+
5054
julia> A = [1 2; 3 4]; # (x - 5.37228)(x + 0.37228)
5155
5256
julia> fromroots(A)
@@ -195,11 +199,11 @@ In-place version of [`chop`](@ref)
195199
function chop!(p::AbstractPolynomial{T};
196200
rtol::Real = Base.rtoldefault(real(T)),
197201
atol::Real = 0,) where {T}
198-
degree(p) == -1 && return p
202+
isempty(coeffs(p)) && return p
199203
for i = lastindex(p):-1:0
200204
val = p[i]
201205
if !isapprox(val, zero(T); rtol = rtol, atol = atol)
202-
resize!(p.coeffs, i + 1)
206+
resize!(p.coeffs, i + 1);
203207
return p
204208
end
205209
end
@@ -219,22 +223,18 @@ function Base.chop(p::AbstractPolynomial{T};
219223
chop!(deepcopy(p), rtol = rtol, atol = atol)
220224
end
221225

222-
"""
223-
round(p::AbstractPolynomial, args...; kwargs)
224-
225-
Applies `round` to the cofficients of `p` with the given arguments. Returns a new polynomial.
226-
"""
227-
Base.round(p::P, args...;kwargs...) where {P <: AbstractPolynomial} = P(round.(coeffs(p), args...; kwargs...), p.var)
228226

229227
"""
230228
variable(var=:x)
231229
variable(::Type{<:AbstractPolynomial}, var=:x)
232230
variable(p::AbstractPolynomial, var=p.var)
233231
234-
Return the monomial `x` in the indicated polynomial basis. If no type is give, will default to [`Polynomial`](@ref).
232+
Return the monomial `x` in the indicated polynomial basis. If no type is give, will default to [`Polynomial`](@ref). Equivalent to `P(var)`.
235233
236234
# Examples
237-
```jldoctest
235+
```jldoctest common
236+
julia> using Polynomials
237+
238238
julia> x = variable()
239239
Polynomial(x)
240240
@@ -243,8 +243,8 @@ Polynomial(100 + 24*x - 3*x^2)
243243
244244
julia> roots((x - 3) * (x + 2))
245245
2-element Array{Float64,1}:
246-
3.0
247246
-2.0
247+
3.0
248248
249249
```
250250
"""
@@ -299,7 +299,7 @@ function Base.iszero(p::AbstractPolynomial)
299299
if length(p) == 0
300300
return true
301301
end
302-
return length(p) == 1 && p[0] == 0
302+
return all(iszero.(coeffs(p))) && p[0] == 0
303303
end
304304

305305
"""
@@ -334,7 +334,9 @@ domain(::P) where {P <: AbstractPolynomial} = domain(P)
334334
Given values of x that are assumed to be unbounded (-∞, ∞), return values rescaled to the domain of the given polynomial.
335335
336336
# Examples
337-
```jldoctest
337+
```jldoctest common
338+
julia> using Polynomials
339+
338340
julia> x = -10:10
339341
-10:10
340342
@@ -383,10 +385,7 @@ Base.collect(p::P) where {P <: AbstractPolynomial} = collect(P, p)
383385

384386
function Base.getproperty(p::AbstractPolynomial, nm::Symbol)
385387
if nm == :a
386-
Base.depwarn("AbstractPolynomial.a is deprecated, use AbstractPolynomial.coeffs or coeffs(AbstractPolynomial) instead.",
387-
Symbol("Base.getproperty"),
388-
)
389-
return getfield(p, :coeffs)
388+
throw(ArgumentError("AbstractPolynomial.a is not supported, use coeffs(AbstractPolynomial) instead."))
390389
end
391390
return getfield(p, nm)
392391
end
@@ -434,16 +433,16 @@ Base.hash(p::AbstractPolynomial, h::UInt) = hash(p.var, hash(coeffs(p), h))
434433
435434
Returns a representation of 0 as the given polynomial.
436435
"""
437-
Base.zero(::Type{P}) where {P <: AbstractPolynomial} = P(zeros(1))
438-
Base.zero(p::P) where {P <: AbstractPolynomial} = zero(P)
436+
Base.zero(::Type{P}, var=:x) where {P <: AbstractPolynomial} = P(zeros(1), var)
437+
Base.zero(p::P) where {P <: AbstractPolynomial} = zero(P, p.var)
439438
"""
440439
one(::Type{<:AbstractPolynomial})
441440
one(::AbstractPolynomial)
442441
443442
Returns a representation of 1 as the given polynomial.
444443
"""
445-
Base.one(::Type{P}) where {P <: AbstractPolynomial} = P(ones(1))
446-
Base.one(p::P) where {P <: AbstractPolynomial} = one(P)
444+
Base.one(::Type{P}, var=:x) where {P <: AbstractPolynomial} = P(ones(1), var)
445+
Base.one(p::P) where {P <: AbstractPolynomial} = one(P, p.var)
447446

448447
#=
449448
arithmetic =#
@@ -493,7 +492,9 @@ Find the greatest common denominator of two polynomials recursively using
493492
494493
# Examples
495494
496-
```jldoctest
495+
```jldoctest common
496+
julia> using Polynomials
497+
497498
julia> gcd(fromroots([1, 1, 2]), fromroots([1, 2, 3]))
498499
Polynomial(4.0 - 6.0*x + 2.0*x^2)
499500
@@ -504,7 +505,7 @@ function Base.gcd(p1::AbstractPolynomial{T}, p2::AbstractPolynomial{S}) where {T
504505
iter = 1
505506
itermax = length(r₁)
506507

507-
while r₁ zero(r₁) && iter itermax # just to avoid unnecessary recursion
508+
while !iszero(r₁) && iter itermax
508509
_, rtemp = divrem(r₀, r₁)
509510
r₀ = r₁
510511
r₁ = truncate(rtemp)

src/compat.jl

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,14 @@
44
## For now we ensure compatability by defining these for `Poly` objects such
55
## that they do not signal a deprecation (save polyfit)),
66
## but do for other `AbstractPolynomial` types.
7-
## At v1.0, it is likely these will be removed.
8-
9-
## Ensure compatability for now
10-
@deprecate polyval(p::AbstractPolynomial, x::Number) p(x)
11-
@deprecate polyval(p::AbstractPolynomial, x) p.(x)
12-
13-
14-
@deprecate polyint(p::AbstractPolynomial, C = 0) integrate(p, C)
15-
@deprecate polyint(p::AbstractPolynomial, a, b) integrate(p, a, b)
16-
17-
@deprecate polyder(p::AbstractPolynomial, ord = 1) derivative(p, ord)
18-
19-
@deprecate polyfit(x, y, n = length(x) - 1, sym=:x) fit(Poly, x, y, n; var = sym)
20-
@deprecate polyfit(x, y, sym::Symbol) fit(Poly, x, y, var = sym)
7+
## At v1.0, these will be opt-in via `using Polynomials.PolyCompat`
218

229

2310
include("polynomials/Poly.jl")
2411
using .PolyCompat
2512
export Poly
26-
export poly, polyval, polyint, polyder, polyfit
27-
28-
29-
13+
export poly, polyval, polyint, polyder
3014

3115
## Pade
32-
## Pade will likely be moved into a separate pacakge
33-
include("pade.jl")
34-
using .PadeApproximation
35-
export Pade
36-
export padeval
16+
## Pade will likely be moved into a separate package, for now we will put into PolyCompat
17+
export Pade, padeval

0 commit comments

Comments
 (0)