Skip to content

Commit 4719fe0

Browse files
author
Release Manager
committed
gh-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 #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 #12345". --> #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, --> <!-- - #12345: short description why this is a dependency --> <!-- - #34567: ... --> cc @tscrim URL: #39978 Reported by: Henry Wu Reviewer(s): David Coudert, Henry Wu, Travis Scrimshaw
2 parents fa4855f + 286871a commit 4719fe0

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/sage/matrix/matrix2.pyx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3610,6 +3610,42 @@ cdef class Matrix(Matrix1):
36103610
s += self.get_unsafe(i, j) * other.get_unsafe(j, i)
36113611
return s
36123612

3613+
def get_bandwidth(self):
3614+
"""
3615+
Return the bandwidth of ``self``, which is the maximum `i` such that
3616+
the `i` superdiagonal or subdiagonal contains a nonzero entry.
3617+
3618+
EXAMPLES::
3619+
3620+
sage: A = matrix([[1,0,0],[0,1,0],[0,0,1]]); A
3621+
[1 0 0]
3622+
[0 1 0]
3623+
[0 0 1]
3624+
sage: A.get_bandwidth()
3625+
0
3626+
3627+
sage: B = matrix([[1,2,3],[0,4,5],[0,0,6]]); B
3628+
[1 2 3]
3629+
[0 4 5]
3630+
[0 0 6]
3631+
sage: B.get_bandwidth()
3632+
2
3633+
3634+
sage: C = matrix(3, 2, range(6)); C
3635+
[0 1]
3636+
[2 3]
3637+
[4 5]
3638+
sage: C.get_bandwidth()
3639+
2
3640+
"""
3641+
cdef Py_ssize_t i
3642+
diag_range = max(self.nrows(), self.ncols()) - 1
3643+
3644+
for i in range(diag_range, 0, -1):
3645+
if any(self.diagonal(i)) or any(self.diagonal(-i)):
3646+
return ZZ(i)
3647+
return ZZ.zero()
3648+
36133649
#####################################################################################
36143650
# Generic Hessenberg Form and charpoly algorithm
36153651
#####################################################################################

0 commit comments

Comments
 (0)