6262function 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
611607
612608function 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)))
628611end
629612
630613# ##############################################################################
@@ -662,22 +645,15 @@ deflation(p::UnivPoly{T}) where {T} = deflation(data(p))
662645
663646function 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)
681657end
682658
683659function deflate (p:: UnivPoly{T} , defl:: Vector{Int} ) where {T}
@@ -686,22 +662,15 @@ end
686662
687663function 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)
705674end
706675
707676function inflate (p:: UnivPoly{T} , defl:: Vector{Int} ) where {T}
@@ -1168,12 +1137,21 @@ end
11681137
11691138function 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
11771155end
11781156
11791157function (a:: UniversalPolyRing{T} )(b:: RingElement ) where {T <: RingElement }
@@ -1194,11 +1172,7 @@ end
11941172
11951173function (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)
12021176end
12031177
12041178function (a:: UniversalPolyRing{T} )(b:: Vector{T} , m:: Vector{Vector{Int}} ) where {T <: RingElement }
0 commit comments