Skip to content

Commit a68dfd3

Browse files
authored
Introduce upgrade! for universal polynomials (#2211)
... and remove a broken test (it only passed "by accident" before)
1 parent 721bf21 commit a68dfd3

File tree

2 files changed

+28
-55
lines changed

2 files changed

+28
-55
lines changed

src/generic/UnivPoly.jl

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ end
6262
function set_exponent_vector!(p::UnivPoly, i::Int, exps::Vector{Int})
6363
S = parent(p)
6464
len = length(exps)
65-
if len != nvars(parent(data(p)))
66-
p.p = upgrade(S, data(p))
67-
if len < nvars(S)
68-
exps = vcat(exps, zeros(Int, nvars(S) - len))
69-
end
65+
upgrade!(p)
66+
if len < nvars(S)
67+
exps = vcat(exps, zeros(Int, nvars(S) - len))
7068
end
7169
p.p = set_exponent_vector!(data(p), i, exps)
7270
return p
@@ -93,11 +91,9 @@ function setcoeff!(p::UnivPoly, exps::Vector{Int}, c::T) where T <: RingElement
9391
c = base_ring(data(p))(c)
9492
S = parent(p)
9593
len = length(exps)
96-
if len != nvars(parent(data(p)))
97-
p.p = upgrade(S, data(p))
98-
if len < nvars(S)
99-
exps = vcat(exps, zeros(Int, nvars(S) - len))
100-
end
94+
upgrade!(p)
95+
if len < nvars(S)
96+
exps = vcat(exps, zeros(Int, nvars(S) - len))
10197
end
10298
p.p = setcoeff!(data(p), exps, c)
10399
return p
@@ -611,20 +607,7 @@ end
611607

612608
function isless(a::UnivPoly{T}, b::UnivPoly{T}) where {T}
613609
check_parent(a, b)
614-
if parent(data(a)) === parent(data(b))
615-
return isless(data(a), data(b))
616-
end
617-
S = parent(a)
618-
num = nvars(S)
619-
s = data(a)
620-
t = data(b)
621-
if nvars(parent(s)) != num
622-
s = upgrade(S, s)
623-
end
624-
if nvars(parent(t)) != num
625-
t = upgrade(S, t)
626-
end
627-
return isless(s, t)
610+
return isless(data(upgrade!(a)), data(upgrade!(b)))
628611
end
629612

630613
###############################################################################
@@ -662,22 +645,15 @@ deflation(p::UnivPoly{T}) where {T} = deflation(data(p))
662645

663646
function deflate(p::UnivPoly{T}, shift::Vector{Int}, defl::Vector{Int}) where {T}
664647
S = parent(p)
648+
num = nvars(S)
665649
vlen = length(shift)
666650
vlen != length(defl) && error("Vector lengths do not match")
667-
num = nvars(parent(data(p)))
668-
pp = data(p)
669-
if vlen == num
670-
return UnivPoly{T}(deflate(pp, shift, defl), S)
671-
end
672-
if vlen > num
673-
pp = upgrade(S, pp)
674-
num = nvars(parent(pp))
675-
end
651+
upgrade!(p)
676652
if vlen < num
677653
shift = vcat(shift, zeros(Int, num - vlen))
678654
defl = vcat(defl, ones(Int, num - vlen))
679655
end
680-
return UnivPoly{T}(deflate(pp, shift, defl), S)
656+
return UnivPoly{T}(deflate(data(p), shift, defl), S)
681657
end
682658

683659
function deflate(p::UnivPoly{T}, defl::Vector{Int}) where {T}
@@ -686,22 +662,15 @@ end
686662

687663
function inflate(p::UnivPoly{T}, shift::Vector{Int}, defl::Vector{Int}) where {T}
688664
S = parent(p)
665+
num = nvars(S)
689666
vlen = length(shift)
690667
vlen != length(defl) && error("Vector lengths do not match")
691-
num = nvars(parent(data(p)))
692-
pp = data(p)
693-
if vlen == num
694-
return UnivPoly{T}(inflate(pp, shift, defl), S)
695-
end
696-
if vlen > num
697-
pp = upgrade(S, pp)
698-
num = nvars(parent(pp))
699-
end
668+
upgrade!(p)
700669
if vlen < num
701670
shift = vcat(shift, zeros(Int, num - vlen))
702671
defl = vcat(defl, ones(Int, num - vlen))
703672
end
704-
return UnivPoly{T}(inflate(pp, shift, defl), S)
673+
return UnivPoly{T}(inflate(data(p), shift, defl), S)
705674
end
706675

707676
function inflate(p::UnivPoly{T}, defl::Vector{Int}) where {T}
@@ -1168,12 +1137,21 @@ end
11681137

11691138
function upgrade(S::UniversalPolyRing{T}, pp::MPolyRingElem{T}) where {T}
11701139
n = nvars(S) - nvars(parent(pp))
1171-
ctx = MPolyBuildCtx(mpoly_ring(S))
1172-
v0 = zeros(Int, n)
1173-
for (c, v) in zip(coefficients(pp), exponent_vectors(pp))
1174-
push_term!(ctx, c, vcat(v, v0))
1140+
if n > 0
1141+
ctx = MPolyBuildCtx(mpoly_ring(S))
1142+
v0 = zeros(Int, n)
1143+
for (c, v) in zip(coefficients(pp), exponent_vectors(pp))
1144+
push_term!(ctx, c, vcat(v, v0))
1145+
end
1146+
return finish(ctx)
1147+
else
1148+
return pp
11751149
end
1176-
return finish(ctx)
1150+
end
1151+
1152+
function upgrade!(p::UnivPoly)
1153+
p.p = upgrade(parent(p), data(p))
1154+
return p
11771155
end
11781156

11791157
function (a::UniversalPolyRing{T})(b::RingElement) where {T <: RingElement}
@@ -1194,11 +1172,7 @@ end
11941172

11951173
function (S::UniversalPolyRing{T})(p::UnivPoly{T}) where {T <: RingElement}
11961174
parent(p) !== S && error("Unable to coerce")
1197-
n = nvars(S) - nvars(parent(data(p)))
1198-
if n != 0
1199-
p = UnivPoly{T}(upgrade(S, data(p)), S)
1200-
end
1201-
return p
1175+
return upgrade!(p)
12021176
end
12031177

12041178
function (a::UniversalPolyRing{T})(b::Vector{T}, m::Vector{Vector{Int}}) where {T <: RingElement}

test/generic/UnivPoly-test.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,6 @@ end
993993

994994
@test evaluate(f, [1], [V[1]]) == evaluate(f, [1], [R(V[1])])
995995
@test evaluate(f, [1], [V[1]]) == evaluate(f, [1], [ZZ(V[1])])
996-
@test evaluate(f, [1], [V[1]]) == evaluate(f, [1], [U(V[1])])
997996
@test evaluate(f, [1], [V[1]]) == f(x=V[1])
998997
@test evaluate(f, [1, 3], [V[1], V[2]]) == evaluate(f, [1, 3], [R(v) for v in V[1:2]])
999998
@test evaluate(f, [1, 3], [V[1], V[2]]) == evaluate(f, [1, 3], [ZZ(v) for v in V[1:2]])

0 commit comments

Comments
 (0)