diff --git a/Project.toml b/Project.toml index ea93c247..e0344b23 100644 --- a/Project.toml +++ b/Project.toml @@ -2,7 +2,7 @@ name = "Polynomials" uuid = "f27b6e38-b328-58d1-80ce-0feddd5e7a45" license = "MIT" author = "JuliaMath" -version = "2.0.15" +version = "2.0.16" [deps] Intervals = "d8418881-c3e1-53bb-8760-2df7ec849ed5" diff --git a/src/abstract.jl b/src/abstract.jl index a32c8ce4..1d6fbc69 100644 --- a/src/abstract.jl +++ b/src/abstract.jl @@ -92,6 +92,9 @@ macro register(name) cs = collect(coeffs) $poly{eltype(cs), Symbol(var)}(cs) end + $poly{T,X}(c::AbstractPolynomial{S,Y}) where {T,X,S,Y} = convert($poly{T,X}, c) + $poly{T}(c::AbstractPolynomial{S,Y}) where {T,S,Y} = convert($poly{T}, c) + $poly(c::AbstractPolynomial{S,Y}) where {S,Y} = convert($poly, c) $poly{T,X}(n::S) where {T, X, S<:Number} = T(n) * one($poly{T, X}) $poly{T}(n::S, var::SymbolLike = :x) where {T, S<:Number} = @@ -121,6 +124,9 @@ macro registerN(name, params...) end $poly{$(αs...)}(coeffs::AbstractVector{T}, var::SymbolLike=:x) where {$(αs...),T} = $poly{$(αs...),T,Symbol(var)}(coeffs) + $poly{$(as...),T,X}(c::AbstractPolynomial{S,Y}) where {$(as...),T,X,S,Y} = convert($poly{$(as...),T,X}, c) + $poly{$(as...),T}(c::AbstractPolynomial{S,Y}) where {$(as...),T,S,Y} = convert($poly{$(as...),T}, c) + $poly{$(as...),}(c::AbstractPolynomial{S,Y}) where {$(as...),S,Y} = convert($poly{$(as...),}, c) $poly{$(αs...),T,X}(n::Number) where {$(αs...),T,X} = T(n)*one($poly{$(αs...),T,X}) $poly{$(αs...),T}(n::Number, var::SymbolLike = :x) where {$(αs...),T} = T(n)*one($poly{$(αs...),T,Symbol(var)}) diff --git a/src/polynomials/ChebyshevT.jl b/src/polynomials/ChebyshevT.jl index 37984259..d5c6d7d7 100644 --- a/src/polynomials/ChebyshevT.jl +++ b/src/polynomials/ChebyshevT.jl @@ -75,7 +75,12 @@ function Base.convert(P::Type{<:Polynomial}, ch::ChebyshevT) end return c0 + c1 * x end -Base.convert(C::Type{<:ChebyshevT}, p::Polynomial) = p(variable(C)) + +function Base.convert(C::Type{<:ChebyshevT}, p::Polynomial) + x = variable(C) + isconstant(p) || assert_same_variable(indeterminate(x),indeterminate(p)) + p(x) +end Base.promote_rule(::Type{P},::Type{Q}) where {T, X, P <: LaurentPolynomial{T,X}, S, Q <: ChebyshevT{S, X}} = LaurentPolynomial{promote_type(T, S), X} diff --git a/test/StandardBasis.jl b/test/StandardBasis.jl index e7d89972..6da5683a 100644 --- a/test/StandardBasis.jl +++ b/test/StandardBasis.jl @@ -736,6 +736,15 @@ end # issue #287 p = LaurentPolynomial([1], -5) @test p ≈ convert(LaurentPolynomial{Float64}, p) + + # issue #358 `P(p::AbstractPolynomial)` should be `convert(P, p)` not `P(pᵢ for pᵢ ∈ p))` + x² = Polynomial([0,0,1], :x) + for P ∈ (ImmutablePolynomial, SparsePolynomial, ChebyshevT) + @test P(x²) == convert(P, x²) + Q = P{Float64} + @test Q(x²) == convert(Q, x²) + end + end @testset "Roots" begin