1 /*
2     Copyright (C) 2019 Daniel Schultz
3 
4     This file is part of FLINT.
5 
6     FLINT is free software: you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 2.1 of the License, or
9     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
10 */
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include "fmpz_mod_poly.h"
15 
16 int
main(void)17 main(void)
18 {
19     slong i, j;
20     FLINT_TEST_INIT(state);
21 
22     flint_printf("find_distinct_nonzero_roots....");
23     fflush(stdout);
24 
25     for (i = 0; i < 200 * flint_test_multiplier(); i++)
26     {
27         int highdegreefactor;
28         fmpz_mod_poly_t a, b, r;
29         fmpz_t p, e, zero;
30 
31         fmpz_init(zero);
32         fmpz_init_set_ui(p, n_randtest_prime(state, 1));
33         fmpz_init(e);
34         fmpz_mod_poly_init(a, p);
35         fmpz_mod_poly_init(b, p);
36         fmpz_mod_poly_init(r, p);
37 
38         fmpz_mod_poly_set_ui(a, 1 + n_randint(state, fmpz_get_ui(p) - 1));
39         highdegreefactor = 0;
40         for (j = n_randint(state, 10); j >= 0; j--)
41         {
42             if (n_randint(state, 10) > 1)
43             {
44                 fmpz_mod_poly_randtest_monic_irreducible(b, state, 1);
45             }
46             else
47             {
48                 highdegreefactor = 1;
49                 fmpz_mod_poly_randtest_monic_irreducible(b, state, 2 + n_randint(state, 9));
50             }
51             fmpz_mod_poly_mul(a, a, b);
52         }
53 
54         fmpz_mod_poly_fit_length(r, fmpz_mod_poly_degree(a));
55         if (fmpz_mod_poly_find_distinct_nonzero_roots(r->coeffs, a))
56         {
57             /* check that a is square free */
58             fmpz_mod_poly_derivative(b, a);
59             fmpz_mod_poly_gcd(b, b, a);
60             if (fmpz_mod_poly_degree(b) > 0)
61             {
62                 flint_printf("FAIL\ncheck multiple roots i = %wd\n", i);
63                 flint_abort();
64             }
65 
66             /* check that each root is a root */
67             for (j = fmpz_mod_poly_degree(a) - 1; j >= 0; j--)
68             {
69                 if (fmpz_is_zero(r->coeffs + j))
70                 {
71                     flint_printf("FAIL\ncheck zero root i = %wd\n", i);
72                     flint_abort();
73                 }
74                 if (fmpz_mod_poly_evaluate_fmpz(e, a, r->coeffs + j), !fmpz_is_zero(e))
75                 {
76                     flint_printf("FAIL\ncheck root is a root i = %wd\n", i);
77                     flint_abort();
78                 }
79             }
80         }
81         else
82         {
83             fmpz_mod_poly_derivative(b, a);
84             fmpz_mod_poly_gcd(b, b, a);
85             if (!highdegreefactor
86                 && fmpz_mod_poly_degree(b) == 0
87                 && (fmpz_mod_poly_evaluate_fmpz(e, a, zero), !fmpz_is_zero(e)))
88             {
89                 flint_printf("FAIL\ncheck fail return i = %wd\n", i);
90                 flint_abort();
91             }
92         }
93 
94         fmpz_mod_poly_clear(a);
95         fmpz_mod_poly_clear(b);
96         fmpz_mod_poly_clear(r);
97         fmpz_clear(p);
98         fmpz_clear(e);
99         fmpz_clear(zero);
100     }
101 
102     FLINT_TEST_CLEANUP(state);
103 
104     flint_printf("PASS\n");
105     return 0;
106 }
107