Skip to content
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

ENH: diagonal banded form in solve banded #11344

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

JDkuba
Copy link
Contributor

@JDkuba JDkuba commented Jan 11, 2020

Reference issue

Closes #8362

What does this implement/fix?

It is helpful to have the possibility of passing a banded matrix in its original form (not only in diagonal ordered form as it is now).

Additional information

I needed this feature and I found the above issue, so I thought that it could be included in Scipy. There is an implementation given in #8362, but I've used my own implementation because it doesn't consume so much memory.

Example (matrix from solve_banded docs):

from scipy.linalg import solve_banded
ab = np.array([[5, 2, -1, 0, 0],
[1, 4, 2, -1, 0],
[0, 1, 3, 2, -1],
[0, 0, 1, 2, 2],
[0, 0, 0, 1, 1]])

b = np.array([0, 1, 2, 2, 3])
solve_banded((1, 2), ab, b, diagonal_form=False)

@tylerjereddy tylerjereddy added scipy.linalg enhancement A new feature or improvement labels Jan 11, 2020
Copy link
Contributor

@tylerjereddy tylerjereddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the CI failure is related. I added a few comments about the code, but a linalg regular will probably need to take a look.

raise ValueError("Matrix must be square (has shape %s)" % (a.shape,))
(nlower, nupper) = l_and_u
if nlower > n or nupper > n:
raise ValueError("Number of nonzero diagonals must be less than square dimension")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither this exception nor the one above are covered by unit tests are the moment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests updated :)


for i in range(nlower):
for j in range(n - i - 1):
lower[i, j] = a[i + j + 1, j]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just a longer-term thought, but if the linalg experts like this addition, I would suggest there's some opportunity for improved efficiency given three separate sets of Python loops, two of them nested. Maybe Cythonization at some point.

for j in range(n - i - 1):
lower[i, j] = a[i + j + 1, j]

return np.concatenate((upper, mid, lower))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's probably opportunity for efficiency improvement here too if you already know the shape of the final result, but that could be a future change since this is disabled by default in any case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rewrote code, and there is only one matrix now. Concatenation has been removed.

"""
Solve the equation a x = b for x, assuming a is banded matrix.

The matrix a is stored in `ab` using the matrix diagonal ordered form::
Until 'diagonal_form' is disabled, the matrix a is stored in `ab` using the matrix diagonal ordered form::
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe Until -> Unless could be slightly clearer? Was just thinking the former might imply mutability during the algorithm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@perimosocordiae
Copy link
Member

See also: gh-6331 and gh-2285.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement scipy.linalg
Projects
None yet
Development

Successfully merging this pull request may close these issues.

a function to convert a matrix into diagonal ordered form (ab) solve_banded
3 participants