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

Composite number for nthroot_mod #18199

Merged
merged 8 commits into from
Jan 9, 2020
Merged

Conversation

abhinav-anand-addepar
Copy link
Member

@abhinav-anand-addepar abhinav-anand-addepar commented Jan 1, 2020

Fixes #17373
Fixes #17377
Fixes #18212

  • ntheory
    • nthroot_mod now supports composite moduli

@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:

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.

Fixes #17373
Fixes #17377
Fixes #18212

<!-- BEGIN RELEASE NOTES -->
* ntheory
  * `nthroot_mod` now supports composite moduli
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@abhinav-anand-addepar abhinav-anand-addepar changed the title Composite number for nsqrt_mod Composite number for nthroot_mod Jan 1, 2020
@abhinav-anand-addepar
Copy link
Member Author

@smichr Could you please look into this.

@czgdp1807
Copy link
Member

czgdp1807 commented Jan 2, 2020

There is a lot of branching in the code using continue. Is it possible to figure out a way to write the code without branching using continue.

@Sc0rpi0n101
Copy link
Member

Sc0rpi0n101 commented Jan 3, 2020

I think a better release notes entry will be:

  • ntheory
    • Improved nthroot_mod to support composite moduli

The release notes are just meant to notify the users of major changes in the releases. If you want to include a reference to an important algorithm or method, you should do it in the documentation or a wiki guide, not the release notes.

@abhinav-anand-addepar
Copy link
Member Author

Don't know why the check failed.

@abhinav-anand-addepar
Copy link
Member Author

dont know why, but it was not able to import crt when at start of file.

@jksuom
Copy link
Member

jksuom commented Jan 3, 2020

It seems that crt should be imported in the function because of circularity.

tot_roots = set()
if e == 1:
root = nthroot_mod(a, n, p, True) or []
if (pow(p, n) - a) % p == 0:
Copy link
Member

Choose a reason for hiding this comment

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

nthroot_mod should return also this case.

Copy link
Member Author

Choose a reason for hiding this comment

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

No is doesn't. nthroot_mod(0, 5, 17, True) returns None but it should return [0]. see issue #18212

Copy link
Member

Choose a reason for hiding this comment

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

So let's get the PR that fixes that in first.

Copy link
Member Author

Choose a reason for hiding this comment

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

i am working on it. Will send it by tomorrow.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think is_nthpow_residue(a, n, m) works only when m is prime. Because is_nthpow_residue(36010, 8, 87382) returns false but it has solutions [40208, 47174]

@abhinav-anand-addepar
Copy link
Member Author

I think i have solved the issue #18212. when p is prime and a % p == 0, then [0] is the only root.
This i have taken care in nthroot_modd and when p is not prime then _nthroot_mod_composite handles it.

@abhinav-anand-addepar
Copy link
Member Author

abhinav-anand-addepar commented Jan 3, 2020

But we still have to fix is_nthpow_residue(a, n, m) because currently it only works for prime m otherwise it returns wrong answer. I confirmed it -> theorem 1.2.

@abhinav-anand-addepar
Copy link
Member Author

abhinav-anand-addepar commented Jan 4, 2020

what happened? Is everything okay?

@smichr
Copy link
Member

smichr commented Jan 4, 2020

When this happens, I rebase locally on a fresh copy of master, create a diff, check out master, delete my branch and recreate it from master and then apply the diff and commit it:

git merge master
git diff master > dif
git checkout master
git branch -D composite
git checkout -b composite
git apply dif
git commit -am "composite number for nthroot_mod"

@codecov
Copy link

codecov bot commented Jan 4, 2020

Codecov Report

Merging #18199 into master will increase coverage by 0.027%.
The diff coverage is 100%.

@@              Coverage Diff              @@
##            master    #18199       +/-   ##
=============================================
+ Coverage   74.945%   74.972%   +0.027%     
=============================================
  Files          642       649        +7     
  Lines       167182    167379      +197     
  Branches     39340     39393       +53     
=============================================
+ Hits        125296    125489      +193     
+ Misses       36359     36347       -12     
- Partials      5527      5543       +16

@smichr
Copy link
Member

smichr commented Jan 5, 2020

we still have to fix is_nthpow_residue(a, n, m)

Does this still need to be done?

@abhinav-anand-addepar
Copy link
Member Author

we still have to fix is_nthpow_residue(a, n, m)

Does this still need to be done?

I will send a separate pr regarding is_nthpow_residue(a, n, m).

@smichr
Copy link
Member

smichr commented Jan 5, 2020

I will send a separate pr

this can be committed without that, right?

@abhinav-anand-addepar
Copy link
Member Author

I will send a separate pr

this can be committed without that, right?

yes

diff = (n * pow(root, n - 1)) % p
if diff != 0:
for j in range(1, e):
root = (root - (pow(root, n) - a)* mod_inverse(diff, p)) % pow(p, j + 1)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
root = (root - (pow(root, n) - a)* mod_inverse(diff, p)) % pow(p, j + 1)
root = (root - (rootn - a)* mod_inverse(diff, p)) % ppow

tot_roots.update(nthroot_mod(a, n, p, True) or [])
else:
for root in nthroot_mod(a, n, p, True) or []:
diff = (n * pow(root, n - 1)) % p
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
diff = (n * pow(root, n - 1)) % p
rootn = pow(root, n)
diff = (rootn//root*n) % p

Copy link
Member Author

Choose a reason for hiding this comment

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

root can be zero , so its better to use pow(root, n-1) and avoid division by root. I have made the changes.

@smichr
Copy link
Member

smichr commented Jan 6, 2020

Just trying to factor out the power calculations. See what you think.

@smichr
Copy link
Member

smichr commented Jan 9, 2020

OK, thanks for this work -- it's in!

@smichr smichr merged commit 4bdfba0 into sympy:master Jan 9, 2020
@jksuom
Copy link
Member

jksuom commented Jan 9, 2020

I think that more work is needed in the prime power modulus case.

In [11]: nthroot_mod(4, 4, 27, True)
Out[11]: [mpz(7), mpz(8)]

In [12]: 7**4 % 27
Out[12]: 25

In [13]: 8**4 % 27
Out[13]: 19

ppow = p
for j in range(1, e):
ppow *= p
root = (root - (rootn - a) * mod_inverse(diff, p)) % ppow
Copy link
Member

Choose a reason for hiding this comment

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

This line should probably be modified.

Copy link
Member Author

Choose a reason for hiding this comment

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

yes you are right, the rootn will change with change in root

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants