#ifdef T
#include "templates.h"
void
TEMPLATE(T, poly_factor_squarefree) (TEMPLATE(T, poly_factor_t) res,
const TEMPLATE(T, poly_t) f,
const TEMPLATE(T, ctx_t) ctx)
{
TEMPLATE(T, poly_t) f_d, g, g_1, r;
TEMPLATE(T, t) x;
slong deg, i, p_ui;
if (f->length <= 1)
{
res->num = 0;
return;
}
if (f->length == 2)
{
TEMPLATE(T, poly_factor_insert) (res, f, 1, ctx);
TEMPLATE(T, poly_make_monic) (res->poly + (res->num - 1),
res->poly + (res->num - 1), ctx);
return;
}
deg = TEMPLATE(T, poly_degree) (f, ctx);
TEMPLATE(T, init) (x, ctx);
TEMPLATE(T, poly_init) (g_1, ctx);
TEMPLATE(T, poly_init) (f_d, ctx);
TEMPLATE(T, poly_init) (g, ctx);
TEMPLATE(T, poly_derivative) (f_d, f, ctx);
if (TEMPLATE(T, poly_is_zero) (f_d, ctx))
{
TEMPLATE(T, poly_factor_t) new_res;
TEMPLATE(T, poly_t) h;
#if defined(FQ_NMOD_POLY_FACTOR_H) || defined(FQ_ZECH_POLY_FACTOR_H)
p_ui = TEMPLATE(T, ctx_prime)(ctx);
#else
p_ui = fmpz_get_ui(TEMPLATE(T, ctx_prime)(ctx));
#endif
TEMPLATE(T, poly_init) (h, ctx);
for (i = 0; i <= deg / p_ui; i++)
{
TEMPLATE(T, poly_get_coeff) (x, f, i * p_ui, ctx);
TEMPLATE(T, pth_root) (x, x, ctx);
TEMPLATE(T, poly_set_coeff) (h, i, x, ctx);
}
TEMPLATE(T, poly_factor_init) (new_res, ctx);
TEMPLATE(T, poly_factor_squarefree) (new_res, h, ctx);
TEMPLATE(T, poly_factor_pow) (new_res, p_ui, ctx);
TEMPLATE(T, poly_factor_concat) (res, new_res, ctx);
TEMPLATE(T, poly_clear) (h, ctx);
TEMPLATE(T, poly_factor_clear) (new_res, ctx);
}
else
{
TEMPLATE(T, poly_t) h, z;
TEMPLATE(T, poly_init) (r, ctx);
TEMPLATE(T, poly_gcd) (g, f, f_d, ctx);
TEMPLATE(T, poly_divrem) (g_1, r, f, g, ctx);
i = 1;
TEMPLATE(T, poly_init) (h, ctx);
TEMPLATE(T, poly_init) (z, ctx);
while (g_1->length > 1)
{
TEMPLATE(T, poly_gcd) (h, g_1, g, ctx);
TEMPLATE(T, poly_divrem) (z, r, g_1, h, ctx);
if (z->length > 1)
{
TEMPLATE(T, poly_factor_insert) (res, z, 1, ctx);
TEMPLATE(T, poly_make_monic) (res->poly + (res->num - 1),
res->poly + (res->num - 1), ctx);
if (res->num)
res->exp[res->num - 1] *= i;
}
i++;
TEMPLATE(T, poly_set) (g_1, h, ctx);
TEMPLATE(T, poly_divrem) (g, r, g, h, ctx);
}
TEMPLATE(T, poly_clear) (h, ctx);
TEMPLATE(T, poly_clear) (z, ctx);
TEMPLATE(T, poly_clear) (r, ctx);
TEMPLATE(T, poly_make_monic) (g, g, ctx);
if (g->length > 1)
{
TEMPLATE(T, poly_t) g_p;
TEMPLATE(T, poly_factor_t) new_res_2;
TEMPLATE(T, poly_init) (g_p, ctx);
#if defined(FQ_NMOD_POLY_FACTOR_H) || defined(FQ_ZECH_POLY_FACTOR_H)
p_ui = TEMPLATE(T, ctx_prime)(ctx);
#else
p_ui = fmpz_get_ui(TEMPLATE(T, ctx_prime)(ctx));
#endif
for (i = 0; i <= TEMPLATE(T, poly_degree) (g, ctx) / p_ui; i++)
{
TEMPLATE(T, poly_get_coeff) (x, g, i * p_ui, ctx);
TEMPLATE(T, pth_root) (x, x, ctx);
TEMPLATE(T, poly_set_coeff) (g_p, i, x, ctx);
}
TEMPLATE(T, poly_factor_init) (new_res_2, ctx);
TEMPLATE(T, poly_factor_squarefree) (new_res_2, g_p, ctx);
TEMPLATE(T, poly_factor_pow) (new_res_2, p_ui, ctx);
TEMPLATE(T, poly_factor_concat) (res, new_res_2, ctx);
TEMPLATE(T, poly_clear) (g_p, ctx);
TEMPLATE(T, poly_factor_clear) (new_res_2, ctx);
}
}
TEMPLATE(T, clear) (x, ctx);
TEMPLATE(T, poly_clear) (g_1, ctx);
TEMPLATE(T, poly_clear) (f_d, ctx);
TEMPLATE(T, poly_clear) (g, ctx);
}
#endif