[SciPy-User] solve_banded usage (once again)

Wagner Sebastian Sebastian.Wagner.fl at ait.ac.at
Mon Sep 2 11:10:22 EDT 2013


Dear Scipy-Users,

I'm currently working with Spare Matrices, in particular diagonal spare matrices. When searching for solve-Functions I found a function called solve_banded and some information about it in the docs (very little and that's why I searched for more), StackOverflow (http://stackoverflow.com/questions/12978518/scipy-sparse-dia-matrix-solver), a ticket on this problem (https://github.com/scipy/scipy/issues/2285) and this mailinglist. But nowhere I found any information about how to use it with a diagonal sparse matrix.

The mentioned thread on this is at http://thread.gmane.org/gmane.comp.python.scientific.user/10308, which states that "the docstring of solve_banded seems to be not correct/up-to-date, or at least unclear." Which is totally correct (the Mail is from 2007!) and until now nothing has changed.

Due to lack of documentation I tried to get it running, and got it running partly with contiuous offsets, like this way:

dia = scipy.sparse.dia_matrix(([[1,1,3,0,0],[1,1,1,1,1],[0,2,1,3,2]],[-2,0,1]), shape=(5,5))
offsets = [np.abs(dia.offsets[0]), dia.offsets[-1]]
data = np.flipud(dia.data)
scipy.linalg.solve_banded(offsets, data, b)

This gives be the same solutions as linalg.solve and sparse.linalg.spsolve.
But with uncontinuous offsets I couldn't, I thought of inserting a row of zeros for the missing diagonals:

dia = scipy.sparse.dia_matrix(([[1,1,3,0,0],[1,1,1,1,1],[0,2,1,3,2]],[-2,0,1]), shape=(5,5))
l = np.abs(dia.offsets[0])
u = dia.offsets[-1]
data = np.empty((l+u+1,dia.data.shape[1]),dtype=int)
missingCounter = 0
for i, d in enumerate(range(-l, u+1)):
if d in dia.offsets:
data[i] = dia.data[-(i+1-missingCounter)] # flip the array
else:
data[i] = np.zeros(dia.data.shape[1])
missingCounter += 1

For me it looks fine, but it gives not the same answer as linalg.solve and sparse.linalg.spsolve.

Can anybody give me an advice of how to use solve_banded correctly?

Regards,
Sebastian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20130902/d81bc308/attachment.html>


More information about the SciPy-User mailing list