1 /*
2     Copyright (C) 2012 Lina Kulakova
3     Copyright (C) 2013 Mike Hansen
4 
5     This file is part of FLINT.
6 
7     FLINT is free software: you can redistribute it and/or modify it under
8     the terms of the GNU Lesser General Public License (LGPL) as published
9     by the Free Software Foundation; either version 2.1 of the License, or
10     (at your option) any later version.  See <http://www.gnu.org/licenses/>.
11 */
12 
13 #ifdef T
14 
15 #include "templates.h"
16 
17 #include <math.h>
18 
19 void
TEMPLATE(T,poly_factor_kaltofen_shoup)20 TEMPLATE(T, poly_factor_kaltofen_shoup) (TEMPLATE(T, poly_factor_t) res,
21                                          const TEMPLATE(T, poly_t) poly,
22                                          const TEMPLATE(T, ctx_t) ctx)
23 {
24     TEMPLATE(T, poly_t) v;
25     TEMPLATE(T, poly_factor_t) sq_free, dist_deg;
26     slong i, j, k, l, res_num, dist_deg_num;
27     slong *degs;
28 
29     if (!
30         (degs =
31          flint_malloc(TEMPLATE(T, poly_degree) (poly, ctx) * sizeof(slong))))
32     {
33         TEMPLATE_PRINTF("Exception (%s_poly_factor_kaltofen_shoup): \n", T);
34         flint_printf("Not enough memory.\n");
35         flint_abort();
36     }
37 
38     TEMPLATE(T, poly_init) (v, ctx);
39 
40     TEMPLATE(T, poly_make_monic) (v, poly, ctx);
41 
42     /* compute squarefree factorisation */
43     TEMPLATE(T, poly_factor_init) (sq_free, ctx);
44     TEMPLATE(T, poly_factor_squarefree) (sq_free, v, ctx);
45 
46     /* compute distinct-degree factorisation */
47     TEMPLATE(T, poly_factor_init) (dist_deg, ctx);
48     for (i = 0; i < sq_free->num; i++)
49     {
50         dist_deg_num = dist_deg->num;
51 
52         TEMPLATE(T, poly_factor_distinct_deg) (dist_deg, sq_free->poly + i,
53                                                &degs, ctx);
54 
55         /* compute equal-degree factorisation */
56         for (j = dist_deg_num, l = 0; j < dist_deg->num; j++, l++)
57         {
58             res_num = res->num;
59 
60             TEMPLATE(T, poly_factor_equal_deg) (res, dist_deg->poly + j,
61                                                 degs[l], ctx);
62             for (k = res_num; k < res->num; k++)
63                 res->exp[k] = TEMPLATE(T, poly_remove) (v, res->poly + k, ctx);
64         }
65     }
66 
67     flint_free(degs);
68     TEMPLATE(T, poly_clear) (v, ctx);
69     TEMPLATE(T, poly_factor_clear) (dist_deg, ctx);
70     TEMPLATE(T, poly_factor_clear) (sq_free, ctx);
71 }
72 
73 
74 #endif
75