Skip to content

Commit 67e8119

Browse files
committed
Move variable check to upgrade
1 parent 7506950 commit 67e8119

File tree

1 file changed

+23
-49
lines changed

1 file changed

+23
-49
lines changed

src/generic/UnivPoly.jl

Lines changed: 23 additions & 49 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+
p.p = upgrade(S, data(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+
p.p = upgrade(S, data(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
@@ -620,15 +616,7 @@ function isless(a::UnivPoly{T}, b::UnivPoly{T}) where {T}
620616
end
621617
S = parent(a)
622618
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)
619+
return isless(upgrade(S, data(s)), upgrade(S, data(t)))
632620
end
633621

634622
###############################################################################
@@ -673,10 +661,8 @@ function deflate(p::UnivPoly{T}, shift::Vector{Int}, defl::Vector{Int}) where {T
673661
if vlen == num
674662
return UnivPoly{T}(deflate(pp, shift, defl), S)
675663
end
676-
if vlen > num
677-
pp = upgrade(S, pp)
678-
num = nvars(parent(pp))
679-
end
664+
pp = upgrade(S, pp)
665+
num = nvars(parent(pp))
680666
if vlen < num
681667
shift = vcat(shift, zeros(Int, num - vlen))
682668
defl = vcat(defl, ones(Int, num - vlen))
@@ -697,10 +683,8 @@ function inflate(p::UnivPoly{T}, shift::Vector{Int}, defl::Vector{Int}) where {T
697683
if vlen == num
698684
return UnivPoly{T}(inflate(pp, shift, defl), S)
699685
end
700-
if vlen > num
701-
pp = upgrade(S, pp)
702-
num = nvars(parent(pp))
703-
end
686+
pp = upgrade(S, pp)
687+
num = nvars(parent(pp))
704688
if vlen < num
705689
shift = vcat(shift, zeros(Int, num - vlen))
706690
defl = vcat(defl, ones(Int, num - vlen))
@@ -958,12 +942,7 @@ end
958942
_change_univ_poly_ring(R, Rx, cached::Bool) = universal_polynomial_ring(R, symbols(Rx); internal_ordering=internal_ordering(Rx), cached)[1]
959943

960944
function change_base_ring(R::Ring, p::UnivPoly{T}; cached::Bool=true, new_parent::UniversalPolyRing = _change_univ_poly_ring(R, parent(p), cached)) where {T <: RingElement}
961-
if nvars(parent(p)) > nvars(parent(data(p)))
962-
pp = upgrade(parent(p), data(p))
963-
else
964-
pp = data(p)
965-
end
966-
return UnivPoly(change_base_ring(R, pp; parent = mpoly_ring(new_parent)), new_parent)
945+
return UnivPoly(change_base_ring(R, upgrade(parent(p), data(p)); parent = mpoly_ring(new_parent)), new_parent)
967946
end
968947

969948
function change_coefficient_ring(R::Ring, p::UnivPoly{T}; cached::Bool=true, parent::UniversalPolyRing = _change_univ_poly_ring(R, parent(p), cached)) where {T <: RingElement}
@@ -977,12 +956,7 @@ end
977956
################################################################################
978957

979958
function map_coefficients(f::T, p::UnivPoly; cached::Bool=true, new_parent::UniversalPolyRing = _change_univ_poly_ring(parent(f(zero(base_ring(p)))), parent(p), cached)) where T
980-
if nvars(parent(p)) > nvars(parent(data(p)))
981-
pp = upgrade(parent(p), data(p))
982-
else
983-
pp = data(p)
984-
end
985-
return UnivPoly(map_coefficients(f, pp; parent = mpoly_ring(new_parent)), new_parent)
959+
return UnivPoly(map_coefficients(f, upgrade(parent(p), data(p)); parent = mpoly_ring(new_parent)), new_parent)
986960
end
987961

988962
###############################################################################
@@ -1171,12 +1145,16 @@ end
11711145

11721146
function upgrade(S::UniversalPolyRing{T}, pp::MPolyRingElem{T}) where {T}
11731147
n = nvars(S) - nvars(parent(pp))
1174-
ctx = MPolyBuildCtx(mpoly_ring(S))
1175-
v0 = zeros(Int, n)
1176-
for (c, v) in zip(coefficients(pp), exponent_vectors(pp))
1177-
push_term!(ctx, c, vcat(v, v0))
1148+
if n > 0
1149+
ctx = MPolyBuildCtx(mpoly_ring(S))
1150+
v0 = zeros(Int, n)
1151+
for (c, v) in zip(coefficients(pp), exponent_vectors(pp))
1152+
push_term!(ctx, c, vcat(v, v0))
1153+
end
1154+
return finish(ctx)
1155+
else
1156+
return pp
11781157
end
1179-
return finish(ctx)
11801158
end
11811159

11821160
function (a::UniversalPolyRing{T})(b::RingElement) where {T <: RingElement}
@@ -1197,11 +1175,7 @@ end
11971175

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

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

0 commit comments

Comments
 (0)