Skip to content

Commit

Permalink
Merge pull request #2066 from Jake-Moss/monomial_deflation_div_by_zero
Browse files Browse the repository at this point in the history
Fix division by zero in `monomials_deflate.c`.
  • Loading branch information
fredrik-johansson committed Sep 17, 2024
2 parents 3dbe239 + 044117b commit 1f98483
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
60 changes: 60 additions & 0 deletions src/fmpz_mpoly/test/t-inflate_deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,65 @@ TEST_FUNCTION_START(fmpz_mpoly_inflate_deflate, state)
fmpz_mpoly_ctx_clear(ctx);
}

/* Check deflate with zero-stride does not divide by zero */
for (i = 0; i < 40 * flint_test_multiplier(); i++)
{
fmpz_mpoly_ctx_t ctx;
fmpz_mpoly_t x, res;
fmpz *stride, *shift;

fmpz_mpoly_ctx_init(ctx, 1, ORD_LEX);
fmpz_mpoly_init(x, ctx);
fmpz_mpoly_init(res, ctx);

stride = flint_malloc(ctx->minfo->nvars * sizeof(fmpz));
shift = flint_malloc(ctx->minfo->nvars * sizeof(fmpz));
fmpz_init(stride + 0);
fmpz_init(shift + 0);

/* Set x to just the generator */
fmpz_mpoly_gen(x, 0, ctx);

/* --- With zero shift --- */
fmpz_set_ui(shift + 0, 0);

/* Attempt to deflate x to 1 with a shift and stride 0. */
/* That is 1 -> (1 - 0) / 0 */
/* Division by zero should not be raised here */
fmpz_mpoly_deflate(res, x, shift, stride, ctx);

if (!fmpz_mpoly_equal_ui(res, 1, ctx))
{
printf("FAIL\n");
flint_printf("Check deflate with zero-shift and zero-stride\n");
fflush(stdout);
flint_abort();
}

/* --- With non-zero shift --- */
fmpz_set_ui(shift + 0, 1);

/* Attempt to deflate x to 1 with a shift of 1 and stride 0. */
/* That is 1 -> (1 - 1) / 0 */
/* Division by zero should not be raised here */
fmpz_mpoly_deflate(res, x, shift, stride, ctx);

if (!fmpz_mpoly_equal_ui(res, 1, ctx))
{
printf("FAIL\n");
flint_printf("Check deflate with non-zero shift and zero-stride\n");
fflush(stdout);
flint_abort();
}

fmpz_clear(stride + 0);
fmpz_clear(shift + 0);
flint_free(stride);
flint_free(shift);

fmpz_mpoly_clear(x, ctx);
fmpz_mpoly_ctx_clear(ctx);
}

TEST_FUNCTION_END(state);
}
10 changes: 8 additions & 2 deletions src/mpoly/monomials_deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@ void mpoly_monomials_deflate(ulong * Aexps, flint_bitcnt_t Abits,
for (j = 0; j < nvars; j++)
{
fmpz_sub(exps + j, exps + j, shift + j);
/* stride + j is allowed to be zero */
if (!fmpz_is_zero(exps + j))
/* stride + j is allowed to be zero, if it is then exps + j is
assumed to be zero, and the quotient is defined as zero */
if (!fmpz_is_zero(exps + j) && !fmpz_is_zero(stride + j))
{
FLINT_ASSERT(fmpz_divisible(exps + j, stride + j));
fmpz_divexact(exps + j, exps + j, stride + j);
}
else
{
fmpz_zero(exps + j);
}

}
FLINT_ASSERT(Abits >= mpoly_exp_bits_required_ffmpz(exps, mctx));
mpoly_set_monomial_ffmpz(Aexps + NA*i, exps, Abits, mctx);
Expand Down

0 comments on commit 1f98483

Please sign in to comment.