Skip to content

Commit e9768d4

Browse files
committed
Change behavior of base_ring for UniversalPolyRing
1 parent 28070b1 commit e9768d4

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

src/generic/UnivPoly.jl

Lines changed: 34 additions & 36 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.mpoly_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) = base_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,23 +30,21 @@ 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
33+
mpoly_ring(S::UniversalPolyRing) = base_ring(S)
3634

37-
number_of_variables(S::UniversalPolyRing) = number_of_variables(mpoly_ring(S))
35+
number_of_variables(S::UniversalPolyRing) = number_of_variables(base_ring(S))
3836

39-
number_of_generators(S::UniversalPolyRing) = number_of_generators(mpoly_ring(S))
37+
number_of_generators(S::UniversalPolyRing) = number_of_generators(base_ring(S))
4038

41-
symbols(S::UniversalPolyRing) = symbols(mpoly_ring(S))
39+
symbols(S::UniversalPolyRing) = symbols(base_ring(S))
4240

4341
function vars(p::UnivPoly{T}) where {T}
4442
S = parent(p)
4543
V = vars(data(p))
4644
return [UnivPoly{T}(v, S) for v in V]
4745
end
4846

49-
internal_ordering(p::UniversalPolyRing) = internal_ordering(mpoly_ring(p))
47+
internal_ordering(p::UniversalPolyRing) = internal_ordering(base_ring(p))
5048

5149
data(p::UnivPoly{T}) where {T<:RingElement} = p.p::mpoly_type(T)
5250

@@ -84,7 +82,7 @@ function coeff(p::UnivPoly, exps::Vector{Int})
8482
n = nvars(parent(data(p)))
8583
if len > n
8684
if !iszero(exps[n + 1:len])
87-
return base_ring(S)()
85+
return coefficient_ring(S)()
8886
end
8987
return coeff(data(p), exps[1:n])
9088
end
@@ -96,7 +94,7 @@ function coeff(p::UnivPoly, exps::Vector{Int})
9694
end
9795

9896
function setcoeff!(p::UnivPoly, exps::Vector{Int}, c::T) where T <: RingElement
99-
c = base_ring(data(p))(c)
97+
c = coefficient_ring(data(p))(c)
10098
S = parent(p)
10199
len = length(exps)
102100
if len != nvars(parent(data(p)))
@@ -120,9 +118,9 @@ end
120118
#
121119
###############################################################################
122120

123-
zero(R::UniversalPolyRing{T}) where {T} = UnivPoly{T}(zero(mpoly_ring(R)), R)
121+
zero(R::UniversalPolyRing{T}) where {T} = UnivPoly{T}(zero(base_ring(R)), R)
124122

125-
one(R::UniversalPolyRing{T}) where {T} = UnivPoly{T}(one(mpoly_ring(R)), R)
123+
one(R::UniversalPolyRing{T}) where {T} = UnivPoly{T}(one(base_ring(R)), R)
126124

127125
iszero(p::UnivPoly) = iszero(data(p))
128126

@@ -155,7 +153,7 @@ function coeff(p::UnivPoly{T}, m::UnivPoly{T}) where {T}
155153
v1 = first(exponent_vectors(m))
156154
len = length(v1)
157155
n = nvars(parent(data(p)))
158-
R = base_ring(p)
156+
R = coefficient_ring(p)
159157
if len > n
160158
if !iszero(v1[n + 1:len])
161159
return zero(R)
@@ -261,7 +259,7 @@ function _ensure_variables(S::UniversalPolyRing, v::Vector{<:VarName})
261259
end
262260
if !isempty(added_symbols)
263261
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)
262+
S.mpoly_ring = AbstractAlgebra.polynomial_ring_only(coefficient_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
265263
end
266264
return idx
267265
end
@@ -272,14 +270,14 @@ function gen(S::UniversalPolyRing, s::VarName)
272270
new_symbols = copy(symbols(S))
273271
push!(new_symbols, Symbol(s))
274272
i = length(new_symbols)
275-
S.mpoly_ring = AbstractAlgebra.polynomial_ring_only(base_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
273+
S.mpoly_ring = AbstractAlgebra.polynomial_ring_only(coefficient_ring(S), new_symbols; internal_ordering=internal_ordering(S), cached=false)
276274
end
277275
return @inbounds gen(S, i)
278276
end
279277

280278
function gen(S::UniversalPolyRing{T}, i::Int) where {T}
281279
@boundscheck 1 <= i <= nvars(S) || throw(ArgumentError("generator index out of range"))
282-
return UnivPoly{T}(gen(mpoly_ring(S), i), S)
280+
return UnivPoly{T}(gen(base_ring(S), i), S)
283281
end
284282

285283
function gens(S::UniversalPolyRing{T}) where {T}
@@ -293,7 +291,7 @@ function _univ_poly_gens(S::UniversalPolyRing{T}, vars::Vector{Symbol}) where {T
293291
idx = _ensure_variables(S, vars)
294292
# TRICK: @varnames_interface expects two return values, but we only care
295293
# for the second; so just return literally nothing for the first
296-
return nothing, [UnivPoly{T}(gen(mpoly_ring(S), i), S) for i in idx]
294+
return nothing, [UnivPoly{T}(gen(base_ring(S), i), S) for i in idx]
297295
end
298296

299297
AbstractAlgebra.@varnames_interface _univ_poly_gens(R::UniversalPolyRing{T}, s) where {T}
@@ -314,7 +312,7 @@ end
314312

315313
canonical_unit(p::UnivPoly) = canonical_unit(data(p))
316314

317-
characteristic(R::UniversalPolyRing) = characteristic(base_ring(R))
315+
characteristic(R::UniversalPolyRing) = characteristic(coefficient_ring(R))
318316

319317
function Base.hash(p::UnivPoly, h::UInt)
320318
b = 0xcf418d4529109236%UInt
@@ -377,7 +375,7 @@ function show(io::IO, R::UniversalPolyRing)
377375
@show_name(io, R)
378376
@show_special(io, R)
379377
print(io, "Universal Polynomial Ring over ")
380-
show(io, base_ring(R))
378+
show(io, coefficient_ring(R))
381379
end
382380

383381
function expressify(a::UnivPoly, x = symbols(parent(a)); context = nothing)
@@ -808,7 +806,7 @@ end
808806
###############################################################################
809807

810808
function evaluate(a::UnivPoly{T}, A::Vector{T}) where {T <: RingElem}
811-
R = base_ring(a)
809+
R = coefficient_ring(a)
812810
n = length(A)
813811
num = nvars(parent(data(a)))
814812
if n > num
@@ -852,7 +850,7 @@ function evaluate(a::UnivPoly{T}, A::Vector{V}) where {T <: RingElement, V <: Ri
852850
end
853851
if n < num
854852
if n == 0
855-
R = base_ring(a)
853+
R = coefficient_ring(a)
856854
return evaluate(data(a), [zero(R) for _ in 1:num])
857855
else
858856
R = parent(A[1])
@@ -963,7 +961,7 @@ is_univariate(p::UnivPoly) = is_univariate(data(p))
963961

964962
is_univariate_with_data(p::UnivPoly) = is_univariate_with_data(data(p))
965963

966-
is_univariate(R::UniversalPolyRing) = is_univariate(mpoly_ring(R))
964+
is_univariate(R::UniversalPolyRing) = is_univariate(base_ring(R))
967965

968966
function coefficients_of_univariate(p::UnivPoly, check_univariate::Bool=true)
969967
return coefficients_of_univariate(data(p), check_univariate)
@@ -978,7 +976,7 @@ end
978976
_change_univ_poly_ring(R, Rx, cached::Bool) = universal_polynomial_ring(R, symbols(Rx); internal_ordering=internal_ordering(Rx), cached)[1]
979977

980978
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}
981-
base_ring(parent) != R && error("Base rings do not match.")
979+
coefficient_ring(parent) != R && error("Base rings do not match.")
982980
return _map(R, p, parent)
983981
end
984982

@@ -992,7 +990,7 @@ end
992990
#
993991
################################################################################
994992

995-
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
993+
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
996994
return _map(f, p, parent)
997995
end
998996

@@ -1032,7 +1030,7 @@ RandomExtensions.maketype(S::AbstractAlgebra.UniversalPolyRing, _, _, _) = elem_
10321030

10331031
function RandomExtensions.make(S::AbstractAlgebra.UniversalPolyRing, term_range::AbstractUnitRange{Int},
10341032
exp_bound::AbstractUnitRange{Int}, vs...)
1035-
R = base_ring(S)
1033+
R = coefficient_ring(S)
10361034
if length(vs) == 1 && elem_type(R) == Random.gentype(vs[1])
10371035
Make(S, term_range, exp_bound, vs[1])
10381036
else
@@ -1045,7 +1043,7 @@ function rand(rng::AbstractRNG, sp::SamplerTrivial{<:Make4{
10451043
S, term_range, exp_bound, v = sp[][1:end]
10461044
f = S()
10471045
g = gens(S)
1048-
R = base_ring(S)
1046+
R = coefficient_ring(S)
10491047
for i = 1:rand(rng, term_range)
10501048
term = S(1)
10511049
for j = 1:length(g)
@@ -1192,7 +1190,7 @@ end
11921190

11931191
function upgrade(S::UniversalPolyRing{T}, pp::MPolyRingElem{T}) where {T}
11941192
n = nvars(S) - nvars(parent(pp))
1195-
ctx = MPolyBuildCtx(mpoly_ring(S))
1193+
ctx = MPolyBuildCtx(base_ring(S))
11961194
v0 = zeros(Int, n)
11971195
for (c, v) in zip(coefficients(pp), exponent_vectors(pp))
11981196
push_term!(ctx, c, vcat(v, v0))
@@ -1201,19 +1199,19 @@ function upgrade(S::UniversalPolyRing{T}, pp::MPolyRingElem{T}) where {T}
12011199
end
12021200

12031201
function (a::UniversalPolyRing{T})(b::RingElement) where {T <: RingElement}
1204-
return a(base_ring(a)(b))
1202+
return a(coefficient_ring(a)(b))
12051203
end
12061204

12071205
function (a::UniversalPolyRing{T})() where {T <: RingElement}
1208-
return UnivPoly{T}(mpoly_ring(a)(), a)
1206+
return UnivPoly{T}(base_ring(a)(), a)
12091207
end
12101208

12111209
function (a::UniversalPolyRing{T})(b::Union{Integer, Rational, AbstractFloat}) where {T <: RingElement}
1212-
return UnivPoly{T}(mpoly_ring(a)(b), a)
1210+
return UnivPoly{T}(base_ring(a)(b), a)
12131211
end
12141212

12151213
function (a::UniversalPolyRing{T})(b::T) where {T <: RingElem}
1216-
return UnivPoly{T}(mpoly_ring(a)(b), a)
1214+
return UnivPoly{T}(base_ring(a)(b), a)
12171215
end
12181216

12191217
function (S::UniversalPolyRing{T})(p::UnivPoly{T}) where {T <: RingElement}
@@ -1228,12 +1226,12 @@ end
12281226
function (a::UniversalPolyRing{T})(b::Vector{T}, m::Vector{Vector{Int}}) where {T <: RingElement}
12291227
if length(m) != 0
12301228
len = length(m[1])
1231-
num = nvars(mpoly_ring(a))
1229+
num = nvars(base_ring(a))
12321230
if len != num
12331231
for i = 1:length(m)
12341232
m[i] = vcat(m[i], zeros(Int, num - len))
12351233
end
12361234
end
12371235
end
1238-
return UnivPoly{T}(mpoly_ring(a)(b, m), a)
1236+
return UnivPoly{T}(base_ring(a)(b, m), a)
12391237
end

test/generic/UnivPoly-test.jl

Lines changed: 5 additions & 5 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

0 commit comments

Comments
 (0)