-
-
Notifications
You must be signed in to change notification settings - Fork 405
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
Improve performance of __getitem__ of CFiniteSequence #36764
Improve performance of __getitem__ of CFiniteSequence #36764
Conversation
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.
Thank you for your contribution! The code looks good to me. In addition to the two comments I already left, I would suggest that you add a reference to your paper with Alin in the docstring, and maybe an example that shows that the code can handle recurrences of higher order.
It could be nice to also improve the case where one wants a whole slice of the sequence, but maybe not in this PR.
src/sage/rings/cfinite_sequence.py
Outdated
V = Matrix(QQ, d, 1, self._a[:d][::-1]) | ||
(quo, num) = num.quo_rem(den) | ||
P = self.parent().polynomial_ring() | ||
x = self.parent().gen() |
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 find it suspect that CFiniteSequences(ZZ).gen()
returns a polynomial rather than a C-finite sequence, so I would write x = P.gen()
here to be safe.
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.
Thank you. I think both are equivalent in the current implementation as in the definition of gen()
in this file. But, 'P.gen()' is slightly more efficient.
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, I agree they are equivalent, but I view the current definition of gen()
as a bug.
src/sage/rings/cfinite_sequence.py
Outdated
return wp + list(M ** (key - off) * V)[d - 1][0] | ||
wp = quo[n] | ||
while n: | ||
num = P((num * den(-x)).list()[n % 2::2]) |
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.
den(-x)
actually takes quite a bit of time to compute in practice with the current implementation, so you may want to do it only once.
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.
Thank you. I improved it. In my enviorment, the improvment was very small. I also tried
nden = P([v if i % 2 == 0 else -v for i, v in enumerate(den)])
But, it was slower.
Thank you! I found you teached this algorithm in your course :) By the way, I hope |
I added a reference and examples at the docstring of |
I haven't looked in detail at the rest of the code, but this seems likely, and yes, this would be nice to have. Let's wait for someone with the right permissions to approve the test workflows. (@fchapoton maybe?) |
Thank you! The implementation of Berlekamp--Massey in SageMath is available for integral domains because we can construct a fraction field from an integral domain. By the way, I notice that we need the middle product for implementing our MSB-first algorithm, which is used for the computation of slices. In general, algorithms obtained by the transposition usually requires the middle product. So, I hope the middle product will be implemented as a primitive for polynomial rings, etc. Of course, I can implement the algorithm for slices without the middle product although it includes the overhead of the constant factor. |
Documentation preview for this PR (built with commit 486c776; changes) is ready! π |
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.
Thank you!
Efficiency of `__getitem__` for CFiniteSequence is improved. Fixes sagemath#36763 <!-- ^^^^^ Please provide a concise, informative and self-explanatory title. Don't put issue numbers in there, do this in the PR body below. For example, instead of "Fixes sagemath#1234" use "Introduce new method to calculate 1+1" --> <!-- Describe your changes here in detail --> <!-- Why is this change required? What problem does it solve? --> <!-- If this PR resolves an open issue, please link to it here. For example "Fixes sagemath#12345". --> <!-- If your change requires a documentation PR, please link it appropriately. --> ### π Checklist <!-- Put an `x` in all the boxes that apply. --> <!-- If your change requires a documentation PR, please link it appropriately --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> <!-- Feel free to remove irrelevant items. --> - [x] The title is concise, informative, and self-explanatory. - [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 accordingly. ### β Dependencies <!-- List all open PRs that this PR logically depends on - sagemath#12345: short description why this is a dependency - sagemath#34567: ... --> <!-- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> URL: sagemath#36764 Reported by: Ryuhei Mori Reviewer(s): Marc Mezzarobba, Ryuhei Mori
Efficiency of
__getitem__
for CFiniteSequence is improved.Fixes #36763
π Checklist
β Dependencies