From 56c6b4de2eb5eb5a894a86c1602739158511a42d Mon Sep 17 00:00:00 2001 From: fredrik Date: Thu, 25 Nov 2021 13:55:44 +0100 Subject: [PATCH] mag_pow_fmpz: allow negative exponents --- doc/source/mag.rst | 4 ++-- fmpr/pow_sloppy.c | 1 + mag/pow_fmpz.c | 14 ++++++++++++-- mag/test/t-pow_fmpz.c | 1 - mag/test/t-pow_fmpz_lower.c | 1 - 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/doc/source/mag.rst b/doc/source/mag.rst index 991b3357..48891427 100644 --- a/doc/source/mag.rst +++ b/doc/source/mag.rst @@ -378,13 +378,13 @@ Powers and logarithms .. function:: void mag_pow_fmpz(mag_t res, const mag_t x, const fmpz_t e) - Sets *res* to an upper bound for `x^e`. Requires `e \ge 0`. + Sets *res* to an upper bound for `x^e`. .. function:: void mag_pow_ui_lower(mag_t res, const mag_t x, ulong e) .. function:: void mag_pow_fmpz_lower(mag_t res, const mag_t x, const fmpz_t e) - Sets *res* to a lower bound for `x^e`. Requires `e \ge 0`. + Sets *res* to a lower bound for `x^e`. .. function:: void mag_sqrt(mag_t res, const mag_t x) diff --git a/fmpr/pow_sloppy.c b/fmpr/pow_sloppy.c index 59dc193f..8ed17cfd 100644 --- a/fmpr/pow_sloppy.c +++ b/fmpr/pow_sloppy.c @@ -35,6 +35,7 @@ fmpr_pow_sloppy_fmpz(fmpr_t y, const fmpr_t b, const fmpz_t e, ? FMPR_RND_UP : FMPR_RND_DOWN); fmpr_ui_div(y, UWORD(1), y, prec, rnd); fmpz_clear(f); + return; } if (y == b) diff --git a/mag/pow_fmpz.c b/mag/pow_fmpz.c index 6342e9f9..1a325f7f 100644 --- a/mag/pow_fmpz.c +++ b/mag/pow_fmpz.c @@ -16,7 +16,12 @@ mag_pow_fmpz(mag_t z, const mag_t x, const fmpz_t e) { if (fmpz_sgn(e) < 0) { - flint_abort(); + fmpz_t t; + fmpz_init(t); + fmpz_neg(t, e); + mag_inv(z, x); + mag_pow_fmpz(z, z, t); + fmpz_clear(t); } else if (!COEFF_IS_MPZ(*e)) { @@ -50,7 +55,12 @@ mag_pow_fmpz_lower(mag_t z, const mag_t x, const fmpz_t e) { if (fmpz_sgn(e) < 0) { - flint_abort(); + fmpz_t t; + fmpz_init(t); + fmpz_neg(t, e); + mag_inv_lower(z, x); + mag_pow_fmpz_lower(z, z, t); + fmpz_clear(t); } else if (!COEFF_IS_MPZ(*e)) { diff --git a/mag/test/t-pow_fmpz.c b/mag/test/t-pow_fmpz.c index edc83da7..09c80d97 100644 --- a/mag/test/t-pow_fmpz.c +++ b/mag/test/t-pow_fmpz.c @@ -38,7 +38,6 @@ int main() mag_randtest_special(xb, state, 80); mag_randtest_special(yb, state, 80); fmpz_randtest(e, state, 200); - fmpz_abs(e, e); mag_get_fmpr(x, xb); diff --git a/mag/test/t-pow_fmpz_lower.c b/mag/test/t-pow_fmpz_lower.c index 905ee35d..7a077543 100644 --- a/mag/test/t-pow_fmpz_lower.c +++ b/mag/test/t-pow_fmpz_lower.c @@ -38,7 +38,6 @@ int main() mag_randtest_special(xb, state, 80); mag_randtest_special(yb, state, 80); fmpz_randtest(e, state, 200); - fmpz_abs(e, e); mag_get_fmpr(x, xb);