-
Notifications
You must be signed in to change notification settings - Fork 0
/
F_mpz_poly.c
9221 lines (7440 loc) · 254 KB
/
F_mpz_poly.c
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
/*============================================================================
F_mpz_poly.c: Polynomials over Z (FLINT 2.0 polynomials)
Copyright (C) 2007, David Harvey (Odd/even Karatsuba)
Copyright (C) 2008, 2009, 2010 William Hart
Copyright (C) 2009, 2010 Andy Novocin
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
===============================================================================*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include "mpz_poly.h"
#include "flint.h"
#include "F_mpz.h"
#include "F_mpz_poly.h"
#include "fmpz_poly.h"
#include "mpn_extras.h"
#include "longlong_wrapper.h"
#include "longlong.h"
#include "memory-manager.h"
#include "ZmodF_poly.h"
#include "long_extras.h"
#include "zmod_poly.h"
#include "F_mpz_mod_poly.h"
#include "F_mpz_mat.h"
#include "F_mpz_LLL.h"
#define POLYPROFILE 0 // whether POLYPROFILE of factor related poly routines is needed
#define CLDPROF 0 // whether POLYPROFILE of CLD computation is wanted
#define WANT_DEFLATION 1 // whether the power hack should be used in factoring
#define TRACE 0 // whether debugging trace should be printed for factoring and related fns
/*===========================================
Some global timing variables
==========================================*/
#if POLYPROFILE
clock_t check_if_solve_start, check_if_solve_stop;
clock_t check_if_solve_total = 0;
clock_t local_factor_start, local_factor_stop;
clock_t local_factor_total = 0;
clock_t lll_start, lll_stop;
clock_t lll_total = 0;
clock_t hensel_start, hensel_stop;
clock_t hensel_total = 0;
clock_t linear_alg_start, linear_alg_stop;
clock_t linear_alg_total = 0;
clock_t cld_data_start, cld_data_stop;
clock_t cld_data_total = 0;
#endif
/*===============================================================================
Memory management
================================================================================*/
void F_mpz_poly_init(F_mpz_poly_t poly)
{
poly->coeffs = NULL;
poly->alloc = 0;
poly->length = 0;
}
void F_mpz_poly_init2(F_mpz_poly_t poly, const ulong alloc)
{
if (alloc) // allocate space for alloc small coeffs
{
poly->coeffs = (F_mpz *) flint_heap_alloc(alloc);
F_mpn_clear(poly->coeffs, alloc);
}
else poly->coeffs = NULL;
poly->alloc = alloc;
poly->length = 0;
}
void F_mpz_poly_realloc(F_mpz_poly_t poly, const ulong alloc)
{
if (!alloc) // alloc == 0, clear up
{
F_mpz_poly_clear(poly);
return;
}
if (poly->alloc) // realloc
{
F_mpz_poly_truncate(poly, alloc);
poly->coeffs = (F_mpz *) flint_heap_realloc(poly->coeffs, alloc);
if (alloc > poly->alloc)
F_mpn_clear(poly->coeffs + poly->alloc, alloc - poly->alloc);
} else // nothing allocated already so do it now
{
poly->coeffs = (mp_limb_t*) flint_heap_alloc(alloc);
F_mpn_clear(poly->coeffs, alloc);
}
poly->alloc = alloc;
}
void F_mpz_poly_fit_length(F_mpz_poly_t poly, const ulong length)
{
ulong alloc = length;
if (alloc <= poly->alloc) return;
// at least double number of allocated coeffs
if (alloc < 2*poly->alloc) alloc = 2*poly->alloc;
F_mpz_poly_realloc(poly, alloc);
}
void F_mpz_poly_clear(F_mpz_poly_t poly)
{
ulong i;
for (i = 0; i < poly->alloc; i++) // Clean up any mpz_t's
_F_mpz_demote(poly->coeffs + i);
if (poly->coeffs) flint_heap_free(poly->coeffs); // clean up ordinary coeffs
}
void F_mpz_poly_factor_init(F_mpz_poly_factor_t fac)
{
fac->alloc = 5;
fac->num_factors = 0;
fac->factors = (F_mpz_poly_t *) flint_heap_alloc_bytes(sizeof(F_mpz_poly_t)*5);
fac->exponents = (unsigned long *) flint_heap_alloc(5);
unsigned long i;
for (i = 0; i < 5; i++)
F_mpz_poly_init(fac->factors[i]);
}
void F_mpz_poly_factor_clear(F_mpz_poly_factor_t fac)
{
unsigned long i;
for (i = 0; i < fac->alloc; i++)
F_mpz_poly_clear(fac->factors[i]);
free(fac->factors);
free(fac->exponents);
}
void F_mpz_poly_factor_pow(F_mpz_poly_factor_t fac, ulong pow)
{
ulong i;
for (i = 0; i < fac->num_factors; i++)
fac->exponents[i] *= pow;
}
/*===============================================================================
F_mpz_poly_factor_t
================================================================================*/
void F_mpz_poly_factor_insert(F_mpz_poly_factor_t fac, F_mpz_poly_t poly, unsigned long exp)
{
if (poly->length <= 1) return;
// if no space left in array, make a new one twice as big (for efficiency)
// and copy contents across
if(fac->alloc == fac->num_factors)
{
fac->factors = (F_mpz_poly_t *) flint_heap_realloc_bytes(fac->factors, sizeof(F_mpz_poly_t)*2*fac->alloc);
fac->exponents = (unsigned long *) flint_heap_realloc(fac->exponents, 2*fac->alloc);
unsigned long i;
for (i = fac->alloc; i < 2*fac->alloc; i++)
F_mpz_poly_init(fac->factors[i]);
fac->alloc = 2*fac->alloc;
}
F_mpz_poly_set(fac->factors[fac->num_factors], poly);
fac->exponents[fac->num_factors] = exp;
fac->num_factors++;
}
void F_mpz_poly_factor_concat(F_mpz_poly_factor_t res, F_mpz_poly_factor_t fac)
{
for(unsigned long i = 0; i < fac->num_factors; i++)
F_mpz_poly_factor_insert(res, fac->factors[i], fac->exponents[i]);
}
void F_mpz_poly_factor_print(F_mpz_poly_factor_t fac)
{
for(unsigned long i = 0; i < fac->num_factors; i++)
{
F_mpz_poly_print(fac->factors[i]);
printf(" ^ %ld\n", fac->exponents[i]);
}
}
/*===============================================================================
Normalisation
================================================================================*/
void _F_mpz_poly_normalise(F_mpz_poly_t poly)
{
ulong length = poly->length;
while ((length) && (!poly->coeffs[length - 1])) length--;
poly->length = length;
}
/*===============================================================================
Coefficient operations
================================================================================*/
void F_mpz_poly_set_coeff_si(F_mpz_poly_t poly, ulong n, const long x)
{
F_mpz_poly_fit_length(poly, n + 1);
if (n + 1 > poly->length) // insert zeroes between end of poly and new coeff if needed
{
ulong i;
for (i = poly->length; i < n; i++)
F_mpz_zero(poly->coeffs + i);
poly->length = n+1;
}
F_mpz_set_si(poly->coeffs + n, x);
_F_mpz_poly_normalise(poly); // we may have set leading coefficient to zero
}
void F_mpz_poly_set_coeff_ui(F_mpz_poly_t poly, ulong n, const ulong x)
{
F_mpz_poly_fit_length(poly, n+1);
if (n + 1 > poly->length) // insert zeroes between end of poly and new coeff if needed
{
long i;
for (i = poly->length; i < n; i++)
F_mpz_zero(poly->coeffs + i);
poly->length = n+1;
}
F_mpz_set_ui(poly->coeffs + n, x);
_F_mpz_poly_normalise(poly); // we may have set leading coefficient to zero
}
void F_mpz_poly_set_coeff_mpz(F_mpz_poly_t poly, ulong n, const mpz_t x)
{
F_mpz_poly_fit_length(poly, n+1);
if (n + 1 > poly->length) // insert zeroes between end of poly and new coeff if needed
{
long i;
for (i = poly->length; i < n; i++)
F_mpz_zero(poly->coeffs + i);
poly->length = n+1;
}
F_mpz_set_mpz(poly->coeffs + n, x);
_F_mpz_poly_normalise(poly); // we may have set leading coefficient to zero
}
void F_mpz_poly_set_coeff(F_mpz_poly_t poly, ulong n, const F_mpz_t x)
{
F_mpz_poly_fit_length(poly, n+1);
if (n + 1 > poly->length) // insert zeroes between end of poly and new coeff if needed
{
long i;
for (i = poly->length; i < n; i++)
F_mpz_zero(poly->coeffs + i);
poly->length = n+1;
}
F_mpz_set(poly->coeffs + n, x);
_F_mpz_poly_normalise(poly); // we may have set leading coefficient to zero
}
long F_mpz_poly_get_coeff_si(const F_mpz_poly_t poly, const ulong n)
{
if (n + 1 > poly->length) // coefficient is beyond end of polynomial
return 0;
return F_mpz_get_si(poly->coeffs + n);
}
ulong F_mpz_poly_get_coeff_ui(const F_mpz_poly_t poly, const ulong n)
{
if (n + 1 > poly->length) // coefficient is beyond end of polynomial
return 0;
return F_mpz_get_ui(poly->coeffs + n);
}
void F_mpz_poly_get_coeff_mpz(mpz_t x, const F_mpz_poly_t poly, const ulong n)
{
if (n + 1 > poly->length) // coefficient is beyond end of polynomial
{
mpz_set_ui(x, 0);
return;
}
F_mpz_get_mpz(x, poly->coeffs + n);
return;
}
/*===============================================================================
Conversions
================================================================================*/
void fmpz_poly_factor_to_F_mpz_poly_factor(F_mpz_poly_factor_t F_fac, fmpz_poly_factor_t f_fac)
{
ulong i;
F_mpz_poly_t temp;
F_mpz_poly_init(temp);
F_mpz_poly_set_coeff_ui(temp, 1, 1UL); /* must be at least length 2 to make it in */
for (i = 0; i < f_fac->num_factors; i++)
{
F_mpz_poly_factor_insert(F_fac, temp, f_fac->exponents[i]);
fmpz_poly_to_F_mpz_poly(temp, f_fac->factors[i]);
F_mpz_poly_swap(F_fac->factors[i], temp);
}
F_mpz_poly_clear(temp);
}
void F_mpz_poly_factor_to_fmpz_poly_factor(fmpz_poly_factor_t f_fac, F_mpz_poly_factor_t F_fac)
{
ulong i;
fmpz_poly_t temp;
fmpz_poly_init(temp);
fmpz_poly_set_coeff_ui(temp, 1, 1UL); /* must be at least length 2 to make it in */
for (i = 0; i < F_fac->num_factors; i++)
{
fmpz_poly_factor_insert(f_fac, temp, F_fac->exponents[i]);
F_mpz_poly_to_fmpz_poly(temp, F_fac->factors[i]);
fmpz_poly_swap(f_fac->factors[i], temp);
}
fmpz_poly_clear(temp);
}
void mpz_poly_to_F_mpz_poly(F_mpz_poly_t F_poly, const mpz_poly_t m_poly)
{
F_mpz_poly_fit_length(F_poly, m_poly->length);
_F_mpz_poly_set_length(F_poly, m_poly->length);
ulong i;
for (i = 0; i < m_poly->length; i++)
F_mpz_set_mpz(F_poly->coeffs + i, m_poly->coeffs[i]);
}
void F_mpz_poly_to_mpz_poly(mpz_poly_t m_poly, const F_mpz_poly_t F_poly)
{
mpz_poly_ensure_alloc(m_poly, F_poly->length);
m_poly->length = F_poly->length;
ulong i;
for (i = 0; i < F_poly->length; i++)
F_mpz_get_mpz(m_poly->coeffs[i], F_poly->coeffs + i);
}
void fmpz_poly_to_F_mpz_poly(F_mpz_poly_t F_poly, const fmpz_poly_t m_poly)
{
F_mpz_poly_fit_length(F_poly, m_poly->length);
_F_mpz_poly_set_length(F_poly, m_poly->length);
ulong i;
mpz_t m; // does *not* need to be initialised
for (i = 0; i < m_poly->length; i++)
{
_fmpz_poly_get_coeff_mpz_read_only(m, m_poly, i);
F_mpz_set_mpz(F_poly->coeffs + i, m);
}
}
void F_mpz_poly_to_fmpz_poly(fmpz_poly_t m_poly, const F_mpz_poly_t F_poly)
{
ulong limbs = F_mpz_poly_max_limbs(F_poly);
fmpz_poly_fit_length(m_poly, F_poly->length);
fmpz_poly_fit_limbs(m_poly, limbs);
mp_limb_t * ptr;
m_poly->length = F_poly->length;
ulong i;
for (i = 0; i < F_poly->length; i++)
{
F_mpz d = F_poly->coeffs[i];
if (!COEFF_IS_MPZ(d))
_fmpz_poly_set_coeff_si(m_poly, i, d);
else
{
ptr = m_poly->coeffs + i*(limbs + 1);
__mpz_struct * mpz_ptr = F_mpz_ptr_mpz(d);
ptr[0] = mpz_ptr->_mp_size;
mpn_copyi(ptr + 1, mpz_ptr->_mp_d, FLINT_ABS(ptr[0]));
}
}
}
void F_mpz_poly_to_zmod_poly(zmod_poly_t zpol, const F_mpz_poly_t fpol)
{
unsigned long p = zpol->p;
if (fpol->length == 0)
{
zmod_poly_zero(zpol);
return;
}
F_mpz_t temp;
F_mpz_init(temp);
zmod_poly_fit_length(zpol, fpol->length);
zpol->length = fpol->length;
unsigned long i;
for (i = 0; i < fpol->length; i++)
{
*(zpol->coeffs + i) = F_mpz_mod_ui(temp, fpol->coeffs + i, p);
}
__zmod_poly_normalise(zpol);
F_mpz_clear(temp);
}
void zmod_poly_to_F_mpz_poly(F_mpz_poly_t fpol, const zmod_poly_t zpol)
{
unsigned long p = zpol->p;
if (zpol->length == 0)
{
F_mpz_poly_zero(fpol);
return;
}
F_mpz_poly_fit_length(fpol, zpol->length);
fpol->length = zpol->length;
unsigned long i;
for (i = 0; i < zpol->length; i++)
{
F_mpz_set_si(fpol->coeffs+i, zpol->coeffs[i]);
}
_F_mpz_poly_normalise(fpol);
}
/*===============================================================================
Input/output
================================================================================*/
int F_mpz_poly_from_string(F_mpz_poly_t poly, const char* s)
{
int ok;
mpz_poly_t p;
mpz_poly_init(p);
ok = mpz_poly_from_string(p, s);
if (ok)
{
mpz_poly_to_F_mpz_poly(poly, p);
}
mpz_poly_clear(p);
return ok;
}
char* F_mpz_poly_to_string(const F_mpz_poly_t poly)
{
char* buf;
mpz_poly_t m_poly;
mpz_poly_init(m_poly);
F_mpz_poly_to_mpz_poly(m_poly, poly);
buf = mpz_poly_to_string(m_poly);
mpz_poly_clear(m_poly);
return buf;
}
char* F_mpz_poly_to_string_pretty(const F_mpz_poly_t poly, const char * x)
{
char* buf;
mpz_poly_t m_poly;
mpz_poly_init(m_poly);
F_mpz_poly_to_mpz_poly(m_poly, poly);
buf = mpz_poly_to_string_pretty(m_poly, x);
mpz_poly_clear(m_poly);
return buf;
}
void F_mpz_poly_fprint(const F_mpz_poly_t poly, FILE* f)
{
char* s = F_mpz_poly_to_string(poly);
fputs(s, f);
free(s);
}
void F_mpz_poly_fprint_pretty(const F_mpz_poly_t poly, FILE* f, const char * x)
{
char* s = F_mpz_poly_to_string_pretty(poly, x);
fputs(s, f);
free(s);
}
void F_mpz_poly_print_pretty(const F_mpz_poly_t poly, const char * x)
{
F_mpz_poly_fprint_pretty(poly, stdout, x);
}
int F_mpz_poly_fread(F_mpz_poly_t poly, FILE* f)
{
int ok;
mpz_poly_t p;
mpz_poly_init(p);
ok = mpz_poly_fread(p, f);
if (ok)
{
mpz_poly_to_F_mpz_poly(poly, p);
}
mpz_poly_clear(p);
return ok;
}
/*===============================================================================
Assignment/swap
================================================================================*/
void F_mpz_poly_set(F_mpz_poly_t poly1, const F_mpz_poly_t poly2)
{
if (poly1 != poly2) // aliasing is trivial
{
ulong length = poly2->length;
F_mpz_poly_fit_length(poly1, poly2->length);
ulong i;
for (i = 0; i < poly2->length; i++)
F_mpz_set(poly1->coeffs + i, poly2->coeffs + i);
_F_mpz_poly_set_length(poly1, poly2->length);
}
}
void F_mpz_poly_swap(F_mpz_poly_t poly1, F_mpz_poly_t poly2)
{
if (poly1 == poly2) return;
ulong temp = poly1->length;
poly1->length = poly2->length;
poly2->length = temp;
temp = poly1->alloc;
poly1->alloc = poly2->alloc;
poly2->alloc = temp;
F_mpz * temp_c = poly1->coeffs;
poly1->coeffs = poly2->coeffs;
poly2->coeffs = temp_c;
return;
}
/*===============================================================================
Comparison
================================================================================*/
int F_mpz_poly_equal(const F_mpz_poly_t poly1, const F_mpz_poly_t poly2)
{
if (poly1 == poly2) return 1; // same polynomial
if (poly1->length != poly2->length) return 0; // check if lengths the same
ulong i;
for (i = 0; i < poly1->length; i++) // check if coefficients the same
if (!F_mpz_equal(poly1->coeffs + i, poly2->coeffs + i))
return 0;
return 1;
}
/*===============================================================================
Coefficient sizes
================================================================================*/
long F_mpz_poly_max_bits1(const F_mpz_poly_t poly)
{
int sign = 0;
F_mpz or = 0;
F_mpz c;
F_mpz mask = (1L<<(FLINT_BITS-3)); // see if the largest bit possible in a small is set
ulong i;
// search until we find an mpz_t coefficient or one of at least FLINT_BITS - 2 bits
for (i = 0; i < poly->length; i++)
{
c = poly->coeffs[i];
if (c < 0L)
{
sign = 1;
or = or | -c;
} else
{
if (COEFF_IS_MPZ(c)) return 0; // found an mpz_t coeff
or = or | c;
}
if (or & mask) break; // found a coeff with FLINT_BIT - 2 bits
}
if (!sign) // if no negative coefficient yet keep searching
for ( ; i < poly->length; i++)
if (poly->coeffs[i] < 0L) return -(FLINT_BITS - 2); // only happens if we hit
// break in previous loop
if (sign) return -FLINT_BIT_COUNT(or); // return bits n or -n if negative found
else return FLINT_BIT_COUNT(or);
}
long F_mpz_poly_max_bits(const F_mpz_poly_t poly)
{
int sign = 0;
ulong max = 0;
ulong bits = 0;
ulong max_limbs = 1;
ulong size;
ulong i;
F_mpz c;
// search until we find an mpz_t coefficient or one of at least FLINT_BITS - 2 bits
for (i = 0; i < poly->length; i++)
{
c = poly->coeffs[i];
if (COEFF_IS_MPZ(c)) break; // found an mpz_t coeff
if (c < 0L)
{
sign = 1;
bits = FLINT_BIT_COUNT(-c);
} else bits = FLINT_BIT_COUNT(c);
if (bits > max)
{
max = bits;
if (max == FLINT_BITS - 2) break; // coeff is at least FLINT_BITS - 2 bits
}
}
// search through mpz coefficients for largest size in bits
for ( ; i < poly->length; i++)
{
c = poly->coeffs[i];
if (COEFF_IS_MPZ(c))
{
__mpz_struct * mpz_ptr = F_mpz_ptr_mpz(c);
if (mpz_sgn(mpz_ptr) < 0) sign = 1;
size = mpz_size(mpz_ptr);
if (size > max_limbs)
{
max_limbs = size;
mp_limb_t * data = mpz_ptr->_mp_d;
bits = FLINT_BIT_COUNT(data[max_limbs - 1]);
max = bits;
} else if (size == max_limbs)
{
mp_limb_t * data = mpz_ptr->_mp_d;
bits = FLINT_BIT_COUNT(data[max_limbs - 1]);
if (bits > max) max = bits;
}
} else if ((long) c < 0L) sign = 1; // still need to check the sign of small coefficients
}
if (sign) return -(max + FLINT_BITS*(max_limbs - 1));
else return max + FLINT_BITS*(max_limbs - 1);
}
ulong F_mpz_poly_max_limbs(const F_mpz_poly_t poly)
{
if (poly->length == 0) return 0; // polynomial is zero
ulong max = 1; // all coefficients have at least one limb
ulong limbs;
ulong c;
// search through mpz coefficients for one of largest size
ulong i;
for (i = 0; i < poly->length; i++)
{
c = poly->coeffs[i];
if (COEFF_IS_MPZ(c))
{
limbs = mpz_size(F_mpz_ptr_mpz(c));
if (limbs > max) max = limbs;
}
}
return max;
}
/*===============================================================================
Reverse
================================================================================*/
void F_mpz_poly_reverse(F_mpz_poly_t res, const F_mpz_poly_t poly, const ulong length)
{
long i;
F_mpz_poly_fit_length(res, length);
if (poly != res) // not the same polynomial
{
for (i = 0; i < FLINT_MIN(length, poly->length); i++)
F_mpz_set(res->coeffs + length - i - 1, poly->coeffs + i); // copy over extant coefficients in reverse
for ( ; i < length; i++) // set other coefficients to zero
F_mpz_zero(res->coeffs + length - i - 1);
} else // same polynomial
{
for (i = 0; i < length/2; i++)
{
// swap extant coefficients
if (length - i - 1 < res->length) F_mpz_swap(res->coeffs + i, res->coeffs + length - i - 1);
else
{
F_mpz_set(res->coeffs + length - i - 1, res->coeffs + i); // for other coefficients "swap" with zero
F_mpz_zero(res->coeffs + i);
}
}
// if length is odd we missed a coefficient in swapping pairs, it may need to be set to zero
if ((length & 1) && (i >= poly->length)) F_mpz_zero(res->coeffs + i);
}
_F_mpz_poly_set_length(res, length);
_F_mpz_poly_normalise(res); // new leading coeff, which was trailing coeff, may now be zero
}
/*===============================================================================
Negation
================================================================================*/
void F_mpz_poly_neg(F_mpz_poly_t res, const F_mpz_poly_t poly)
{
F_mpz_poly_fit_length(res, poly->length);
ulong i;
for (i = 0; i < poly->length; i++)
F_mpz_neg(res->coeffs + i, poly->coeffs + i);
_F_mpz_poly_set_length(res, poly->length);
}
/*===============================================================================
Addition/subtraction
================================================================================*/
void _F_mpz_poly_add(F_mpz_poly_t res, const F_mpz_poly_t poly1, const F_mpz_poly_t poly2)
{
ulong longer = FLINT_MAX(poly1->length, poly2->length);
ulong shorter = FLINT_MIN(poly1->length, poly2->length);
ulong i;
for (i = 0; i < shorter; i++) // add up to the length of the shorter poly
F_mpz_add(res->coeffs + i, poly1->coeffs + i, poly2->coeffs + i);
if (poly1 != res) // copy any remaining coefficients from poly1
for (i = shorter; i < poly1->length; i++)
F_mpz_set(res->coeffs + i, poly1->coeffs + i);
if (poly2 != res) // copy any remaining coefficients from poly2
for (i = shorter; i < poly2->length; i++)
F_mpz_set(res->coeffs + i, poly2->coeffs + i);
if (poly1->length == poly2->length)
{
_F_mpz_poly_set_length(res, poly1->length);
_F_mpz_poly_normalise(res); // there may have been cancellation
} else
_F_mpz_poly_set_length(res, longer);
}
void F_mpz_poly_add(F_mpz_poly_t res, const F_mpz_poly_t poly1, const F_mpz_poly_t poly2)
{
ulong longer = FLINT_MAX(poly1->length, poly2->length);
F_mpz_poly_fit_length(res, longer);
_F_mpz_poly_add(res, poly1, poly2);
}
void _F_mpz_poly_sub(F_mpz_poly_t res, const F_mpz_poly_t poly1, const F_mpz_poly_t poly2)
{
ulong longer = FLINT_MAX(poly1->length, poly2->length);
ulong shorter = FLINT_MIN(poly1->length, poly2->length);
ulong i;
for (i = 0; i < shorter; i++) // add up to the length of the shorter poly
F_mpz_sub(res->coeffs + i, poly1->coeffs + i, poly2->coeffs + i);
if (poly1 != res) // copy any remaining coefficients from poly1
for (i = shorter; i < poly1->length; i++)
F_mpz_set(res->coeffs + i, poly1->coeffs + i);
// careful, it is *always* necessary to negate coeffs from poly2, even if this is already res
for (i = shorter; i < poly2->length; i++)
F_mpz_neg(res->coeffs + i, poly2->coeffs + i);
if (poly1->length == poly2->length)
{
_F_mpz_poly_set_length(res, poly1->length);
_F_mpz_poly_normalise(res); // there may have been cancellation
} else
_F_mpz_poly_set_length(res, longer);
}
void F_mpz_poly_sub(F_mpz_poly_t res, const F_mpz_poly_t poly1, const F_mpz_poly_t poly2)
{
ulong longer = FLINT_MAX(poly1->length, poly2->length);
F_mpz_poly_fit_length(res, longer);
_F_mpz_poly_sub(res, poly1, poly2);
}
/*===============================================================================
Shifting
================================================================================*/
void F_mpz_poly_left_shift(F_mpz_poly_t res, const F_mpz_poly_t poly, const ulong n)
{
if (n == 0) // special case, no shift
{
if (res != poly) F_mpz_poly_set(res, poly);
return;
}
if (poly->length == 0) // nothing to shift
{
_F_mpz_poly_set_length(res, 0);
return;
}
F_mpz_poly_fit_length(res, poly->length + n);
// copy in reverse order to avoid writing over unshifted coeffs
long i;
for (i = poly->length - 1; i >= 0; i--)
F_mpz_set(res->coeffs + i + n, poly->coeffs + i);
// insert n zeroes
for (i = 0; i < n; i++) F_mpz_zero(res->coeffs + i);
_F_mpz_poly_set_length(res, poly->length + n);
}
void F_mpz_poly_right_shift(F_mpz_poly_t res, const F_mpz_poly_t poly, const ulong n)
{
if (poly->length <= n)
{
F_mpz_poly_zero(res);
return;
}
F_mpz_poly_fit_length(res, poly->length - n);
// copy in forward order to avoid writing over unshifted coeffs
ulong i;
for (i = 0; i < poly->length - n; i++)
F_mpz_set(res->coeffs + i, poly->coeffs + i + n);
_F_mpz_poly_set_length(res, poly->length - n);
}
/*===============================================================================
Interleave
================================================================================*/
void F_mpz_poly_interleave(F_mpz_poly_t res, F_mpz_poly_t poly1, F_mpz_poly_t poly2)
{
ulong length = FLINT_MAX(2*poly1->length - 1, 2*poly2->length);
F_mpz_poly_fit_length(res, length + 1); // extra one in case length is odd
// it will be set to zero
ulong shorter = FLINT_MIN(poly1->length, poly2->length);
ulong i;
for (i = 0; i < shorter; i++)
{
F_mpz_set(res->coeffs + 2*i, poly1->coeffs + i);
F_mpz_set(res->coeffs + 2*i+1, poly2->coeffs + i);
}
for ( ; i < poly1->length; i++)
{
F_mpz_set(res->coeffs + 2*i, poly1->coeffs + i);
F_mpz_zero(res->coeffs + 2*i+1);
}
for ( ; i < poly2->length; i++)
{
F_mpz_zero(res->coeffs + 2*i);
F_mpz_set(res->coeffs + 2*i+1, poly2->coeffs + i);
}
_F_mpz_poly_set_length(res, length);
}
void F_mpz_poly_interleave_small(F_mpz_poly_t res, F_mpz_poly_t poly1, F_mpz_poly_t poly2)
{
ulong length = FLINT_MAX(2*poly1->length - 1, 2*poly2->length);
F_mpz_poly_fit_length(res, length + 1); // extra one in case length is odd
// it will be set to zero
ulong shorter = FLINT_MIN(poly1->length, poly2->length);
ulong i;
for (i = 0; i < shorter; i++)
{
_F_mpz_demote(res->coeffs + 2*i);
_F_mpz_demote(res->coeffs + 2*i + 1);
res->coeffs[2*i] = poly1->coeffs[i];
res->coeffs[2*i+1] = poly2->coeffs[i];
}
for ( ; i < poly1->length; i++)
{
_F_mpz_demote(res->coeffs + 2*i);
_F_mpz_demote(res->coeffs + 2*i + 1);
res->coeffs[2*i] = poly1->coeffs[i];
res->coeffs[2*i+1] = 0;
}
for ( ; i < poly2->length; i++)
{