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

Updated nthroot to raise NotImplementedError for composite modulo #18194

Merged
merged 3 commits into from
Jan 2, 2020

Conversation

risubaba
Copy link
Contributor

@risubaba risubaba commented Jan 1, 2020

References to other Issues or PRs

#18172

Brief description of what is fixed or changed

nthroot_mod , primitive_root and _primitive_root_prime_iter have been updated to not work for composite modulo.

Other comments

A more general algorithm to compute nthroot has to be implemented to work with all possible modulo

Release Notes

  • ntheory
    • Make nthroot_mod , primitive_root and _primitive_root_prime_iter incompatible with composite modulo.

@sympy-bot
Copy link

sympy-bot commented Jan 1, 2020

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:

  • ntheory
    • Make nthroot_mod , primitive_root and _primitive_root_prime_iter incompatible with composite modulo. (#18194 by @risubaba)

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.

<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234". See
https://github.com/blog/1506-closing-issues-via-pull-requests . Please also
write a comment on that issue linking back to this pull request once it is
open. -->
#18172 


#### Brief description of what is fixed or changed
`nthroot_mod` , `primitive_root` and `_primitive_root_prime_iter` have been updated to not work for composite modulo.

#### Other comments
A more general algorithm to compute nthroot has to be implemented to work with all possible modulo

#### Release Notes

<!-- Write the release notes for this release below. See
https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more information
on how to write release notes. The bot will check your release notes
automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
* ntheory
  * Make `nthroot_mod` , `primitive_root` and `_primitive_root_prime_iter` incompatible with composite modulo.
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@risubaba
Copy link
Contributor Author

risubaba commented Jan 1, 2020

In [4]: solveset(Mod(x**4,9) - 4, x ,S.Integers)                                                         
Out[4]: ConditionSet(x, Eq(Mod(x**4, 9) - 4, 0), Integers)

This is the output on my terminal


assert solveset(Mod(x**4, 9) - 4, x, S.Integers) == \
            ConditionSet(x, Eq(Mod(x**4, 9) - 4, 0), S.Integers)

This is the test which failed the last time.

@abhinav-anand-addepar
Copy link
Member

Primitive root in sympy can compute for composite number.

@@ -773,6 +777,8 @@ def nthroot_mod(a, n, p, all_roots=False):
# see Hackman "Elementary Number Theory" (2009), page 76
if not is_nthpow_residue(a, n, p):
return None
if isprime(p) is False:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that isprime() will return True or False. Hence this could be if not isprime(p):.

@@ -773,6 +777,8 @@ def nthroot_mod(a, n, p, all_roots=False):
# see Hackman "Elementary Number Theory" (2009), page 76
if not is_nthpow_residue(a, n, p):
return None
if isprime(p) is False:
raise NotImplementedError("Not implemented for composite p")
if primitive_root(p) is None:
raise NotImplementedError("Not Implemented for m without primitive root")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If p is prime, then a primitive root exists. So these two lines can be removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we have to add a primality test for the modulo right. Without it we were getting wrong answers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primality test on line 776 is currently necessary, but if p is prime, then a primitive root exists and the test on line 778 will always fail. So the two lines 778-779 can be removed.

@@ -72,6 +72,8 @@ def _primitive_root_prime_iter(p):

"""
# it is assumed that p is an int
if isprime(p) is False:
raise NotImplementedError("Not implemented for composite p")
Copy link
Member

@jksuom jksuom Jan 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is not necessary as this is a private method and 'prime' is mentioned.

@@ -110,6 +112,8 @@ def primitive_root(p):
p = as_int(p)
if p < 1:
raise ValueError('p is required to be positive')
if isprime(p) is False:
raise NotImplementedError("Not implemented for composite p")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Primitive roots exist also for some composite moduli (e.g. odd prime powers) and the implementation below takes care of those cases as well. So these two lines should be removed.

@codecov
Copy link

codecov bot commented Jan 1, 2020

Codecov Report

Merging #18194 into master will increase coverage by 0.03%.
The diff coverage is 100%.

@@             Coverage Diff              @@
##            master    #18194      +/-   ##
============================================
+ Coverage   74.936%   74.967%   +0.03%     
============================================
  Files          642       642              
  Lines       167128    167128              
  Branches     39331     39331              
============================================
+ Hits        125240    125291      +51     
+ Misses       36351     36303      -48     
+ Partials      5537      5534       -3

@jksuom
Copy link
Member

jksuom commented Jan 2, 2020

Thank you.

@jksuom jksuom merged commit 6dddc8d into sympy:master Jan 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants