add poly set_trunc and set_trunc_round methods

This commit is contained in:
Fredrik Johansson 2016-09-02 16:01:15 +02:00
parent 58f2f4dc10
commit 9f0e392d2b
10 changed files with 320 additions and 0 deletions

View file

@ -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);

35
acb_poly/set_trunc.c Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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;
}

View file

@ -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)

35
arb_poly/set_trunc.c Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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);
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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;
}

View file

@ -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)

View file

@ -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)