-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmpz_poly.h
1608 lines (1238 loc) · 48.2 KB
/
fmpz_poly.h
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
/*============================================================================
This file is part of FLINT.
FLINT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
FLINT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===============================================================================*/
/****************************************************************************
fmpz_poly.h: Polynomials over Z, implemented as contiguous block of fmpz_t's
Copyright (C) 2007, William Hart and David Harvey
Copyright (C) 2010, William Hart
*****************************************************************************/
#ifndef FLINT_FMPZ_POLY_H
#define FLINT_FMPZ_POLY_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
#include "mpn_extras.h"
#include "fmpz.h"
#include "zmod_poly.h"
/****************************************************************************
fmpz_poly_t
-----------
fmpz_poly_t represents a dense polynomial in Z[x] using a single block of
memory to hold all the coefficients.
This type is better suited to handling very dense polynomials with relatively
small coefficients, where the memory management overhead of Zpoly_t would
be too expensive.
"coeffs" is an array of limbs of length (alloc * (limbs+1)). Each
coefficient uses limbs+1 limbs. For each coefficient, the first limb is
a sign/size limb: the number of limbs of the absolute value of the
coefficient is given by the absolute value of this limb, and the sign
of this limb is the sign of the coefficient. (Zero is stored as a
sign/size of zero followed by arbitrary data.) The remaining "limbs"
limbs represent the absolute value of the coefficient, stored in
GMP's mpn format.
Only the first "length" coefficients actually represent coefficients
of the polynomial; i.e. it's a polynomial of degree at most length-1.
If length == 0, this is the zero polynomial. All functions normalise
so that the (length-1)-th coefficient is non-zero.
Obviously always alloc >= length.
There are two classes of functions operating on fmpz_poly_t:
-- The _fmpz_poly_* functions NEVER free or reallocate "coeffs", so they
don't care how "coeffs" was allocated, and they never even look at the
"alloc" attribute. They always assume the output has enough space for
the result. They also NEVER modify the limbs attribute (since this
would screw up the block size).
-- The fmpz_poly_* functions ASSUME that "coeffs" was allocated via
flint_heap_alloc, and they MAY free or reallocate "coeffs" using
flint_heap_realloc, flint_heap_free etc, whenever they feel the need.
*/
typedef struct
{
mp_limb_t* coeffs;
unsigned long alloc;
unsigned long length;
unsigned long limbs;
} fmpz_poly_struct;
// fmpz_poly_t allows reference-like semantics for fmpz_poly_struct:
typedef fmpz_poly_struct fmpz_poly_t[1];
typedef fmpz_poly_struct * fmpz_poly_p;
/*
This struct encapsulates an array of factors with the given exponents.
The current number of allocated slots in the struct is given by alloc.
The number of slots occupied is given by num_factors.
*/
typedef struct
{
fmpz_poly_t * factors;
unsigned long * exponents;
unsigned long alloc;
unsigned long num_factors;
} fmpz_poly_factor_struct;
// allows call by reference-like semantics for fmpz_poly_factor_struct
typedef fmpz_poly_factor_struct fmpz_poly_factor_t[1];
#define SWAP(x_dummy, y_dummy) \
do { \
fmpz_poly_p swap_temp = x_dummy; \
x_dummy = y_dummy; \
y_dummy = swap_temp; \
} while(0);
#define SWAP_PTRS(x_dummy_p, y_dummy_p) \
do { \
fmpz_t swap_temp_p = x_dummy_p; \
x_dummy_p = y_dummy_p; \
y_dummy_p = swap_temp_p; \
} while(0);
/****************************************************************************
Conversion Routines
****************************************************************************/
/*
Converts fmpz_poly_t "poly_fmpz" to a ZmodF_poly.
Each coefficient of poly_fmpz is assumed to fit into a coefficient
of poly_f.
The maximum number of *bits* that any coefficient takes is returned
and made negative if any of the coefficients was negative.
Only _length_ coefficients are converted.
Assumes 0 < length <= poly_fmpz->length
*/
long fmpz_poly_to_ZmodF_poly(ZmodF_poly_t poly_f, const fmpz_poly_t poly_fmpz,
const unsigned long length);
/*
Normalise and converts ZmodF_poly "poly_f" to a fmpz_poly_t.
Each coefficient of poly_f is assumed to fit into a coefficient
of poly_fmpz.
The normalisation ensures that this function is the inverse of
ZmodF_poly_convert_in_mpn.
Assumes 0 < poly_f->length
*/
void ZmodF_poly_to_fmpz_poly(fmpz_poly_t poly_fmpz, const ZmodF_poly_t poly_f, const long sign);
/*
Packs poly_fmpz->length coefficients, each assumed to fit into a limb, into
the array of limbs coeffs_f. Coefficients are stored in B-adic symmetric
format. The neg variant changes the sign of the coefficients before packing
(the input polynomial is not modified).
Assumes 0 < poly_fmpz->length
*/
void fmpz_poly_limb_pack_1(mp_limb_t * array, const fmpz_poly_t poly_fmpz);
void fmpz_poly_limb_pack_neg_1(mp_limb_t * array, const fmpz_poly_t poly_fmpz);
/*
Unpacks length signed coefficients from the array, each assumed to be
stored in precisely one limb, into the polynomial poly_fmpz.
Coefficients are assumed to be stored in B-adic symmetric format.
Assumes 0 < length <= poly_f->length
*/
void fmpz_poly_limb_unpack_1(fmpz_poly_t poly_fmpz, const mp_limb_t * array,
const unsigned long length);
/*
Packs length coefficients of poly_fmpz into the array into fields with the
given number of limbs. The neg variant negates the coefficients.
Assumes 0 < length.
*/
void fmpz_poly_limb_pack(mp_limb_t * array, const fmpz_poly_t poly_fmpz,
const unsigned long length, const long limbs);
void fmpz_poly_limb_pack_neg(mp_limb_t * array, const fmpz_poly_t poly_fmpz,
const unsigned long length, const long limbs);
/*
Unpacks length signed coefficients from the array, each assumed to be
stored in a field of the given number of limbs, into the polynomial
poly_fmpz. Coefficients are assumed to be stored in B-adic symmetric
format.
Assumes 0 < length <= poly_f->length
*/
void fmpz_poly_limb_unpack(fmpz_poly_t poly_fmpz, mp_limb_t * array,
const unsigned long length, const unsigned long limbs);
/*
Unpacks bundle coefficients from the given array, each assumed to be
stored in a field of the given number of limbs. Assumes the coefficients
are unsigned.
Assumes 0 < bundle <= poly_f->length
*/
void fmpz_poly_limb_unpack_unsigned(fmpz_poly_t poly_fmpz, const mp_limb_t * array,
const unsigned long length, const unsigned long limbs);
/*
Packs length coefficients of poly_fmpz down to the bit into the given
array. Each coefficient is packed into a bitfield "bits" bits wide
including one bit for a sign bit.
"bits" is assumed to be less than FLINT_BITS. If bits is
negative, the input poly is assumed to have signed coefficients.
Assumes 0 < bundle and 0 < poly_fmpz->length
*/
void fmpz_poly_bit_pack(mp_limb_t * array, const fmpz_poly_t poly_fmpz,
const unsigned long length, const long bits, const long negate);
/*
Unpacks the given array into poly_fmpz. This is the inverse of fmpz_poly_bit_pack,
however array must have one more (zero) limb than is necessary to store all the
packed coefficients.
The total number of coefficients to be unpacked is given by length. One must
ensure each of the coefficients of poly_fmpz are set to zero before calling
this function for the first time since it adds to existing coefficients of
poly_fmpz, rather than overwriting them.
The unsigned variant assumes the coefficients are unsigned, otherwise the bitfield
is assumed to be one bit wider to allow for a sign bit.
"bits" is assumed to be less than FLINT_BITS.
Assumes 0 < bundle and 0 < poly_f->length
*/
void fmpz_poly_bit_unpack(fmpz_poly_t poly_fmpz, const mp_limb_t * array,
const unsigned long length, const unsigned long bits);
void fmpz_poly_bit_unpack_unsigned(fmpz_poly_t poly_fmpz, const mp_limb_t * array,
const unsigned long length, const unsigned long bits);
/*
Packs coefficients of poly_fmpz down to the byte into array, each packed
into a field "bytes" bytes wide.
"coeff_bytes" is assumed to be at least FLINT_BITS/8, i.e. the
coefficients are assumed to be at least a limb wide.
Assumes 0 < length and 0 < poly_fmpz->length
*/
void fmpz_poly_byte_pack(mp_limb_t * array, const fmpz_poly_t poly_fmpz,
const unsigned long length, const unsigned long coeff_bytes,
const long negate);
/*
Unpacks coefficients from array into poly_fmpz. Each coefficient stored is
assumed to be packed into a field "bytes" bytes wide, with one bit reserved
for a sign bit except for the unsigned variant of the function where the
coefficients are assumed to be unsigned.
It is also assumed that array has one extra (zero) limb beyond what is
required to store the packed coefficients.
The total number of coefficients to be unpacked is given by length.
"coeff_bytes" is assumed to be at least FLINT_BITS/8, i.e. the
coefficients are assumed to be at least a limb wide.
Assumes 0 < length.
*/
void fmpz_poly_byte_unpack_unsigned(fmpz_poly_t poly_m, const mp_limb_t * array,
const unsigned long length, const unsigned long bytes);
void fmpz_poly_byte_unpack(fmpz_poly_t poly_m, const mp_limb_t * array,
const unsigned long length, const unsigned long bytes);
void fmpz_poly_pack_bytes(fmpz_poly_t res, fmpz_poly_t poly, ulong n, ulong bytes);
void fmpz_poly_unpack_bytes(fmpz_poly_t res, fmpz_poly_t poly, ulong n, ulong bytes);
/*
Reduce coefficients of the given fmpz_poly fpol, modulo the modulus of the given
zmod_poly zpol, and store the result in zpol.
*/
void fmpz_poly_to_zmod_poly(zmod_poly_t zpol, const fmpz_poly_t fpol);
void fmpz_poly_to_zmod_poly_no_red(zmod_poly_t zpol, const fmpz_poly_t fpol);
/*
Store the unsigned long coefficients of the zmod_poly zpol in the given fmpz_poly fpol.
The unsigned version normalised to [0, p) the other version to [-(p-1)/2, p/2]
*/
void zmod_poly_to_fmpz_poly(fmpz_poly_t fpol, zmod_poly_t zpol);
void zmod_poly_to_fmpz_poly_unsigned(fmpz_poly_t fpol, zmod_poly_t zpol);
/*
Given an fmpz_poly_t fpol representing the reduction modulo oldmod of a polynomial
and a zmod_poly zpol with modulus p, use Chinese remaindering to reconstruct the
polynomial modulo newmod, with newmod = p*oldmod, where each new coefficient fpol[i]
is set to the unique non-negative integer in [0, newmod) which is fpol[i] mod oldmod
and zpol[i] mod p.
Assumes p and oldmod are coprime.
Returns 1 if the CRT has stabilised, i.e. if the new output equals the old input
else it returns 0.
The unsigned version normalises the output to [0, newmod) the main version
normalises to [-nemod/2, newmod/2].
Allows aliasing of fpol and res.
*/
int fmpz_poly_CRT(fmpz_poly_t res, fmpz_poly_t fpol, zmod_poly_t zpol, fmpz_t newmod, fmpz_t oldmod);
int fmpz_poly_CRT_unsigned(fmpz_poly_t res, fmpz_poly_t fpol, zmod_poly_t zpol, fmpz_t newmod, fmpz_t oldmod);
/*============================================================================
Functions in _fmpz_poly_* layer
===============================================================================*/
void _fmpz_poly_stack_init(fmpz_poly_t poly, const unsigned long alloc, const unsigned long limbs);
void _fmpz_poly_stack_clear(fmpz_poly_t poly);
void _fmpz_poly_check(const fmpz_poly_t poly);
void _fmpz_poly_normalise(fmpz_poly_t poly);
void _fmpz_poly_check_normalisation(const fmpz_poly_t poly);
static inline
fmpz_t _fmpz_poly_get_coeff_ptr(const fmpz_poly_t poly, const unsigned long n)
{
return poly->coeffs+n*(poly->limbs+1);
}
static inline
fmpz_t _fmpz_poly_lead(const fmpz_poly_t poly)
{
if (poly->length == 0) return NULL;
return poly->coeffs+(poly->length-1)*(poly->limbs+1);
}
static inline
fmpz_t fmpz_poly_lead(const fmpz_poly_t poly)
{
if (poly->length == 0) return NULL;
return poly->coeffs+(poly->length-1)*(poly->limbs+1);
}
/*
Set "output" to the given coefficient and return the sign
Assumes length of output is poly->limbs limbs long.
*/
static inline
long _fmpz_poly_get_coeff(mp_limb_t * output, const fmpz_poly_t poly,
const unsigned long n)
{
F_mpn_clear(output, poly->limbs);
if (poly->coeffs[n*(poly->limbs+1)] != 0L)
{
F_mpn_copy(output, poly->coeffs+n*(poly->limbs+1)+1, ABS(poly->coeffs[n*(poly->limbs+1)]));
}
return poly->coeffs[n*(poly->limbs+1)];
}
static inline
unsigned long _fmpz_poly_get_coeff_ui(fmpz_poly_t poly, const unsigned long n)
{
if (n >= poly->length) return 0L;
if (poly->coeffs[n*(poly->limbs+1)] == 0L) return 0L;
else return poly->coeffs[n*(poly->limbs+1)+1];
}
static inline
long _fmpz_poly_get_coeff_si(fmpz_poly_t poly, const unsigned long n)
{
if (n >= poly->length) return 0L;
if (poly->coeffs[n*(poly->limbs+1)] == 0L) return 0L;
if ((long) poly->coeffs[n*(poly->limbs+1)] > 0L)
return poly->coeffs[n*(poly->limbs+1)+1];
else return -poly->coeffs[n*(poly->limbs+1)+1];
}
void _fmpz_poly_get_coeff_mpz(mpz_t x, const fmpz_poly_t poly, const unsigned long n);
void _fmpz_poly_get_coeff_mpz_read_only(mpz_t x, const fmpz_poly_t poly, const unsigned long n);
static inline
void _fmpz_poly_set_coeff_mpz(fmpz_poly_t poly, const unsigned long n, const mpz_t x)
{
if (poly->limbs == 0) return;
if (n+1 > poly->length)
{
long i;
for (i = poly->length; i + 1 < n; i++)
{
poly->coeffs[i*(poly->limbs+1)] = 0;
}
poly->length = n+1;
}
mpz_to_fmpz(poly->coeffs + n*(poly->limbs+1), x);
_fmpz_poly_normalise(poly);
}
void _fmpz_poly_set_coeff_fmpz(fmpz_poly_t poly, const unsigned long n, fmpz_t x);
void _fmpz_poly_get_coeff_fmpz(fmpz_t x, const fmpz_poly_t poly, const unsigned long n);
/*
Set a coefficient to the given value having "size" limbs.
Assumes that the poly->limbs is at least "size" and
that n < poly->length
*/
static inline
void _fmpz_poly_set_coeff(fmpz_poly_t poly, const unsigned long n,
const mp_limb_t * x, const long sign, const unsigned long size)
{
FLINT_ASSERT(poly->limbs >= size);
F_mpn_copy(poly->coeffs+n*(poly->limbs+1)+1, x, size);
poly->coeffs[n*(poly->limbs+1)] = sign;
if (poly->limbs > size)
F_mpn_clear(poly->coeffs+n*(poly->limbs+1)+size+1, poly->limbs-size);
_fmpz_poly_normalise(poly);
}
void _fmpz_poly_set_coeff_ui(fmpz_poly_t poly, const unsigned long n, const unsigned long x);
void _fmpz_poly_set_coeff_si(fmpz_poly_t poly, const unsigned long n, const long x);
static inline
long _fmpz_poly_degree(const fmpz_poly_t poly)
{
return poly->length - 1;
}
static inline
unsigned long _fmpz_poly_length(const fmpz_poly_t poly)
{
return poly->length;
}
static inline
unsigned long _fmpz_poly_limbs(const fmpz_poly_t poly)
{
return poly->limbs;
}
void _fmpz_poly_set(fmpz_poly_t output, const fmpz_poly_t input);
/*
Zero the polynomial by setting the length to zero.
Does not set the actual limbs to zero.
*/
static inline
void _fmpz_poly_zero(fmpz_poly_t output)
{
output->length = 0;
}
/* Zero first n coefficients of poly, regardless of what length is */
void _fmpz_poly_zero_coeffs(fmpz_poly_t poly, const unsigned long n);
static inline
void _fmpz_poly_attach(fmpz_poly_t output, const fmpz_poly_t input)
{
output->length = input->length;
output->limbs = input->limbs;
output->coeffs = input->coeffs;
}
/*
Attach input shifted right by n to output
*/
static inline
void _fmpz_poly_attach_shift(fmpz_poly_t output,
const fmpz_poly_t input, unsigned long n)
{
if (input->length >= n) output->length = input->length - n;
else output->length = 0;
output->limbs = input->limbs;
output->coeffs = input->coeffs + n*(input->limbs+1);
}
/*
Attach input to first n coefficients of input
*/
static inline
void _fmpz_poly_attach_truncate(fmpz_poly_t output,
const fmpz_poly_t input, unsigned long n)
{
if (input->length < n) output->length = input->length;
else output->length = n;
output->limbs = input->limbs;
output->coeffs = input->coeffs;
_fmpz_poly_normalise(output);
}
long _fmpz_poly_max_bits1(const fmpz_poly_t poly);
long _fmpz_poly_max_bits(const fmpz_poly_t poly);
unsigned long _fmpz_poly_max_limbs(const fmpz_poly_t poly);
int _fmpz_poly_equal(const fmpz_poly_t input1, const fmpz_poly_t input2);
void _fmpz_poly_neg(fmpz_poly_t output, const fmpz_poly_t input);
void _fmpz_poly_scalar_abs(fmpz_poly_t output, const fmpz_poly_t input);
void _fmpz_poly_truncate(fmpz_poly_t poly, const unsigned long trunc);
void _fmpz_poly_reverse(fmpz_poly_t output, const fmpz_poly_t input, const unsigned long length);
void _fmpz_poly_left_shift(fmpz_poly_t output, const fmpz_poly_t input,
const unsigned long n);
void _fmpz_poly_right_shift(fmpz_poly_t output, const fmpz_poly_t input, const unsigned long n);
void _fmpz_poly_add(fmpz_poly_t output, const fmpz_poly_t input1, const fmpz_poly_t input2);
void _fmpz_poly_sub(fmpz_poly_t output, const fmpz_poly_t input1, const fmpz_poly_t input2);
void _fmpz_poly_scalar_mul_fmpz(fmpz_poly_t output, const fmpz_poly_t poly, const fmpz_t x);
void _fmpz_poly_scalar_mul_ui(fmpz_poly_t output, const fmpz_poly_t poly, const unsigned long x);
void _fmpz_poly_scalar_mul_si(fmpz_poly_t output, const fmpz_poly_t poly, const long x);
void _fmpz_poly_scalar_div_fmpz(fmpz_poly_t output, const fmpz_poly_t poly, const fmpz_t x);
void _fmpz_poly_scalar_tdiv_ui(fmpz_poly_t output, const fmpz_poly_t poly, const unsigned long x);
void _fmpz_poly_scalar_div_ui(fmpz_poly_t output, const fmpz_poly_t poly, const unsigned long x);
void _fmpz_poly_scalar_div_si(fmpz_poly_t output, const fmpz_poly_t poly, const long x);
void _fmpz_poly_scalar_tdiv_si(fmpz_poly_t output, const fmpz_poly_t poly, const long x);
void _fmpz_poly_scalar_div_exact_ui(fmpz_poly_t output, const fmpz_poly_t poly, const unsigned long x);
void _fmpz_poly_scalar_div_exact_si(fmpz_poly_t output, const fmpz_poly_t poly, const long x);
void _fmpz_poly_mul_classical(fmpz_poly_t output, const fmpz_poly_t input1,
const fmpz_poly_t input2);
void _fmpz_poly_mul_classical_trunc(fmpz_poly_t output, const fmpz_poly_t input1,
const fmpz_poly_t input2, const unsigned long trunc);
void _fmpz_poly_mul_classical_trunc_left(fmpz_poly_t output, const fmpz_poly_t input1,
const fmpz_poly_t input2, const unsigned long trunc);
void __fmpz_poly_karamul_recursive(fmpz_poly_t res, const fmpz_poly_t a, const fmpz_poly_t b, fmpz_poly_t scratch, fmpz_poly_t scratchb, const unsigned long crossover);
void _fmpz_poly_mul_karatsuba(fmpz_poly_t output, const fmpz_poly_t input1,
const fmpz_poly_t input2);
void _fmpz_poly_mul_karatsuba_trunc(fmpz_poly_t output, const fmpz_poly_t input1,
const fmpz_poly_t input2, const unsigned long trunc);
void _fmpz_poly_mul_karatsuba_trunc_left(fmpz_poly_t output, const fmpz_poly_t input1, const fmpz_poly_t input2, const unsigned long trunc);
void _fmpz_poly_mul_KS(fmpz_poly_t output, const fmpz_poly_t input1,
const fmpz_poly_t input2, const long bits);
void _fmpz_poly_mul_KS_trunc(fmpz_poly_t output, const fmpz_poly_t in1,
const fmpz_poly_t in2, const unsigned long trunc, long bits);
void _fmpz_poly_mul_SS(fmpz_poly_t output, const fmpz_poly_t input1,
const fmpz_poly_t input2);
void _fmpz_poly_mul_SS_trunc(fmpz_poly_t output, const fmpz_poly_t input1,
const fmpz_poly_t input2, const unsigned long trunc);
void _fmpz_poly_mul_trunc_n(fmpz_poly_t output, const fmpz_poly_t input1,
const fmpz_poly_t input2, const unsigned long trunc);
void _fmpz_poly_mul_trunc_left_n(fmpz_poly_t output, const fmpz_poly_t input1,
const fmpz_poly_t input2, const unsigned long trunc);
void _fmpz_poly_mul(fmpz_poly_t output, const fmpz_poly_t input1, const fmpz_poly_t input2);
/*============================================================================
Functions in fmpz_poly_* layer
===============================================================================*/
void fmpz_poly_init(fmpz_poly_t poly);
void fmpz_poly_init2(fmpz_poly_t poly, const unsigned long alloc, const unsigned long limbs);
void fmpz_poly_realloc(fmpz_poly_t poly, const unsigned long alloc);
void fmpz_poly_fit_length(fmpz_poly_t poly, const unsigned long alloc);
void fmpz_poly_resize_limbs(fmpz_poly_t poly, const unsigned long limbs);
static inline void fmpz_poly_fit_limbs(fmpz_poly_t poly, const unsigned long limbs)
{
if ((long) limbs > (long) poly->limbs) fmpz_poly_resize_limbs(poly, limbs);
}
void fmpz_poly_clear(fmpz_poly_t poly);
void fmpz_poly_check(const fmpz_poly_t poly);
void fmpz_poly_check_normalisation(const fmpz_poly_t poly);
void fmpz_poly_factor_init(fmpz_poly_factor_t fac);
void fmpz_poly_factor_clear(fmpz_poly_factor_t fac);
// ------------------------------------------------------
// fmpz_poly_factor_t
void fmpz_poly_factor_insert(fmpz_poly_factor_t fac, fmpz_poly_t poly, ulong exp);
void fmpz_poly_factor_print(fmpz_poly_factor_t fac);
// ------------------------------------------------------
// String conversions and I/O
int fmpz_poly_from_string(fmpz_poly_t poly, const char* s);
char* fmpz_poly_to_string(const fmpz_poly_t poly);
void fmpz_poly_print(const fmpz_poly_t poly);
void fmpz_poly_fprint(const fmpz_poly_t poly, FILE* f);
int fmpz_poly_fread(fmpz_poly_t poly, FILE* f);
char* fmpz_poly_to_string_pretty(const fmpz_poly_t poly, const char * x);
void fmpz_poly_fprint_pretty(const fmpz_poly_t poly, FILE* f, const char * x);
void fmpz_poly_print_pretty(const fmpz_poly_t poly, const char * x);
static inline
int fmpz_poly_read(fmpz_poly_t poly)
{
return fmpz_poly_fread(poly, stdin);
}
static inline
unsigned long fmpz_poly_limbs(const fmpz_poly_t poly)
{
return poly->limbs;
}
static inline
long fmpz_poly_degree(const fmpz_poly_t poly)
{
return poly->length - 1;
}
static inline
unsigned long fmpz_poly_length(const fmpz_poly_t poly)
{
return poly->length;
}
static inline
long fmpz_poly_max_bits1(const fmpz_poly_t poly)
{
return _fmpz_poly_max_bits1(poly);
}
static inline
long fmpz_poly_max_bits(const fmpz_poly_t poly)
{
return _fmpz_poly_max_bits(poly);
}
static inline
long fmpz_poly_max_limbs(const fmpz_poly_t poly)
{
return _fmpz_poly_max_limbs(poly);
}
static inline
void fmpz_poly_truncate(fmpz_poly_t poly, const unsigned long length)
{
poly->length = FLINT_MIN(length, poly->length);
_fmpz_poly_normalise(poly);
}
static inline
void fmpz_poly_swap(fmpz_poly_t x, fmpz_poly_t y)
{
if (x == y) return;
fmpz_t temp_p;
mp_limb_t temp_l;
temp_l = x->alloc;
x->alloc = y->alloc;
y->alloc = temp_l;
temp_p = x->coeffs;
x->coeffs = y->coeffs;
y->coeffs = temp_p;
temp_l = x->length;
x->length = y->length;
y->length = temp_l;
temp_l = x->limbs;
x->limbs = y->limbs;
y->limbs = temp_l;
}
static inline
fmpz_t fmpz_poly_get_coeff_ptr(const fmpz_poly_t poly, const unsigned long n)
{
if (n >= poly->length)
{
return NULL;
}
return poly->coeffs+n*(poly->limbs+1);
}
static inline
long fmpz_poly_get_coeff(mp_limb_t * output, const fmpz_poly_t poly,
const unsigned long n)
{
if (n >= poly->length)
{
F_mpn_clear(output, poly->limbs);
return 0;
}
return _fmpz_poly_get_coeff(output, poly, n);
}
static inline
unsigned long fmpz_poly_get_coeff_ui(const fmpz_poly_t poly, const unsigned long n)
{
if (n >= poly->length)
{
return 0;
}
if (poly->coeffs[n*(poly->limbs+1)] == 0) return 0;
else return poly->coeffs[n*(poly->limbs+1)+1];
}
static inline
long fmpz_poly_get_coeff_si(const fmpz_poly_t poly, const unsigned long n)
{
if (n >= poly->length)
{
return 0;
}
if (poly->coeffs[n*(poly->limbs+1)] == 0) return 0;
if ((long) poly->coeffs[n*(poly->limbs+1)] >= 1L)
return poly->coeffs[n*(poly->limbs+1)+1];
else return -poly->coeffs[n*(poly->limbs+1)+1];
}
void fmpz_poly_get_coeff_mpz(mpz_t x, const fmpz_poly_t poly, const unsigned long n);
void fmpz_poly_get_coeff_mpz_read_only(mpz_t x, const fmpz_poly_t poly, const unsigned long n);
static inline
void fmpz_poly_set_coeff_fmpz(fmpz_poly_t poly, const unsigned long n, fmpz_t x)
{
if ((poly->length == 0) && (x[0] == 0)) return;
fmpz_poly_fit_length(poly, n+1);
fmpz_poly_fit_limbs(poly, fmpz_size(x));
if (n+1 > poly->length)
{
long i;
for (i = poly->length; i < n; i++)
{
poly->coeffs[i*(poly->limbs+1)] = 0L;
}
poly->length = n+1;
}
_fmpz_poly_set_coeff_fmpz(poly, n, x);
_fmpz_poly_normalise(poly);
}
static inline
void fmpz_poly_get_coeff_fmpz(fmpz_t x, const fmpz_poly_t poly, const unsigned long n)
{
if (n >= poly->length)
{
x[0] = 0;
return;
}
_fmpz_poly_get_coeff_fmpz(x, poly, n);
}
static inline
void fmpz_poly_set_coeff(fmpz_poly_t poly, const unsigned long n,
const mp_limb_t * x, const long sign, const unsigned long size)
{
if ((poly->length == 0) && (size == 0)) return;
fmpz_poly_fit_length(poly, n+1);
fmpz_poly_fit_limbs(poly, size);
if (n+1 > poly->length)
{
long i;
for (i = poly->length; i + 1 < n; i++)
{
poly->coeffs[i*(poly->limbs+1)] = 0L;
}
poly->length = n+1;
}
_fmpz_poly_set_coeff(poly, n, x, sign, size);
_fmpz_poly_normalise(poly);
}
static inline
void fmpz_poly_set_coeff_si(fmpz_poly_t poly, const unsigned long n, const long x)
{
if ((poly->length == 0) && (x == 0)) return;
fmpz_poly_fit_length(poly, n+1);
fmpz_poly_fit_limbs(poly, 1);
if (n+1 > poly->length)
{
long i;
for (i = poly->length; i < n; i++)
{
poly->coeffs[i*(poly->limbs+1)] = 0L;
}
poly->length = n+1;
}
_fmpz_poly_set_coeff_si(poly, n, x);
_fmpz_poly_normalise(poly);
}
static inline
void fmpz_poly_set_coeff_ui(fmpz_poly_t poly, const unsigned long n, const unsigned long x)
{
if ((poly->length == 0) && (x == 0)) return;
fmpz_poly_fit_length(poly, n+1);
fmpz_poly_fit_limbs(poly, 1);
if (n+1 > poly->length)
{
long i;
for (i = poly->length; i < n; i++)
{
poly->coeffs[i*(poly->limbs+1)] = 0L;
}
poly->length = n+1;
}
_fmpz_poly_set_coeff_ui(poly, n, x);
_fmpz_poly_normalise(poly);
}
static inline
void fmpz_poly_set_coeff_mpz(fmpz_poly_t poly, const unsigned long n, const mpz_t x)
{
if ((poly->length == 0) && (mpz_size(x) == 0)) return;
fmpz_poly_fit_length(poly, n+1);
fmpz_poly_fit_limbs(poly, mpz_size(x));
if (n+1 > poly->length)
{
long i;
for (i = poly->length; i < n; i++)
{
poly->coeffs[i*(poly->limbs+1)] = 0L;
}
poly->length = n+1;
}
_fmpz_poly_set_coeff_mpz(poly, n, x);
}
static inline
void fmpz_poly_set(fmpz_poly_t output, const fmpz_poly_t input)
{
fmpz_poly_fit_length(output, input->length);
fmpz_poly_fit_limbs(output, input->limbs);
_fmpz_poly_set(output, input);
}
static inline
int fmpz_poly_equal(const fmpz_poly_t input1, const fmpz_poly_t input2)
{
return _fmpz_poly_equal(input1, input2);
}
static inline
void fmpz_poly_zero(fmpz_poly_t output)
{
output->length = 0;
}
static inline
void fmpz_poly_zero_coeffs(fmpz_poly_t poly, const unsigned long n)
{
fmpz_poly_fit_length(poly, n);
if (n >= poly->length)
{
fmpz_poly_zero(poly);
return;
}
_fmpz_poly_zero_coeffs(poly, n);
}
static inline
void fmpz_poly_neg(fmpz_poly_t output, const fmpz_poly_t input)
{
fmpz_poly_fit_length(output, input->length);
fmpz_poly_fit_limbs(output, input->limbs);
_fmpz_poly_neg(output, input);
}
static inline
void fmpz_poly_scalar_abs(fmpz_poly_t output, const fmpz_poly_t input)
{
fmpz_poly_fit_length(output, input->length);
fmpz_poly_fit_limbs(output, input->limbs);
_fmpz_poly_scalar_abs(output, input);
}
static inline
void fmpz_poly_reverse(fmpz_poly_t output, const fmpz_poly_t input, const unsigned long length)
{
fmpz_poly_fit_length(output, length);
fmpz_poly_fit_limbs(output, input->limbs);
_fmpz_poly_reverse(output, input, length);
}
static inline
void fmpz_poly_left_shift(fmpz_poly_t output, const fmpz_poly_t input,
const unsigned long n)
{
if (input->length + n == 0)
{
fmpz_poly_zero(output);
return;
}
fmpz_poly_fit_length(output, input->length + n);
fmpz_poly_fit_limbs(output, input->limbs);
_fmpz_poly_left_shift(output, input, n);
}
static inline
void fmpz_poly_right_shift(fmpz_poly_t output, const fmpz_poly_t input, const unsigned long n)
{
if ((long)(input->length - n) <= 0L)
{
fmpz_poly_zero(output);
return;
}
fmpz_poly_fit_length(output, input->length - n);
fmpz_poly_fit_limbs(output, input->limbs);
_fmpz_poly_right_shift(output, input, n);
}
void fmpz_poly_2norm(fmpz_t norm, const fmpz_poly_t pol);
void fmpz_poly_scalar_mul_fmpz(fmpz_poly_t output,
const fmpz_poly_t input, const fmpz_t x);
void fmpz_poly_scalar_mul_ui(fmpz_poly_t output,
const fmpz_poly_t input, unsigned long x);
void fmpz_poly_scalar_mul_si(fmpz_poly_t output,
const fmpz_poly_t input, long x);
void fmpz_poly_scalar_mul_mpz(fmpz_poly_t output,
const fmpz_poly_t input, const mpz_t x);
static inline
void fmpz_poly_scalar_div_ui(fmpz_poly_t output, const fmpz_poly_t poly, const unsigned long x)
{