-
-
Notifications
You must be signed in to change notification settings - Fork 652
Added a bandwidth feature #39978
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
Added a bandwidth feature #39978
Conversation
Documentation preview for this PR (built with commit 2b47e90; changes) is ready! 🎉 |
The order of operations could be improved by visiting the matrix by diagonals and stop as soon as a non zero value is found. it is best to visit simultaneously the upper and the lower diagonals. I'm not sure that the method should be called |
@dcoudert For the namesake, I wasn't careful to see that there is a method with the same name but different behavior implemented elsewhere. In that case, naming it the same would not be appropriate, will change to As for using So to clarify what you proposed, you're saying if I go through every diagonal starting from the corner to the main diagonal via |
I don't think it's necessary.
Precisely and this is what was proposed in the original patch #13565. The idea is to stop computation as soon as possible. This should be an improvement for large matrix. |
@dcoudert Hi so I've adjusted the |
I don't think this will work for non-square matrices. You should add a test for this case. As such, I think it is better to split it into two separate loops. This way to can cleanly break out of each one and can account for the difference in the rows and columns. Two minor points: (1) Please include the definition in the docstring. (2) |
@tscrim Hi a non-square matrix doctest is added, and I've fixed some minor bugs in the loop. They should work fine for both square and non-square now. Also adjusted for the empty line under |
I have a doubt regarding the definition of bandwidth used in this PR. According the wikipedia page https://en.wikipedia.org/wiki/Band_matrix, it is the maximum of |
That's a small technicality which can be easily changed. But it was by my search the sum of both. I'll look more into the page and get back on this issue. |
if it's a max, you can do: for i in range(diag_range, 0, -1):
if any(x != zero for x in self.diagonal(i)):
max_up = i
break
for i in range(diag_range, max_up, -1):
if any(x != zero for x in self.diagonal(-i)):
return i
return max_up |
@dcoudert It would seem my initial search was misguided. I've changed the code and doctest accordingly |
May be we can do even better like this: for i in range(diag_range, 0, -1):
if (any(x != zero for x in self.diagonal(i))
or any(x != zero for x in self.diagonal(-i))):
return i
return 0 |
@dcoudert Yes, that would significantly reduce the amount of code needed. I've pushed the latest version and tested out to make sure it works with past test cases. |
I'm not sure what you propose. |
@dcoudert I'm fine with your last suggestion, and have adjusted and pushed the code accordingly. I guess I was more talking about the coding style that I'm personally used to. But computation time wise, I think currently there's no other improvement. If it aligns with sage requirement, I'm happy with the status quo. |
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. Just another small tweak suggestion from me.
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.
Two other minor things.
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.
LGTM.
simple an elegant code.
sagemathgh-39978: Added a bandwidth feature <!-- ^ 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". --> sagemath#13565 Added a bandwidth feature that gives the bandwidth of a matrix. The bandwidth of a matrix measures how far from the main diagonal the nonzero entries extend. Mostly used for storage efficiency / reordering to speed up solvers etc. For now it is just a new feature to add. ### 📝 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. - [x] 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: ... --> cc @tscrim URL: sagemath#39978 Reported by: Henry Wu Reviewer(s): David Coudert, Henry Wu, Travis Scrimshaw
sagemathgh-39978: Added a bandwidth feature <!-- ^ 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". --> sagemath#13565 Added a bandwidth feature that gives the bandwidth of a matrix. The bandwidth of a matrix measures how far from the main diagonal the nonzero entries extend. Mostly used for storage efficiency / reordering to speed up solvers etc. For now it is just a new feature to add. ### 📝 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. - [x] 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: ... --> cc @tscrim URL: sagemath#39978 Reported by: Henry Wu Reviewer(s): David Coudert, Henry Wu, Travis Scrimshaw
sagemathgh-39978: Added a bandwidth feature <!-- ^ 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". --> sagemath#13565 Added a bandwidth feature that gives the bandwidth of a matrix. The bandwidth of a matrix measures how far from the main diagonal the nonzero entries extend. Mostly used for storage efficiency / reordering to speed up solvers etc. For now it is just a new feature to add. ### 📝 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. - [x] 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: ... --> cc @tscrim URL: sagemath#39978 Reported by: Henry Wu Reviewer(s): David Coudert, Henry Wu, Travis Scrimshaw
#13565
Added a bandwidth feature that gives the bandwidth of a matrix. The bandwidth of a matrix measures how far from the main diagonal the nonzero entries extend. Mostly used for storage efficiency / reordering to speed up solvers etc. For now it is just a new feature to add.
📝 Checklist
⌛ Dependencies
cc @tscrim