Skip to content
Draft
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
34 changes: 34 additions & 0 deletions src/MPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,40 @@ function _map(g::T, p::MPolyRingElem, Rx) where {T}
return finish(M)
end

###############################################################################
#
# Universal polynomial ring methods
#
###############################################################################

@doc raw"""
_upgrade(p::MPolyRingElem{T}, R::MPolyRing{T}) where {T}

Return an element of `R` which is obtained from `p` by mapping the $i$-th variable
of `parent(p)` to the $i$-th variable of `R`.
For this to work, `R` needs to have at least as many variables as `parent(p)`.
"""
function _upgrade(p::MPolyRingElem{T}, R::MPolyRing{T}) where {T}
n = nvars(R) - nvars(parent(p))
n < 0 && error("Too few variables")
ctx = MPolyBuildCtx(R)
v0 = zeros(Int, n)
for (c, v) in zip(coefficients(p), exponent_vectors(p))
push_term!(ctx, c, vcat(v, v0))
end
return finish(ctx)
end

@doc raw"""
_add_gens(R::MPolyRing, varnames::Vector{Symbol})

Return a new uncached multivariate polynomial ring which has the same properties
as `R` but `varnames` as additional generators.
"""
function _add_gens(R::MPolyRing, varnames::Vector{Symbol})
return polynomial_ring_only(base_ring(R), vcat(symbols(R), varnames); internal_ordering=internal_ordering(R), cached=false)
end

###############################################################################
#
# Factorization
Expand Down
28 changes: 7 additions & 21 deletions src/generic/UnivPoly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,19 +226,16 @@ function _ensure_variables(S::UniversalPolyRing, v::Vector{<:VarName})
end
end
if !isempty(added_symbols)
new_symbols = vcat(current_symbols, added_symbols)
S.base_ring = AbstractAlgebra.polynomial_ring_only(coefficient_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
S.base_ring = AbstractAlgebra._add_gens(base_ring(S), added_symbols)
end
return idx
end

function gen(S::UniversalPolyRing, s::VarName)
i = findfirst(==(Symbol(s)), symbols(S))
if i === nothing
new_symbols = copy(symbols(S))
push!(new_symbols, Symbol(s))
i = length(new_symbols)
S.base_ring = AbstractAlgebra.polynomial_ring_only(coefficient_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
S.base_ring = AbstractAlgebra._add_gens(base_ring(S), [Symbol(s)])
i = length(symbols(S))
end
return @inbounds gen(S, i)
end
Expand Down Expand Up @@ -1042,22 +1039,11 @@ end
#
###############################################################################

function upgrade(S::UniversalPolyRing{T}, pp::MPolyRingElem{T}) where {T}
n = nvars(S) - nvars(parent(pp))
if n > 0
ctx = MPolyBuildCtx(base_ring(S))
v0 = zeros(Int, n)
for (c, v) in zip(coefficients(pp), exponent_vectors(pp))
push_term!(ctx, c, vcat(v, v0))
end
return finish(ctx)
else
return pp
end
end

function upgrade!(p::UnivPoly)
p.p = upgrade(parent(p), data(p))
R = base_ring(parent(p))
if R != parent(p.p)
p.p = AbstractAlgebra._upgrade(p.p, R)
end
return p
end

Expand Down
Loading