File: factor_squarefree.c

package info (click to toggle)
flint 2.5.2-19
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 30,308 kB
  • sloc: ansic: 289,367; cpp: 11,210; python: 1,280; sh: 649; makefile: 283
file content (163 lines) | stat: -rw-r--r-- 4,812 bytes parent folder | download | duplicates (4)
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
/*=============================================================================

    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

=============================================================================*/
/******************************************************************************

    Copyright (C) 2007 David Howden
    Copyright (C) 2007, 2008, 2009, 2010 William Hart
    Copyright (C) 2008 Richard Howell-Peak
    Copyright (C) 2011 Fredrik Johansson
    Copyright (C) 2012 Lina Kulakova

******************************************************************************/

#include "fmpz_mod_poly.h"
#include "ulong_extras.h"

void
fmpz_mod_poly_factor_squarefree(fmpz_mod_poly_factor_t res,
                                const fmpz_mod_poly_t f)
{
    fmpz_mod_poly_t f_d, g, g_1, r;
    fmpz_t p, x;
    slong deg, i, p_ui;

    if (f->length <= 1)
    {
        res->num = 0;
        return;
    }

    if (f->length == 2)
    {
        fmpz_mod_poly_factor_insert(res, f, 1);
        return;
    }

    fmpz_init(p);
    fmpz_set(p, &f->p);
    p_ui = fmpz_get_ui(p);
    deg = fmpz_mod_poly_degree(f);

    /* Step 1, look at f', if it is zero then we are done since f = h(x)^p
       for some particular h(x), clearly f(x) = sum a_k x^kp, k <= deg(f) */

    fmpz_init(x);
    fmpz_mod_poly_init(g_1, p);
    fmpz_mod_poly_init(f_d, p);
    fmpz_mod_poly_init(g, p);
    fmpz_mod_poly_derivative(f_d, f);

    /* Case 1 */
    if (fmpz_mod_poly_is_zero(f_d))
    {
        fmpz_mod_poly_factor_t new_res;
        fmpz_mod_poly_t h;

        fmpz_mod_poly_init(h, p);

        for (i = 0; i <= deg / p_ui; i++)   /* this will be an integer since f'=0 */
        {
            fmpz_mod_poly_get_coeff_fmpz(x, f, i * p_ui);
            fmpz_mod_poly_set_coeff_fmpz(h, i, x);
        }

        /* Now run squarefree on h, and return it to the pth power */
        fmpz_mod_poly_factor_init(new_res);

        fmpz_mod_poly_factor_squarefree(new_res, h);
        fmpz_mod_poly_factor_pow(new_res, p_ui);

        fmpz_mod_poly_factor_concat(res, new_res);
        fmpz_mod_poly_clear(h);
        fmpz_mod_poly_factor_clear(new_res);
    }
    else
    {
        fmpz_mod_poly_t h, z;

        fmpz_mod_poly_init(r, p);

        fmpz_mod_poly_gcd(g, f, f_d);
        fmpz_mod_poly_divrem(g_1, r, f, g);

        i = 1;

        fmpz_mod_poly_init(h, p);
        fmpz_mod_poly_init(z, p);

        /* Case 2 */
        while (g_1->length > 1)
        {
            fmpz_mod_poly_gcd(h, g_1, g);
            fmpz_mod_poly_divrem(z, r, g_1, h);

            /* out <- out.z */
            if (z->length > 1)
            {
                fmpz_mod_poly_factor_insert(res, z, 1);
                fmpz_mod_poly_make_monic(res->poly + (res->num - 1),
                                         res->poly + (res->num - 1));
                if (res->num)
                    res->exp[res->num - 1] *= i;
            }

            i++;
            fmpz_mod_poly_set(g_1, h);
            fmpz_mod_poly_divrem(g, r, g, h);
        }

        fmpz_mod_poly_clear(h);
        fmpz_mod_poly_clear(z);
        fmpz_mod_poly_clear(r);

        fmpz_mod_poly_make_monic(g, g);

        if (g->length > 1)
        {
            /* so now we multiply res with squarefree(g^1/p) ^ p  */
            fmpz_mod_poly_t g_p;    /* g^(1/p) */
            fmpz_mod_poly_factor_t new_res_2;

            fmpz_mod_poly_init(g_p, p);

            for (i = 0; i <= fmpz_mod_poly_degree(g) / p_ui; i++)
            {
                fmpz_mod_poly_get_coeff_fmpz(x, g, i * p_ui);
                fmpz_mod_poly_set_coeff_fmpz(g_p, i, x);
            }

            fmpz_mod_poly_factor_init(new_res_2);

            /* squarefree(g^(1/p)) */
            fmpz_mod_poly_factor_squarefree(new_res_2, g_p);
            fmpz_mod_poly_factor_pow(new_res_2, p_ui);

            fmpz_mod_poly_factor_concat(res, new_res_2);
            fmpz_mod_poly_clear(g_p);
            fmpz_mod_poly_factor_clear(new_res_2);
        }
    }

    fmpz_clear(p);
    fmpz_clear(x);
    fmpz_mod_poly_clear(g_1);
    fmpz_mod_poly_clear(f_d);
    fmpz_mod_poly_clear(g);
}