Skip to content

Commit 04b6167

Browse files
authored
improve copy_with_eltype (#492)
* improve copy_with_eltype * easier to read means to modify type of tuple * remove piracy
1 parent 5df7fe9 commit 04b6167

File tree

5 files changed

+26
-23
lines changed

5 files changed

+26
-23
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "Polynomials"
22
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
33
license = "MIT"
44
author = "JuliaMath"
5-
version = "3.2.8"
5+
version = "3.2.9"
66

77
[deps]
88
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"

src/common.jl

+15-13
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,9 @@ function truncate!(ps::Dict{Int,T};
347347
end
348348

349349
truncate!(ps::NTuple; kwargs...) = throw(ArgumentError("`truncate!` not defined."))
350-
Base.truncate(ps::NTuple{0}; kwargs...) = ps
351-
function Base.truncate(ps::NTuple{N,T};
350+
351+
_truncate(ps::NTuple{0}; kwargs...) = ps
352+
function _truncate(ps::NTuple{N,T};
352353
rtol::Real = Base.rtoldefault(real(T)),
353354
atol::Real = 0,) where {N,T}
354355
thresh = norm(ps, Inf) * rtol + atol
@@ -415,8 +416,9 @@ function chop!(ps::Dict{Int,T};
415416
end
416417

417418
chop!(ps::NTuple; kwargs...) = throw(ArgumentError("chop! not defined"))
418-
Base.chop(ps::NTuple{0}; kwargs...) = ps
419-
function Base.chop(ps::NTuple{N,T};
419+
420+
_chop(ps::NTuple{0}; kwargs...) = ps
421+
function _chop(ps::NTuple{N,T};
420422
rtol::Real = Base.rtoldefault(real(T)),
421423
atol::Real = 0,) where {N,T}
422424
thresh = norm(ps, Inf) * rtol + atol
@@ -530,19 +532,19 @@ function _eltype(P::Type{<:AbstractPolynomial}, p::AbstractPolynomial)
530532
end
531533

532534
"""
533-
copy_with_eltype(::Val{T}, [::Val{X}], p::AbstractPolynomial)
535+
copy_with_eltype(::Type{T}, [::Val{X}], p::AbstractPolynomial)
534536
535537
Copy polynomial `p` changing the underlying element type and optionally the symbol.
536538
"""
537-
copy_with_eltype(::Val{T}, ::Val{X}, p::P) where {T, X, S, Y, P <:AbstractPolynomial{S,Y}} =
538-
(P){T, X}(p.coeffs)
539-
copy_with_eltype(V::Val{T}, p::P) where {T, S, Y, P <:AbstractPolynomial{S,Y}} =
540-
copy_with_eltype(V, Val(Y), p)
539+
copy_with_eltype(::Type{T}, ::Val{X}, p::P) where {T, X, S, Y, P <:AbstractPolynomial{S,Y}} =
540+
(P){T, Symbol(X)}(p.coeffs)
541+
copy_with_eltype(::Type{T}, p::P) where {T, S, Y, P <:AbstractPolynomial{S,Y}} =
542+
copy_with_eltype(T, Val(Y), p)
541543
# easier to type if performance isn't an issue, but could be dropped
542-
copy_with_eltype(::Type{T}, X, p::P) where {T, S, Y, P<:AbstractPolynomial{S, Y}} =
543-
copy_with_eltype(Val(T), Val(X), p)
544-
copy_with_eltype(::Type{T}, p::P) where {T, S, X, P<:AbstractPolynomial{S,X}} =
545-
copy_with_eltype(Val(T), Val(X), p)
544+
#copy_with_eltype(::Type{T}, X, p::P) where {T, S, Y, P<:AbstractPolynomial{S, Y}} =
545+
# copy_with_eltype(Val(T), Val(X), p)
546+
#copy_with_eltype(::Type{T}, p::P) where {T, S, X, P<:AbstractPolynomial{S,X}} =
547+
# copy_with_eltype(Val(T), Val(X), p)
546548

547549
Base.iszero(p::AbstractPolynomial) = all(iszero, p)
548550

src/polynomials/ImmutablePolynomial.jl

+5-4
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,9 @@ end
109109
# overrides from common.jl due to coeffs being non mutable, N in type parameters
110110

111111
Base.copy(p::P) where {P <: ImmutablePolynomial} = P(coeffs(p))
112-
Base.similar(p::ImmutablePolynomial, args...) =
113-
similar(collect(oeffs(p)), args...)
112+
copy_with_eltype(::Type{T}, ::Val{X}, p::P) where {T, X, S, Y, N, P <:ImmutablePolynomial{S,Y,N}} =
113+
(P){T, Symbol(X),N}(map(T, p.coeffs))
114+
Base.similar(p::ImmutablePolynomial, args...) = similar(collect(coeffs(p)), args...) # ???
114115
# degree, isconstant
115116
degree(p::ImmutablePolynomial{T,X, N}) where {T,X,N} = N - 1 # no trailing zeros
116117
isconstant(p::ImmutablePolynomial{T,X,N}) where {T,X,N} = N <= 1
@@ -135,14 +136,14 @@ end
135136
function Base.chop(p::ImmutablePolynomial{T,X};
136137
rtol::Real = Base.rtoldefault(real(T)),
137138
atol::Real = 0) where {T,X}
138-
ps = chop(p.coeffs; rtol=rtol, atol=atol)
139+
ps = _chop(p.coeffs; rtol=rtol, atol=atol)
139140
return ImmutablePolynomial{T,X}(ps)
140141
end
141142

142143
function Base.truncate(p::ImmutablePolynomial{T,X};
143144
rtol::Real = Base.rtoldefault(real(T)),
144145
atol::Real = 0) where {T,X}
145-
ps = truncate(p.coeffs; rtol=rtol, atol=atol)
146+
ps = _truncate(p.coeffs; rtol=rtol, atol=atol)
146147
ImmutablePolynomial{T,X}(ps)
147148
end
148149

src/polynomials/factored_polynomial.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function Base.convert(P::Type{<:FactoredPolynomial}, p::Polynomial{T,X}) where {
119119
(P)(coeffs(p), X)
120120
end
121121

122-
function copy_with_eltype(::Val{T}, ::Val{X}, p::P) where {T, X, S, Y, P<:FactoredPolynomial{S, Y}}
122+
function copy_with_eltype(::Type{T}, ::Val{X}, p::P) where {T, X, S, Y, P<:FactoredPolynomial{S, Y}}
123123
d = convert(Dict{T, Int}, p.coeffs)
124124
FactoredPolynomial{T, X}(d)
125125
end

test/StandardBasis.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -871,11 +871,11 @@ end
871871
for T in (Float64, Rational)
872872
xs = [1,2,3]
873873
p = fromroots(P,xs)
874-
@test Polynomials.copy_with_eltype(Val(T), p) == fromroots(P, T.(xs))
875-
@test Polynomials.copy_with_eltype(Val(T), Val(:u), p) == fromroots(P, T.(xs); var=:u)
874+
@test Polynomials.copy_with_eltype(T, p) == fromroots(P, T.(xs))
875+
@test Polynomials.copy_with_eltype(T, Val(:u), p) == fromroots(P, T.(xs); var=:u)
876876
P == ImmutablePolynomial && continue
877-
@inferred Polynomials.copy_with_eltype(Val(T), Val(:u), p)
878-
@inferred Polynomials.copy_with_eltype(Val(T), p)
877+
@inferred Polynomials.copy_with_eltype(T, Val(:u), p)
878+
@inferred Polynomials.copy_with_eltype(T, p)
879879
end
880880
end
881881
end

0 commit comments

Comments
 (0)