[SciPy-User] create sparse matrix from diagonal arrays

Darcoux Christine bouloumag at gmail.com
Mon Apr 14 08:36:52 EDT 2014


Le vendredi 11 avril 2014, Tony Stillfjord <tony at maths.lth.se> a écrit :

> On Fri, Apr 11, 2014 at 4:53 AM, Geordie McBain <gdmcbain at freeshell.org<javascript:_e(%7B%7D,'cvml','gdmcbain at freeshell.org');>
> > wrote:
>
>> 2014-04-11 12:00 GMT+10:00 Darcoux Christine <bouloumag at gmail.com<javascript:_e(%7B%7D,'cvml','bouloumag at gmail.com');>
>> >:
>> > I would like to solve a PDE using an implicit finite volume
>> discretization.
>> > Since the problem is 3D, the matrix should be septadiagonal. I have
>> computed
>> > the 7 diagonals (for top, bottom, east, west, north south and central
>> point)
>> > in 7 different arrays.
>> >
>> > Now, I would like to create a sparse matrix object from these arrays,
>> but I
>> > feel a little lost with the documentation of scipy.sparse.spdiags.
>> >
>> > Help would be greatly appreciated.
>>
>> Put the diagonals, padded to the length of the main diagonal
>> (postpadded for the subdiagonals and prepadded for the superdiagonals,
>> it doesn't matter what with) as the rows of an array, or elements of a
>> list or other iterable.
>>
>> For example, to generate the classical tridiagonal one-dimensional
>> Laplacian:
>>
>> >>> spdiags([[1, 1, np.nan], [-2, -2, -2], [np.nan, 1, 1]], [-1, 0, 1],
>> 3, 3).toarray()
>> array([[-2.,  1.,  0.],
>>        [ 1., -2.,  1.],
>>        [ 0.,  1., -2.]])
>>
>
>
> Or if you don't want to pad your diagonals, use the scipy.sparse.diags
> function instead. It was included in 0.11.0. Then you would have something
> like
>
> diags([top, bottom, east, west, north, south, central], [i0, i1, i2, i3,
> i4, i5, i6])
>
> where the i's are the corresponding diagonal offsets. For example, i6
> would be 0.
>
> Simple example:
>
> >>> e0 = array([1, 2, 3])
> >>> e1 = array([4, 5])
> >>> e2 = array([6])
> >>> diags([e0, e1, e2], [0, 1, -2]).toarray()
> array([[ 1.,  4.,  0.],
>        [ 0.,  2.,  5.],
>        [ 6.,  0.,  3.]])
>
> Tony
>

I have found the stencil_grid function in pyamg, which is exactly what I
need.

BTW, it would be nice if scipy could include a similar function.

Christine
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20140414/d7e78d40/attachment.html>


More information about the SciPy-User mailing list