1111# ##############################################################################
1212
1313base_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
1616coefficient_ring_type (T:: Type{<:UniversalPolyRing} ) = base_ring_type (T)
1717coefficient_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)
3535end
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
4343function vars (p:: UnivPoly{T} ) where {T}
4444 S = parent (p)
@@ -246,23 +246,36 @@ length(p::UnivPoly) = length(data(p))
246246
247247function _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
263267end
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
267280function 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
904917end
905918
906919function (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
963976function _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
969981end
0 commit comments