-
Notifications
You must be signed in to change notification settings - Fork 63
/
MPoly.jl
1470 lines (1268 loc) · 45.8 KB
/
MPoly.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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
#
# MPoly.jl : sparse distributed multivariate polynomials over rings
#
###############################################################################
###############################################################################
#
# Data type and parent object methods
#
###############################################################################
base_ring(a::MPolyRingElem{T}) where T <: RingElement = base_ring(parent(a))
coefficient_ring(a::MPolyRingElem) = base_ring(a)
coefficient_ring(R::MPolyRing) = base_ring(R)
@doc raw"""
mpoly_type(::Type{T}) where T<:RingElement
mpoly_type(::T) where T<:RingElement
mpoly_type(::Type{S}) where S<:Ring
mpoly_type(::S) where S<:Ring
The type of multivariate polynomials with coefficients of type `T` respectively `elem_type(S)`.
Falls back to `Generic.MPoly{T}`.
See also [`mpoly_ring_type`](@ref), [`dense_poly_type`](@ref) and [`dense_poly_ring_type`](@ref).
# Examples
```jldoctest; setup = :(using AbstractAlgebra)
julia> mpoly_type(AbstractAlgebra.ZZ(1))
AbstractAlgebra.Generic.MPoly{BigInt}
julia> mpoly_type(elem_type(AbstractAlgebra.ZZ))
AbstractAlgebra.Generic.MPoly{BigInt}
julia> mpoly_type(AbstractAlgebra.ZZ)
AbstractAlgebra.Generic.MPoly{BigInt}
julia> mpoly_type(typeof(AbstractAlgebra.ZZ))
AbstractAlgebra.Generic.MPoly{BigInt}
```
"""
mpoly_type(::Type{T}) where T<:RingElement = Generic.MPoly{T}
mpoly_type(::Type{S}) where S<:Ring = mpoly_type(elem_type(S))
mpoly_type(x) = mpoly_type(typeof(x)) # to stop this method from eternally recursing on itself, we better add ...
mpoly_type(::Type{T}) where T = throw(ArgumentError("Type `$T` must be subtype of `RingElement`."))
@doc raw"""
mpoly_ring_type(::Type{T}) where T<:RingElement
mpoly_ring_type(::T) where T<:RingElement
mpoly_ring_type(::Type{S}) where S<:Ring
mpoly_ring_type(::S) where S<:Ring
The type of multivariate polynomial rings with coefficients of type `T`
respectively `elem_type(S)`. Implemented via [`mpoly_type`](@ref).
See also [`dense_poly_type`](@ref) and [`dense_poly_ring_type`](@ref).
# Examples
```jldoctest; setup = :(using AbstractAlgebra)
julia> mpoly_ring_type(AbstractAlgebra.ZZ(1))
AbstractAlgebra.Generic.MPolyRing{BigInt}
julia> mpoly_ring_type(elem_type(AbstractAlgebra.ZZ))
AbstractAlgebra.Generic.MPolyRing{BigInt}
julia> mpoly_ring_type(AbstractAlgebra.ZZ)
AbstractAlgebra.Generic.MPolyRing{BigInt}
julia> mpoly_ring_type(typeof(AbstractAlgebra.ZZ))
AbstractAlgebra.Generic.MPolyRing{BigInt}
```
"""
mpoly_ring_type(x) = parent_type(mpoly_type(x))
function is_domain_type(::Type{T}) where {S <: RingElement, T <: MPolyRingElem{S}}
return is_domain_type(S)
end
function is_exact_type(a::Type{T}) where {S <: RingElement, T <: MPolyRingElem{S}}
return is_exact_type(S)
end
@doc raw"""
ngens(R::MPolyRing)
Return the number of variables in `R`.
"""
ngens(R::MPolyRing) = nvars(R)
@doc raw"""
vars(p::MPolyRingElem{T}) where {T <: RingElement}
Return the variables actually occurring in $p$.
"""
function vars(p::MPolyRingElem{T}) where {T <: RingElement}
U = typeof(p)
vars_in_p = Vector{U}(undef, 0)
n = nvars(p.parent)
gen_list = gens(p.parent)
biggest = [0 for i in 1:n]
for v in exponent_vectors(p)
for j = 1:n
if v[j] > biggest[j]
biggest[j] = v[j]
end
end
end
for i = 1:n
if biggest[i] != 0
push!(vars_in_p, gen_list[i])
end
end
return(vars_in_p)
end
@doc raw"""
var_index(p::MPolyRingElem{T}) where {T <: RingElement}
Return the index of the given variable $x$. If $x$ is not a variable
in a multivariate polynomial ring, an exception is raised.
"""
function var_index(x::MPolyRingElem{T}) where {T <: RingElement}
!is_monomial(x) && error("Not a variable in var_index")
exps = first(exponent_vectors(x))
count = 0
index = 0
for i = 1:length(exps)
if exps[i] > 1
error("Not a variable in var_index")
end
if exps[i] == 1
count += 1
index = i
end
end
count != 1 && error("Not a variable in var_index")
return index
end
function characteristic(a::MPolyRing{T}) where T <: RingElement
return characteristic(base_ring(a))
end
###############################################################################
#
# Multivariate coefficients
#
###############################################################################
@doc raw"""
coeff(a::MPolyRingElem{T}, vars::Vector{Int}, exps::Vector{Int}) where T <: RingElement
Return the "coefficient" of $a$ (as a multivariate polynomial in the same
ring) of the monomial consisting of the product of the variables of the given
indices raised to the given exponents (note that not all variables need to
appear and the exponents can be zero). E.g. `coeff(f, [1, 3], [0, 2])` returns
the coefficient of $x^0*z^2$ in the polynomial $f$ (assuming variables
$x, y, z$ in that order).
"""
function coeff(a::MPolyRingElem{T}, vars::Vector{Int}, exps::Vector{Int}) where T <: RingElement
unique(vars) != vars && error("Variables not unique")
length(vars) != length(exps) &&
error("Number of variables does not match number of exponents")
for i = 1:length(vars)
if vars[i] < 1 || vars[i] > nvars(parent(a))
error("Variable index not in range")
end
if exps[i] < 0
error("Exponent cannot be negative")
end
end
S = parent(a)
M = Generic.MPolyBuildCtx(S)
cvzip = zip(coefficients(a), exponent_vectors(a))
for (c, v) in cvzip
flag = true
for j = 1:length(vars)
if v[vars[j]] != exps[j]
flag = false
break
else
v[vars[j]] = 0
end
end
if flag
push_term!(M, c, v)
end
end
return finish(M)
end
@doc raw"""
coeff(a::T, vars::Vector{T}, exps::Vector{Int}) where T <: MPolyRingElem
Return the "coefficient" of $a$ (as a multivariate polynomial in the same
ring) of the monomial consisting of the product of the given variables
to the given exponents (note that not all variables need to appear and the
exponents can be zero). E.g. `coeff(f, [x, z], [0, 2])` returns the
coefficient of $x^0*z^2$ in the polynomial $f$.
"""
function coeff(a::T, vars::Vector{T}, exps::Vector{Int}) where T <: MPolyRingElem
varidx = [var_index(x) for x in vars]
return coeff(a, varidx, exps)
end
###############################################################################
#
# Basic manipulation
#
###############################################################################
# Fallback hash function for multivariate polynomials implementing the
# iterators
function Base.hash(x::MPolyRingElem{T}, h::UInt) where {T <: RingElement}
b = 0x53dd43cd511044d1%UInt
for (e, c) in zip(exponent_vectors(x), coefficients(x))
b = xor(b, hash(c, h), h)
b = xor(b, hash(e, h), h)
b = (b << 1) | (b >> (sizeof(Int)*8 - 1))
end
return b
end
@doc raw"""
is_degree(s::Symbol)
Return `true` if the given symbol represents a degree ordering (deglex or
degrevlex).
"""
is_degree(s::Symbol) = s == :deglex || s == :degrevlex
@doc raw"""
is_reverse(s::Symbol)
Return `true` if the given symbol represents a reverse ordering (degrevlex).
"""
is_reverse(s::Symbol) = s == :degrevlex
@doc raw"""
coeff(f::MPolyRingElem{T}, m::MPolyRingElem{T}) where T <: RingElement
Return the coefficient of the monomial $m$ of the polynomial $f$. If there
is no such monomial, zero is returned.
"""
function coeff(f::MPolyRingElem{T}, m::MPolyRingElem{T}) where T <: RingElement
!is_monomial(m) && error("Not a monomial in coeff")
v1 = first(exponent_vectors(m))
cvzip = zip(coefficients(f), exponent_vectors(f))
for (c, v) in cvzip
if v == v1
return c
end
end
return zero(base_ring(f))
end
@doc raw"""
leading_coefficient(p::MPolyRingElem)
Return the leading coefficient of the polynomial $p$.
"""
function leading_coefficient(p::MPolyRingElem{T}) where T <: RingElement
if iszero(p)
return zero(base_ring(p))
else
return first(coefficients(p))
end
end
@doc raw"""
trailing_coefficient(p::MPolyRingElem)
Return the trailing coefficient of the polynomial $p$, i.e. the coefficient of
the last nonzero term, or zero if the polynomial is zero.
"""
function trailing_coefficient(p::MPolyRingElem{T}) where T <: RingElement
coeff = zero(base_ring(p))
for c in coefficients(p)
coeff = c
end
return coeff
end
@doc raw"""
tail(p::MPolyRingElem)
Return the tail of the polynomial $p$, i.e. the polynomial without its leading
term (if any).
"""
function tail(p::MPolyRingElem{T}) where T <: RingElement
S = parent(p)
if iszero(p)
return S()
end
ctx = Generic.MPolyBuildCtx(S)
tail_cv = Iterators.drop(zip(coefficients(p), exponent_vectors(p)), 1)
for (c, v) in tail_cv
push_term!(ctx, c, v)
end
return finish(ctx)
end
@doc raw"""
constant_coefficient(p::MPolyRingElem)
Return the constant coefficient of the polynomial $p$ or zero if it doesn't
have one.
"""
function constant_coefficient(p::MPolyRingElem{T}) where T <: RingElement
if !iszero(p)
for (c, v) in zip(coefficients(p), exponent_vectors(p))
if iszero(v)
return c
end
end
end
return zero(base_ring(p))
end
function constant_coefficient(p::MPolyRingElem)
len = length(p)
if !iszero(p) && iszero(exponent_vector(p, len))
return coeff(p, len)
end
return zero(base_ring(p))
end
@doc raw"""
leading_monomial(p::MPolyRingElem)
Return the leading monomial of $p$.
This function throws an `ArgumentError` if $p$ is zero.
"""
function leading_monomial(p::MPolyRingElem{T}) where T <: RingElement
if iszero(p)
throw(ArgumentError("Zero polynomial does not have a leading monomial"))
end
return first(monomials(p))
end
@doc raw"""
leading_exponent_vector(p::MPolyRingElem)
Return the exponent vector of the leading term of $p$. The return is a Julia
1-dimensional array giving the exponent for each variable of the leading term.
This function throws an `ArgumentError` if $p$ is zero.
"""
function leading_exponent_vector(p::MPolyRingElem{T}) where T <: RingElement
if iszero(p)
throw(ArgumentError("Zero polynomial does not have a leading exponent vector"))
end
return first(exponent_vectors(p))
end
@doc raw"""
leading_term(p::MPolyRingElem)
Return the leading term of the polynomial p.
This function throws an `ArgumentError` if $p$ is zero.
"""
function leading_term(p::MPolyRingElem{T}) where T <: RingElement
if iszero(p)
throw(ArgumentError("Zero polynomial does not have a leading term"))
end
return first(terms(p))
end
@doc raw"""
degree(f::MPolyRingElem{T}, i::Int) where T <: RingElement
Return the degree of the polynomial $f$ in terms of the i-th variable.
"""
function degree(f::MPolyRingElem{T}, i::Int) where T <: RingElement
biggest = -1
if length(f) != 0
R = parent(f)
if ordering(R) == :lex && i == 1
biggest = first(exponent_vectors(f))[1]
else
for v in exponent_vectors(f)
if v[i] > biggest
biggest = v[i]
end
end
end
end
return biggest
end
@doc raw"""
degree(f::MPolyRingElem{T}, x::MPolyRingElem{T}) where T <: RingElement
Return the degree of the polynomial $f$ in terms of the variable $x$.
"""
function degree(f::MPolyRingElem{T}, x::MPolyRingElem{T}) where T <: RingElement
return degree(f, var_index(x))
end
@doc raw"""
degrees(f::MPolyRingElem{T}) where T <: RingElement
Return an array of the degrees of the polynomial $f$ in terms of each variable.
"""
function degrees(f::MPolyRingElem{T}) where T <: RingElement
R = parent(f)
if nvars(R) == 1 && ordering(R) == :lex && length(f) > 0
return first(exponent_vectors(f))
else
biggest = [-1 for i = 1:nvars(R)]
for v in exponent_vectors(f)
for j = 1:nvars(R)
if v[j] > biggest[j]
biggest[j] = v[j]
end
end
end
return biggest
end
end
one(R::MPolyRing) = R(1)
zero(R::MPolyRing) = R(0)
function isone(x::MPolyRingElem{T}) where T <: RingElement
return length(x) == 1 && iszero(first(exponent_vectors(x))) &&
first(coefficients(x)) == 1
end
iszero(x::MPolyRingElem{T}) where T <: RingElement = length(x) == 0
function is_unit(a::MPolyRingElem{T}) where T <: RingElement
if is_constant(a)
return is_unit(leading_coefficient(a))
elseif is_domain_type(elem_type(coefficient_ring(a)))
return false
elseif length(a) == 1
return false
else
throw(NotImplementedError(:is_unit, a))
end
end
function content(a::MPolyRingElem{T}) where T <: RingElement
z = zero(coefficient_ring(a))
for c in coefficients(a)
z = gcd(z, c)
end
return z
end
function is_zero_divisor(x::MPolyRingElem{T}) where T <: RingElement
return is_zero_divisor(content(x))
end
function is_zero_divisor_with_annihilator(a::MPolyRingElem{T}) where T <: RingElement
f, b = is_zero_divisor_with_annihilator(content(a))
return f, parent(a)(b)
end
@doc raw"""
is_constant(x::MPolyRingElem{T}) where T <: RingElement
Return `true` if `x` is a degree zero polynomial or the zero polynomial, i.e.
a constant polynomial.
"""
function is_constant(x::MPolyRingElem{T}) where T <: RingElement
return length(x) == 0 || (length(x) == 1 &&
iszero(first(exponent_vectors(x))))
end
@doc raw"""
is_term(x::MPolyRingElem)
Return `true` if the given polynomial has precisely one term.
"""
is_term(x::MPolyRingElem{T}) where T <: RingElement = length(x) == 1
@doc raw"""
is_monomial(x::MPolyRingElem)
Return `true` if the given polynomial has precisely one term whose coefficient is one.
"""
function is_monomial(x::MPolyRingElem{T}) where T <: RingElement
return length(x) == 1 && isone(first(coefficients(x)))
end
###############################################################################
#
# Iterators
#
###############################################################################
@doc raw"""
coefficients(a::MPolyRingElem{T}) where T <: RingElement
Return an iterator for the coefficients of the given polynomial. To retrieve
an array of the coefficients, use `collect(coefficients(a))`.
"""
function coefficients(a::MPolyRingElem{T}) where T <: RingElement
return Generic.MPolyCoeffs(a)
end
@doc raw"""
exponent_vectors(a::MPolyRingElem{T}) where T <: RingElement
Return an iterator for the exponent vectors of the given polynomial. To
retrieve an array of the exponent vectors, use
`collect(exponent_vectors(a))`.
"""
function exponent_vectors(a::MPolyRingElem{T}) where T <: RingElement
return Generic.MPolyExponentVectors(a)
end
@doc raw"""
monomials(a::MPolyRingElem{T}) where T <: RingElement
Return an iterator for the monomials of the given polynomial. To retrieve
an array of the monomials, use `collect(monomials(a))`.
"""
function monomials(a::MPolyRingElem{T}) where T <: RingElement
return Generic.MPolyMonomials(a)
end
@doc raw"""
terms(a::MPolyRingElem{T}) where T <: RingElement
Return an iterator for the terms of the given polynomial. To retrieve
an array of the terms, use `collect(terms(a))`.
"""
function terms(a::MPolyRingElem{T}) where T <: RingElement
return Generic.MPolyTerms(a)
end
###############################################################################
#
# String I/O
#
###############################################################################
function expressify(a::MPolyRingElem, x = symbols(parent(a)); context = nothing)
sum = Expr(:call, :+)
n = nvars(parent(a))
for (c, v) in zip(coefficients(a), exponent_vectors(a))
prod = Expr(:call, :*)
if !isone(c)
push!(prod.args, expressify(c, context = context))
end
for i in 1:n
if v[i] > 1
push!(prod.args, Expr(:call, :^, x[i], v[i]))
elseif v[i] == 1
push!(prod.args, x[i])
end
end
push!(sum.args, prod)
end
return sum
end
@enable_all_show_via_expressify MPolyRingElem
function show(io::IO, ::MIME"text/plain", p::MPolyRing)
max_vars = 5 # largest number of variables to print
n = nvars(p)
print(io, "Multivariate polynomial ring")
print(io, " in ", ItemQuantity(nvars(p), "variable"), " ")
if n > max_vars
join(io, symbols(p)[1:max_vars - 1], ", ")
println(io, "..., ", symbols(p)[n])
else
join(io, symbols(p), ", ")
println(io)
end
io = pretty(io) # we need this to allow indented and lowercase printing
print(io, Indent(), "over ", Lowercase(), base_ring(p))
print(io, Dedent())
end
function show(io::IO, p::MPolyRing)
if get(io, :supercompact, false)
# no nested printing
print(io, "Multivariate polynomial ring")
else
io = pretty(io) # we need this to allow printing lowercase
# nested printing allowed, preferably supercompact
print(io, "Multivariate polynomial ring in ", ItemQuantity(nvars(p), "variable"))
print(IOContext(io, :supercompact => true), " over ", Lowercase(), base_ring(p))
end
end
function canonical_unit(x::MPolyRingElem)
if length(x) == 0
return base_ring(x)()
else
return canonical_unit(coeff(x, 1))
end
end
###############################################################################
#
# Inflation/deflation
#
###############################################################################
@doc raw"""
deflation(f::MPolyRingElem{T}) where T <: RingElement
Compute deflation parameters for the exponents of the polynomial $f$. This
is a pair of arrays of integers, the first array of which (the shift) gives
the minimum exponent for each variable of the polynomial, and the second of
which (the deflation) gives the gcds of all the exponents after subtracting
the shift, again per variable. This functionality is used by gcd (and can be
used by factorisation algorithms).
"""
function deflation(f::MPolyRingElem{T}) where T <: RingElement
N = nvars(parent(f))
if length(f) == 0
return [0 for i in 1:N], [0 for i in 1:N]
end
defl = [0 for i in 1:N]
shift = first(exponent_vectors(f))
for v in Iterators.drop(exponent_vectors(f), 1)
for j = 1:N
exj = v[j]
if exj < shift[j]
defl[j] = defl[j] == 1 ? 1 : gcd(defl[j], shift[j] - exj)
shift[j] = exj
else
defl[j] = defl[j] == 1 ? 1 : gcd(defl[j], exj - shift[j])
end
end
end
return shift, defl
end
@doc raw"""
deflate(f::MPolyRingElem{T}, shift::Vector{Int}, defl::Vector{Int}) where T <: RingElement
Return a polynomial with the same coefficients as $f$ but whose exponents
have been reduced by the given shifts (supplied as an array of shifts,
one for each variable), then deflated (divided) by the given exponents
(again supplied as an array of deflation factors, one for each variable).
The algorithm automatically replaces a deflation of $0$ by $1$, to avoid
division by $0$.
"""
function deflate(f::MPolyRingElem{T}, shift::Vector{Int}, defl::Vector{Int}) where T <: RingElement
S = parent(f)
N = nvars(S)
for i = 1:N
if defl[i] == 0
defl[i] = 1
end
end
M = Generic.MPolyBuildCtx(S)
cvzip = zip(coefficients(f), exponent_vectors(f))
for (c, v) in cvzip
for j = 1:N
v[j] = div(v[j] - shift[j], defl[j])
end
push_term!(M, c, v)
end
return finish(M)
end
@doc raw"""
deflate(f::MPolyRingElem{T}, defl::Vector{Int}) where T <: RingElement
Return a polynomial with the same coefficients as $f$ but whose exponents have
been deflated (divided) by the given exponents (supplied as an array of
deflation factors, one for each variable).
The algorithm automatically replaces a deflation of $0$ by $1$, to avoid
division by $0$.
"""
function deflate(f::MPolyRingElem{T}, defl::Vector{Int}) where T <: RingElement
return deflate(f, [0 for i in 1:nvars(parent(f))], defl)
end
@doc raw"""
deflate(f::MPolyRingElem{T}, defl::Vector{Int}) where T <: RingElement
Return a polynomial with the same coefficients as $f$ but whose exponents have
been deflated maximally, i.e. with each exponent divide by the largest integer
which divides the degrees of all exponents of that variable in $f$.
"""
function deflate(f::MPolyRingElem{T}) where T <: RingElement
shift, defl = deflation(f)
defl = gcd.(shift, defl)
return deflate(f, defl), defl
end
function inflate_deflate_vectors(R::MPolyRing, vars::Vector{Int}, shift::Vector{Int}, defl::Vector{Int})::Tuple{Vector{Int},Vector{Int}}
unique(vars) != vars && error("Variables not unique")
!(length(vars) == length(shift) == length(defl)) && error("Number of variables does not match lengths of shift and deflation vectors")
shift1 = zeros(Int, nvars(R))
defl1 = ones(Int, nvars(R))
for i in 1:(length(vars))
!(1 <= vars[i] <= nvars(R)) && error("Variable index not in range")
shift1[vars[i]] = shift[i]
defl1[vars[i]] = defl[i]
end
return (shift1, defl1)
end
@doc raw"""
deflate(f::MPolyRingElem, vars::Vector{Int}, shift::Vector{Int}, defl::Vector{Int})
Return a polynomial with the same coefficients as $f$ but where exponents of
some variables (supplied as an array of variable indices) have been reduced by
the given shifts (supplied as an array of shifts), then deflated (divided) by
the given exponents (again supplied as an array of deflation factors). The
algorithm automatically replaces a deflation of $0$ by $1$, to avoid division by
$0$.
"""
function deflate(f::MPolyRingElem, vars::Vector{Int}, shift::Vector{Int}, defl::Vector{Int})
(shift1, defl1) = inflate_deflate_vectors(parent(f), vars, shift, defl)
return deflate(f, shift1, defl1)
end
@doc raw"""
deflate(f::T, vars::Vector{T}, shift::Vector{Int}, defl::Vector{Int}) where T <: MPolyRingElem
Return a polynomial with the same coefficients as $f$ but where the exponents of
the given variables have been reduced by the given shifts (supplied as an array
of shifts), then deflated (divided) by the given exponents (again supplied as an
array of deflation factors). The algorithm automatically replaces a deflation of
$0$ by $1$, to avoid division by $0$.
"""
function deflate(f::T, vars::Vector{T}, shift::Vector{Int}, defl::Vector{Int}) where T <: MPolyRingElem
varidx = [var_index(x) for x in vars]
return deflate(f, varidx, shift, defl)
end
@doc raw"""
inflate(f::MPolyRingElem{T}, shift::Vector{Int}, defl::Vector{Int}) where T <: RingElement
Return a polynomial with the same coefficients as $f$ but whose exponents
have been inflated (multiplied) by the given deflation exponents (supplied
as an array of inflation factors, one for each variable) and then increased
by the given shifts (again supplied as an array of shifts, one for each
variable).
"""
function inflate(f::MPolyRingElem{T}, shift::Vector{Int}, defl::Vector{Int}) where T <: RingElement
S = parent(f)
N = nvars(S)
M = Generic.MPolyBuildCtx(S)
cvzip = zip(coefficients(f), exponent_vectors(f))
for (c, v) in cvzip
for j = 1:N
v[j] = v[j]*defl[j] + shift[j]
end
push_term!(M, c, v)
end
return finish(M)
end
@doc raw"""
inflate(f::MPolyRingElem{T}, defl::Vector{Int}) where T <: RingElement
Return a polynomial with the same coefficients as $f$ but whose exponents
have been inflated (multiplied) by the given deflation exponents (supplied
as an array of inflation factors, one for each variable).
"""
function inflate(f::MPolyRingElem{T}, defl::Vector{Int}) where T <: RingElement
return inflate(f, [0 for i in 1:nvars(parent(f))], defl)
end
@doc raw"""
inflate(f::MPolyRingElem, vars::Vector{Int}, shift::Vector{Int}, defl::Vector{Int})
Return a polynomial with the same coefficients as $f$ but where exponents of
some variables (supplied as an array of variable indices) have been inflated
(multiplied) by the given deflation exponents (supplied as an array of inflation
factors) and then increased by the given shifts (again supplied as an array of
shifts).
"""
function inflate(f::MPolyRingElem, vars::Vector{Int}, shift::Vector{Int}, defl::Vector{Int})
(shift1, defl1) = inflate_deflate_vectors(parent(f), vars, shift, defl)
return inflate(f, shift1, defl1)
end
@doc raw"""
inflate(f::T, vars::Vector{T}, shift::Vector{Int}, defl::Vector{Int}) where T <: MPolyRingElem
Return a polynomial with the same coefficients as $f$ but where the exponents of
the given variables have been inflated (multiplied) by the given deflation
exponents (supplied as an array of inflation factors) and then increased by the
given shifts (again supplied as an array of shifts).
"""
function inflate(f::T, vars::Vector{T}, shift::Vector{Int}, defl::Vector{Int}) where T <: MPolyRingElem
varidx = [var_index(x) for x in vars]
return inflate(f, varidx, shift, defl)
end
################################################################################
#
# Remove and valuation
#
################################################################################
@doc raw"""
remove(z::MPolyRingElem{T}, p::MPolyRingElem{T}) where {T <: RingElement}
Compute the valuation of $z$ at $p$, that is, the largest $k$ such that
$p^k$ divides $z$. Additionally, $z/p^k$ is returned as the second return.
value.
See also `valuation`, which only returns the valuation.
"""
function remove(z::MPolyRingElem{T}, p::MPolyRingElem{T}) where {T <: RingElement}
check_parent(z, p)
iszero(z) && error("Not yet implemented")
fl, q = divides(z, p)
if !fl
return 0, z
end
v = 0
qn = q
while fl
q = qn
fl, qn = divides(q, p)
v += 1
end
return v, q
end
@doc raw"""
valuation(z::MPolyRingElem{T}, p::MPolyRingElem{T}) where {T <: RingElement}
Compute the valuation of $z$ at $p$, that is, the largest $k$ such that
$p^k$ divides $z$.
See also `remove`, which also returns $z/p^k$.
"""
function valuation(z::MPolyRingElem{T}, p::MPolyRingElem{T}) where {T <: RingElement}
v, _ = remove(z, p)
return v
end
###############################################################################
#
# Evaluation
#
###############################################################################
function evaluate(a::MPolyRingElem{T}, vals::Vector) where {T <: RingElement}
return evaluate(a, parent(a).(vals))
end
@doc raw"""
evaluate(a::MPolyRingElem{T}, vals::Vector{U}) where {T <: RingElement, U <: RingElement}
Evaluate the polynomial expression by substituting in the array of values for
each of the variables. The evaluation will succeed if multiplication is
defined between elements of the coefficient ring of $a$ and elements of the
supplied vector.
"""
function evaluate(a::MPolyRingElem{T}, vals::Vector{U}) where {T <: RingElement, U <: RingElement}
length(vals) != nvars(parent(a)) && error("Incorrect number of values in evaluation")
R = base_ring(a)
if (U <: Integer && U != BigInt) ||
(U <: Rational && U != Rational{BigInt})
c = zero(R)*zero(U)
V = typeof(c)
if U != V
vals = [parent(c)(v) for v in vals]
powers = [Dict{Int, V}() for i in 1:length(vals)]
else
powers = [Dict{Int, U}() for i in 1:length(vals)]
end
else
powers = [Dict{Int, U}() for i in 1:length(vals)]
end
# The best we can do here is to cache previously used powers of the values
# being substituted, as we cannot assume anything about the relative
# performance of powering vs multiplication. The function should not try
# to optimise computing new powers in any way.
# Note that this function accepts values in a non-commutative ring, so operations
# must be done in a certain order.
# But addition is associative.
S = parent(one(R)*one(parent(vals[1])))
r = elem_type(S)[zero(S)]
i = UInt(1)
cvzip = zip(coefficients(a), exponent_vectors(a))
for (c, v) in cvzip
t = one(S)
for j = 1:length(vals)
exp = v[j]
if iszero(exp)
continue
end
if !haskey(powers[j], exp)
powers[j][exp] = vals[j]^exp
end
t = t*powers[j][exp]
end
push!(r, c*t)
j = i = i + 1
while iseven(j) && length(r) > 1
top = pop!(r)
r[end] = addeq!(r[end], top)
j >>= 1
end
end
while length(r) > 1
top = pop!(r)
r[end] = addeq!(r[end], top)
end
return r[1]
end
@doc raw"""
evaluate(a::MPolyRingElem{T}, vars::Vector{Int}, vals::Vector{U}) where {T <: RingElement, U <: RingElement}
Evaluate the polynomial expression by substituting in the supplied values in
the array `vals` for the corresponding variables with indices given by the
array `vars`. The evaluation will succeed if multiplication is defined between
elements of the coefficient ring of $a$ and elements of `vals`.
"""
function evaluate(a::MPolyRingElem{T}, vars::Vector{Int}, vals::Vector{U}) where {T <: RingElement, U <: RingElement}
unique(vars) != vars && error("Variables not unique")
length(vars) != length(vals) &&
error("Number of variables does not match number of values")
for i = 1:length(vars)
if vars[i] < 1 || vars[i] > nvars(parent(a))
error("Variable index not in range")
end
end
if length(vars) == 0
return a
end
S = parent(a)
R = base_ring(a)
return _evaluate(a, S, R, vars, vals)
end
function _evaluate(a, S, R, vars, vals::Vector{U}) where {U <: Integer}
c = zero(R) * zero(U)
V = typeof(c)
if V === U
powers = Dict{Int, U}[Dict{Int, U}() for i in 1:length(vals)]
return __evaluate(a, vars, vals, powers)
else
vals2 = V[parent(c)(v) for v in vals]
powers = Dict{Int, V}[Dict{Int, V}() for i in 1:length(vals)]
return __evaluate(a, vars, vals2, powers)
end
end
function _evaluate(a, S, R, vars, vals::Vector{U}) where {U <: Rational}
c = zero(R) * zero(U)
V = typeof(c)
if V === U
powers = Dict{Int, U}[Dict{Int, U}() for i in 1:length(vals)]
return __evaluate(a, vars, vals, powers)
else
vals2 = V[parent(c)(v) for v in vals]
powers = Dict{Int, V}[Dict{Int, V}() for i in 1:length(vals)]
return __evaluate(a, vars, vals2, powers)
end
end
function _evaluate(a, S, R, vars, vals::Vector{U}) where {U <: RingElement}
powers = Dict{Int, U}[Dict{Int, U}() for i in 1:length(vals)]
return __evaluate(a, vars, vals, powers)
end
function _evaluate(a, S, R, vars, vals::Vector{BigInt})
powers = Dict{Int, BigInt}[Dict{Int, BigInt}() for i in 1:length(vals)]
return __evaluate(a, vars, vals, powers)
end
function _evaluate(a, S, R, vars, vals::Vector{Rational{BigInt}})
powers = Dict{Int, Rational{BigInt}}[Dict{Int, Rational{BigInt}}() for i in 1:length(vals)]
return __evaluate(a, vars, vals, powers)
end
function __evaluate(a, vars, vals, powers)
R = base_ring(a)
S = parent(a)
# The best we can do here is to cache previously used powers of the values
# being substituted, as we cannot assume anything about the relative
# performance of powering vs multiplication. The function should not try
# to optimise computing new powers in any way.
# We use a geobucket if the result will be an element in the same ring as a
if parent(vals[1] * one(S)) == S
r = Generic.geobucket(S)
cvzip = zip(coefficients(a), exponent_vectors(a))
for (c, v) in cvzip
t = one(S)
for j = 1:length(vars)