ALL: Add flint

This commit is contained in:
2014-05-19 00:03:37 +02:00
parent a15ef46ea6
commit d51d8e3652
3752 changed files with 446416 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_add(fmpz *res, const fmpz *poly1, slong len1,
const fmpz *poly2, slong len2, const fmpz_t p)
{
slong i, len = FLINT_MAX(len1, len2);
_fmpz_poly_add(res, poly1, len1, poly2, len2);
for (i = 0; i < len; i++)
{
if (fmpz_cmpabs(res + i, p) >= 0)
fmpz_sub(res + i, res + i, p);
}
}
void fmpz_mod_poly_add(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly1, const fmpz_mod_poly_t poly2)
{
slong max = FLINT_MAX(poly1->length, poly2->length);
fmpz_mod_poly_fit_length(res, max);
_fmpz_mod_poly_add(res->coeffs, poly1->coeffs, poly1->length,
poly2->coeffs, poly2->length, &(res->p));
_fmpz_mod_poly_set_length(res, max);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,42 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include <stdlib.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_clear(fmpz_mod_poly_t poly)
{
slong i;
for (i = 0; i < poly->alloc; i++) /* Clean up any mpz_t's */
_fmpz_demote(poly->coeffs + i);
if (poly->coeffs)
flint_free(poly->coeffs); /* clean up ordinary coeffs */
fmpz_clear(&(poly->p));
}

View File

@@ -0,0 +1,160 @@
/*=============================================================================
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 copy1 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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz_mod_poly.h"
/*
Let i be such that 2^{i} < len1 <= 2^{i+1}.
Note that the jth step of the recursion requires temporary space
of size no more than (len2 - 1)(2^j - 1) + 1. Note the smallest
step j=0 doesn't require any temporary space and the largest step
has j = i, and hence the sum is
sum_{j=1}^i [(len2 - 1) (2^j - 1) + 1]
= (len2 - 1)(2^{i+1} - 2) - (len2 - 2) i
*/
void _fmpz_mod_poly_compose_divconquer_recursive(fmpz *res,
const fmpz *poly1, slong len1, fmpz **pow2, slong len2, fmpz *v,
const fmpz_t p)
{
if (len1 == 1)
{
fmpz_set(res, poly1);
}
else if (len1 == 2)
{
_fmpz_mod_poly_scalar_mul_fmpz(res, pow2[0], len2, poly1 + 1, p);
fmpz_add(res, res, poly1);
if (fmpz_cmpabs(res, p) >= 0)
fmpz_sub(res, res, p);
}
else
{
const slong i = FLINT_BIT_COUNT(len1 - 1) - 1;
fmpz *w = v + ((WORD(1) << i) - 1) * (len2 - 1) + 1;
_fmpz_mod_poly_compose_divconquer_recursive(v,
poly1 + (WORD(1) << i), len1 - (WORD(1) << i), pow2, len2, w, p);
_fmpz_mod_poly_mul(res, pow2[i], (len2 - 1) * (WORD(1) << i) + 1,
v, (len2 - 1) * (len1 - (WORD(1) << i) - 1) + 1, p);
_fmpz_mod_poly_compose_divconquer_recursive(v, poly1, WORD(1) << i,
pow2, len2, w, p);
_fmpz_mod_poly_add(res, res, (len2 - 1) * ((WORD(1) << i) - 1) + 1,
v, (len2 - 1) * ((WORD(1) << i) - 1) + 1, p);
}
}
void _fmpz_mod_poly_compose_divconquer(fmpz *res,
const fmpz *poly1, slong len1,
const fmpz *poly2, slong len2,
const fmpz_t p)
{
if (len1 == 1 || len2 == 0)
{
fmpz_set(res, poly1);
}
else
{
const slong k = FLINT_BIT_COUNT(len1 - 1);
const slong lenV = len2 * ((WORD(1) << k) - 1) + k;
const slong lenW = (len2 - 1) * ((WORD(1) << k) - 2) - (len2 - 2) * (k-1);
slong i;
fmpz *v, *w, **pow2;
v = _fmpz_vec_init(lenV + lenW);
w = v + lenV;
pow2 = flint_malloc(k * sizeof(fmpz *));
for (i = 0; i < k; i++)
{
pow2[i] = v + (len2 * ((WORD(1) << i) - 1) + i);
}
_fmpz_vec_set(pow2[0], poly2, len2);
for (i = 1; i < k; i++)
{
_fmpz_mod_poly_sqr(pow2[i],
pow2[i-1], (len2 - 1) * (WORD(1) << (i - 1)) + 1, p);
}
_fmpz_mod_poly_compose_divconquer_recursive(res, poly1, len1,
pow2, len2, w, p);
_fmpz_vec_clear(v, lenV + lenW);
flint_free(pow2);
}
}
void fmpz_mod_poly_compose_divconquer(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly1,
const fmpz_mod_poly_t poly2)
{
const slong len1 = poly1->length;
const slong len2 = poly2->length;
if (len1 == 0)
{
fmpz_mod_poly_zero(res);
}
else if (len1 == 1 || len2 == 0)
{
fmpz_mod_poly_set_fmpz(res, poly1->coeffs);
}
else
{
const slong lenr = (len1 - 1) * (len2 - 1) + 1;
if ((res != poly1) && (res != poly2))
{
fmpz_mod_poly_fit_length(res, lenr);
_fmpz_mod_poly_compose_divconquer(res->coeffs, poly1->coeffs, len1,
poly2->coeffs, len2,
&(res->p));
}
else
{
fmpz *t = _fmpz_vec_init(lenr);
_fmpz_mod_poly_compose_divconquer(t, poly1->coeffs, len1,
poly2->coeffs, len2, &(res->p));
_fmpz_vec_clear(res->coeffs, res->alloc);
res->coeffs = t;
res->alloc = lenr;
res->length = lenr;
}
_fmpz_mod_poly_set_length(res, lenr);
_fmpz_mod_poly_normalise(res);
}
}

View File

@@ -0,0 +1,108 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_compose_horner(fmpz *res, const fmpz *poly1, slong len1,
const fmpz *poly2, slong len2,
const fmpz_t p)
{
if (len1 == 1 || len2 == 0)
{
fmpz_set(res, poly1);
}
else
{
const slong alloc = (len1 - 1) * (len2 - 1) + 1;
slong i = len1 - 1, lenr = len2;
fmpz * t = _fmpz_vec_init(alloc);
/*
Perform the first two steps as one,
"res = a(m) * poly2 + a(m-1)".
*/
{
_fmpz_mod_poly_scalar_mul_fmpz(res, poly2, len2, poly1 + i, p);
i--;
fmpz_add(res, res, poly1 + i);
if (fmpz_cmpabs(res, p) >= 0)
fmpz_sub(res, res, p);
}
while (i > 0)
{
i--;
_fmpz_mod_poly_mul(t, res, lenr, poly2, len2, p);
lenr += len2 - 1;
_fmpz_mod_poly_add(res, t, lenr, poly1 + i, 1, p);
}
_fmpz_vec_clear(t, alloc);
}
}
void fmpz_mod_poly_compose_horner(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly1,
const fmpz_mod_poly_t poly2)
{
const slong len1 = poly1->length;
const slong len2 = poly2->length;
if (len1 == 0)
{
fmpz_mod_poly_zero(res);
}
else if (len1 == 1 || len2 == 0)
{
fmpz_mod_poly_set_fmpz(res, poly1->coeffs);
}
else
{
const slong lenr = (len1 - 1) * (len2 - 1) + 1;
if ((res != poly1) && (res != poly2))
{
fmpz_mod_poly_fit_length(res, lenr);
_fmpz_mod_poly_compose_horner(res->coeffs, poly1->coeffs, len1,
poly2->coeffs, len2,
&(res->p));
}
else
{
fmpz *t = _fmpz_vec_init(lenr);
_fmpz_mod_poly_compose_horner(t, poly1->coeffs, len1,
poly2->coeffs, len2, &(res->p));
_fmpz_vec_clear(res->coeffs, res->alloc);
res->coeffs = t;
res->alloc = lenr;
res->length = lenr;
}
_fmpz_mod_poly_set_length(res, lenr);
_fmpz_mod_poly_normalise(res);
}
}

View File

@@ -0,0 +1,108 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
void
_fmpz_mod_poly_compose_mod(fmpz * res, const fmpz * f, slong lenf, const fmpz * g,
const fmpz * h, slong lenh, const fmpz_t p)
{
if (lenh < 12 || lenf >= lenh)
_fmpz_mod_poly_compose_mod_horner(res, f, lenf, g, h, lenh, p);
else
_fmpz_mod_poly_compose_mod_brent_kung(res, f, lenf, g, h, lenh, p);
}
void
fmpz_mod_poly_compose_mod(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly1,
const fmpz_mod_poly_t poly2, const fmpz_mod_poly_t poly3)
{
fmpz_t inv3;
slong len1 = poly1->length;
slong len2 = poly2->length;
slong len3 = poly3->length;
slong len = len3 - 1;
slong vec_len = FLINT_MAX(len3 - 1, len2);
fmpz * ptr2;
if (len3 == 0)
{
flint_printf("Exception (fmpz_mod_poly_compose_mod)."
"Division by zero.\n");
abort();
}
if (len1 == 0 || len3 == 1)
{
fmpz_mod_poly_zero(res);
return;
}
if (len1 == 1)
{
fmpz_mod_poly_set(res, poly1);
return;
}
if (res == poly3 || res == poly1)
{
fmpz_mod_poly_t tmp;
fmpz_mod_poly_init(tmp, &res->p);
fmpz_mod_poly_compose_mod(tmp, poly1, poly2, poly3);
fmpz_mod_poly_swap(tmp, res);
fmpz_mod_poly_clear(tmp);
return;
}
ptr2 = _fmpz_vec_init(vec_len);
if (len2 <= len)
{
_fmpz_vec_set(ptr2, poly2->coeffs, len2);
_fmpz_vec_zero(ptr2 + len2, len - len2);
}
else
{
fmpz_init(inv3);
fmpz_invmod(inv3, poly3->coeffs + len, &res->p);
_fmpz_mod_poly_rem(ptr2, poly2->coeffs, len2,
poly3->coeffs, len3, inv3, &res->p);
fmpz_clear(inv3);
}
fmpz_mod_poly_fit_length(res, len);
_fmpz_mod_poly_compose_mod(res->coeffs,
poly1->coeffs, len1, ptr2, poly3->coeffs, len3, &res->p);
_fmpz_mod_poly_set_length(res, len);
_fmpz_mod_poly_normalise(res);
_fmpz_vec_clear(ptr2, vec_len);
}

View File

@@ -0,0 +1,181 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#include "fmpz_mat.h"
#include "ulong_extras.h"
void
_fmpz_mod_poly_compose_mod_brent_kung(fmpz * res, const fmpz * poly1, slong len1,
const fmpz * poly2, const fmpz * poly3, slong len3,
const fmpz_t p)
{
fmpz_mat_t A, B, C;
fmpz * t, * h, * tmp;
slong i, j, n, m;
n = len3 - 1;
if (len3 == 1)
return;
if (len1 == 1)
{
fmpz_set(res, poly1);
return;
}
if (len3 == 2)
{
_fmpz_mod_poly_evaluate_fmpz(res, poly1, len1, poly2, p);
return;
}
m = n_sqrt(n) + 1;
fmpz_mat_init(A, m, n);
fmpz_mat_init(B, m, m);
fmpz_mat_init(C, m, n);
h = _fmpz_vec_init(2 * n - 1);
t = _fmpz_vec_init(2 * n - 1);
/* Set rows of B to the segments of poly1 */
for (i = 0; i < len1 / m; i++)
_fmpz_vec_set(B->rows[i], poly1 + i * m, m);
_fmpz_vec_set(B->rows[i], poly1 + i * m, len1 % m);
/* Set rows of A to powers of poly2 */
fmpz_one(A->rows[0]);
_fmpz_vec_set(A->rows[1], poly2, n);
tmp = _fmpz_vec_init(2 * n - 1);
for (i = 2; i < m; i++)
{
_fmpz_mod_poly_mulmod(tmp, A->rows[i - 1], n, poly2, n, poly3, len3, p);
_fmpz_vec_set(A->rows[i], tmp, n);
}
_fmpz_vec_clear(tmp, 2 * n - 1);
fmpz_mat_mul(C, B, A);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
fmpz_mod(C->rows[i] + j, C->rows[i] + j, p);
/* Evaluate block composition using the Horner scheme */
_fmpz_vec_set(res, C->rows[m - 1], n);
_fmpz_mod_poly_mulmod(h, A->rows[m - 1], n, poly2, n, poly3, len3, p);
for (i = m - 2; i >= 0; i--)
{
_fmpz_mod_poly_mulmod(t, res, n, h, n, poly3, len3, p);
_fmpz_mod_poly_add(res, t, n, C->rows[i], n, p);
}
_fmpz_vec_clear(h, 2 * n - 1);
_fmpz_vec_clear(t, 2 * n - 1);
fmpz_mat_clear(A);
fmpz_mat_clear(B);
fmpz_mat_clear(C);
}
void
fmpz_mod_poly_compose_mod_brent_kung(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly1,
const fmpz_mod_poly_t poly2, const fmpz_mod_poly_t poly3)
{
slong len1 = poly1->length;
slong len2 = poly2->length;
slong len3 = poly3->length;
slong len = len3 - 1;
slong vec_len = FLINT_MAX(len3 - 1, len2);
fmpz * ptr2;
fmpz_t inv3;
if (len3 == 0)
{
flint_printf("Exception (fmpz_mod_poly_compose_mod_brent_kung)."
"Division by zero in\n");
abort();
}
if (len1 >= len3)
{
flint_printf("Exception (fmpz_mod_poly_compose_brent_kung). the degree of the"
" first polynomial must be smaller than that of the modulus\n");
abort();
}
if (len1 == 0 || len3 == 1)
{
fmpz_mod_poly_zero(res);
return;
}
if (len1 == 1)
{
fmpz_mod_poly_set(res, poly1);
return;
}
if (res == poly3 || res == poly1)
{
fmpz_mod_poly_t tmp;
fmpz_mod_poly_init(tmp, &res->p);
fmpz_mod_poly_compose_mod_brent_kung(tmp, poly1, poly2, poly3);
fmpz_mod_poly_swap(tmp, res);
fmpz_mod_poly_clear(tmp);
return;
}
ptr2 = _fmpz_vec_init(vec_len);
if (len2 <= len)
{
_fmpz_vec_set(ptr2, poly2->coeffs, len2);
_fmpz_vec_zero(ptr2 + len2, vec_len - len2);
}
else
{
fmpz_init(inv3);
fmpz_invmod(inv3, poly3->coeffs + len, &res->p);
_fmpz_mod_poly_rem(ptr2, poly2->coeffs, len2,
poly3->coeffs, len3, inv3, &res->p);
fmpz_clear(inv3);
}
fmpz_mod_poly_fit_length(res, len);
_fmpz_mod_poly_compose_mod_brent_kung(res->coeffs,
poly1->coeffs, len1, ptr2, poly3->coeffs, len3, &res->p);
_fmpz_mod_poly_set_length(res, len);
_fmpz_mod_poly_normalise(res);
_fmpz_vec_clear(ptr2, vec_len);
}

View File

@@ -0,0 +1,255 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
Copyright (C) 2013 Martin Lee
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#include "fmpz_mat.h"
#include "ulong_extras.h"
void
_fmpz_mod_poly_reduce_matrix_mod_poly (fmpz_mat_t A, const fmpz_mat_t B,
const fmpz_mod_poly_t f)
{
fmpz * tmp1, *tmp2;
slong n = f->length - 1;
slong i, m = n_sqrt(n) + 1;
fmpz_t invf;
fmpz_init(invf);
fmpz_invmod(invf, f->coeffs + n, &f->p);
fmpz_mat_init(A, m, n);
fmpz_one(A->rows[0]);
tmp1 = _fmpz_vec_init(2 * (B->c) - n);
tmp2 = tmp1 + (B->c - n);
for (i= 1; i < m; i++)
{
_fmpz_mod_poly_divrem(tmp1, tmp2, B->rows[i], B->c, f->coeffs,
f->length, invf, &f->p);
_fmpz_vec_set(A->rows[i], tmp2, n);
}
_fmpz_vec_clear(tmp1, 2 * (B->c) - n);
fmpz_clear(invf);
}
void
_fmpz_mod_poly_precompute_matrix (fmpz_mat_t A, const fmpz * poly1,
const fmpz * poly2, slong len2, const fmpz * poly2inv,
slong len2inv, const fmpz_t p)
{
/* Set rows of A to powers of poly1 */
slong i, n, m;
n = len2 - 1;
m = n_sqrt(n) + 1;
fmpz_one(A->rows[0]);
_fmpz_vec_set(A->rows[1], poly1, n);
for (i = 2; i < m; i++)
_fmpz_mod_poly_mulmod_preinv(A->rows[i], A->rows[i - 1], n, poly1, n,
poly2, len2, poly2inv, len2inv, p);
}
void
fmpz_mod_poly_precompute_matrix(fmpz_mat_t A, const fmpz_mod_poly_t poly1,
const fmpz_mod_poly_t poly2, const fmpz_mod_poly_t poly2inv)
{
slong len1 = poly1->length;
slong len2 = poly2->length;
slong len = len2 - 1;
slong vec_len = FLINT_MAX(len2 - 1, len1);
slong m= n_sqrt(len) + 1;
fmpz* ptr;
fmpz_t inv2;
if (len2 == 0)
{
flint_printf("Exception (fmpz_mod_poly_precompute_matrix)."
"Division by zero.\n");
abort();
}
if (A->r != m || A->c != len)
{
flint_printf("Exception (fmpz_mod_poly_precompute_matrix)."
" Wrong dimensions.\n");
abort();
}
if (len2 == 1)
{
fmpz_mat_zero(A);
return;
}
ptr = _fmpz_vec_init(vec_len);
if (len1 <= len)
{
_fmpz_vec_set(ptr, poly1->coeffs, len1);
_fmpz_vec_zero(ptr + len1, vec_len - len1);
}
else
{
fmpz_init(inv2);
fmpz_invmod(inv2, poly2->coeffs + len, &poly1->p);
_fmpz_mod_poly_rem(ptr, poly1->coeffs, len1,
poly2->coeffs, len2, inv2, &poly1->p);
fmpz_clear(inv2);
}
_fmpz_mod_poly_precompute_matrix (A, ptr, poly2->coeffs, len2,
poly2inv->coeffs, poly2inv->length, &poly1->p);
_fmpz_vec_clear(ptr, vec_len);
}
void
_fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(fmpz * res,
const fmpz * poly1, slong len1, const fmpz_mat_t A, const fmpz * poly3,
slong len3, const fmpz * poly3inv, slong len3inv, const fmpz_t p)
{
fmpz_mat_t B, C;
fmpz * t, * h;
slong i, j, n, m;
n = len3 - 1;
if (len3 == 1)
return;
if (len1 == 1)
{
fmpz_set(res, poly1);
return;
}
if (len3 == 2)
{
_fmpz_mod_poly_evaluate_fmpz(res, poly1, len1, A->rows[1], p);
return;
}
m = n_sqrt(n) + 1;
fmpz_mat_init(B, m, m);
fmpz_mat_init(C, m, n);
h = _fmpz_vec_init(n);
t = _fmpz_vec_init(n);
/* Set rows of B to the segments of poly1 */
for (i = 0; i < len1 / m; i++)
_fmpz_vec_set(B->rows[i], poly1 + i * m, m);
_fmpz_vec_set(B->rows[i], poly1 + i * m, len1 % m);
fmpz_mat_mul(C, B, A);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
fmpz_mod(C->rows[i] + j, C->rows[i] + j, p);
/* Evaluate block composition using the Horner scheme */
_fmpz_vec_set(res, C->rows[m - 1], n);
_fmpz_mod_poly_mulmod_preinv(h, A->rows[m - 1], n, A->rows[1], n, poly3,
len3, poly3inv, len3inv, p);
for (i = m - 2; i >= 0; i--)
{
_fmpz_mod_poly_mulmod_preinv(t, res, n, h, n, poly3, len3,
poly3inv, len3inv, p);
_fmpz_mod_poly_add(res, t, n, C->rows[i], n, p);
}
_fmpz_vec_clear(h, n);
_fmpz_vec_clear(t, n);
fmpz_mat_clear(B);
fmpz_mat_clear(C);
}
void
fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly1, const fmpz_mat_t A,
const fmpz_mod_poly_t poly3, const fmpz_mod_poly_t poly3inv)
{
slong len1 = poly1->length;
slong len3 = poly3->length;
slong len = len3 - 1;
if (len3 == 0)
{
flint_printf("Exception (fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv)."
"Division by zero\n");
abort();
}
if (len1 >= len3)
{
flint_printf("Exception (fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv)."
"The degree of the first polynomial must be smaller than that of the "
" modulus\n");
abort();
}
if (len1 == 0 || len3 == 1)
{
fmpz_mod_poly_zero(res);
return;
}
if (len1 == 1)
{
fmpz_mod_poly_set(res, poly1);
return;
}
if (res == poly3 || res == poly1 || res == poly3inv)
{
fmpz_mod_poly_t tmp;
fmpz_mod_poly_init(tmp, &res->p);
fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(tmp, poly1, A,
poly3, poly3inv);
fmpz_mod_poly_swap(tmp, res);
fmpz_mod_poly_clear(tmp);
return;
}
fmpz_mod_poly_fit_length(res, len);
_fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(res->coeffs,
poly1->coeffs, len1, A, poly3->coeffs, len3,
poly3inv->coeffs, poly3inv->length, &res->p);
_fmpz_mod_poly_set_length(res, len);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,183 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
Copyright (C) 2013 Martin Lee
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#include "fmpz_mat.h"
#include "ulong_extras.h"
void
_fmpz_mod_poly_compose_mod_brent_kung_preinv(fmpz * res, const fmpz * poly1,
slong len1, const fmpz * poly2, const fmpz * poly3, slong len3,
const fmpz * poly3inv, slong len3inv, const fmpz_t p)
{
fmpz_mat_t A, B, C;
fmpz * t, * h;
slong i, j, n, m;
n = len3 - 1;
if (len3 == 1)
return;
if (len1 == 1)
{
fmpz_set(res, poly1);
return;
}
if (len3 == 2)
{
_fmpz_mod_poly_evaluate_fmpz(res, poly1, len1, poly2, p);
return;
}
m = n_sqrt(n) + 1;
fmpz_mat_init(A, m, n);
fmpz_mat_init(B, m, m);
fmpz_mat_init(C, m, n);
h = _fmpz_vec_init(2 * n - 1);
t = _fmpz_vec_init(2 * n - 1);
/* Set rows of B to the segments of poly1 */
for (i = 0; i < len1 / m; i++)
_fmpz_vec_set(B->rows[i], poly1 + i * m, m);
_fmpz_vec_set(B->rows[i], poly1 + i * m, len1 % m);
/* Set rows of A to powers of poly2 */
fmpz_one(A->rows[0]);
_fmpz_vec_set(A->rows[1], poly2, n);
for (i = 2; i < m; i++)
_fmpz_mod_poly_mulmod_preinv (A->rows[i], A->rows[i - 1], n, poly2, n,
poly3, len3, poly3inv, len3inv, p);
fmpz_mat_mul(C, B, A);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
fmpz_mod(C->rows[i] + j, C->rows[i] + j, p);
/* Evaluate block composition using the Horner scheme */
_fmpz_vec_set(res, C->rows[m - 1], n);
_fmpz_mod_poly_mulmod_preinv(h, A->rows[m - 1], n, poly2, n, poly3, len3,
poly3inv, len3inv, p);
for (i = m - 2; i >= 0; i--)
{
_fmpz_mod_poly_mulmod_preinv(t, res, n, h, n, poly3, len3,
poly3inv, len3inv, p);
_fmpz_mod_poly_add(res, t, n, C->rows[i], n, p);
}
_fmpz_vec_clear(h, 2 * n - 1);
_fmpz_vec_clear(t, 2 * n - 1);
fmpz_mat_clear(A);
fmpz_mat_clear(B);
fmpz_mat_clear(C);
}
void
fmpz_mod_poly_compose_mod_brent_kung_preinv(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly1, const fmpz_mod_poly_t poly2,
const fmpz_mod_poly_t poly3, const fmpz_mod_poly_t poly3inv)
{
slong len1 = poly1->length;
slong len2 = poly2->length;
slong len3 = poly3->length;
slong len = len3 - 1;
fmpz * ptr2;
fmpz_t inv3;
if (len3 == 0)
{
flint_printf("Exception (fmpz_mod_poly_compose_mod_brent_kung preinv)."
"Division by zero\n");
abort();
}
if (len1 >= len3)
{
flint_printf("Exception (fmpz_mod_poly_compose_mod_brent_kung_preinv)."
"The degree of the first polynomial must be smaller than that of the "
" modulus\n");
abort();
}
if (len1 == 0 || len3 == 1)
{
fmpz_mod_poly_zero(res);
return;
}
if (len1 == 1)
{
fmpz_mod_poly_set(res, poly1);
return;
}
if (res == poly3 || res == poly1 || res == poly3inv)
{
fmpz_mod_poly_t tmp;
fmpz_mod_poly_init(tmp, &res->p);
fmpz_mod_poly_compose_mod_brent_kung_preinv(tmp, poly1, poly2,
poly3, poly3inv);
fmpz_mod_poly_swap(tmp, res);
fmpz_mod_poly_clear(tmp);
return;
}
ptr2 = _fmpz_vec_init(len);
if (len2 <= len)
{
_fmpz_vec_set(ptr2, poly2->coeffs, len2);
_fmpz_vec_zero(ptr2 + len2, len - len2);
}
else
{
fmpz_init(inv3);
fmpz_invmod(inv3, poly3->coeffs + len, &res->p);
_fmpz_mod_poly_rem(ptr2, poly2->coeffs, len2,
poly3->coeffs, len3, inv3, &res->p);
fmpz_clear(inv3);
}
fmpz_mod_poly_fit_length(res, len);
_fmpz_mod_poly_compose_mod_brent_kung_preinv(res->coeffs,
poly1->coeffs, len1, ptr2, poly3->coeffs, len3,
poly3inv->coeffs, poly3inv->length, &res->p);
_fmpz_mod_poly_set_length(res, len);
_fmpz_mod_poly_normalise(res);
_fmpz_vec_clear(ptr2, len);
}

View File

@@ -0,0 +1,141 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
void
_fmpz_mod_poly_compose_mod_horner(fmpz * res, const fmpz * f, slong lenf, const fmpz * g,
const fmpz * h, slong lenh, const fmpz_t p)
{
slong i, len;
fmpz * t;
if (lenh == 1)
return;
if (lenf == 1)
{
fmpz_set(res, f);
return;
}
if (lenh == 2)
{
_fmpz_mod_poly_evaluate_fmpz(res, f, lenf, g, p);
return;
}
len = lenh - 1;
i = lenf - 1;
t = _fmpz_vec_init(2 * lenh - 3);
_fmpz_mod_poly_scalar_mul_fmpz(res, g, len, f + i, p);
i--;
if (i >= 0)
{
fmpz_add(res, res, f + i);
fmpz_mod(res, res, p);
}
while (i > 0)
{
i--;
_fmpz_mod_poly_mulmod(t, res, len, g, len, h, lenh, p);
_fmpz_mod_poly_add(res, t, len, f + i, 1, p);
}
_fmpz_vec_clear(t, 2 * lenh - 3);
}
void
fmpz_mod_poly_compose_mod_horner(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly1,
const fmpz_mod_poly_t poly2, const fmpz_mod_poly_t poly3)
{
fmpz_t inv3;
slong len1 = poly1->length;
slong len2 = poly2->length;
slong len3 = poly3->length;
slong len = len3 - 1;
slong vec_len = FLINT_MAX(len3 - 1, len2);
fmpz * ptr2;
if (len3 == 0)
{
flint_printf("Exception (fmpz_mod_poly_compose_mod_horner). Division by zero \n");
abort();
}
if (len1 == 0 || len3 == 1)
{
fmpz_mod_poly_zero(res);
return;
}
if (len1 == 1)
{
fmpz_mod_poly_set(res, poly1);
return;
}
if (res == poly3 || res == poly1)
{
fmpz_mod_poly_t tmp;
fmpz_mod_poly_init(tmp, &res->p);
fmpz_mod_poly_compose_mod_horner(tmp, poly1, poly2, poly3);
fmpz_mod_poly_swap(tmp, res);
fmpz_mod_poly_clear(tmp);
return;
}
ptr2 = _fmpz_vec_init(vec_len);
if (len2 <= len3 - 1)
{
_fmpz_vec_set(ptr2, poly2->coeffs, len2);
_fmpz_vec_zero(ptr2 + len2, vec_len - len2);
}
else
{
fmpz_init(inv3);
fmpz_invmod(inv3, poly3->coeffs + len, &res->p);
_fmpz_mod_poly_rem(ptr2, poly2->coeffs, len2,
poly3->coeffs, len3, inv3, &res->p);
fmpz_clear(inv3);
}
fmpz_mod_poly_fit_length(res, len);
_fmpz_mod_poly_compose_mod_horner(res->coeffs,
poly1->coeffs, len1, ptr2, poly3->coeffs, len3, &res->p);
_fmpz_mod_poly_set_length(res, len);
_fmpz_mod_poly_normalise(res);
_fmpz_vec_clear(ptr2, vec_len);
}

View File

@@ -0,0 +1,70 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_derivative(fmpz *res, const fmpz *poly, slong len,
const fmpz_t p)
{
slong j, k = 1;
for (j = 1; j < len; j++)
{
if (k == 0)
fmpz_zero(res + (j - 1));
else if (k == 1)
fmpz_set(res + (j - 1), poly + j);
else
{
fmpz_mul_ui(res + (j - 1), poly + j, k);
fmpz_mod(res + (j - 1), res + (j - 1), p);
}
if (fmpz_equal_ui(p, ++k))
k = 0;
}
}
void fmpz_mod_poly_derivative(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly)
{
const slong len = poly->length;
if (len < 2)
{
fmpz_mod_poly_zero(res);
}
else
{
fmpz_mod_poly_fit_length(res, len - 1);
_fmpz_mod_poly_derivative(res->coeffs, poly->coeffs, len, &(res->p));
_fmpz_mod_poly_set_length(res, len - 1);
_fmpz_mod_poly_normalise(res);
}
}

View File

@@ -0,0 +1,114 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <stdlib.h>
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_div_basecase(fmpz *Q, fmpz *R,
const fmpz *A, slong lenA, const fmpz *B, slong lenB,
const fmpz_t invB, const fmpz_t p)
{
const slong alloc = (R == NULL) ? lenA : 0;
slong lenR = lenB - 1, iQ;
if (alloc)
R = _fmpz_vec_init(alloc);
if (R != A)
_fmpz_vec_set(R + lenR, A + lenR, lenA - lenR);
for (iQ = lenA - lenB; iQ >= 0; iQ--)
{
if (fmpz_is_zero(R + lenA - 1))
{
fmpz_zero(Q + iQ);
}
else
{
fmpz_mul(Q + iQ, R + lenA - 1, invB);
fmpz_mod(Q + iQ, Q + iQ, p);
_fmpz_vec_scalar_submul_fmpz(R + lenA - lenR - 1, B, lenR, Q + iQ);
_fmpz_vec_scalar_mod_fmpz(R + lenA - lenR - 1, R + lenA - lenR - 1, lenR, p);
}
if (lenR - 1 >= iQ)
{
B++;
lenR--;
}
lenA--;
}
if (alloc)
_fmpz_vec_clear(R, alloc);
}
void fmpz_mod_poly_div_basecase(fmpz_mod_poly_t Q,
const fmpz_mod_poly_t A, const fmpz_mod_poly_t B)
{
const slong lenA = A->length, lenB = B->length, lenQ = lenA - lenB + 1;
fmpz *q;
fmpz_t invB;
if (lenA < lenB)
{
fmpz_mod_poly_zero(Q);
return;
}
fmpz_init(invB);
fmpz_invmod(invB, B->coeffs + (lenB - 1), &(B->p));
if (Q == A || Q == B)
{
q = _fmpz_vec_init(lenQ);
}
else
{
fmpz_mod_poly_fit_length(Q, lenQ);
q = Q->coeffs;
}
_fmpz_mod_poly_div_basecase(q, NULL, A->coeffs, lenA,
B->coeffs, lenB, invB, &(B->p));
if (Q == A || Q == B)
{
_fmpz_vec_clear(Q->coeffs, Q->alloc);
Q->coeffs = q;
Q->alloc = lenQ;
Q->length = lenQ;
}
else
{
_fmpz_mod_poly_set_length(Q, lenQ);
}
fmpz_clear(invB);
}

View File

@@ -0,0 +1,111 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_div_newton_n_preinv (fmpz* Q, const fmpz* A, slong lenA,
const fmpz* B, slong lenB, const fmpz* Binv,
slong lenBinv, const fmpz_t mod)
{
const slong lenQ = lenA - lenB + 1;
fmpz * Arev;
Arev = _fmpz_vec_init(lenQ);
_fmpz_poly_reverse(Arev, A + (lenA - lenQ), lenQ, lenQ);
_fmpz_mod_poly_mullow(Q, Arev, lenQ, Binv, FLINT_MIN(lenQ, lenBinv), mod,
lenQ);
_fmpz_poly_reverse(Q, Q, lenQ, lenQ);
_fmpz_vec_clear(Arev, lenQ);
}
void fmpz_mod_poly_div_newton_n_preinv(fmpz_mod_poly_t Q,
const fmpz_mod_poly_t A, const fmpz_mod_poly_t B,
const fmpz_mod_poly_t Binv)
{
const slong lenA = A->length, lenB = B->length, lenQ = lenA - lenB + 1,
lenBinv = Binv->length;
fmpz *q;
if (lenB == 0)
{
flint_printf("Exception (fmpz_mod_poly_div_newton_n_preinv). Division by zero.\n");
abort();
}
if (lenA < lenB)
{
fmpz_mod_poly_zero(Q);
return;
}
if (lenA > 2 * lenB - 2)
{
flint_printf ("Exception (fmpz_mod_poly_div_newton_n_preinv).\n");
}
if (Q == A || Q == B || Q == Binv)
{
q = _fmpz_vec_init(lenQ);
}
else
{
fmpz_mod_poly_fit_length(Q, lenQ);
q = Q->coeffs;
}
_fmpz_mod_poly_div_newton_n_preinv (q, A->coeffs, lenA, B->coeffs, lenB,
Binv->coeffs, lenBinv, &(B->p));
if (Q == A || Q == B || Q == Binv)
{
_fmpz_vec_clear(Q->coeffs, Q->alloc);
Q->coeffs = q;
Q->alloc = lenQ;
Q->length = lenQ;
}
else
{
_fmpz_mod_poly_set_length(Q, lenQ);
}
}

View File

@@ -0,0 +1,121 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <stdlib.h>
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_divrem_basecase(fmpz *Q, fmpz *R,
const fmpz *A, slong lenA, const fmpz *B, slong lenB,
const fmpz_t invB, const fmpz_t p)
{
slong iQ, iR;
if (R != A)
_fmpz_vec_set(R, A, lenA);
for (iQ = lenA - lenB, iR = lenA - 1; iQ >= 0; iQ--, iR--)
{
if (fmpz_is_zero(R + iR))
fmpz_zero(Q + iQ);
else
{
fmpz_mul(Q + iQ, R + iR, invB);
fmpz_mod(Q + iQ, Q + iQ, p);
_fmpz_vec_scalar_submul_fmpz(R + iQ, B, lenB, Q + iQ);
_fmpz_vec_scalar_mod_fmpz(R + iQ, R + iQ, lenB, p);
}
}
}
void fmpz_mod_poly_divrem_basecase(fmpz_mod_poly_t Q, fmpz_mod_poly_t R,
const fmpz_mod_poly_t A, const fmpz_mod_poly_t B)
{
const slong lenA = A->length, lenB = B->length, lenQ = lenA - lenB + 1;
fmpz *q, *r;
fmpz_t invB;
if (lenA < lenB)
{
fmpz_mod_poly_set(R, A);
fmpz_mod_poly_zero(Q);
return;
}
fmpz_init(invB);
fmpz_invmod(invB, B->coeffs + (lenB - 1), &(B->p));
if (Q == A || Q == B)
{
q = _fmpz_vec_init(lenQ);
}
else
{
fmpz_mod_poly_fit_length(Q, lenQ);
q = Q->coeffs;
}
if (R == B)
{
r = _fmpz_vec_init(lenA);
}
else
{
fmpz_mod_poly_fit_length(R, lenA);
r = R->coeffs;
}
_fmpz_mod_poly_divrem_basecase(q, r, A->coeffs, lenA,
B->coeffs, lenB, invB, &(B->p));
if (Q == A || Q == B)
{
_fmpz_vec_clear(Q->coeffs, Q->alloc);
Q->coeffs = q;
Q->alloc = lenQ;
Q->length = lenQ;
}
else
{
_fmpz_mod_poly_set_length(Q, lenQ);
}
if (R == B)
{
_fmpz_vec_clear(R->coeffs, R->alloc);
R->coeffs = r;
R->alloc = lenB - 1;
R->length = lenB - 1;
}
else
{
_fmpz_mod_poly_set_length(R, lenB - 1);
}
_fmpz_mod_poly_normalise(R);
fmpz_clear(invB);
}

View File

@@ -0,0 +1,196 @@
/*=============================================================================
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) 2008, 2009 William Hart
Copyright (C) 2010, 2011 Sebastian Pancratz
******************************************************************************/
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
static void
__fmpz_mod_poly_divrem_divconquer(fmpz * Q, fmpz * R,
const fmpz * A, slong lenA, const fmpz * B, slong lenB,
const fmpz_t invB, const fmpz_t p)
{
if (lenA < 2 * lenB - 1)
{
/*
Convert unbalanced division into a 2 n1 - 1 by n1 division
*/
const slong n1 = lenA - lenB + 1;
const slong n2 = lenB - n1;
const fmpz * p1 = A + n2;
const fmpz * d1 = B + n2;
const fmpz * d2 = B;
fmpz * W = _fmpz_vec_init((2 * n1 - 1) + lenB - 1);
fmpz * d1q1 = R + n2;
fmpz * d2q1 = W + (2 * n1 - 1);
_fmpz_mod_poly_divrem_divconquer_recursive(Q, d1q1, W, p1, d1, n1,
invB, p);
/*
Compute d2q1 = Q d2, of length lenB - 1
*/
if (n1 >= n2)
_fmpz_mod_poly_mul(d2q1, Q, n1, d2, n2, p);
else
_fmpz_mod_poly_mul(d2q1, d2, n2, Q, n1, p);
/*
Compute BQ = d1q1 * x^n1 + d2q1, of length lenB - 1;
then compute R = A - BQ
*/
_fmpz_vec_swap(R, d2q1, n2);
_fmpz_mod_poly_add(R + n2, R + n2, n1 - 1, d2q1 + n2, n1 - 1, p);
_fmpz_mod_poly_sub(R, A, lenA, R, lenA, p);
_fmpz_vec_clear(W, (2 * n1 - 1) + lenB - 1);
}
else /* lenA = 2 * lenB - 1 */
{
fmpz * W = _fmpz_vec_init(lenA);
_fmpz_mod_poly_divrem_divconquer_recursive(Q, R, W,
A, B, lenB, invB, p);
_fmpz_mod_poly_sub(R, A, lenB - 1, R, lenB - 1, p);
_fmpz_vec_clear(W, lenA);
}
}
void _fmpz_mod_poly_divrem_divconquer(fmpz *Q, fmpz *R,
const fmpz *A, slong lenA, const fmpz *B, slong lenB,
const fmpz_t invB, const fmpz_t p)
{
if (lenA <= 2 * lenB - 1)
{
__fmpz_mod_poly_divrem_divconquer(Q, R, A, lenA, B, lenB, invB, p);
}
else /* lenA > 2 * lenB - 1 */
{
slong shift, n = 2 * lenB - 1;
fmpz *QB, *W;
_fmpz_vec_set(R, A, lenA);
W = _fmpz_vec_init(2 * n);
QB = W + n;
while (lenA >= n)
{
shift = lenA - n;
_fmpz_mod_poly_divrem_divconquer_recursive(Q + shift, QB,
W, R + shift, B, lenB, invB, p);
_fmpz_mod_poly_sub(R + shift, R + shift, n, QB, n, p);
lenA -= lenB;
}
if (lenA >= lenB)
{
__fmpz_mod_poly_divrem_divconquer(Q, W, R, lenA, B, lenB, invB, p);
_fmpz_vec_swap(W, R, lenA);
}
_fmpz_vec_clear(W, 2 * n);
}
}
void
fmpz_mod_poly_divrem_divconquer(fmpz_mod_poly_t Q, fmpz_mod_poly_t R,
const fmpz_mod_poly_t A, const fmpz_mod_poly_t B)
{
const slong lenA = A->length;
const slong lenB = B->length;
const slong lenQ = lenA - lenB + 1;
fmpz *q, *r;
fmpz_t invB;
if (lenA < lenB)
{
fmpz_mod_poly_set(R, A);
fmpz_mod_poly_zero(Q);
return;
}
fmpz_init(invB);
fmpz_invmod(invB, fmpz_mod_poly_lead(B), &(B->p));
if (Q == A || Q == B)
{
q = _fmpz_vec_init(lenQ);
}
else
{
fmpz_mod_poly_fit_length(Q, lenQ);
q = Q->coeffs;
}
if (R == A || R == B)
{
r = _fmpz_vec_init(lenA);
}
else
{
fmpz_mod_poly_fit_length(R, lenA);
r = R->coeffs;
}
_fmpz_mod_poly_divrem_divconquer(q, r, A->coeffs, lenA,
B->coeffs, lenB, invB, &(B->p));
if (Q == A || Q == B)
{
_fmpz_vec_clear(Q->coeffs, Q->alloc);
Q->coeffs = q;
Q->alloc = lenQ;
Q->length = lenQ;
}
else
{
_fmpz_mod_poly_set_length(Q, lenQ);
}
if (R == A || R == B)
{
_fmpz_vec_clear(R->coeffs, R->alloc);
R->coeffs = r;
R->alloc = lenA;
R->length = lenA;
}
_fmpz_mod_poly_set_length(R, lenB - 1);
_fmpz_mod_poly_normalise(R);
fmpz_clear(invB);
}

View File

@@ -0,0 +1,140 @@
/*=============================================================================
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) 2008, 2009 William Hart
Copyright (C) 2010, 2011 Sebastian Pancratz
******************************************************************************/
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#define FMPZ_MOD_POLY_DIVREM_DIVCONQUER_CUTOFF 16
void
_fmpz_mod_poly_divrem_divconquer_recursive(fmpz * Q, fmpz * BQ, fmpz * W,
const fmpz * A, const fmpz * B, slong lenB,
const fmpz_t invB, const fmpz_t p)
{
if (lenB <= FMPZ_MOD_POLY_DIVREM_DIVCONQUER_CUTOFF)
{
_fmpz_vec_zero(BQ, lenB - 1);
_fmpz_vec_set(BQ + (lenB - 1), A + (lenB - 1), lenB);
_fmpz_mod_poly_divrem_basecase(Q, BQ, BQ, 2 * lenB - 1, B, lenB, invB, p);
_fmpz_mod_poly_neg(BQ, BQ, lenB - 1, p);
_fmpz_vec_set(BQ + (lenB - 1), A + (lenB - 1), lenB);
}
else
{
const slong n2 = lenB / 2;
const slong n1 = lenB - n2;
fmpz * W1 = W;
fmpz * W2 = W + lenB;
const fmpz * p1 = A + 2 * n2;
const fmpz * p2;
const fmpz * d1 = B + n2;
const fmpz * d2 = B;
const fmpz * d3 = B + n1;
const fmpz * d4 = B;
fmpz * q1 = Q + n2;
fmpz * q2 = Q;
fmpz * dq1 = BQ + n2;
fmpz * d1q1 = BQ + 2 * n2;
fmpz *d2q1, *d3q2, *d4q2, *t;
/*
Set q1 to p1 div d1, a 2 n1 - 1 by n1 division so q1 ends up
being of length n1; d1q1 = d1 q1 is of length 2 n1 - 1
*/
_fmpz_mod_poly_divrem_divconquer_recursive(q1, d1q1, W1,
p1, d1, n1, invB, p);
/*
Compute d2q1 = d2 q1, of length lenB - 1
*/
d2q1 = W1;
_fmpz_mod_poly_mul(d2q1, q1, n1, d2, n2, p);
/*
Compute dq1 = d1 q1 x^n2 + d2 q1, of length 2 n1 + n2 - 1
*/
_fmpz_vec_swap(dq1, d2q1, n2);
_fmpz_mod_poly_add(dq1 + n2, dq1 + n2, n1 - 1, d2q1 + n2, n1 - 1, p);
/*
Compute t = A/x^n2 - dq1, which has length 2 n1 + n2 - 1, but we
are not interested in the top n1 coeffs as they will be zero, so
this has effective length n1 + n2 - 1
For the following division, we want to set {p2, 2 n2 - 1} to the
top 2 n2 - 1 coeffs of this
Since the bottom n2 - 1 coeffs of p2 are irrelevant for the
division, we in fact set {t, n2} to the relevant coeffs
*/
t = BQ;
_fmpz_mod_poly_sub(t, A + n2 + (n1 - 1), n2, dq1 + (n1 - 1), n2, p);
p2 = t - (n2 - 1);
/*
Compute q2 = t div d3, a 2 n2 - 1 by n2 division, so q2 will have
length n2; let d3q2 = d3 q2, of length 2 n2 - 1
*/
d3q2 = W1;
_fmpz_mod_poly_divrem_divconquer_recursive(q2, d3q2, W2,
p2, d3, n2, invB, p);
/*
Compute d4q2 = d4 q2, of length n1 + n2 - 1 = lenB - 1
*/
d4q2 = W2;
_fmpz_mod_poly_mul(d4q2, d4, n1, q2, n2, p);
/*
Compute dq2 = d3q2 x^n1 + d4q2, of length n1 + 2 n2 - 1
*/
_fmpz_vec_swap(BQ, d4q2, n2);
_fmpz_mod_poly_add(BQ + n2, BQ + n2, n1 - 1, d4q2 + n2, n1 - 1, p);
_fmpz_mod_poly_add(BQ + n1, BQ + n1, 2 * n2 - 1, d3q2, 2 * n2 - 1, p);
/*
Note Q = q1 x^n2 + q2, and BQ = dq1 x^n2 + dq2
*/
}
}

View File

@@ -0,0 +1,120 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <stdlib.h>
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_divrem_f(fmpz_t f, fmpz *Q, fmpz *R,
const fmpz *A, slong lenA,
const fmpz *B, slong lenB, const fmpz_t p)
{
fmpz_t invB;
fmpz_init(invB);
fmpz_gcdinv(f, invB, B + lenB - 1, p);
if (fmpz_is_one(f))
{
_fmpz_mod_poly_divrem(Q, R, A, lenA, B, lenB, invB, p);
}
fmpz_clear(invB);
}
void fmpz_mod_poly_divrem_f(fmpz_t f, fmpz_mod_poly_t Q, fmpz_mod_poly_t R,
const fmpz_mod_poly_t A, const fmpz_mod_poly_t B)
{
const slong lenA = A->length;
const slong lenB = B->length;
const slong lenQ = lenA - lenB + 1;
fmpz *q, *r;
fmpz_t invB;
fmpz_init(invB);
fmpz_gcdinv(f, invB, fmpz_poly_lead(B), &(B->p));
if (!fmpz_is_one(f))
{
fmpz_clear(invB);
return;
}
if (lenA < lenB)
{
fmpz_mod_poly_set(R, A);
fmpz_mod_poly_zero(Q);
fmpz_clear(invB);
return;
}
if (Q == A || Q == B)
{
q = _fmpz_vec_init(lenQ);
}
else
{
fmpz_mod_poly_fit_length(Q, lenQ);
q = Q->coeffs;
}
if (R == A || R == B)
{
r = _fmpz_vec_init(lenA);
}
else
{
fmpz_mod_poly_fit_length(R, lenA);
r = R->coeffs;
}
_fmpz_mod_poly_divrem_divconquer(q, r, A->coeffs, lenA,
B->coeffs, lenB, invB, &(B->p));
if (Q == A || Q == B)
{
_fmpz_vec_clear(Q->coeffs, Q->alloc);
Q->coeffs = q;
Q->alloc = lenQ;
Q->length = lenQ;
}
else
{
_fmpz_mod_poly_set_length(Q, lenQ);
}
if (R == A || R == B)
{
_fmpz_vec_clear(R->coeffs, R->alloc);
R->coeffs = r;
R->alloc = lenA;
R->length = lenA;
}
_fmpz_mod_poly_set_length(R, lenB - 1);
_fmpz_mod_poly_normalise(R);
fmpz_clear(invB);
}

View File

@@ -0,0 +1,134 @@
/*=============================================================================
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) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_divrem_newton_n_preinv (fmpz* Q, fmpz* R, const fmpz* A,
slong lenA, const fmpz* B, slong lenB,
const fmpz* Binv, slong lenBinv, const fmpz_t mod)
{
const slong lenQ = lenA - lenB + 1;
_fmpz_mod_poly_div_newton_n_preinv(Q, A, lenA, B, lenB, Binv, lenBinv, mod);
if (lenB > 1)
{
if (lenQ >= lenB - 1)
_fmpz_mod_poly_mullow(R, Q, lenQ, B, lenB - 1, mod, lenB - 1);
else
_fmpz_mod_poly_mullow(R, B, lenB - 1, Q, lenQ, mod, lenB - 1);
_fmpz_vec_sub(R, A, R, lenB - 1);
_fmpz_vec_scalar_mod_fmpz(R, R, lenB - 1, mod);
}
}
void fmpz_mod_poly_divrem_newton_n_preinv(fmpz_mod_poly_t Q, fmpz_mod_poly_t R,
const fmpz_mod_poly_t A, const fmpz_mod_poly_t B,
const fmpz_mod_poly_t Binv)
{
const slong lenA = A->length, lenB = B->length, lenBinv= Binv->length,
lenQ = lenA - lenB + 1;
fmpz* q, * r;
if (lenB == 0)
{
flint_printf("Exception (fmpz_mod_poly_divrem_newton_n_preinv)."
" Division by zero.\n");
abort();
}
if (lenA < lenB)
{
fmpz_mod_poly_set(R, A);
fmpz_mod_poly_zero(Q);
return;
}
if (lenA > 2 * lenB - 2)
{
flint_printf ("Exception (fmpz_mod_poly_divrem_newton_n_preinv).\n");
}
if (Q == A || Q == B || Q == Binv)
{
q = _fmpz_vec_init(lenQ);
}
else
{
fmpz_mod_poly_fit_length(Q, lenQ);
q = Q->coeffs;
}
if (R == A || R == B || R == Binv)
{
r = _fmpz_vec_init(lenB - 1);
}
else
{
fmpz_mod_poly_fit_length(R, lenB - 1);
r = R->coeffs;
}
_fmpz_mod_poly_divrem_newton_n_preinv (q, r, A->coeffs, lenA,
B->coeffs, lenB, Binv->coeffs,
lenBinv, &(B->p));
if (Q == A || Q == B || Q == Binv)
{
_fmpz_vec_clear(Q->coeffs, Q->alloc);
Q->coeffs = q;
Q->alloc = lenQ;
Q->length = lenQ;
}
else
{
_fmpz_mod_poly_set_length(Q, lenQ);
}
if (R == A || R == B || R == Binv)
{
_fmpz_vec_clear(R->coeffs, R->alloc);
R->coeffs = r;
R->alloc = lenB - 1;
R->length = lenB - 1;
}
else
{
_fmpz_mod_poly_set_length(R, lenB - 1);
}
_fmpz_mod_poly_normalise(R);
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,80 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_evaluate_fmpz(fmpz_t res, const fmpz *poly, slong len,
const fmpz_t a, const fmpz_t p)
{
if (len == 0)
{
fmpz_zero(res);
}
else if (len == 1 || fmpz_is_zero(a))
{
fmpz_set(res, poly);
}
else
{
slong i = len - 1;
fmpz_t t;
fmpz_init(t);
fmpz_set(res, poly + i);
for (i = len - 2; i >= 0; i--)
{
fmpz_mul(t, res, a);
fmpz_mod(t, t, p);
fmpz_add(res, poly + i, t);
}
fmpz_clear(t);
if (fmpz_cmpabs(res, p) >= 0)
fmpz_sub(res, res, p);
}
}
void fmpz_mod_poly_evaluate_fmpz(fmpz_t res,
const fmpz_mod_poly_t poly, const fmpz_t a)
{
if (res == a)
{
fmpz_t t;
fmpz_init(t);
_fmpz_mod_poly_evaluate_fmpz(t, poly->coeffs, poly->length,
a, &(poly->p));
fmpz_swap(res, t);
fmpz_clear(t);
}
else
{
_fmpz_mod_poly_evaluate_fmpz(res, poly->coeffs, poly->length,
a, &(poly->p));
}
}

View File

@@ -0,0 +1,50 @@
/*=============================================================================
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
Copyright (C) 2012 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "ulong_extras.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void
_fmpz_mod_poly_evaluate_fmpz_vec(fmpz * ys, const fmpz * coeffs,
slong len, const fmpz * xs, slong n, const fmpz_t mod)
{
if (len < 32)
_fmpz_mod_poly_evaluate_fmpz_vec_iter(ys, coeffs, len, xs, n, mod);
else
_fmpz_mod_poly_evaluate_fmpz_vec_fast(ys, coeffs, len, xs, n, mod);
}
void
fmpz_mod_poly_evaluate_fmpz_vec(fmpz * ys,
const fmpz_mod_poly_t poly, const fmpz * xs, slong n)
{
_fmpz_mod_poly_evaluate_fmpz_vec(ys, poly->coeffs,
poly->length, xs, n, &(poly->p));
}

View File

@@ -0,0 +1,151 @@
/*=============================================================================
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) 2012 Fredrik Johansson
Copyright (C) 2012 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "ulong_extras.h"
#include "fmpz.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void
_fmpz_mod_poly_evaluate_fmpz_vec_fast_precomp(fmpz * vs, const fmpz * poly,
slong plen, fmpz_poly_struct * const * tree, slong len, const fmpz_t mod)
{
slong height, i, j, pow, left;
slong tree_height;
fmpz_t temp, inv;
fmpz * t, * u, * pb, * pc, * swap;
fmpz_poly_struct * pa;
fmpz_init(temp);
fmpz_init(inv);
/* avoid worrying about some degenerate cases */
if (len < 2 || plen < 2)
{
if (len == 1)
{
fmpz_negmod(temp, tree[0]->coeffs, mod);
_fmpz_mod_poly_evaluate_fmpz(vs, poly, plen, temp, mod);
} else if (len != 0 && plen == 0)
_fmpz_vec_zero(vs, len);
else if (len != 0 && plen == 1)
for (i = 0; i < len; i++)
fmpz_set(vs + i, poly);
fmpz_clear(temp);
return;
}
t = _fmpz_vec_init(2*len);
u = _fmpz_vec_init(2*len);
left = len;
/* Initial reduction. We allow the polynomial to be larger
or smaller than the number of points. */
height = FLINT_BIT_COUNT(plen - 1) - 1;
tree_height = FLINT_CLOG2(len);
while (height >= tree_height)
height--;
pow = WORD(1) << height;
for (i = j = 0; i < len; i += pow, j++)
{
pa = tree[height] + j;
fmpz_invmod(inv, pa->coeffs + pa->length - 1, mod);
_fmpz_mod_poly_rem(t + i, poly, plen, pa->coeffs, pa->length, inv, mod);
}
for (i = height - 1; i >= 0; i--)
{
pow = WORD(1) << i;
left = len;
pa = tree[i];
pb = t;
pc = u;
left = len;
while (left >= 2 * pow)
{
fmpz_invmod(inv, pa->coeffs + pa->length - 1, mod);
_fmpz_mod_poly_rem(pc, pb, 2 * pow, pa->coeffs, pa->length, inv, mod);
pa++;
fmpz_invmod(inv, pa->coeffs + pa->length - 1, mod);
_fmpz_mod_poly_rem(pc + pow, pb, 2 * pow, pa->coeffs, pa->length, inv, mod);
pa++;
pb += 2 * pow;
pc += 2 * pow;
left -= 2 * pow;
}
if (left > pow)
{
fmpz_invmod(inv, pa->coeffs + pa->length - 1, mod);
_fmpz_mod_poly_rem(pc, pb, left, pa->coeffs, pa->length, inv, mod);
pa ++;
fmpz_invmod(inv, pa->coeffs + pa->length - 1, mod);
_fmpz_mod_poly_rem(pc + pow, pb, left, pa->coeffs, pa->length, inv, mod);
}
else if (left > 0)
_fmpz_vec_set(pc, pb, left);
swap = t;
t = u;
u = swap;
}
fmpz_clear(temp);
fmpz_clear(inv);
_fmpz_vec_set(vs, t, len);
_fmpz_vec_clear(t, 2*len);
_fmpz_vec_clear(u, 2*len);
}
void _fmpz_mod_poly_evaluate_fmpz_vec_fast(fmpz * ys, const fmpz * poly, slong plen,
const fmpz * xs, slong n, const fmpz_t mod)
{
fmpz_poly_struct ** tree;
tree = _fmpz_mod_poly_tree_alloc(n);
_fmpz_mod_poly_tree_build(tree, xs, n, mod);
_fmpz_mod_poly_evaluate_fmpz_vec_fast_precomp(ys, poly, plen, tree, n, mod);
_fmpz_mod_poly_tree_free(tree, n);
}
void
fmpz_mod_poly_evaluate_fmpz_vec_fast(fmpz * ys,
const fmpz_mod_poly_t poly, const fmpz * xs, slong n)
{
_fmpz_mod_poly_evaluate_fmpz_vec_fast(ys, poly->coeffs,
poly->length, xs, n, &(poly->p));
}

View File

@@ -0,0 +1,49 @@
/*=============================================================================
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
Copyright (C) 2012 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "ulong_extras.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void
_fmpz_mod_poly_evaluate_fmpz_vec_iter(fmpz * ys, const fmpz * coeffs, slong len,
const fmpz * xs, slong n, const fmpz_t mod)
{
slong i;
for (i = 0; i < n; i++)
_fmpz_mod_poly_evaluate_fmpz(ys + i, coeffs, len, xs + i, mod);
}
void
fmpz_mod_poly_evaluate_fmpz_vec_iter(fmpz * ys,
const fmpz_mod_poly_t poly, const fmpz * xs, slong n)
{
_fmpz_mod_poly_evaluate_fmpz_vec_iter(ys, poly->coeffs,
poly->length, xs, n, &(poly->p));
}

View File

@@ -0,0 +1,42 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_fit_length(fmpz_mod_poly_t poly, slong len)
{
if (len > poly->alloc)
{
/* At least double number of allocated coeffs */
if (len < 2 * poly->alloc)
len = 2 * poly->alloc;
fmpz_mod_poly_realloc(poly, len);
}
}

View File

@@ -0,0 +1,70 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <stdio.h>
#include <gmp.h>
#include "fmpz.h"
#include "fmpz_mod_poly.h"
int _fmpz_mod_poly_fprint(FILE * file, const fmpz *poly, slong len,
const fmpz_t p)
{
int r;
slong i;
r = flint_fprintf(file, "%wd ", len);
if (r <= 0)
return r;
r = fmpz_fprint(file, p);
if (r <= 0)
return r;
if (len == 0)
return r;
r = flint_fprintf(file, " ");
if (r <= 0)
return r;
for (i = 0; (r > 0) && (i < len); i++)
{
r = flint_fprintf(file, " ");
if (r <= 0)
return r;
r = fmpz_fprint(file, poly + i);
if (r <= 0)
return r;
}
return r;
}
int fmpz_mod_poly_fprint(FILE * file, const fmpz_mod_poly_t poly)
{
return _fmpz_mod_poly_fprint(file, poly->coeffs, poly->length, &(poly->p));
}

View File

@@ -0,0 +1,69 @@
/*=============================================================================
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) 2007, David Howden.
Copyright (C) 2010 William Hart
******************************************************************************/
#include <gmp.h>
#include <stdio.h>
#include "flint.h"
#include "fmpz_mod_poly.h"
#include "fmpz.h"
int fmpz_mod_poly_fread(FILE * f, fmpz_mod_poly_t poly)
{
slong i, length;
fmpz_t coeff;
ulong res;
fmpz_init(coeff);
if (flint_fscanf(f, "%wd", &length) != 1) {
fmpz_clear(coeff);
return 0;
}
fmpz_fread(f,coeff);
fmpz_mod_poly_clear(poly);
fmpz_mod_poly_init(poly, coeff);
fmpz_mod_poly_fit_length(poly, length);
poly->length = length;
for (i = 0; i < length; i++)
{
res = fmpz_fread(f, coeff);
fmpz_mod_poly_set_coeff_fmpz(poly,i,coeff);
if (!res)
{
poly->length = i;
fmpz_clear(coeff);
return 0;
}
}
fmpz_clear(coeff);
_fmpz_mod_poly_normalise(poly);
return 1;
}

View File

@@ -0,0 +1,151 @@
/*=============================================================================
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 William Hart
Copyright (C) 2011 Sebastian Pancratz
******************************************************************************/
#include <stdlib.h>
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
slong _fmpz_mod_poly_gcd_euclidean(fmpz *G, const fmpz *A, slong lenA,
const fmpz *B, slong lenB,
const fmpz_t invB, const fmpz_t p)
{
if (lenB == 1)
{
fmpz_one(G);
return 1;
}
else /* lenA >= lenB > 1 */
{
const slong lenW = FLINT_MAX(lenA - lenB + 1, lenB) + lenA + 2 * lenB;
fmpz_t invR3;
fmpz *Q, *R1, *R2, *R3, *T, *W;
slong lenR2, lenR3;
W = _fmpz_vec_init(lenW);
Q = W;
R1 = W + FLINT_MAX(lenA - lenB + 1, lenB);
R2 = R1 + lenA;
R3 = R2 + lenB;
_fmpz_mod_poly_divrem(Q, R1, A, lenA, B, lenB, invB, p);
lenR3 = lenB - 1;
FMPZ_VEC_NORM(R1, lenR3);
if (lenR3 == 0)
{
_fmpz_vec_set(G, B, lenB);
_fmpz_vec_clear(W, lenW);
return lenB;
}
fmpz_init(invR3);
T = R3;
R3 = R1;
R1 = T;
_fmpz_vec_set(R2, B, lenB);
lenR2 = lenB;
do
{
fmpz_invmod(invR3, R3 + (lenR3 - 1), p);
_fmpz_mod_poly_divrem(Q, R1, R2, lenR2, R3, lenR3, invR3, p);
lenR2 = lenR3--;
FMPZ_VEC_NORM(R1, lenR3);
T = R2; R2 = R3; R3 = R1; R1 = T;
}
while (lenR3 > 0);
_fmpz_vec_set(G, R2, lenR2);
_fmpz_vec_clear(W, lenW);
fmpz_clear(invR3);
return lenR2;
}
}
void fmpz_mod_poly_gcd_euclidean(fmpz_mod_poly_t G,
const fmpz_mod_poly_t A,
const fmpz_mod_poly_t B)
{
if (A->length < B->length)
{
fmpz_mod_poly_gcd_euclidean(G, B, A);
}
else /* lenA >= lenB >= 0 */
{
const slong lenA = A->length, lenB = B->length;
slong lenG;
fmpz *g;
if (lenA == 0) /* lenA = lenB = 0 */
{
fmpz_mod_poly_zero(G);
}
else if (lenB == 0) /* lenA > lenB = 0 */
{
fmpz_mod_poly_make_monic(G, A);
}
else /* lenA >= lenB >= 1 */
{
fmpz_t invB;
if (G == A || G == B)
{
g = _fmpz_vec_init(FLINT_MIN(lenA, lenB));
}
else
{
fmpz_mod_poly_fit_length(G, FLINT_MIN(lenA, lenB));
g = G->coeffs;
}
fmpz_init(invB);
fmpz_invmod(invB, fmpz_mod_poly_lead(B), &(B->p));
lenG = _fmpz_mod_poly_gcd_euclidean(g, A->coeffs, lenA,
B->coeffs, lenB, invB, &(B->p));
fmpz_clear(invB);
if (G == A || G == B)
{
_fmpz_vec_clear(G->coeffs, G->alloc);
G->coeffs = g;
G->alloc = FLINT_MIN(lenA, lenB);
G->length = FLINT_MIN(lenA, lenB);
}
_fmpz_mod_poly_set_length(G, lenG);
if (lenG == 1)
fmpz_one(G->coeffs);
else
fmpz_mod_poly_make_monic(G, G);
}
}
}

View File

@@ -0,0 +1,176 @@
/*=============================================================================
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 William Hart
Copyright (C) 2011 Sebastian Pancratz
******************************************************************************/
#include <stdlib.h>
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
slong _fmpz_mod_poly_gcd_euclidean_f(fmpz_t f, fmpz *G,
const fmpz *A, slong lenA,
const fmpz *B, slong lenB, const fmpz_t p)
{
slong lenG = 0;
if (lenB == 1)
{
fmpz_t invB;
fmpz_init(invB);
fmpz_gcdinv(f, invB, B, p);
if (fmpz_is_one(f))
{
fmpz_one(G);
lenG = 1;
}
fmpz_clear(invB);
}
else /* lenA >= lenB > 1 */
{
const slong lenW = FLINT_MAX(lenA - lenB + 1, lenB) + lenA + 2 * lenB;
fmpz *Q, *R1, *R2, *R3, *T, *W;
slong lenR2, lenR3;
W = _fmpz_vec_init(lenW);
Q = W;
R1 = W + FLINT_MAX(lenA - lenB + 1, lenB);
R2 = R1 + lenA;
R3 = R2 + lenB;
_fmpz_mod_poly_divrem_f(f, Q, R1, A, lenA, B, lenB, p);
if (!fmpz_is_one(f))
goto exit;
lenR3 = lenB - 1;
FMPZ_VEC_NORM(R1, lenR3);
if (lenR3 == 0)
{
_fmpz_vec_set(G, B, lenB);
lenG = lenB;
}
else
{
T = R3;
R3 = R1;
R1 = T;
_fmpz_vec_set(R2, B, lenB);
lenR2 = lenB;
do
{
_fmpz_mod_poly_divrem_f(f, Q, R1, R2, lenR2, R3, lenR3, p);
if (!fmpz_is_one(f))
goto exit;
lenR2 = lenR3--;
FMPZ_VEC_NORM(R1, lenR3);
T = R2; R2 = R3; R3 = R1; R1 = T;
}
while (lenR3 > 0);
_fmpz_vec_set(G, R2, lenR2);
lenG = lenR2;
}
exit:
_fmpz_vec_clear(W, lenW);
}
return lenG;
}
void fmpz_mod_poly_gcd_euclidean_f(fmpz_t f, fmpz_mod_poly_t G,
const fmpz_mod_poly_t A,
const fmpz_mod_poly_t B)
{
if (A->length < B->length)
{
fmpz_mod_poly_gcd_euclidean_f(f, G, B, A);
}
else /* lenA >= lenB >= 0 */
{
const slong lenA = A->length, lenB = B->length;
slong lenG;
fmpz *g;
if (lenA == 0) /* lenA = lenB = 0 */
{
fmpz_mod_poly_zero(G);
fmpz_one(f);
}
else if (lenB == 0) /* lenA > lenB = 0 */
{
fmpz_t invA;
fmpz_init(invA);
fmpz_gcdinv(f, invA, A->coeffs + lenA - 1, &B->p);
if (fmpz_is_one(f))
fmpz_mod_poly_scalar_mul_fmpz(G, A, invA);
else
fmpz_mod_poly_zero(G);
fmpz_clear(invA);
}
else /* lenA >= lenB >= 1 */
{
if (G == A || G == B)
{
g = _fmpz_vec_init(FLINT_MIN(lenA, lenB));
}
else
{
fmpz_mod_poly_fit_length(G, FLINT_MIN(lenA, lenB));
g = G->coeffs;
}
lenG = _fmpz_mod_poly_gcd_euclidean_f(f, g, A->coeffs, lenA,
B->coeffs, lenB, &(B->p));
if (fmpz_is_one(f))
{
if (G == A || G == B)
{
_fmpz_vec_clear(G->coeffs, G->alloc);
G->coeffs = g;
G->alloc = FLINT_MIN(lenA, lenB);
G->length = FLINT_MIN(lenA, lenB);
}
_fmpz_mod_poly_set_length(G, lenG);
if (lenG == 1)
fmpz_one(G->coeffs);
else
fmpz_mod_poly_make_monic(G, G);
}
else /* Factor found, ensure G is normalised */
{
if (G == A || G == B)
_fmpz_vec_clear(g, FLINT_MIN(lenA, lenB));
else
{
_fmpz_vec_zero(G->coeffs, FLINT_MIN(lenA, lenB));
_fmpz_mod_poly_set_length(G, 0);
}
}
}
}
}

View File

@@ -0,0 +1,133 @@
/*=============================================================================
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 William Hart
Copyright (C) 2012 Sebastian Pancratz
******************************************************************************/
#include <stdlib.h>
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
slong _fmpz_mod_poly_gcdinv(fmpz *G, fmpz *S,
const fmpz *A, slong lenA, const fmpz *B, slong lenB,
const fmpz_t p)
{
fmpz *T;
fmpz_t inv;
slong ans;
T = _fmpz_vec_init(lenA - 1);
fmpz_init(inv);
fmpz_invmod(inv, A + (lenA - 1), p);
ans = _fmpz_mod_poly_xgcd(G, T, S, B, lenB, A, lenA, inv, p);
fmpz_clear(inv);
_fmpz_vec_clear(T, lenA - 1);
return ans;
}
void fmpz_mod_poly_gcdinv(fmpz_mod_poly_t G, fmpz_mod_poly_t S,
const fmpz_mod_poly_t A, const fmpz_mod_poly_t B)
{
const slong lenA = A->length, lenB = B->length;
if (lenB < 2)
{
flint_printf("Exception (fmpz_mod_poly_gcdinv). lenB < 2.\n");
abort();
}
if (lenA >= lenB)
{
fmpz_mod_poly_t T;
fmpz_mod_poly_init(T, &A->p);
fmpz_mod_poly_rem(T, A, B);
fmpz_mod_poly_gcdinv(G, S, T, B);
fmpz_mod_poly_clear(T);
return;
}
if (lenA == 0)
{
fmpz_mod_poly_zero(G);
fmpz_mod_poly_zero(S);
}
else
{
fmpz *g, *s;
slong lenG;
if (G == A || G == B)
{
g = _fmpz_vec_init(lenA);
}
else
{
fmpz_mod_poly_fit_length(G, lenA);
g = G->coeffs;
}
if (S == A || S == B)
{
s = _fmpz_vec_init(lenB - 1);
}
else
{
fmpz_mod_poly_fit_length(S, lenB - 1);
s = S->coeffs;
}
lenG = _fmpz_mod_poly_gcdinv(g, s,
A->coeffs, lenA, B->coeffs, lenB, &A->p);
if (G == A || G == B)
{
_fmpz_vec_clear(G->coeffs, G->alloc);
G->coeffs = g;
G->alloc = lenA;
}
if (S == A || S == B)
{
_fmpz_vec_clear(S->coeffs, S->alloc);
S->coeffs = s;
S->alloc = lenB - 1;
}
_fmpz_mod_poly_set_length(G, lenG);
_fmpz_mod_poly_set_length(S, lenB - lenG);
_fmpz_mod_poly_normalise(S);
if (!fmpz_is_one(fmpz_mod_poly_lead(G)))
{
fmpz_t inv;
fmpz_init(inv);
fmpz_invmod(inv, fmpz_mod_poly_lead(G), &A->p);
fmpz_mod_poly_scalar_mul_fmpz(G, G, inv);
fmpz_mod_poly_scalar_mul_fmpz(S, S, inv);
fmpz_clear(inv);
}
}
}

View File

@@ -0,0 +1,38 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_get_fmpz_poly(fmpz_poly_t f, const fmpz_mod_poly_t g)
{
fmpz_poly_fit_length(f, g->length);
_fmpz_poly_set_length(f, g->length);
_fmpz_vec_set(f->coeffs, g->coeffs, g->length);
}

View File

@@ -0,0 +1,53 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include <stdlib.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_init(fmpz_mod_poly_t poly, const fmpz_t p)
{
poly->coeffs = NULL;
poly->alloc = 0;
poly->length = 0;
fmpz_init(&(poly->p));
fmpz_set(&(poly->p), p);
}
void fmpz_mod_poly_init2(fmpz_mod_poly_t poly, const fmpz_t p, slong alloc)
{
if (alloc) /* allocate space for alloc small coeffs */
poly->coeffs = (fmpz *) flint_calloc(alloc, sizeof(fmpz));
else
poly->coeffs = NULL;
poly->alloc = alloc;
poly->length = 0;
fmpz_init(&(poly->p));
fmpz_set(&(poly->p), p);
}

View File

@@ -0,0 +1,134 @@
/*=============================================================================
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, 2011 Sebastian Pancratz
******************************************************************************/
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_vec.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
#define FMPZ_MOD_POLY_INV_NEWTON_CUTOFF 64
void
_fmpz_mod_poly_inv_series_newton(fmpz * Qinv, const fmpz * Q, slong n,
const fmpz_t cinv, const fmpz_t p)
{
if (n == 1) /* {Q,1} * cinv == 1 mod (x) */
{
fmpz_set(Qinv, cinv);
}
else
{
const slong alloc = FLINT_MAX(n, 3 * FMPZ_MOD_POLY_INV_NEWTON_CUTOFF);
slong *a, i, m;
fmpz *W;
W = _fmpz_vec_init(alloc);
for (i = 1; (WORD(1) << i) < n; i++) ;
a = (slong *) flint_malloc(i * sizeof(slong));
a[i = 0] = n;
while (n >= FMPZ_MOD_POLY_INV_NEWTON_CUTOFF)
a[++i] = (n = (n + 1) / 2);
/* Base case */
{
fmpz *Qrev = W + 2 * FMPZ_MOD_POLY_INV_NEWTON_CUTOFF;
_fmpz_poly_reverse(Qrev, Q, n, n);
_fmpz_vec_zero(W, 2*n - 2);
fmpz_one(W + (2*n - 2));
_fmpz_mod_poly_div_basecase(Qinv, W, W, 2*n - 1, Qrev, n, cinv, p);
_fmpz_poly_reverse(Qinv, Qinv, n, n);
}
for (i--; i >= 0; i--)
{
m = n;
n = a[i];
_fmpz_mod_poly_mullow(W, Q, n, Qinv, m, p, n);
_fmpz_mod_poly_mullow(Qinv + m, Qinv, m, W + m, n - m, p, n - m);
_fmpz_mod_poly_neg(Qinv + m, Qinv + m, n - m, p);
}
_fmpz_vec_clear(W, alloc);
flint_free(a);
}
}
void fmpz_mod_poly_inv_series_newton(fmpz_mod_poly_t Qinv,
const fmpz_mod_poly_t Q, slong n)
{
const fmpz *p = &(Q->p);
fmpz_t cinv;
fmpz *Qcopy;
int Qalloc;
if (Q->length >= n)
{
Qcopy = Q->coeffs;
Qalloc = 0;
}
else
{
slong i;
Qcopy = (fmpz *) flint_malloc(n * sizeof(fmpz));
for (i = 0; i < Q->length; i++)
Qcopy[i] = Q->coeffs[i];
flint_mpn_zero((mp_ptr) Qcopy + i, n - i);
Qalloc = 1;
}
fmpz_init(cinv);
fmpz_invmod(cinv, Q->coeffs, p);
if (Qinv != Q)
{
fmpz_mod_poly_fit_length(Qinv, n);
_fmpz_mod_poly_inv_series_newton(Qinv->coeffs, Qcopy, n, cinv, p);
}
else
{
fmpz *t = _fmpz_vec_init(n);
_fmpz_mod_poly_inv_series_newton(t, Qcopy, n, cinv, p);
_fmpz_vec_clear(Qinv->coeffs, Qinv->alloc);
Qinv->coeffs = t;
Qinv->alloc = n;
Qinv->length = n;
}
_fmpz_mod_poly_set_length(Qinv, n);
_fmpz_mod_poly_normalise(Qinv);
if (Qalloc)
flint_free(Qcopy);
fmpz_clear(cinv);
}

View File

@@ -0,0 +1,108 @@
/*=============================================================================
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 William Hart
Copyright (C) 2012 Sebastian Pancratz
******************************************************************************/
#include <stdlib.h>
#include "fmpz_mod_poly.h"
int _fmpz_mod_poly_invmod(fmpz *A,
const fmpz *B, slong lenB,
const fmpz *P, slong lenP, const fmpz_t p)
{
fmpz *G;
slong lenG;
FMPZ_VEC_NORM(B, lenB);
G = _fmpz_vec_init(lenB);
lenG = _fmpz_mod_poly_gcdinv(G, A, B, lenB, P, lenP, p);
if (lenG == 1 && !fmpz_is_one(G + 0))
{
fmpz_t invG;
fmpz_init(invG);
fmpz_invmod(invG, G + 0, p);
_fmpz_mod_poly_scalar_mul_fmpz(A, A, lenP - 1, invG, p);
fmpz_clear(invG);
}
_fmpz_vec_clear(G, lenB);
return (lenG == 1);
}
int fmpz_mod_poly_invmod(fmpz_mod_poly_t A,
const fmpz_mod_poly_t B, const fmpz_mod_poly_t P)
{
const slong lenB = B->length, lenP = P->length;
fmpz *t;
int ans;
if (lenP < 2)
{
flint_printf("Exception (fmpz_mod_poly_invmod). lenP < 2.\n");
abort();
}
if (lenB == 0)
{
fmpz_mod_poly_zero(A);
return 0;
}
if (lenB >= lenP)
{
fmpz_mod_poly_t T;
fmpz_mod_poly_init(T, &B->p);
fmpz_mod_poly_rem(T, B, P);
ans = fmpz_mod_poly_invmod(A, T, P);
fmpz_mod_poly_clear(T);
return ans;
}
if (A != B && A != P)
{
fmpz_mod_poly_fit_length(A, lenP - 1);
t = A->coeffs;
}
else
{
t = _fmpz_vec_init(lenP);
}
ans = _fmpz_mod_poly_invmod(t, B->coeffs, lenB, P->coeffs, lenP, &B->p);
if (A == B || A == P)
{
_fmpz_vec_clear(A->coeffs, A->alloc);
A->coeffs = t;
A->alloc = lenP - 1;
}
_fmpz_mod_poly_set_length(A, lenP - 1);
_fmpz_mod_poly_normalise(A);
return ans;
}

View File

@@ -0,0 +1,52 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_make_monic(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly)
{
const slong len = poly->length;
fmpz_t inv;
if (len == 0)
{
fmpz_mod_poly_zero(res);
return;
}
fmpz_init(inv);
fmpz_invmod(inv, fmpz_mod_poly_lead(poly), &(poly->p));
fmpz_mod_poly_fit_length(res, len);
_fmpz_mod_poly_set_length(res, len);
_fmpz_mod_poly_scalar_mul_fmpz(res->coeffs,
poly->coeffs, len, inv, &(poly->p));
fmpz_clear(inv);
}

View File

@@ -0,0 +1,84 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2010 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_mul(fmpz *res, const fmpz *poly1, slong len1,
const fmpz *poly2, slong len2, const fmpz_t p)
{
_fmpz_poly_mul(res, poly1, len1, poly2, len2);
_fmpz_vec_scalar_mod_fmpz(res, res, len1 + len2 - 1, p);
}
void fmpz_mod_poly_mul(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly1, const fmpz_mod_poly_t poly2)
{
const slong len1 = poly1->length;
const slong len2 = poly2->length;
const slong lenr = len1 + len2 - 1;
if ((len1 == 0) || (len2 == 0))
{
fmpz_mod_poly_zero(res);
return;
}
if ((res == poly1) || (res == poly2))
{
fmpz *t = _fmpz_vec_init(lenr);
if (len1 >= len2)
_fmpz_mod_poly_mul(t, poly1->coeffs, len1,
poly2->coeffs, len2, &(res->p));
else
_fmpz_mod_poly_mul(t, poly2->coeffs, len2,
poly1->coeffs, len1, &(res->p));
_fmpz_vec_clear(res->coeffs, res->alloc);
res->alloc = lenr;
res->length = lenr;
res->coeffs = t;
}
else
{
fmpz_mod_poly_fit_length(res, lenr);
if (len1 >= len2)
_fmpz_mod_poly_mul(res->coeffs, poly1->coeffs, len1,
poly2->coeffs, len2, &(res->p));
else
_fmpz_mod_poly_mul(res->coeffs, poly2->coeffs, len2,
poly1->coeffs, len1, &(res->p));
_fmpz_mod_poly_set_length(res, lenr);
}
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,87 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2010 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_mullow(fmpz *res, const fmpz *poly1, slong len1,
const fmpz *poly2, slong len2,
const fmpz_t p, slong n)
{
_fmpz_poly_mullow(res, poly1, len1, poly2, len2, n);
_fmpz_vec_scalar_mod_fmpz(res, res, n, p);
}
void fmpz_mod_poly_mullow(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly1, const fmpz_mod_poly_t poly2, slong n)
{
const slong len1 = poly1->length;
const slong len2 = poly2->length;
if ((len1 == 0) || (len2 == 0) || (n == 0))
{
fmpz_mod_poly_zero(res);
return;
}
n = FLINT_MIN(n, len1 + len2 - 1);
if ((res == poly1) || (res == poly2))
{
fmpz *t = _fmpz_vec_init(n);
if (len1 >= len2)
_fmpz_mod_poly_mullow(t, poly1->coeffs, len1,
poly2->coeffs, len2, &(res->p), n);
else
_fmpz_mod_poly_mullow(t, poly2->coeffs, len2,
poly1->coeffs, len1, &(res->p), n);
_fmpz_vec_clear(res->coeffs, res->alloc);
res->coeffs = t;
res->alloc = n;
res->length = n;
_fmpz_mod_poly_normalise(res);
}
else
{
fmpz_mod_poly_fit_length(res, n);
if (len1 >= len2)
_fmpz_mod_poly_mullow(res->coeffs, poly1->coeffs, len1,
poly2->coeffs, len2, &(res->p), n);
else
_fmpz_mod_poly_mullow(res->coeffs, poly2->coeffs, len2,
poly1->coeffs, len1, &(res->p), n);
_fmpz_mod_poly_set_length(res, n);
_fmpz_mod_poly_normalise(res);
}
}

View File

@@ -0,0 +1,110 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_mulmod(fmpz * res, const fmpz * poly1, slong len1,
const fmpz * poly2, slong len2, const fmpz * f,
slong lenf, const fmpz_t p)
{
fmpz * T, * Q;
fmpz_t invf;
slong lenT, lenQ;
lenT = len1 + len2 - 1;
lenQ = lenT - lenf + 1;
T = _fmpz_vec_init(lenT + lenQ);
Q = T + lenT;
if (len1 >= len2)
_fmpz_mod_poly_mul(T, poly1, len1, poly2, len2, p);
else
_fmpz_mod_poly_mul(T, poly2, len2, poly1, len1, p);
fmpz_init(invf);
fmpz_invmod(invf, f + lenf - 1, p);
_fmpz_mod_poly_divrem(Q, res, T, lenT, f, lenf, invf, p);
_fmpz_vec_clear(T, lenT + lenQ);
fmpz_clear(invf);
}
void
fmpz_mod_poly_mulmod(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly1,
const fmpz_mod_poly_t poly2, const fmpz_mod_poly_t f)
{
slong len1, len2, lenf;
fmpz * fcoeffs;
lenf = f->length;
len1 = poly1->length;
len2 = poly2->length;
if (lenf == 0)
{
flint_printf("Exception (fmpz_mod_poly_mulmod). Divide by zero\n");
abort();
}
if (lenf == 1 || len1 == 0 || len2 == 0)
{
fmpz_mod_poly_zero(res);
return;
}
if (len1 + len2 - lenf > 0)
{
if (f == res)
{
fcoeffs = _fmpz_vec_init(lenf);
_fmpz_vec_set(fcoeffs, f->coeffs, lenf);
}
else
fcoeffs = f->coeffs;
fmpz_mod_poly_fit_length(res, len1 + len2 - 1);
_fmpz_mod_poly_mulmod(res->coeffs, poly1->coeffs, len1,
poly2->coeffs, len2,
fcoeffs, lenf,
&res->p);
if (f == res)
_fmpz_vec_clear(fcoeffs, lenf);
_fmpz_mod_poly_set_length(res, lenf - 1);
_fmpz_mod_poly_normalise(res);
}
else
{
fmpz_mod_poly_mul(res, poly1, poly2);
}
}

View File

@@ -0,0 +1,121 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_mulmod_preinv(fmpz * res, const fmpz * poly1, slong len1,
const fmpz * poly2, slong len2, const fmpz * f, slong lenf,
const fmpz* finv, slong lenfinv, const fmpz_t p)
{
fmpz * T, * Q;
slong lenT, lenQ;
lenT = len1 + len2 - 1;
lenQ = lenT - lenf + 1;
T = _fmpz_vec_init(lenT + lenQ);
Q = T + lenT;
if (len1 >= len2)
_fmpz_mod_poly_mul(T, poly1, len1, poly2, len2, p);
else
_fmpz_mod_poly_mul(T, poly2, len2, poly1, len1, p);
_fmpz_mod_poly_divrem_newton_n_preinv(Q, res, T, lenT, f, lenf,
finv, lenfinv, p);
_fmpz_vec_clear(T, lenT + lenQ);
}
void
fmpz_mod_poly_mulmod_preinv(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly1,
const fmpz_mod_poly_t poly2, const fmpz_mod_poly_t f,
const fmpz_mod_poly_t finv)
{
slong len1, len2, lenf;
fmpz * fcoeffs;
lenf = f->length;
len1 = poly1->length;
len2 = poly2->length;
if (lenf == 0)
{
flint_printf("Exception (fmpz_mod_poly_mulmod_preinv). Divide by zero\n");
abort();
}
if (lenf <= len1 || lenf <= len2)
{
flint_printf("Exception (fmpz_mod_poly_mulmod_preinv). Input larger than modulus.\n");
abort();
}
if (lenf == 1 || len1 == 0 || len2 == 0)
{
fmpz_mod_poly_zero(res);
return;
}
if (len1 + len2 - lenf > 0)
{
if (f == res)
{
fcoeffs = _fmpz_vec_init(lenf);
_fmpz_vec_set(fcoeffs, f->coeffs, lenf);
}
else
fcoeffs = f->coeffs;
fmpz_mod_poly_fit_length(res, len1 + len2 - 1);
_fmpz_mod_poly_mulmod_preinv(res->coeffs, poly1->coeffs, len1,
poly2->coeffs, len2, fcoeffs, lenf,
finv->coeffs, finv->length, &res->p);
if (f == res)
_fmpz_vec_clear(fcoeffs, lenf);
_fmpz_mod_poly_set_length(res, lenf - 1);
_fmpz_mod_poly_normalise(res);
}
else
{
fmpz_mod_poly_mul(res, poly1, poly2);
}
}

View File

@@ -0,0 +1,54 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_neg(fmpz *res, const fmpz *poly, slong len, const fmpz_t p)
{
slong i;
for (i = 0; i < len; i++)
{
if (poly[i])
fmpz_sub(res + i, p, poly + i);
else
fmpz_zero(res + i);
}
}
void fmpz_mod_poly_neg(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly)
{
const slong len = poly->length;
fmpz_mod_poly_fit_length(res, len);
_fmpz_mod_poly_set_length(res, len);
_fmpz_mod_poly_neg(res->coeffs, poly->coeffs, poly->length, &(poly->p));
}

View File

@@ -0,0 +1,39 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_normalise(fmpz_mod_poly_t poly)
{
slong i;
for (i = poly->length - 1; (i >= 0) && !poly->coeffs[i]; i--) ;
poly->length = i + 1;
}

162
external/flint-2.4.3/fmpz_mod_poly/pow.c vendored Normal file
View File

@@ -0,0 +1,162 @@
/*=============================================================================
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, 2011 Sebastian Pancratz
******************************************************************************/
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_vec.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_pow(fmpz *res, const fmpz *poly, slong len, ulong e,
const fmpz_t p)
{
ulong bit = ~((~UWORD(0)) >> 1);
slong rlen;
slong alloc = (slong) e * (len - 1) + 1;
fmpz *v = _fmpz_vec_init(alloc);
fmpz *R, *S, *T;
/*
Set bits to the bitmask with a 1 one place lower than the msb of e
*/
while ((bit & e) == UWORD(0))
bit >>= 1;
bit >>= 1;
/*
Trial run without any polynomial arithmetic to determine the parity
of the number of swaps; then set R and S accordingly
*/
{
unsigned int swaps = 0U;
ulong bit2 = bit;
if ((bit2 & e))
swaps = ~swaps;
while (bit2 >>= 1)
if ((bit2 & e) == UWORD(0))
swaps = ~swaps;
if (swaps == 0U)
{
R = res;
S = v;
}
else
{
R = v;
S = res;
}
}
/*
We unroll the first step of the loop, referring to {poly, len}
*/
_fmpz_mod_poly_sqr(R, poly, len, p);
rlen = 2 * len - 1;
if ((bit & e))
{
_fmpz_mod_poly_mul(S, R, rlen, poly, len, p);
rlen += len - 1;
T = R;
R = S;
S = T;
}
while ((bit >>= 1))
{
if ((bit & e))
{
_fmpz_mod_poly_sqr(S, R, rlen, p);
rlen += rlen - 1;
_fmpz_mod_poly_mul(R, S, rlen, poly, len, p);
rlen += len - 1;
}
else
{
_fmpz_mod_poly_sqr(S, R, rlen, p);
rlen += rlen - 1;
T = R;
R = S;
S = T;
}
}
_fmpz_vec_clear(v, alloc);
}
void fmpz_mod_poly_pow(fmpz_mod_poly_t rop, const fmpz_mod_poly_t op, ulong e)
{
const slong len = op->length;
slong rlen;
if ((len < 2) || (e < UWORD(3)))
{
if (e == UWORD(0))
fmpz_mod_poly_set_ui(rop, 1);
else if (len == 0)
fmpz_mod_poly_zero(rop);
else if (len == 1)
{
fmpz_mod_poly_fit_length(rop, 1);
fmpz_powm_ui(rop->coeffs, op->coeffs, e, &(rop->p));
_fmpz_mod_poly_set_length(rop, 1);
_fmpz_mod_poly_normalise(rop);
}
else if (e == UWORD(1))
fmpz_mod_poly_set(rop, op);
else /* e == UWORD(2) */
fmpz_mod_poly_sqr(rop, op);
return;
}
rlen = (slong) e * (len - 1) + 1;
if (rop != op)
{
fmpz_mod_poly_fit_length(rop, rlen);
_fmpz_mod_poly_pow(rop->coeffs, op->coeffs, len, e, &(rop->p));
_fmpz_mod_poly_set_length(rop, rlen);
}
else
{
fmpz *t = _fmpz_vec_init(rlen);
_fmpz_mod_poly_pow(t, op->coeffs, len, e, &(rop->p));
_fmpz_vec_clear(rop->coeffs, rop->alloc);
rop->coeffs = t;
rop->alloc = rlen;
rop->length = rlen;
}
_fmpz_mod_poly_normalise(rop);
}

View File

@@ -0,0 +1,105 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2010 William Hart
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void
_fmpz_mod_poly_pow_trunc(fmpz * res, const fmpz * poly,
ulong e, slong trunc, const fmpz_t p)
{
_fmpz_mod_poly_pow_trunc_binexp(res, poly, e, trunc, p);
}
void
fmpz_mod_poly_pow_trunc(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly, ulong e, slong trunc)
{
const slong len = poly->length;
fmpz * q;
int qcopy = 0;
if (len < 2 || e < UWORD(3) || trunc == 0)
{
if (len == 0 || trunc == 0)
fmpz_mod_poly_zero(res);
else if (len == 1)
{
fmpz_mod_poly_fit_length(res, 1);
fmpz_powm_ui(res->coeffs, poly->coeffs, e, &res->p);
_fmpz_mod_poly_set_length(res, 1);
_fmpz_mod_poly_normalise(res);
}
else if (e == UWORD(0))
{
fmpz_mod_poly_set_coeff_ui(res, 0, UWORD(1));
_fmpz_mod_poly_set_length(res, 1);
_fmpz_mod_poly_normalise(res);
}
else if (e == UWORD(1))
{
fmpz_mod_poly_set(res, poly);
fmpz_mod_poly_truncate(res, trunc);
}
else /* e == UWORD(2) */
fmpz_mod_poly_mullow(res, poly, poly, trunc);
return;
}
if (poly->length < trunc)
{
q = _fmpz_vec_init(trunc);
_fmpz_vec_set(q, poly->coeffs, poly->length);
_fmpz_vec_zero(q + poly->length, trunc - poly->length);
qcopy = 1;
} else
q = poly->coeffs;
if (res != poly || qcopy)
{
fmpz_mod_poly_fit_length(res, trunc);
_fmpz_mod_poly_pow_trunc(res->coeffs, q, e, trunc, &poly->p);
}
else
{
fmpz_mod_poly_t t;
fmpz_mod_poly_init2(t, &poly->p, trunc);
_fmpz_mod_poly_pow_trunc(t->coeffs, q, e, trunc, &poly->p);
fmpz_mod_poly_swap(res, t);
fmpz_mod_poly_clear(t);
}
if (qcopy)
_fmpz_vec_clear(q, trunc);
_fmpz_mod_poly_set_length(res, trunc);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,174 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2010 William Hart
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void
_fmpz_mod_poly_pow_trunc_binexp(fmpz * res, const fmpz * poly,
ulong e, slong trunc, const fmpz_t p)
{
ulong bit = ~((~UWORD(0)) >> 1);
fmpz * v = _fmpz_vec_init(trunc);
fmpz * R, * S, * T;
/*
Set bits to the bitmask with a 1 one place lower than the msb of e
*/
while ((bit & e) == UWORD(0))
bit >>= 1;
bit >>= 1;
/*
Trial run without any polynomial arithmetic to determine the parity
of the number of swaps; then set R and S accordingly
*/
{
unsigned int swaps = 0U;
ulong bit2 = bit;
if ((bit2 & e))
swaps = ~swaps;
while (bit2 >>= 1)
if ((bit2 & e) == UWORD(0))
swaps = ~swaps;
if (swaps == 0U)
{
R = res;
S = v;
}
else
{
R = v;
S = res;
}
}
/*
We unroll the first step of the loop, referring to {poly, len}
*/
_fmpz_mod_poly_mullow(R, poly, trunc, poly, trunc, p, trunc);
if ((bit & e))
{
_fmpz_mod_poly_mullow(S, R, trunc, poly, trunc, p, trunc);
T = R;
R = S;
S = T;
}
while ((bit >>= 1))
{
if ((bit & e))
{
_fmpz_mod_poly_mullow(S, R, trunc, R, trunc, p, trunc);
_fmpz_mod_poly_mullow(R, S, trunc, poly, trunc, p, trunc);
}
else
{
_fmpz_mod_poly_mullow(S, R, trunc, R, trunc, p, trunc);
T = R;
R = S;
S = T;
}
}
_fmpz_vec_clear(v, trunc);
}
void
fmpz_mod_poly_pow_trunc_binexp(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly, ulong e, slong trunc)
{
const slong len = poly->length;
fmpz * q;
int qcopy = 0;
if (len < 2 || e < UWORD(3) || trunc == 0)
{
if (len == 0 || trunc == 0)
fmpz_mod_poly_zero(res);
else if (len == 1)
{
fmpz_mod_poly_fit_length(res, 1);
fmpz_powm_ui(res->coeffs, poly->coeffs, e, &res->p);
_fmpz_mod_poly_set_length(res, 1);
_fmpz_mod_poly_normalise(res);
}
else if (e == UWORD(0))
{
fmpz_mod_poly_set_coeff_ui(res, 0, UWORD(1));
_fmpz_mod_poly_set_length(res, 1);
_fmpz_mod_poly_normalise(res);
}
else if (e == UWORD(1))
{
fmpz_mod_poly_set(res, poly);
fmpz_mod_poly_truncate(res, trunc);
}
else /* e == UWORD(2) */
fmpz_mod_poly_mullow(res, poly, poly, trunc);
return;
}
if (poly->length < trunc)
{
q = _fmpz_vec_init(trunc);
_fmpz_vec_set(q, poly->coeffs, poly->length);
_fmpz_vec_zero(q + poly->length, trunc - poly->length);
qcopy = 1;
} else
q = poly->coeffs;
if (res != poly || qcopy)
{
fmpz_mod_poly_fit_length(res, trunc);
_fmpz_mod_poly_pow_trunc_binexp(res->coeffs, q, e, trunc, &poly->p);
}
else
{
fmpz_mod_poly_t t;
fmpz_mod_poly_init2(t, &poly->p, trunc);
_fmpz_mod_poly_pow_trunc_binexp(t->coeffs, q, e, trunc, &poly->p);
fmpz_mod_poly_swap(res, t);
fmpz_mod_poly_clear(t);
}
if (qcopy)
_fmpz_vec_clear(q, trunc);
_fmpz_mod_poly_set_length(res, trunc);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,172 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2010 William Hart
Copyright (C) 2011 Fredrik Johansson
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void
_fmpz_mod_poly_powmod_fmpz_binexp(fmpz * res, const fmpz * poly,
const fmpz_t e, const fmpz * f,
slong lenf, const fmpz_t p)
{
fmpz * T, * Q;
fmpz_t invf;
slong lenT, lenQ;
slong i;
if (lenf == 2)
{
fmpz_powm(res, poly, e, p);
return;
}
lenT = 2 * lenf - 3;
lenQ = lenT - lenf + 1;
T = _fmpz_vec_init(lenT + lenQ);
Q = T + lenT;
fmpz_init(invf);
fmpz_invmod(invf, f + lenf - 1, p);
_fmpz_vec_set(res, poly, lenf - 1);
for (i = fmpz_sizeinbase(e, 2) - 2; i >= 0; i--)
{
_fmpz_mod_poly_sqr(T, res, lenf - 1, p);
_fmpz_mod_poly_divrem(Q, res, T, 2 * lenf - 3, f, lenf, invf, p);
if (fmpz_tstbit(e, i))
{
_fmpz_mod_poly_mul(T, res, lenf - 1, poly, lenf - 1, p);
_fmpz_mod_poly_divrem(Q, res, T, 2 * lenf - 3, f, lenf, invf, p);
}
}
fmpz_clear(invf);
_fmpz_vec_clear(T, lenT + lenQ);
}
void
fmpz_mod_poly_powmod_fmpz_binexp(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly, const fmpz_t e,
const fmpz_mod_poly_t f)
{
fmpz * q;
slong len = poly->length;
slong lenf = f->length;
slong trunc = lenf - 1;
int qcopy = 0;
if (lenf == 0)
{
flint_printf("Exception (fmpz_mod_poly_powmod_fmpz_binexp). Divide by zero\n");
abort();
}
if (fmpz_sgn(e) < 0)
{
flint_printf("Exception (fmpz_mod_poly_powmod_fmpz_binexp). negative exp not implemented\n");
abort();
}
if (len >= lenf)
{
fmpz_mod_poly_t t, r;
fmpz_mod_poly_init(t, &res->p);
fmpz_mod_poly_init(r, &res->p);
fmpz_mod_poly_divrem(t, r, poly, f);
fmpz_mod_poly_powmod_fmpz_binexp(res, r, e, f);
fmpz_mod_poly_clear(t);
fmpz_mod_poly_clear(r);
return;
}
if (fmpz_abs_fits_ui(e))
{
ulong exp = fmpz_get_ui(e);
if (exp <= 2)
{
if (exp == UWORD(0))
{
fmpz_mod_poly_fit_length(res, 1);
fmpz_one(res->coeffs);
_fmpz_mod_poly_set_length(res, 1);
}
else if (exp == UWORD(1))
{
fmpz_mod_poly_set(res, poly);
}
else
fmpz_mod_poly_mulmod(res, poly, poly, f);
return;
}
}
if (lenf == 1 || len == 0)
{
fmpz_mod_poly_zero(res);
return;
}
if (poly->length < trunc)
{
q = _fmpz_vec_init(trunc);
_fmpz_vec_set(q, poly->coeffs, len);
_fmpz_vec_zero(q + len, trunc - len);
qcopy = 1;
} else
q = poly->coeffs;
if ((res == poly && !qcopy) || (res == f))
{
fmpz_mod_poly_t t;
fmpz_mod_poly_init2(t, &poly->p, 2 * lenf - 3);
_fmpz_mod_poly_powmod_fmpz_binexp(t->coeffs,
q, e, f->coeffs, lenf, &poly->p);
fmpz_mod_poly_swap(res, t);
fmpz_mod_poly_clear(t);
}
else
{
fmpz_mod_poly_fit_length(res, 2 * lenf - 3);
_fmpz_mod_poly_powmod_fmpz_binexp(res->coeffs,
q, e, f->coeffs, lenf, &poly->p);
}
if (qcopy)
_fmpz_vec_clear(q, trunc);
_fmpz_mod_poly_set_length(res, trunc);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,182 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2010 William Hart
Copyright (C) 2011 Fredrik Johansson
Copyright (C) 2012 Lina Kulakova
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void
_fmpz_mod_poly_powmod_fmpz_binexp_preinv(fmpz * res, const fmpz * poly,
const fmpz_t e, const fmpz * f,
slong lenf, const fmpz* finv, slong lenfinv,
const fmpz_t p)
{
fmpz * T, * Q;
slong lenT, lenQ;
slong i;
if (lenf == 2)
{
fmpz_powm(res, poly, e, p);
return;
}
lenT = 2 * lenf - 3;
lenQ = lenT - lenf + 1;
T = _fmpz_vec_init(lenT + lenQ);
Q = T + lenT;
_fmpz_vec_set(res, poly, lenf - 1);
for (i = fmpz_sizeinbase(e, 2) - 2; i >= 0; i--)
{
_fmpz_mod_poly_sqr(T, res, lenf - 1, p);
_fmpz_mod_poly_divrem_newton_n_preinv(Q, res, T, 2 * lenf - 3, f, lenf,
finv, lenfinv, p);
if (fmpz_tstbit(e, i))
{
_fmpz_mod_poly_mul(T, res, lenf - 1, poly, lenf - 1, p);
_fmpz_mod_poly_divrem_newton_n_preinv(Q, res, T, 2 * lenf - 3, f,
lenf, finv, lenfinv, p);
}
}
_fmpz_vec_clear(T, lenT + lenQ);
}
void
fmpz_mod_poly_powmod_fmpz_binexp_preinv(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly, const fmpz_t e,
const fmpz_mod_poly_t f, const fmpz_mod_poly_t finv)
{
fmpz * q;
slong len = poly->length;
slong lenf = f->length;
slong trunc = lenf - 1;
int qcopy = 0;
if (lenf == 0)
{
flint_printf("Exception (fmpz_mod_poly_powmod_fmpz_binexp_preinv)."
"Divide by zero.\n");
abort();
}
if (fmpz_sgn(e) < 0)
{
flint_printf("Exception (fmpz_mod_poly_powmod_fmpz_binexp_preinv)."
"Negative exp not implemented\n");
abort();
}
if (len >= lenf)
{
fmpz_mod_poly_t t, r;
fmpz_mod_poly_init(t, &res->p);
fmpz_mod_poly_init(r, &res->p);
fmpz_mod_poly_divrem(t, r, poly, f);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(res, r, e, f, finv);
fmpz_mod_poly_clear(t);
fmpz_mod_poly_clear(r);
return;
}
if (fmpz_abs_fits_ui(e))
{
ulong exp = fmpz_get_ui(e);
if (exp <= 2)
{
if (exp == UWORD (0))
{
fmpz_mod_poly_fit_length(res, 1);
fmpz_one(res->coeffs);
_fmpz_mod_poly_set_length(res, 1);
}
else if (exp == UWORD (1))
{
fmpz_mod_poly_set(res, poly);
}
else
fmpz_mod_poly_mulmod_preinv(res, poly, poly, f, finv);
return;
}
}
if (lenf == 1 || len == 0)
{
fmpz_mod_poly_zero(res);
return;
}
if (poly->length < trunc)
{
q = _fmpz_vec_init(trunc);
_fmpz_vec_set(q, poly->coeffs, len);
_fmpz_vec_zero(q + len, trunc - len);
qcopy = 1;
} else
q = poly->coeffs;
if ((res == poly && !qcopy) || (res == f) || (res == finv))
{
fmpz_mod_poly_t t;
fmpz_mod_poly_init2(t, &poly->p, 2 * lenf - 3);
_fmpz_mod_poly_powmod_fmpz_binexp_preinv(t->coeffs,
q, e, f->coeffs, lenf, finv->coeffs, finv->length, &poly->p);
fmpz_mod_poly_swap(res, t);
fmpz_mod_poly_clear(t);
}
else
{
fmpz_mod_poly_fit_length(res, 2 * lenf - 3);
_fmpz_mod_poly_powmod_fmpz_binexp_preinv(res->coeffs,
q, e, f->coeffs, lenf, finv->coeffs, finv->length, &poly->p);
}
if (qcopy)
_fmpz_vec_clear(q, trunc);
_fmpz_mod_poly_set_length(res, trunc);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,161 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2010 William Hart
Copyright (C) 2011 Fredrik Johansson
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
void
_fmpz_mod_poly_powmod_ui_binexp(fmpz * res, const fmpz * poly,
ulong e, const fmpz * f, slong lenf, const fmpz_t p)
{
fmpz * T, * Q;
fmpz_t invf;
slong lenT, lenQ;
int i;
if (lenf == 2)
{
fmpz_powm_ui(res, poly, e, p);
return;
}
lenT = 2 * lenf - 3;
lenQ = FLINT_MAX(lenT - lenf + 1, 1);
T = _fmpz_vec_init(lenT + lenQ);
Q = T + lenT;
fmpz_init(invf);
fmpz_invmod(invf, f + lenf - 1, p);
_fmpz_vec_set(res, poly, lenf - 1);
for (i = ((int) FLINT_BIT_COUNT(e) - 2); i >= 0; i--)
{
_fmpz_mod_poly_sqr(T, res, lenf - 1, p);
_fmpz_mod_poly_divrem(Q, res, T, 2 * lenf - 3, f, lenf, invf, p);
if (e & (UWORD(1) << i))
{
_fmpz_mod_poly_mul(T, res, lenf - 1, poly, lenf - 1, p);
_fmpz_mod_poly_divrem(Q, res, T, 2 * lenf - 3, f, lenf, invf, p);
}
}
fmpz_clear(invf);
_fmpz_vec_clear(T, lenT + lenQ);
}
void
fmpz_mod_poly_powmod_ui_binexp(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly, ulong e,
const fmpz_mod_poly_t f)
{
fmpz * q;
slong len = poly->length;
slong lenf = f->length;
slong trunc = lenf - 1;
int qcopy = 0;
if (lenf == 0)
{
flint_printf("Exception (fmpz_mod_poly_powmod). Divide by zero\n");
abort();
}
if (len >= lenf)
{
fmpz_mod_poly_t t, r;
fmpz_mod_poly_init(t, &res->p);
fmpz_mod_poly_init(r, &res->p);
fmpz_mod_poly_divrem(t, r, poly, f);
fmpz_mod_poly_powmod_ui_binexp(res, r, e, f);
fmpz_mod_poly_clear(t);
fmpz_mod_poly_clear(r);
return;
}
if (e <= 2)
{
if (e == UWORD(0))
{
fmpz_mod_poly_fit_length(res, 1);
fmpz_one(res->coeffs);
_fmpz_mod_poly_set_length(res, 1);
}
else if (e == UWORD(1))
{
fmpz_mod_poly_set(res, poly);
}
else
fmpz_mod_poly_mulmod(res, poly, poly, f);
return;
}
if (lenf == 1 || len == 0)
{
fmpz_mod_poly_zero(res);
return;
}
if (len < trunc)
{
q = _fmpz_vec_init(trunc);
_fmpz_vec_set(q, poly->coeffs, len);
_fmpz_vec_zero(q + len, trunc - len);
qcopy = 1;
} else
q = poly->coeffs;
if ((res == poly && !qcopy) || (res == f))
{
fmpz_mod_poly_t t;
fmpz_mod_poly_init2(t, &poly->p, 2 * lenf - 3);
_fmpz_mod_poly_powmod_ui_binexp(t->coeffs,
q, e, f->coeffs, lenf, &poly->p);
fmpz_mod_poly_swap(res, t);
fmpz_mod_poly_clear(t);
}
else
{
fmpz_mod_poly_fit_length(res, 2 * lenf - 3);
_fmpz_mod_poly_powmod_ui_binexp(res->coeffs,
q, e, f->coeffs, lenf, &poly->p);
}
if (qcopy)
_fmpz_vec_clear(q, trunc);
_fmpz_mod_poly_set_length(res, trunc);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,170 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2010 William Hart
Copyright (C) 2011 Fredrik Johansson
Copyright (C) 2012 Lina Kulakova
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
void
_fmpz_mod_poly_powmod_ui_binexp_preinv(fmpz * res, const fmpz * poly,
ulong e, const fmpz * f, slong lenf,
const fmpz * finv, slong lenfinv, const fmpz_t p)
{
fmpz * T, * Q;
slong lenT, lenQ;
int i;
if (lenf == 2)
{
fmpz_powm_ui(res, poly, e, p);
return;
}
lenT = 2 * lenf - 3;
lenQ = FLINT_MAX(lenT - lenf + 1, 1);
T = _fmpz_vec_init(lenT + lenQ);
Q = T + lenT;
_fmpz_vec_set(res, poly, lenf - 1);
for (i = ((int) FLINT_BIT_COUNT(e) - 2); i >= 0; i--)
{
_fmpz_mod_poly_sqr(T, res, lenf - 1, p);
_fmpz_mod_poly_divrem_newton_n_preinv(Q, res, T, 2 * lenf - 3, f, lenf,
finv, lenfinv, p);
if (e & (UWORD (1) << i))
{
_fmpz_mod_poly_mul(T, res, lenf - 1, poly, lenf - 1, p);
_fmpz_mod_poly_divrem_newton_n_preinv(Q, res, T, 2 * lenf - 3, f,
lenf, finv, lenfinv, p);
}
}
_fmpz_vec_clear(T, lenT + lenQ);
}
void
fmpz_mod_poly_powmod_ui_binexp_preinv(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly, ulong e,
const fmpz_mod_poly_t f, const fmpz_mod_poly_t finv)
{
fmpz * q;
slong len = poly->length;
slong lenf = f->length;
slong trunc = lenf - 1;
int qcopy = 0;
if (lenf == 0)
{
flint_printf("Exception (fmpz_mod_poly_powmod_ui_binexp_preinv)."
"Divide by zero\n");
abort();
}
if (len >= lenf)
{
fmpz_mod_poly_t t, r;
fmpz_mod_poly_init(t, &res->p);
fmpz_mod_poly_init(r, &res->p);
fmpz_mod_poly_divrem(t, r, poly, f);
fmpz_mod_poly_powmod_ui_binexp_preinv(res, r, e, f, finv);
fmpz_mod_poly_clear(t);
fmpz_mod_poly_clear(r);
return;
}
if (e <= 2)
{
if (e == UWORD (0))
{
fmpz_mod_poly_fit_length(res, 1);
fmpz_one(res->coeffs);
_fmpz_mod_poly_set_length(res, 1);
}
else if (e == UWORD (1))
{
fmpz_mod_poly_set(res, poly);
}
else
fmpz_mod_poly_mulmod_preinv(res, poly, poly, f, finv);
return;
}
if (lenf == 1 || len == 0)
{
fmpz_mod_poly_zero(res);
return;
}
if (len < trunc)
{
q = _fmpz_vec_init(trunc);
_fmpz_vec_set(q, poly->coeffs, len);
_fmpz_vec_zero(q + len, trunc - len);
qcopy = 1;
} else
q = poly->coeffs;
if ((res == poly && !qcopy) || (res == f) || (res == finv))
{
fmpz_mod_poly_t t;
fmpz_mod_poly_init2(t, &poly->p, 2 * lenf - 3);
_fmpz_mod_poly_powmod_ui_binexp_preinv(t->coeffs,
q, e, f->coeffs, lenf, finv->coeffs, finv->length, &poly->p);
fmpz_mod_poly_swap(res, t);
fmpz_mod_poly_clear(t);
}
else
{
fmpz_mod_poly_fit_length(res, 2 * lenf - 3);
_fmpz_mod_poly_powmod_ui_binexp_preinv(res->coeffs,
q, e, f->coeffs, lenf, finv->coeffs, finv->length, &poly->p);
}
if (qcopy)
_fmpz_vec_clear(q, trunc);
_fmpz_mod_poly_set_length(res, trunc);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,208 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2010 William Hart
Copyright (C) 2011 Fredrik Johansson
Copyright (C) 2012 Lina Kulakova
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#include "long_extras.h"
void
_fmpz_mod_poly_powmod_x_fmpz_preinv(fmpz * res, const fmpz_t e, const fmpz * f,
slong lenf, const fmpz* finv, slong lenfinv,
const fmpz_t p)
{
fmpz * T, * Q;
slong lenT, lenQ;
slong i, window, l, c;
lenT = 2 * lenf - 3;
lenQ = lenT - lenf + 1;
T = _fmpz_vec_init(lenT + lenQ);
Q = T + lenT;
fmpz_one (res);
l = z_sizeinbase (lenf - 1, 2) - 2;
window = WORD(0);
window = (WORD(1) << l);
c = l;
i = fmpz_sizeinbase(e, 2) - 2;
if (i <= l)
{
window = WORD(0);
window = (WORD(1) << i);
c = i;
l = i;
}
if (c == 0)
{
_fmpz_mod_poly_shift_left(T, res, lenf - 1, window);
_fmpz_mod_poly_divrem_newton_n_preinv(Q, res, T, lenf - 1 + window, f,
lenf, finv, lenfinv, p);
c = l + 1;
window = WORD(0);
}
for (; i >= 0; i--)
{
_fmpz_mod_poly_sqr(T, res, lenf - 1, p);
_fmpz_mod_poly_divrem_newton_n_preinv(Q, res, T, 2 * lenf - 3, f, lenf,
finv, lenfinv, p);
c--;
if (fmpz_tstbit(e, i))
{
if (window == WORD(0) && i <= l - 1)
c = i;
if ( c >= 0)
window = window | (WORD(1) << c);
}
else if (window == WORD(0))
c = l + 1;
if (c == 0)
{
_fmpz_mod_poly_shift_left(T, res, lenf - 1, window);
_fmpz_mod_poly_divrem_newton_n_preinv(Q, res, T, lenf - 1 + window,
f, lenf, finv, lenfinv, p);
c = l + 1;
window = WORD(0);
}
}
_fmpz_vec_clear(T, lenT + lenQ);
}
void
fmpz_mod_poly_powmod_x_fmpz_preinv(fmpz_mod_poly_t res, const fmpz_t e,
const fmpz_mod_poly_t f, const fmpz_mod_poly_t finv)
{
slong lenf = f->length;
slong trunc = lenf - 1;
fmpz_mod_poly_t tmp;
if (lenf == 0)
{
flint_printf("Exception (fmpz_mod_poly_powmod_x_fmpz_preinv)."
"Divide by zero\n");
abort();
}
if (fmpz_sgn(e) < 0)
{
flint_printf("Exception (fmpz_mod_poly_powmod_x_fmpz_preinv)."
"Negative exp not implemented\n");
abort();
}
if (lenf == 1)
{
fmpz_mod_poly_zero(res);
return;
}
if (lenf == 2)
{
fmpz_mod_poly_t r, poly;
fmpz_mod_poly_init(tmp, &res->p);
fmpz_mod_poly_init(r, &res->p);
fmpz_mod_poly_init2(poly, &res->p, 2);
fmpz_mod_poly_set_coeff_ui (poly, 1, 1);
fmpz_mod_poly_divrem(tmp, r, poly, f);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(res, r, e, f, finv);
fmpz_mod_poly_clear(tmp);
fmpz_mod_poly_clear(r);
fmpz_mod_poly_clear(poly);
return;
}
if (fmpz_abs_fits_ui(e))
{
ulong exp = fmpz_get_ui(e);
if (exp <= 2)
{
if (exp == UWORD(0))
{
fmpz_mod_poly_fit_length(res, 1);
fmpz_one(res->coeffs);
_fmpz_mod_poly_set_length(res, 1);
}
else if (exp == UWORD(1))
{
fmpz_mod_poly_t r;
fmpz_mod_poly_init2(r, &f->p, 2);
fmpz_mod_poly_set_coeff_ui(r, 1, 1);
fmpz_mod_poly_init(tmp, &f->p);
fmpz_mod_poly_divrem(tmp, res, r, f);
fmpz_mod_poly_clear(tmp);
fmpz_mod_poly_clear(r);
}
else
{
fmpz_mod_poly_init2(tmp, &f->p, 3);
fmpz_mod_poly_set_coeff_ui(tmp, 1, 1);
fmpz_mod_poly_mulmod(res, tmp, tmp, f);
fmpz_mod_poly_clear(tmp);
}
return;
}
}
if ((res == f) || (res == finv))
{
fmpz_mod_poly_init2(tmp, &f->p, trunc);
_fmpz_mod_poly_powmod_x_fmpz_preinv(tmp->coeffs, e, f->coeffs, lenf,
finv->coeffs, finv->length, &f->p);
fmpz_mod_poly_swap(res, tmp);
fmpz_mod_poly_clear(tmp);
}
else
{
fmpz_mod_poly_fit_length(res, trunc);
_fmpz_mod_poly_powmod_x_fmpz_preinv(res->coeffs, e, f->coeffs, lenf,
finv->coeffs, finv->length, &f->p);
}
_fmpz_mod_poly_set_length(res, trunc);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,93 @@
/*=============================================================================
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) 2012 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <float.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
#include "profiler.h"
int
main(void)
{
slong len, iter;
fmpz_mod_poly_t f, g, q, r;
fmpz_t N, c, one;
timeit_t t;
char N_str[201] = "29799904256775982671863388319999573561548825027149399972531599612392671227006866151136667908641695103422986028076864929902803267437351318167549013218980573566942647077444419419003164546362008247462049";
FLINT_TEST_INIT(state);
len = 36865;
fmpz_init(N);
fmpz_init(c);
fmpz_init(one);
fmpz_set_str(N, N_str, 10);
fmpz_set_ui(one, 1);
fmpz_mod_poly_init2(f, N, len);
fmpz_mod_poly_init2(g, N, 2*len - 1);
fmpz_mod_poly_init2(q, N, len);
fmpz_mod_poly_init2(r, N, len - 1);
/*
Construct random polynomial f
*/
fmpz_mod_poly_randtest(f, state, len);
fmpz_mod_poly_set_coeff_fmpz(g, 2*len - 2, one);
/*
Time inversion
*/
timeit_start(t);
for (iter = 0; iter < 10; iter++)
fmpz_mod_poly_inv_series_newton(q, f, len);
timeit_stop(t);
flint_printf("len = %wd, time = %wdms\n", len, ((slong) t->cpu)/10);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(g);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(N);
fmpz_clear(c);
fmpz_clear(one);
flint_randclear(state);
}

View File

@@ -0,0 +1,85 @@
/*=============================================================================
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) 2012 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <float.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
#include "profiler.h"
int
main(void)
{
slong len, iter;
fmpz_mod_poly_t f, g, r;
fmpz_t N;
timeit_t t;
char N_str[201] = "29799904256775982671863388319999573561548825027149399972531599612392671227006866151136667908641695103422986028076864929902803267437351318167549013218980573566942647077444419419003164546362008247462049";
FLINT_TEST_INIT(state);
len = 36865;
fmpz_init(N);
fmpz_set_str(N, N_str, 10);
fmpz_mod_poly_init2(f, N, len);
fmpz_mod_poly_init2(g, N, len);
fmpz_mod_poly_init2(r, N, 2*len - 1);
/*
Construct random polynomials f, g
*/
fmpz_mod_poly_randtest(f, state, len);
fmpz_mod_poly_randtest(g, state, len);
/*
Time multiplication
*/
timeit_start(t);
for (iter = 0; iter < 10; iter++)
fmpz_mod_poly_mul(r, f, g);
timeit_stop(t);
flint_printf("len = %wd, time = %wdms\n", len, ((slong) t->cpu)/10);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(g);
fmpz_mod_poly_clear(r);
fmpz_clear(N);
flint_randclear(state);
}

View File

@@ -0,0 +1,86 @@
/*=============================================================================
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) 2012 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include <float.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
#include "profiler.h"
int
main(void)
{
slong len, iter, i;
fmpz_mod_poly_t f;
fmpz * roots;
fmpz_poly_struct ** tree;
fmpz_t N;
timeit_t t;
char N_str[201] = "29799904256775982671863388319999573561548825027149399972531599612392671227006866151136667908641695103422986028076864929902803267437351318167549013218980573566942647077444419419003164546362008247462049";
FLINT_TEST_INIT(state);
len = 36865;
fmpz_init(N);
fmpz_set_str(N, N_str, 10);
roots = _fmpz_vec_init(len);
for (i = 0; i < len; i++)
fmpz_randm(roots + i, state, N);
fmpz_mod_poly_init(f, N);
/*
Time tree
*/
timeit_start(t);
for (iter = 0; iter < 10; iter++)
{
tree = _fmpz_mod_poly_tree_alloc(len);
_fmpz_mod_poly_tree_build(tree, roots, len, N);
_fmpz_mod_poly_tree_free(tree, len);
}
timeit_stop(t);
flint_printf("len = %wd, time = %wdms\n", len, ((slong) t->cpu)/10);
fmpz_mod_poly_clear(f);
_fmpz_vec_clear(roots, len);
fmpz_clear(N);
flint_randclear(state);
}

View File

@@ -0,0 +1,235 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_radix_init(fmpz **Rpow, fmpz **Rinv,
const fmpz *R, slong lenR, slong k,
const fmpz_t invL, const fmpz_t p)
{
const slong degR = lenR - 1;
slong i;
fmpz_t invLP;
fmpz *W;
fmpz_init_set(invLP, invL);
W = flint_malloc((WORD(1) << (k - 1)) * degR * sizeof(fmpz));
_fmpz_vec_set(Rpow[0], R, lenR);
for (i = 1; i < k; i++)
{
_fmpz_mod_poly_sqr(Rpow[i], Rpow[i - 1], degR * (WORD(1) << (i - 1)) + 1, p);
}
for (i = 0; i < k; i++)
{
const slong lenQ = (WORD(1) << i) * degR;
slong j;
/* W := rev{Rpow[i], lenQ} */
for (j = 0; j < lenQ; j++)
{
W[j] = Rpow[i][lenQ - j];
}
_fmpz_mod_poly_inv_series_newton(Rinv[i], W, lenQ, invLP, p);
/* invLP := inv{lead{R^{2^i}}} */
if (i != k - 1)
{
fmpz_mul(invLP, invLP, invLP);
fmpz_mod(invLP, invLP, p);
}
}
fmpz_clear(invLP);
flint_free(W);
}
void fmpz_mod_poly_radix_init(fmpz_mod_poly_radix_t D,
const fmpz_mod_poly_t R, slong degF)
{
const slong degR = R->length - 1;
if (degF < degR)
{
D->k = 0;
D->degR = degR;
}
else
{
const slong N = degF / degR;
const slong k = FLINT_BIT_COUNT(N); /* k := ceil{log{N+1}} */
const slong lenV = degR * ((WORD(1) << k) - 1) + k;
const slong lenW = degR * ((WORD(1) << k) - 1);
slong i;
D->V = _fmpz_vec_init(lenV + lenW);
D->W = D->V + lenV;
D->Rpow = flint_malloc(k * sizeof(fmpz *));
D->Rinv = flint_malloc(k * sizeof(fmpz *));
for (i = 0; i < k; i++)
{
D->Rpow[i] = D->V + (degR * ((WORD(1) << i) - 1) + i);
D->Rinv[i] = D->W + (degR * ((WORD(1) << i) - 1));
}
fmpz_init(&(D->invL));
fmpz_invmod(&(D->invL), R->coeffs + degR, &(R->p));
_fmpz_mod_poly_radix_init(D->Rpow, D->Rinv, R->coeffs, degR + 1,
k, &(D->invL), &(R->p));
D->k = k;
D->degR = degR;
}
}
void fmpz_mod_poly_radix_clear(fmpz_mod_poly_radix_t D)
{
if (D->k)
{
const slong degR = D->degR;
const slong k = D->k;
const slong lenV = degR * ((WORD(1) << k) - 1) + k;
const slong lenW = degR * ((WORD(1) << k) - 1);
_fmpz_vec_clear(D->V, lenV + lenW);
flint_free(D->Rpow);
flint_free(D->Rinv);
fmpz_clear(&(D->invL));
}
}
void _fmpz_mod_poly_radix(fmpz **B, const fmpz *F, fmpz **Rpow, fmpz **Rinv,
slong degR, slong k, slong i, fmpz *W, const fmpz_t p)
{
if (i == -1)
{
_fmpz_vec_set(B[k], F, degR);
}
else
{
const slong lenQ = (WORD(1) << i) * degR;
fmpz *Frev = W;
fmpz *Q = W + lenQ;
fmpz *S = W;
_fmpz_poly_reverse(Frev, F + lenQ, lenQ, lenQ);
_fmpz_mod_poly_mullow(Q, Frev, lenQ, Rinv[i], lenQ, p, lenQ);
_fmpz_poly_reverse(Q, Q, lenQ, lenQ);
_fmpz_mod_poly_radix(B, Q, Rpow, Rinv, degR, k + (WORD(1) << i), i-1, W, p);
_fmpz_mod_poly_mullow(S, Rpow[i], lenQ, Q, lenQ, p, lenQ);
_fmpz_mod_poly_sub(S, F, lenQ, S, lenQ, p);
_fmpz_mod_poly_radix(B, S, Rpow, Rinv, degR, k, i-1, W + lenQ, p);
}
}
void fmpz_mod_poly_radix(fmpz_mod_poly_struct **B,
const fmpz_mod_poly_t F,
const fmpz_mod_poly_radix_t D)
{
const slong lenF = F->length;
const slong degF = F->length - 1;
const slong degR = D->degR;
const slong N = degF / degR;
if (N == 0)
{
fmpz_mod_poly_set(B[0], F);
}
else
{
const slong k = FLINT_BIT_COUNT(N); /* k := ceil{log{N+1}} */
const slong lenG = (WORD(1) << k) * degR; /* Padded size */
const slong t = (lenG - 1) / degR - N; /* Extra {degR}-blocks */
fmpz *G; /* Padded copy of F */
fmpz *T; /* Additional B[i] */
fmpz **C; /* Enlarged version of B */
fmpz *W; /* Temporary space */
slong i;
if (lenF < lenG)
{
G = flint_malloc(lenG * sizeof(fmpz));
for (i = 0; i < lenF; i++)
G[i] = F->coeffs[i];
flint_mpn_zero((mp_ptr) G + lenF, lenG - lenF);
T = t ? _fmpz_vec_init(t * degR) : NULL;
}
else /* lenF == lenG */
{
G = F->coeffs;
T = NULL;
}
C = flint_malloc((N + 1 + t) * sizeof(fmpz *));
for (i = 0; i <= N; i++)
{
fmpz_mod_poly_fit_length(B[i], degR);
C[i] = B[i]->coeffs;
}
for (i = 0; i < t; i++)
{
C[N + 1 + i] = T + i * degR;
}
W = _fmpz_vec_init(lenG);
_fmpz_mod_poly_radix(C, G, D->Rpow, D->Rinv, degR, 0, k-1, W, &(F->p));
_fmpz_vec_clear(W, lenG);
for (i = 0; i <= N; i++)
{
_fmpz_mod_poly_set_length(B[i], degR);
_fmpz_mod_poly_normalise(B[i]);
}
flint_free(C);
if (lenF < lenG)
{
flint_free(G);
}
if (t)
{
_fmpz_vec_clear(T, t * degR);
}
}
}

View File

@@ -0,0 +1,198 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2009 William Hart
Copyright (C) 2013 Mike Hansen
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void
fmpz_mod_poly_randtest(fmpz_mod_poly_t f, flint_rand_t state, slong len)
{
slong i;
fmpz_mod_poly_fit_length(f, len);
for (i = 0; i < len; i++)
fmpz_randm(f->coeffs + i, state, &(f->p));
_fmpz_mod_poly_set_length(f, len);
_fmpz_mod_poly_normalise(f);
}
void
fmpz_mod_poly_randtest_monic(fmpz_mod_poly_t f, flint_rand_t state, slong len)
{
slong i;
fmpz_mod_poly_fit_length(f, len);
for (i = 0; i < len - 1; i++)
fmpz_randm(f->coeffs + i, state, &(f->p));
fmpz_one(f->coeffs + len - 1);
_fmpz_mod_poly_set_length(f, len);
}
void
fmpz_mod_poly_randtest_irreducible(fmpz_mod_poly_t f,
flint_rand_t state, slong len)
{
if (len == 0)
{
flint_printf("Exception (fmpz_mod_poly_randtest_irreducible). len == 0.\n");
abort();
}
fmpz_mod_poly_randtest(f, state, len);
while (fmpz_mod_poly_is_zero(f) || !fmpz_mod_poly_is_irreducible(f))
fmpz_mod_poly_randtest(f, state, len);
}
void
fmpz_mod_poly_randtest_monic_irreducible(fmpz_mod_poly_t f,
flint_rand_t state, slong len)
{
if (len == 0)
{
flint_printf("Exception (fmpz_mod_poly_randtest_monic_irreducible). len == 0.\n");
abort();
}
fmpz_mod_poly_randtest_monic(f, state, len);
while (fmpz_mod_poly_is_zero(f) || !fmpz_mod_poly_is_irreducible(f))
fmpz_mod_poly_randtest_monic(f, state, len);
}
void
fmpz_mod_poly_randtest_not_zero(fmpz_mod_poly_t f,
flint_rand_t state, slong len)
{
if (len == 0)
{
flint_printf("Exception (fmpz_mod_poly_randtest_not_zero). len == 0.\n");
abort();
}
fmpz_mod_poly_randtest(f, state, len);
while (fmpz_mod_poly_is_zero(f))
fmpz_mod_poly_randtest(f, state, len);
}
void
fmpz_mod_poly_randtest_trinomial(fmpz_mod_poly_t poly, flint_rand_t state, slong len)
{
ulong k;
fmpz_mod_poly_fit_length(poly, len);
_fmpz_vec_zero(poly->coeffs, len);
fmpz_randm(poly->coeffs, state, &(poly->p));
k = (n_randtest(state) % (len - 2)) + 1;
fmpz_randm(poly->coeffs + k, state, &(poly->p));
fmpz_one(poly->coeffs + len - 1);
_fmpz_mod_poly_set_length(poly, len);
}
void
fmpz_mod_poly_randtest_pentomial(fmpz_mod_poly_t poly, flint_rand_t state, slong len)
{
fmpz_mod_poly_fit_length(poly, len);
_fmpz_vec_zero(poly->coeffs, len);
fmpz_randm(poly->coeffs, state, &(poly->p));
fmpz_randm(poly->coeffs + 1, state, &(poly->p));
fmpz_randm(poly->coeffs + 2, state, &(poly->p));
fmpz_randm(poly->coeffs + 3, state, &(poly->p));
fmpz_one(poly->coeffs + len - 1);
_fmpz_mod_poly_set_length(poly, len);
}
int
fmpz_mod_poly_randtest_trinomial_irreducible(fmpz_mod_poly_t poly, flint_rand_t state,
slong len, slong max_attempts)
{
slong i = 0;
while (max_attempts == 0 || i < max_attempts)
{
fmpz_mod_poly_randtest_trinomial(poly, state, len);
if (!fmpz_mod_poly_is_zero(poly) && fmpz_mod_poly_is_irreducible(poly))
{
return 1;
}
i++;
}
return 0;
}
int
fmpz_mod_poly_randtest_pentomial_irreducible(fmpz_mod_poly_t poly, flint_rand_t state,
slong len, slong max_attempts)
{
slong i = 0;
while (max_attempts == 0 || i < max_attempts)
{
fmpz_mod_poly_randtest_pentomial(poly, state, len);
if (!fmpz_mod_poly_is_zero(poly) && fmpz_mod_poly_is_irreducible(poly))
{
return 1;
}
i++;
}
return 0;
}
void
fmpz_mod_poly_randtest_sparse_irreducible(fmpz_mod_poly_t poly, flint_rand_t state, slong len)
{
if (len < 3)
{
fmpz_mod_poly_randtest_monic_irreducible(poly, state, len);
return;
}
/* Try trinomials */
if (fmpz_mod_poly_randtest_trinomial_irreducible(poly, state, len, 2*len))
return;
if (len < 5)
{
fmpz_mod_poly_randtest_monic_irreducible(poly, state, len);
return;
}
/* Try pentomials */
if (fmpz_mod_poly_randtest_pentomial_irreducible(poly, state, len, 2*len))
return;
/* Give up */
fmpz_mod_poly_randtest_monic_irreducible(poly, state, len);
}

View File

@@ -0,0 +1,57 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_realloc(fmpz_mod_poly_t poly, slong alloc)
{
if (alloc == 0) /* Clear up, reinitialise */
{
if (poly->coeffs)
_fmpz_vec_clear(poly->coeffs, poly->alloc);
poly->coeffs = NULL;
poly->length = 0;
poly->alloc = 0;
return;
}
if (poly->alloc) /* Realloc */
{
fmpz_mod_poly_truncate(poly, alloc);
poly->coeffs = (fmpz *) flint_realloc(poly->coeffs, alloc * sizeof(fmpz));
if (alloc > poly->alloc)
flint_mpn_zero((mp_ptr) (poly->coeffs + poly->alloc),
alloc - poly->alloc);
}
else /* Nothing allocated already so do it now */
{
poly->coeffs = (fmpz *) flint_calloc(alloc, sizeof(fmpz));
}
poly->alloc = alloc;
}

View File

@@ -0,0 +1,98 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <stdlib.h>
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_rem_basecase(fmpz *R,
const fmpz *A, slong lenA, const fmpz *B, slong lenB,
const fmpz_t invB, const fmpz_t p)
{
fmpz_t q;
slong iR;
fmpz_init(q);
if (R != A)
_fmpz_vec_set(R, A, lenA);
for (iR = lenA - 1; iR >= lenB - 1; iR--)
{
if (!fmpz_is_zero(R + iR))
{
fmpz_mul(q, R + iR, invB);
fmpz_mod(q, q, p);
_fmpz_vec_scalar_submul_fmpz(R + (iR - lenB + 1), B, lenB, q);
_fmpz_vec_scalar_mod_fmpz(R + (iR - lenB + 1), R + (iR - lenB + 1), lenB, p);
}
}
fmpz_clear(q);
}
void fmpz_mod_poly_rem_basecase(fmpz_mod_poly_t R,
const fmpz_mod_poly_t A, const fmpz_mod_poly_t B)
{
const slong lenA = A->length, lenB = B->length;
fmpz *r;
fmpz_t invB;
if (lenA < lenB)
{
fmpz_mod_poly_set(R, A);
return;
}
fmpz_init(invB);
fmpz_invmod(invB, B->coeffs + (lenB - 1), &(B->p));
if (R == B)
{
r = _fmpz_vec_init(lenA);
}
else
{
fmpz_mod_poly_fit_length(R, lenA);
r = R->coeffs;
}
_fmpz_mod_poly_rem_basecase(r, A->coeffs, lenA,
B->coeffs, lenB, invB, &(B->p));
if (R == B)
{
_fmpz_vec_clear(R->coeffs, R->alloc);
R->coeffs = r;
R->alloc = lenA;
R->length = lenA;
}
_fmpz_mod_poly_set_length(R, lenB - 1);
_fmpz_mod_poly_normalise(R);
fmpz_clear(invB);
}

View File

@@ -0,0 +1,55 @@
/*=============================================================================
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) 2007 David Howden
Copyright (C) 2007, 2008, 2009, 2010 William Hart
Copyright (C) 2008 Richard Howell-Peak
Copyright (C) 2011 Fredrik Johansson
******************************************************************************/
#include "fmpz_mod_poly.h"
ulong fmpz_mod_poly_remove(fmpz_mod_poly_t f, const fmpz_mod_poly_t g)
{
fmpz_mod_poly_t q, r;
ulong i = 0;
fmpz_mod_poly_init(q, &g->p);
fmpz_mod_poly_init(r, &g->p);
while (1)
{
if (f->length < g->length)
break;
fmpz_mod_poly_divrem(q, r, f, g);
if (r->length == 0)
fmpz_mod_poly_swap(q, f);
else
break;
i++;
}
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
return i;
}

View File

@@ -0,0 +1,76 @@
/*=============================================================================
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) 2013 Mike Hansen
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void
_fmpz_mod_poly_reverse(fmpz * res, const fmpz * poly, slong len, slong n)
{
if (res == poly)
{
slong i;
for (i = 0; i < n / 2; i++)
{
fmpz t = res[i];
res[i] = res[n - 1 - i];
res[n - 1 - i] = t;
}
for (i = 0; i < n - len; i++)
fmpz_zero(res + i);
}
else
{
slong i;
for (i = 0; i < n - len; i++)
fmpz_zero(res + i);
for (i = 0; i < len; i++)
fmpz_set(res + (n - len) + i, poly + (len - 1) - i);
}
}
void
fmpz_mod_poly_reverse(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly, slong n)
{
slong len = FLINT_MIN(n, poly->length);
if (len == 0)
{
fmpz_mod_poly_zero(res);
return;
}
fmpz_mod_poly_fit_length(res, n);
_fmpz_mod_poly_reverse(res->coeffs, poly->coeffs, len, n);
_fmpz_mod_poly_set_length(res, n);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,49 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_scalar_mul_fmpz(fmpz *res, const fmpz *poly, slong len,
const fmpz_t x, const fmpz_t p)
{
_fmpz_vec_scalar_mul_fmpz(res, poly, len, x);
_fmpz_vec_scalar_mod_fmpz(res, res, len, p);
}
void fmpz_mod_poly_scalar_mul_fmpz(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly, const fmpz_t x)
{
fmpz_mod_poly_fit_length(res, poly->length);
_fmpz_mod_poly_scalar_mul_fmpz(res->coeffs,
poly->coeffs, poly->length, x, &(poly->p));
_fmpz_mod_poly_set_length(res, poly->length);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,45 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_set(fmpz_mod_poly_t poly1, const fmpz_mod_poly_t poly2)
{
if (poly1 != poly2) /* Aliasing is trivial */
{
slong i, len = poly2->length;
fmpz_mod_poly_fit_length(poly1, len);
for (i = 0; i < len; i++)
fmpz_set(poly1->coeffs + i, poly2->coeffs + i);
_fmpz_mod_poly_set_length(poly1, len);
}
}

View File

@@ -0,0 +1,47 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <gmp.h>
#include <stdlib.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void
fmpz_mod_poly_set_coeff_fmpz(fmpz_mod_poly_t poly, slong n, const fmpz_t x)
{
fmpz_mod_poly_fit_length(poly, n + 1);
if (n + 1 > poly->length)
{
flint_mpn_zero((mp_ptr) (poly->coeffs + poly->length), n - poly->length);
poly->length = n + 1;
}
fmpz_mod(poly->coeffs + n, x, &(poly->p));
_fmpz_mod_poly_normalise(poly);
}

View File

@@ -0,0 +1,47 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <gmp.h>
#include <stdlib.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_set_coeff_ui(fmpz_mod_poly_t poly, slong n, ulong x)
{
fmpz_mod_poly_fit_length(poly, n + 1);
if (n + 1 > poly->length)
{
flint_mpn_zero((mp_ptr) (poly->coeffs + poly->length), n - poly->length);
poly->length = n + 1;
}
fmpz_set_ui(poly->coeffs + n, x);
fmpz_mod(poly->coeffs + n, poly->coeffs + n, &(poly->p));
_fmpz_mod_poly_normalise(poly);
}

View File

@@ -0,0 +1,38 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_set_fmpz(fmpz_mod_poly_t poly, const fmpz_t c)
{
fmpz_mod_poly_fit_length(poly, 1);
fmpz_mod(poly->coeffs, c, &(poly->p));
_fmpz_mod_poly_set_length(poly, 1);
_fmpz_mod_poly_normalise(poly);
}

View File

@@ -0,0 +1,45 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_set_fmpz_poly(fmpz_mod_poly_t f, const fmpz_poly_t g)
{
slong i;
fmpz_mod_poly_fit_length(f, g->length);
_fmpz_mod_poly_set_length(f, g->length);
for (i = 0; i < g->length; i++)
{
fmpz_mod(f->coeffs + i, g->coeffs + i, &(f->p));
}
_fmpz_mod_poly_normalise(f);
}

View File

@@ -0,0 +1,72 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2009, 2008 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void
_fmpz_mod_poly_shift_left(fmpz * res, const fmpz * poly, slong len, slong n)
{
slong i;
/* Copy in reverse to avoid writing over unshifted coefficients */
if (res != poly)
{
for (i = len; i--; )
fmpz_set(res + n + i, poly + i);
}
else
{
for (i = len; i--; )
fmpz_swap(res + n + i, res + i);
}
for (i = 0; i < n; i++)
fmpz_zero(res + i);
}
void
fmpz_mod_poly_shift_left(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly, slong n)
{
if (n == 0)
{
fmpz_mod_poly_set(res, poly);
return;
}
if (poly->length == 0)
{
fmpz_mod_poly_zero(res);
return;
}
fmpz_mod_poly_fit_length(res, poly->length + n);
_fmpz_mod_poly_shift_left(res->coeffs, poly->coeffs, poly->length, n);
_fmpz_mod_poly_set_length(res, poly->length + n);
}

View File

@@ -0,0 +1,69 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2009, 2008 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void
_fmpz_mod_poly_shift_right(fmpz * res, const fmpz * poly, slong len, slong n)
{
slong i;
/* Copy in forward order to avoid writing over unshifted coefficients */
if (res != poly)
{
for (i = 0; i < len - n; i++)
fmpz_set(res + i, poly + n + i);
}
else
{
for (i = 0; i < len - n; i++)
fmpz_swap(res + i, res + n + i);
}
}
void
fmpz_mod_poly_shift_right(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly, slong n)
{
if (n == 0)
{
fmpz_mod_poly_set(res, poly);
return;
}
if (poly->length <= n)
{
fmpz_mod_poly_zero(res);
return;
}
fmpz_mod_poly_fit_length(res, poly->length - n);
_fmpz_mod_poly_shift_right(res->coeffs, poly->coeffs, poly->length, n);
_fmpz_mod_poly_set_length(res, poly->length - n);
}

View File

@@ -0,0 +1,71 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2010 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_sqr(fmpz *res, const fmpz *poly, slong len, const fmpz_t p)
{
_fmpz_poly_sqr(res, poly, len);
_fmpz_vec_scalar_mod_fmpz(res, res, 2 * len - 1, p);
}
void fmpz_mod_poly_sqr(fmpz_mod_poly_t res, const fmpz_mod_poly_t poly)
{
const slong len = poly->length;
if (len == 0)
{
fmpz_mod_poly_zero(res);
return;
}
if (res == poly)
{
fmpz *t = flint_calloc(2 * len - 1, sizeof(fmpz));
_fmpz_mod_poly_sqr(t, poly->coeffs, len, &(res->p));
_fmpz_vec_clear(res->coeffs, res->alloc);
res->alloc = 2 * len - 1;
res->length = 2 * len - 1;
res->coeffs = t;
_fmpz_mod_poly_normalise(res);
}
else
{
fmpz_mod_poly_fit_length(res, 2 * len - 1);
_fmpz_mod_poly_sqr(res->coeffs, poly->coeffs, len, &(res->p));
_fmpz_mod_poly_set_length(res, 2 * len - 1);
_fmpz_mod_poly_normalise(res);
}
}

View File

@@ -0,0 +1,60 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
void _fmpz_mod_poly_sub(fmpz *res, const fmpz *poly1, slong len1,
const fmpz *poly2, slong len2, const fmpz_t p)
{
slong i, len = FLINT_MAX(len1, len2);
_fmpz_poly_sub(res, poly1, len1, poly2, len2);
for (i = 0; i < len; i++)
{
if (fmpz_sgn(res + i) < 0)
fmpz_add(res + i, res + i, p);
}
}
void fmpz_mod_poly_sub(fmpz_mod_poly_t res,
const fmpz_mod_poly_t poly1, const fmpz_mod_poly_t poly2)
{
slong max = FLINT_MAX(poly1->length, poly2->length);
fmpz_mod_poly_fit_length(res, max);
_fmpz_mod_poly_sub(res->coeffs, poly1->coeffs, poly1->length,
poly2->coeffs, poly2->length, &(res->p));
_fmpz_mod_poly_set_length(res, max);
_fmpz_mod_poly_normalise(res);
}

View File

@@ -0,0 +1,52 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2008, 2009 William Hart
******************************************************************************/
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
void fmpz_mod_poly_swap(fmpz_mod_poly_t poly1, fmpz_mod_poly_t poly2)
{
if (poly1 != poly2)
{
slong t;
fmpz *c;
t = poly1->length;
poly1->length = poly2->length;
poly2->length = t;
t = poly1->alloc;
poly1->alloc = poly2->alloc;
poly2->alloc = t;
c = poly1->coeffs;
poly1->coeffs = poly2->coeffs;
poly2->coeffs = c;
}
}

View File

@@ -0,0 +1,121 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("add....");
fflush(stdout);
/* Check aliasing of a and c */
for (i = 0; i < 10000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
fmpz_mod_poly_add(c, a, b);
fmpz_mod_poly_add(a, a, b);
result = (fmpz_mod_poly_equal(a, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Check aliasing of b and c */
for (i = 0; i < 10000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
fmpz_mod_poly_add(c, a, b);
fmpz_mod_poly_add(b, a, b);
result = (fmpz_mod_poly_equal(b, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,170 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("compose_divconquer....");
fflush(stdout);
/* Check aliasing of a and c */
for (i = 0; i < 100; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 80));
fmpz_mod_poly_randtest(b, state, n_randint(state, 30));
fmpz_mod_poly_compose_divconquer(c, a, b);
fmpz_mod_poly_compose_divconquer(a, a, b);
result = (fmpz_mod_poly_equal(a, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Check aliasing of b and c */
for (i = 0; i < 100; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 80));
fmpz_mod_poly_randtest(b, state, n_randint(state, 30));
fmpz_mod_poly_compose_divconquer(c, a, b);
fmpz_mod_poly_compose_divconquer(b, a, b);
result = (fmpz_mod_poly_equal(b, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Compare with composition over Z[X] */
for (i = 0; i < 100; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c, d;
fmpz_poly_t A, B, C;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 80));
fmpz_mod_poly_randtest(b, state, n_randint(state, 30));
fmpz_poly_init(A);
fmpz_poly_init(B);
fmpz_poly_init(C);
fmpz_mod_poly_get_fmpz_poly(A, a);
fmpz_mod_poly_get_fmpz_poly(B, b);
fmpz_mod_poly_compose_divconquer(c, a, b);
fmpz_poly_compose(C, A, B);
fmpz_mod_poly_set_fmpz_poly(d, C);
result = (fmpz_mod_poly_equal(c, d));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
fmpz_mod_poly_print(d), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
fmpz_poly_clear(A);
fmpz_poly_clear(B);
fmpz_poly_clear(C);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,170 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("compose_horner....");
fflush(stdout);
/* Check aliasing of a and c */
for (i = 0; i < 100; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 80));
fmpz_mod_poly_randtest(b, state, n_randint(state, 30));
fmpz_mod_poly_compose_horner(c, a, b);
fmpz_mod_poly_compose_horner(a, a, b);
result = (fmpz_mod_poly_equal(a, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Check aliasing of b and c */
for (i = 0; i < 100; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 80));
fmpz_mod_poly_randtest(b, state, n_randint(state, 30));
fmpz_mod_poly_compose_horner(c, a, b);
fmpz_mod_poly_compose_horner(b, a, b);
result = (fmpz_mod_poly_equal(b, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Compare with composition over Z[X] */
for (i = 0; i < 100; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c, d;
fmpz_poly_t A, B, C;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 80));
fmpz_mod_poly_randtest(b, state, n_randint(state, 30));
fmpz_poly_init(A);
fmpz_poly_init(B);
fmpz_poly_init(C);
fmpz_mod_poly_get_fmpz_poly(A, a);
fmpz_mod_poly_get_fmpz_poly(B, b);
fmpz_mod_poly_compose_horner(c, a, b);
fmpz_poly_compose(C, A, B);
fmpz_mod_poly_set_fmpz_poly(d, C);
result = (fmpz_mod_poly_equal(c, d));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
fmpz_mod_poly_print(d), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
fmpz_poly_clear(A);
fmpz_poly_clear(B);
fmpz_poly_clear(C);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,202 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i;
FLINT_TEST_INIT(state);
flint_printf("compose_mod....");
fflush(stdout);
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d, e;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_init(e, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_compose_mod(d, a, b, c);
fmpz_mod_poly_compose(e, a, b);
fmpz_mod_poly_rem(e, e, c);
if (!fmpz_mod_poly_equal(d, e))
{
flint_printf("FAIL (composition):\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
flint_printf("e:\n"); fmpz_mod_poly_print(e); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
fmpz_mod_poly_clear(e);
}
/* Test aliasing of res and a */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_compose_mod(d, a, b, c);
fmpz_mod_poly_compose_mod(a, a, b, c);
if (!fmpz_mod_poly_equal(d, a))
{
flint_printf("FAIL (aliasing a):\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and b */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_compose_mod(d, a, b, c);
fmpz_mod_poly_compose_mod(b, a, b, c);
if (!fmpz_mod_poly_equal(d, b))
{
flint_printf("FAIL (aliasing b)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and c */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_compose_mod(d, a, b, c);
fmpz_mod_poly_compose_mod(c, a, b, c);
if (!fmpz_mod_poly_equal(d, c))
{
flint_printf("FAIL (aliasing c)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,206 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i;
FLINT_TEST_INIT(state);
flint_printf("compose_mod_brent_kung....");
fflush(stdout);
for (i = 0; i < 2000; i++)
{
fmpz_mod_poly_t a, b, c, d, e;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_init(e, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung(d, a, b, c);
fmpz_mod_poly_compose(e, a, b);
fmpz_mod_poly_rem(e, e, c);
if (!fmpz_mod_poly_equal(d, e))
{
flint_printf("FAIL (composition):\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
flint_printf("e:\n"); fmpz_mod_poly_print(e); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
fmpz_mod_poly_clear(e);
}
/* Test aliasing of res and a */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung(d, a, b, c);
fmpz_mod_poly_compose_mod_brent_kung(a, a, b, c);
if (!fmpz_mod_poly_equal(d, a))
{
flint_printf("FAIL (aliasing a):\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and b */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung(d, a, b, c);
fmpz_mod_poly_compose_mod_brent_kung(b, a, b, c);
if (!fmpz_mod_poly_equal(d, b))
{
flint_printf("FAIL (aliasing b)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and c */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung(d, a, b, c);
fmpz_mod_poly_compose_mod_brent_kung(c, a, b, c);
if (!fmpz_mod_poly_equal(d, c))
{
flint_printf("FAIL (aliasing c)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,253 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#include <stdio.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i;
FLINT_TEST_INIT(state);
flint_printf("compose_mod_brent_kung_precomp_preinv....");
fflush(stdout);
/* no aliasing */
for (i = 0; i < 2000; i++)
{
fmpz_mod_poly_t a, b, c, cinv, d, e;
fmpz_t p;
fmpz_mat_t B;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(cinv, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_init(e, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_reverse (cinv, c, c->length);
fmpz_mod_poly_inv_series_newton (cinv, cinv, c->length);
fmpz_mat_init (B, n_sqrt (c->length-1)+1, c->length-1);
fmpz_mod_poly_precompute_matrix (B, b, c, cinv);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(d, a, B, c, cinv);
fmpz_mod_poly_compose(e, a, b);
fmpz_mod_poly_rem(e, e, c);
if (!fmpz_mod_poly_equal(d, e))
{
flint_printf("FAIL (composition):\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
flint_printf("e:\n"); fmpz_mod_poly_print(e); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mat_clear (B);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(cinv);
fmpz_mod_poly_clear(d);
fmpz_mod_poly_clear(e);
}
/* Test aliasing of res and a */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, cinv, d;
fmpz_t p;
fmpz_mat_t B;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(cinv, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_reverse (cinv, c, c->length);
fmpz_mod_poly_inv_series_newton (cinv, cinv, c->length);
fmpz_mat_init (B, n_sqrt (c->length-1)+1, c->length-1);
fmpz_mod_poly_precompute_matrix (B, b, c, cinv);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(d, a, B, c, cinv);
fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(a, a, B, c, cinv);
if (!fmpz_mod_poly_equal(d, a))
{
flint_printf("FAIL (aliasing a):\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mat_clear (B);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(cinv);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and c */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, cinv, d;
fmpz_t p;
fmpz_mat_t B;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(cinv, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_reverse (cinv, c, c->length);
fmpz_mod_poly_inv_series_newton (cinv, cinv, c->length);
fmpz_mat_init (B, n_sqrt (c->length-1)+1, c->length-1);
fmpz_mod_poly_precompute_matrix (B, b, c, cinv);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(d, a, B, c, cinv);
fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(c, a, B, c, cinv);
if (!fmpz_mod_poly_equal(d, c))
{
flint_printf("FAIL (aliasing c)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mat_clear (B);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(cinv);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and cinv */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, cinv, d;
fmpz_t p;
fmpz_mat_t B;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(cinv, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_reverse (cinv, c, c->length);
fmpz_mod_poly_inv_series_newton (cinv, cinv, c->length);
fmpz_mat_init (B, n_sqrt (c->length-1)+1, c->length-1);
fmpz_mod_poly_precompute_matrix (B, b, c, cinv);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(d, a, B, c, cinv);
fmpz_mod_poly_compose_mod_brent_kung_precomp_preinv(cinv, a, B, c, cinv);
if (!fmpz_mod_poly_equal(d, cinv))
{
flint_printf("FAIL (aliasing c)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mat_clear (B);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(cinv);
fmpz_mod_poly_clear(d);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,281 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#include <stdio.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i;
FLINT_TEST_INIT(state);
flint_printf("compose_mod_brent_kung_preinv....");
fflush(stdout);
/* no aliasing */
for (i = 0; i < 2000; i++)
{
fmpz_mod_poly_t a, b, c, cinv, d, e;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(cinv, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_init(e, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_reverse (cinv, c, c->length);
fmpz_mod_poly_inv_series_newton (cinv, cinv, c->length);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung_preinv(d, a, b, c, cinv);
fmpz_mod_poly_compose(e, a, b);
fmpz_mod_poly_rem(e, e, c);
if (!fmpz_mod_poly_equal(d, e))
{
flint_printf("FAIL (composition):\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
flint_printf("e:\n"); fmpz_mod_poly_print(e); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(cinv);
fmpz_mod_poly_clear(d);
fmpz_mod_poly_clear(e);
}
/* Test aliasing of res and a */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, cinv, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(cinv, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_reverse (cinv, c, c->length);
fmpz_mod_poly_inv_series_newton (cinv, cinv, c->length);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung_preinv(d, a, b, c, cinv);
fmpz_mod_poly_compose_mod_brent_kung_preinv(a, a, b, c, cinv);
if (!fmpz_mod_poly_equal(d, a))
{
flint_printf("FAIL (aliasing a):\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(cinv);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and b */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, cinv, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(cinv, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_reverse (cinv, c, c->length);
fmpz_mod_poly_inv_series_newton (cinv, cinv, c->length);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung_preinv(d, a, b, c, cinv);
fmpz_mod_poly_compose_mod_brent_kung_preinv(b, a, b, c, cinv);
if (!fmpz_mod_poly_equal(d, b))
{
flint_printf("FAIL (aliasing b)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(cinv);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and c */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, cinv, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(cinv, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_reverse (cinv, c, c->length);
fmpz_mod_poly_inv_series_newton (cinv, cinv, c->length);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung_preinv(d, a, b, c, cinv);
fmpz_mod_poly_compose_mod_brent_kung_preinv(c, a, b, c, cinv);
if (!fmpz_mod_poly_equal(d, c))
{
flint_printf("FAIL (aliasing c)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(cinv);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and cinv */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, cinv, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(cinv, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_reverse (cinv, c, c->length);
fmpz_mod_poly_inv_series_newton (cinv, cinv, c->length);
fmpz_mod_poly_rem(a, a, c);
fmpz_mod_poly_compose_mod_brent_kung_preinv(d, a, b, c, cinv);
fmpz_mod_poly_compose_mod_brent_kung_preinv(cinv, a, b, c, cinv);
if (!fmpz_mod_poly_equal(d, cinv))
{
flint_printf("FAIL (aliasing c)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(cinv);
fmpz_mod_poly_clear(d);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,202 @@
/*=============================================================================
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
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i;
FLINT_TEST_INIT(state);
flint_printf("compose_mod_horner....");
fflush(stdout);
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d, e;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_init(e, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_compose_mod_horner(d, a, b, c);
fmpz_mod_poly_compose(e, a, b);
fmpz_mod_poly_rem(e, e, c);
if (!fmpz_mod_poly_equal(d, e))
{
flint_printf("FAIL (composition):\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
flint_printf("e:\n"); fmpz_mod_poly_print(e); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
fmpz_mod_poly_clear(e);
}
/* Test aliasing of res and a */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_compose_mod_horner(d, a, b, c);
fmpz_mod_poly_compose_mod_horner(a, a, b, c);
if (!fmpz_mod_poly_equal(d, a))
{
flint_printf("FAIL (aliasing a):\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and b */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_compose_mod_horner(d, a, b, c);
fmpz_mod_poly_compose_mod_horner(b, a, b, c);
if (!fmpz_mod_poly_equal(d, b))
{
flint_printf("FAIL (aliasing b)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
}
/* Test aliasing of res and c */
for (i = 0; i < 1000; i++)
{
fmpz_mod_poly_t a, b, c, d;
fmpz_t p;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest(b, state, n_randint(state, 20) + 1);
fmpz_mod_poly_randtest_not_zero(c, state, n_randint(state, 20) + 1);
fmpz_mod_poly_compose_mod_horner(d, a, b, c);
fmpz_mod_poly_compose_mod_horner(c, a, b, c);
if (!fmpz_mod_poly_equal(d, c))
{
flint_printf("FAIL (aliasing c)\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a); flint_printf("\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b); flint_printf("\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c); flint_printf("\n");
flint_printf("d:\n"); fmpz_mod_poly_print(d); flint_printf("\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,163 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("derivative....");
fflush(stdout);
/* Check aliasing */
for (i = 0; i < 10000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_set(b, a);
fmpz_mod_poly_derivative(c, b);
fmpz_mod_poly_derivative(b, b);
result = (fmpz_mod_poly_equal(b, c));
if (!result)
{
flint_printf("FAIL (alias):\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Check constants have derivative zero */
for (i = 0; i < 10000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 2));
fmpz_mod_poly_derivative(b, a);
result = (b->length == 0);
if (!result)
{
flint_printf("FAIL (da == 0):\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_clear(p);
}
/* Check (f g)' = f' g + f g' */
for (i = 0; i < 10000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c, d, lhs, rhs;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_init(lhs, p);
fmpz_mod_poly_init(rhs, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
fmpz_mod_poly_mul(lhs, a, b);
fmpz_mod_poly_derivative(lhs, lhs);
fmpz_mod_poly_derivative(c, a);
fmpz_mod_poly_derivative(d, b);
fmpz_mod_poly_mul(c, c, b);
fmpz_mod_poly_mul(d, a, d);
fmpz_mod_poly_add(rhs, c, d);
result = fmpz_mod_poly_equal(lhs, rhs);
if (!result)
{
flint_printf("FAIL (Leibniz):\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("(ab)' = "), fmpz_mod_poly_print(lhs), flint_printf("\n\n");
flint_printf("a'b + ab' = "), fmpz_mod_poly_print(rhs), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
fmpz_mod_poly_clear(lhs);
fmpz_mod_poly_clear(rhs);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,206 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("div_basecase....");
fflush(stdout);
/* Compare to divrem_basecase */
for (i = 0; i < 5000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, q2, r2;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(q2, p);
fmpz_mod_poly_init(r2, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_div_basecase(q, a, b);
fmpz_mod_poly_divrem_basecase(q2, r2, a, b);
result = (fmpz_mod_poly_equal(q, q2));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("q2 = "), fmpz_mod_poly_print(q2), flint_printf("\n\n");
flint_printf("r2 = "), fmpz_mod_poly_print(r2), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(q2);
fmpz_mod_poly_clear(r2);
fmpz_clear(p);
}
/* Alias a and q */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_div_basecase(q, a, b);
fmpz_mod_poly_div_basecase(a, a, b);
result = (fmpz_mod_poly_equal(q, a));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_clear(p);
}
/* Alias b and q */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_div_basecase(q, a, b);
fmpz_mod_poly_div_basecase(b, a, b);
result = (fmpz_mod_poly_equal(q, b));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,297 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2009 William Hart
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#include <stdio.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("div_newton_n_preinv....");
fflush(stdout);
/* Compare to div_basecase */
for (i = 0; i < 5000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, q2, binv;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(q2, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_div_basecase(q, a, b);
fmpz_mod_poly_div_newton_n_preinv(q2, a, b, binv);
result = (fmpz_mod_poly_equal(q, q2));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("q2 = "), fmpz_mod_poly_print(q2), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(q2);
fmpz_clear(p);
}
/* Alias a and q */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, binv;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_div_newton_n_preinv(q, a, b, binv);
fmpz_mod_poly_div_newton_n_preinv(a, a, b, binv);
result = (fmpz_mod_poly_equal(q, a));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_clear(p);
}
/* Alias b and q */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, binv;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_div_newton_n_preinv(q, a, b, binv);
fmpz_mod_poly_div_newton_n_preinv(b, a, b, binv);
result = (fmpz_mod_poly_equal(q, b));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_clear(p);
}
/* Alias binv and q */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, binv;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_div_newton_n_preinv(q, a, b, binv);
fmpz_mod_poly_div_newton_n_preinv(binv, a, b, binv);
result = (fmpz_mod_poly_equal(q, binv));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("binv = "), fmpz_mod_poly_print(binv), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,213 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("divrem_basecase....");
fflush(stdout);
/* Check q*b + r = a, no aliasing */
for (i = 0; i < 5000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, r, t;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_divrem_basecase(q, r, a, b);
fmpz_mod_poly_mul(t, q, b);
fmpz_mod_poly_add(t, t, r);
result = (fmpz_mod_poly_equal(a, t));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
flint_printf("t = "), fmpz_mod_poly_print(t), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_mod_poly_clear(t);
fmpz_clear(p);
}
/* Alias a and q, b and r */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, r;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_divrem_basecase(q, r, a, b);
fmpz_mod_poly_divrem_basecase(a, b, a, b);
result = (fmpz_mod_poly_equal(q, a) && fmpz_mod_poly_equal(r, b));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(p);
}
/* Alias b and q, a and r */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, r;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_divrem_basecase(q, r, a, b);
fmpz_mod_poly_divrem_basecase(b, a, a, b);
result = (fmpz_mod_poly_equal(q, b) && fmpz_mod_poly_equal(r, a));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,213 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("divrem_divconquer....");
fflush(stdout);
/* Check q*b + r = a, no aliasing */
for (i = 0; i < 5000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, r, t;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_divrem_divconquer(q, r, a, b);
fmpz_mod_poly_mul(t, q, b);
fmpz_mod_poly_add(t, t, r);
result = (fmpz_mod_poly_equal(a, t));
if (!result)
{
flint_printf("FAIL #1:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
flint_printf("t = "), fmpz_mod_poly_print(t), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_mod_poly_clear(t);
fmpz_clear(p);
}
/* Alias a and q, b and r */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, r;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_divrem_divconquer(q, r, a, b);
fmpz_mod_poly_divrem_divconquer(a, b, a, b);
result = (fmpz_mod_poly_equal(q, a) && fmpz_mod_poly_equal(r, b));
if (!result)
{
flint_printf("FAIL #2:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(p);
}
/* Alias b and q, a and r */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, q, r;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_divrem_divconquer(q, r, a, b);
fmpz_mod_poly_divrem_divconquer(b, a, a, b);
result = (fmpz_mod_poly_equal(q, b) && fmpz_mod_poly_equal(r, a));
if (!result)
{
flint_printf("FAIL #3:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,176 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("divrem_f....");
fflush(stdout);
/* Check q*b + r = a when gcd(lead(B),p) = 1, no aliasing */
for (i = 0; i < 5000; i++)
{
fmpz_t f, p;
fmpz_mod_poly_t a, b, q, r, t;
fmpz_init(f);
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_divrem_f(f, q, r, a, b);
fmpz_mod_poly_mul(t, q, b);
fmpz_mod_poly_add(t, t, r);
result = (fmpz_is_one(f) && fmpz_mod_poly_equal(a, t));
if (!result)
{
flint_printf("FAIL (divrem):\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("f = "), fmpz_print(f), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
flint_printf("t = "), fmpz_mod_poly_print(t), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_mod_poly_clear(t);
fmpz_clear(f);
fmpz_clear(p);
}
/* Check f | p when gcd(lead(B),p) > 1 */
for (i = 0; i < 5000; i++)
{
fmpz_t f, p, q1, q2;
fmpz_mod_poly_t a, b, q, r, t;
fmpz_init(f);
fmpz_init(p);
fmpz_init(q1);
fmpz_init(q2);
fmpz_randtest_unsigned(q1, state, 2 * FLINT_BITS);
fmpz_randtest_unsigned(q2, state, 2 * FLINT_BITS);
fmpz_add_ui(q1, q1, 2);
fmpz_add_ui(q2, q2, 2);
fmpz_mul(p, q1, q2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
if (fmpz_is_one(d))
fmpz_set(leadB, q1);
fmpz_clear(d);
}
fmpz_mod_poly_divrem_f(f, q, r, a, b);
fmpz_mod_poly_mul(t, q, b);
fmpz_mod_poly_add(t, t, r);
result = (fmpz_cmp_ui(f, 1) > 0 && fmpz_cmp(f, p) < 0 && fmpz_divisible(p, f));
if (!result)
{
flint_printf("FAIL (factor):\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("f = "), fmpz_print(f), flint_printf("\n\n");
flint_printf("q1 = "), fmpz_print(q1), flint_printf("\n\n");
flint_printf("q2 = "), fmpz_print(q2), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
flint_printf("t = "), fmpz_mod_poly_print(t), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_mod_poly_clear(t);
fmpz_clear(f);
fmpz_clear(p);
fmpz_clear(q1);
fmpz_clear(q2);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,521 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2009 William Hart
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#include <stdio.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("divrem_newton_n_preinv....");
fflush(stdout);
/* Check q*b + r = a, no aliasing */
for (i = 0; i < 5000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, binv, q, r, t, q2, r2;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(q2, p);
fmpz_mod_poly_init(r, p);
fmpz_mod_poly_init(r2, p);
fmpz_mod_poly_init(t, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_divrem_newton_n_preinv (q, r, a, b, binv);
fmpz_mod_poly_mul(t, q, b);
fmpz_mod_poly_add(t, t, r);
result = (fmpz_mod_poly_equal(a, t));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
flint_printf("t = "), fmpz_mod_poly_print(t), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_divrem_basecase(q2, r2, a, b);
result = (fmpz_mod_poly_equal(q2, q));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("q2 = "), fmpz_mod_poly_print(q2), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
flint_printf("t = "), fmpz_mod_poly_print(t), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(q2);
fmpz_mod_poly_clear(r);
fmpz_mod_poly_clear(r2);
fmpz_mod_poly_clear(t);
fmpz_clear(p);
}
/* Alias a and q, b and r */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, binv, q, r;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_divrem_newton_n_preinv(q, r, a, b, binv);
fmpz_mod_poly_divrem_newton_n_preinv(a, b, a, b, binv);
result = (fmpz_mod_poly_equal(q, a) && fmpz_mod_poly_equal(r, b));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(p);
}
/* Alias b and q, a and r */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, binv, q, r;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_divrem_newton_n_preinv(q, r, a, b, binv);
fmpz_mod_poly_divrem_newton_n_preinv(b, a, a, b, binv);
result = (fmpz_mod_poly_equal(q, b) && fmpz_mod_poly_equal(r, a));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(p);
}
/* Alias binv and q, a and r*/
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, binv, q, r;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_divrem_newton_n_preinv(q, r, a, b, binv);
fmpz_mod_poly_divrem_newton_n_preinv(binv, a, a, b, binv);
result = (fmpz_mod_poly_equal(q, binv) && fmpz_mod_poly_equal(r, a));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("binv = "), fmpz_mod_poly_print(binv), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(p);
}
/* Alias binv and q, b and r*/
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, binv, q, r;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_divrem_newton_n_preinv(q, r, a, b, binv);
fmpz_mod_poly_divrem_newton_n_preinv(binv, b, a, b, binv);
result = (fmpz_mod_poly_equal(q, binv) && fmpz_mod_poly_equal(r, b));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("binv = "), fmpz_mod_poly_print(binv), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(p);
}
/* Alias a and q, binv and r*/
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, binv, q, r;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_divrem_newton_n_preinv(q, r, a, b, binv);
fmpz_mod_poly_divrem_newton_n_preinv(a, binv, a, b, binv);
result = (fmpz_mod_poly_equal(q, a) && fmpz_mod_poly_equal(r, binv));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("binv = "), fmpz_mod_poly_print(binv), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(p);
}
/* Alias b and q, binv and r*/
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, binv, q, r;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(binv, p);
fmpz_mod_poly_init(q, p);
fmpz_mod_poly_init(r, p);
do
fmpz_mod_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1);
while (b->length <= 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
if (a->length > 2*(b->length)-3)
fmpz_mod_poly_truncate (a, 2*(b->length)-3);
{
fmpz_t d;
fmpz *leadB = fmpz_mod_poly_lead(b);
fmpz_init(d);
fmpz_gcd(d, p, leadB);
while (!fmpz_is_one(d))
{
fmpz_divexact(leadB, leadB, d);
fmpz_gcd(d, p, leadB);
}
fmpz_clear(d);
}
fmpz_mod_poly_reverse (binv, b, b->length);
fmpz_mod_poly_inv_series_newton (binv, binv, b->length);
fmpz_mod_poly_divrem_newton_n_preinv(q, r, a, b, binv);
fmpz_mod_poly_divrem_newton_n_preinv(b, binv, a, b, binv);
result = (fmpz_mod_poly_equal(q, b) && fmpz_mod_poly_equal(r, binv));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("binv = "), fmpz_mod_poly_print(binv), flint_printf("\n\n");
flint_printf("q = "), fmpz_mod_poly_print(q), flint_printf("\n\n");
flint_printf("r = "), fmpz_mod_poly_print(r), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(binv);
fmpz_mod_poly_clear(q);
fmpz_mod_poly_clear(r);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,130 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("evaluate_fmpz....");
fflush(stdout);
/* Check aliasing */
for (i = 0; i < 10000; i++)
{
fmpz_t a, b, p;
fmpz_mod_poly_t f;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_init(a);
fmpz_init(b);
fmpz_randm(a, state, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_randtest(f, state, n_randint(state, 100));
fmpz_mod_poly_evaluate_fmpz(b, f, a);
fmpz_mod_poly_evaluate_fmpz(a, f, a);
result = (fmpz_equal(a, b));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(f), flint_printf("\n\n");
fmpz_print(a), flint_printf("\n\n");
fmpz_print(b), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(f);
fmpz_clear(a);
fmpz_clear(b);
fmpz_clear(p);
}
/* Check that the result agrees with Z[X] */
for (i = 0; i < 10000; i++)
{
fmpz_t a, b, c, p;
fmpz_mod_poly_t f;
fmpz_poly_t g;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_init(a);
fmpz_init(b);
fmpz_init(c);
fmpz_mod_poly_init(f, p);
fmpz_poly_init(g);
fmpz_randm(a, state, p);
fmpz_mod_poly_randtest(f, state, n_randint(state, 100));
fmpz_mod_poly_get_fmpz_poly(g, f);
fmpz_mod_poly_evaluate_fmpz(b, f, a);
fmpz_poly_evaluate_fmpz(c, g, a);
fmpz_mod(c, c, p);
result = (fmpz_equal(b, c));
if (!result)
{
flint_printf("FAIL (cmp with fmpz_poly):\n");
fmpz_mod_poly_print(f), flint_printf("\n\n");
fmpz_poly_print(g), flint_printf("\n\n");
fmpz_print(a), flint_printf("\n\n");
fmpz_print(b), flint_printf("\n\n");
fmpz_print(c), flint_printf("\n\n");
abort();
}
fmpz_clear(a);
fmpz_clear(b);
fmpz_clear(c);
fmpz_clear(p);
fmpz_mod_poly_clear(f);
fmpz_poly_clear(g);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,106 @@
/*=============================================================================
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, 2012 William Hart
Copyright (C) 2011, 2012 Fredrik Johansson
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_poly.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result = 1;
FLINT_TEST_INIT(state);
flint_printf("evaluate_fmpz_vec_fast....");
fflush(stdout);
for (i = 0; i < 1000 * flint_test_multiplier(); i++)
{
fmpz_mod_poly_t P;
fmpz * x, * y, * z;
fmpz_t mod;
slong j, n, npoints;
fmpz_init(mod);
do
{
fmpz_randtest_unsigned(mod, state, 5);
fmpz_add_ui(mod, mod, 2);
} while (!fmpz_is_probabprime(mod));
npoints = n_randint(state, 10);
n = n_randint(state, 10);
fmpz_mod_poly_init(P, mod);
x = _fmpz_vec_init(npoints);
y = _fmpz_vec_init(npoints);
z = _fmpz_vec_init(npoints);
fmpz_mod_poly_randtest(P, state, n);
for (j = 0; j < npoints; j++)
fmpz_randtest_mod(x + j, state, mod);
fmpz_mod_poly_evaluate_fmpz_vec_iter(y, P, x, npoints);
fmpz_mod_poly_evaluate_fmpz_vec_fast(z, P, x, npoints);
result = _fmpz_vec_equal(y, z, npoints);
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("mod=");
fmpz_print(mod);
flint_printf(", n=%wd, npoints=%wd\n\n", n, npoints);
flint_printf("P: "); fmpz_mod_poly_print(P); flint_printf("\n\n");
for (j = 0; j < npoints; j++)
fmpz_print(y + j), flint_printf(" ");
flint_printf("\n");
for (j = 0; j < npoints; j++)
fmpz_print(z + j), flint_printf(" ");
flint_printf("\n");
abort();
}
fmpz_clear(mod);
fmpz_mod_poly_clear(P);
_fmpz_vec_clear(x, npoints);
_fmpz_vec_clear(y, npoints);
_fmpz_vec_clear(z, npoints);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,335 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("gcd_euclidean....");
fflush(stdout);
/* Generic case, most likely co-prime arguments ******************************/
/* Check aliasing of a and c */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
fmpz_mod_poly_gcd_euclidean(c, a, b);
fmpz_mod_poly_gcd_euclidean(a, a, b);
result = (fmpz_mod_poly_equal(a, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Check aliasing of b and c */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
fmpz_mod_poly_gcd_euclidean(c, a, b);
fmpz_mod_poly_gcd_euclidean(b, a, b);
result = (fmpz_mod_poly_equal(b, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/*
Check that g = GCD(a,b) divides a and b,
and that 1 == GCD(a/g, b/g)
*/
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c, d, g, h, s, t;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_init(g, p);
fmpz_mod_poly_init(h, p);
fmpz_mod_poly_init(s, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
fmpz_mod_poly_gcd_euclidean(g, a, b);
if (fmpz_mod_poly_is_zero(g))
{
result = 1;
}
else
{
fmpz_mod_poly_divrem_basecase(c, s, a, g);
fmpz_mod_poly_divrem_basecase(d, t, b, g);
fmpz_mod_poly_gcd_euclidean(h, c, d);
result = (fmpz_mod_poly_is_zero(s) && fmpz_mod_poly_is_zero(t)
&& (h->length == 1) && (fmpz_is_one(h->coeffs)));
}
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("c = "), fmpz_mod_poly_print(c), flint_printf("\n\n");
flint_printf("d = "), fmpz_mod_poly_print(d), flint_printf("\n\n");
flint_printf("g = "), fmpz_mod_poly_print(g), flint_printf("\n\n");
flint_printf("h = "), fmpz_mod_poly_print(h), flint_printf("\n\n");
flint_printf("s = "), fmpz_mod_poly_print(s), flint_printf("\n\n");
flint_printf("t = "), fmpz_mod_poly_print(t), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
fmpz_mod_poly_clear(g);
fmpz_mod_poly_clear(h);
fmpz_mod_poly_clear(s);
fmpz_mod_poly_clear(t);
fmpz_clear(p);
}
/* Special case, arguments share a factor ********************************/
/* Check aliasing of a and c */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c, f;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
fmpz_mod_poly_randtest(f, state, n_randint(state, 20));
fmpz_mod_poly_mul(a, a, f);
fmpz_mod_poly_mul(b, b, f);
fmpz_mod_poly_gcd_euclidean(c, a, b);
fmpz_mod_poly_gcd_euclidean(a, a, b);
result = (fmpz_mod_poly_equal(a, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(f);
fmpz_clear(p);
}
/* Check aliasing of b and c */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c, f;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
fmpz_mod_poly_randtest(f, state, n_randint(state, 20));
fmpz_mod_poly_mul(a, a, f);
fmpz_mod_poly_mul(b, b, f);
fmpz_mod_poly_gcd_euclidean(c, a, b);
fmpz_mod_poly_gcd_euclidean(b, a, b);
result = (fmpz_mod_poly_equal(b, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(f);
fmpz_clear(p);
}
/*
Check that g = GCD(a,b) divides a and b,
and that 1 == GCD(a/g, b/g)
*/
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c, d, f, g, h, s, t;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(g, p);
fmpz_mod_poly_init(h, p);
fmpz_mod_poly_init(s, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
fmpz_mod_poly_randtest(f, state, n_randint(state, 20));
fmpz_mod_poly_mul(a, a, f);
fmpz_mod_poly_mul(b, b, f);
fmpz_mod_poly_gcd_euclidean(g, a, b);
if (fmpz_mod_poly_is_zero(g))
{
result = 1;
}
else
{
fmpz_mod_poly_divrem_basecase(c, s, a, g);
fmpz_mod_poly_divrem_basecase(d, t, b, g);
fmpz_mod_poly_gcd_euclidean(h, c, d);
result = (fmpz_mod_poly_is_zero(s) && fmpz_mod_poly_is_zero(t)
&& (h->length == 1) && (fmpz_is_one(h->coeffs)));
}
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("c = "), fmpz_mod_poly_print(c), flint_printf("\n\n");
flint_printf("d = "), fmpz_mod_poly_print(d), flint_printf("\n\n");
flint_printf("g = "), fmpz_mod_poly_print(g), flint_printf("\n\n");
flint_printf("h = "), fmpz_mod_poly_print(h), flint_printf("\n\n");
flint_printf("s = "), fmpz_mod_poly_print(s), flint_printf("\n\n");
flint_printf("t = "), fmpz_mod_poly_print(t), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(g);
fmpz_mod_poly_clear(h);
fmpz_mod_poly_clear(s);
fmpz_mod_poly_clear(t);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,100 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("gcd_euclidean_f....");
fflush(stdout);
/*
Compare with the usual GCD function.
N.B. I checked by hand that this test shows both outcomes,
i.e. trivial and non-trivial factors, sufficiently frequently.
*/
for (i = 0; i < 1000; i++)
{
fmpz_t p, f;
fmpz_mod_poly_t a, b, c, d;
fmpz_init(f);
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 100);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 60));
fmpz_mod_poly_randtest(b, state, n_randint(state, 60));
fmpz_mod_poly_gcd_euclidean_f(f, c, a, b);
if (!fmpz_is_one(f))
{
result = 1;
}
else
{
fmpz_mod_poly_gcd(d, a, b);
result = fmpz_mod_poly_equal(c, d);
}
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("c = "), fmpz_mod_poly_print(c), flint_printf("\n\n");
flint_printf("d = "), fmpz_mod_poly_print(d), flint_printf("\n\n");
flint_printf("f = "), fmpz_print(f), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
fmpz_clear(f);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,160 @@
/*=============================================================================
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) 2012 Sebastian Pancratz
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("gcdinv....");
fflush(stdout);
/* Generic case, most likely co-prime arguments ******************************/
/* Compare with result from XGCD */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, d, g, s, t, u;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_init(g, p);
fmpz_mod_poly_init(s, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_init(u, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
do
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
while (b->length < 2);
fmpz_mod_poly_gcdinv(d, u, a, b);
fmpz_mod_poly_xgcd(g, s, t, a, b);
result = ((fmpz_mod_poly_equal(d, g) && fmpz_mod_poly_equal(u, s))
|| (fmpz_mod_poly_is_zero(d)));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("d = "), fmpz_mod_poly_print(d), flint_printf("\n\n");
flint_printf("g = "), fmpz_mod_poly_print(g), flint_printf("\n\n");
flint_printf("s = "), fmpz_mod_poly_print(s), flint_printf("\n\n");
flint_printf("t = "), fmpz_mod_poly_print(t), flint_printf("\n\n");
flint_printf("u = "), fmpz_mod_poly_print(u), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(d);
fmpz_mod_poly_clear(g);
fmpz_mod_poly_clear(s);
fmpz_mod_poly_clear(t);
fmpz_mod_poly_clear(u);
fmpz_clear(p);
}
/* Special case, arguments share a factor ********************************/
/* Compare with result from XGCD */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, d, f, g, s, t, u;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(g, p);
fmpz_mod_poly_init(s, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_init(u, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
do
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
while (b->length < 2);
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 20) + 1);
fmpz_mod_poly_mul(a, f, a);
fmpz_mod_poly_mul(b, f, b);
fmpz_mod_poly_gcdinv(d, u, a, b);
fmpz_mod_poly_xgcd(g, s, t, a, b);
result = ((fmpz_mod_poly_equal(d, g) && fmpz_mod_poly_equal(u, s))
|| (fmpz_mod_poly_is_zero(d)));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("d = "), fmpz_mod_poly_print(d), flint_printf("\n\n");
flint_printf("f = "), fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("g = "), fmpz_mod_poly_print(g), flint_printf("\n\n");
flint_printf("s = "), fmpz_mod_poly_print(s), flint_printf("\n\n");
flint_printf("t = "), fmpz_mod_poly_print(t), flint_printf("\n\n");
flint_printf("u = "), fmpz_mod_poly_print(u), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(d);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(g);
fmpz_mod_poly_clear(s);
fmpz_mod_poly_clear(t);
fmpz_mod_poly_clear(u);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,84 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <stdio.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("get/set_fmpz_poly....");
fflush(stdout);
for (i = 0; i < 10000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b;
fmpz_poly_t c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_poly_init(c);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_get_fmpz_poly(c, a);
fmpz_mod_poly_set_fmpz_poly(b, c);
result = fmpz_mod_poly_equal(a, b);
if (!result)
{
flint_printf("FAIL:\n\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n");
flint_printf("c = "), fmpz_poly_print(c), flint_printf("\n");
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_poly_clear(c);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,95 @@
/*=============================================================================
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 Sebastian Pancratz
******************************************************************************/
#include <stdio.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i;
FLINT_TEST_INIT(state);
flint_printf("init/init2/realloc/clear....");
fflush(stdout);
for (i = 0; i < 10000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init2(a, p, n_randint(state, 100));
fmpz_mod_poly_clear(a);
fmpz_clear(p);
}
for (i = 0; i < 10000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init2(a, p, n_randint(state, 100));
fmpz_mod_poly_realloc(a, n_randint(state, 100));
fmpz_mod_poly_clear(a);
fmpz_clear(p);
}
for (i = 0; i < 10000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_clear(a);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,104 @@
/*=============================================================================
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) 2009 William Hart
Copyright (C) 2011 Sebastian Pancratz
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("inv_series_newton....");
fflush(stdout);
/* Check Q^{-1} * Q is congruent 1 mod t^n */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c, one;
slong n = n_randint(state, 80) + 1;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(one, p);
fmpz_mod_poly_randtest_not_zero(a, state, n_randint(state, 80) + 1);
{
fmpz_t d;
fmpz_init(d);
fmpz_gcd(d, a->coeffs, p);
while (!fmpz_is_one(d))
{
fmpz_randm(a->coeffs, state, p);
fmpz_gcd(d, a->coeffs, p);
}
fmpz_clear(d);
}
fmpz_mod_poly_set_ui(one, 1);
fmpz_mod_poly_inv_series_newton(b, a, n);
fmpz_mod_poly_mullow(c, a, b, n);
result = (fmpz_mod_poly_equal(c, one));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a = "), fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b = "), fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("c = "), fmpz_mod_poly_print(c), flint_printf("\n\n");
flint_printf("p = "), fmpz_print(p), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(one);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,231 @@
/*=============================================================================
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) 2012 Sebastian Pancratz
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("invmod....");
fflush(stdout);
/* Test aliasing *************************************************************/
/* Aliasing c and a */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
int ans1, ans2;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
do
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
while (b->length < 3);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
ans1 = fmpz_mod_poly_invmod(c, a, b);
ans2 = fmpz_mod_poly_invmod(a, a, b);
result = (ans1 == ans2 && fmpz_mod_poly_equal(a, c));
if (!result)
{
flint_printf("FAIL (alias a and c):\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
flint_printf("ans1 = %d\n\n", ans1);
flint_printf("ans2 = %d\n\n", ans2);
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Aliasing c and b */
for (i = 0; i < 500; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
int ans1, ans2;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
do
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
while (b->length < 3);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
ans1 = fmpz_mod_poly_invmod(c, a, b);
ans2 = fmpz_mod_poly_invmod(b, a, b);
result = ((ans1 == ans2) && fmpz_mod_poly_equal(b, c));
if (!result)
{
flint_printf("FAIL (alias b and c):\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
flint_printf("ans1 = %d\n\n", ans1);
flint_printf("ans2 = %d\n\n", ans2);
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Compare with result from XGCD */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, g, s, t, u;
int ans;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(g, p);
fmpz_mod_poly_init(s, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_init(u, p);
do
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
while (b->length < 3);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
ans = fmpz_mod_poly_invmod(u, a, b);
fmpz_mod_poly_xgcd(g, s, t, a, b);
result = (((ans) && g->length == 1
&& fmpz_is_one(g->coeffs) && fmpz_mod_poly_equal(s, u))
|| (!(ans) && g->length > 1));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(g), flint_printf("\n\n");
fmpz_mod_poly_print(s), flint_printf("\n\n");
fmpz_mod_poly_print(t), flint_printf("\n\n");
fmpz_mod_poly_print(u), flint_printf("\n\n");
flint_printf("ans = %d\n\n", ans);
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(g);
fmpz_mod_poly_clear(s);
fmpz_mod_poly_clear(t);
fmpz_mod_poly_clear(u);
fmpz_clear(p);
}
/* Special case, arguments share a factor ********************************/
/* Check correctness */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, f, u;
int ans;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(u, p);
do
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
while (b->length < 2);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
do
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 20) + 1);
while (f->length < 2);
fmpz_mod_poly_mul(a, f, a);
fmpz_mod_poly_mul(b, f, b);
ans = fmpz_mod_poly_invmod(u, a, b);
result = (!ans);
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(f), flint_printf("\n\n");
fmpz_mod_poly_print(u), flint_printf("\n\n");
flint_printf("ans = %d\n\n", ans);
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(u);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,161 @@
/*=============================================================================
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, 2010 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("mul....");
fflush(stdout);
/* Check aliasing of a and b */
for (i = 0; i < 2000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest(c, state, n_randint(state, 50));
fmpz_mod_poly_mul(a, b, c);
fmpz_mod_poly_mul(b, b, c);
result = (fmpz_mod_poly_equal(a, b));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Check aliasing of a and c */
for (i = 0; i < 2000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest(c, state, n_randint(state, 50));
fmpz_mod_poly_mul(a, b, c);
fmpz_mod_poly_mul(c, b, c);
result = (fmpz_mod_poly_equal(a, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
/* Check (b*c)+(b*d) = b*(c+d) */
for (i = 0; i < 2000; i++)
{
fmpz_mod_poly_t a1, a2, b, c, d;
fmpz_t p;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a1, p);
fmpz_mod_poly_init(a2, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_init(d, p);
fmpz_mod_poly_randtest(b, state, n_randint(state, 100));
fmpz_mod_poly_randtest(c, state, n_randint(state, 100));
fmpz_mod_poly_randtest(d, state, n_randint(state, 100));
fmpz_mod_poly_mul(a1, b, c);
fmpz_mod_poly_mul(a2, b, d);
fmpz_mod_poly_add(a1, a1, a2);
fmpz_mod_poly_add(c, c, d);
fmpz_mod_poly_mul(a2, b, c);
result = (fmpz_mod_poly_equal(a1, a2));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a1), flint_printf("\n\n");
fmpz_mod_poly_print(a2), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a1);
fmpz_mod_poly_clear(a2);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_mod_poly_clear(d);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,88 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("mullow....");
fflush(stdout);
/* Compare with truncated product of a and b */
for (i = 0; i < 2000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
slong trunc;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
trunc = n_randint(state, 50);
fmpz_mod_poly_randtest(b, state, trunc);
fmpz_mod_poly_randtest(c, state, trunc);
fmpz_mod_poly_mullow(a, b, c, trunc);
fmpz_mod_poly_mul(b, b, c);
fmpz_mod_poly_truncate(b, trunc);
result = (fmpz_mod_poly_equal(a, b));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,213 @@
/*=============================================================================
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) 2012 Lina Kulakova
Copyright (C) 2011, 2010 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("mulmod....");
fflush(stdout);
/* Check aliasing of res and a */
for (i = 0; i < 2000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, res, f;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_mulmod(res, a, b, f);
fmpz_mod_poly_mulmod(a, a, b, f);
result = (fmpz_mod_poly_equal(res, a));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(res);
fmpz_clear(p);
}
/* Check aliasing of res and b */
for (i = 0; i < 2000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, f, res;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_mulmod(res, a, b, f);
fmpz_mod_poly_mulmod(b, a, b, f);
result = (fmpz_mod_poly_equal(res, b));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(res);
fmpz_clear(p);
}
/* Check aliasing of res and f */
for (i = 0; i < 2000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, f, res;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_mulmod(res, a, b, f);
fmpz_mod_poly_mulmod(f, a, b, f);
result = (fmpz_mod_poly_equal(res, f));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(res);
fmpz_clear(p);
}
/* No aliasing */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, res1, res2, t, f;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_init(res1, p);
fmpz_mod_poly_init(res2, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_mulmod(res1, a, b, f);
fmpz_mod_poly_mul(res2, a, b);
fmpz_mod_poly_divrem(t, res2, res2, f);
result = (fmpz_mod_poly_equal(res1, res2));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res1:\n"); fmpz_mod_poly_print(res1), flint_printf("\n\n");
flint_printf("res2:\n"); fmpz_mod_poly_print(res2), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(res1);
fmpz_mod_poly_clear(res2);
fmpz_mod_poly_clear(t);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,305 @@
/*=============================================================================
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) 2012 Lina Kulakova
Copyright (C) 2011, 2010 Sebastian Pancratz
Copyright (C) 2009 William Hart
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#include <stdio.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("mulmod_preinv....");
fflush(stdout);
/* Check aliasing of res and a */
for (i = 0; i < 2000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, res, f, finv;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(finv, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
if (a->length >= f->length)
fmpz_mod_poly_rem (a, a, f);
if (b->length >= f->length)
fmpz_mod_poly_rem (b, b, f);
fmpz_mod_poly_reverse (finv, f, f->length);
fmpz_mod_poly_inv_series_newton (finv, finv, f->length);
fmpz_mod_poly_mulmod_preinv(res, a, b, f, finv);
fmpz_mod_poly_mulmod_preinv(a, a, b, f, finv);
result = (fmpz_mod_poly_equal(res, a));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(finv);
fmpz_mod_poly_clear(res);
fmpz_clear(p);
}
/* Check aliasing of res and b */
for (i = 0; i < 2000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, f, finv, res;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(finv, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
if (a->length >= f->length)
fmpz_mod_poly_rem (a, a, f);
if (b->length >= f->length)
fmpz_mod_poly_rem (b, b, f);
fmpz_mod_poly_reverse (finv, f, f->length);
fmpz_mod_poly_inv_series_newton (finv, finv, f->length);
fmpz_mod_poly_mulmod_preinv(res, a, b, f, finv);
fmpz_mod_poly_mulmod_preinv(b, a, b, f, finv);
result = (fmpz_mod_poly_equal(res, b));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(finv);
fmpz_mod_poly_clear(res);
fmpz_clear(p);
}
/* Check aliasing of res and f */
for (i = 0; i < 2000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, f, finv, res;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(finv, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
if (a->length >= f->length)
fmpz_mod_poly_rem (a, a, f);
if (b->length >= f->length)
fmpz_mod_poly_rem (b, b, f);
fmpz_mod_poly_reverse (finv, f, f->length);
fmpz_mod_poly_inv_series_newton (finv, finv, f->length);
fmpz_mod_poly_mulmod_preinv(res, a, b, f, finv);
fmpz_mod_poly_mulmod_preinv(f, a, b, f, finv);
result = (fmpz_mod_poly_equal(res, f));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(finv);
fmpz_mod_poly_clear(res);
fmpz_clear(p);
}
/* Check aliasing of res and finv */
for (i = 0; i < 2000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, f, finv, res;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(finv, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
if (a->length >= f->length)
fmpz_mod_poly_rem (a, a, f);
if (b->length >= f->length)
fmpz_mod_poly_rem (b, b, f);
fmpz_mod_poly_reverse (finv, f, f->length);
fmpz_mod_poly_inv_series_newton (finv, finv, f->length);
fmpz_mod_poly_mulmod_preinv(res, a, b, f, finv);
fmpz_mod_poly_mulmod_preinv(finv, a, b, f, finv);
result = (fmpz_mod_poly_equal(res, finv));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("finv:\n"); fmpz_mod_poly_print(finv), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(finv);
fmpz_mod_poly_clear(res);
fmpz_clear(p);
}
/* No aliasing */
for (i = 0; i < 1000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, res1, res2, f, finv;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(finv, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest(b, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
if (a->length >= f->length)
fmpz_mod_poly_rem (a, a, f);
if (b->length >= f->length)
fmpz_mod_poly_rem (b, b, f);
fmpz_mod_poly_reverse (finv, f, f->length);
fmpz_mod_poly_inv_series_newton (finv, finv, f->length);
fmpz_mod_poly_init(res1, p);
fmpz_mod_poly_init(res2, p);
fmpz_mod_poly_mulmod(res1, a, b, f);
fmpz_mod_poly_mulmod_preinv(res2, a, b, f, finv);
result = (fmpz_mod_poly_equal(res1, res2));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res1:\n"); fmpz_mod_poly_print(res1), flint_printf("\n\n");
flint_printf("res2:\n"); fmpz_mod_poly_print(res2), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(finv);
fmpz_mod_poly_clear(res1);
fmpz_mod_poly_clear(res2);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,83 @@
/*=============================================================================
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 Sebastian Pancratz
Copyright (C) 2009 William Hart
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("neg....");
fflush(stdout);
for (i = 0; i < 10000; i++)
{
fmpz_t p;
fmpz_mod_poly_t a, b, c;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 100));
fmpz_mod_poly_neg(b, a);
fmpz_mod_poly_neg(c, b);
result = (fmpz_mod_poly_equal(a, c));
if (!result)
{
flint_printf("FAIL:\n");
fmpz_mod_poly_print(a), flint_printf("\n\n");
fmpz_mod_poly_print(b), flint_printf("\n\n");
fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
fmpz_clear(p);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,131 @@
/*=============================================================================
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) 2012 Lina Kulakova
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("pow_trunc....");
fflush(stdout);
/* Check aliasing */
for (i = 0; i < 10000; i++)
{
fmpz_mod_poly_t a, b, c;
fmpz_t p;
slong e, trunc;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 30));
e = n_randint(state, 20);
trunc = n_randint(state, 30);
fmpz_mod_poly_set(c, a);
fmpz_mod_poly_pow_trunc(b, a, e, trunc);
fmpz_mod_poly_pow_trunc(c, c, e, trunc);
result = (fmpz_mod_poly_equal(b, c));
if (!result)
{
flint_printf("FAIL aliasing:\n");
flint_printf("a->length = %wd, p = %wu, exp = %wd, trunc = %wd\n",
a->length, a->p, e, trunc);
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
}
/* Check powering against naive method */
for (i = 0; i < 10000; i++)
{
fmpz_mod_poly_t a, b, c;
fmpz_t p;
slong e, trunc;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 30));
e = n_randint(state, 20);
trunc = n_randint(state, 30);
fmpz_mod_poly_pow_trunc(b, a, e, trunc);
fmpz_mod_poly_pow(c, a, e);
fmpz_mod_poly_truncate(c, trunc);
result = (fmpz_mod_poly_equal(b, c)
|| (a->length == 0 && e == 0 && c->length == 1 && c->coeffs[0] == 1));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a->length = %wd, p = %wu, exp = %wd, trunc = %wd\n",
a->length, a->p, e, trunc);
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,131 @@
/*=============================================================================
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) 2012 Lina Kulakova
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("pow_trunc_binexp....");
fflush(stdout);
/* Check aliasing */
for (i = 0; i < 10000; i++)
{
fmpz_mod_poly_t a, b, c;
fmpz_t p;
slong e, trunc;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 30));
e = n_randint(state, 20);
trunc = n_randint(state, 30);
fmpz_mod_poly_set(c, a);
fmpz_mod_poly_pow_trunc_binexp(b, a, e, trunc);
fmpz_mod_poly_pow_trunc_binexp(c, c, e, trunc);
result = (fmpz_mod_poly_equal(b, c));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a->length = %wd, p = %wu, exp = %wd, trunc = %wd\n",
a->length, a->p, e, trunc);
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
}
/* Check powering against naive method */
for (i = 0; i < 10000; i++)
{
fmpz_mod_poly_t a, b, c;
fmpz_t p;
slong e, trunc;
fmpz_init(p);
fmpz_randtest_unsigned(p, state, 2 * FLINT_BITS);
fmpz_add_ui(p, p, 2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(b, p);
fmpz_mod_poly_init(c, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 30));
e = n_randint(state, 20);
trunc = n_randint(state, 30);
fmpz_mod_poly_pow_trunc_binexp(b, a, e, trunc);
fmpz_mod_poly_pow(c, a, e);
fmpz_mod_poly_truncate(c, trunc);
result = (fmpz_mod_poly_equal(b, c)
|| (a->length == 0 && e == 0 && c->length == 1 && c->coeffs[0] == 1));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a->length = %wd, p = %wu, exp = %wd, trunc = %wd\n",
a->length, a->p, e, trunc);
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("b:\n"); fmpz_mod_poly_print(b), flint_printf("\n\n");
flint_printf("c:\n"); fmpz_mod_poly_print(c), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(b);
fmpz_mod_poly_clear(c);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,238 @@
/*=============================================================================
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) 2009 William Hart
Copyright (C) 2011 Fredrik Johansson
Copyright (C) 2012 Lina Kulakova
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <gmp.h>
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("powmod_fmpz_binexp....");
fflush(stdout);
/* Aliasing of res and a */
for (i = 0; i < 250; i++)
{
fmpz_mod_poly_t a, res, t, f;
fmpz_t p;
ulong exp;
fmpz_t expz;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
exp = n_randint(state, 50);
fmpz_init_set_ui(expz, exp);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_powmod_fmpz_binexp(res, a, expz, f);
fmpz_mod_poly_powmod_fmpz_binexp(a, a, expz, f);
result = (fmpz_mod_poly_equal(res, a));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(res);
fmpz_mod_poly_clear(t);
fmpz_clear(expz);
}
/* Aliasing of res and f */
for (i = 0; i < 250; i++)
{
fmpz_mod_poly_t a, res, t, f;
fmpz_t p;
ulong exp;
fmpz_t expz;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
exp = n_randint(state, 50);
fmpz_init_set_ui(expz, exp);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_powmod_fmpz_binexp(res, a, expz, f);
fmpz_mod_poly_powmod_fmpz_binexp(f, a, expz, f);
result = (fmpz_mod_poly_equal(res, f));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(res);
fmpz_mod_poly_clear(t);
fmpz_clear(expz);
}
/* No aliasing */
for (i = 0; i < 500; i++)
{
fmpz_mod_poly_t a, res1, res2, t, f;
fmpz_t p;
ulong exp;
fmpz_t expz;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
exp = n_randint(state, 50);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(res1, p);
fmpz_mod_poly_init(res2, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_init_set_ui(expz, exp);
fmpz_mod_poly_powmod_fmpz_binexp(res1, a, expz, f);
fmpz_mod_poly_powmod_ui_binexp(res2, a, exp, f);
result = (fmpz_mod_poly_equal(res1, res2));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res1:\n"); fmpz_mod_poly_print(res1), flint_printf("\n\n");
flint_printf("res2:\n"); fmpz_mod_poly_print(res2), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(res1);
fmpz_mod_poly_clear(res2);
fmpz_mod_poly_clear(t);
fmpz_clear(expz);
}
/* Check that a^(b+c) = a^b * a^c */
for (i = 0; i < 500; i++)
{
fmpz_mod_poly_t a, res1, res2, res3, res4, t, f;
fmpz_t p;
fmpz_t exp1, exp2, exp3;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_init(exp1);
fmpz_init(exp2);
fmpz_randtest(exp1, state, 200);
if (fmpz_sgn(exp1) == -1) fmpz_neg(exp1, exp1);
fmpz_randtest(exp2, state, 200);
if (fmpz_sgn(exp2) == -1) fmpz_neg(exp2, exp2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(res1, p);
fmpz_mod_poly_init(res2, p);
fmpz_mod_poly_init(res3, p);
fmpz_mod_poly_init(res4, p);
fmpz_mod_poly_init(t, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_powmod_fmpz_binexp(res1, a, exp1, f);
fmpz_mod_poly_powmod_fmpz_binexp(res2, a, exp2, f);
fmpz_mod_poly_mulmod(res4, res1, res2, f);
fmpz_init(exp3);
fmpz_add(exp3, exp1, exp2);
fmpz_mod_poly_powmod_fmpz_binexp(res3, a, exp3, f);
result = (fmpz_mod_poly_equal(res4, res3));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res3:\n"); fmpz_mod_poly_print(res3), flint_printf("\n\n");
flint_printf("res4:\n"); fmpz_mod_poly_print(res4), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(res1);
fmpz_mod_poly_clear(res2);
fmpz_mod_poly_clear(res3);
fmpz_mod_poly_clear(res4);
fmpz_mod_poly_clear(t);
fmpz_clear(exp1);
fmpz_clear(exp2);
fmpz_clear(exp3);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

View File

@@ -0,0 +1,308 @@
/*=============================================================================
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) 2009 William Hart
Copyright (C) 2011 Fredrik Johansson
Copyright (C) 2012 Lina Kulakova
Copyright (C) 2013 Martin Lee
******************************************************************************/
#undef ulong
#define ulong ulongxx/* interferes with system includes */
#include <stdlib.h>
#include <stdio.h>
#undef ulong
#include <gmp.h>
#define ulong mp_limb_t
#include "flint.h"
#include "fmpz_vec.h"
#include "fmpz_mod_poly.h"
#include "ulong_extras.h"
int
main(void)
{
int i, result;
FLINT_TEST_INIT(state);
flint_printf("powmod_fmpz_binexp_preinv....");
fflush(stdout);
/* Aliasing of res and a */
for (i = 0; i < 250; i++)
{
fmpz_mod_poly_t a, res, f, finv;
fmpz_t p;
ulong exp;
fmpz_t expz;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
exp = n_randint(state, 50);
fmpz_init_set_ui(expz, exp);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(finv, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_reverse (finv, f, f->length);
fmpz_mod_poly_inv_series_newton (finv, finv, f->length);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(res, a, expz, f, finv);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(a, a, expz, f, finv);
result = (fmpz_mod_poly_equal(res, a));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(finv);
fmpz_mod_poly_clear(res);
fmpz_clear(expz);
}
/* Aliasing of res and f */
for (i = 0; i < 250; i++)
{
fmpz_mod_poly_t a, res, f, finv;
fmpz_t p;
ulong exp;
fmpz_t expz;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
exp = n_randint(state, 50);
fmpz_init_set_ui(expz, exp);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(finv, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_reverse (finv, f, f->length);
fmpz_mod_poly_inv_series_newton (finv, finv, f->length);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(res, a, expz, f, finv);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(f, a, expz, f, finv);
result = (fmpz_mod_poly_equal(res, f));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(finv);
fmpz_mod_poly_clear(res);
fmpz_clear(expz);
}
/* Aliasing of res and finv */
for (i = 0; i < 250; i++)
{
fmpz_mod_poly_t a, res, f, finv;
fmpz_t p;
ulong exp;
fmpz_t expz;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
exp = n_randint(state, 50);
fmpz_init_set_ui(expz, exp);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(finv, p);
fmpz_mod_poly_init(res, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_reverse (finv, f, f->length);
fmpz_mod_poly_inv_series_newton (finv, finv, f->length);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(res, a, expz, f, finv);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(finv, a, expz, f, finv);
result = (fmpz_mod_poly_equal(res, finv));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("finv:\n"); fmpz_mod_poly_print(finv), flint_printf("\n\n");
flint_printf("res:\n"); fmpz_mod_poly_print(res), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(finv);
fmpz_mod_poly_clear(res);
fmpz_clear(expz);
}
/* No aliasing */
for (i = 0; i < 500; i++)
{
fmpz_mod_poly_t a, res1, res2, f, finv;
fmpz_t p;
ulong exp;
fmpz_t expz;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
exp = n_randint(state, 50);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(finv, p);
fmpz_mod_poly_init(res1, p);
fmpz_mod_poly_init(res2, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_init_set_ui(expz, exp);
fmpz_mod_poly_reverse (finv, f, f->length);
fmpz_mod_poly_inv_series_newton (finv, finv, f->length);
fmpz_mod_poly_powmod_fmpz_binexp(res1, a, expz, f);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(res2, a, expz, f, finv);
result = (fmpz_mod_poly_equal(res1, res2));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("finv:\n"); fmpz_mod_poly_print(finv), flint_printf("\n\n");
flint_printf("res1:\n"); fmpz_mod_poly_print(res1), flint_printf("\n\n");
flint_printf("res2:\n"); fmpz_mod_poly_print(res2), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(res1);
fmpz_mod_poly_clear(res2);
fmpz_mod_poly_clear(finv);
fmpz_clear(expz);
}
/* Check that a^(b+c) = a^b * a^c */
for (i = 0; i < 500; i++)
{
fmpz_mod_poly_t a, res1, res2, res3, res4, f, finv;
fmpz_t p;
fmpz_t exp1, exp2, exp3;
fmpz_init(p);
fmpz_set_ui(p, n_randtest_prime(state, 0));
fmpz_init(exp1);
fmpz_init(exp2);
fmpz_randtest(exp1, state, 200);
if (fmpz_sgn(exp1) == -1) fmpz_neg(exp1, exp1);
fmpz_randtest(exp2, state, 200);
if (fmpz_sgn(exp2) == -1) fmpz_neg(exp2, exp2);
fmpz_mod_poly_init(a, p);
fmpz_mod_poly_init(f, p);
fmpz_mod_poly_init(finv, p);
fmpz_mod_poly_init(res1, p);
fmpz_mod_poly_init(res2, p);
fmpz_mod_poly_init(res3, p);
fmpz_mod_poly_init(res4, p);
fmpz_mod_poly_randtest(a, state, n_randint(state, 50));
fmpz_mod_poly_randtest_not_zero(f, state, n_randint(state, 50) + 1);
fmpz_mod_poly_reverse (finv, f, f->length);
fmpz_mod_poly_inv_series_newton (finv, finv, f->length);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(res1, a, exp1, f, finv);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(res2, a, exp2, f, finv);
fmpz_mod_poly_mulmod(res4, res1, res2, f);
fmpz_init(exp3);
fmpz_add(exp3, exp1, exp2);
fmpz_mod_poly_powmod_fmpz_binexp_preinv(res3, a, exp3, f, finv);
result = (fmpz_mod_poly_equal(res4, res3));
if (!result)
{
flint_printf("FAIL:\n");
flint_printf("a:\n"); fmpz_mod_poly_print(a), flint_printf("\n\n");
flint_printf("f:\n"); fmpz_mod_poly_print(f), flint_printf("\n\n");
flint_printf("res3:\n"); fmpz_mod_poly_print(res3), flint_printf("\n\n");
flint_printf("res4:\n"); fmpz_mod_poly_print(res4), flint_printf("\n\n");
abort();
}
fmpz_clear(p);
fmpz_mod_poly_clear(a);
fmpz_mod_poly_clear(f);
fmpz_mod_poly_clear(finv);
fmpz_mod_poly_clear(res1);
fmpz_mod_poly_clear(res2);
fmpz_mod_poly_clear(res3);
fmpz_mod_poly_clear(res4);
fmpz_clear(exp1);
fmpz_clear(exp2);
fmpz_clear(exp3);
}
FLINT_TEST_CLEANUP(state);
flint_printf("PASS\n");
return 0;
}

Some files were not shown because too many files have changed in this diff Show More