-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1766 from flintlib/divexact2
Implement fmpz_poly_divexact
- Loading branch information
Showing
23 changed files
with
239 additions
and
50 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
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
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
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,108 @@ | ||
/* | ||
Copyright (C) 2024 Fredrik Johansson | ||
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 2.1 of the License, or | ||
(at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "fmpz.h" | ||
#include "fmpz_vec.h" | ||
#include "fmpz_poly.h" | ||
#include "gr.h" | ||
#include "gr_poly.h" | ||
|
||
void | ||
_fmpz_poly_divexact(fmpz * Q, const fmpz * A, slong lenA, | ||
const fmpz * B, slong lenB) | ||
{ | ||
slong lenQ = lenA - lenB + 1; | ||
|
||
if (lenQ == 1) | ||
{ | ||
fmpz_divexact(Q, A + lenA - 1, B + lenB - 1); | ||
} | ||
else if (lenB == 1) | ||
{ | ||
if (fmpz_is_pm1(B)) | ||
_fmpz_vec_scalar_mul_fmpz(Q, A, lenA, B); | ||
else | ||
_fmpz_vec_scalar_divexact_fmpz(Q, A, lenA, B); | ||
} | ||
else if (lenQ <= 100 || lenB <= 16) | ||
{ | ||
gr_ctx_t ctx; | ||
gr_ctx_init_fmpz(ctx); | ||
GR_MUST_SUCCEED(_gr_poly_divexact_basecase_bidirectional(Q, A, lenA, B, lenB, ctx)); | ||
} | ||
else | ||
{ | ||
/* todo: the true cutoffs are sometimes higher, especially with | ||
unbalanced operands, possibly because divconquer division needs tuning */ | ||
slong A_bits, B_bits, B_cutoff, Q_cutoff; | ||
gr_ctx_t ctx; | ||
gr_ctx_init_fmpz(ctx); | ||
|
||
A_bits = _fmpz_vec_max_bits(A, lenQ); | ||
B_bits = _fmpz_vec_max_bits(B, FLINT_MIN(lenB, lenQ)); | ||
A_bits = FLINT_ABS(A_bits); | ||
B_bits = FLINT_ABS(B_bits); | ||
|
||
B_cutoff = (B_bits > 3000) ? 20 : 60; | ||
Q_cutoff = (A_bits > 1000) ? 100 : 200; | ||
|
||
if (A_bits >= 100 * B_bits) | ||
{ | ||
Q_cutoff *= 2; | ||
B_cutoff *= 2; | ||
} | ||
|
||
if (lenQ <= Q_cutoff || lenB <= B_cutoff) | ||
GR_MUST_SUCCEED(_gr_poly_divexact_basecase_bidirectional(Q, A, lenA, B, lenB, ctx)); | ||
else | ||
_fmpz_poly_div(Q, A, lenA, B, lenB, 0); | ||
} | ||
|
||
} | ||
|
||
void | ||
fmpz_poly_divexact(fmpz_poly_t Q, const fmpz_poly_t A, const fmpz_poly_t B) | ||
{ | ||
fmpz_poly_t T; | ||
slong lenA = A->length; | ||
slong lenB = B->length; | ||
slong lenQ = lenA - lenB + 1; | ||
|
||
if (lenB == 0) | ||
{ | ||
flint_throw(FLINT_ERROR, "Exception (fmpz_poly_divexact). Division by zero.\n"); | ||
} | ||
|
||
if (lenA < lenB) | ||
{ | ||
fmpz_poly_zero(Q); | ||
return; | ||
} | ||
|
||
if (Q == A || Q == B) | ||
{ | ||
fmpz_poly_init2(T, lenQ); | ||
_fmpz_poly_divexact(T->coeffs, A->coeffs, lenA, B->coeffs, lenB); | ||
_fmpz_poly_set_length(T, lenQ); | ||
fmpz_poly_swap(T, Q); | ||
fmpz_poly_clear(T); | ||
} | ||
else | ||
{ | ||
fmpz_poly_fit_length(Q, lenQ); | ||
_fmpz_poly_divexact(Q->coeffs, A->coeffs, lenA, B->coeffs, lenB); | ||
_fmpz_poly_set_length(Q, lenQ); | ||
} | ||
|
||
/* should not be needed, but produce something normalised in case | ||
this was called with invalid input */ | ||
_fmpz_poly_normalise(Q); | ||
} |
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
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,72 @@ | ||
/* | ||
Copyright (C) 2009 William Hart | ||
Copyright (C) 2010 Sebastian Pancratz | ||
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 2.1 of the License, or | ||
(at your option) any later version. See <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "test_helpers.h" | ||
#include "fmpz.h" | ||
#include "fmpz_poly.h" | ||
#include "ulong_extras.h" | ||
|
||
TEST_FUNCTION_START(fmpz_poly_divexact, state) | ||
{ | ||
int i, result; | ||
|
||
for (i = 0; i < 1000 * flint_test_multiplier(); i++) | ||
{ | ||
fmpz_poly_t a, b, c, q; | ||
int aliasing; | ||
|
||
fmpz_poly_init(a); | ||
fmpz_poly_init(b); | ||
fmpz_poly_init(c); | ||
fmpz_poly_init(q); | ||
|
||
fmpz_poly_randtest(a, state, 1 + n_randint(state, 100), 1 + n_randint(state, 200)); | ||
fmpz_poly_randtest_not_zero(b, state, 1 + n_randint(state, 100), 1 + n_randint(state, 200)); | ||
fmpz_poly_mul(c, a, b); | ||
aliasing = n_randint(state, 3); | ||
|
||
if (aliasing == 0) | ||
{ | ||
fmpz_poly_divexact(q, c, b); | ||
} | ||
else if (aliasing == 1) | ||
{ | ||
fmpz_poly_set(q, c); | ||
fmpz_poly_divexact(q, q, b); | ||
} | ||
else | ||
{ | ||
fmpz_poly_set(q, b); | ||
fmpz_poly_divexact(q, c, q); | ||
} | ||
|
||
result = fmpz_poly_equal(q, a); | ||
|
||
if (!result) | ||
{ | ||
flint_printf("FAIL:\n"); | ||
fmpz_poly_print(a), flint_printf("\n\n"); | ||
fmpz_poly_print(b), flint_printf("\n\n"); | ||
fmpz_poly_print(c), flint_printf("\n\n"); | ||
fmpz_poly_print(q), flint_printf("\n\n"); | ||
fflush(stdout); | ||
flint_abort(); | ||
} | ||
|
||
fmpz_poly_clear(a); | ||
fmpz_poly_clear(b); | ||
fmpz_poly_clear(c); | ||
fmpz_poly_clear(q); | ||
} | ||
|
||
TEST_FUNCTION_END(state); | ||
} |
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
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
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
Oops, something went wrong.