Skip to content

Commit 593b4a6

Browse files
committed
code cleanup
1 parent 0bee2bc commit 593b4a6

File tree

2 files changed

+55
-78
lines changed

2 files changed

+55
-78
lines changed

src/common.jl

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -229,33 +229,7 @@ function Base.chop(p::AbstractPolynomial{T};
229229
end
230230

231231

232-
"""
233-
variable(var=:x)
234-
variable(::Type{<:AbstractPolynomial}, var=:x)
235-
variable(p::AbstractPolynomial, var=p.var)
236232

237-
Return the monomial `x` in the indicated polynomial basis. If no type is give, will default to [`Polynomial`](@ref). Equivalent to `P(var)`.
238-
239-
# Examples
240-
```jldoctest common
241-
julia> using Polynomials
242-
243-
julia> x = variable()
244-
Polynomial(x)
245-
246-
julia> p = 100 + 24x - 3x^2
247-
Polynomial(100 + 24*x - 3*x^2)
248-
249-
julia> roots((x - 3) * (x + 2))
250-
2-element Array{Float64,1}:
251-
-2.0
252-
3.0
253-
254-
```
255-
"""
256-
variable(::Type{P}, var::SymbolLike = :x) where {P <: AbstractPolynomial} = MethodError()
257-
variable(p::AbstractPolynomial, var::SymbolLike = p.var) = variable(typeof(p), var)
258-
variable(var::SymbolLike = :x) = variable(Polynomial{Int}, var)
259233

260234
"""
261235
check_same_variable(p::AbstractPolynomial, q::AbstractPolynomial)
@@ -388,18 +362,6 @@ Base.lastindex(p::AbstractPolynomial) = length(p) - 1
388362
Base.eachindex(p::AbstractPolynomial) = 0:length(p) - 1
389363
Base.broadcastable(p::AbstractPolynomial) = Ref(p)
390364

391-
# basis
392-
# var is a positional argument, not a keyword; can't deprecate so we do `_var; var=_var`
393-
#@deprecate basis(p::P, k::Int; var=:x) where {P<:AbstractPolynomial} basis(p, k, var)
394-
#@deprecate basis(::Type{P}, k::Int; var=:x) where {P <: AbstractPolynomial} basis(P, k,var)
395-
# return the kth basis polynomial for the given polynomial type, e.g. x^k for Polynomial{T}
396-
function basis(::Type{P}, k::Int, _var::SymbolLike=:x; var=_var) where {P <: AbstractPolynomial}
397-
zs = zeros(Int, k+1)
398-
zs[end] = 1
399-
(P){eltype(P)}(zs, var)
400-
end
401-
basis(p::P, k::Int, _var::SymbolLike=:x; var=_var) where {P<:AbstractPolynomial} = basis(P, k, var)
402-
403365

404366

405367

@@ -450,6 +412,9 @@ Base.setindex!(p::AbstractPolynomial, values, ::Colon) =
450412
identity =#
451413
Base.copy(p::P) where {P <: AbstractPolynomial} = P(copy(coeffs(p)), p.var)
452414
Base.hash(p::AbstractPolynomial, h::UInt) = hash(p.var, hash(coeffs(p), h))
415+
416+
#=
417+
zero, one, variable, basis =#
453418
"""
454419
zero(::Type{<:AbstractPolynomial})
455420
zero(::AbstractPolynomial)
@@ -470,6 +435,47 @@ Base.one(p::P) where {P <: AbstractPolynomial} = one(P, p.var)
470435
Base.oneunit(::Type{P}, args...) where {P <: AbstractPolynomial} = one(P, args...)
471436
Base.oneunit(p::P, args...) where {P <: AbstractPolynomial} = one(p, args...)
472437

438+
439+
"""
440+
variable(var=:x)
441+
variable(::Type{<:AbstractPolynomial}, var=:x)
442+
variable(p::AbstractPolynomial, var=p.var)
443+
444+
Return the monomial `x` in the indicated polynomial basis. If no type is give, will default to [`Polynomial`](@ref). Equivalent to `P(var)`.
445+
446+
# Examples
447+
```jldoctest common
448+
julia> using Polynomials
449+
450+
julia> x = variable()
451+
Polynomial(x)
452+
453+
julia> p = 100 + 24x - 3x^2
454+
Polynomial(100 + 24*x - 3*x^2)
455+
456+
julia> roots((x - 3) * (x + 2))
457+
2-element Array{Float64,1}:
458+
-2.0
459+
3.0
460+
461+
```
462+
"""
463+
variable(::Type{P}, var::SymbolLike = :x) where {P <: AbstractPolynomial} = MethodError()
464+
variable(p::AbstractPolynomial, var::SymbolLike = p.var) = variable(typeof(p), var)
465+
variable(var::SymbolLike = :x) = variable(Polynomial{Int}, var)
466+
467+
# basis
468+
# var is a positional argument, not a keyword; can't deprecate so we do `_var; var=_var`
469+
#@deprecate basis(p::P, k::Int; var=:x) where {P<:AbstractPolynomial} basis(p, k, var)
470+
#@deprecate basis(::Type{P}, k::Int; var=:x) where {P <: AbstractPolynomial} basis(P, k,var)
471+
# return the kth basis polynomial for the given polynomial type, e.g. x^k for Polynomial{T}
472+
function basis(::Type{P}, k::Int, _var::SymbolLike=:x; var=_var) where {P <: AbstractPolynomial}
473+
zs = zeros(Int, k+1)
474+
zs[end] = 1
475+
(P){eltype(P)}(zs, var)
476+
end
477+
basis(p::P, k::Int, _var::SymbolLike=:x; var=_var) where {P<:AbstractPolynomial} = basis(P, k, var)
478+
473479
#=
474480
arithmetic =#
475481
Base.:-(p::P) where {P <: AbstractPolynomial} = P(-coeffs(p), p.var)

src/polynomials/ImmutablePolynomial.jl

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ Construct an immutable (static) polynomial from its coefficients `a`,
77
lowest order first, optionally in terms of the given variable `x`
88
where `x` can be a character, symbol, or string.
99
10-
If ``p = a_n x^n + \\ldots + a_2 x^2 + a_1 x + a_0``, we construct this through
11-
`ImmutablePolynomial((a_0, a_1, ..., a_n))`.
10+
If ``p = a_n x^n + \\ldots + a_2 x^2 + a_1 x + a_0``, we construct
11+
this through `ImmutablePolynomial((a_0, a_1, ..., a_n))` (assuming
12+
`a_n ≠ 0`). As well, a vector or number can be used for construction.
1213
1314
The usual arithmetic operators are overloaded to work with polynomials
1415
as well as with combinations of polynomials and scalars. However,
1516
operations involving two polynomials of different variables causes an
1617
error, though for `+` and `*` operations, constant polynomials are
17-
treated as having no variable. (This adds a runtime check, but is useful when working with matrices of polynomials.)
18+
treated as having no variable.
1819
19-
As the coefficient size is a compile-time constant, immutable polynomials can take advantage of faster polynomial evaluation provided by `evalpoly` from Julia 1.4.
20+
As the coefficient size is a compile-time constant, immutable
21+
polynomials can take advantage of faster polynomial evaluation
22+
provided by `evalpoly` from Julia 1.4.
2023
2124
# Examples
2225
@@ -74,12 +77,6 @@ end
7477

7578
@register ImmutablePolynomial
7679

77-
#Base.promote_rule(::Type{ImmutablePolynomial{T,N}},q::Type{ImmutablePolynomial{S,M}}) where {T,N,S,M} =
78-
# ImmutablePolynomial{promote_type(T,S)}
79-
80-
#Base.promote_rule(::Type{ImmutablePolynomial{T,N}}, ::Type{S}) where {T,N, S<:Number} =
81-
# ImmutablePolynomial{promote_type(T, S)}
82-
8380
function ImmutablePolynomial{T,N}(coeffs::Tuple, var) where {T,N}
8481
ImmutablePolynomial{T,N}(NTuple{N,T}(c for c in coeffs), var)
8582
end
@@ -94,21 +91,6 @@ function ImmutablePolynomial(coeffs::NTuple{M,T}, var::SymbolLike=:x) where {M,
9491
end
9592
ImmutablePolynomial{T,N}(cs, var)
9693
end
97-
function ImmutablePolynomial(coeffs::Vector{T}, var::SymbolLike=:x) where {T}
98-
N = findlast(!iszero, coeffs)
99-
if N == nothing
100-
return zero(ImmutablePolynomial{T},var)
101-
else
102-
cs = NTuple{N,T}(T(c) for c in coeffs[1:N])
103-
end
104-
ImmutablePolynomial{T,N}(cs, var)
105-
end
106-
107-
#ImmutablePolynomial(n::T, var::SymbolLike = :x) where {T <: Number} =
108-
# ImmutablePolynomial{T,1}(NTuple{1,T}(n), var)
109-
110-
#ImmutablePolynomial(var::SymbolLike=:x) = variable(ImmutablePolynomial, var)
111-
11294

11395
# Convenience; pass tuple to Polynomial
11496
# Not documented, not sure this is a good idea as P(...)::P is not true...
@@ -123,24 +105,12 @@ end
123105
##
124106
# overrides from common.jl due to coeffs possibly being padded, coeffs being non mutable, ...
125107

126-
## promote N,M case; may not change p,q if T==S
127-
#function Base.promote(p::ImmutablePolynomial{T,N}, q::ImmutablePolynomial{S,M}) where {N,T,M,S}
128-
# R = promote_type(T,S)
129-
# ImmutablePolynomial{R}(p.coeffs, p.var), ImmutablePolynomial{R}(q.coeffs, q.var)
130-
#end
131-
132108
Base.collect(p::P) where {P <: ImmutablePolynomial} = [pᵢ for pᵢ p]
133109

134110
Base.copy(p::P) where {P <: ImmutablePolynomial} = P(coeffs(p), p.var)
135111

136-
#function Base.hash(p::ImmutablePolynomial{T,N}, h::UInt) where {T,N}
137-
# n = findlast(!iszero, coeffs(p))
138-
# n == nothing && return hash(p.var, hash(NTuple{0,T}(),h))
139-
# hash(p.var, hash(coeffs(p)[1:n], h))
140-
#end
141-
142112
# catch q == 0 case
143-
LinearAlgebra.norm(q::ImmutablePolynomial{T}, p::Real = 2) where {T}= degree(q) == -1 ? zero(T) : norm(coeffs(q), p)
113+
LinearAlgebra.norm(q::ImmutablePolynomial{T}, p::Real = 2) where {T} = degree(q) == -1 ? zero(T) : norm(coeffs(q), p)
144114

145115
# zero, one, variable
146116
function Base.zero(P::Type{<:ImmutablePolynomial},var::SymbolLike=:x)
@@ -276,10 +246,8 @@ function Base.:+(p1::ImmutablePolynomial{T,N}, p2::ImmutablePolynomial{S,M}) whe
276246
return ImmutablePolynomial(cs, p1.var) # N unknown, as leading terms can cancel
277247
elseif N < M
278248
(p2,p1)
279-
280249
else
281250
(p1,p2)
282-
283251
end
284252

285253
end
@@ -301,6 +269,7 @@ end
301269
Base.@_inline_meta
302270
ImmutablePolynomial{$(R),$(N)}(tuple($(exprs...)), p1.var)
303271
end
272+
304273
end
305274

306275

@@ -328,10 +297,12 @@ end
328297
end
329298
end
330299
end
300+
331301
return quote
332302
Base.@_inline_meta
333303
ImmutablePolynomial{$(R),$(max(0,P))}(tuple($(exprs...)), p1.var)
334304
end
305+
335306
end
336307

337308
# scalar ops

0 commit comments

Comments
 (0)