-
Notifications
You must be signed in to change notification settings - Fork 63
/
MPoly.jl
4124 lines (3873 loc) · 117 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 : Generic sparse distributed multivariate polynomials over rings
#
###############################################################################
###############################################################################
#
# Data type and parent object methods
#
###############################################################################
parent(a::MPoly{T}) where T <: RingElement = a.parent
parent_type(::Type{MPoly{T}}) where T <: RingElement = MPolyRing{T}
elem_type(::Type{MPolyRing{T}}) where T <: RingElement = MPoly{T}
base_ring(R::MPolyRing{T}) where T <: RingElement = R.base_ring::parent_type(T)
@doc raw"""
symbols(a::MPolyRing)
Return an array of symbols representing the variable names for the given
polynomial ring.
"""
symbols(a::MPolyRing) = a.S
@doc raw"""
number_of_variables(x::MPolyRing)
Return the number of variables of the polynomial ring.
"""
number_of_variables(a::MPolyRing) = a.num_vars
number_of_generators(a::MPolyRing) = a.num_vars
function gen(a::MPolyRing{T}, i::Int, ::Val{:lex}) where {T <: RingElement}
n = nvars(a)
@boundscheck 1 <= i <= n || throw(ArgumentError("variable index out of range"))
return a([one(base_ring(a))], reshape(UInt[UInt(j == n - i + 1)
for j = 1:n], n, 1))
end
function gen(a::MPolyRing{T}, i::Int, ::Val{:deglex}) where {T <: RingElement}
n = nvars(a)
@boundscheck 1 <= i <= n || throw(ArgumentError("variable index out of range"))
return a([one(base_ring(a))], reshape(UInt[(UInt(j == n - i + 1)
for j in 1:n)..., UInt(1)], n + 1, 1))
end
function gen(a::MPolyRing{T}, i::Int, ::Val{:degrevlex}) where {T <: RingElement}
n = nvars(a)
@boundscheck 1 <= i <= n || throw(ArgumentError("variable index out of range"))
return a([one(base_ring(a))], reshape(UInt[(UInt(j == i)
for j in 1:n)..., UInt(1)], n + 1, 1))
end
@doc raw"""
gens(a::MPolyRing{T}) where {T <: RingElement}
Return an array of all the generators (variables) of the given polynomial
ring.
"""
function gens(a::MPolyRing{T}) where {T <: RingElement}
n = a.num_vars
return elem_type(a)[gen(a, i, Val(internal_ordering(a))) for i in 1:n]
end
@doc raw"""
gen(a::MPolyRing{T}, i::Int) where {T <: RingElement}
Return the $i$-th generator (variable) of the given polynomial
ring.
"""
function gen(a::MPolyRing{T}, i::Int) where {T <: RingElement}
return gen(a, i, Val(a.ord))
end
function vars(p::MPoly{T}) where {T <: RingElement}
vars_in_p = Vector{MPoly{T}}(undef, 0)
n = nvars(p.parent)
exps = p.exps
size_exps = size(exps)
gen_list = gens(p.parent)
for j = 1:n
for i = 1:length(p)
if exps[j, i] > 0
if p.parent.ord == :degrevlex
push!(vars_in_p, gen_list[j])
else
push!(vars_in_p, gen_list[n - j + 1])
end
break
end
end
end
if p.parent.ord != :degrevlex
vars_in_p = reverse(vars_in_p)
end
return(vars_in_p)
end
@doc raw"""
internal_ordering(a::MPolyRing{T}) where {T <: RingElement}
Return the ordering of the given polynomial ring as a symbol. The options are
`:lex`, `:deglex` and `:degrevlex`.
"""
function internal_ordering(a::MPolyRing{T}) where {T <: RingElement}
return a.ord
end
###############################################################################
#
# Manipulating terms and monomials
#
###############################################################################
function exponent_vector(a::MPoly{T}, i::Int, ::Val{:lex}) where T <: RingElement
A = a.exps
N = size(A, 1)
return [Int(A[j, i]) for j in N:-1:1]
end
function exponent(a::MPoly{T}, i::Int, j::Int, ::Val{:lex}) where T <: RingElement
return Int(a.exps[size(a.exps, 1) + 1 - j, i])
end
function exponent_vector(a::MPoly{T}, i::Int, ::Val{:deglex}) where T <: RingElement
A = a.exps
N = size(A, 1)
return [Int(A[j, i]) for j in N - 1:-1:1]
end
function exponent(a::MPoly{T}, i::Int, j::Int, ::Val{:deglex}) where T <: RingElement
return Int(a.exps[size(a.exps, 1) - j, i])
end
function exponent_vector(a::MPoly{T}, i::Int, ::Val{:degrevlex}) where T <: RingElement
A = a.exps
N = size(A, 1)
return [Int(A[j, i]) for j in 1:N - 1]
end
function exponent(a::MPoly{T}, i::Int, j::Int, ::Val{:degrevlex}) where T <: RingElement
return Int(a.exps[j, i])
end
@doc raw"""
exponent_vector(a::MPoly{T}, i::Int) where T <: RingElement
Return a vector of exponents, corresponding to the exponent vector of the
i-th term of the polynomial. Term numbering begins at $1$ and the exponents
are given in the order of the variables for the ring, as supplied when the
ring was created.
"""
function exponent_vector(a::MPoly{T}, i::Int) where T <: RingElement
return exponent_vector(a, i, Val(internal_ordering(parent(a))))
end
@doc raw"""
exponent{T <: RingElem}(a::MPoly{T}, i::Int, j::Int)
Return exponent of the j-th variable in the i-th term of the polynomial.
Term and variable numbering begins at $1$ and variables are ordered as
during the creation of the ring.
"""
function exponent(a::MPoly{T}, i::Int, j::Int) where T <: RingElement
return exponent(a, i, j, Val(internal_ordering(parent(a))))
end
function set_exponent_vector!(a::MPoly{T}, i::Int, exps::Vector{Int}, ::Val{:lex}) where T <: RingElement
fit!(a, i)
A = a.exps
A[:, i] = exps[end:-1:1]
if i > length(a)
a.length = i
end
return a
end
function set_exponent_vector!(a::MPoly{T}, i::Int, exps::Vector{Int}, ::Val{:deglex}) where T <: RingElement
fit!(a, i)
A = a.exps
A[1:end - 1, i] = exps[end:-1:1]
A[end, i] = sum(exps)
if i > length(a)
a.length = i
end
return a
end
function set_exponent_vector!(a::MPoly{T}, i::Int, exps::Vector{Int}, ::Val{:degrevlex}) where T <: RingElement
fit!(a, i)
A = a.exps
A[1:end - 1, i] = exps
A[end, i] = sum(exps)
if i > length(a)
a.length = i
end
return a
end
@doc raw"""
set_exponent_vector!(a::MPoly{T}, i::Int, exps::Vector{Int}) where T <: RingElement
Set the i-th exponent vector to the supplied vector, where the entries
correspond to the exponents of the variables in the order supplied when
the ring was created. The modified polynomial is returned.
"""
function set_exponent_vector!(a::MPoly{T}, i::Int, exps::Vector{Int}) where T <: RingElement
return set_exponent_vector!(a, i, exps, Val(internal_ordering(parent(a))))
end
@doc raw"""
coeff(a::MPoly{T}, exps::Vector{Int}) where T <: RingElement
Return the coefficient of the term with the given exponent vector, or zero
if there is no such term.
"""
function coeff(a::MPoly{T}, exps::Vector{Int}) where T <: RingElement
A = a.exps
N = size(A, 1)
exp2 = Vector{UInt}(undef, N)
ord = parent(a).ord
if ord == :lex
exp2[:] = exps[end:-1:1]
elseif ord == :deglex
exp2[1:end - 1] = exps[end:-1:1]
exp2[end] = sum(exps)
else
exp2[1:end - 1] = exps[1:end]
exp2[end] = sum(exps)
end
exp2 = reshape(exp2, N, 1)
lo = 1
hi = length(a)
n = div(hi - lo + 1, 2)
while hi >= lo
v = monomial_cmp(A, lo + n, exp2, 1, N, parent(a), UInt(0))
if v == 0
return a.coeffs[lo + n]
elseif v < 0
hi = lo + n - 1
else
lo = lo + n + 1
end
n = div(hi - lo + 1, 2)
end
return base_ring(a)()
end
@doc raw"""
setcoeff!(a::MPoly, exps::Vector{Int}, c::S) where S <: RingElement
Set the coefficient of the term with the given exponent vector to the given
value $c$. This function takes $O(\log n)$ operations if a term with the given
exponent already exists, or if the term is inserted at the end of the
polynomial. Otherwise it can take $O(n)$ operations in the worst case.
"""
function setcoeff!(a::MPoly, exps::Vector{Int}, c::S) where S <: RingElement
c = base_ring(a)(c)
A = a.exps
N = size(A, 1)
exp2 = Vector{UInt}(undef, N)
ord = parent(a).ord
if ord == :lex
exp2[:] = exps[end:-1:1]
elseif ord == :deglex
exp2[1:end - 1] = exps[end:-1:1]
exp2[end] = sum(exps)
else
exp2[1:end - 1] = exps[1:end]
exp2[end] = sum(exps)
end
exp2 = reshape(exp2, N, 1)
lo = 1
hi = length(a)
if hi > 0
n = div(hi - lo + 1, 2)
while hi >= lo
v = monomial_cmp(A, lo + n, exp2, 1, N, parent(a), UInt(0))
if v == 0
if !iszero(c) # just insert the coefficient
a.coeffs[lo + n] = c
else # coefficient is zero, shift everything
for i = lo + n:length(a) - 1
a.coeffs[i] = a.coeffs[i + 1]
monomial_set!(A, i, A, i + 1, N)
end
a.coeffs[length(a)] = c # zero final coefficient
a.length -= 1
end
return a
elseif v < 0
hi = lo + n - 1
else
lo = lo + n + 1
end
n = div(hi - lo + 1, 2)
end
end
# exponent not found, must insert at lo
if !iszero(c)
lena = length(a)
fit!(a, lena + 1)
A = a.exps
for i = lena:-1:lo
a.coeffs[i + 1] = a.coeffs[i]
monomial_set!(A, i + 1, A, i, N)
end
a.coeffs[lo] = c
monomial_set!(A, lo, exp2, 1, N)
a.length += 1
end
return a
end
@doc raw"""
sort_terms!(a::MPoly{T}) where {T <: RingElement}
Sort the terms of the given polynomial according to the polynomial ring
ordering. Zero terms and duplicate exponents are ignored. To deal with those
call `combine_like_terms`. The sorted polynomial is returned.
"""
function sort_terms!(a::MPoly{T}) where {T <: RingElement}
N = parent(a).N
# The reverse order is the fastest order if already sorted
V = [(ntuple(i -> a.exps[i, r], Val(N)), r) for r in length(a):-1:1]
ord = parent(a).ord
if ord == :lex || ord == :deglex
sort!(V, lt = is_less_lex)
else
sort!(V, lt = is_less_degrevlex)
end
Rc = [a.coeffs[V[i][2]] for i in length(V):-1:1]
Re = zeros(UInt, N, length(V))
for i = 1:length(V)
for j = 1:N
Re[j, length(V) - i + 1] = V[i][1][j]
end
end
a.coeffs = Rc
a.exps = Re
return a
end
###############################################################################
#
# Monomial operations
#
###############################################################################
# Computes a degrevlex xor mask for the most significant word of an exponent
# vector. Requires the number of bits per field and the polynomial ring.
function monomial_drmask(R::MPolyRing{T}, bits::Int) where T <: RingElement
vars_per_word = div(sizeof(Int)*8, bits)
n = rem(nvars(R), vars_per_word)
return reinterpret(UInt, (1 << (bits*n)) - 1)
end
# Sets the i-th exponent vector of the exponent array A to zero
function monomial_zero!(A::Matrix{UInt}, i::Int, N::Int)
for k = 1:N
A[k, i] = UInt(0)
end
nothing
end
# Returns true if the i-th exponent vector of the exponent array A is zero
# For degree orderings, this inefficiently also checks the degree field
function monomial_iszero(A::Matrix{UInt}, i::Int, N::Int)
for k = 1:N
if A[k, i] != UInt(0)
return false
end
end
return true
end
# Returns true if the i-th and j-th exponent vectors of the array A are equal
# For degree orderings, this inefficiently also checks the degree fields
function monomial_isequal(A::Matrix{UInt}, i::Int, j::Int, N::Int)
for k = 1:N
if A[k, i] != A[k, j]
return false
end
end
return true
end
# Returns true if the i-th exponent vector of the array A is less than that of
# the j-th, according to the ordering of R
function monomial_isless(A::Matrix{UInt}, i::Int, j::Int, N::Int, R::MPolyRing{T}, drmask::UInt) where {T <: RingElement}
if R.ord == :degrevlex
if (xor(A[N, i], drmask)) < (xor(A[N, j], drmask))
return true
elseif (xor(A[N, i], drmask)) > (xor(A[N, j], drmask))
return false
end
for k = N-1:-1:1
if A[k, i] > A[k, j]
return true
elseif A[k, i] < A[k, j]
return false
end
end
else
for k = N:-1:1
if A[k, i] < A[k, j]
return true
elseif A[k, i] > A[k, j]
return false
end
end
end
return false
end
# Return true if the i-th exponent vector of the array A is less than the j-th
# exponent vector of the array B
function monomial_isless(A::Matrix{UInt}, i::Int, B::Matrix{UInt}, j::Int, N::Int, R::MPolyRing{T}, drmask::UInt) where {T <: RingElement}
if R.ord == :degrevlex
if xor(A[N, i], drmask) < xor(B[N, j], drmask)
return true
elseif xor(A[N, i], drmask) > xor(B[N, j], drmask)
return false
end
for k = N-1:-1:1
if A[k, i] > B[k, j]
return true
elseif A[k, i] < B[k, j]
return false
end
end
else
for k = N:-1:1
if A[k, i] < B[k, j]
return true
elseif A[k, i] > B[k, j]
return false
end
end
end
return false
end
# Set the i-th exponent vector of the array A to the word by word minimum of
# itself and the j-th exponent vector of B. Used for lexical orderings only.
function monomial_vecmin!(A::Matrix{UInt}, i::Int, B::Matrix{UInt}, j::Int, N::Int)
for k = 1:N
if B[k, j] < A[k, i]
A[k, i] = B[k, j]
end
end
nothing
end
# Set the i-th exponent vector of the array A to the j-th exponent vector of B
function monomial_set!(A::Matrix{UInt}, i::Int, B::Matrix{UInt}, j::Int, N::Int)
for k = 1:N
A[k, i] = B[k, j]
end
nothing
end
# Set the i-th exponent vector of the array A to the word by word reverse of
# the j-th exponent vector of B, excluding the degree. (Used for printing
# degrevlex only.)
function monomial_reverse!(A::Matrix{UInt}, i::Int, B::Matrix{UInt}, j::Int, N::Int)
for k = 1:N - 1
A[N - k, i] = B[k, j]
end
nothing
end
# Set the i-th exponent vector of the array A to the word by word sum of the
# j1-th exponent vector of B and the j2-th exponent vector of C
function monomial_add!(A::Matrix{UInt}, i::Int,
B::Matrix{UInt}, j1::Int, C::Matrix{UInt}, j2::Int, N::Int)
for k = 1:N
A[k, i] = B[k, j1] + C[k, j2]
end
nothing
end
# Set the i-th exponent vector of the array A to the word by word difference of
# the j1-th exponent vector of B and the j2-th exponent vector of C
function monomial_sub!(A::Matrix{UInt}, i::Int,
B::Matrix{UInt}, j1::Int, C::Matrix{UInt}, j2::Int, N::Int)
for k = 1:N
A[k, i] = B[k, j1] - C[k, j2]
end
nothing
end
# Set the i-th exponent vector of the array A to the scalar product of the j-th
# exponent vector of the array B with the non-negative integer n. (Used for
# raising a monomial to a power.)
function monomial_mul!(A::Matrix{UInt}, i::Int, B::Matrix{UInt}, j::Int, n::Int, N::Int)
for k = 1:N
A[k, i] = B[k, j]*reinterpret(UInt, n)
end
nothing
end
# Return true if the j1-th exponent vector of the array B has all components
# greater than or equal to those of the j2-th exponent vector of C. If so, the
# difference is returned as the i-th exponent vector of the array A. Note that
# a mask must be supplied which has 1's in all bit positions that correspond to
# an overflow of the corresponding exponent field. (Used for testing
# divisibility of monomials, and returning the quotient monomial.)
function monomial_divides!(A::Matrix{UInt}, i::Int, B::Matrix{UInt}, j1::Int, C::Matrix{UInt}, j2::Int, mask::UInt, N::Int)
flag = true
for k = 1:N
A[k, i] = reinterpret(UInt, reinterpret(Int, B[k, j1]) - reinterpret(Int, C[k, j2]))
if (A[k, i] & mask != 0)
flag = false
end
end
return flag
end
# Return true is the j-th exponent vector of the array B can be halved
# If so the i-th exponent i-th exponent vector of A is set to the half
function monomial_halves!(A::Matrix{UInt}, i::Int, B::Matrix{UInt}, j::Int, mask::UInt, N::Int)
flag = true
for k = 1:N
b = reinterpret(Int, B[k, j])
if isodd(b)
flag = false
else
A[k, i] = reinterpret(UInt, div(b, 2))
end
if A[k, i] & mask != 0
flag = false
end
end
return flag
end
# Return true if the i-th exponent vector of the array A is in an overflow
# condition. Note that a mask must be supplied which has 1's in all bit
# positions that correspond to an overflow of the corresponding exponent field.
# Used for overflow detection inside algorithms.
function monomial_overflows(A::Matrix{UInt}, i::Int, mask::UInt, N::Int)
for k = 1:N
if (A[k, i] & mask) != UInt(0)
return true
end
end
return false
end
# Return a positive integer if the i-th exponent vector of the array A is
# bigger than the j-th exponent vector of B with respect to the ordering,
# zero if it is equal and a negative integer if it is less. (Used to compare
# monomials with respect to an ordering.)
function monomial_cmp(A::Matrix{UInt}, i::Int, B::Matrix{UInt}, j::Int, N::Int, R::MPolyRing{T}, drmask::UInt) where {T <: RingElement}
if N == 0
return 0
end
k = N
while k > 1 && A[k, i] == B[k, j]
k -= 1
end
if R.ord == :degrevlex
return k == N ? reinterpret(Int, (xor(drmask, A[k, i])) - (xor(drmask, B[k, j]))) : reinterpret(Int, B[k, j] - A[k, i])
else
return reinterpret(Int, A[k, i] - B[k, j])
end
end
###############################################################################
#
# Basic manipulation
#
###############################################################################
function Base.hash(x::MPoly{T}, h::UInt) where {T <: RingElement}
b = 0x53dd43cd511044d1%UInt
b = xor(b, xor(Base.hash(x.exps, h), h))
for i in 1:length(x)
b = xor(b, xor(hash(x.coeffs[i], h), h))
b = (b << 1) | (b >> (sizeof(Int)*8 - 1))
end
return b
end
function is_gen(x::MPoly{T}, ::Val{:lex}) where {T <: RingElement}
exps = x.exps
N = size(exps, 1)
for k = 1:N
exp = exps[k, 1]
if exp != UInt(0)
if exp != UInt(1)
return false
end
for j = k + 1:N
if exps[j, 1] != UInt(0)
return false
end
end
return true
end
end
return false
end
function is_gen(x::MPoly{T}, ::Val{:deglex}) where {T <: RingElement}
N = size(x.exps, 1)
return x.exps[N, 1] == UInt(1)
end
function is_gen(x::MPoly{T}, ::Val{:degrevlex}) where {T <: RingElement}
N = size(x.exps, 1)
return x.exps[N, 1] == UInt(1)
end
@doc raw"""
is_gen(x::MPoly{T}) where {T <: RingElement}
Return `true` if the given polynomial is a generator (variable) of the
polynomial ring it belongs to.
"""
function is_gen(x::MPoly{T}) where {T <: RingElement}
if length(x) != 1
return false
end
if !isone(coeff(x, 1))
return false
end
return is_gen(x, Val(internal_ordering(parent(x))))
end
@doc raw"""
is_homogeneous(x::MPoly{T}) where {T <: RingElement}
Return `true` if the given polynomial is homogeneous with respect to the standard grading and `false` otherwise.
"""
function is_homogeneous(x::MPoly{T}) where {T <: RingElement}
last_deg = 0
is_first = true
for e in exponent_vectors(x)
d = sum(e)
if !is_first
if d != last_deg
return false
else
last_deg = d
end
else
is_first = false
last_deg = d
end
end
return true
end
@doc raw"""
coeff(x::MPoly, i::Int)
Return the coefficient of the $i$-th term of the polynomial.
"""
function coeff(x::MPoly, i::Int)
return x.coeffs[i]
end
function trailing_coefficient(p::MPoly{T}) where T <: RingElement
if iszero(p)
return zero(base_ring(p))
else
return coeff(p, length(p))
end
end
@doc raw"""
monomial(x::MPoly, i::Int)
Return the monomial of the $i$-th term of the polynomial (as a polynomial
of length $1$ with coefficient $1$).
"""
function monomial(x::MPoly, i::Int)
R = base_ring(x)
N = size(x.exps, 1)
exps = Matrix{UInt}(undef, N, 1)
monomial_set!(exps, 1, x.exps, i, N)
return parent(x)([one(R)], exps)
end
@doc raw"""
monomial!(m::Mpoly{T}, x::MPoly{T}, i::Int) where T <: RingElement
Set $m$ to the monomial of the $i$-th term of the polynomial (as a
polynomial of length $1$ with coefficient $1$.
"""
function monomial!(m::MPoly{T}, x::MPoly{T}, i::Int) where T <: RingElement
N = size(x.exps, 1)
fit!(m, 1)
monomial_set!(m.exps, 1, x.exps, i, N)
m.coeffs[1] = one(base_ring(x))
m.length = 1
return m
end
@doc raw"""
term(x::MPoly, i::Int)
Return the $i$-th nonzero term of the polynomial $x$ (as a polynomial).
"""
function term(x::MPoly, i::Int)
R = base_ring(x)
N = size(x.exps, 1)
exps = Matrix{UInt}(undef, N, 1)
monomial_set!(exps, 1, x.exps, i, N)
return parent(x)([deepcopy(x.coeffs[i])], exps)
end
@doc raw"""
max_fields(f::MPoly{T}) where {T <: RingElement}
Return a tuple `(degs, biggest)` consisting of an array `degs` of the maximum
exponent for each field in the exponent vectors of `f` and an integer which
is the largest of the entries in `degs`. The array `degs` will have `n + 1`
entries in the case of a degree ordering, or `n` otherwise, where `n` is the
number of variables of the polynomial ring `f` belongs to. The fields are
returned in the order they exist in the internal representation (which is not
intended to be specified, and not needed for current applications).
"""
function max_fields(f::MPoly{T}) where {T <: RingElement}
A = f.exps
N = size(A, 1)
if N == 0
return Int[], 0
end
biggest = zeros(Int, N)
for i = 1:length(f)
for k = 1:N
if reinterpret(Int, A[k, i]) > biggest[k]
biggest[k] = reinterpret(Int, A[k, i])
end
end
end
b = biggest[1]
for k = 2:N
if biggest[k] > b
b = biggest[k]
end
end
return biggest, b
end
function degree(f::MPoly{T}, i::Int, ::Val{:lex}) where T <: RingElement
A = f.exps
N = size(A, 1)
if i == 1
return length(f) == 0 ? -1 : Int(A[N, 1])
else
biggest = -1
for j = 1:length(f)
d = Int(A[N - i + 1, j])
if d > biggest
biggest = d
end
end
return biggest
end
end
function degree(f::MPoly{T}, i::Int, ::Val{:deglex}) where T <: RingElement
A = f.exps
N = size(A, 1)
biggest = -1
for j = 1:length(f)
d = Int(A[N - i, j])
if d > biggest
biggest = d
end
end
return biggest
end
function degree(f::MPoly{T}, i::Int, ::Val{:degrevlex}) where T <: RingElement
A = f.exps
N = size(A, 1)
biggest = -1
for j = 1:length(f)
d = Int(A[i, j])
if d > biggest
biggest = d
end
end
return biggest
end
function degree(f::MPoly{T}, i::Int) where T <: RingElement
return degree(f, i, Val(internal_ordering(parent(f))))
end
@doc raw"""
total_degree(f::MPoly{T}) where {T <: RingElement}
Return the total degree of `f`.
"""
function total_degree(f::MPoly{T}) where {T <: RingElement}
A = f.exps
N = size(A, 1)
ord = internal_ordering(parent(f))
if ord == :lex
if N == 1
return length(f) == 0 ? -1 : Int(A[1, N])
end
max_deg = -1
for i = 1:length(f)
sum_deg = 0
for k = 1:N
sum_deg += A[k, i]
sum_deg < A[k, i] && error("Integer overflow in total_degree")
end
if sum_deg > max_deg
max_deg = sum_deg
end
end
return Int(max_deg) # Julia already checks this for overflow
elseif ord == :deglex || ord == :degrevlex
return length(f) == 0 ? -1 : Int(A[N, 1])
else
error("total_degree is not implemented for this ordering.")
end
end
@doc raw"""
length(x::MPoly)
Return the number of terms of the polynomial.
"""
length(x::MPoly) = x.length
isone(x::MPoly) = x.length == 1 && monomial_iszero(x.exps, 1, size(x.exps, 1)) && is_one(x.coeffs[1])
is_constant(x::MPoly) = x.length == 0 || (x.length == 1 && monomial_iszero(x.exps, 1, size(x.exps, 1)))
function Base.deepcopy_internal(a::MPoly{T}, dict::IdDict) where {T <: RingElement}
Re = deepcopy_internal(a.exps, dict)
Rc = Vector{T}(undef, a.length)
for i = 1:a.length
Rc[i] = deepcopy(a.coeffs[i])
end
return parent(a)(Rc, Re)
end
Base.copy(f::Generic.MPoly) = deepcopy(f)
###############################################################################
#
# Iterators
#
###############################################################################
function Base.iterate(x::MPolyCoeffs)
if length(x.poly) >= 1
return coeff(x.poly, 1), 1
else
return nothing
end
end
function Base.iterate(x::MPolyCoeffs, state)
state += 1
if length(x.poly) >= state
return coeff(x.poly, state), state
else
return nothing
end
end
function Base.iterate(x::MPolyExponentVectors)
if length(x.poly) >= 1
return exponent_vector(x.poly, 1), 1
else
return nothing
end
end
function Base.iterate(x::MPolyExponentVectors, state)
state += 1
if length(x.poly) >= state
return exponent_vector(x.poly, state), state
else
return nothing
end
end
function Base.iterate(x::MPolyTerms)
if length(x.poly) >= 1
return term(x.poly, 1), 1
else
return nothing
end
end
function Base.iterate(x::MPolyTerms, state)
state += 1
if length(x.poly) >= state
return term(x.poly, state), state
else
return nothing
end
end
function Base.iterate(x::MPolyMonomials)
if length(x.poly) >= 1
return monomial(x.poly, 1), 1
else
return nothing
end
end
function Base.iterate(x::MPolyMonomials, state)
state += 1
if length(x.poly) >= state
return monomial(x.poly, state), state
else
return nothing
end
end
function Base.length(x::Union{MPolyCoeffs, MPolyExponentVectors, MPolyTerms, MPolyMonomials})
return length(x.poly)
end
function Base.eltype(x::MPolyCoeffs{T}) where T <: AbstractAlgebra.MPolyRingElem{S} where S <: RingElement
return S
end
function Base.eltype(x::MPolyExponentVectors{T}) where T <: AbstractAlgebra.MPolyRingElem{S} where S <: RingElement
return Vector{Int}
end
function Base.eltype(x::MPolyMonomials{T}) where T <: AbstractAlgebra.MPolyRingElem{S} where S <: RingElement
return T
end
function Base.eltype(x::MPolyTerms{T}) where T <: AbstractAlgebra.MPolyRingElem{S} where S <: RingElement
return T
end
###############################################################################
#
# Geobuckets
#
###############################################################################
mutable struct geobucket{T}
len::Int
buckets::Vector{T}
function geobucket(R::Ring)
return new{elem_type(R)}(1, [R(), R()])
end
end
function Base.push!(G::geobucket{T}, p::T) where T
R = parent(p)
i = max(1, ndigits(length(p), base=4))
l = length(G.buckets)
if length(G.buckets) < i
resize!(G.buckets, i)
for j in (l + 1):i
G.buckets[j] = zero(R)
end
end
G.buckets[i] = add!(G.buckets[i], p)
while i <= G.len
if length(G.buckets[i]) >= 4^i
G.buckets[i + 1] = add!(G.buckets[i + 1], G.buckets[i])
G.buckets[i] = R()
i += 1
end
break
end
if i == G.len + 1
Base.push!(G.buckets, R())
G.len += 1
end
end
function finish(G::geobucket{T}) where T
p = G.buckets[1]
for i = 2:length(G.buckets)
p = add!(p, G.buckets[i])
end
return p::T
end
###############################################################################
#
# Arithmetic functions