-
Notifications
You must be signed in to change notification settings - Fork 30
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
Add terms
kwarg to irreducible and primitive poly functions
#463
Conversation
2aab4da
to
33b3449
Compare
Codecov ReportBase: 96.39% // Head: 96.40% // Increases project coverage by
Additional details and impacted files@@ Coverage Diff @@
## release/0.3.x #463 +/- ##
=================================================
+ Coverage 96.39% 96.40% +0.01%
=================================================
Files 43 43
Lines 5624 5701 +77
=================================================
+ Hits 5421 5496 +75
- Misses 203 205 +2
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
723f242
to
ae1f535
Compare
ae1f535
to
5b04764
Compare
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.
Looks good to me! I will start testing things with the polynomials from the databases.
src/galois/_polys/_primitive.py
Outdated
else: | ||
if terms == "min": | ||
terms = _minimum_terms(order, degree, "is_primitive") | ||
poly = _random_search(order, degree, terms) |
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.
Maybe a future improvement for performance is not to use a random search when terms="min"
? Or is this the best known algorithm?
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.
Good point.
I struggled a lot getting an efficient iteration through only the polynomials with t
non-zero terms. I used combinations
and permutations
from itertools
, but was never able to get the iteration order in precisely lexicographical order. So, instead, I created this recursive algorithm to iterate in lexicographical order.
But now that you mention this, I could resurrect my combinations
and permutations
code, add an extra shuffle, and iterate through that for the method="random"
and fixed-term case. I'll work on that.
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.
Probably it's also a good idea to check how people using NTL find these polynomials?
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, that could be helpful.
src/galois/_polys/_primitive.py
Outdated
try: | ||
if method == "min": | ||
poly = next(primitive_polys(order, degree, terms)) | ||
elif method == "max": | ||
poly = next(primitive_polys(order, degree, terms, reverse=True)) | ||
else: | ||
if terms == "min": | ||
terms = _minimum_terms(order, degree, "is_primitive") | ||
poly = _random_search(order, degree, terms) |
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.
What do you think about moving the if terms == "min"
to the top? I would like to only include the code to search in the database in one place, but as it is, if a user writes, for example, primitive_poly(7, 129, terms="min")
, it would still enter the first if method="min"
.
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.
I have a commit I'm about to push. Can you let me know your suggested change to my latest?
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.
As an alternative, you could also change the default value of method
to "random"
when the user specifies terms="min"
. What do you think?
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.
This was the kind of thing I had in mind for the database lookup. What are your thoughts?
try:
if terms == "min" and method == "min":
try:
# Database lookup
return poly
except LookupError:
pass
if method == "min":
return next(irreducible_polys(order, degree, terms))
if method == "max":
return next(irreducible_polys(order, degree, terms, reverse=True))
# Random search
if terms is None:
return next(_random_search(order, degree, "is_irreducible"))
if terms == "min":
terms = _minimum_terms(order, degree, "is_irreducible")
return next(_random_search_fixed_terms(order, degree, terms, "is_irreducible"))
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.
That works too, yes!
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.
Actually, even better is this:
if terms is "min" and method == "min":
try:
# Database lookup
return poly
except LookupError:
pass
try:
if method == "min":
return next(irreducible_polys(order, degree, terms))
if method == "max":
return next(irreducible_polys(order, degree, terms, reverse=True))
# Random search
if terms is None:
return next(_random_search(order, degree, "is_irreducible"))
if terms == "min":
terms = _minimum_terms(order, degree, "is_irreducible")
return next(_random_search_fixed_terms(order, degree, terms, "is_irreducible"))
except StopIteration as e:
terms_str = "any" if terms is None else str(terms)
raise RuntimeError(
f"No monic irreducible polynomial of degree {degree} over GF({order}) with {terms_str} terms exists."
) from e
bc30bb7
to
ec4289f
Compare
@iyanmv I think this branch is good-to-go. If you agree, I'll merge into |
Sure, go ahead! |
Implements #458.
Examples
Examples related to #464