diff --git a/Project.toml b/Project.toml index 207a2924..211ac814 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/common.jl b/src/common.jl index 4627b187..ad75fda7 100644 --- a/src/common.jl +++ b/src/common.jl @@ -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 @@ -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 @@ -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) diff --git a/src/polynomials/ImmutablePolynomial.jl b/src/polynomials/ImmutablePolynomial.jl index 67c8eb8a..778b71ee 100644 --- a/src/polynomials/ImmutablePolynomial.jl +++ b/src/polynomials/ImmutablePolynomial.jl @@ -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 @@ -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 diff --git a/src/polynomials/factored_polynomial.jl b/src/polynomials/factored_polynomial.jl index b98595b7..2b24bd41 100644 --- a/src/polynomials/factored_polynomial.jl +++ b/src/polynomials/factored_polynomial.jl @@ -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 diff --git a/test/StandardBasis.jl b/test/StandardBasis.jl index 0dcebec8..1240eb04 100644 --- a/test/StandardBasis.jl +++ b/test/StandardBasis.jl @@ -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