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
base: master
Are you sure you want to change the base?
Conversation
abhinav-anand-addepar
commented
Jan 19, 2020
•
edited
edited
- solvers
- Extended solvers to solve polynomial congruence equation when the domain is subset of integers
✅ Hi, I am the SymPy bot (v149). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.6. Note: This comment will be updated with the latest check if you edit the pull request. You need to reload the page to see it. Click here to see the pull request description that was parsed.
|
@smichr @oscarbenjamin This might not be the best way and it need improvements. |
I don't know this code so well. Maybe @jmig5776 does... |
sympy/solvers/solveset.py
Outdated
@@ -1175,7 +1175,17 @@ 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_polynomial(): |
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.
@abhinav28071999 The main problem with this PR is that you code added in _invert_modular
does not fulfill the goal of _invert_modular
.
Let me explain it briefly.
_invert_modular
is supposed to return the inverted form of the equation so that the solution of inverted form should give all solutions of the original equation. And here it is missing.
sympy/solvers/tests/test_solveset.py
Outdated
@@ -2172,12 +2172,12 @@ def test_invert_modular(): | |||
assert invert_modular(Mod(x + 8, 7), S(5), n, x) == \ | |||
(x, ImageSet(Lambda(n, 7*n + 4), S.Integers)) | |||
assert invert_modular(Mod(x**2 + x, 7), S(5), n, x) == \ | |||
(Mod(x**2 + x, 7), 5) | |||
(x, ImageSet(Lambda(n, 7*n + 3), S.Integers)) |
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.
Let us take this example According to your code
7*n+3
is *all the solutions to Mod(x**2 + x, 7)-5
.
But that's not the case :
Clearly solution of x**2 + x - 5=0
gives one of the solution to this equation.
i.e x = (-1+/- sqrt(21))/2
which is not included in your solution.
I hope you understand the crux of the problem. Try to be creative to solve this problem. Its nice you are working on this. Try to add it in solve_modular.
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 you are right, i get it now. I should implement it to solve_modular
function which is designed to return set of integer solutions
.
It would be very difficult to invert a general polynomial modular equation to general solutions, but it is relatively easier to invert it to only integer solutions.
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.
@jmig5776 One more thing, the current implementation of invert_modular does not seem to give all solution.
For example: invert_modular(Mod(x**4, 7), S(5), n, x)
output (x, EmptySet)
. But 5**0.25 is a solution to Mod(x**4,7) -5
.
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.
Yeah this bug was irritating while we were creating _invert_modular
. Can you try to fix this? Although this is in the further development task of solve_modular
.
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.
@smichr @jmig5776 I don't think that this bug can be solved for a general polynomial. For example: mod(x**2+x-5, 10) =0
, integer solution can be easily calculated but for other solution we have to solve for roots of x**2+x-5 = 10k
where k is any integer. For f(x)=x**n it may be done but for general polynomial this will not be feasible as there will be no relation between the roots for different k. Moreover _invert_modular
function is only used by _solve_modular
, and the return type of _solve_modular
is set of integers solutions so this is suitable for _solve_modular
but not for _invert_modular
.
For now the changes i have made should do the trick for solving polynomial congruence equation. What are your thoughts on this?
@jmig5776 I made changes to the solve_modular function. This might not be the best way. It is impossible to invert a general polynomial modular equation to its all roots, but it is simpler to get its integer solutions. |
Codecov Report
@@ Coverage Diff @@
## master #18402 +/- ##
=============================================
+ Coverage 75.292% 75.386% +0.093%
=============================================
Files 633 637 +4
Lines 166944 167355 +411
Branches 39396 39595 +199
=============================================
+ Hits 125697 126163 +466
+ Misses 35709 35665 -44
+ Partials 5538 5527 -11 |
Then you can use this when the domain is a subset of Integers. |
Add some more description to the PR.
|
@abhinav28071999 Any updates on this? |
@smichr @czgdp1807 @jmig5776 Based on #18402 (comment) I think that when the polynomial equation is used the invert modular inverts the equation only in integer domain. More over modular equations are only meaningful in subset of integer domain. So probably the definition of |
@jmig5776 @smichr Any thoughts on #18402 (comment) ? |
I agree with you that it is extremely difficult to generalize the |