diff --git a/acb_poly.h b/acb_poly.h index b43be299..65a71eed 100644 --- a/acb_poly.h +++ b/acb_poly.h @@ -173,6 +173,10 @@ void acb_poly_set(acb_poly_t dest, const acb_poly_t src); void acb_poly_set_round(acb_poly_t dest, const acb_poly_t src, slong prec); +void acb_poly_set_trunc(acb_poly_t res, const acb_poly_t poly, slong n); + +void acb_poly_set_trunc_round(acb_poly_t res, const acb_poly_t poly, slong n, slong prec); + void acb_poly_set_arb_poly(acb_poly_t poly, const arb_poly_t re); void acb_poly_set2_arb_poly(acb_poly_t poly, const arb_poly_t re, const arb_poly_t im); diff --git a/acb_poly/set_trunc.c b/acb_poly/set_trunc.c new file mode 100644 index 00000000..4aa57bd5 --- /dev/null +++ b/acb_poly/set_trunc.c @@ -0,0 +1,35 @@ +/* + Copyright (C) 2016 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 "acb_poly.h" + + +void +acb_poly_set_trunc(acb_poly_t res, const acb_poly_t poly, slong n) +{ + if (poly == res) + { + acb_poly_truncate(res, n); + } + else + { + slong rlen; + + rlen = FLINT_MIN(n, poly->length); + while (rlen > 0 && acb_is_zero(poly->coeffs + rlen - 1)) + rlen--; + + acb_poly_fit_length(res, rlen); + _acb_vec_set(res->coeffs, poly->coeffs, rlen); + _acb_poly_set_length(res, rlen); + } +} + diff --git a/acb_poly/set_trunc_round.c b/acb_poly/set_trunc_round.c new file mode 100644 index 00000000..4bc41630 --- /dev/null +++ b/acb_poly/set_trunc_round.c @@ -0,0 +1,36 @@ +/* + Copyright (C) 2016 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 "acb_poly.h" + + +void +acb_poly_set_trunc_round(acb_poly_t res, const acb_poly_t poly, slong n, slong prec) +{ + if (poly == res) + { + acb_poly_truncate(res, n); + _acb_vec_set_round(res->coeffs, res->coeffs, res->length, prec); + } + else + { + slong rlen; + + rlen = FLINT_MIN(n, poly->length); + while (rlen > 0 && acb_is_zero(poly->coeffs + rlen - 1)) + rlen--; + + acb_poly_fit_length(res, rlen); + _acb_vec_set_round(res->coeffs, poly->coeffs, rlen, prec); + _acb_poly_set_length(res, rlen); + } +} + diff --git a/acb_poly/test/t-set_trunc_round.c b/acb_poly/test/t-set_trunc_round.c new file mode 100644 index 00000000..b8e29689 --- /dev/null +++ b/acb_poly/test/t-set_trunc_round.c @@ -0,0 +1,79 @@ +/* + Copyright (C) 2016 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 "acb_poly.h" + +int +main(void) +{ + int iter; + flint_rand_t state; + + flint_printf("set_trunc_round...."); + fflush(stdout); + + flint_randinit(state); + + for (iter = 0; iter < 1000 * arb_test_multiplier(); iter++) + { + acb_poly_t a, b, c, d, e; + slong n, prec; + + acb_poly_init(a); + acb_poly_init(b); + acb_poly_init(c); + acb_poly_init(d); + acb_poly_init(e); + + acb_poly_randtest(a, state, n_randint(state, 10), 2 + n_randint(state, 200), 10); + acb_poly_randtest(b, state, n_randint(state, 10), 2 + n_randint(state, 200), 10); + acb_poly_randtest(c, state, n_randint(state, 10), 2 + n_randint(state, 200), 10); + acb_poly_randtest(d, state, n_randint(state, 10), 2 + n_randint(state, 200), 10); + acb_poly_randtest(e, state, n_randint(state, 10), 2 + n_randint(state, 200), 10); + + n = n_randint(state, 10); + prec = 2 + n_randint(state, 200); + + acb_poly_set_trunc(b, a, n); + acb_poly_set_round(b, b, prec); + + acb_poly_set_round(c, a, prec); + acb_poly_set_trunc(c, c, n); + + acb_poly_set_trunc_round(d, a, n, prec); + + acb_poly_set(e, a); + acb_poly_set_trunc_round(e, e, n, prec); + + if (!acb_poly_equal(b, c) || !acb_poly_equal(c, d) || !acb_poly_equal(d, e)) + { + flint_printf("FAIL\n\n"); + acb_poly_printd(a, 50), flint_printf("\n\n"); + acb_poly_printd(b, 50), flint_printf("\n\n"); + acb_poly_printd(c, 50), flint_printf("\n\n"); + acb_poly_printd(d, 50), flint_printf("\n\n"); + acb_poly_printd(e, 50), flint_printf("\n\n"); + abort(); + } + + acb_poly_clear(a); + acb_poly_clear(b); + acb_poly_clear(c); + acb_poly_clear(d); + acb_poly_clear(e); + } + + flint_randclear(state); + flint_cleanup(); + flint_printf("PASS\n"); + return 0; +} + diff --git a/arb_poly.h b/arb_poly.h index b580c43d..684d0da4 100644 --- a/arb_poly.h +++ b/arb_poly.h @@ -65,6 +65,10 @@ void arb_poly_set(arb_poly_t poly, const arb_poly_t src); void arb_poly_set_round(arb_poly_t poly, const arb_poly_t src, slong prec); +void arb_poly_set_trunc(arb_poly_t res, const arb_poly_t poly, slong n); + +void arb_poly_set_trunc_round(arb_poly_t res, const arb_poly_t poly, slong n, slong prec); + /* Basic manipulation */ ARB_POLY_INLINE slong arb_poly_length(const arb_poly_t poly) diff --git a/arb_poly/set_trunc.c b/arb_poly/set_trunc.c new file mode 100644 index 00000000..5f1206fe --- /dev/null +++ b/arb_poly/set_trunc.c @@ -0,0 +1,35 @@ +/* + Copyright (C) 2016 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_poly.h" + + +void +arb_poly_set_trunc(arb_poly_t res, const arb_poly_t poly, slong n) +{ + if (poly == res) + { + arb_poly_truncate(res, n); + } + else + { + slong rlen; + + rlen = FLINT_MIN(n, poly->length); + while (rlen > 0 && arb_is_zero(poly->coeffs + rlen - 1)) + rlen--; + + arb_poly_fit_length(res, rlen); + _arb_vec_set(res->coeffs, poly->coeffs, rlen); + _arb_poly_set_length(res, rlen); + } +} + diff --git a/arb_poly/set_trunc_round.c b/arb_poly/set_trunc_round.c new file mode 100644 index 00000000..321c7b4b --- /dev/null +++ b/arb_poly/set_trunc_round.c @@ -0,0 +1,36 @@ +/* + Copyright (C) 2016 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_poly.h" + + +void +arb_poly_set_trunc_round(arb_poly_t res, const arb_poly_t poly, slong n, slong prec) +{ + if (poly == res) + { + arb_poly_truncate(res, n); + _arb_vec_set_round(res->coeffs, res->coeffs, res->length, prec); + } + else + { + slong rlen; + + rlen = FLINT_MIN(n, poly->length); + while (rlen > 0 && arb_is_zero(poly->coeffs + rlen - 1)) + rlen--; + + arb_poly_fit_length(res, rlen); + _arb_vec_set_round(res->coeffs, poly->coeffs, rlen, prec); + _arb_poly_set_length(res, rlen); + } +} + diff --git a/arb_poly/test/t-set_trunc_round.c b/arb_poly/test/t-set_trunc_round.c new file mode 100644 index 00000000..3243ee02 --- /dev/null +++ b/arb_poly/test/t-set_trunc_round.c @@ -0,0 +1,79 @@ +/* + Copyright (C) 2016 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_poly.h" + +int +main(void) +{ + int iter; + flint_rand_t state; + + flint_printf("set_trunc_round...."); + fflush(stdout); + + flint_randinit(state); + + for (iter = 0; iter < 1000 * arb_test_multiplier(); iter++) + { + arb_poly_t a, b, c, d, e; + slong n, prec; + + arb_poly_init(a); + arb_poly_init(b); + arb_poly_init(c); + arb_poly_init(d); + arb_poly_init(e); + + arb_poly_randtest(a, state, n_randint(state, 10), 2 + n_randint(state, 200), 10); + arb_poly_randtest(b, state, n_randint(state, 10), 2 + n_randint(state, 200), 10); + arb_poly_randtest(c, state, n_randint(state, 10), 2 + n_randint(state, 200), 10); + arb_poly_randtest(d, state, n_randint(state, 10), 2 + n_randint(state, 200), 10); + arb_poly_randtest(e, state, n_randint(state, 10), 2 + n_randint(state, 200), 10); + + n = n_randint(state, 10); + prec = 2 + n_randint(state, 200); + + arb_poly_set_trunc(b, a, n); + arb_poly_set_round(b, b, prec); + + arb_poly_set_round(c, a, prec); + arb_poly_set_trunc(c, c, n); + + arb_poly_set_trunc_round(d, a, n, prec); + + arb_poly_set(e, a); + arb_poly_set_trunc_round(e, e, n, prec); + + if (!arb_poly_equal(b, c) || !arb_poly_equal(c, d) || !arb_poly_equal(d, e)) + { + flint_printf("FAIL\n\n"); + arb_poly_printd(a, 50), flint_printf("\n\n"); + arb_poly_printd(b, 50), flint_printf("\n\n"); + arb_poly_printd(c, 50), flint_printf("\n\n"); + arb_poly_printd(d, 50), flint_printf("\n\n"); + arb_poly_printd(e, 50), flint_printf("\n\n"); + abort(); + } + + arb_poly_clear(a); + arb_poly_clear(b); + arb_poly_clear(c); + arb_poly_clear(d); + arb_poly_clear(e); + } + + flint_randclear(state); + flint_cleanup(); + flint_printf("PASS\n"); + return 0; +} + diff --git a/doc/source/acb_poly.rst b/doc/source/acb_poly.rst index 8dc7b298..868fdbbb 100644 --- a/doc/source/acb_poly.rst +++ b/doc/source/acb_poly.rst @@ -101,6 +101,12 @@ Basic properties and manipulation Sets *dest* to a copy of *src*, rounded to *prec* bits. +.. function:: void acb_poly_set_trunc(acb_poly_t dest, const acb_poly_t src, slong n) + +.. function:: void acb_poly_set_trunc_round(acb_poly_t dest, const acb_poly_t src, slong n, slong prec) + + Sets *dest* to a copy of *src*, truncated to length *n* and rounded to *prec* bits. + .. function:: void acb_poly_set_coeff_si(acb_poly_t poly, slong n, slong c) .. function:: void acb_poly_set_coeff_acb(acb_poly_t poly, slong n, const acb_t c) diff --git a/doc/source/arb_poly.rst b/doc/source/arb_poly.rst index 4b107e84..226b0f1a 100644 --- a/doc/source/arb_poly.rst +++ b/doc/source/arb_poly.rst @@ -95,6 +95,12 @@ Basic manipulation Sets *dest* to a copy of *src*, rounded to *prec* bits. +.. function:: void arb_poly_set_trunc(arb_poly_t dest, const arb_poly_t src, slong n) + +.. function:: void arb_poly_set_trunc_round(arb_poly_t dest, const arb_poly_t src, slong n, slong prec) + + Sets *dest* to a copy of *src*, truncated to length *n* and rounded to *prec* bits. + .. function:: void arb_poly_set_coeff_si(arb_poly_t poly, slong n, slong c) .. function:: void arb_poly_set_coeff_arb(arb_poly_t poly, slong n, const arb_t c)