-
Notifications
You must be signed in to change notification settings - Fork 63
/
NCPoly.jl
757 lines (646 loc) · 22.2 KB
/
NCPoly.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
###############################################################################
#
# NCPoly.jl : polynomials over noncommutative rings
#
###############################################################################
###############################################################################
#
# Data type and parent object methods
#
###############################################################################
base_ring_type(::Type{NCPolyRing{T}}) where T <: NCRingElem = parent_type(T)
base_ring(R::NCPolyRing{T}) where T <: NCRingElem = R.base_ring::parent_type(T)
coefficient_ring(R::NCPolyRing) = base_ring(R)
function is_exact_type(a::Type{T}) where {S <: NCRingElem, T <: NCPolyRingElem{S}}
return is_exact_type(S)
end
@doc raw"""
dense_poly_type(::Type{T}) where T<:NCRingElement
dense_poly_type(::T) where T<:NCRingElement
dense_poly_type(::Type{S}) where S<:NCRing
dense_poly_type(::S) where S<:NCRing
The type of univariate polynomials with coefficients of type `T` respectively `elem_type(S)`.
Falls back to `Generic.NCPoly{T}` respectively `Generic.Poly{T}`.
See also [`dense_poly_ring_type`](@ref), [`mpoly_type`](@ref) and [`mpoly_ring_type`](@ref).
# Examples
```jldoctest; setup = :(using AbstractAlgebra)
julia> dense_poly_type(AbstractAlgebra.ZZ(1))
AbstractAlgebra.Generic.Poly{BigInt}
julia> dense_poly_type(elem_type(AbstractAlgebra.ZZ))
AbstractAlgebra.Generic.Poly{BigInt}
julia> dense_poly_type(AbstractAlgebra.ZZ)
AbstractAlgebra.Generic.Poly{BigInt}
julia> dense_poly_type(typeof(AbstractAlgebra.ZZ))
AbstractAlgebra.Generic.Poly{BigInt}
```
"""
dense_poly_type(::Type{T}) where T<:NCRingElement = Generic.NCPoly{T}
dense_poly_type(::Type{S}) where S<:NCRing = dense_poly_type(elem_type(S))
dense_poly_type(x) = dense_poly_type(typeof(x)) # to stop this method from eternally recursing on itself, we better add ...
dense_poly_type(::Type{T}) where T = throw(ArgumentError("Type `$T` must be subtype of `NCRingElement`."))
@doc raw"""
dense_poly_ring_type(::Type{T}) where T<:NCRingElement
dense_poly_ring_type(::T) where T<:NCRingElement
dense_poly_ring_type(::Type{S}) where S<:NCRing
dense_poly_ring_type(::S) where S<:NCRing
The type of univariate polynomial rings with coefficients of type `T` respectively
`elem_type(S)`. Implemented via [`dense_poly_type`](@ref).
See also [`mpoly_type`](@ref) and [`mpoly_ring_type`](@ref).
# Examples
```jldoctest; setup = :(using AbstractAlgebra)
julia> dense_poly_ring_type(AbstractAlgebra.ZZ(1))
AbstractAlgebra.Generic.PolyRing{BigInt}
julia> dense_poly_ring_type(elem_type(AbstractAlgebra.ZZ))
AbstractAlgebra.Generic.PolyRing{BigInt}
julia> dense_poly_ring_type(AbstractAlgebra.ZZ)
AbstractAlgebra.Generic.PolyRing{BigInt}
julia> dense_poly_ring_type(typeof(AbstractAlgebra.ZZ))
AbstractAlgebra.Generic.PolyRing{BigInt}
```
"""
dense_poly_ring_type(x) = parent_type(dense_poly_type(x))
@doc raw"""
var(a::NCPolyRing)
Return the internal name of the generator of the polynomial ring. Note that
this is returned as a `Symbol` not a `String`.
"""
var(a::NCPolyRing) = a.S
@doc raw"""
symbols(a::NCPolyRing)
Return an array of the variable names for the polynomial ring. Note that
this is returned as an array of `Symbol` not `String`.
"""
symbols(a::NCPolyRing) = [a.S]
###############################################################################
#
# Basic manipulation
#
###############################################################################
function Base.hash(a::NCPolyRingElem, h::UInt)
b = 0xd3f41ffbf953cbd8%UInt
for i in 0:length(a) - 1
b = xor(b, xor(hash(coeff(a, i), h), h))
b = (b << 1) | (b >> (sizeof(Int)*8 - 1))
end
return b
end
zero(R::NCPolyRing) = R(0)
one(R::NCPolyRing) = R(1)
@doc raw"""
gen(R::NCPolyRing)
Return the generator of the given polynomial ring.
"""
gen(R::NCPolyRing) = R([zero(base_ring(R)), one(base_ring(R))])
is_term(a::T) where T <: NCRingElem = true
###############################################################################
#
# String I/O
#
###############################################################################
function show(io::IO, p::NCPolyRing)
io = pretty(io)
print(io, "Univariate polynomial ring in ", var(p), " over ")
print(IOContext(io, :compact => true), Lowercase(), base_ring(p))
end
###############################################################################
#
# Binary operations
#
###############################################################################
function +(a::NCPolyRingElem{T}, b::NCPolyRingElem{T}) where T <: NCRingElem
check_parent(a, b)
lena = length(a)
lenb = length(b)
lenz = max(lena, lenb)
z = parent(a)()
fit!(z, lenz)
i = 1
while i <= min(lena, lenb)
z = setcoeff!(z, i - 1, coeff(a, i - 1) + coeff(b, i - 1))
i += 1
end
while i <= lena
z = setcoeff!(z, i - 1, deepcopy(coeff(a, i - 1)))
i += 1
end
while i <= lenb
z = setcoeff!(z, i - 1, deepcopy(coeff(b, i - 1)))
i += 1
end
z = set_length!(z, normalise(z, i - 1))
return z
end
function -(a::NCPolyRingElem{T}, b::NCPolyRingElem{T}) where T <: NCRingElem
check_parent(a, b)
lena = length(a)
lenb = length(b)
lenz = max(lena, lenb)
z = parent(a)()
fit!(z, lenz)
i = 1
while i <= min(lena, lenb)
z = setcoeff!(z, i - 1, coeff(a, i - 1) - coeff(b, i - 1))
i += 1
end
while i <= lena
z = setcoeff!(z, i - 1, deepcopy(coeff(a, i - 1)))
i += 1
end
while i <= lenb
z = setcoeff!(z, i - 1, -coeff(b, i - 1))
i += 1
end
z = set_length!(z, normalise(z, i - 1))
return z
end
function *(a::NCPolyRingElem{T}, b::NCPolyRingElem{T}) where T <: NCRingElem
lena = length(a)
lenb = length(b)
if lena == 0 || lenb == 0
return parent(a)()
end
t = base_ring(a)()
lenz = lena + lenb - 1
d = Vector{T}(undef, lenz)
for i = 1:lena
d[i] = coeff(a, i - 1)*coeff(b, 0)
end
for i = 2:lenb
d[lena + i - 1] = a.coeffs[lena]*coeff(b, i - 1)
end
for i = 1:lena - 1
for j = 2:lenb
t = mul!(t, coeff(a, i - 1), b.coeffs[j])
d[i + j - 1] = addeq!(d[i + j - 1], t)
end
end
z = parent(a)(d)
z = set_length!(z, normalise(z, lenz))
return z
end
###############################################################################
#
# Ad hoc binary operators
#
###############################################################################
function *(a::T, b::NCPolyRingElem{T}) where T <: NCRingElem
len = length(b)
z = parent(b)()
fit!(z, len)
for i = 1:len
z = setcoeff!(z, i - 1, a*coeff(b, i - 1))
end
z = set_length!(z, normalise(z, len))
return z
end
function *(a::NCPolyRingElem{T}, b::T) where T <: NCRingElem
len = length(a)
z = parent(a)()
fit!(z, len)
for i = 1:len
z = setcoeff!(z, i - 1, coeff(a, i - 1)*b)
end
z = set_length!(z, normalise(z, len))
return z
end
function +(a::T, b::NCPolyRingElem{T}) where {T <: NCRingElem}
z = deepcopy(b)
len = length(z)
z = setcoeff!(z, 0, a + coeff(b, 0))
z = set_length!(z, normalise(z, len))
return z
end
+(a::NCPolyRingElem{T}, b::T) where {T <: NCRingElem} = b + a
+(a::Union{Integer, Rational}, b::NCPolyRingElem{T}) where {T <: NCRingElem} = parent(b)(a) + b
+(a::NCPolyRingElem{T}, b::Union{Integer, Rational}) where {T <: NCRingElem} = b + a
-(a::Union{Integer, Rational}, b::NCPolyRingElem{T}) where {T <: NCRingElem} = parent(b)(a) - b
-(a::NCPolyRingElem{T}, b::Union{Integer, Rational}) where {T <: NCRingElem} = a - parent(a)(b)
###############################################################################
#
# Powering
#
###############################################################################
@doc raw"""
^(a::NCPolyRingElem{T}, b::Int) where T <: NCRingElem
Return $a^b$. We require $b \geq 0$.
"""
function ^(a::NCPolyRingElem{T}, b::Int) where T <: NCRingElem
b < 0 && throw(DomainError(b, "exponent must be >= 0"))
# special case powers of x for constructing polynomials efficiently
R = parent(a)
if is_gen(a)
z = R()
fit!(z, b + 1)
z = setcoeff!(z, b, deepcopy(coeff(a, 1)))
for i = 1:b
z = setcoeff!(z, i - 1, deepcopy(coeff(a, 0)))
end
z = set_length!(z, b + 1)
return z
elseif b == 0
return one(R)
elseif length(a) == 0
return zero(R)
elseif length(a) == 1
return R(coeff(a, 0)^b)
elseif b == 1
return deepcopy(a)
else
bit = ~((~UInt(0)) >> 1)
while (UInt(bit) & b) == 0
bit >>= 1
end
z = a
bit >>= 1
while bit != 0
z = z*z
if (UInt(bit) & b) != 0
z *= a
end
bit >>= 1
end
return z
end
end
###############################################################################
#
# Comparisons
#
###############################################################################
@doc raw"""
==(x::NCPolyRingElem{T}, y::NCPolyRingElem{T}) where T <: NCRingElem
Return `true` if $x == y$ arithmetically, otherwise return `false`. Recall
that power series to different precisions may still be arithmetically
equal to the minimum of the two precisions.
"""
function ==(x::NCPolyRingElem{T}, y::NCPolyRingElem{T}) where T <: NCRingElem
b = check_parent(x, y, false)
!b && return false
if length(x) != length(y)
return false
else
for i = 1:length(x)
if coeff(x, i - 1) != coeff(y, i - 1)
return false
end
end
end
return true
end
@doc raw"""
isequal(x::NCPolyRingElem{T}, y::NCPolyRingElem{T}) where T <: NCRingElem
Return `true` if $x == y$ exactly, otherwise return `false`. This function is
useful in cases where the coefficients of the polynomial are inexact, e.g.
power series. Only if the power series are precisely the same, to the same
precision, are they declared equal by this function.
"""
function isequal(x::NCPolyRingElem{T}, y::NCPolyRingElem{T}) where T <: NCRingElem
if parent(x) != parent(y)
return false
end
if length(x) != length(y)
return false
end
for i = 1:length(x)
if !isequal(coeff(x, i - 1), coeff(y, i - 1))
return false
end
end
return true
end
###############################################################################
#
# Ad hoc comparison
#
###############################################################################
@doc raw"""
==(x::NCPolyRingElem{T}, y::T) where T <: NCRingElem
Return `true` if $x == y$.
"""
==(x::NCPolyRingElem{T}, y::T) where T <: NCRingElem = ((length(x) == 0 && y == 0)
|| (length(x) == 1 && coeff(x, 0) == y))
@doc raw"""
==(x::T, y::NCPolyRingElem{T}) where T <: NCRingElem
Return `true` if $x = y$.
"""
==(x::T, y::NCPolyRingElem{T}) where T <: NCRingElem = y == x
@doc raw"""
==(x::Union{Integer, Rational, AbstractFloat}, y::NCPolyRingElem)
Return `true` if $x == y$ arithmetically, otherwise return `false`.
"""
==(x::Union{Integer, Rational, AbstractFloat}, y::NCPolyRingElem) = y == x
###############################################################################
#
# Truncation
#
###############################################################################
@doc raw"""
mullow(a::NCPolyRingElem{T}, b::NCPolyRingElem{T}, n::Int) where T <: NCRingElem
Return $a\times b$ truncated to $n$ terms.
"""
function mullow(a::NCPolyRingElem{T}, b::NCPolyRingElem{T}, n::Int) where T <: NCRingElem
check_parent(a, b)
lena = length(a)
lenb = length(b)
if lena == 0 || lenb == 0
return zero(parent(a))
end
if n < 0
n = 0
end
t = base_ring(a)()
lenz = min(lena + lenb - 1, n)
d = Vector{T}(undef, lenz)
for i = 1:min(lena, lenz)
d[i] = coeff(a, i - 1)*coeff(b, 0)
end
if lenz > lena
for j = 2:min(lenb, lenz - lena + 1)
d[lena + j - 1] = coeff(a, lena - 1)*coeff(b, j - 1)
end
end
for i = 1:lena - 1
if lenz > i
for j = 2:min(lenb, lenz - i + 1)
t = mul!(t, coeff(a, i - 1), coeff(b, j - 1))
d[i + j - 1] = addeq!(d[i + j - 1], t)
end
end
end
z = parent(a)(d)
z = set_length!(z, normalise(z, lenz))
return z
end
###############################################################################
#
# Exact division
#
###############################################################################
@doc raw"""
divexact_right(f::NCPolyRingElem{T}, g::NCPolyRingElem{T}; check::Bool=true) where T <: NCRingElem
Assuming $f = qg$, return $q$. By default if the division is not exact an
exception is raised. If `check=false` this test is omitted.
"""
function divexact_right(f::NCPolyRingElem{T}, g::NCPolyRingElem{T}; check::Bool=true) where T <: NCRingElem
check_parent(f, g)
iszero(g) && throw(DivideError())
if iszero(f)
return zero(parent(f))
end
lenq = length(f) - length(g) + 1
d = Vector{T}(undef, lenq)
for i = 1:lenq
d[i] = zero(base_ring(f))
end
x = gen(parent(f))
leng = length(g)
while length(f) >= leng
lenf = length(f)
q1 = d[lenf - leng + 1] = divexact_right(coeff(f, lenf - 1), coeff(g, leng - 1); check=check)
f = f - shift_left(q1*g, lenf - leng)
if length(f) == lenf # inexact case
f = set_length!(f, normalise(f, lenf - 1))
end
end
check && length(f) != 0 && error("Not an exact division")
q = parent(f)(d)
q = set_length!(q, lenq)
return q
end
@doc raw"""
divexact_left(f::NCPolyRingElem{T}, g::NCPolyRingElem{T}; check::Bool=true) where T <: NCRingElem
Assuming $f = gq$, return $q$. By default if the division is not exact an
exception is raised. If `check=false` this test is omitted.
"""
function divexact_left(f::NCPolyRingElem{T}, g::NCPolyRingElem{T}; check::Bool=true) where T <: NCRingElem
check_parent(f, g)
iszero(g) && throw(DivideError())
if iszero(f)
return zero(parent(f))
end
lenq = length(f) - length(g) + 1
d = Vector{T}(undef, lenq)
for i = 1:lenq
d[i] = zero(base_ring(f))
end
x = gen(parent(f))
leng = length(g)
while length(f) >= leng
lenf = length(f)
q1 = d[lenf - leng + 1] = divexact_left(coeff(f, lenf - 1), coeff(g, leng - 1); check=check)
f = f - shift_left(g*q1, lenf - leng)
if length(f) == lenf # inexact case
f = set_length!(f, normalise(f, lenf - 1))
end
end
check && length(f) != 0 && error("Not an exact division")
q = parent(f)(d)
q = set_length!(q, lenq)
return q
end
###############################################################################
#
# Ad hoc exact division
#
###############################################################################
@doc raw"""
divexact_right(a::NCPolyRingElem{T}, b::T; check::Bool=true) where T <: NCRingElem
Assuming $a = qb$, return $q$. By default if the division is not exact an
exception is raised. If `check=false` this test is omitted.
"""
function divexact_right(a::NCPolyRingElem{T}, b::T; check::Bool=true) where T <: NCRingElem
iszero(b) && throw(DivideError())
z = parent(a)()
fit!(z, length(a))
for i = 1:length(a)
z = setcoeff!(z, i - 1, divexact_right(coeff(a, i - 1), b; check=check))
end
z = set_length!(z, length(a))
return z
end
@doc raw"""
divexact_left(a::NCPolyRingElem{T}, b::T; check::Bool=true) where T <: NCRingElem
Assuming $a = bq$, return $q$. By default if the division is not exact an
exception is raised. If `check=false` this test is omitted.
"""
function divexact_left(a::NCPolyRingElem{T}, b::T; check::Bool=true) where T <: NCRingElem
iszero(b) && throw(DivideError())
z = parent(a)()
fit!(z, length(a))
for i = 1:length(a)
z = setcoeff!(z, i - 1, divexact_left(coeff(a, i - 1), b; check=check))
end
z = set_length!(z, length(a))
return z
end
@doc raw"""
divexact_right(a::NCPolyRingElem, b::Union{Integer, Rational, AbstractFloat}; check::Bool=true)
Assuming $a = qb$, return $q$. By default if the division is not exact an
exception is raised. If `check=false` this test is omitted.
"""
function divexact_right(a::NCPolyRingElem, b::Union{Integer, Rational, AbstractFloat}; check::Bool=true)
iszero(b) && throw(DivideError())
z = parent(a)()
fit!(z, length(a))
for i = 1:length(a)
z = setcoeff!(z, i - 1, divexact_right(coeff(a, i - 1), b; check=check))
end
z = set_length!(z, length(a))
return z
end
@doc raw"""
divexact_left(a::NCPolyRingElem, b::Union{Integer, Rational, AbstractFloat}; check::Bool=true)
Assuming $a = bq$, return $q$. By default if the division is not exact an
exception is raised. If `check=false` this test is omitted.
"""
function divexact_left(a::NCPolyRingElem, b::Union{Integer, Rational, AbstractFloat}; check::Bool=true)
return divexact_right(a, b; check=check)
end
###############################################################################
#
# Evaluation
#
###############################################################################
@doc raw"""
evaluate(a::NCPolyRingElem, b::T) where T <: NCRingElem
Evaluate the polynomial $a$ at the value $b$ and return the result.
"""
function evaluate(a::NCPolyRingElem, b::T) where T <: NCRingElem
i = length(a)
R = base_ring(a)
if i == 0
return zero(R)
end
z = R(coeff(a, i - 1))
while i > 1
i -= 1
z = R(coeff(a, i - 1)) + z*b
parent(z) # To work around a bug in julia
end
return z
end
@doc raw"""
evaluate(a::NCPolyRingElem, b::Union{Integer, Rational, AbstractFloat})
Evaluate the polynomial $a$ at the value $b$ and return the result.
"""
function evaluate(a::NCPolyRingElem, b::Union{Integer, Rational, AbstractFloat})
i = length(a)
R = base_ring(a)
if i == 0
return zero(R)
end
z = R(coeff(a, i - 1))
while i > 1
i -= 1
z = R(coeff(a, i - 1)) + z*b
parent(z) # To work around a bug in julia
end
return z
end
# Note: composition is not associative, e.g. consider fo(goh) vs (fog)oh
# for f and g of degree 2 and h of degree 1 -- and recall coeffs don't commute
################################################################################
#
# Change base ring
#
################################################################################
function change_base_ring(R::NCRing, p::NCPolyRingElem{T}; cached::Bool = true, parent::PolyRing = _change_poly_ring(R, parent(p), cached)) where T <: NCRingElement
return _map(R, p, parent)
end
function change_coefficient_ring(R::NCRing, p::NCPolyRingElem{T}; cached::Bool = true, parent::PolyRing = _change_poly_ring(R, parent(p), cached)) where T <: NCRingElement
return change_base_ring(R, p; cached = cached, parent = parent)
end
################################################################################
#
# Map
#
################################################################################
_make_parent(g, p::NCPolyRingElem, cached::Bool) =
_change_poly_ring(parent(g(zero(base_ring(p)))),
parent(p), cached)
function map_coefficients(g, p::NCPolyRingElem{<:NCRingElement};
cached::Bool = true,
parent::NCPolyRing = _make_parent(g, p, cached))
return _map(g, p, parent)
end
function _map(g, p::NCPolyRingElem, Rx)
R = base_ring(Rx)
new_coefficients = elem_type(R)[let c = coeff(p, i)
iszero(c) ? zero(R) : R(g(c))
end for i in 0:degree(p)]
return Rx(new_coefficients)
end
###############################################################################
#
# Unsafe functions
#
###############################################################################
function addmul!(z::NCPolyRingElem{T}, x::NCPolyRingElem{T}, y::NCPolyRingElem{T}, c::NCPolyRingElem{T}) where T <: NCRingElem
c = mul!(c, x, y)
z = addeq!(z, c)
return z
end
###############################################################################
#
# Random elements
#
###############################################################################
RandomExtensions.maketype(S::NCPolyRing, dr::AbstractUnitRange{Int}, _) = elem_type(S)
function RandomExtensions.make(S::NCPolyRing, deg_range::AbstractUnitRange{Int}, vs...)
R = base_ring(S)
if length(vs) == 1 && elem_type(R) == Random.gentype(vs[1])
Make(S, deg_range, vs[1]) # forward to default Make constructor
else
Make(S, deg_range, make(R, vs...))
end
end
function rand(rng::AbstractRNG,
sp::SamplerTrivial{<:Make3{<:NCPolyRingElem,
<:NCPolyRing,
<:AbstractUnitRange{Int}}})
S, deg_range, v = sp[][1:end]
R = base_ring(S)
f = S()
x = gen(S)
for i = 0:rand(rng, deg_range)
f += rand(rng, v)*x^i
end
return f
end
rand(rng::AbstractRNG, S::NCPolyRing, deg_range::AbstractUnitRange{Int}, v...) =
rand(rng, make(S, deg_range, v...))
rand(S::NCPolyRing, deg_range, v...) = rand(Random.GLOBAL_RNG, S, deg_range, v...)
###############################################################################
#
# polynomial_ring constructor
#
###############################################################################
@doc raw"""
polynomial_ring(R::NCRing, s::VarName; cached::Bool = true)
Given a base ring `R` and symbol/string `s` specifying how the generator
(variable) should be printed, return a tuple `S, x` representing the new
polynomial ring $S = R[x]$ and the generator $x$ of the ring.
By default the parent object `S` depends only on `R` and `x` and will be cached.
Setting the optional argument `cached` to `false` will prevent the parent object `S` from being cached.
# Examples
```jldoctest; setup = :(using AbstractAlgebra)
julia> R, x = polynomial_ring(ZZ, :x)
(Univariate polynomial ring in x over integers, x)
julia> S, y = polynomial_ring(R, :y)
(Univariate polynomial ring in y over univariate polynomial ring, y)
```
"""
function polynomial_ring(R::NCRing, s::VarName; kw...)
S = polynomial_ring_only(R, Symbol(s); kw...)
(S, gen(S))
end
polynomial_ring(R::NCRing; kw...) = polynomial_ring(R, :x; kw...)
@doc raw"""
polynomial_ring_only(R::NCRing, s::Symbol; cached::Bool=true)
Like [`polynomial_ring(R::NCRing, s::Symbol)`](@ref) but return only the
polynomial ring.
"""
polynomial_ring_only(R::T, s::Symbol; cached::Bool=true) where T<:NCRing =
dense_poly_ring_type(T)(R, s, cached)
# Simplified constructor
PolyRing(R::NCRing) = polynomial_ring_only(R, :x; cached=false)