Skip to content
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

extended solveset for polynomial congruence #18402

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 15 additions & 3 deletions sympy/solvers/solveset.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from sympy.matrices import Matrix, MatrixBase
from sympy.ntheory import totient
from sympy.ntheory.factor_ import divisors
from sympy.ntheory.residue_ntheory import discrete_log, nthroot_mod
from sympy.ntheory.residue_ntheory import discrete_log, nthroot_mod, polynomial_congruence
from sympy.polys import (roots, Poly, degree, together, PolynomialError,
RootOf, factor)
from sympy.polys.polyerrors import CoercionFailed
Expand Down Expand Up @@ -1175,7 +1175,6 @@ def _invert_modular(modterm, rhs, n, symbol):
if g is not S.One:
x_indep_term = rhs*invert(g, m)
return _invert_modular(Mod(h, m), Mod(x_indep_term, m), n, symbol)

if a.is_Pow:
# base**expo = a
base, expo = a.args
Expand Down Expand Up @@ -1280,7 +1279,20 @@ def _solve_modular(f, symbol, domain):
return unsolved_result

n = Dummy('n', integer=True)
f_x, g_n = _invert_modular(modterm, rhs, n, symbol)

if modterm.args[0].is_polynomial():
a, m = modterm.args
f_x = symbol
g_n = EmptySet
if abs(rhs) < abs(m):
try:
remainder_list = polynomial_congruence(a - rhs, int(m))
for rem in remainder_list:
g_n += ImageSet(Lambda(n, m*n + rem), S.Integers)
except (ValueError, NotImplementedError):
return unsolved_result
else:
f_x, g_n = _invert_modular(modterm, rhs, n, symbol)

if f_x is modterm and g_n is rhs:
return unsolved_result
Expand Down
8 changes: 8 additions & 0 deletions sympy/solvers/tests/test_solveset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2253,6 +2253,14 @@ def test_solve_modular():
# domain intersection
assert solveset(3 - Mod(5*x - 8, 7), x, S.Naturals0) == \
Intersection(ImageSet(Lambda(n, 7*n + 5), S.Integers), S.Naturals0)

assert solveset(Mod(x**16 - 7, 36) - 2, x, S.Integers) == \
Union(ImageSet(Lambda(n, 36*n + 3), S.Integers),
ImageSet(Lambda(n, 36*n + 9), S.Integers),
ImageSet(Lambda(n, 36*n + 15), S.Integers),
ImageSet(Lambda(n, 36*n + 21), S.Integers),
ImageSet(Lambda(n, 36*n + 27), S.Integers),
ImageSet(Lambda(n, 36*n + 33), S.Integers))
# Complex args
assert solveset(Mod(x, 3) - I, x, S.Integers) == \
EmptySet()
Expand Down