Skip to content

Commit e0e2033

Browse files
committed
modify eltype(P), clean up functions
1 parent 32af155 commit e0e2033

File tree

8 files changed

+62
-36
lines changed

8 files changed

+62
-36
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/common.jl

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ julia> roots((x - 3) * (x + 2))
249249
250250
```
251251
"""
252-
variable(::Type{P}, var::SymbolLike = :x) where {P <: AbstractPolynomial} = P([0, 1], var)
252+
variable(::Type{P}, var::SymbolLike = :x) where {P <: AbstractPolynomial} = MethodError()
253253
variable(p::AbstractPolynomial, var::SymbolLike = p.var) = variable(typeof(p), var)
254-
variable(var::SymbolLike = :x) = variable(Polynomial{Int})
254+
variable(var::SymbolLike = :x) = variable(Polynomial{Int}, var)
255255

256256
"""
257257
check_same_variable(p::AbstractPolynomial, q::AbstractPolynomial)
@@ -303,7 +303,10 @@ Returns the size of the polynomials coefficients, along axis `i` if provided.
303303
Base.size(p::AbstractPolynomial) = size(coeffs(p))
304304
Base.size(p::AbstractPolynomial, i::Integer) = size(coeffs(p), i)
305305
Base.eltype(p::AbstractPolynomial{T}) where {T} = T
306-
Base.eltype(::Type{P}) where {P <: AbstractPolynomial} = P
306+
# in analogy with polynomial as a Vector{T} with different operations defined.
307+
Base.eltype(::Type{<:AbstractPolynomial}) = Float64
308+
Base.eltype(::Type{<:AbstractPolynomial{T}}) where {T} = T
309+
#Base.eltype(::Type{P}) where {P <: AbstractPolynomial} = P # changed in v1.1.0
307310
function Base.iszero(p::AbstractPolynomial)
308311
if length(p) == 0
309312
return true
@@ -382,16 +385,20 @@ Base.eachindex(p::AbstractPolynomial) = 0:length(p) - 1
382385
Base.broadcastable(p::AbstractPolynomial) = Ref(p)
383386

384387
# basis
388+
# var is a positional argument, not a keyword; can't deprecate so we do `_var; var=_var`
389+
#@deprecate basis(p::P, k::Int; var=:x) where {P<:AbstractPolynomial} basis(p, k, var)
390+
#@deprecate basis(::Type{P}, k::Int; var=:x) where {P <: AbstractPolynomial} basis(P, k,var)
385391
# 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}
392+
function basis(::Type{P}, k::Int, _var::SymbolLike=:x; var=_var) where {P <: AbstractPolynomial}
391393
zs = zeros(Int, k+1)
392394
zs[end] = 1
393395
P(zs, var)
394396
end
397+
basis(p::P, k::Int, _var::SymbolLike=:x; var=_var) where {P<:AbstractPolynomial} = basis(P, k, var)
398+
399+
400+
401+
395402

396403
# iteration
397404
# iteration occurs over the basis polynomials
@@ -445,20 +452,20 @@ Base.hash(p::AbstractPolynomial, h::UInt) = hash(p.var, hash(coeffs(p), h))
445452
446453
Returns a representation of 0 as the given polynomial.
447454
"""
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)
455+
Base.zero(::Type{P}, var=:x) where {P <: AbstractPolynomial} = P(zeros(eltype(P), 1), var)
450456
Base.zero(p::P) where {P <: AbstractPolynomial} = zero(P, p.var)
451457
"""
452458
one(::Type{<:AbstractPolynomial})
453459
one(::AbstractPolynomial)
454460
455461
Returns a representation of 1 as the given polynomial.
456462
"""
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)
463+
Base.one(::Type{P}, var=:x) where {P <: AbstractPolynomial} = P(ones(eltype(P),1), var)
459464
Base.one(p::P) where {P <: AbstractPolynomial} = one(P, p.var)
460-
Base.oneunit(p::P, args...) where {P <: AbstractPolynomial} = one(p, args...)
465+
461466
Base.oneunit(::Type{P}, args...) where {P <: AbstractPolynomial} = one(P, args...)
467+
Base.oneunit(p::P, args...) where {P <: AbstractPolynomial} = one(p, args...)
468+
462469
#=
463470
arithmetic =#
464471
Base.:-(p::P) where {P <: AbstractPolynomial} = P(-coeffs(p), p.var)
@@ -552,7 +559,8 @@ Base.:(==)(n::Number, p::AbstractPolynomial) = p == n
552559
function Base.isapprox(p1::AbstractPolynomial{T},
553560
p2::AbstractPolynomial{S};
554561
rtol::Real = (Base.rtoldefault(T, S, 0)),
555-
atol::Real = 0,) where {T,S}
562+
atol::Real = 0,) where {T,S}
563+
556564
p1, p2 = promote(p1, p2)
557565
check_same_variable(p1, p2) || error("p1 and p2 must have same var")
558566
p1t = truncate(p1; rtol = rtol, atol = atol)
@@ -561,6 +569,7 @@ function Base.isapprox(p1::AbstractPolynomial{T},
561569
return false
562570
end
563571
isapprox(coeffs(p1t), coeffs(p2t), rtol = rtol, atol = atol)
572+
564573
end
565574

566575
function Base.isapprox(p1::AbstractPolynomial{T},

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

src/polynomials/LaurentPolynomial.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ Base.hash(p::LaurentPolynomial, h::UInt) = hash(p.var, hash(range(p), hash(coeff
158158

159159
degree(p::LaurentPolynomial) = p.n[]
160160
isconstant(p::LaurentPolynomial) = range(p) == 0:0
161-
basis(P::Type{<:LaurentPolynomial{T}}, n::Int, var=:x) where{T} = LaurentPolynomial(ones(T,1), n:n, var)
162-
basis(P::Type{LaurentPolynomial}, n::Int, var=:x) = LaurentPolynomial(ones(Float64, 1), n:n, var)
161+
basis(P::Type{<:LaurentPolynomial{T}}, n::Int, var::SymbolLike=:x) where{T} = LaurentPolynomial(ones(T,1), n:n, var)
162+
basis(P::Type{LaurentPolynomial}, n::Int, var::SymbolLike=:x) = LaurentPolynomial(ones(Float64, 1), n:n, var)
163163

164164
Base.zero(::Type{LaurentPolynomial{T}}, var=Symbollike=:x) where {T} = LaurentPolynomial{T}(zeros(T,1), 0:0, Symbol(var))
165165
Base.zero(::Type{LaurentPolynomial}, var=Symbollike=:x) = zero(LaurentPolynomial{Float64}, var)
@@ -259,7 +259,10 @@ function showterm(io::IO, ::Type{<:LaurentPolynomial}, pj::T, var, j, first::Boo
259259
printcoefficient(io, pj, j, mimetype)
260260
end
261261
printproductsign(io, pj, j, mimetype)
262-
unicode_exponent(io, var, j)
262+
iszero(j) && return
263+
print(io, var)
264+
j ==1 && return
265+
unicode_exponent(io, j)
263266
return true
264267
end
265268

src/polynomials/Poly.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ Polynomials.@register Poly
3838

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

41+
Base.eltype(P::Type{<:Poly}) = P
42+
Base.zero(::Type{<:Poly{T}},var=:x) where {T} = Poly{T}(zeros(T,0), var)
43+
Base.zero(::Type{<:Poly},var=:x) where {T} = Poly(zeros(Float64,0), var)
44+
Base.one(::Type{<:Poly{T}},var=:x) where {T} = Poly{T}(ones(T,1), var)
45+
Base.one(::Type{<:Poly},var=:x) where {T} = Poly(ones(Float64,1), var)
46+
4147
function (p::Poly{T})(x::S) where {T,S}
4248
oS = one(x)
4349
length(p) == 0 && return zero(T) * oS
@@ -52,7 +58,7 @@ end
5258
function Base.:+(p1::Poly, p2::Poly)
5359
p1.var != p2.var && error("Polynomials must have same variable")
5460
n = max(length(p1), length(p2))
55-
c = [p1[i] + p2[i] for i = 0:n]
61+
c = [p1[i] + p2[i] for i = 0:n-1]
5662
return Poly(c, p1.var)
5763
end
5864

src/polynomials/SparsePolynomial.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function isconstant(p::SparsePolynomial)
9696
return true
9797
end
9898

99-
basis(P::Type{<:SparsePolynomial}, n::Int, var=:x) =
99+
basis(P::Type{<:SparsePolynomial}, n::Int, var::SymbolLike=:x) =
100100
SparsePolynomial(Dict(n=>one(eltype(one(P)))), var)
101101

102102
# return coeffs as a vector

src/polynomials/standard-basis.jl

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
abstract type StandardBasisPolynomial{T} <: AbstractPolynomial{T} end
22

3-
const = constructorof #\upin
3+
# We want ⟒(P{α…,T}) = P{α…}
4+
# For StandardBasisPolynomials this is
5+
(P::Type{<:StandardBasisPolynomial}) = constructorof(P)
6+
47

58

69
function showterm(io::IO, ::Type{<:StandardBasisPolynomial}, pj::T, var, j, first::Bool, mimetype) where {T}
@@ -14,6 +17,7 @@ function showterm(io::IO, ::Type{<:StandardBasisPolynomial}, pj::T, var, j, firs
1417
return true
1518
end
1619

20+
1721
# allows broadcast issue #209
1822
evalpoly(x, p::StandardBasisPolynomial) = p(x)
1923

@@ -25,6 +29,8 @@ isconstant(p::StandardBasisPolynomial) = degree(p) <= 0
2529

2630
Base.convert(P::Type{<:StandardBasisPolynomial}, q::StandardBasisPolynomial) = isa(q, P) ? q : P([q[i] for i in 0:degree(q)], q.var)
2731

32+
variable(::Type{P}, var::SymbolLike = :x) where {P <: StandardBasisPolynomial} = P([0, 1], var)
33+
2834
function fromroots(P::Type{<:StandardBasisPolynomial}, r::AbstractVector{T}; var::SymbolLike = :x) where {T <: Number}
2935
n = length(r)
3036
c = zeros(T, n + 1)
@@ -45,9 +51,9 @@ function Base.:+(p::P, c::S) where {T, P <: StandardBasisPolynomial{T}, S<:Numbe
4551
end
4652

4753

48-
function derivative(p::P, order::Integer = 1) where {P <: StandardBasisPolynomial}
54+
function derivative(p::P, order::Integer = 1) where {T, P <: StandardBasisPolynomial{T}}
4955
order < 0 && error("Order of derivative must be non-negative")
50-
T = eltype(p)
56+
5157
# we avoid usage like Base.promote_op(*, T, Int) here, say, as
5258
# Base.promote_op(*, Rational, Int) is Any, not Rational in analogy to
5359
# Base.promote_op(*, Complex, Int)
@@ -66,8 +72,8 @@ function derivative(p::P, order::Integer = 1) where {P <: StandardBasisPolynomia
6672
end
6773

6874

69-
function integrate(p::P, k::S) where {P <: StandardBasisPolynomial, S<:Number}
70-
T = eltype(p)
75+
function integrate(p::P, k::S) where {T, P <: StandardBasisPolynomial{T}, S<:Number}
76+
7177
R = eltype((one(T)+one(S))/1)
7278
if hasnan(p) || isnan(k)
7379
return (P)([NaN])
@@ -82,7 +88,7 @@ function integrate(p::P, k::S) where {P <: StandardBasisPolynomial, S<:Number}
8288
end
8389

8490

85-
function Base.divrem(num::P, den::Q) where {P <: StandardBasisPolynomial, Q <: StandardBasisPolynomial}
91+
function Base.divrem(num::P, den::Q) where {T, P <: StandardBasisPolynomial{T}, S, Q <: StandardBasisPolynomial{S}}
8692

8793
check_same_variable(num, den) || error("Polynomials must have same variable")
8894
var = num.var
@@ -94,7 +100,6 @@ function Base.divrem(num::P, den::Q) where {P <: StandardBasisPolynomial, Q <: S
94100
m == -1 && throw(DivideError())
95101
if m == 0 && den[0] 0 throw(DivideError()) end
96102

97-
T, S = eltype(num), eltype(den)
98103
R = eltype(one(T)/one(S))
99104

100105
deg = n - m + 1
@@ -120,13 +125,12 @@ function Base.divrem(num::P, den::Q) where {P <: StandardBasisPolynomial, Q <: S
120125
end
121126

122127

123-
function companion(p::P) where {P <: StandardBasisPolynomial}
128+
function companion(p::P) where {T, P <: StandardBasisPolynomial{T}}
124129
d = length(p) - 1
125130
d < 1 && error("Series must have degree greater than 1")
126131
d == 1 && return diagm(0 => [-p[0] / p[1]])
127132

128133

129-
T = eltype(p)
130134
R = eltype(one(T)/one(T))
131135

132136
comp = diagm(-1 => ones(R, d - 1))
@@ -137,8 +141,8 @@ function companion(p::P) where {P <: StandardBasisPolynomial}
137141
return comp
138142
end
139143

140-
function roots(p::P; kwargs...) where {P <: StandardBasisPolynomial}
141-
T = eltype(p)
144+
function roots(p::P; kwargs...) where {T, P <: StandardBasisPolynomial{T}}
145+
142146
R = eltype(one(T)/one(T))
143147
d = degree(p)
144148
if d < 1

src/show.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,16 @@ function printexponent(io, var, i, mimetype::MIME)
239239
end
240240
end
241241

242-
function unicode_exponent(io, var, j)
243-
iszero(j) && return
244-
print(io, var)
245-
j ==1 && return
242+
function unicode_exponent(io, j)
246243
a = ("","","","","¹","²","³","","","","","","")
247244
for i in string(j)
248245
print(io, a[Int(i)-44])
249246
end
250247
end
248+
249+
function unicode_subscript(io, j)
250+
a = ("","","","","","","","","","","","","")
251+
for i in string(j)
252+
print(io, a[Int(i)-44])
253+
end
254+
end

0 commit comments

Comments
 (0)