diff --git a/arb.h b/arb.h index 0bb40c4a..f42c52f2 100644 --- a/arb.h +++ b/arb.h @@ -206,6 +206,8 @@ 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); + void _arb_digits_round_inplace(char * s, mp_bitcnt_t * shift, fmpz_t error, slong n, arf_rnd_t rnd); int arb_set_str(arb_t res, const char * inp, slong prec); diff --git a/arb/sgn.c b/arb/sgn.c new file mode 100644 index 00000000..6e4c6802 --- /dev/null +++ b/arb/sgn.c @@ -0,0 +1,45 @@ +/*============================================================================= + + This file is part of ARB. + + ARB is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + ARB is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ARB; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +=============================================================================*/ +/****************************************************************************** + + Copyright (C) 2016 Fredrik Johansson + +******************************************************************************/ + +#include "arb.h" + +void +arb_sgn(arb_t res, const arb_t x) +{ + if (arb_is_zero(x)) + { + arb_zero(res); + } + else if (arb_contains_zero(x)) + { + arf_zero(arb_midref(res)); + mag_one(arb_radref(res)); + } + else + { + arb_set_si(res, arf_sgn(arb_midref(x))); + } +} + diff --git a/arb/test/t-sgn.c b/arb/test/t-sgn.c new file mode 100644 index 00000000..b47cbeba --- /dev/null +++ b/arb/test/t-sgn.c @@ -0,0 +1,74 @@ +/*============================================================================= + + This file is part of ARB. + + ARB is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + ARB is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with ARB; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +=============================================================================*/ +/****************************************************************************** + + Copyright (C) 2016 Fredrik Johansson + +******************************************************************************/ + +#include "arb.h" + +int main() +{ + slong iter; + flint_rand_t state; + + flint_printf("sgn...."); + fflush(stdout); + + flint_randinit(state); + + for (iter = 0; iter < 1000; iter++) + { + arb_t a, b; + int result; + + arb_init(a); + arb_init(b); + + arb_randtest_special(a, state, 1 + n_randint(state, 200), 10); + arb_randtest_special(b, state, 1 + n_randint(state, 200), 10); + arb_sgn(b, a); + + result = 1; + if (arb_contains_zero(a)) + result = result & arb_contains_si(b, 0); + if (arb_contains_positive(a)) + result = result & arb_contains_si(b, 1); + if (arb_contains_negative(a)) + result = result & arb_contains_si(b, -1); + + if (!result) + { + flint_printf("FAIL\n\n"); + flint_printf("a = "); arb_print(a); flint_printf("\n\n"); + flint_printf("b = "); arb_print(b); flint_printf("\n\n"); + abort(); + } + + arb_clear(a); + arb_clear(b); + } + + flint_randclear(state); + flint_cleanup(); + flint_printf("PASS\n"); + return EXIT_SUCCESS; +} diff --git a/doc/source/arb.rst b/doc/source/arb.rst index 4be4c4f5..eabdda47 100644 --- a/doc/source/arb.rst +++ b/doc/source/arb.rst @@ -602,6 +602,11 @@ Arithmetic Sets *y* to the absolute value of *x*. No attempt is made to improve the interval represented by *x* if it contains zero. +.. function:: void arb_sgn(arb_t y, const arb_t x) + + Sets *y* to the sign function of *x*. The result is `[0 \pm 1]` if + *x* contains both zero and nonzero numbers. + .. function:: void arb_add(arb_t z, const arb_t x, const arb_t y, slong prec) .. function:: void arb_add_arf(arb_t z, const arb_t x, const arf_t y, slong prec)