Skip to content

Commit 31ea1cd

Browse files
Change behavior of base_ring for UniversalPolyRing (#2182)
Co-authored-by: Max Horn <max@quendi.de>
1 parent f37df53 commit 31ea1cd

File tree

3 files changed

+40
-44
lines changed

3 files changed

+40
-44
lines changed

src/generic/GenericTypes.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ end
464464
###############################################################################
465465

466466
@attributes mutable struct UniversalPolyRing{T <: RingElement} <: AbstractAlgebra.UniversalPolyRing{T}
467-
mpoly_ring::AbstractAlgebra.MPolyRing{T}
467+
base_ring::AbstractAlgebra.MPolyRing{T}
468468

469469
function UniversalPolyRing{T}(
470470
R::Ring, s::Vector{Symbol}, internal_ordering::Symbol, cached::Bool=true

src/generic/UnivPoly.jl

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#
1111
###############################################################################
1212

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

16-
coefficient_ring_type(T::Type{<:UniversalPolyRing}) = base_ring_type(T)
17-
coefficient_ring(S::UniversalPolyRing) = base_ring(S)
16+
coefficient_ring_type(::Type{<:UniversalPolyRing{T}}) where T = parent_type(T)
17+
coefficient_ring(S::UniversalPolyRing) = coefficient_ring(base_ring(S))::coefficient_ring_type(S)
1818

1919
function is_domain_type(::Type{<:UnivPoly{S}}) where {S <: RingElement}
2020
return is_domain_type(S)
@@ -30,17 +30,13 @@ elem_type(::Type{UniversalPolyRing{T}}) where {T<:RingElement} = UnivPoly{T}
3030

3131
parent_type(::Type{UnivPoly{T}}) where {T<:RingElement} = UniversalPolyRing{T}
3232

33-
function mpoly_ring(S::UniversalPolyRing{T}) where {T<:RingElement}
34-
return S.mpoly_ring::mpoly_ring_type(T)
35-
end
36-
37-
number_of_variables(S::UniversalPolyRing) = number_of_variables(mpoly_ring(S))
33+
number_of_variables(S::UniversalPolyRing) = number_of_variables(base_ring(S))
3834

39-
number_of_generators(S::UniversalPolyRing) = number_of_generators(mpoly_ring(S))
35+
number_of_generators(S::UniversalPolyRing) = number_of_generators(base_ring(S))
4036

41-
symbols(S::UniversalPolyRing) = symbols(mpoly_ring(S))
37+
symbols(S::UniversalPolyRing) = symbols(base_ring(S))
4238

43-
internal_ordering(p::UniversalPolyRing) = internal_ordering(mpoly_ring(p))
39+
internal_ordering(p::UniversalPolyRing) = internal_ordering(base_ring(p))
4440

4541
data(p::UnivPoly{T}) where {T<:RingElement} = p.p::mpoly_type(T)
4642

@@ -76,7 +72,7 @@ function coeff(p::UnivPoly, exps::Vector{Int})
7672
n = nvars(parent(data(p)))
7773
if len > n
7874
if !iszero(exps[n + 1:len])
79-
return base_ring(S)()
75+
return coefficient_ring(S)()
8076
end
8177
return coeff(data(p), exps[1:n])
8278
end
@@ -88,7 +84,7 @@ function coeff(p::UnivPoly, exps::Vector{Int})
8884
end
8985

9086
function setcoeff!(p::UnivPoly, exps::Vector{Int}, c::T) where T <: RingElement
91-
c = base_ring(data(p))(c)
87+
c = coefficient_ring(data(p))(c)
9288
S = parent(p)
9389
len = length(exps)
9490
upgrade!(p)
@@ -110,9 +106,9 @@ end
110106
#
111107
###############################################################################
112108

113-
zero(R::UniversalPolyRing{T}) where {T} = UnivPoly{T}(zero(mpoly_ring(R)), R)
109+
zero(R::UniversalPolyRing{T}) where {T} = UnivPoly{T}(zero(base_ring(R)), R)
114110

115-
one(R::UniversalPolyRing{T}) where {T} = UnivPoly{T}(one(mpoly_ring(R)), R)
111+
one(R::UniversalPolyRing{T}) where {T} = UnivPoly{T}(one(base_ring(R)), R)
116112

117113
iszero(p::UnivPoly) = iszero(data(p))
118114

@@ -231,7 +227,7 @@ function _ensure_variables(S::UniversalPolyRing, v::Vector{<:VarName})
231227
end
232228
if !isempty(added_symbols)
233229
new_symbols = vcat(current_symbols, added_symbols)
234-
S.mpoly_ring = AbstractAlgebra.polynomial_ring_only(base_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
230+
S.base_ring = AbstractAlgebra.polynomial_ring_only(coefficient_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
235231
end
236232
return idx
237233
end
@@ -242,14 +238,14 @@ function gen(S::UniversalPolyRing, s::VarName)
242238
new_symbols = copy(symbols(S))
243239
push!(new_symbols, Symbol(s))
244240
i = length(new_symbols)
245-
S.mpoly_ring = AbstractAlgebra.polynomial_ring_only(base_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
241+
S.base_ring = AbstractAlgebra.polynomial_ring_only(coefficient_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
246242
end
247243
return @inbounds gen(S, i)
248244
end
249245

250246
function gen(S::UniversalPolyRing{T}, i::Int) where {T}
251247
@boundscheck 1 <= i <= nvars(S) || throw(ArgumentError("generator index out of range"))
252-
return UnivPoly{T}(gen(mpoly_ring(S), i), S)
248+
return UnivPoly{T}(gen(base_ring(S), i), S)
253249
end
254250

255251
function gens(S::UniversalPolyRing{T}) where {T}
@@ -263,7 +259,7 @@ function _univ_poly_gens(S::UniversalPolyRing{T}, vars::Vector{Symbol}) where {T
263259
idx = _ensure_variables(S, vars)
264260
# TRICK: @varnames_interface expects two return values, but we only care
265261
# for the second; so just return literally nothing for the first
266-
return nothing, [UnivPoly{T}(gen(mpoly_ring(S), i), S) for i in idx]
262+
return nothing, [UnivPoly{T}(gen(base_ring(S), i), S) for i in idx]
267263
end
268264

269265
AbstractAlgebra.@varnames_interface _univ_poly_gens(R::UniversalPolyRing{T}, s) where {T}
@@ -286,7 +282,7 @@ end
286282

287283
canonical_unit(p::UnivPoly) = canonical_unit(data(p))
288284

289-
characteristic(R::UniversalPolyRing) = characteristic(base_ring(R))
285+
characteristic(R::UniversalPolyRing) = characteristic(coefficient_ring(R))
290286

291287
function Base.hash(p::UnivPoly, h::UInt)
292288
b = 0xcf418d4529109236%UInt
@@ -349,7 +345,7 @@ function show(io::IO, R::UniversalPolyRing)
349345
@show_name(io, R)
350346
@show_special(io, R)
351347
print(io, "Universal Polynomial Ring over ")
352-
show(io, base_ring(R))
348+
show(io, coefficient_ring(R))
353349
end
354350

355351
function expressify(a::UnivPoly, x = symbols(parent(a)); context = nothing)
@@ -828,7 +824,7 @@ is_univariate(p::UnivPoly) = is_univariate(data(p))
828824

829825
is_univariate_with_data(p::UnivPoly) = is_univariate_with_data(data(p))
830826

831-
is_univariate(R::UniversalPolyRing) = is_univariate(mpoly_ring(R))
827+
is_univariate(R::UniversalPolyRing) = is_univariate(base_ring(R))
832828

833829
function coefficients_of_univariate(p::UnivPoly, check_univariate::Bool=true)
834830
return coefficients_of_univariate(data(p), check_univariate)
@@ -844,7 +840,7 @@ _change_univ_poly_ring(R, Rx, cached::Bool) = universal_polynomial_ring(R, symbo
844840

845841
function change_base_ring(R::Ring, p::UnivPoly{T}; cached::Bool=true, parent::UniversalPolyRing = _change_univ_poly_ring(R, parent(p), cached)) where {T <: RingElement}
846842
upgrade!(p)
847-
return UnivPoly(change_base_ring(R, data(p); parent = mpoly_ring(parent)), parent)
843+
return UnivPoly(change_base_ring(R, data(p); parent = base_ring(parent)), parent)
848844
end
849845

850846
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}
@@ -857,9 +853,9 @@ end
857853
#
858854
################################################################################
859855

860-
function map_coefficients(f::T, p::UnivPoly; cached::Bool=true, parent::UniversalPolyRing = _change_univ_poly_ring(parent(f(zero(base_ring(p)))), parent(p), cached)) where T
856+
function map_coefficients(f::T, p::UnivPoly; cached::Bool=true, parent::UniversalPolyRing = _change_univ_poly_ring(parent(f(zero(coefficient_ring(p)))), parent(p), cached)) where T
861857
upgrade!(p)
862-
return UnivPoly(map_coefficients(f, data(p); parent = mpoly_ring(parent)), parent)
858+
return UnivPoly(map_coefficients(f, data(p); parent = base_ring(parent)), parent)
863859
end
864860

865861
###############################################################################
@@ -888,7 +884,7 @@ RandomExtensions.maketype(S::AbstractAlgebra.UniversalPolyRing, _, _, _) = elem_
888884

889885
function RandomExtensions.make(S::AbstractAlgebra.UniversalPolyRing, term_range::AbstractUnitRange{Int},
890886
exp_bound::AbstractUnitRange{Int}, vs...)
891-
R = base_ring(S)
887+
R = coefficient_ring(S)
892888
if length(vs) == 1 && elem_type(R) == Random.gentype(vs[1])
893889
Make(S, term_range, exp_bound, vs[1])
894890
else
@@ -901,7 +897,7 @@ function rand(rng::AbstractRNG, sp::SamplerTrivial{<:Make4{
901897
S, term_range, exp_bound, v = sp[][1:end]
902898
f = S()
903899
g = gens(S)
904-
R = base_ring(S)
900+
R = coefficient_ring(S)
905901
for i = 1:rand(rng, term_range)
906902
term = S(1)
907903
for j = 1:length(g)
@@ -1049,7 +1045,7 @@ end
10491045
function upgrade(S::UniversalPolyRing{T}, pp::MPolyRingElem{T}) where {T}
10501046
n = nvars(S) - nvars(parent(pp))
10511047
if n > 0
1052-
ctx = MPolyBuildCtx(mpoly_ring(S))
1048+
ctx = MPolyBuildCtx(base_ring(S))
10531049
v0 = zeros(Int, n)
10541050
for (c, v) in zip(coefficients(pp), exponent_vectors(pp))
10551051
push_term!(ctx, c, vcat(v, v0))
@@ -1066,19 +1062,19 @@ function upgrade!(p::UnivPoly)
10661062
end
10671063

10681064
function (a::UniversalPolyRing{T})(b::RingElement) where {T <: RingElement}
1069-
return a(base_ring(a)(b))
1065+
return a(coefficient_ring(a)(b))
10701066
end
10711067

10721068
function (a::UniversalPolyRing{T})() where {T <: RingElement}
1073-
return UnivPoly{T}(mpoly_ring(a)(), a)
1069+
return UnivPoly{T}(base_ring(a)(), a)
10741070
end
10751071

10761072
function (a::UniversalPolyRing{T})(b::JuliaRingElement) where {T <: RingElement}
1077-
return UnivPoly{T}(mpoly_ring(a)(b), a)
1073+
return UnivPoly{T}(base_ring(a)(b), a)
10781074
end
10791075

10801076
function (a::UniversalPolyRing{T})(b::T) where {T <: RingElem}
1081-
return UnivPoly{T}(mpoly_ring(a)(b), a)
1077+
return UnivPoly{T}(base_ring(a)(b), a)
10821078
end
10831079

10841080
function (S::UniversalPolyRing{T})(p::UnivPoly{T}) where {T <: RingElement}
@@ -1089,12 +1085,12 @@ end
10891085
function (a::UniversalPolyRing{T})(b::Vector{T}, m::Vector{Vector{Int}}) where {T <: RingElement}
10901086
if length(m) != 0
10911087
len = length(m[1])
1092-
num = nvars(mpoly_ring(a))
1088+
num = nvars(base_ring(a))
10931089
if len != num
10941090
for i = 1:length(m)
10951091
m[i] = vcat(m[i], zeros(Int, num - len))
10961092
end
10971093
end
10981094
end
1099-
return UnivPoly{T}(mpoly_ring(a)(b, m), a)
1095+
return UnivPoly{T}(base_ring(a)(b, m), a)
11001096
end

test/generic/UnivPoly-test.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
@test elem_type(Generic.UniversalPolyRing{elem_type(R)}) == Generic.UnivPoly{elem_type(R)}
3030
@test parent_type(Generic.UnivPoly{elem_type(R)}) == Generic.UniversalPolyRing{elem_type(R)}
3131

32-
@test base_ring(S) === R
32+
@test coefficient_ring(S) === R
3333
@test coefficient_ring(S) === R
3434
@test coefficient_ring_type(S) === typeof(R)
3535

@@ -137,10 +137,10 @@ end
137137
@test parent(f2) === S
138138
@test parent(f3) === S
139139

140-
@test base_ring(S) === R
141-
@test base_ring(f1) === R
142-
@test base_ring(f2) === R
143-
@test base_ring(f3) === R
140+
@test coefficient_ring(S) === R
141+
@test coefficient_ring(f1) === R
142+
@test coefficient_ring(f2) === R
143+
@test coefficient_ring(f3) === R
144144

145145
@test nvars(S) == 3
146146

@@ -1119,9 +1119,9 @@ end
11191119
@test length(g) == length(g1)
11201120
@test length(h) == length(h1)
11211121

1122-
@test base_ring(f1) === U
1123-
@test base_ring(g1) === U
1124-
@test base_ring(h1) === U
1122+
@test coefficient_ring(f1) === U
1123+
@test coefficient_ring(g1) === U
1124+
@test coefficient_ring(h1) === U
11251125

11261126
f2 = map_coefficients(x->x^2, f)
11271127
g2 = map_coefficients(x->x^2, g)

0 commit comments

Comments
 (0)