90 changes: 90 additions & 0 deletions nmod_poly/test/t-evaluate_nmod_vec_fast.c
@@ -0,0 +1,90 @@
/*=============================================================================
This file is part of FLINT.
FLINT 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.
FLINT 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 FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2010 William Hart
Copyright (C) 2011, 2012 Fredrik Johansson
******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <mpir.h>
#include "flint.h"
#include "nmod_poly.h"
#include "ulong_extras.h"

int
main(void)
{
int i, result = 1;
flint_rand_t state;
flint_randinit(state);

printf("evaluate_nmod_vec_fast....");
fflush(stdout);

for (i = 0; i < 10000; i++)
{
nmod_poly_t P, Q;
mp_ptr x, y, z;
mp_limb_t mod;
long j, n, npoints;

mod = n_randtest_prime(state, 0);
npoints = n_randint(state, 100);
n = n_randint(state, 100);

nmod_poly_init(P, mod);
nmod_poly_init(Q, mod);
x = _nmod_vec_init(npoints);
y = _nmod_vec_init(npoints);
z = _nmod_vec_init(npoints);

nmod_poly_randtest(P, state, n);

for (j = 0; j < npoints; j++)
x[j] = n_randint(state, mod);

nmod_poly_evaluate_nmod_vec(y, P, x, npoints);
nmod_poly_evaluate_nmod_vec_fast(z, P, x, npoints);

result = _nmod_vec_equal(y, z, npoints);

if (!result)
{
printf("FAIL:\n");
printf("mod=%lu, n=%ld, npoints=%ld\n\n", mod, n, npoints);
printf("P: "); nmod_poly_print(P); printf("\n\n");
abort();
}

nmod_poly_clear(P);
nmod_poly_clear(Q);
_nmod_vec_clear(x);
_nmod_vec_clear(y);
_nmod_vec_clear(z);
}

flint_randclear(state);

printf("PASS\n");
return 0;
}
2 changes: 0 additions & 2 deletions nmod_poly/test/t-interpolate_nmod_vec.c
Expand Up @@ -30,8 +30,6 @@
#include "flint.h"
#include "nmod_poly.h"
#include "ulong_extras.h"
#include "fmpz.h"


int
main(void)
Expand Down
89 changes: 89 additions & 0 deletions nmod_poly/test/t-interpolate_nmod_vec_fast.c
@@ -0,0 +1,89 @@
/*=============================================================================
This file is part of FLINT.
FLINT 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.
FLINT 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 FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2010 William Hart
Copyright (C) 2011, 2012 Fredrik Johansson
******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <mpir.h>
#include "flint.h"
#include "nmod_poly.h"
#include "ulong_extras.h"

int
main(void)
{
int i, result = 1;
flint_rand_t state;
flint_randinit(state);

printf("interpolate_nmod_vec_fast....");
fflush(stdout);

for (i = 0; i < 10000; i++)
{
nmod_poly_t P, Q;
mp_ptr x, y;
mp_limb_t mod;
long j, n, npoints;

mod = n_randtest_prime(state, 0);
npoints = n_randint(state, FLINT_MIN(100, mod));
n = n_randint(state, npoints + 1);

nmod_poly_init(P, mod);
nmod_poly_init(Q, mod);
x = _nmod_vec_init(npoints);
y = _nmod_vec_init(npoints);

nmod_poly_randtest(P, state, n);

for (j = 0; j < npoints; j++)
x[j] = j;

nmod_poly_evaluate_nmod_vec_fast(y, P, x, npoints);
nmod_poly_interpolate_nmod_vec_fast(Q, x, y, npoints);

result = nmod_poly_equal(P, Q);

if (!result)
{
printf("FAIL:\n");
printf("mod=%lu, n=%ld, npoints=%ld\n\n", mod, n, npoints);
nmod_poly_print(P), printf("\n\n");
nmod_poly_print(Q), printf("\n\n");
abort();
}

nmod_poly_clear(P);
nmod_poly_clear(Q);
_nmod_vec_clear(x);
_nmod_vec_clear(y);
}

flint_randclear(state);

printf("PASS\n");
return 0;
}
123 changes: 123 additions & 0 deletions nmod_poly/tree.c
@@ -0,0 +1,123 @@
/*=============================================================================
This file is part of FLINT.
FLINT 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.
FLINT 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 FLINT; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
=============================================================================*/
/******************************************************************************
Copyright (C) 2011 Fredrik Johansson
******************************************************************************/

#include <mpir.h>
#include "flint.h"
#include "ulong_extras.h"
#include "nmod_vec.h"
#include "nmod_poly.h"

mp_ptr * _nmod_poly_tree_alloc(long len)
{
mp_ptr * tree = NULL;

if (len)
{
long i, height = FLINT_CLOG2(len);

tree = flint_malloc(sizeof(mp_ptr) * (height + 1));
for (i = 0; i <= height; i++)
tree[i] = _nmod_vec_init(len + (len >> i) + 1);
}

return tree;
}

void _nmod_poly_tree_free(mp_ptr * tree, long len)
{
if (len)
{
long i, height = FLINT_CLOG2(len);

for (i = 0; i <= height; i++)
flint_free(tree[i]);

flint_free(tree);
}
}

void
_nmod_poly_tree_build(mp_ptr * tree, mp_srcptr roots, long len, nmod_t mod)
{
long height, pow, left, i;
mp_ptr pa, pb;

if (len == 0)
return;

height = FLINT_CLOG2(len);

/* zeroth level, (x-a) */
for (i = 0; i < len; i++)
{
tree[0][2 * i + 1] = 1;
tree[0][2 * i] = nmod_neg(roots[i], mod);
}

/* first level, (x-a)(x-b) = x^2 + (-a-b)*x + a*b */
if (height > 1)
{
pa = tree[1];

for (i = 0; i < len / 2; i++)
{
mp_limb_t a, b;

a = roots[2 * i];
b = roots[2 * i + 1];

pa[3 * i] = nmod_mul(a, b, mod);
pa[3 * i + 1] = nmod_neg(nmod_add(a, b, mod), mod);
pa[3 * i + 2] = 1;
}

if (len & 1)
{
pa[3 * (len / 2)] = nmod_neg(roots[len-1], mod);
pa[3 * (len / 2) + 1] = 1;
}
}

for (i = 1; i < height - 1; i++)
{
left = len;
pow = 1L << i;
pa = tree[i];
pb = tree[i + 1];

while (left >= 2 * pow)
{
_nmod_poly_mul(pb, pa, pow + 1, pa + pow + 1, pow + 1, mod);
left -= 2 * pow;
pa += 2 * pow + 2;
pb += 2 * pow + 1;
}

if (left > pow)
_nmod_poly_mul(pb, pa, pow + 1, pa + pow + 1, left - pow + 1, mod);
else if (left > 0)
_nmod_vec_set(pb, pa, left + 1);
}
}