Skip to content

Commit cf410ff

Browse files
committed
Introduce upgrade! for universal polynomials
1 parent f97729d commit cf410ff

File tree

1 file changed

+28
-54
lines changed

1 file changed

+28
-54
lines changed

src/generic/UnivPoly.jl

Lines changed: 28 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ end
6868
function set_exponent_vector!(p::UnivPoly, i::Int, exps::Vector{Int})
6969
S = parent(p)
7070
len = length(exps)
71-
if len != nvars(parent(data(p)))
72-
p.p = upgrade(S, data(p))
73-
if len < nvars(S)
74-
exps = vcat(exps, zeros(Int, nvars(S) - len))
75-
end
71+
upgrade!(p)
72+
if len < nvars(S)
73+
exps = vcat(exps, zeros(Int, nvars(S) - len))
7674
end
7775
p.p = set_exponent_vector!(data(p), i, exps)
7876
return p
@@ -99,11 +97,9 @@ function setcoeff!(p::UnivPoly, exps::Vector{Int}, c::T) where T <: RingElement
9997
c = base_ring(data(p))(c)
10098
S = parent(p)
10199
len = length(exps)
102-
if len != nvars(parent(data(p)))
103-
p.p = upgrade(S, data(p))
104-
if len < nvars(S)
105-
exps = vcat(exps, zeros(Int, nvars(S) - len))
106-
end
100+
upgrade!(p)
101+
if len < nvars(S)
102+
exps = vcat(exps, zeros(Int, nvars(S) - len))
107103
end
108104
p.p = setcoeff!(data(p), exps, c)
109105
return p
@@ -615,20 +611,7 @@ end
615611

616612
function isless(a::UnivPoly{T}, b::UnivPoly{T}) where {T}
617613
check_parent(a, b)
618-
if parent(data(a)) === parent(data(b))
619-
return isless(data(a), data(b))
620-
end
621-
S = parent(a)
622-
num = nvars(S)
623-
s = data(a)
624-
t = data(b)
625-
if nvars(parent(s)) != num
626-
s = upgrade(S, s)
627-
end
628-
if nvars(parent(t)) != num
629-
t = upgrade(S, t)
630-
end
631-
return isless(s, t)
614+
return isless(data(upgrade!(a)), data(upgrade!(b)))
632615
end
633616

634617
###############################################################################
@@ -666,22 +649,15 @@ deflation(p::UnivPoly{T}) where {T} = deflation(data(p))
666649

667650
function deflate(p::UnivPoly{T}, shift::Vector{Int}, defl::Vector{Int}) where {T}
668651
S = parent(p)
652+
num = nvars(S)
669653
vlen = length(shift)
670654
vlen != length(defl) && error("Vector lengths do not match")
671-
num = nvars(parent(data(p)))
672-
pp = data(p)
673-
if vlen == num
674-
return UnivPoly{T}(deflate(pp, shift, defl), S)
675-
end
676-
if vlen > num
677-
pp = upgrade(S, pp)
678-
num = nvars(parent(pp))
679-
end
655+
upgrade!(p)
680656
if vlen < num
681657
shift = vcat(shift, zeros(Int, num - vlen))
682658
defl = vcat(defl, ones(Int, num - vlen))
683659
end
684-
return UnivPoly{T}(deflate(pp, shift, defl), S)
660+
return UnivPoly{T}(deflate(data(p), shift, defl), S)
685661
end
686662

687663
function deflate(p::UnivPoly{T}, defl::Vector{Int}) where {T}
@@ -690,22 +666,15 @@ end
690666

691667
function inflate(p::UnivPoly{T}, shift::Vector{Int}, defl::Vector{Int}) where {T}
692668
S = parent(p)
669+
num = nvars(S)
693670
vlen = length(shift)
694671
vlen != length(defl) && error("Vector lengths do not match")
695-
num = nvars(parent(data(p)))
696-
pp = data(p)
697-
if vlen == num
698-
return UnivPoly{T}(inflate(pp, shift, defl), S)
699-
end
700-
if vlen > num
701-
pp = upgrade(S, pp)
702-
num = nvars(parent(pp))
703-
end
672+
upgrade!(p)
704673
if vlen < num
705674
shift = vcat(shift, zeros(Int, num - vlen))
706675
defl = vcat(defl, ones(Int, num - vlen))
707676
end
708-
return UnivPoly{T}(inflate(pp, shift, defl), S)
677+
return UnivPoly{T}(inflate(data(p), shift, defl), S)
709678
end
710679

711680
function inflate(p::UnivPoly{T}, defl::Vector{Int}) where {T}
@@ -1172,12 +1141,21 @@ end
11721141

11731142
function upgrade(S::UniversalPolyRing{T}, pp::MPolyRingElem{T}) where {T}
11741143
n = nvars(S) - nvars(parent(pp))
1175-
ctx = MPolyBuildCtx(mpoly_ring(S))
1176-
v0 = zeros(Int, n)
1177-
for (c, v) in zip(coefficients(pp), exponent_vectors(pp))
1178-
push_term!(ctx, c, vcat(v, v0))
1144+
if n > 0
1145+
ctx = MPolyBuildCtx(mpoly_ring(S))
1146+
v0 = zeros(Int, n)
1147+
for (c, v) in zip(coefficients(pp), exponent_vectors(pp))
1148+
push_term!(ctx, c, vcat(v, v0))
1149+
end
1150+
return finish(ctx)
1151+
else
1152+
return pp
11791153
end
1180-
return finish(ctx)
1154+
end
1155+
1156+
function upgrade!(p::UnivPoly)
1157+
p.p = upgrade(parent(p), data(p))
1158+
return p
11811159
end
11821160

11831161
function (a::UniversalPolyRing{T})(b::RingElement) where {T <: RingElement}
@@ -1198,11 +1176,7 @@ end
11981176

11991177
function (S::UniversalPolyRing{T})(p::UnivPoly{T}) where {T <: RingElement}
12001178
parent(p) !== S && error("Unable to coerce")
1201-
n = nvars(S) - nvars(parent(data(p)))
1202-
if n != 0
1203-
p = UnivPoly{T}(upgrade(S, data(p)), S)
1204-
end
1205-
return p
1179+
return upgrade!(p)
12061180
end
12071181

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

0 commit comments

Comments
 (0)