1+ /* This file is public domain. Author: Hartmut Monien. */
2+
13#include <stdlib.h>
24
35#include "flint.h"
@@ -27,14 +29,14 @@ typedef struct
2729
2830typedef fmpz_poly_roots_fq_struct fmpz_poly_roots_fq_t [1 ];
2931
30- void fmpz_poly_roots_fq_init (fmpz_poly_roots_fq_t roots , fq_ctx_t fctx );
31- void fmpz_poly_roots_fq_clear (fmpz_poly_roots_fq_t roots , fq_ctx_t fctx );
32- int fmpz_poly_roots_fq_print (fmpz_poly_roots_fq_t roots , fq_ctx_t fctx );
33- int fmpz_poly_roots_fq_fprint_pretty (FILE * file , fmpz_poly_roots_fq_t roots ,
34- fq_ctx_t fctx );
35- int fmpz_poly_roots_fq_print_pretty (fmpz_poly_roots_fq_t roots ,
32+ void fmpz_poly_roots_fq_init (fmpz_poly_roots_fq_t roots , fq_ctx_t fctx );
33+ void fmpz_poly_roots_fq_clear (fmpz_poly_roots_fq_t roots , fq_ctx_t fctx );
34+ int fmpz_poly_roots_fq_print (fmpz_poly_roots_fq_t roots , fq_ctx_t fctx );
35+ int fmpz_poly_roots_fq_fprint_pretty (FILE * file , fmpz_poly_roots_fq_t roots ,
3636 fq_ctx_t fctx );
37- void fmpz_poly_roots_fq (fmpz_poly_roots_fq_t roots , fmpz_poly_t poly ,
37+ int fmpz_poly_roots_fq_print_pretty (fmpz_poly_roots_fq_t roots ,
38+ fq_ctx_t fctx );
39+ void fmpz_poly_roots_fq (fmpz_poly_roots_fq_t roots , fmpz_poly_t poly ,
3840 fq_ctx_t fctx );
3941
4042
@@ -178,7 +180,7 @@ padic_hensel_iteration (fmpz_poly_t poly,
178180 padic_init2 (y1 , prec );
179181 do
180182 {
181- // Horner evaluation of poly and poly' at x
183+ /* Horner evaluation of poly and poly' at x */
182184 padic_set_fmpz (y0 , poly -> coeffs + poly -> length - 1 , ctx );
183185 padic_zero (y1 );
184186 for (slong j = poly -> length - 2 ; j >= 0 ; j -- )
@@ -189,7 +191,7 @@ padic_hensel_iteration (fmpz_poly_t poly,
189191 padic_set_fmpz (tmp , poly -> coeffs + j , ctx );
190192 padic_add (y0 , y0 , tmp , ctx );
191193 }
192- // Newton step: x -> x - poly / poly'
194+ /* Newton step: x -> x - poly / poly' */
193195 padic_inv (y1 , y1 , ctx );
194196 padic_mul (y1 , y1 , y0 , ctx );
195197 padic_sub (x , x , y1 , ctx );
250252_padic_roots (fmpz_poly_t poly , fq_ctx_t fctx , padic_ctx_t pctx , slong prec )
251253{
252254 slong j , level , js , ns = 1 , nr = 0 , nz = 0 , n = fmpz_poly_degree (poly );
253- fmpz_poly_struct * s = _fmpz_poly_vec_init (n );
255+ fmpz_poly_struct * s = _fmpz_poly_vec_init (n );
254256 padic_struct
255257 * x0 = _padic_vec_init2 (n , prec ), * xs = _padic_vec_init2 (n , prec );
256258 fmpz_poly_roots_fq_t froots ;
@@ -263,7 +265,6 @@ _padic_roots (fmpz_poly_t poly, fq_ctx_t fctx, padic_ctx_t pctx, slong prec)
263265 padic_zero (xs );
264266 for (level = 0 ; level < PADIC_DEFAULT_PREC ; level ++ )
265267 {
266- // flint_printf("level %wd\n", level);
267268 for (js = 0 ; js < ns ; js ++ )
268269 {
269270 if (!fmpz_poly_is_zero (s + js ))
@@ -272,11 +273,9 @@ _padic_roots (fmpz_poly_t poly, fq_ctx_t fctx, padic_ctx_t pctx, slong prec)
272273 fmpz_poly_roots_fq (froots , s + js , fctx );
273274 for (j = 0 ; j < froots -> num ; j ++ )
274275 {
275- //flint_printf("froots->num: %d, j : %d\n", froots->num, j);
276276 fmpz_poly_get_coeff_fmpz (zero , froots -> x0 + j , 0 );
277277 if (* (froots -> multiplicity + j ) > 1 )
278278 {
279- // flint_printf("multiplity: %wd\n", *(froots->multiplicity+j) ),
280279 padic_set_fmpz (xs + n - 1 - nr , zero , pctx );
281280 padic_shift (xs + n - 1 - nr , xs + n - 1 - nr , level ,
282281 pctx );
@@ -307,15 +306,12 @@ _padic_roots (fmpz_poly_t poly, fq_ctx_t fctx, padic_ctx_t pctx, slong prec)
307306 }
308307 else
309308 {
310- // flint_printf("multiplicity 1\n");
311309 padic_set_fmpz (x0 + nz , zero , pctx );
312310 if (!fmpz_is_zero (zero ))
313311 {
314312 padic_shift (x0 + nz , x0 + nz , level , pctx );
315313 }
316314 padic_add (x0 + nz , x0 + nz , xs + js , pctx );
317- // padic_print(x0+nz, pctx);
318- // flint_printf("\n");
319315 padic_hensel_iteration (poly , x0 + nz , pctx , prec );
320316 padic_print (x0 + nz , pctx );
321317 flint_printf (" (1)\n" );
@@ -377,44 +373,52 @@ padic_roots (fmpz_poly_t poly, padic_ctx_t pctx, slong prec)
377373 fq_ctx_clear (fctx );
378374}
379375
380- // example polynomials
381-
382- char polys [] =
383- "3 -2774119 -2468 1\n"
384- "3 6 -7 1\n"
385- "4 -156 188 -33 1\n"
386- "3 -11 0 1\n"
387- "3 -30 1 1\n"
388- "4 -17576 2028 -78 1\n"
389- "10 -362880 1026576 -1172700 723680 -269325 63273 -9450 870 -45 1\n"
390- "12 -39916800 120543840 -150917976 105258076 -45995730 13339535 -2637558 357423 -32670 1925 -66 1\n"
391- "9 44100 -103740 103429 -57034 19019 -3928 491 -34 1\n"
392- "5 83521 -19652 1734 -68 1\n"
393- "12 22370117 15978655 10666271 5010005 1846306 575366 142702 28538 4585 523 35 1\n"
394- "4 0 -11 0 1\n"
395- "7 3 1 0 0 1 0 1\n"
396- "11 23 -74 89 -68 35 0 -14 8 -2 -1 1\n" "4 -12 0 0 1\n" ;
376+ /* example polynomials */
377+
378+ char * polys [] = {
379+ "3 -2774119 -2468 1" ,
380+ "3 6 -7 1" ,
381+ "4 -156 188 -33 1" ,
382+ "3 -11 0 1" ,
383+ "3 -30 1 1" ,
384+ "4 -17576 2028 -78 1" ,
385+ "10 -362880 1026576 -1172700 723680 -269325 63273 -9450 870 -45 1" ,
386+ "12 -39916800 120543840 -150917976 105258076 -45995730 13339535 -2637558 357423 -32670 1925 -66 1" ,
387+ "9 44100 -103740 103429 -57034 19019 -3928 491 -34 1" ,
388+ "5 83521 -19652 1734 -68 1" ,
389+ "12 22370117 15978655 10666271 5010005 1846306 575366 142702 28538 4585 523 35 1" ,
390+ "4 0 -11 0 1" ,
391+ "7 3 1 0 0 1 0 1" ,
392+ "11 23 -74 89 -68 35 0 -14 8 -2 -1 1" ,
393+ "4 -12 0 0 1" ,
394+ };
397395
398396int
399397main (int argc , char * argv [])
400398{
401399
402400 const slong np = sizeof (polys ) / sizeof (polys [0 ]);
401+ flint_printf ("# examples: %d\n" , np );
403402
404403 ulong n = 7 ;
405-
406- if ( argc != 2 ) {
407- flint_printf ("usage: %s p (prime)\n" , argv [0 ]);
408- flint_printf ("find roots of polynomials over p-adic field Z[p].\n" );
409- exit (1 );
410- } else {
411- n = atoi (argv [1 ]);
412- if ( !n_is_prime (n ) ) {
413- flint_printf ("%d is not a prime as required for p-adic field.\n" , n );
414- exit (1 );
415- }
416- };
417404
405+ if (argc != 2 )
406+ {
407+ flint_printf ("usage: %s p (prime)\n" , argv [0 ]);
408+ flint_printf ("find roots of polynomials over p-adic field Z[p].\n" );
409+ exit (1 );
410+ }
411+ else
412+ {
413+ n = atoi (argv [1 ]);
414+ if (!n_is_prime (n ))
415+ {
416+ flint_printf ("%d is not a prime as required for p-adic field.\n" ,
417+ n );
418+ exit (1 );
419+ }
420+ };
421+
418422 fmpz_t p ;
419423 fmpz_init (p );
420424 fmpz_set_ui (p , n );
@@ -425,23 +429,20 @@ main (int argc, char *argv[])
425429 fmpz_poly_t poly ;
426430 fmpz_poly_init (poly );
427431
428- // use this to read from "poly_data" string.
429-
430- FILE * poly_data = fmemopen (polys , np , "r" );
432+ /* use this to read from "poly_data" string. */
431433
432434 flint_printf ("reading polynomials:\n" );
433435
434- while ( fmpz_poly_fread ( poly_data , poly ) )
436+ for ( slong j = 0 ; j < sizeof ( polys )/ sizeof ( polys [ 0 ]); j ++ )
435437 {
436- flint_printf ("polynomial:\n\n" );
438+ flint_printf ("polynomial:\n\n" );
439+ fmpz_poly_set_str (poly , polys [j ]);
437440 fmpz_poly_print_pretty (poly , "x" );
438441 flint_printf ("\n\nroots with multiplicity:\n\n" );
439442 padic_roots (poly , pctx , 64 );
440443 flint_printf ("\n" );
441444 }
442445
443- fclose (poly_data );
444-
445446 fmpz_poly_clear (poly );
446447 fmpz_clear (p );
447448
0 commit comments