Skip to content

Commit 1f98483

Browse files
Merge pull request #2066 from Jake-Moss/monomial_deflation_div_by_zero
Fix division by zero in `monomials_deflate.c`.
2 parents 3dbe239 + 044117b commit 1f98483

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

src/fmpz_mpoly/test/t-inflate_deflate.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,65 @@ TEST_FUNCTION_START(fmpz_mpoly_inflate_deflate, state)
196196
fmpz_mpoly_ctx_clear(ctx);
197197
}
198198

199+
/* Check deflate with zero-stride does not divide by zero */
200+
for (i = 0; i < 40 * flint_test_multiplier(); i++)
201+
{
202+
fmpz_mpoly_ctx_t ctx;
203+
fmpz_mpoly_t x, res;
204+
fmpz *stride, *shift;
205+
206+
fmpz_mpoly_ctx_init(ctx, 1, ORD_LEX);
207+
fmpz_mpoly_init(x, ctx);
208+
fmpz_mpoly_init(res, ctx);
209+
210+
stride = flint_malloc(ctx->minfo->nvars * sizeof(fmpz));
211+
shift = flint_malloc(ctx->minfo->nvars * sizeof(fmpz));
212+
fmpz_init(stride + 0);
213+
fmpz_init(shift + 0);
214+
215+
/* Set x to just the generator */
216+
fmpz_mpoly_gen(x, 0, ctx);
217+
218+
/* --- With zero shift --- */
219+
fmpz_set_ui(shift + 0, 0);
220+
221+
/* Attempt to deflate x to 1 with a shift and stride 0. */
222+
/* That is 1 -> (1 - 0) / 0 */
223+
/* Division by zero should not be raised here */
224+
fmpz_mpoly_deflate(res, x, shift, stride, ctx);
225+
226+
if (!fmpz_mpoly_equal_ui(res, 1, ctx))
227+
{
228+
printf("FAIL\n");
229+
flint_printf("Check deflate with zero-shift and zero-stride\n");
230+
fflush(stdout);
231+
flint_abort();
232+
}
233+
234+
/* --- With non-zero shift --- */
235+
fmpz_set_ui(shift + 0, 1);
236+
237+
/* Attempt to deflate x to 1 with a shift of 1 and stride 0. */
238+
/* That is 1 -> (1 - 1) / 0 */
239+
/* Division by zero should not be raised here */
240+
fmpz_mpoly_deflate(res, x, shift, stride, ctx);
241+
242+
if (!fmpz_mpoly_equal_ui(res, 1, ctx))
243+
{
244+
printf("FAIL\n");
245+
flint_printf("Check deflate with non-zero shift and zero-stride\n");
246+
fflush(stdout);
247+
flint_abort();
248+
}
249+
250+
fmpz_clear(stride + 0);
251+
fmpz_clear(shift + 0);
252+
flint_free(stride);
253+
flint_free(shift);
254+
255+
fmpz_mpoly_clear(x, ctx);
256+
fmpz_mpoly_ctx_clear(ctx);
257+
}
258+
199259
TEST_FUNCTION_END(state);
200260
}

src/mpoly/monomials_deflate.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,18 @@ void mpoly_monomials_deflate(ulong * Aexps, flint_bitcnt_t Abits,
3636
for (j = 0; j < nvars; j++)
3737
{
3838
fmpz_sub(exps + j, exps + j, shift + j);
39-
/* stride + j is allowed to be zero */
40-
if (!fmpz_is_zero(exps + j))
39+
/* stride + j is allowed to be zero, if it is then exps + j is
40+
assumed to be zero, and the quotient is defined as zero */
41+
if (!fmpz_is_zero(exps + j) && !fmpz_is_zero(stride + j))
4142
{
4243
FLINT_ASSERT(fmpz_divisible(exps + j, stride + j));
4344
fmpz_divexact(exps + j, exps + j, stride + j);
4445
}
46+
else
47+
{
48+
fmpz_zero(exps + j);
49+
}
50+
4551
}
4652
FLINT_ASSERT(Abits >= mpoly_exp_bits_required_ffmpz(exps, mctx));
4753
mpoly_set_monomial_ffmpz(Aexps + NA*i, exps, Abits, mctx);

0 commit comments

Comments
 (0)