-
-
Notifications
You must be signed in to change notification settings - Fork 656
Implement Square Roots to FiniteFields Category #40401
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
base: develop
Are you sure you want to change the base?
Conversation
Added the following methods to the finite field category - quadratic_non_residue - square_root and sqrt - _tonelli and _cipolla: the algorithm for finding a square root - is_square Moved is_square and sqrt methods from element.pyx to their proper categories.
Brian did this work as part of an undergrad mentorship program at the University of Calgary, and I was the grad student mentoring him. I'll review this for mathematical correctness/style/documentation/etc., but since I worked closely with him on this I'd like a second person to review it as well (especially for the refactoring that was done to move the default square root implementation out of element.pyx and into the appropriate category). |
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.
First pass, mostly style things and wording in the documentation.
Co-authored-by: Vincent Macri <[email protected]>
Co-authored-by: Vincent Macri <[email protected]>
Co-authored-by: Vincent Macri <[email protected]>
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.
Docstring fixes
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.
Docstring formatting
Co-authored-by: Vincent Macri <[email protected]>
Not completely related to this change: @tobiasdiez another |
do fix the lints. Also, build the HTML files locally (the CI for HTML preview is currently broken) and check that they're rendered correctly. They're incorrect in many places. Check some existing docstring for the formatting. I think first word should be in infinitive form, first sentence has full stop, and no bullet point in output if only one output. (Do verify that.) Refer to #39798 . First character of error message should not be capitalized. What's the algorithm for computing square root in e.g. |
Yes, ruff is not supporting cython. |
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.
Docstrings, cleanup.
Curious, did you approve the CI run? I don't have permission to approve CI runs was wondering who did (so I can ping them to do it again).
That's annoying that the CI is broken. The process to build the docs with Meson is not very well-documented (I should get around to filing a bug report about that eventually...), but I eventually figured it out and sent the instructions to Brian.
I'm not too sure what you mean by wanting to distinguish with the naming, but I don't think we need to worry about it. In terms of what algorithm That said, I do think that at least for |
Co-authored-by: Vincent Macri <[email protected]>
Failures seem irrelevant. I'm going to try building locally. |
The failed CI runs work fine on my Fedora machine, so i think the failures are just the CI being flaky. (I didn't test the PDF build since I think that's broken already, and I didn't test Windows because I don't have a Windows machine.) @fchapoton @user202729 do either of you want to set this to positive review? |
currently for Zmod(p) the method is named |
Not sure why GitHub says I'm requesting changes. I agree with the other reviewer's suggestions and do not have additional changes that I want to request. |
It seems to work fine now. In case this comes up again in the future, I think it was needed before because @Brian-Heckel was added to the SageMath organization after the commit was pushed. It seems that adding someone to the SageMath organization does not automatically approve pending CI runs, but does automatically approve future CI runs. |
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.
No further comments (does this make GitHub stop complaining about me requesting changes?)
would you edit the labels set 'positive review" one? |
As I said (much) earlier in this thread, I think I worked too closely with Brian to be comfortable giving this a positive review myself. While Brian did write the code himself, I think it's a bit of a conflict of interest for me to approve it alone.
I'm planning to use a different workflow for these situations in the future to make this process a bit faster/less confusing. |
okay, unless @fchapoton has other comments. |
The old In theory this could slow down calls to Out of scope for this PR, but Brian and I did talk about the possibility of implementing some of the category methods in Cython, I posted about it on sage-devel a while ago but didn't get any response. If you have any ideas/thoughts/knowledge on that feel free to reply there. |
Thanks for the review @user202729! |
Point is cython generates a vtable for each declared method of cdef class, so if something overrides it, the overridden method can be quickly called (just lookup the function pointer from the vtable and call it, instead of going through Python getattr) if you know the object is an instance of the parent class. |
As I understand it (I may be wrong), if a method is implemented in a class then it replaces (not overrides) the method from the category framework. So I think anything that implements |
Yes.
No, Cython code that does
Speaking of this, there are some small issues (Python-implemented method "cannot" override Cython-implemented method), but in principle they should be all solvable. In any case, if the methods themselves are expensive (rather than you want to optimize the overhead of calling a Python method), an easy way out is to implement a helper library in pyx, then make the category method call that. I haven't looked at the situation too closely, however. |
I'm don't quite understand what you mean. Here's an example of what I meant.
I think it's more an issue of the function call overhead. At least in this PR the underlying arithmetic is still handled by Cython or compiled libraries.
I've considered that too, but was surprised to see there is very little Cython code in the category framework. Not sure if this is because of technical limitations that I would encounter if I tried or if it's just because nobody has gotten around to it yet. |
I stand corrected. There are some code in the category framework that manually does what this does automatically e.g.
in |
sagemathgh-40401: Implement Square Roots to FiniteFields Category <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Fixes sagemath#39688 Fixes sagemath#39798 Added the following methods to the finite field category - `quadratic_non_residue()` - `square_root()` and `sqrt()` - `_tonelli()` and `_cipolla()`: the algorithms for finding a square root - `is_square()` Moved `is_square()` and `sqrt()` methods from element.pyx to their proper categories. Most profiling tests showed that Tonelli's algorithm outperforms Cipolla. However a profile with a Finite Field with order of a Cullen prime resulted in Cipolla outperforming Tonelli. I'm running the meson build and currently cannot find the way to build the html documentation. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#40401 Reported by: Brian Heckel Reviewer(s): Brian Heckel, Frédéric Chapoton, Vincent Macri
sagemathgh-40401: Implement Square Roots to FiniteFields Category <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> Fixes sagemath#39688 Fixes sagemath#39798 Added the following methods to the finite field category - `quadratic_non_residue()` - `square_root()` and `sqrt()` - `_tonelli()` and `_cipolla()`: the algorithms for finding a square root - `is_square()` Moved `is_square()` and `sqrt()` methods from element.pyx to their proper categories. Most profiling tests showed that Tonelli's algorithm outperforms Cipolla. However a profile with a Finite Field with order of a Cullen prime resulted in Cipolla outperforming Tonelli. I'm running the meson build and currently cannot find the way to build the html documentation. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#40401 Reported by: Brian Heckel Reviewer(s): Brian Heckel, Frédéric Chapoton, Vincent Macri
Fixes #39688
Fixes #39798
Added the following methods to the finite field category
quadratic_non_residue()
square_root()
andsqrt()
_tonelli()
and_cipolla()
: the algorithms for finding a square rootis_square()
Movedis_square()
andsqrt()
methods from element.pyx to their proper categories.Most profiling tests showed that Tonelli's algorithm outperforms Cipolla. However a profile with a Finite Field with order of a Cullen prime resulted in Cipolla outperforming Tonelli.
I'm running the meson build and currently cannot find the way to build the html documentation.
📝 Checklist
⌛ Dependencies