Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve copy_with_eltype #492

Merged
merged 3 commits into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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