#include "test_helpers.h"
#include "nmod_mpoly_factor.h"
void check_it(const nmod_mpoly_t p, const nmod_mpoly_ctx_t ctx)
{
slong i, j;
nmod_mpoly_t q;
nmod_mpoly_factor_t g, h;
nmod_mpoly_factor_init(g, ctx);
nmod_mpoly_factor_init(h, ctx);
nmod_mpoly_init(q, ctx);
if (!nmod_mpoly_factor_squarefree(g, p, ctx))
{
flint_printf("FAIL:\ncheck factorization could be computed\n");
fflush(stdout);
flint_abort();
}
for (i = 0; i < g->num; i++)
{
if (g->poly[i].length < 1 || g->poly[i].coeffs[0] != 1)
{
flint_printf("FAIL:\nfactorization is not unit normal\n");
fflush(stdout);
flint_abort();
}
}
nmod_mpoly_factor_expand(q, g, ctx);
if (!nmod_mpoly_equal(q, p, ctx))
{
flint_printf("FAIL:\nfactorization does not match original polynomial\n");
fflush(stdout);
flint_abort();
}
for (i = 0; i < g->num; i++)
{
nmod_mpoly_factor_squarefree(h, g->poly + i, ctx);
for (j = 0; j < h->num; j++)
{
if (!fmpz_is_one(h->exp + j))
{
flint_printf("FAIL:\nfactor has a square factor\n");
fflush(stdout);
flint_abort();
}
}
}
for (i = 1; i < g->num; i++)
for (j = 0; j < i; j++)
{
if (!nmod_mpoly_gcd(q, g->poly + i, g->poly + j, ctx))
{
flint_printf("FAIL:\ncheck gcd could be computed\n");
}
if (!nmod_mpoly_is_one(q, ctx))
{
flint_printf("FAIL:\nbases have a common factor\n");
}
}
nmod_mpoly_clear(q, ctx);
nmod_mpoly_factor_clear(g, ctx);
nmod_mpoly_factor_clear(h, ctx);
}
TEST_FUNCTION_START(nmod_mpoly_factor_squarefree, state)
{
slong i, j, tmul = 30;
for (i = 0; i < tmul * flint_test_multiplier(); i++)
{
nmod_mpoly_ctx_t ctx;
nmod_mpoly_t a, t;
slong n, nfacs, len;
ulong expbound, powbound, pow;
ulong p;
p = n_randint(state, (i % 2 == 0) ? 4 : FLINT_BITS - 1) + 1;
p = n_randbits(state, p);
p = n_nextprime(p, 1);
nmod_mpoly_ctx_init_rand(ctx, state, 6, p);
nmod_mpoly_init(a, ctx);
nmod_mpoly_init(t, ctx);
n = FLINT_MAX(WORD(1), ctx->minfo->nvars);
nfacs = 1 + (6 + n_randint(state, 6))/n;
powbound = 1 + n_randint(state, 3);
expbound = 3 + 25/nfacs/n/powbound;
nmod_mpoly_one(a, ctx);
for (j = 0; j < nfacs; j++)
{
len = 1 + n_randint(state, 60/powbound/n);
nmod_mpoly_randtest_bound(t, state, len, expbound, ctx);
if (nmod_mpoly_is_zero(t, ctx))
nmod_mpoly_one(t, ctx);
pow = 1 + n_randint(state, powbound);
nmod_mpoly_pow_ui(t, t, pow, ctx);
nmod_mpoly_mul(a, a, t, ctx);
}
check_it(a, ctx);
nmod_mpoly_clear(t, ctx);
nmod_mpoly_clear(a, ctx);
nmod_mpoly_ctx_clear(ctx);
}
TEST_FUNCTION_END(state);
}