forked from flintlib/flint2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add fast multipoint evaluation and interpolation for nmod_poly (worki…
…ng, but needs integration)
- Loading branch information
1 parent
077c6a8
commit 0e204e4
Showing
7 changed files
with
609 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| /*============================================================================= | ||
| 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 | ||
| ******************************************************************************/ | ||
|
|
||
| #include <mpir.h> | ||
| #include "flint.h" | ||
| #include "ulong_extras.h" | ||
| #include "nmod_poly.h" | ||
|
|
||
| void | ||
| _nmod_poly_evaluate_nmod_vec_fast_precomp(mp_ptr vs, mp_srcptr poly, | ||
| long plen, mp_ptr * tree, long len, nmod_t mod) | ||
| { | ||
| long height, i, j, pow, left; | ||
| long tree_height; | ||
| long tlen; | ||
| mp_ptr t, u, swap, pa, pb, pc; | ||
|
|
||
| /* avoid worrying about some degenerate cases */ | ||
| if (len < 2 || plen < 2) | ||
| { | ||
| if (len == 1) | ||
| vs[0] = _nmod_poly_evaluate_nmod(poly, plen, | ||
| nmod_neg(tree[0][0], mod), mod); | ||
| else if (len != 0 && plen == 0) | ||
| _nmod_vec_zero(vs, len); | ||
| else if (len != 0 && plen == 1) | ||
| for (i = 0; i < len; i++) | ||
| vs[i] = poly[0]; | ||
| return; | ||
| } | ||
|
|
||
| t = _nmod_vec_init(len); | ||
| u = _nmod_vec_init(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 = 1L << height; | ||
|
|
||
| for (i = j = 0; i < len; i += pow, j += (pow + 1)) | ||
| { | ||
| tlen = ((i + pow) <= len) ? pow : len % pow; | ||
| _nmod_poly_rem(t + i, poly, plen, tree[height] + j, tlen + 1, mod); | ||
| } | ||
|
|
||
| for (i = height - 1; i >= 0; i--) | ||
| { | ||
| pow = 1L << i; | ||
| left = len; | ||
| pa = tree[i]; | ||
| pb = t; | ||
| pc = u; | ||
|
|
||
| while (left >= 2 * pow) | ||
| { | ||
| _nmod_poly_rem(pc, pb, 2 * pow, pa, pow + 1, mod); | ||
| _nmod_poly_rem(pc + pow, pb, 2 * pow, pa + pow + 1, pow + 1, mod); | ||
|
|
||
| pa += 2 * pow + 2; | ||
| pb += 2 * pow; | ||
| pc += 2 * pow; | ||
| left -= 2 * pow; | ||
| } | ||
|
|
||
| if (left > pow) | ||
| { | ||
| _nmod_poly_rem(pc, pb, left, pa, pow + 1, mod); | ||
| _nmod_poly_rem(pc + pow, pb, left, pa + pow + 1, left - pow + 1, mod); | ||
| } | ||
| else if (left > 0) | ||
| _nmod_vec_set(pc, pb, left); | ||
|
|
||
| swap = t; | ||
| t = u; | ||
| u = swap; | ||
| } | ||
|
|
||
| _nmod_vec_set(vs, t, len); | ||
| _nmod_vec_clear(t); | ||
| _nmod_vec_clear(u); | ||
| } | ||
|
|
||
| void _nmod_poly_evaluate_nmod_vec_fast(mp_ptr ys, mp_srcptr poly, long plen, | ||
| mp_srcptr xs, long n, nmod_t mod) | ||
| { | ||
| mp_ptr * tree; | ||
|
|
||
| tree = _nmod_poly_tree_alloc(n); | ||
| _nmod_poly_tree_build(tree, xs, n, mod); | ||
| _nmod_poly_evaluate_nmod_vec_fast_precomp(ys, poly, plen, tree, n, mod); | ||
| _nmod_poly_tree_free(tree, n); | ||
| } | ||
|
|
||
| void | ||
| nmod_poly_evaluate_nmod_vec_fast(mp_ptr ys, | ||
| const nmod_poly_t poly, mp_srcptr xs, long n) | ||
| { | ||
| _nmod_poly_evaluate_nmod_vec_fast(ys, poly->coeffs, | ||
| poly->length, xs, n, poly->mod); | ||
| } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /*============================================================================= | ||
| 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 | ||
| ******************************************************************************/ | ||
|
|
||
| #include <mpir.h> | ||
| #include "flint.h" | ||
| #include "ulong_extras.h" | ||
| #include "nmod_vec.h" | ||
| #include "nmod_poly.h" | ||
|
|
||
| void | ||
| _nmod_poly_interpolation_weights(mp_ptr w, mp_ptr * tree, long len, nmod_t mod) | ||
| { | ||
| mp_ptr tmp; | ||
| long i, n, height; | ||
|
|
||
| if (len == 0) | ||
| return; | ||
|
|
||
| if (len == 1) | ||
| { | ||
| w[0] = 1; | ||
| return; | ||
| } | ||
|
|
||
| tmp = _nmod_vec_init(len + 1); | ||
| height = FLINT_CLOG2(len); | ||
| n = 1L << (height - 1); | ||
|
|
||
| _nmod_poly_mul(tmp, tree[height-1], n + 1, | ||
| tree[height-1] + (n + 1), (len - n + 1), mod); | ||
|
|
||
| _nmod_poly_derivative(tmp, tmp, len + 1, mod); | ||
| _nmod_poly_evaluate_nmod_vec_fast_precomp(w, tmp, len, tree, len, mod); | ||
|
|
||
| for (i = 0; i < len; i++) | ||
| w[i] = n_invmod(w[i], mod.n); | ||
|
|
||
| _nmod_vec_clear(tmp); | ||
| } | ||
|
|
||
| void | ||
| _nmod_poly_interpolate_nmod_vec_fast_precomp(mp_ptr poly, mp_srcptr ys, | ||
| mp_ptr * tree, mp_srcptr weights, long len, nmod_t mod) | ||
| { | ||
| mp_ptr t, u, pa, pb; | ||
| long i, pow, left; | ||
|
|
||
| if (len == 0) | ||
| return; | ||
|
|
||
| t = _nmod_vec_init(len); | ||
| u = _nmod_vec_init(len); | ||
|
|
||
| for (i = 0; i < len; i++) | ||
| poly[i] = nmod_mul(weights[i], ys[i], mod); | ||
|
|
||
| for (i = 0; i < FLINT_CLOG2(len); i++) | ||
| { | ||
| pow = (1L << i); | ||
| pa = tree[i]; | ||
| pb = poly; | ||
| left = len; | ||
|
|
||
| while (left >= 2 * pow) | ||
| { | ||
| _nmod_poly_mul(t, pa, pow + 1, pb + pow, pow, mod); | ||
| _nmod_poly_mul(u, pa + pow + 1, pow + 1, pb, pow, mod); | ||
| _nmod_vec_add(pb, t, u, 2 * pow, mod); | ||
|
|
||
| left -= 2 * pow; | ||
| pa += 2 * pow + 2; | ||
| pb += 2 * pow; | ||
| } | ||
|
|
||
| if (left > pow) | ||
| { | ||
| _nmod_poly_mul(t, pa, pow + 1, pb + pow, left - pow, mod); | ||
| _nmod_poly_mul(u, pb, pow, pa + pow + 1, left - pow + 1, mod); | ||
| _nmod_vec_add(pb, t, u, left, mod); | ||
| } | ||
| } | ||
|
|
||
| _nmod_vec_clear(t); | ||
| _nmod_vec_clear(u); | ||
| } | ||
|
|
||
|
|
||
| void | ||
| _nmod_poly_interpolate_nmod_vec_fast(mp_ptr poly, | ||
| mp_srcptr xs, mp_srcptr ys, long len, nmod_t mod) | ||
| { | ||
| mp_ptr * tree; | ||
| mp_ptr w; | ||
|
|
||
| tree = _nmod_poly_tree_alloc(len); | ||
| _nmod_poly_tree_build(tree, xs, len, mod); | ||
|
|
||
| w = _nmod_vec_init(len); | ||
| _nmod_poly_interpolation_weights(w, tree, len, mod); | ||
|
|
||
| _nmod_poly_interpolate_nmod_vec_fast_precomp(poly, ys, tree, w, len, mod); | ||
|
|
||
| _nmod_vec_clear(w); | ||
| _nmod_poly_tree_free(tree, len); | ||
| } | ||
|
|
||
| void | ||
| nmod_poly_interpolate_nmod_vec_fast(nmod_poly_t poly, | ||
| mp_srcptr xs, mp_srcptr ys, long n) | ||
| { | ||
| if (n == 0) | ||
| { | ||
| nmod_poly_zero(poly); | ||
| } | ||
| else | ||
| { | ||
| nmod_poly_fit_length(poly, n); | ||
| poly->length = n; | ||
| _nmod_poly_interpolate_nmod_vec_fast(poly->coeffs, | ||
| xs, ys, n, poly->mod); | ||
| _nmod_poly_normalise(poly); | ||
| } | ||
| } |
Oops, something went wrong.