Skip to content

Commit 9c494d1

Browse files
authored
Remove redundancy in UniversalPolyRing (#2176)
1 parent 9679054 commit 9c494d1

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

src/generic/GenericTypes.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,6 @@ end
417417
###############################################################################
418418

419419
@attributes mutable struct UniversalPolyRing{T <: RingElement} <: AbstractAlgebra.UniversalPolyRing{T}
420-
base_ring::Ring
421-
S::Vector{Symbol}
422-
ord::Symbol
423420
mpoly_ring::AbstractAlgebra.MPolyRing{T}
424421

425422
function UniversalPolyRing{T}(
@@ -430,9 +427,6 @@ end
430427
UnivPolyID, (R, s, internal_ordering), cached
431428
) do
432429
new{T}(
433-
R,
434-
s,
435-
internal_ordering,
436430
AbstractAlgebra.polynomial_ring_only(R, s; internal_ordering, cached=false)
437431
)
438432
end::UniversalPolyRing{T}

src/generic/UnivPoly.jl

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
###############################################################################
1212

1313
base_ring_type(::Type{<:UniversalPolyRing{T}}) where T = parent_type(T)
14-
base_ring(S::UniversalPolyRing) = S.base_ring::base_ring_type(S)
14+
base_ring(S::UniversalPolyRing) = base_ring(mpoly_ring(S))::base_ring_type(S)
1515

1616
coefficient_ring_type(T::Type{<:UniversalPolyRing}) = base_ring_type(T)
1717
coefficient_ring(S::UniversalPolyRing) = base_ring(S)
@@ -34,11 +34,11 @@ function mpoly_ring(S::UniversalPolyRing{T}) where {T<:RingElement}
3434
return S.mpoly_ring::mpoly_ring_type(T)
3535
end
3636

37-
number_of_variables(S::UniversalPolyRing) = length(S.S)
37+
number_of_variables(S::UniversalPolyRing) = number_of_variables(mpoly_ring(S))
3838

39-
number_of_generators(S::UniversalPolyRing) = length(S.S)
39+
number_of_generators(S::UniversalPolyRing) = number_of_generators(mpoly_ring(S))
4040

41-
symbols(S::UniversalPolyRing) = S.S
41+
symbols(S::UniversalPolyRing) = symbols(mpoly_ring(S))
4242

4343
function vars(p::UnivPoly{T}) where {T}
4444
S = parent(p)
@@ -246,23 +246,36 @@ length(p::UnivPoly) = length(data(p))
246246

247247
function _ensure_variables(S::UniversalPolyRing, v::Vector{<:VarName})
248248
idx = Int[]
249+
current_symbols = symbols(S)
250+
n = length(current_symbols)
251+
added_symbols = Symbol[]
249252
for s_ in v
250253
s = Symbol(s_)
251-
i = findfirst(==(s), S.S)
254+
i = findfirst(==(s), current_symbols)
252255
if i === nothing
253-
push!(S.S, s)
254-
push!(idx, length(S.S))
256+
push!(added_symbols, s)
257+
push!(idx, n+length(added_symbols))
255258
else
256259
push!(idx, i)
257260
end
258261
end
259-
if length(S.S) > ngens(S.mpoly_ring)
260-
S.mpoly_ring = AbstractAlgebra.polynomial_ring_only(base_ring(S), copy(S.S); internal_ordering=S.ord, cached=false)
262+
if !isempty(added_symbols)
263+
new_symbols = vcat(current_symbols, added_symbols)
264+
S.mpoly_ring = AbstractAlgebra.polynomial_ring_only(base_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
261265
end
262266
return idx
263267
end
264268

265-
gen(S::UniversalPolyRing, s::VarName) = gens(S, [s])[1]
269+
function gen(S::UniversalPolyRing, s::VarName)
270+
i = findfirst(==(Symbol(s)), symbols(S))
271+
if i === nothing
272+
new_symbols = copy(symbols(S))
273+
push!(new_symbols, Symbol(s))
274+
i = length(new_symbols)
275+
S.mpoly_ring = AbstractAlgebra.polynomial_ring_only(base_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
276+
end
277+
return @inbounds gen(S, i)
278+
end
266279

267280
function gen(S::UniversalPolyRing{T}, i::Int) where {T}
268281
@boundscheck 1 <= i <= nvars(S) || throw(ArgumentError("generator index out of range"))
@@ -904,11 +917,11 @@ function evaluate(a::S, vars::Vector{S}, vals::Vector{V}) where {S <: UnivPoly{T
904917
end
905918

906919
function (a::Union{MPolyRingElem, UniversalPolyRingElem})(;kwargs...)
907-
S = parent(a)
920+
ss = symbols(parent(a))
908921
vars = Array{Int}(undef, length(kwargs))
909922
vals = Array{RingElement}(undef, length(kwargs))
910923
for (i, (var, val)) in enumerate(kwargs)
911-
vari = findfirst(isequal(var), S.S)
924+
vari = findfirst(isequal(var), ss)
912925
vari === nothing && error("Given polynomial has no variable $var")
913926
vars[i] = vari
914927
vals[i] = val
@@ -963,7 +976,6 @@ end
963976
function _change_univ_poly_ring(R, Rx, cached::Bool)
964977
P = AbstractAlgebra.polynomial_ring_only(R, symbols(Rx); internal_ordering=internal_ordering(Rx), cached)
965978
S = universal_polynomial_ring(R; internal_ordering=internal_ordering(Rx), cached)
966-
S.S = deepcopy(symbols(Rx))
967979
S.mpoly_ring = P
968980
return S
969981
end

0 commit comments

Comments
 (0)