-
Notifications
You must be signed in to change notification settings - Fork 276
Add nmod poly divrem with divisor $x^n - c$ #2470
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
Draft
vneiger
wants to merge
9
commits into
flintlib:main
Choose a base branch
from
vneiger:add_nmod_poly_special_rem
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
885aab9
Add nmod_poly_divrem specialized to divisor x**n - c:
vneiger 523b2c3
add special cases for c == -1, c == +1, c == 0
vneiger 33b5bed
profile: add special cases for c == -1, c == +1, c == 0
vneiger f0558bb
add variants with precomputation when modulus is small enough, tested
vneiger d535b58
update profile file
vneiger 7ac03bb
small fixes and update documentation
vneiger 4bf1d0a
fix typo in test file
vneiger eca69f8
add rem with special divisor, along with tests and profile
vneiger ed77123
add try_sparse comparison
vneiger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,285 @@ | ||
| /* | ||
| Copyright (C) 2025 Vincent Neiger | ||
|
|
||
| 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 "flint.h" */ | ||
| #include "nmod_poly.h" | ||
| #include "nmod_vec.h" | ||
| #include "ulong_extras.h" | ||
|
|
||
| /* division by x**n - 1 */ | ||
| void _nmod_poly_divrem_xnm1(nn_ptr RQ, nn_srcptr A, slong len, ulong n, ulong modn) | ||
| { | ||
| /* assumes len >= n */ | ||
| slong i; | ||
| ulong j, r; | ||
|
|
||
| if (RQ != A) | ||
| for (j = 0; j < n; j++) | ||
| RQ[len-n+j] = A[len-n+j]; | ||
|
|
||
| r = len % n; | ||
| i = len - r - n; /* multiple of n, >= 0 by assumption */ | ||
|
|
||
| for (j = 0; j < r; j++) | ||
| RQ[i+j] = n_addmod(RQ[i+n+j], A[i+j], modn); | ||
|
|
||
| i -= n; | ||
| while (i >= 0) | ||
| { | ||
| for (j = 0; j < n; j++) | ||
| RQ[i+j] = n_addmod(RQ[i+n+j], A[i+j], modn); | ||
| i -= n; | ||
| } | ||
| } | ||
|
|
||
| /* division by x**n + 1 */ | ||
| void _nmod_poly_divrem_xnp1(nn_ptr RQ, nn_srcptr A, slong len, ulong n, ulong modn) | ||
| { | ||
| /* assumes len >= n */ | ||
| slong i; | ||
| ulong j, r; | ||
|
|
||
| if (RQ != A) | ||
| for (j = 0; j < n; j++) | ||
| RQ[len-n+j] = A[len-n+j]; | ||
|
|
||
| r = len % n; | ||
| i = len - r - n; /* multiple of n, >= 0 by assumption */ | ||
|
|
||
| for (j = 0; j < r; j++) | ||
| RQ[i+j] = n_submod(A[i+j], RQ[i+n+j], modn); | ||
|
|
||
| i -= n; | ||
| while (i >= 0) | ||
| { | ||
| for (j = 0; j < n; j++) | ||
| RQ[i+j] = n_submod(A[i+j], RQ[i+n+j], modn); | ||
| i -= n; | ||
| } | ||
| } | ||
|
|
||
| /* division by x**n - c, general variant */ | ||
| void _nmod_poly_divrem_xnmc(nn_ptr RQ, nn_srcptr A, slong len, ulong n, ulong c, nmod_t mod) | ||
| { | ||
| /* assumes len >= n */ | ||
| slong i; | ||
| ulong j, r, val; | ||
|
|
||
| if (RQ != A) | ||
| for (j = 0; j < n; j++) | ||
| RQ[len-n+j] = A[len-n+j]; | ||
|
|
||
| r = len % n; | ||
| i = len - r - n; /* multiple of n, >= 0 by assumption */ | ||
|
|
||
| for (j = 0; j < r; j++) | ||
| { | ||
| val = nmod_mul(RQ[i+n+j], c, mod); | ||
| RQ[i+j] = n_addmod(val, A[i+j], mod.n); | ||
| } | ||
|
|
||
| i -= n; | ||
| while (i >= 0) | ||
| { | ||
| for (j = 0; j < n; j++) | ||
| { | ||
| val = nmod_mul(RQ[i+n+j], c, mod); | ||
| RQ[i+j] = n_addmod(val, A[i+j], mod.n); | ||
| } | ||
| i -= n; | ||
| } | ||
| } | ||
|
|
||
| /* division by x**n - c, with precomputation on c */ | ||
| /* constraint: modn < 2**(FLINT_BITS-1) */ | ||
| void _nmod_poly_divrem_xnmc_precomp(nn_ptr RQ, nn_srcptr A, slong len, ulong n, ulong c, ulong c_precomp, ulong modn) | ||
| { | ||
| /* assumes len >= n */ | ||
| slong i; | ||
| ulong j, r, val; | ||
|
|
||
| if (RQ != A) | ||
| for (j = 0; j < n; j++) | ||
| RQ[len-n+j] = A[len-n+j]; | ||
|
|
||
| r = len % n; | ||
| i = len - r - n; /* multiple of n, >= 0 by assumption */ | ||
|
|
||
| for (j = 0; j < r; j++) | ||
| { | ||
| val = n_mulmod_shoup(c, RQ[i+n+j], c_precomp, modn); | ||
| RQ[i+j] = n_addmod(val, A[i+j], modn); | ||
| } | ||
|
|
||
| i -= n; | ||
| while (i >= 0) | ||
| { | ||
| for (j = 0; j < n; j++) | ||
| { | ||
| val = n_mulmod_shoup(c, RQ[i+n+j], c_precomp, modn); | ||
| RQ[i+j] = n_addmod(val, A[i+j], modn); | ||
| } | ||
| i -= n; | ||
| } | ||
| } | ||
|
|
||
| /* division by x**n - c, lazy with precomputation */ | ||
| /* constraint: max(A) + 2*modn <= 2**FLINT_BITS */ | ||
| /* coeff bounds: in [0, max(A)] | out [0, max(A) + 2*modn) */ | ||
| void _nmod_poly_divrem_xnmc_precomp_lazy(nn_ptr RQ, nn_srcptr A, slong len, ulong n, ulong c, ulong c_precomp, ulong modn) | ||
| { | ||
| /* assumes len >= n */ | ||
| slong i; | ||
| ulong j, r, val, p_hi, p_lo; | ||
|
|
||
| if (RQ != A) | ||
| for (j = 0; j < n; j++) | ||
| RQ[len-n+j] = A[len-n+j]; | ||
|
|
||
| r = len % n; | ||
| i = len - r - n; /* multiple of n, >= 0 by assumption */ | ||
|
|
||
| for (j = 0; j < r; j++) | ||
| { | ||
| /* computes either val = (c*val mod n) or val = (c*val mod n) + n */ | ||
| val = RQ[i+n+j]; | ||
| umul_ppmm(p_hi, p_lo, c_precomp, val); | ||
| val = c * val - p_hi * modn; | ||
| /* lazy addition, yields RQ[i+j] in [0..k+2n), where max(RQ) <= k */ | ||
| RQ[i+j] = val + A[i+j]; | ||
| } | ||
|
|
||
| i -= n; | ||
| while (i >= 0) | ||
| { | ||
| for (j = 0; j < n; j++) | ||
| { | ||
| /* computes either val = (c*val mod n) or val = (c*val mod n) + n */ | ||
| val = RQ[i+n+j]; | ||
| umul_ppmm(p_hi, p_lo, c_precomp, val); | ||
| val = c * val - p_hi * modn; | ||
| /* lazy addition, yields RQ[i+j] in [0..k+2n), where max(RQ) <= k */ | ||
| RQ[i+j] = val + A[i+j]; | ||
| } | ||
| i -= n; | ||
| } | ||
| } | ||
|
|
||
| void nmod_poly_divrem_xnmc(nmod_poly_t Q, nmod_poly_t R, nmod_poly_t A, ulong n, ulong c) | ||
| { | ||
| const ulong len = A->length; | ||
|
|
||
| if (len <= n) | ||
| { | ||
| nmod_poly_zero(Q); | ||
| nmod_poly_set(R, A); | ||
| return; | ||
| } | ||
|
|
||
| if (c == 0) | ||
| { | ||
| nmod_poly_set_trunc(R, A, n); | ||
| nmod_poly_shift_right(Q, A, n); | ||
| return; | ||
| } | ||
|
|
||
| int lazy = 0; | ||
| nn_ptr RQ = _nmod_vec_init(len); | ||
|
|
||
| /* perform division */ | ||
| if (c == 1) | ||
| _nmod_poly_divrem_xnm1(RQ, A->coeffs, len, n, A->mod.n); | ||
|
|
||
| else if (c == A->mod.n - 1) | ||
| _nmod_poly_divrem_xnp1(RQ, A->coeffs, len, n, A->mod.n); | ||
|
|
||
| /* if degree below the n_mulmod_shoup threshold, */ | ||
| /* or if modulus forbids n_mulmod_shoup usage, use general */ | ||
| #if FLINT_MULMOD_SHOUP_THRESHOLD <= 2 | ||
| else if (A->mod.norm == 0) /* here A->length >= threshold */ | ||
| #else | ||
| else if ((A->length < FLINT_MULMOD_SHOUP_THRESHOLD) | ||
| || (A->mod.norm == 0)) | ||
| #endif | ||
| { | ||
| _nmod_poly_divrem_xnmc(RQ, A->coeffs, len, n, c, A->mod); | ||
| } | ||
|
|
||
| else | ||
| { | ||
| const ulong modn = A->mod.n; | ||
| const ulong c_precomp = n_mulmod_precomp_shoup(c, modn); | ||
|
|
||
| /* if 3*mod.n - 1 <= 2**FLINT_BITS, use precomp+lazy variant */ | ||
| #if FLINT_BITS == 64 | ||
| if (modn <= UWORD(6148914691236517205)) | ||
| #else /* FLINT_BITS == 32 */ | ||
| if (modn <= UWORD(1431655765)) | ||
| #endif | ||
| { | ||
| lazy = 1; | ||
| _nmod_poly_divrem_xnmc_precomp_lazy(RQ, A->coeffs, len, n, c, c_precomp, modn); | ||
| } | ||
|
|
||
| /* use n_mulmod_shoup, non-lazy variant */ | ||
| else | ||
| { | ||
| _nmod_poly_divrem_xnmc_precomp(RQ, A->coeffs, len, n, c, c_precomp, modn); | ||
| } | ||
| } | ||
|
|
||
| /* copy remainder R */ | ||
| nmod_poly_fit_length(R, n); | ||
| if (lazy) | ||
| { | ||
| /* correct excess */ | ||
| const ulong modn = A->mod.n; | ||
| for (ulong i = 0; i < n; i++) | ||
| { | ||
| ulong v = RQ[i]; | ||
| if (v >= 2*modn) | ||
| v -= 2*modn; | ||
| else if (v >= modn) | ||
| v -= modn; | ||
| R->coeffs[i] = v; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| _nmod_vec_set(R->coeffs, RQ, n); | ||
| } | ||
| _nmod_poly_set_length(R, n); | ||
| _nmod_poly_normalise(R); | ||
|
|
||
| /* copy quotient Q */ | ||
| nmod_poly_fit_length(Q, len - n); | ||
| if (lazy) | ||
| { | ||
| /* correct excess */ | ||
| const ulong modn = A->mod.n; | ||
| for (ulong i = 0; i < len - n; i++) | ||
| { | ||
| ulong v = RQ[n+i]; | ||
| if (v >= 2*modn) | ||
| v -= 2*modn; | ||
| else if (v >= modn) | ||
| v -= modn; | ||
| Q->coeffs[i] = v; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| _nmod_vec_set(Q->coeffs, RQ + n, len - n); | ||
| } | ||
| _nmod_poly_set_length(Q, len - n); | ||
|
|
||
| _nmod_vec_clear(RQ); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A temporary allocation would be good to try here. @fredrik-johansson how do we deal with stack allocation for these types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like what is at lines 89--94 of
nmod_poly/rem.c?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I believe so!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I will give this a try!