Skip to content

Commit 64bab4f

Browse files
authored
over_ring (#450)
* over_ring * change name; special case factored polynomial
1 parent 63b756b commit 64bab4f

File tree

4 files changed

+27
-1
lines changed

4 files changed

+27
-1
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.0"
5+
version = "3.2.1"
66

77
[deps]
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/common.jl

+11
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,17 @@ function _eltype(P::Type{<:AbstractPolynomial}, p::AbstractPolynomial)
465465
T = isnothing(T′) ? eltype(p) : T′
466466
T
467467
end
468+
469+
"""
470+
copy_with_eltype(T, [X], p::AbstractPolynomial)
471+
472+
Copy polynomial `p` changing the underlying element type and optionally the symbol.
473+
"""
474+
function copy_with_eltype(::Type{T}, X, p::P) where {T, S, Y, P<:AbstractPolynomial{S, Y}}
475+
(P){T, X}(p.coeffs)
476+
end
477+
copy_with_eltype(::Type{T}, p::P) where {T, S, X, P<:AbstractPolynomial{S,X}} = copy_with_eltype(T, X, p)
478+
468479
Base.iszero(p::AbstractPolynomial) = all(iszero, p)
469480

470481

src/polynomials/factored_polynomial.jl

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

122+
function copy_with_eltype(::Type{T}, X, p::P) where {T, S, Y, P<:FactoredPolynomial{S, Y}}
123+
d = convert(Dict{T, Int}, p.coeffs)
124+
FactoredPolynomial{T, X}(d)
125+
end
126+
122127
## ----
123128
## apply map to factors and the leading coefficient, not the coefficients
124129
function Base.map(fn, p::P, args...) where {T,X,P<:FactoredPolynomial{T,X}}

test/StandardBasis.jl

+10
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,16 @@ end
794794
end
795795
@test convert(FactoredPolynomial, p) p
796796
end
797+
798+
# reinterpret coefficients
799+
for P in (ImmutablePolynomial, Polynomial, SparsePolynomial, LaurentPolynomial, FactoredPolynomial)
800+
for T in (Float64, Rational)
801+
xs = [1,2,3]
802+
p = fromroots(P,xs)
803+
@test Polynomials.copy_with_eltype(T, p) == fromroots(P, T.(xs))
804+
@test Polynomials.copy_with_eltype(T, :u, p) == fromroots(P, T.(xs); var=:u)
805+
end
806+
end
797807
end
798808

799809
@testset "Roots" begin

0 commit comments

Comments
 (0)