Skip to content

Commit cbd60b4

Browse files
committed
Add _upgrade and _add_gens
1 parent 8042af2 commit cbd60b4

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

src/MPoly.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,6 +1367,40 @@ function _map(g::T, p::MPolyRingElem, Rx) where {T}
13671367
return finish(M)
13681368
end
13691369

1370+
###############################################################################
1371+
#
1372+
# Universal polynomial ring methods
1373+
#
1374+
###############################################################################
1375+
1376+
@doc raw"""
1377+
_upgrade(p::MPolyRingElem{T}, R::MPolyRing{T}) where {T}
1378+
1379+
Return an element of `R` which is obtained from `p` by mapping the $i$-th variable
1380+
of `parent(p)` to the $i$-th variable of `R`.
1381+
For this to work, `R` needs to have at least as many variables as `parent(p)`.
1382+
"""
1383+
function _upgrade(p::MPolyRingElem{T}, R::MPolyRing{T}) where {T}
1384+
n = nvars(R) - nvars(parent(p))
1385+
n < 0 && error("Too few variables")
1386+
ctx = MPolyBuildCtx(R)
1387+
v0 = zeros(Int, n)
1388+
for (c, v) in zip(coefficients(p), exponent_vectors(p))
1389+
push_term!(ctx, c, vcat(v, v0))
1390+
end
1391+
return finish(ctx)
1392+
end
1393+
1394+
@doc raw"""
1395+
_add_gens(R::MPolyRing, varnames::Vector{Symbol})
1396+
1397+
Return a new uncached multivariate polynomial ring which has the same properties
1398+
as `R` but `varnames` as additional generators.
1399+
"""
1400+
function _add_gens(R::MPolyRing, varnames::Vector{Symbol})
1401+
return polynomial_ring_only(base_ring(R), vcat(symbols(R), varnames); internal_ordering=internal_ordering(R), cached=false)
1402+
end
1403+
13701404
###############################################################################
13711405
#
13721406
# Factorization

src/generic/UnivPoly.jl

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function set_exponent_vector!(p::UnivPoly, i::Int, exps::Vector{Int})
6565
S = parent(p)
6666
len = length(exps)
6767
if len != nvars(parent(data(p)))
68-
p.p = upgrade(S, data(p))
68+
p.p = AbstractAlgebra._upgrade(data(p), base_ring(S))
6969
if len < nvars(S)
7070
exps = vcat(exps, zeros(Int, nvars(S) - len))
7171
end
@@ -96,7 +96,7 @@ function setcoeff!(p::UnivPoly, exps::Vector{Int}, c::T) where T <: RingElement
9696
S = parent(p)
9797
len = length(exps)
9898
if len != nvars(parent(data(p)))
99-
p.p = upgrade(S, data(p))
99+
p.p = AbstractAlgebra._upgrade(data(p), base_ring(S))
100100
if len < nvars(S)
101101
exps = vcat(exps, zeros(Int, nvars(S) - len))
102102
end
@@ -256,19 +256,16 @@ function _ensure_variables(S::UniversalPolyRing, v::Vector{<:VarName})
256256
end
257257
end
258258
if !isempty(added_symbols)
259-
new_symbols = vcat(current_symbols, added_symbols)
260-
S.base_ring = AbstractAlgebra.polynomial_ring_only(coefficient_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
259+
S.base_ring = AbstractAlgebra.AbstractAlgebra._add_gens(base_ring(S), added_symbols)
261260
end
262261
return idx
263262
end
264263

265264
function gen(S::UniversalPolyRing, s::VarName)
266265
i = findfirst(==(Symbol(s)), symbols(S))
267266
if i === nothing
268-
new_symbols = copy(symbols(S))
269-
push!(new_symbols, Symbol(s))
270-
i = length(new_symbols)
271-
S.base_ring = AbstractAlgebra.polynomial_ring_only(coefficient_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
267+
S.base_ring = AbstractAlgebra.AbstractAlgebra._add_gens(base_ring(S), [Symbol(s)])
268+
i = length(symbols(S))
272269
end
273270
return @inbounds gen(S, i)
274271
end
@@ -639,10 +636,10 @@ function isless(a::UnivPoly{T}, b::UnivPoly{T}) where {T}
639636
s = data(a)
640637
t = data(b)
641638
if nvars(parent(s)) != num
642-
s = upgrade(S, s)
639+
s = AbstractAlgebra._upgrade(s, base_ring(S))
643640
end
644641
if nvars(parent(t)) != num
645-
t = upgrade(S, t)
642+
t = AbstractAlgebra._upgrade(t, base_ring(S))
646643
end
647644
return isless(s, t)
648645
end
@@ -690,7 +687,7 @@ function deflate(p::UnivPoly{T}, shift::Vector{Int}, defl::Vector{Int}) where {T
690687
return UnivPoly{T}(deflate(pp, shift, defl), S)
691688
end
692689
if vlen > num
693-
pp = upgrade(S, pp)
690+
pp = AbstractAlgebra._upgrade(pp, base_ring(S))
694691
num = nvars(parent(pp))
695692
end
696693
if vlen < num
@@ -714,7 +711,7 @@ function inflate(p::UnivPoly{T}, shift::Vector{Int}, defl::Vector{Int}) where {T
714711
return UnivPoly{T}(inflate(pp, shift, defl), S)
715712
end
716713
if vlen > num
717-
pp = upgrade(S, pp)
714+
pp = AbstractAlgebra._upgrade(pp, base_ring(S))
718715
num = nvars(parent(pp))
719716
end
720717
if vlen < num
@@ -1186,16 +1183,6 @@ end
11861183
#
11871184
###############################################################################
11881185

1189-
function upgrade(S::UniversalPolyRing{T}, pp::MPolyRingElem{T}) where {T}
1190-
n = nvars(S) - nvars(parent(pp))
1191-
ctx = MPolyBuildCtx(base_ring(S))
1192-
v0 = zeros(Int, n)
1193-
for (c, v) in zip(coefficients(pp), exponent_vectors(pp))
1194-
push_term!(ctx, c, vcat(v, v0))
1195-
end
1196-
return finish(ctx)
1197-
end
1198-
11991186
function (a::UniversalPolyRing{T})(b::RingElement) where {T <: RingElement}
12001187
return a(coefficient_ring(a)(b))
12011188
end
@@ -1216,7 +1203,7 @@ function (S::UniversalPolyRing{T})(p::UnivPoly{T}) where {T <: RingElement}
12161203
parent(p) !== S && error("Unable to coerce")
12171204
n = nvars(S) - nvars(parent(data(p)))
12181205
if n != 0
1219-
p = UnivPoly{T}(upgrade(S, data(p)), S)
1206+
p = UnivPoly{T}(AbstractAlgebra._upgrade(data(p), base_ring(S)), S)
12201207
end
12211208
return p
12221209
end

0 commit comments

Comments
 (0)