diff --git a/arb.h b/arb.h index f4202c14..dad2fcbd 100644 --- a/arb.h +++ b/arb.h @@ -159,6 +159,7 @@ void arb_neg_round(arb_t x, const arb_t y, slong prec); void arb_abs(arb_t y, const arb_t x); void arb_sgn(arb_t res, const arb_t x); +int arb_sgn_nonzero(const arb_t x); void _arb_digits_round_inplace(char * s, mp_bitcnt_t * shift, fmpz_t error, slong n, arf_rnd_t rnd); diff --git a/arb/sgn_nonzero.c b/arb/sgn_nonzero.c new file mode 100644 index 00000000..50645085 --- /dev/null +++ b/arb/sgn_nonzero.c @@ -0,0 +1,23 @@ +/* + Copyright (C) 2019 Fredrik Johansson + + This file is part of Arb. + + Arb is free software: you can redistribute it and/or modify it under + the terms of the GNU Lesser General Public License (LGPL) as published + by the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. See . +*/ + +#include "arb.h" + +int +arb_sgn_nonzero(const arb_t x) +{ + if (arb_is_positive(x)) + return 1; + else if (arb_is_negative(x)) + return -1; + else + return 0; +} diff --git a/arb_calc/isolate_roots.c b/arb_calc/isolate_roots.c index 07471530..c97fdf2f 100644 --- a/arb_calc/isolate_roots.c +++ b/arb_calc/isolate_roots.c @@ -15,18 +15,6 @@ #define BLOCK_ISOLATED_ZERO 1 #define BLOCK_UNKNOWN 2 -/* 0 means that it *could* be zero; otherwise +/- 1 */ -static __inline__ int -_arb_sign(const arb_t t) -{ - if (arb_is_positive(t)) - return 1; - else if (arb_is_negative(t)) - return -1; - else - return 0; -} - static int check_block(arb_calc_func_t func, void * param, const arf_interval_t block, int asign, int bsign, slong prec) @@ -175,11 +163,11 @@ arb_calc_isolate_roots(arf_interval_ptr * blocks, int ** flags, arb_set_arf(m, &block->a); func(v, m, param, 1, prec); - asign = _arb_sign(v); + asign = arb_sgn_nonzero(v); arb_set_arf(m, &block->b); func(v, m, param, 1, prec); - bsign = _arb_sign(v); + bsign = arb_sgn_nonzero(v); arb_clear(m); arb_clear(v); diff --git a/arb_calc/refine_root_bisect.c b/arb_calc/refine_root_bisect.c index 61e608b1..dcd6e0ff 100644 --- a/arb_calc/refine_root_bisect.c +++ b/arb_calc/refine_root_bisect.c @@ -13,18 +13,6 @@ /* TODO: refactor/combine some of this code with isolate_roots.c */ -/* 0 means that it *could* be zero; otherwise +/- 1 */ -static __inline__ int -_arb_sign(const arb_t t) -{ - if (arb_is_positive(t)) - return 1; - else if (arb_is_negative(t)) - return -1; - else - return 0; -} - int arb_calc_partition(arf_interval_t L, arf_interval_t R, arb_calc_func_t func, void * param, const arf_interval_t block, slong prec) { @@ -43,7 +31,7 @@ int arb_calc_partition(arf_interval_t L, arf_interval_t R, /* Evaluate and get sign at midpoint */ arb_set_arf(m, u); func(t, m, param, 1, prec); - msign = _arb_sign(t); + msign = arb_sgn_nonzero(t); /* L, R = block, split at midpoint */ arf_set(&L->a, &block->a); @@ -73,11 +61,11 @@ int arb_calc_refine_root_bisect(arf_interval_t r, arb_calc_func_t func, arb_set_arf(m, &start->a); func(v, m, param, 1, prec); - asign = _arb_sign(v); + asign = arb_sgn_nonzero(v); arb_set_arf(m, &start->b); func(v, m, param, 1, prec); - bsign = _arb_sign(v); + bsign = arb_sgn_nonzero(v); /* must have proper sign changes */ if (asign == 0 || bsign == 0 || asign == bsign) diff --git a/doc/source/arb.rst b/doc/source/arb.rst index df47b065..bdacad23 100644 --- a/doc/source/arb.rst +++ b/doc/source/arb.rst @@ -599,7 +599,7 @@ Comparisons Returns nonzero iff the given number (or ball) *y* is contained in the interval represented by *x*. - If *x* is contains NaN, this function always returns nonzero (as it + If *x* contains NaN, this function always returns nonzero (as it could represent anything, and in particular could represent all the points included in *y*). If *y* contains NaN and *x* does not, it always returns zero. @@ -666,6 +666,12 @@ Arithmetic Sets *y* to the sign function of *x*. The result is `[0 \pm 1]` if *x* contains both zero and nonzero numbers. +.. function:: int arb_sgn_nonzero(const arb_t x) + + Returns 1 if *x* is strictly positive, -1 if *x* is strictly negative, + and 0 if *x* is zero or a ball containing zero so that its sign + is not determined. + .. function:: void arb_min(arb_t z, const arb_t x, const arb_t y, slong prec) .. function:: void arb_max(arb_t z, const arb_t x, const arb_t y, slong prec)