add some fmpcb_poly helper functions

This commit is contained in:
Fredrik Johansson 2013-07-27 10:04:38 +02:00
parent 872e2b94c7
commit 4e61ef9e7a
8 changed files with 316 additions and 2 deletions

View file

@ -61,7 +61,18 @@ Basic properties and manipulation
.. function:: long fmpcb_poly_length(const fmpcb_poly_t poly)
Returns the length of the polynomial.
Returns the length of *poly*, i.e. zero if *poly* is
identically zero, and otherwise one more than the index
of the highest term that is not identically zero.
.. function:: long fmpcb_poly_degree(const fmpcb_poly_t poly)
Returns the degree of *poly*, defined as one less than its length.
Note that if one or several leading coefficients are balls
containing zero, this value can be larger than the true
degree of the exact polynomial represented by *poly*,
so the return value of this function is effectively
an upper bound.
.. function:: void fmpcb_poly_zero(fmpcb_poly_t poly)
@ -75,6 +86,23 @@ Basic properties and manipulation
Sets *dest* to a copy of *src*.
.. function:: void fmpcb_poly_set_coeff_si(fmpcb_poly_t poly, long n, long c)
.. function:: void fmpcb_poly_set_coeff_fmpcb(fmpcb_poly_t poly, long n, const fmpcb_t c)
Sets the coefficient with index *n* in *poly* to the value *c*.
We require that *n* is nonnegative.
.. function:: void fmpcb_poly_get_coeff_fmpcb(fmpcb_t v, const fmpcb_poly_t poly, long n)
-
Sets *v* to the value of the coefficient with index *n* in *poly*.
We require that *n* is nonnegative.
.. macro:: fmpcb_poly_get_coeff_ptr(poly, n)
Given `n \ge 0`, returns a pointer to coefficient *n* of *poly*,
or *NULL* if *n* exceeds the length of *poly*.
Input and output
-------------------------------------------------------------------------------

View file

@ -75,7 +75,7 @@ Basic manipulation
.. macro:: fmprb_poly_get_coeff_ptr(poly, n)
Given `n >= 0`, returns a pointer to coefficient *n* of *poly*,
Given `n \ge 0`, returns a pointer to coefficient *n* of *poly*,
or *NULL* if *n* exceeds the length of *poly*.
.. function:: void _fmprb_poly_shift_right(fmprb_ptr res, fmprb_srcptr poly, long len, long n)

View file

@ -67,6 +67,11 @@ static __inline__ long fmpcb_poly_length(const fmpcb_poly_t poly)
return poly->length;
}
static __inline__ long fmpcb_poly_degree(const fmpcb_poly_t poly)
{
return poly->length - 1;
}
static __inline__ void fmpcb_poly_zero(fmpcb_poly_t poly)
{
poly->length = 0;
@ -80,6 +85,15 @@ fmpcb_poly_one(fmpcb_poly_t poly)
_fmpcb_poly_set_length(poly, 1);
}
void fmpcb_poly_set_coeff_si(fmpcb_poly_t poly, long n, long x);
void fmpcb_poly_set_coeff_fmpcb(fmpcb_poly_t poly, long n, const fmpcb_t x);
void fmpcb_poly_get_coeff_fmpcb(fmpcb_t x, const fmpcb_poly_t poly, long n);
#define fmpcb_poly_get_coeff_ptr(poly, n) \
((n) < (poly)->length ? (poly)->coeffs + (n) : NULL)
void fmpcb_poly_printd(const fmpcb_poly_t poly, long digits);
void _fmpcb_poly_evaluate(fmpcb_t res, fmpcb_srcptr f, long len, const fmpcb_t a, long prec);

View file

@ -0,0 +1,36 @@
/*=============================================================================
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) 2013 Fredrik Johansson
******************************************************************************/
#include "fmpcb_poly.h"
void
fmpcb_poly_get_coeff_fmpcb(fmpcb_t x, const fmpcb_poly_t poly, long n)
{
if (n < poly->length)
fmpcb_set(x, poly->coeffs + n);
else
fmpcb_zero(x);
}

View file

@ -0,0 +1,42 @@
/*=============================================================================
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) 2013 Fredrik Johansson
******************************************************************************/
#include "fmpcb_poly.h"
void
fmpcb_poly_set_coeff_fmpcb(fmpcb_poly_t poly, long n, const fmpcb_t x)
{
fmpcb_poly_fit_length(poly, n + 1);
if (n + 1 > poly->length)
{
_fmpcb_vec_zero(poly->coeffs + poly->length, n - poly->length);
poly->length = n + 1;
}
fmpcb_set(poly->coeffs + n, x);
_fmpcb_poly_normalise(poly);
}

42
fmpcb_poly/set_coeff_si.c Normal file
View file

@ -0,0 +1,42 @@
/*=============================================================================
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) 2013 Fredrik Johansson
******************************************************************************/
#include "fmpcb_poly.h"
void
fmpcb_poly_set_coeff_si(fmpcb_poly_t poly, long n, long x)
{
fmpcb_poly_fit_length(poly, n + 1);
if (n + 1 > poly->length)
{
_fmpcb_vec_zero(poly->coeffs + poly->length, n - poly->length);
poly->length = n + 1;
}
fmpcb_set_si(poly->coeffs + n, x);
_fmpcb_poly_normalise(poly);
}

View file

@ -0,0 +1,73 @@
/*=============================================================================
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) 2010 Sebastian Pancratz
Copyright (C) 2013 Fredrik Johansson
******************************************************************************/
#include "fmpcb_poly.h"
int
main(void)
{
int i, result;
flint_rand_t state;
printf("get_coeff_ptr....");
fflush(stdout);
flint_randinit(state);
for (i = 0; i < 1000; i++)
{
fmpcb_poly_t A;
fmpcb_t a;
long n = n_randint(state, 100);
fmpcb_poly_init(A);
fmpcb_poly_randtest(A, state, n_randint(state, 100), 100, 10);
fmpcb_init(a);
fmpcb_poly_get_coeff_fmpcb(a, A, n);
result = n < fmpcb_poly_length(A) ?
fmpcb_equal(a, fmpcb_poly_get_coeff_ptr(A, n)) :
fmpcb_poly_get_coeff_ptr(A, n) == NULL;
if (!result)
{
printf("FAIL:\n");
printf("A = "), fmpcb_poly_printd(A, 10), printf("\n\n");
printf("a = "), fmpcb_print(a), printf("\n\n");
printf("n = %ld\n\n", n);
abort();
}
fmpcb_poly_clear(A);
fmpcb_clear(a);
}
flint_randclear(state);
_fmpz_cleanup();
printf("PASS\n");
return 0;
}

View file

@ -0,0 +1,79 @@
/*=============================================================================
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) 2009 William Hart
Copyright (C) 2013 Fredrik Johansson
******************************************************************************/
#include "fmpcb_poly.h"
int
main(void)
{
int i, j, result;
flint_rand_t state;
printf("get/set_coeff_fmpcb....");
fflush(stdout);
flint_randinit(state);
for (i = 0; i < 100; i++)
{
fmpcb_poly_t a;
fmpcb_t x1, x2;
slong coeff, len;
fmpcb_poly_init(a);
fmpcb_init(x1);
fmpcb_init(x2);
len = n_randint(state, 100) + 1;
for (j = 0; j < 100; j++)
{
fmpcb_randtest(x1, state, 2 + n_randint(state, 200), 10);
coeff = n_randint(state, len);
fmpcb_poly_set_coeff_fmpcb(a, coeff, x1);
fmpcb_poly_get_coeff_fmpcb(x2, a, coeff);
result = (fmpcb_equal(x1, x2));
if (!result)
{
printf("FAIL:\n");
printf("x1 = "), fmpcb_print(x1), printf("\n");
printf("x2 = "), fmpcb_print(x2), printf("\n");
printf("coeff = %ld, length = %ld\n", coeff, len);
abort();
}
}
fmpcb_clear(x1);
fmpcb_clear(x2);
fmpcb_poly_clear(a);
}
flint_randclear(state);
_fmpz_cleanup();
printf("PASS\n");
return 0;
}