-
Notifications
You must be signed in to change notification settings - Fork 275
Port PML's geometric evaluation / interpolation integration for univariate polynomial #2449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
0fc26d6
3aed36c
0ce5e5f
2bc9678
f208902
0967764
88fbcb9
8fbaed2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| Copyright (C) 2025, Vincent Neiger | ||
maelhos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Copyright (C) 2025, Mael Hostettler | ||
| This file is part of FLINT. | ||
| FLINT is free software: you can redistribute it and/or modify it under | ||
| the terms of the GNU Lesser General Public License (LGPL) as published | ||
| by the Free Software Foundation; either version 3 of the License, or | ||
| (at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include "nmod.h" | ||
| #include "nmod_vec.h" | ||
maelhos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #include "nmod_poly.h" | ||
|
|
||
| void | ||
| _nmod_poly_evaluate_geometric_nmod_vec_fast_precomp(nn_ptr vs, nn_srcptr poly, | ||
| slong plen, const nmod_geometric_progression_t G, slong len) | ||
| { | ||
| nmod_poly_t a, b; | ||
| slong i, d; | ||
|
|
||
| d = G->d; | ||
| FLINT_ASSERT(len <= d); | ||
|
|
||
| if (plen == 0) | ||
| { | ||
| for (i = 0; i < d; i++) | ||
| { | ||
| vs[i] = 0; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
| return; | ||
| } | ||
|
|
||
| nmod_poly_init2(a, G->mod.n, d); | ||
| nmod_poly_init(b, G->mod.n); | ||
|
|
||
| for (i = 0; i < plen; i++) | ||
| { | ||
| nmod_poly_set_coeff_ui(a, d - 1 - i, nmod_mul(G->x[i], poly[i], G->mod)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| for (i = plen + 1; i < d; i++) | ||
| { | ||
| nmod_poly_set_coeff_ui(a, d - 1 - i, 0); | ||
| } | ||
maelhos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| nmod_poly_mul(b, a, G->f); | ||
|
|
||
| for (i = 0; i < len; i++) | ||
| { | ||
| vs[i] = nmod_mul(G->x[i], nmod_poly_get_coeff_ui(b, i+d-1), G->mod); | ||
| } | ||
|
|
||
| nmod_poly_clear(b); | ||
| nmod_poly_clear(a); | ||
| } | ||
|
|
||
| void | ||
| _nmod_poly_evaluate_geometric_nmod_vec_fast(nn_ptr ys, nn_srcptr poly, | ||
| slong plen, ulong r, slong n, nmod_t mod) | ||
| { | ||
| nmod_geometric_progression_t G; | ||
| nmod_geometric_progression_init(G, r, n, mod); | ||
|
|
||
| _nmod_poly_evaluate_geometric_nmod_vec_fast_precomp(ys, poly, plen, G, n); | ||
| nmod_geometric_progression_clear(G); | ||
| } | ||
|
|
||
| void | ||
| nmod_poly_evaluate_geometric_nmod_vec_fast(nn_ptr ys, | ||
| const nmod_poly_t poly, ulong r, slong n) | ||
| { | ||
| _nmod_poly_evaluate_geometric_nmod_vec_fast(ys, poly->coeffs, | ||
| poly->length, r, n, poly->mod); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| Copyright (C) 2025, Vincent Neiger | ||
maelhos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Copyright (C) 2025, Mael Hostettler | ||
| This file is part of FLINT. | ||
| FLINT is free software: you can redistribute it and/or modify it under | ||
| the terms of the GNU Lesser General Public License (LGPL) as published | ||
| by the Free Software Foundation; either version 3 of the License, or | ||
| (at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include "nmod.h" | ||
| #include "nmod_vec.h" | ||
| #include "nmod_poly.h" | ||
|
|
||
| void | ||
| nmod_geometric_progression_init(nmod_geometric_progression_t G, ulong r, slong d, nmod_t mod) | ||
| { | ||
| ulong q, inv_r, inv_q, tmp, qk, inv_qk, qq, s; | ||
| nn_ptr diff, inv_diff, prod_diff; | ||
| slong i; | ||
|
|
||
| G->d = d; | ||
| G->mod = mod; | ||
|
|
||
| nmod_poly_init2(G->f, mod.n, 2*d-1); | ||
| nmod_poly_init(G->g1, mod.n); | ||
| nmod_poly_init(G->g2, mod.n); | ||
| nmod_poly_set_coeff_ui(G->g1, 0, 1); | ||
| nmod_poly_set_coeff_ui(G->g2, 0, 1); | ||
|
|
||
| G->x = _nmod_vec_init(d); | ||
| G->w = _nmod_vec_init(d); | ||
| G->z = _nmod_vec_init(d); | ||
| G->y = _nmod_vec_init(d); | ||
|
|
||
| G->x[0] = 1; | ||
| G->y[0] = 1; | ||
| G->w[0] = 1; | ||
| G->z[0] = 1; | ||
|
|
||
| q = nmod_mul(r, r, mod); | ||
| inv_r = nmod_inv(r, mod); | ||
| inv_q = nmod_mul(inv_r, inv_r, mod); | ||
|
|
||
| nmod_poly_set_coeff_ui(G->f, 0, 1); | ||
| tmp = r; | ||
| for (i = 1; i < 2*d-1; i++) | ||
| { | ||
| nmod_poly_set_coeff_ui(G->f, i, nmod_mul(nmod_poly_get_coeff_ui(G->f, i-1), tmp, mod)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same remark as in other file: I think this could use direct coefficient access ( |
||
| tmp = nmod_mul(tmp, q, mod); | ||
| } | ||
|
|
||
| tmp = inv_r; | ||
| for (i = 1; i < d; i++) | ||
| { | ||
| G->x[i] = nmod_mul(G->x[i-1], tmp, mod); | ||
| tmp = nmod_mul(tmp, inv_q, mod); | ||
| } | ||
|
|
||
| inv_diff = _nmod_vec_init(d); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be confirmed (todo for me): if I remember well, this is inverting a bunch of elements via the approach recently merged (#2432). If confirmed, we should see how to call that new |
||
| diff = _nmod_vec_init(d); | ||
| prod_diff = _nmod_vec_init(d); | ||
| inv_diff[0] = 1; | ||
| diff[0] = 1; | ||
| prod_diff[0] = 1; | ||
|
|
||
| qk = q; // montgomery inversion | ||
| for (i = 1; i < d; i++) | ||
| { | ||
| diff[i] = qk - 1; | ||
| inv_diff[i] = diff[i]; | ||
| qk = nmod_mul(qk, q, mod); | ||
| prod_diff[i] = nmod_mul(diff[i], prod_diff[i-1], mod); | ||
| } | ||
|
|
||
| tmp = n_invmod(prod_diff[d-1], mod.n); | ||
| for (i = d-1; i > 0; i--) | ||
| { | ||
| inv_diff[i] = nmod_mul(prod_diff[i-1], tmp, mod); | ||
| tmp = nmod_mul(tmp, diff[i], mod); | ||
| } | ||
| inv_diff[0] = tmp; | ||
| // end montgomery inversion | ||
|
|
||
| // sets sequences w, y, z and polynomials g1, g2 | ||
| qk = 1; | ||
| inv_qk = 1; | ||
| qq = 1; | ||
| s = 1; | ||
|
|
||
| for (i = 1; i < d; i++) | ||
| { | ||
| qq = nmod_mul(qq, qk, mod); // prod q^i | ||
| s = nmod_mul(s, inv_qk, mod); // prod 1/q^i | ||
| G->w[i] = nmod_mul(G->w[i-1], inv_diff[i], mod); // prod 1/(q^i-1) | ||
| tmp = nmod_mul(qq, G->w[i], mod); // prod q^i/(q^i-1) | ||
| nmod_poly_set_coeff_ui(G->g2, i, tmp); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could see here as well about avoiding the use of |
||
|
|
||
| if ((i & 1) == 1) | ||
| { | ||
| nmod_poly_set_coeff_ui(G->g1, i, nmod_neg(tmp, mod)); | ||
| G->y[i] = nmod_neg(prod_diff[i], mod); | ||
| G->z[i] = nmod_neg(G->w[i], mod); | ||
| } | ||
| else | ||
| { | ||
| nmod_poly_set_coeff_ui(G->g1, i, tmp); | ||
| G->y[i] = prod_diff[i]; | ||
| G->z[i] = G->w[i]; | ||
| } | ||
| G->y[i] = nmod_mul(G->y[i], s, mod); | ||
|
|
||
| qk = nmod_mul(qk, q, mod); | ||
| inv_qk = nmod_mul(inv_qk, inv_q, mod); | ||
| } | ||
|
|
||
| _nmod_vec_clear(prod_diff); | ||
| _nmod_vec_clear(inv_diff); | ||
| _nmod_vec_clear(diff); | ||
| } | ||
|
|
||
| void | ||
| nmod_geometric_progression_clear(nmod_geometric_progression_t G) | ||
| { | ||
| nmod_poly_clear(G->f); | ||
| nmod_poly_clear(G->g2); | ||
| nmod_poly_clear(G->g1); | ||
| _nmod_vec_clear(G->x); | ||
| _nmod_vec_clear(G->z); | ||
| _nmod_vec_clear(G->y); | ||
| _nmod_vec_clear(G->w); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| /* | ||
| Copyright (C) 2025, Vincent Neiger | ||
| Copyright (C) 2025, Mael Hostettler | ||
|
|
||
| This file is part of FLINT. | ||
|
|
||
| FLINT is free software: you can redistribute it and/or modify it under | ||
| the terms of the GNU Lesser General Public License (LGPL) as published | ||
| by the Free Software Foundation; either version 3 of the License, or | ||
| (at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| #include "nmod.h" | ||
| #include "nmod_vec.h" | ||
| #include "nmod_poly.h" | ||
|
|
||
| void | ||
| nmod_poly_interpolate_geometric_nmod_vec_fast_precomp(nmod_poly_t poly, nn_srcptr v, | ||
| const nmod_geometric_progression_t G, slong len) | ||
| { | ||
| slong i, N; | ||
| nmod_poly_t f, h; | ||
| nmod_t mod; | ||
|
|
||
| N = G->d; | ||
| FLINT_ASSERT(len == N); | ||
| nmod_poly_fit_length(poly, N); | ||
| nmod_poly_zero(poly); | ||
|
|
||
| if (N == 1) | ||
| { | ||
| nmod_poly_set_coeff_ui(poly, 0, v[0]); | ||
| return; | ||
| } | ||
|
|
||
| mod = G->mod; | ||
| nmod_poly_init2(f, mod.n, N); | ||
| nmod_poly_init2(h, mod.n, N); | ||
|
|
||
| for(i = 0; i < N; i++) | ||
| { | ||
| nmod_poly_set_coeff_ui(f, i, nmod_mul(v[i], G->w[i], mod)); | ||
| } | ||
|
|
||
| nmod_poly_mullow(h, f, G->g1, N); | ||
|
|
||
| for (i = 0; i < N; i++) | ||
| { | ||
| nmod_poly_set_coeff_ui(f, N - 1 - i, nmod_mul(nmod_poly_get_coeff_ui(h, i), G->y[i], mod)); | ||
| } | ||
|
|
||
| nmod_poly_mullow(h, f, G->g2, N); | ||
|
|
||
| for (i = 0; i < N; i++) | ||
| { | ||
| nmod_poly_set_coeff_ui(poly, i, nmod_mul(nmod_poly_get_coeff_ui(h, N - 1 - i), G->z[i], mod)); | ||
| } | ||
|
|
||
| nmod_poly_clear(f); | ||
| nmod_poly_clear(h); | ||
| } | ||
|
|
||
| void | ||
| nmod_poly_interpolate_geometric_nmod_vec_fast(nmod_poly_t poly, ulong r, | ||
| nn_srcptr ys, slong n) | ||
| { | ||
| nmod_geometric_progression_t G; | ||
| nmod_geometric_progression_init(G, r, n, poly->mod); | ||
|
|
||
| nmod_poly_interpolate_geometric_nmod_vec_fast_precomp(poly, ys, G, n); | ||
| nmod_geometric_progression_clear(G); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,6 +88,17 @@ nmod_mpoly_factor_struct; | |
|
|
||
| typedef nmod_mpoly_factor_struct nmod_mpoly_factor_t[1]; | ||
|
|
||
| typedef struct | ||
| { | ||
| nn_ptr x, t, w, y, z; // five vectors of precomputed constants | ||
| nmod_poly_t f, g1, g2; // three precomputed polys | ||
| nmod_t mod; | ||
| slong d; // number of points | ||
|
|
||
| } nmod_geometric_progression_struct; | ||
|
|
||
| typedef nmod_geometric_progression_struct nmod_geometric_progression_t[1]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering whether these typedefs that should go here or in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I followed as close as possible the structure for |
||
|
|
||
| typedef enum | ||
| { | ||
| _DOT0 = 0, /* len == 0 || mod.n == 1 */ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.