Skip to content

Commit

Permalink
improve copy_with_eltype (#492)
Browse files Browse the repository at this point in the history
* improve copy_with_eltype

* easier to read means to modify type of tuple

* remove piracy
  • Loading branch information
jverzani authored Apr 16, 2023
1 parent 5df7fe9 commit 04b6167
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "Polynomials"
uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45"
license = "MIT"
author = "JuliaMath"
version = "3.2.8"
version = "3.2.9"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
28 changes: 15 additions & 13 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ function truncate!(ps::Dict{Int,T};
end

truncate!(ps::NTuple; kwargs...) = throw(ArgumentError("`truncate!` not defined."))
Base.truncate(ps::NTuple{0}; kwargs...) = ps
function Base.truncate(ps::NTuple{N,T};

_truncate(ps::NTuple{0}; kwargs...) = ps
function _truncate(ps::NTuple{N,T};
rtol::Real = Base.rtoldefault(real(T)),
atol::Real = 0,) where {N,T}
thresh = norm(ps, Inf) * rtol + atol
Expand Down Expand Up @@ -415,8 +416,9 @@ function chop!(ps::Dict{Int,T};
end

chop!(ps::NTuple; kwargs...) = throw(ArgumentError("chop! not defined"))
Base.chop(ps::NTuple{0}; kwargs...) = ps
function Base.chop(ps::NTuple{N,T};

_chop(ps::NTuple{0}; kwargs...) = ps
function _chop(ps::NTuple{N,T};
rtol::Real = Base.rtoldefault(real(T)),
atol::Real = 0,) where {N,T}
thresh = norm(ps, Inf) * rtol + atol
Expand Down Expand Up @@ -530,19 +532,19 @@ function _eltype(P::Type{<:AbstractPolynomial}, p::AbstractPolynomial)
end

"""
copy_with_eltype(::Val{T}, [::Val{X}], p::AbstractPolynomial)
copy_with_eltype(::Type{T}, [::Val{X}], p::AbstractPolynomial)
Copy polynomial `p` changing the underlying element type and optionally the symbol.
"""
copy_with_eltype(::Val{T}, ::Val{X}, p::P) where {T, X, S, Y, P <:AbstractPolynomial{S,Y}} =
(P){T, X}(p.coeffs)
copy_with_eltype(V::Val{T}, p::P) where {T, S, Y, P <:AbstractPolynomial{S,Y}} =
copy_with_eltype(V, Val(Y), p)
copy_with_eltype(::Type{T}, ::Val{X}, p::P) where {T, X, S, Y, P <:AbstractPolynomial{S,Y}} =
(P){T, Symbol(X)}(p.coeffs)
copy_with_eltype(::Type{T}, p::P) where {T, S, Y, P <:AbstractPolynomial{S,Y}} =
copy_with_eltype(T, Val(Y), p)
# easier to type if performance isn't an issue, but could be dropped
copy_with_eltype(::Type{T}, X, p::P) where {T, S, Y, P<:AbstractPolynomial{S, Y}} =
copy_with_eltype(Val(T), Val(X), p)
copy_with_eltype(::Type{T}, p::P) where {T, S, X, P<:AbstractPolynomial{S,X}} =
copy_with_eltype(Val(T), Val(X), p)
#copy_with_eltype(::Type{T}, X, p::P) where {T, S, Y, P<:AbstractPolynomial{S, Y}} =
# copy_with_eltype(Val(T), Val(X), p)
#copy_with_eltype(::Type{T}, p::P) where {T, S, X, P<:AbstractPolynomial{S,X}} =
# copy_with_eltype(Val(T), Val(X), p)

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

Expand Down
9 changes: 5 additions & 4 deletions src/polynomials/ImmutablePolynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ end
# overrides from common.jl due to coeffs being non mutable, N in type parameters

Base.copy(p::P) where {P <: ImmutablePolynomial} = P(coeffs(p))
Base.similar(p::ImmutablePolynomial, args...) =
similar(collect(oeffs(p)), args...)
copy_with_eltype(::Type{T}, ::Val{X}, p::P) where {T, X, S, Y, N, P <:ImmutablePolynomial{S,Y,N}} =
(P){T, Symbol(X),N}(map(T, p.coeffs))
Base.similar(p::ImmutablePolynomial, args...) = similar(collect(coeffs(p)), args...) # ???
# degree, isconstant
degree(p::ImmutablePolynomial{T,X, N}) where {T,X,N} = N - 1 # no trailing zeros
isconstant(p::ImmutablePolynomial{T,X,N}) where {T,X,N} = N <= 1
Expand All @@ -135,14 +136,14 @@ end
function Base.chop(p::ImmutablePolynomial{T,X};
rtol::Real = Base.rtoldefault(real(T)),
atol::Real = 0) where {T,X}
ps = chop(p.coeffs; rtol=rtol, atol=atol)
ps = _chop(p.coeffs; rtol=rtol, atol=atol)
return ImmutablePolynomial{T,X}(ps)
end

function Base.truncate(p::ImmutablePolynomial{T,X};
rtol::Real = Base.rtoldefault(real(T)),
atol::Real = 0) where {T,X}
ps = truncate(p.coeffs; rtol=rtol, atol=atol)
ps = _truncate(p.coeffs; rtol=rtol, atol=atol)
ImmutablePolynomial{T,X}(ps)
end

Expand Down
2 changes: 1 addition & 1 deletion src/polynomials/factored_polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ function Base.convert(P::Type{<:FactoredPolynomial}, p::Polynomial{T,X}) where {
(P)(coeffs(p), X)
end

function copy_with_eltype(::Val{T}, ::Val{X}, p::P) where {T, X, S, Y, P<:FactoredPolynomial{S, Y}}
function copy_with_eltype(::Type{T}, ::Val{X}, p::P) where {T, X, S, Y, P<:FactoredPolynomial{S, Y}}
d = convert(Dict{T, Int}, p.coeffs)
FactoredPolynomial{T, X}(d)
end
Expand Down
8 changes: 4 additions & 4 deletions test/StandardBasis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -871,11 +871,11 @@ end
for T in (Float64, Rational)
xs = [1,2,3]
p = fromroots(P,xs)
@test Polynomials.copy_with_eltype(Val(T), p) == fromroots(P, T.(xs))
@test Polynomials.copy_with_eltype(Val(T), Val(:u), p) == fromroots(P, T.(xs); var=:u)
@test Polynomials.copy_with_eltype(T, p) == fromroots(P, T.(xs))
@test Polynomials.copy_with_eltype(T, Val(:u), p) == fromroots(P, T.(xs); var=:u)
P == ImmutablePolynomial && continue
@inferred Polynomials.copy_with_eltype(Val(T), Val(:u), p)
@inferred Polynomials.copy_with_eltype(Val(T), p)
@inferred Polynomials.copy_with_eltype(T, Val(:u), p)
@inferred Polynomials.copy_with_eltype(T, p)
end
end
end
Expand Down

2 comments on commit 04b6167

@jverzani
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/81702

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.2.9 -m "<description of version>" 04b6167961e8ccfd62da572474e42a4dd8062ebd
git push origin v3.2.9

Please sign in to comment.